· 3 years ago · Feb 10, 2022, 08:10 PM
1function pickTargets(targetList, targetWanted) -- given a list of targets, finds targets of the desired type from said list.
2 targets = {}
3 for k,v in pairs(targetList) do
4 if v.RawName == targetWanted
5 then
6 table.insert(targets,k)
7 end
8 end
9 return targets
10end
11
12function printInColumns(row) -- prints out a of data in equal sized columns. "row" must be a table of strings.
13 w,h = term.getSize()
14 colSize = (w / (#row))
15 x, y = term.getCursorPos()
16 for i = 1,#row do
17 term.setCursorPos(((i - 1) * colSize + 1),y)
18 term.write(row[i])
19 end
20 print()
21end
22
23function validateTargets(targetList, sensorUsed, printResults) -- Checks that desired targets actually exist. If not, removes them from the returned list. Assumes target names (in OCS sens) are table key, anything in V is fluff to be passed through.
24 printResults = printResults or true
25 validTargetList = {}
26 for k,v in pairs(targetList) do
27 reading = sensorUsed.getTargetDetails(k)
28 if reading == nil
29 then
30 if printResults
31 then
32 print("Target at " .. k .. " does not exist. Removing.")
33 end
34 else
35 if printResults
36 then
37 print("Valid target found at " .. k)
38 end
39 validTargetList[k] = v
40 end
41 end
42 return validTargetList
43end
44
45function printPairs(tableToPrint) -- Ronseal
46 for k, v in pairs(tableToPrint) do
47 print(k .. " , " .. v)
48 end
49end
50
51function getPeripherals() -- Finds one of each connected peripheral. Adapted from the buggy version included with ccSensors.
52-- WARNING: Will only feed back the last discovered of each peripheral. Do not use this if you wish to use multiple monitors or sensors!
53 local dict = {sensor=nil,monitor=nil,modem=nil,pipe=nil,ITank=nil}
54 local sides = rs.getSides()
55 for i=1,#sides do
56 if peripheral.isPresent( sides[i] ) then
57 if peripheral.getType( sides[i]) == "SensorController" then
58 dict["sensor"]=sides[i]
59 elseif peripheral.getType( sides[i]) == "monitor" then
60 dict["monitor"]=sides[i]
61 term.redirect(peripheral.wrap(sides[i]))
62 print("Monitor found by getPeripherals")
63 term.restore()
64 elseif peripheral.getType( sides[i]) == "modem" then
65 dict["modem"]=sides[i]
66 rednet.open(sides[i])
67 elseif peripheral.getType( sides[i]) == "" then -- turns out this is a bug, and it should return something like "LogisticsPipes:Request" - depends on order of placing
68 dict["pipe"]=sides[i]
69 print("Pipe found by getPeripherals")
70 elseif peripheral.getType( sides[i]) == "rcirontankvalvetile" then
71 dict["ITank"]=sides[i]
72 print("Iron Tank found by getPeripherals")
73 else
74 dict[peripheral.getType( sides[i])] = sides[i]
75 -- guess this will find printers etc.
76 end
77 end
78 end
79 return dict
80end
81
82function findAllPeripherals(filter) -- Finds all networked or local peripherals whose type matches filter (if used). Returns a table of index side and value a wrapped peripheral
83 locs = peripheral.getNames()
84 periphs = {}
85 for k,v in pairs(locs) do
86 if filter ~= nil
87 then
88 if (peripheral.getType(v):find(filter)) ~= nil
89 then
90 periphs[v] = peripheral.wrap(v)
91 end
92 else
93 periphs[v] = peripheral.wrap(v)
94 end
95 end
96 return periphs
97end
98
99function pulseCable(side, colour) -- turns on the wires of a designated colour for 0.2 sec, then turns them off again. Note - assumes that said signals were low in the first place
100 initalState = rs.getBundledOutput(side) -- get the current cable state
101 rs.setBundledOutput(side, colours.combine(initalState, colour))
102 sleep(0.2)
103 rs.setBundledOutput(side, initalState)
104 sleep(0.2)
105end
106
107function cableOn(side, colour) -- turns on wires of designated colours
108 initalState = rs.getBundledOutput(side) -- get the current cable state
109 --print("inital colours:" .. initalState )
110 --print("requested colour: " .. colour)
111 newState = colours.combine(initalState, colour)
112 --print("new colours: " .. newState)
113 rs.setBundledOutput(side, newState)
114 sleep(0.2)
115end
116
117function cableOff(side, colour) -- turn off a certain colour wire
118 initalState = rs.getBundledOutput(side) -- get the current cable state
119 rs.setBundledOutput(side, colours.subtract(initalState, colour))
120 sleep(0.2)
121end
122
123function cableCheck(side, colour) -- Is a certain colour wire on?
124 if colours.test(rs.getBundledInput(side), colour) then
125 return true
126 else
127 return false
128 end
129end
130
131function menu(menuItems, menuHeading) -- Runs a menu for the user to interact with. menuItems should be a table of all the objects in the menu, in top to bottom order. Returns index of selected menu item.
132 --ToDo - make this SCROLL
133 w, h = term.getSize()
134 selectedItem = 1
135
136 firstItem = 1
137 if #menuItems < (h - 2)
138 then
139 lastItem = #menuItems
140 startLine = (h/2) - (#menuItems/2) - 1
141 else
142 lastItem = (h-2)
143 startLine = 1
144 end
145 while true do
146 term.clear()
147 if menuHeading ~= nil
148 then
149 term.setCursorPos((w/2)-(#menuHeading/2), startLine)
150 term.write(menuHeading)
151 end
152 j = 1
153 for i = firstItem, lastItem do -- draw the menu in the middle
154 toWrite = menuItems[i].text or menuItems[i]
155 if i == selectedItem -- If the current item is the selected one, make it obvious to the user
156 then
157 toWrite = "=" .. toWrite .. "="
158 end
159 term.setCursorPos((w/2)-((#toWrite + 2)/2), startLine + j)
160 term.write(toWrite)
161 j = j + 1
162 end
163
164 event, result = os.pullEvent("key") -- wait here until a key is pressed
165 if result == 200 -- On "up" Go up one item if there are still items available
166 then
167 if selectedItem > 1
168 then
169 selectedItem = selectedItem - 1
170 if selectedItem < firstItem -- scheck we haven't moved out of the current displayed range. If so, scroll.
171 then
172 firstItem = selectedItem
173 lastItem = lastItem - 1
174 end
175 end
176 elseif result == 208
177 then -- on "down" Go down one item if there are still items available
178 if selectedItem < #menuItems
179 then
180 selectedItem = selectedItem + 1
181 if selectedItem > lastItem -- scheck we haven't moved out of the current displayed range. If so, scroll.
182 then
183 lastItem = selectedItem
184 firstItem = firstItem + 1
185 end
186 end
187 elseif result == 28 then -- on "enter" return that item number
188 if menuItems[selectedItem].func ~= nil -- if the menu is "functional" then call the function. Else don't)
189 then
190 p = menuItems[selectedItem].params or {}
191 menuItems[selectedItem].func(p[1],p[2],p[3],p[4],p[5],p[6])
192 end
193 return selectedItem
194 end
195 end
196end
197
198function sideCheck(check) -- checks that a named side is a valid side
199 for k,v in pairs(redstone.getSides) do
200 if check == v
201 then
202 return true
203 end
204 end
205 return false
206end
207
208function findTargets(sensorUsed, targetTypes) -- given a sensor and a list of types to search for, will return a table of target locations for those matching
209 allTargets = sensorUsed.getTargets()
210 targets = {}
211 for k,v in pairs(allTargets) do
212 for l,u in pairs (targetTypes) do
213 if v.RawName == l
214 then
215 table.insert(targets,k)
216 end
217 end
218 end
219 return targets
220end
221
222function saveData(dataToSave, saveFileName, userResponse) -- saves any variable away in the specified location in serialized form.
223 userResponse = userResponse or true
224 print("")
225 term.write("Saving to " .. saveFileName)
226 fs.delete(saveFileName)
227 term.write("...")
228 targetFile = io.open(saveFileName, "w")
229 term.write("......")
230 targetFile:write(textutils.serialize(dataToSave))
231 term.write("......")
232 targetFile:close()
233 print("...Done")
234-- if userResponse
235-- then
236-- print("Enter to continue")
237-- read()
238-- end
239end
240
241function loadData(fileName) -- loads a requested serialized text file, returns the contents
242 if fs.exists(fileName)
243 then
244 targetFile = io.open(fileName)
245 data = textutils.unserialize(targetFile:read("*a"))
246 targetFile:close()
247
248 else
249 print("No ".. fileName .. " file exists")
250 end
251 return data
252end
253
254
255function combineTables(tab1,tab2)
256 newTab = {}
257 for k, v in pairs(tab1) do
258 newTab[k] = v
259 end
260 for k, v in pairs(tab2) do
261 newTab[k] = v
262 end
263 return newTab
264end
265
266function selectPeripheral(periphType) -- This function should move to common functions
267 periphs = commonFunctions.findAllPeripherals(periphType) -- Scan for network attached and side attached peripherals of the given type
268 if periphs == nil -- Check that there are any periphs returned
269 then
270 print ("No linked " .. periphType .. "s detected")
271 return "none"
272 end
273
274 -- Create the menu for the user to pick from
275 menuItems = {}
276 for k, v in pairs(periphs) do
277 table.insert(menuItems, k)
278 end
279 table.insert(menuItems, "none")
280 periphType = periphType or "peripheral"
281 heading = "Select " .. periphType .. "to Use:"
282 menuResult = commonFunctions.menu(menuItems, heading)
283 return menuItems[menuResult] -- return which peripheral was picked.
284end