· 4 years ago · Dec 16, 2020, 04:36 AM
1local version = "0.01"
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 -- Remove a string from a table
32 local function purge_string(local_string, local_table)
33 for key, value in local_table do
34 if value == local_string then
35 table.remove(local_table, key)
36 return local_table
37 end
38 end
39end
40
41
42-- User input checker, iterates through
43-- target input list to see if its there
44local function verify_input(local_called_input, local_called_target)
45 for key, value in pairs(local_called_target) do
46 if value == local_called_input then
47 return 1
48 end
49 end
50 return 0
51end
52
53
54-- Check to see if a statefile exists
55local function statefile_checker()
56 local statefile_name = "statefile.txt"
57 while true do
58 if fs.exists(statefile_name) == true then
59 clear()
60 skip_line()
61 print("A pre-existing statefile has been found...")
62 skip_line()
63 print("Loading this statefile will resume previous function. Press 1 to use this statefile or 2 to start fresh.")
64 local local_input = tonumber(prep_input("Selection"))
65 local local_tgt_input = {1, 2}
66 if verify_input(local_input, local_tgt_input) == 1 then
67 if local_input == 1 then
68 -- Keep the old statefile
69 break
70 else
71 -- Create a new statefile
72 fs.delete(statefile_name)
73 statefile_setup()
74 end
75 else
76 print("Invalid input.")
77 end
78 else
79 print("Statefile not found, querying user")
80 statefile_setup()
81 end
82 end
83end
84
85
86-- Perform first time setup if statefile missing
87function statefile_setup()
88 file = fs.open("statefile.txt", "w")
89 face_array = {"top", "bottom", "left", "right", "front", "back"}
90 while true do
91 clear()
92 skip_line()
93 print("ccController has not be set up on this computer yet")
94 skip_line()
95 print("How many redstone faces are needed?")
96 local_input = prep_input("Number of faces")
97 local_input = tonumber(local_input)
98 local_tgt_input = {1, 2, 3, 4, 5, 6}
99 if verify_input(local_input, local_tgt_input) == 1 then
100 break
101 end
102 end
103 for i = 1, local_input do
104 -- Get the name of the function from the user and validate
105 while true do
106 clear()
107 skip_line()
108 print("Face "..i.." configuration, part 1/3")
109 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'")
110 skip_line()
111 print("MUST BE UNDER 35 CHARS, ABBREVIATE IF NECESSARY. COMPUTERCRAFT SCREENS ARE SMALL!")
112 face_function_name = tostring(prep_input("Function Name"))
113 if string.len(face_function_name) <= 35 then
114 break
115 end
116 end
117 -- Get the side of the computer for the function and validate
118 local_step_bool = false
119 while local_step_bool == false do
120 clear()
121 skip_line()
122 print("Face "..i.." configuration, part 2/3")
123 print("Designated: "..face_function_name)
124 skip_line()
125 print("Select the side that will control the redstone")
126 skip_line()
127 term.write("Options: ")
128 for key, value in pairs(face_array) do
129 term.write(value.." ")
130 end
131 local_input = tostring(prep_input("Desired Face"))
132 for key, value in pairs(face_array) do
133 if local_input == value then
134 face_function_side = local_input
135 -- Remove the side from the list of sides
136 face_array = purge_string(local_input, face_array)
137 local_step_bool = true
138 end
139 end
140 end
141 -- Get the on/off state of the redstone and validate
142 while true do
143 clear()
144 skip_line()
145 print("Face "..i.."configuration, part 3/3")
146 print("Designated: "..face_function_name)
147 print("Side: "..face_function_side)
148 skip_line()
149 print("Select the redstone state")
150 skip_line()
151 print("Is the function on when the redstone is signal on, or off?")
152 print("Simply enter on or off")
153 local_input = prep_input("Desired State Setting: ")
154 local_input = string.lower(local_input)
155 if local_input == "on" or local_input == "off" then
156 face_function_state = local_input
157 break
158 end
159 end
160 -- Write that shit to the save file
161 file.writeLine("func="..i.."/side="..face_function_state.."/state="..face_function_state.."/name="..face_function_name)
162 end
163 -- Close the file to save the data. Setup is complete
164 file.close()
165end
166
167-- Bullshit text file dumper
168
169local function bullshit()
170 local file = fs.open("statefile.txt", "r")
171 local text = file.readAll()
172 print(text)
173end
174
175-- Main loop
176
177statefile_checker()
178
179bullshit()