SHOW:
|
|
- or go back to the newest paste.
1 | function 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 | |
10 | end | |
11 | ||
12 | function 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() | |
21 | end | |
22 | ||
23 | function 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 | |
43 | end | |
44 | ||
45 | function printPairs(tableToPrint) -- Ronseal | |
46 | for k, v in pairs(tableToPrint) do | |
47 | print(k .. " , " .. v) | |
48 | end | |
49 | end | |
50 | ||
51 | function 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 | dict["monitor"].restoreTo = term.current() | |
62 | term.redirect(peripheral.wrap(sides[i])) | |
63 | - | term.restore() |
63 | + | |
64 | term.redirect(dict["monitor"].restoreTo) | |
65 | elseif peripheral.getType( sides[i]) == "modem" then | |
66 | dict["modem"]=sides[i] | |
67 | rednet.open(sides[i]) | |
68 | 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 | |
69 | dict["pipe"]=sides[i] | |
70 | print("Pipe found by getPeripherals") | |
71 | elseif peripheral.getType( sides[i]) == "rcirontankvalvetile" then | |
72 | dict["ITank"]=sides[i] | |
73 | print("Iron Tank found by getPeripherals") | |
74 | else | |
75 | dict[peripheral.getType( sides[i])] = sides[i] | |
76 | -- guess this will find printers etc. | |
77 | end | |
78 | end | |
79 | end | |
80 | return dict | |
81 | end | |
82 | ||
83 | function 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 | |
84 | locs = peripheral.getNames() | |
85 | periphs = {} | |
86 | for k,v in pairs(locs) do | |
87 | if filter ~= nil | |
88 | then | |
89 | if (peripheral.getType(v):find(filter)) ~= nil | |
90 | then | |
91 | periphs[v] = peripheral.wrap(v) | |
92 | end | |
93 | else | |
94 | periphs[v] = peripheral.wrap(v) | |
95 | end | |
96 | end | |
97 | return periphs | |
98 | end | |
99 | ||
100 | function 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 | |
101 | initalState = rs.getBundledOutput(side) -- get the current cable state | |
102 | rs.setBundledOutput(side, colours.combine(initalState, colour)) | |
103 | sleep(0.2) | |
104 | rs.setBundledOutput(side, initalState) | |
105 | sleep(0.2) | |
106 | end | |
107 | ||
108 | function cableOn(side, colour) -- turns on wires of designated colours | |
109 | initalState = rs.getBundledOutput(side) -- get the current cable state | |
110 | --print("inital colours:" .. initalState ) | |
111 | --print("requested colour: " .. colour) | |
112 | newState = colours.combine(initalState, colour) | |
113 | --print("new colours: " .. newState) | |
114 | rs.setBundledOutput(side, newState) | |
115 | sleep(0.2) | |
116 | end | |
117 | ||
118 | function cableOff(side, colour) -- turn off a certain colour wire | |
119 | initalState = rs.getBundledOutput(side) -- get the current cable state | |
120 | rs.setBundledOutput(side, colours.subtract(initalState, colour)) | |
121 | sleep(0.2) | |
122 | end | |
123 | ||
124 | function cableCheck(side, colour) -- Is a certain colour wire on? | |
125 | if colours.test(rs.getBundledInput(side), colour) then | |
126 | return true | |
127 | else | |
128 | return false | |
129 | end | |
130 | end | |
131 | ||
132 | function 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. | |
133 | --ToDo - make this SCROLL | |
134 | w, h = term.getSize() | |
135 | selectedItem = 1 | |
136 | ||
137 | firstItem = 1 | |
138 | if #menuItems < (h - 2) | |
139 | then | |
140 | lastItem = #menuItems | |
141 | startLine = (h/2) - (#menuItems/2) - 1 | |
142 | else | |
143 | lastItem = (h-2) | |
144 | startLine = 1 | |
145 | end | |
146 | while true do | |
147 | term.clear() | |
148 | if menuHeading ~= nil | |
149 | then | |
150 | term.setCursorPos((w/2)-(#menuHeading/2), startLine) | |
151 | term.write(menuHeading) | |
152 | end | |
153 | j = 1 | |
154 | for i = firstItem, lastItem do -- draw the menu in the middle | |
155 | toWrite = menuItems[i].text or menuItems[i] | |
156 | if i == selectedItem -- If the current item is the selected one, make it obvious to the user | |
157 | then | |
158 | toWrite = "=" .. toWrite .. "=" | |
159 | end | |
160 | term.setCursorPos((w/2)-((#toWrite + 2)/2), startLine + j) | |
161 | term.write(toWrite) | |
162 | j = j + 1 | |
163 | end | |
164 | ||
165 | event, result = os.pullEvent("key") -- wait here until a key is pressed | |
166 | if result == 200 -- On "up" Go up one item if there are still items available | |
167 | then | |
168 | if selectedItem > 1 | |
169 | then | |
170 | selectedItem = selectedItem - 1 | |
171 | if selectedItem < firstItem -- scheck we haven't moved out of the current displayed range. If so, scroll. | |
172 | then | |
173 | firstItem = selectedItem | |
174 | lastItem = lastItem - 1 | |
175 | end | |
176 | end | |
177 | elseif result == 208 | |
178 | then -- on "down" Go down one item if there are still items available | |
179 | if selectedItem < #menuItems | |
180 | then | |
181 | selectedItem = selectedItem + 1 | |
182 | if selectedItem > lastItem -- scheck we haven't moved out of the current displayed range. If so, scroll. | |
183 | then | |
184 | lastItem = selectedItem | |
185 | firstItem = firstItem + 1 | |
186 | end | |
187 | end | |
188 | elseif result == 28 then -- on "enter" return that item number | |
189 | if menuItems[selectedItem].func ~= nil -- if the menu is "functional" then call the function. Else don't) | |
190 | then | |
191 | p = menuItems[selectedItem].params or {} | |
192 | menuItems[selectedItem].func(p[1],p[2],p[3],p[4],p[5],p[6]) | |
193 | end | |
194 | return selectedItem | |
195 | end | |
196 | end | |
197 | end | |
198 | ||
199 | function sideCheck(check) -- checks that a named side is a valid side | |
200 | for k,v in pairs(redstone.getSides) do | |
201 | if check == v | |
202 | then | |
203 | return true | |
204 | end | |
205 | end | |
206 | return false | |
207 | end | |
208 | ||
209 | function findTargets(sensorUsed, targetTypes) -- given a sensor and a list of types to search for, will return a table of target locations for those matching | |
210 | allTargets = sensorUsed.getTargets() | |
211 | targets = {} | |
212 | for k,v in pairs(allTargets) do | |
213 | for l,u in pairs (targetTypes) do | |
214 | if v.RawName == l | |
215 | then | |
216 | table.insert(targets,k) | |
217 | end | |
218 | end | |
219 | end | |
220 | return targets | |
221 | end | |
222 | ||
223 | function saveData(dataToSave, saveFileName, userResponse) -- saves any variable away in the specified location in serialized form. | |
224 | userResponse = userResponse or true | |
225 | print("") | |
226 | term.write("Saving to " .. saveFileName) | |
227 | fs.delete(saveFileName) | |
228 | term.write("...") | |
229 | targetFile = io.open(saveFileName, "w") | |
230 | term.write("......") | |
231 | targetFile:write(textutils.serialize(dataToSave)) | |
232 | term.write("......") | |
233 | targetFile:close() | |
234 | print("...Done") | |
235 | -- if userResponse | |
236 | -- then | |
237 | -- print("Enter to continue") | |
238 | -- read() | |
239 | -- end | |
240 | end | |
241 | ||
242 | function loadData(fileName) -- loads a requested serialized text file, returns the contents | |
243 | if fs.exists(fileName) | |
244 | then | |
245 | targetFile = io.open(fileName) | |
246 | data = textutils.unserialize(targetFile:read("*a")) | |
247 | targetFile:close() | |
248 | ||
249 | else | |
250 | print("No ".. fileName .. " file exists") | |
251 | end | |
252 | return data | |
253 | end | |
254 | ||
255 | ||
256 | function combineTables(tab1,tab2) | |
257 | newTab = {} | |
258 | for k, v in pairs(tab1) do | |
259 | newTab[k] = v | |
260 | end | |
261 | for k, v in pairs(tab2) do | |
262 | newTab[k] = v | |
263 | end | |
264 | return newTab | |
265 | end | |
266 | ||
267 | function selectPeripheral(periphType) -- This function should move to common functions | |
268 | periphs = commonFunctions.findAllPeripherals(periphType) -- Scan for network attached and side attached peripherals of the given type | |
269 | if periphs == nil -- Check that there are any periphs returned | |
270 | then | |
271 | print ("No linked " .. periphType .. "s detected") | |
272 | return "none" | |
273 | end | |
274 | ||
275 | -- Create the menu for the user to pick from | |
276 | menuItems = {} | |
277 | for k, v in pairs(periphs) do | |
278 | table.insert(menuItems, k) | |
279 | end | |
280 | table.insert(menuItems, "none") | |
281 | periphType = periphType or "peripheral" | |
282 | heading = "Select " .. periphType .. "to Use:" | |
283 | menuResult = commonFunctions.menu(menuItems, heading) | |
284 | return menuItems[menuResult] -- return which peripheral was picked. | |
285 | end |