· 4 years ago · May 01, 2021, 03:14 AM
1term.clear()
2term.setCursorPos(1,1)
3-- it's like print() but center (and maybe better)
4local w,h = term.getSize()
5function printCenter(y,s)
6 local x = math.floor(w-string.len(s)) /2
7 term.setCursorPos(x,y)
8 term.clearLine()
9 term.write(s)
10end
11
12
13-- get settings (if they don't exist, it sets it to nil)
14local sRoutes = settings.get("sRoutes")
15
16-- load APIs
17if not fs.exists("s.lua") then
18 print("installing APIs...")
19 term.setCursorPos(1,2)
20 shell.run("pastebin get APF0HTE0 s.lua")
21 sleep(2)
22 os.reboot()
23end
24os.loadAPI("s.lua")
25-- greet user if dispatcher hasn't been run already
26if not sRoutes then
27 printCenter(1, "\\\\ Welcome to Enlight3ned's auto dispatcher! //")
28 printCenter(2, "Since this is your first time setting up")
29 printCenter(3, "you will need to enter some info")
30 printCenter(4, "press enter to continue")
31 repeat
32 local _, choice = os.pullEvent("key")
33 until choice == keys.enter
34 end
35-- new route function
36
37local function newRoute(oldRoutes)
38 -- set variables
39 local routes = {} -- table made of route tables
40 local route = {} -- route table made with stops table and destin
41 local stops = {} -- table of stops
42 -- clear term and stuff
43 term.clear()
44 term.setCursorPos(1,1)
45 -- get number of stops
46 print("How many junctions/pass-throughs does the train go\nthrough to get to the destination?\n(including the destination)")
47 local numStops = tonumber(read())
48 term.clear()
49 term.setCursorPos(1,1)
50 print("Now you will enter all of the junctions/pass-throughs\nThe order doesn't matter as long as\nthe destination is last")
51 -- create table using stops table
52 for i=1,numStops do
53 -- ask for stops
54 if i == numStops then
55 print("What is the final destination?")
56 else
57 print("What is junction/pass-through "..i.."?")
58 end
59 stops[i] = read()
60 end
61 -- ask for destination after the last stop
62 print("After arriving, what is the next destination?\nuse \"term\" if it terminates")
63 local destin = read()
64 -- create the table bro
65 route = {stops = stops, destin = destin}
66
67 -- now add to the existing routes table back (if it exists)
68 if oldRoutes then
69 -- put the old routes (if they exist) back into the routes table
70 for i=1, #oldRoutes do
71 table.insert(routes, i, oldRoutes[i])
72 end
73 -- then add the new route
74 table.insert(routes, #oldRoutes + 1, route)
75 -- but what if there is no oldRoutes? O:
76 elseif not oldRoutes then
77 table.insert(routes, 1, route)
78 -- (what where you expecting? Im not some hackerman)
79 end
80 -- (cowboy accent) serialize damnit (thx Wojbie)
81 local sRoutes = s.serializeRec(routes)
82 -- set and save
83 settings.set("sRoutes", sRoutes)
84 settings.save(".settings")
85 os.reboot() -- "goodbye"
86end
87
88if not sRoutes then
89 newRoute(nil)
90end
91
92local routes = textutils.unserialize(sRoutes)
93print(textutils.serialize(routes[1]))
94--draw the UI
95local function drawSelectionUI(onVal)
96 term.clear()
97 printCenter(1, "Please Select destination:")
98 for i=1,#routes do
99 if i == onVal then
100 printCenter(math.floor(h/2)-7 + i,"[ "..routes[i]["stops"][#routes[i]["stops"]].." ]")
101 else
102 printCenter(math.floor(h/2)-7 + i, routes[i]["stops"][#routes[i]["stops"]])
103 end
104 end
105 if onVal == #routes + 1 then
106 printCenter(math.floor(h/2)-7 + #routes+1, "[ New Route ]")
107 else
108 printCenter(math.floor(h/2)-7 + #routes+1, "New Route")
109 end
110end
111
112-- now get the user to select what they want to do
113local userSet = 1
114local function userSelection()
115 while true do
116 drawSelectionUI(userSet)
117 local _, key = os.pullEvent("key")
118
119 if key == keys.down then -- down arrow
120 userSet = userSet + 1
121 elseif key == keys.up then -- up arrow
122 userSet = userSet - 1
123 elseif key == keys.enter then -- enter
124 return userSet
125 end
126
127 if userSet > #routes + 1 then
128 userSet = 1
129 elseif userSet < 1 then
130 userSet = #routes + 1
131 end
132 end
133end
134local userChoice = userSelection()
135if userChoice == #routes + 1 then
136 newRoute(routes)
137else
138 term.clear()
139 printCenter(math.floor(h/2), routes[userChoice]["stops"][#routes[userChoice]["stops"]].." chosen")
140end
141sleep(3)
142os.reboot()
143
144
145
146