· 4 years ago · May 06, 2021, 07:52 PM
1term.clear()
2term.setCursorPos(1,1)
3rednet.open("top")
4-- it's like print() but center (and maybe better)
5local w,h = term.getSize()
6function printCenter(y,s)
7 local x = math.floor(w-string.len(s)) /2
8 term.setCursorPos(x,y)
9 term.clearLine()
10 term.write(s)
11end
12function slowPrintCenter(y,s)
13 local x = math.floor(w-string.len(s)) /2
14 term.setCursorPos(x,y)
15 term.clearLine()
16 textutils.slowWrite(s)
17end
18
19-- get settings (if they don'event exist, it sets it to nil)
20local sRoutes = settings.get("sRoutes")
21local dSettings = settings.get("dSettings")
22-- load APIs
23if fs.exists("s.lua") == false then
24 print("installing APIs...")
25 term.setCursorPos(1,2)
26 shell.run("pastebin get APF0HTE0 s.lua")
27 sleep(2)
28 os.reboot()
29end
30os.loadAPI("s.lua")
31-- greet user if dispatcher hasn'event been run already
32if not dSettings then
33 printCenter(1, "\\\\ Welcome to Enlight3ned's auto dispatcher! //")
34 printCenter(2, "Since this is your first time setting up")
35 printCenter(3, "you will need to enter some info to setup")
36 printCenter(4, "press enter to continue")
37 repeat
38 local _, choice = os.pullEvent("key")
39 until choice == keys.enter
40 term.clear()
41 term.setCursorPos(1,1)
42 print("What is the Tagger ID? This is shown when you boot one up")
43 taggerID = tonumber(read())
44 term.clear()
45 term.setCursorPos(1,1)
46 print("Now go to your tagger and set the ID to: "..os.getComputerID())
47 print("press enter to continue")
48 repeat
49 local _,choice = os.pullEvent("key")
50 until choice == keys.enter
51 term.clear()
52 term.setCursorPos(1,1)
53 settings.set("dSettings", {taggerID = taggerID})
54 settings.save(".settings")
55 os.reboot()
56end
57-- new route function
58local function newRoute(oldRoutes)
59 -- set variables
60 local routes = {} -- table made of route tables
61 local route = {} -- route table made with stops table, queued destin, and route name
62 local stops = {} -- table of stops
63 local eta
64 -- clear term and stuff
65 term.clear()
66 term.setCursorPos(1,1)
67 -- get the route name
68 print("What would you like to call this route?")
69 local routeName = read()
70 term.clear()
71 term.setCursorPos(1,1)
72 -- get number of stops
73 print("How many junctions/pass-throughs does the train go\nthrough to get to the stop?\n(including the stop)")
74 local numStops = tonumber(read())
75 -- create table using stops table
76 for i=1,numStops do
77 term.clear()
78 term.setCursorPos(1,1)
79 -- ask for stops
80 if i == numStops then
81 print("What is the name of the stop?")
82 else
83 print("What is junction/pass-through "..i.."?")
84 end
85 stops[i] = read()
86 end
87 -- ask for destination after the stop
88 term.clear()
89 term.setCursorPos(1,1)
90 print("Would you like to set the ETA to get to the stop?")
91 print("[Y/N]")
92 repeat
93 local _,choice = os.pullEvent("char")
94 if choice == "y" then
95 print("What is the ETA in minutes?")
96 eta = tonumber(read())
97 elseif choice == "n" then
98 eta = "not set"
99 end
100 until choice == "y" or choice == "n"
101
102
103
104 -- create the table bro
105 route = {stops = stops, routeName = routeName, eta = eta}
106
107 -- now add to the existing routes table back (if it exists)
108 if oldRoutes then
109 -- put the old routes (if they exist) back into the routes table
110 for i=1, #oldRoutes do
111 table.insert(routes, i, oldRoutes[i])
112 end
113 -- then add the new route
114 table.insert(routes, #oldRoutes + 1, route)
115 -- but what if there is no oldRoutes? O:
116 elseif not oldRoutes then
117 table.insert(routes, 1, route)
118 -- (what where you expecting? Im not some hackerman)
119 end
120 -- (cowboy accent) serialize damnit (thx Wojbie)
121 local sRoutes = s.serializeRec(routes)
122 -- set and save
123 settings.set("sRoutes", sRoutes)
124 settings.save(".settings")
125 os.reboot() -- "goodbye"
126end
127
128if not sRoutes then
129 newRoute(nil)
130end
131
132local routes = textutils.unserialize(sRoutes)
133--draw the UI
134local function drawSelectionUI(onVal)
135 term.clear()
136 printCenter(1, "Please Select Route:")
137 for i=1,#routes do
138 if i == onVal then
139 printCenter(math.floor(h/2)-7 + i,"[ "..routes[i]["routeName"].." ]")
140 else
141 printCenter(math.floor(h/2)-7 + i, routes[i]["routeName"])
142 end
143 end
144 if onVal == #routes + 1 then
145 printCenter(math.floor(h/2)-7 + #routes+1, "[ New Route ]")
146 else
147 printCenter(math.floor(h/2)-7 + #routes+1, "New Route")
148 end
149end
150
151-- now get the user to select what they want to do
152local userSet = 1
153local function logic()
154 while true do
155 drawSelectionUI(userSet)
156 local _, key = os.pullEvent("key")
157 -- user input
158 if key == keys.down then -- down arrow
159 userSet = userSet + 1
160 elseif key == keys.up then -- up arrow
161 userSet = userSet - 1
162 elseif key == keys.enter then -- enter
163 return userSet
164 end
165
166 if userSet > #routes + 1 then
167 userSet = 1
168 elseif userSet < 1 then
169 userSet = #routes + 1
170 end
171 end
172end
173
174local userChoice = logic()
175local trainTag = {}
176local trainCargo
177if userChoice == #routes + 1 then
178 newRoute(routes)
179else
180 -- print chosen destination
181 term.clear()
182 term.setCursorPos(1,1)
183 print(routes[userChoice]["routeName"].." chosen")
184 -- Ask for train contents
185 sleep(2)
186 print("What is the train's contents?")
187 trainCargo = read()
188end
189-- create trainTag (kinda hard ngl)
190local trainTag = {
191 trainStops = routes[userChoice]["stops"],
192 trainCargo = trainCargo,
193 trainETA = routes[userChoice]["eta"],
194 trainID = os.time(),
195}
196print(textutils.serialize(trainTag))
197-- serialize trainTag to put on train
198local sTrainTag = textutils.serialize(trainTag)
199
200-- now send tag to detector augment using rednet
201
202print("Waiting for Tagger "..dSettings["taggerID"])
203rednet.open("top")
204while true do
205 rednet.send(dSettings["taggerID"], "Ready?")
206 local _,ready = rednet.receive(1)
207 if ready == "Ready" then
208 break
209 end
210end
211print("Tagger Ready")
212sleep(1)
213term.clear()
214term.setCursorPos(1,1)
215rednet.send(dSettings["taggerID"], sTrainTag)
216print("sent tag to tagger")
217-- wait for train to depart
218sleep(1)
219print("Waiting for train to depart...")
220repeat
221 local x, trainDeparted = rednet.receive(1)
222until x == dSettings["taggerID"] and trainDeparted == "Train has departed"
223-- send info to stations
224print("train has departed, sending info to stations...")
225-- create a table for stations to receive
226local stationDepartedTrainInfo = {
227 departedTrainCargo = trainCargo,
228 departedTrainTo = routes[userChoice]["stops"],
229 departedTrainETA = routes[userChoice]["eta"],
230 departedTrainID = trainTag["trainID"],
231}
232sleep(1)
233rednet.broadcast(stationDepartedTrainInfo)
234print(textutils.serialize(stationDepartedTrainInfo))
235print("sent, rebooting...")
236sleep(5)
237os.reboot()