· 4 years ago · Dec 16, 2020, 08:16 AM
1local version = "0.02"
2-- Script by Kotevski
3
4
5-- Skips a single line
6local function skip_line()
7 print(" ")
8end
9
10
11-- Takes user input in a fancy way
12local function prep_input(prompt)
13 term.setCursorPos(1, 18)
14 term.write("--------------------------------------------------")
15 term.setCursorPos(1, 19)
16 term.write(prompt..": ")
17 response = read()
18 return response
19 end
20
21-- Clear the screen and reset the cursor position
22local function clear()
23 term.setBackgroundColor(colours.black)
24 term.clear()
25 term.setCursorPos(1,1)
26 print("ccController v"..version.." by Kotevski")
27 skip_line()
28end
29
30
31-- Really stupid shit added for asthetics
32local function pretty()
33 sleep(.005)
34end
35
36
37-- Remove a string from a table
38local function purge_string(local_called_string, local_called_table)
39 for key, value in pairs(local_called_table) do
40 if value == local_called_string then
41 table.remove(local_called_table, key)
42 return local_called_table
43 end
44 end
45end
46
47
48-- User input checker, iterates through
49-- target input list to see if its there
50local function verify_input(local_called_input, local_called_target)
51 for key, value in pairs(local_called_target) do
52 if value == local_called_input then
53 return 1
54 end
55 end
56 return 0
57end
58
59
60-- Split function, lua doesn't have one native
61function split(pString, pPattern)
62 local Table = {} -- NOTE: use {n = 0} in Lua-5.0
63 local fpat = "(.-)" .. pPattern
64 local last_end = 1
65 local s, e, cap = pString:find(fpat, 1)
66 while s do
67 if s ~= 1 or cap ~= "" then
68 table.insert(Table,cap)
69 end
70 last_end = e+1
71 s, e, cap = pString:find(fpat, last_end)
72 end
73 if last_end <= #pString then
74 cap = pString:sub(last_end)
75 table.insert(Table, cap)
76 end
77 return Table
78end
79
80-- Statefile unpacker, the worst function here
81local function unpack_statefile()
82 file = fs.open("statefile.txt", "r")
83 -- Dump every line into an array and then split
84 -- This is super shitty don't judge me i'm tired
85 func_1_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
86 func_2_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
87 func_3_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
88 func_4_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
89 func_5_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
90 func_6_dict = {["Number"] = nil, ["Side"] = nil, ["State"] = nil, ["Name"] = nil}
91 func_array = {func_1_dict, func_2_dict, func_3_dict, func_4_dict, func_5,dict, func_6_dict}
92 line_number = 0
93 clear()
94 skip_line()
95 print("Opening statefile...")
96 pretty()
97 for line in file.readLine do
98 print("Reading line...")
99 pretty()
100 line_number = line_number+1
101 split1 = split(line, "/")
102 for key, value in pairs(split1) do
103 print("Splitting line terms...")
104 pretty()
105 split2 = split(split1[key], "=")
106 print("Assigning terms...")
107 pretty()
108 if key == 1 then
109 func_array[line_number]["Number"] = split2[2]
110 elseif key == 2 then
111 func_array[line_number]["Side"] = split2[2]
112 elseif key == 3 then
113 func_array[line_number]["State"] = split2[2]
114 elseif key == 4 then
115 func_array[line_number]["Name"] = split2[2]
116 end
117 end
118 end
119 clear()
120 skip_line()
121 print("Unpacking complete")
122 skip_line()
123 for key, value in pairs(func_array) do
124 if value["Name"] ~= nil then
125 print("Function: "..value["Number"])
126 print("Name: "..value["Name"])
127 print("Side: "..value["Side"])
128 print("State: "..value["State"])
129 print(" ")
130 end
131end
132file.close()
133end
134
135
136-- Check to see if a statefile exists
137local function statefile_checker()
138 local statefile_name = "statefile.txt"
139 while true do
140 if fs.exists(statefile_name) == true then
141 clear()
142 skip_line()
143 print("A pre-existing statefile has been found...")
144 skip_line()
145 print("Loading this statefile will resume previous function. Press 1 to use this statefile or 2 to start fresh.")
146 local_input = tonumber(prep_input("Selection"))
147 local_tgt_input = {1, 2}
148 if verify_input(local_input, local_tgt_input) == 1 then
149 if local_input == 1 then
150 -- Keep the old statefile
151 break
152 else
153 -- Create a new statefile
154 fs.delete(statefile_name)
155 statefile_setup()
156 end
157 else
158 print("Invalid input.")
159 end
160 else
161 print("Statefile not found, querying user")
162 state = statefile_setup()
163 if state == 1 then
164 break
165 end
166 end
167 end
168end
169
170
171
172
173-- Perform first time setup if statefile missing
174function statefile_setup()
175 file = fs.open("statefile.txt", "w")
176 face_table = {"top", "bottom", "left", "right", "front", "back"}
177 while true do
178 clear()
179 skip_line()
180 print("ccController has not be set up on this computer yet")
181 skip_line()
182 print("How many redstone faces are needed?")
183 local_input = tonumber(prep_input("Number of faces"))
184 face_target = local_input
185 local_tgt_input = {1, 2, 3, 4, 5, 6}
186 if verify_input(local_input, local_tgt_input) == 1 then
187 break
188 end
189 end
190 for i = 1, face_target do
191 -- Get the name of the function from the user and validate
192 while true do
193 clear()
194 skip_line()
195 print("Face "..i.."/"..face_target.." configuration, part 1/3")
196 skip_line()
197 print("Select the name of the function. This is the name that will be broadcasted to the entire network so please make it intuitive as to what it does, like 'Sieve gravel feed' or 'Main door'")
198 skip_line()
199 print("MUST BE UNDER 35 CHARS, ABBREVIATE IF NECESSARY. COMPUTERCRAFT SCREENS ARE SMALL!")
200 face_function_name = tostring(prep_input("Function Name"))
201 if string.len(face_function_name) <= 35 then
202 break
203 end
204 end
205 -- Get the side of the computer for the function and validate
206 local_step_bool = false
207 while local_step_bool == false do
208 clear()
209 skip_line()
210 print("Face "..i.."/"..face_target.." configuration, part 2/3")
211 skip_line()
212 print("Designated: "..face_function_name)
213 skip_line()
214 print("Select the side that will control the redstone")
215 skip_line()
216 term.write("Options: ")
217 for key, value in pairs(face_table) do
218 term.write(value.." ")
219 end
220 local_input = tostring(prep_input("Desired Face"))
221 for key, value in pairs(face_table) do
222 if local_input == value then
223 face_function_side = local_input
224 -- Remove the side from the list of sides
225 face_table = purge_string(local_input, face_table)
226 local_step_bool = true
227 end
228 end
229 end
230 -- Get the on/off state of the redstone and validate
231 while true do
232 clear()
233 skip_line()
234 print("Face "..i.."/"..face_target.." configuration, part 3/3")
235 skip_line()
236 print("Designated: "..face_function_name)
237 print("Side: "..face_function_side)
238 skip_line()
239 print("Select the redstone state")
240 skip_line()
241 print("Is the function on when the redstone is signal on, or off?")
242 skip_line()
243 print("Simply enter on or off")
244 local_input = prep_input("Desired State Setting")
245 local_input = string.lower(local_input)
246 if local_input == "on" or local_input == "off" then
247 face_function_state = local_input
248 break
249 end
250 end
251 -- Write that shit to the save file
252 file.writeLine("func="..i.."/side="..face_function_side.."/state="..face_function_state.."/name="..face_function_name)
253 end
254 -- Close the file to save the data. Setup is complete
255 file.close()
256 return 1
257end
258
259
260statefile_checker()
261
262unpack_statefile()