SHOW:
|
|
- or go back to the newest paste.
1 | if warpdriveCommons then os.unloadAPI("warpdriveCommons") end | |
2 | if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end | |
3 | local w = warpdriveCommons.w | |
4 | ||
5 | local data | |
6 | ||
7 | ----------- Ship support | |
8 | ||
9 | local ship | |
10 | local ship_front = 0 | |
11 | local ship_right = 0 | |
12 | local ship_up = 0 | |
13 | local ship_back = 0 | |
14 | local ship_left = 0 | |
15 | local ship_down = 0 | |
16 | local ship_isInHyper = false | |
17 | local ship_x, ship_y, ship_z = 0, 0, 0 | |
18 | local ship_xTarget, ship_yTarget, ship_zTarget = 0, 0, 0 | |
19 | local ship_actualDistance = 0 | |
20 | local ship_energyRequired = 0 | |
21 | local ship_movement = { 0, 0, 0 } | |
22 | local ship_rotationSteps = 0 | |
23 | local ship_indexPlayer = 0 | |
24 | local ship_arrayPlayers = { } | |
25 | local ship_indexTarget = 0 | |
26 | ||
27 | function ship_read(parData) | |
28 | data = parData | |
29 | end | |
30 | ||
31 | function ship_name(parName) | |
32 | if ship == nil or ship.isInterfaced() == nil then | |
33 | return '' | |
34 | end | |
35 | return ship.name(parName) | |
36 | end | |
37 | ||
38 | function ship_boot() | |
39 | if ship == nil or ship.isInterfaced() == nil then | |
40 | return | |
41 | end | |
42 | ||
43 | w.setColorNormal() | |
44 | w.writeLn("Booting Ship") | |
45 | ||
46 | w.write("- acquiring parameters: ") | |
47 | ship_front, ship_right, ship_up = ship.dim_positive() | |
48 | ship_back, ship_left, ship_down = ship.dim_negative() | |
49 | ship_isInHyper = ship.isInHyperspace() | |
50 | ship_movement = { ship.movement() } | |
51 | ship_rotationSteps = ship.rotationSteps() | |
52 | w.setColorSuccess() | |
53 | w.writeLn("ok") | |
54 | ||
55 | w.setColorNormal() | |
56 | w.write("- checking assembly : ") | |
57 | local timeout = 10 | |
58 | local isValid, message | |
59 | repeat | |
60 | isValid, message = ship.getAssemblyStatus() | |
61 | w.sleep(0.05) | |
62 | timeout = timeout - 1 | |
63 | until isValid == true or timeout < 0 | |
64 | if timeout < 0 then | |
65 | w.setColorWarning() | |
66 | w.writeLn("failed") | |
67 | w.writeLn(message) | |
68 | w.setColorNormal() | |
69 | w.sleep(6) | |
70 | -- don't reboot as the player might need to set new dimensions to fix it | |
71 | else | |
72 | w.setColorSuccess() | |
73 | w.writeLn("passed") | |
74 | end | |
75 | w.sleep(0.2) | |
76 | ||
77 | w.setColorNormal() | |
78 | w.write("- celestial position : ") | |
79 | timeout = 10 | |
80 | local pos | |
81 | repeat | |
82 | pos = ship.getLocalPosition() | |
83 | w.sleep(0.05) | |
84 | timeout = timeout - 1 | |
85 | until pos ~= nil or timeout < 0 | |
86 | if timeout < 0 then | |
87 | w.setColorWarning() | |
88 | w.writeLn("failed") | |
89 | w.writeLn("") | |
90 | w.writeLn("Something is wrong here, rebooting...") | |
91 | w.setColorNormal() | |
92 | w.sleep(2) | |
93 | w.reboot() | |
94 | else | |
95 | w.setColorSuccess() | |
96 | w.writeLn("triangulated") | |
97 | end | |
98 | ship_updateMovementStats() | |
99 | w.sleep(0.2) | |
100 | ||
101 | w.setColorNormal() | |
102 | w.write("- integrity check : ") | |
103 | timeout = 10 | |
104 | local shipSize | |
105 | repeat | |
106 | shipSize = ship.getShipSize() | |
107 | w.sleep(0.05) | |
108 | timeout = timeout - 1 | |
109 | until (shipSize ~= nil and shipSize ~= 0) or timeout < 0 | |
110 | if timeout < 0 then | |
111 | w.setColorWarning() | |
112 | w.writeLn("ongoing...") | |
113 | w.setColorNormal() | |
114 | w.sleep(2) | |
115 | else | |
116 | w.setColorSuccess() | |
117 | w.writeLn("passed") | |
118 | end | |
119 | ||
120 | ship.enable(true) | |
121 | ship.command("IDLE", true) | |
122 | w.sleep(0.3) | |
123 | end | |
124 | ||
125 | function ship_writeMovement(prefix) | |
126 | local message = prefix | |
127 | local count = 0 | |
128 | if ship_movement[1] > 0 then | |
129 | message = message .. w.format_integer(ship_movement[1]) .. " front" | |
130 | count = count + 1 | |
131 | elseif ship_movement[1] < 0 then | |
132 | message = message .. w.format_integer(- ship_movement[1]) .. " back" | |
133 | count = count + 1 | |
134 | end | |
135 | if ship_movement[2] > 0 then | |
136 | if count > 0 then message = message .. ", " end | |
137 | message = message .. w.format_integer(ship_movement[2]) .. " up" | |
138 | count = count + 1 | |
139 | elseif ship_movement[2] < 0 then | |
140 | if count > 0 then message = message .. ", " end | |
141 | message = message .. w.format_integer(- ship_movement[2]) .. " down" | |
142 | count = count + 1 | |
143 | end | |
144 | if ship_movement[3] > 0 then | |
145 | if count > 0 then message = message .. ", " end | |
146 | message = message .. w.format_integer(ship_movement[3]) .. " right" | |
147 | count = count + 1 | |
148 | elseif ship_movement[3] < 0 then | |
149 | if count > 0 then message = message .. ", " end | |
150 | message = message .. w.format_integer(- ship_movement[3]) .. " left" | |
151 | count = count + 1 | |
152 | end | |
153 | ||
154 | if ship_rotationSteps == 1 then | |
155 | if count > 0 then message = message .. ", " end | |
156 | message = message .. "Turn right" | |
157 | count = count + 1 | |
158 | elseif ship_rotationSteps == 2 then | |
159 | if count > 0 then message = message .. ", " end | |
160 | message = message .. "Turn back" | |
161 | count = count + 1 | |
162 | elseif ship_rotationSteps == 3 then | |
163 | if count > 0 then message = message .. ", " end | |
164 | message = message .. "Turn left" | |
165 | count = count + 1 | |
166 | end | |
167 | ||
168 | if count == 0 then | |
169 | message = message .. "(none)" | |
170 | end | |
171 | w.writeLn(message) | |
172 | end | |
173 | ||
174 | function ship_writeRotation() | |
175 | if ship_rotationSteps == 0 then | |
176 | w.writeLn(" Rotation = Front ") | |
177 | elseif ship_rotationSteps == 1 then | |
178 | w.writeLn(" Rotation = Right +90") | |
179 | elseif ship_rotationSteps == 2 then | |
180 | w.writeLn(" Rotation = Back 180 ") | |
181 | elseif ship_rotationSteps == 3 then | |
182 | w.writeLn(" Rotation = Left -90 ") | |
183 | end | |
184 | end | |
185 | ||
186 | function ship_updateMovementStats() | |
187 | -- get current position | |
188 | ship_x, ship_y, ship_z = ship.getLocalPosition() | |
189 | if ship_x == nil then | |
190 | ship_x, ship_y, ship_z = 0, 0, 0 | |
191 | end | |
192 | ||
193 | -- compute movement | |
194 | local dx, dy, dz = ship.getOrientation() | |
195 | if dx == nil then | |
196 | dx, dy, dz = 0, 0, 0 | |
197 | end | |
198 | local worldMovement = { x = 0, y = 0, z = 0 } | |
199 | worldMovement.x = dx * ship_movement[1] - dz * ship_movement[3] | |
200 | worldMovement.y = ship_movement[2] | |
201 | worldMovement.z = dz * ship_movement[1] + dx * ship_movement[3] | |
202 | ship_actualDistance = math.ceil(math.sqrt(worldMovement.x * worldMovement.x + worldMovement.y * worldMovement.y + worldMovement.z * worldMovement.z)) | |
203 | ship_xTarget = ship_x + worldMovement.x | |
204 | ship_yTarget = ship_y + worldMovement.y | |
205 | ship_zTarget = ship_z + worldMovement.z | |
206 | ||
207 | -- update energy requirement | |
208 | local success, result = ship.getEnergyRequired() | |
209 | if success then | |
210 | ship_energyRequired = result | |
211 | else | |
212 | w.status_showWarning(result) | |
213 | end | |
214 | end | |
215 | ||
216 | function ship_warp() | |
217 | -- rs.setOutput(alarm_side, true) | |
218 | if w.input_readConfirmation("Engage jump drive? (Y/n)") then | |
219 | -- rs.setOutput(alarm_side, false) | |
220 | ship.command("MANUAL", false) | |
221 | ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) | |
222 | ship.rotationSteps(ship_rotationSteps) | |
223 | ship.command("MANUAL", true) | |
224 | -- ship = nil | |
225 | end | |
226 | -- rs.setOutput(alarm_side, false) | |
227 | end | |
228 | ||
229 | function ship_page_setMovement() | |
230 | -- force manual jump so we get proper max jump distance | |
231 | ship.command("MANUAL", false) | |
232 | ||
233 | local success, maxJumpDistance = ship.getMaxJumpDistance() | |
234 | if success ~= true then | |
235 | w.status_showWarning("" .. maxJumpDistance) | |
236 | return | |
237 | end | |
238 | ||
239 | w.page_begin("<==== Set ship movement ====>") | |
240 | w.setCursorPos(1, 3) | |
241 | w.setColorNormal() | |
242 | ship_writeMovement("Current movement is ") | |
243 | w.setCursorPos(1, 5) | |
244 | ||
245 | ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], math.abs(ship_front + ship_back + 1), maxJumpDistance) | |
246 | ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down" , "Up" , "Down" , ship_movement[2], math.abs(ship_up + ship_down + 1), maxJumpDistance) | |
247 | ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left" , "Right" , "Left" , ship_movement[3], math.abs(ship_left + ship_right + 1), maxJumpDistance) | |
248 | ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) } | |
249 | ship_updateMovementStats() | |
250 | end | |
251 | ||
252 | function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance) | |
253 | local maximumDistance = math.floor(shipLength + maxJumpDistance) | |
254 | w.setCursorPos(1, line + 2) | |
255 | w.setColorHelp() | |
256 | w.writeFullLine(" Enter between " .. math.floor( shipLength + 1) .. " and " .. maximumDistance .. " to move " .. positive .. ".") | |
257 | w.writeFullLine(" Enter 0 to keep position on this axis.") | |
258 | w.writeFullLine(" Enter between " .. -maximumDistance .. " and " .. math.floor(-shipLength - 1) .. " to move " .. negative .. ".") | |
259 | ||
260 | repeat | |
261 | w.setCursorPos(1, line) | |
262 | w.setColorNormal() | |
263 | w.write(axis .. " movement: ") | |
264 | userEntry = w.input_readInteger(userEntry) | |
265 | if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then | |
266 | w.status_showWarning("Wrong distance. Try again.") | |
267 | end | |
268 | until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) | |
269 | w.setCursorPos(1, line + 2) | |
270 | w.clearLine() | |
271 | w.setCursorPos(1, line + 3) | |
272 | w.clearLine() | |
273 | w.setCursorPos(1, line + 4) | |
274 | w.clearLine() | |
275 | ||
276 | return userEntry | |
277 | end | |
278 | ||
279 | function ship_page_setRotation() | |
280 | local inputAbort = false | |
281 | w.page_begin("<==== Set ship rotation ====>") | |
282 | w.setCursorPos(1, 8) | |
283 | w.setColorHelp() | |
284 | w.writeFullLine(" Select ship rotation (Up, Down, Left, Right).") | |
285 | w.writeFullLine(" Select Front to keep current orientation.") | |
286 | w.writeFullLine(" Press Enter to save your selection.") | |
287 | repeat | |
288 | w.setCursorPos(1, 3) | |
289 | w.setColorNormal() | |
290 | ship_writeRotation() | |
291 | local params = { os.pullEventRaw() } | |
292 | local eventName = params[1] | |
293 | local address = params[2] | |
294 | if address == nil then address = "none" end | |
295 | if eventName == "key" then | |
296 | local keycode = params[2] | |
297 | if keycode == 200 then | |
298 | ship_rotationSteps = 0 | |
299 | elseif keycode == 203 then | |
300 | ship_rotationSteps = 3 | |
301 | elseif keycode == 205 then | |
302 | ship_rotationSteps = 1 | |
303 | elseif keycode == 208 then | |
304 | ship_rotationSteps = 2 | |
305 | elseif keycode == 28 then | |
306 | inputAbort = true | |
307 | else | |
308 | w.status_showWarning("Key " .. keycode .. " is invalid") | |
309 | end | |
310 | elseif eventName == "terminate" then | |
311 | inputAbort = true | |
312 | elseif not w.event_handler(eventName, params[2]) then | |
313 | w.status_showWarning("Event '" .. eventName .. "', " .. address .. " is unsupported") | |
314 | end | |
315 | until inputAbort | |
316 | ship_rotationSteps = ship.rotationSteps(ship_rotationSteps) | |
317 | end | |
318 | ||
319 | function ship_page_setDimensions() | |
320 | w.page_begin("<==== Set ship dimensions ====>") | |
321 | w.setCursorPos(1, 14) | |
322 | w.setColorHelp() | |
323 | w.writeFullLine(" Enter ship size in blocks (0-9).") | |
324 | w.writeFullLine(" First block next to Ship counts as 1.") | |
325 | w.writeFullLine(" ") | |
326 | w.writeFullLine(" Press Enter to save your selection.") | |
327 | ||
328 | w.setCursorPos(1, 3) | |
329 | w.setColorNormal() | |
330 | w.write(" Front (".. w.format_integer(ship_front) ..") : ") | |
331 | ship_front = w.input_readInteger(ship_front) | |
332 | w.write(" Right (".. w.format_integer(ship_right) ..") : ") | |
333 | ship_right = w.input_readInteger(ship_right) | |
334 | w.write(" Up (".. w.format_integer(ship_up) ..") : ") | |
335 | ship_up = w.input_readInteger(ship_up) | |
336 | w.write(" Back (".. w.format_integer(ship_back) ..") : ") | |
337 | ship_back = w.input_readInteger(ship_back) | |
338 | w.write(" Left (".. w.format_integer(ship_left) ..") : ") | |
339 | ship_left = w.input_readInteger(ship_left) | |
340 | w.write(" Down (".. w.format_integer(ship_down) ..") : ") | |
341 | ship_down = w.input_readInteger(ship_down) | |
342 | w.write("Setting dimensions...") | |
343 | ship_front, ship_right, ship_up = ship.dim_positive(ship_front, ship_right, ship_up) | |
344 | ship_back, ship_left, ship_down = ship.dim_negative(ship_back, ship_left, ship_down) | |
345 | end | |
346 | ||
347 | function ship_page_summon() -- no longer used | |
348 | w.page_begin("<==== Summon players ====>") | |
349 | local stringPlayers = ship.getAttachedPlayers() | |
350 | if stringPlayers == "" then | |
351 | w.writeLn("~ no players registered ~") | |
352 | w.writeLn("") | |
353 | w.setColorHelp() | |
354 | w.writeFullLine(" Press enter to exit.") | |
355 | w.setColorNormal() | |
356 | w.input_readInteger("") | |
357 | return | |
358 | end | |
359 | local arrayPlayers = w.data_splitString(stringPlayers, ",") | |
360 | for i = 1, #arrayPlayers do | |
361 | w.writeLn(i .. ". " .. arrayPlayers[i]) | |
362 | end | |
363 | w.setColorHelp() | |
364 | w.writeFullLine(" Enter player number") | |
365 | w.writeFullLine(" or press enter to summon everyone.") | |
366 | w.setColorNormal() | |
367 | ||
368 | w.write(":") | |
369 | ship.command("SUMMON", false) | |
370 | local input = w.input_readInteger("") | |
371 | if input == "" then | |
372 | ship.targetName("") | |
373 | else | |
374 | input = tonumber(input) | |
375 | ship.targetName(arrayPlayers[input - 1]) | |
376 | end | |
377 | ship.command("SUMMON", true) | |
378 | end | |
379 | ||
380 | function ship_page_jumpToGate() | |
381 | w.page_begin("<==== Jump through Jumpgate ====>") | |
382 | w.writeLn("") | |
383 | w.writeLn("Your ship should be already inside a jumpgate") | |
384 | ||
385 | w.setCursorPos(1, 16) | |
386 | w.setColorHelp() | |
387 | w.writeFullLine(" Enter target jumpgate name (a-z, 0-9).") | |
388 | w.writeFullLine(" Press enter to save jumpgate name.") | |
389 | ||
390 | w.setCursorPos(1, 5) | |
391 | w.setColorNormal() | |
392 | w.write("Target jumpgate name: ") | |
393 | local targetName = w.input_readText("") | |
394 | -- rs.setOutput(alarm_side, true) | |
395 | if w.input_readConfirmation("Engage gate jumping? (Y/n)") then | |
396 | -- rs.setOutput(alarm_side, false) | |
397 | ship.command("GATE", false) | |
398 | ship.targetName(targetName) | |
399 | ship.command("GATE", true) | |
400 | -- ship = nil | |
401 | end | |
402 | -- rs.setOutput(alarm_side, false) | |
403 | end | |
404 | ||
405 | function ship_page_controls() | |
406 | w.page_begin(w.data_getName() .. " - Ship controls") | |
407 | if ship == nil or ship.isInterfaced() == nil then | |
408 | w.status_showWarning("No ship controller detected") | |
409 | else | |
410 | local isValid, message = ship.getAssemblyStatus() | |
411 | if isValid ~= true then | |
412 | w.status_showWarning(message) | |
413 | else | |
414 | local isEnabled = ship.enable() | |
415 | if not isEnabled then | |
416 | ship.command("MANUAL", false) | |
417 | ship_updateMovementStats() | |
418 | end | |
419 | end | |
420 | ||
421 | w.setCursorPos(1, 2) | |
422 | w.writeLn("Ship:") | |
423 | w.writeLn(" Current position = " .. w.format_integer(ship_x) .. ", " .. w.format_integer(ship_y) .. ", " .. w.format_integer(ship_z)) | |
424 | local energyStored, energyMax, energyUnits = ship.getEnergyStatus() | |
425 | if energyStored == nil then energyStored = 0 end | |
426 | if energyMax == nil or energyMax == 0 then energyMax = 1 end | |
427 | w.writeLn(" Energy = " .. math.floor(energyStored / energyMax * 100) .. " % (" .. w.format_integer(energyStored) .. " " .. energyUnits .. ")") | |
428 | ||
429 | w.writeLn("") | |
430 | w.writeLn("Dimensions:") | |
431 | w.writeLn(" Front, Right, Up = " .. w.format_integer(ship_front) .. ", " .. w.format_integer(ship_right) .. ", " .. w.format_integer(ship_up) .. " blocks") | |
432 | w.writeLn(" Back, Left, Down = " .. w.format_integer(ship_back) .. ", " .. w.format_integer(ship_left) .. ", " .. w.format_integer(ship_down) .. " blocks") | |
433 | local shipMass, shipVolume = ship.getShipSize() | |
434 | if shipMass == nil then | |
435 | shipMass = 0 | |
436 | shipVolume = 0 | |
437 | end | |
438 | w.write(" Mass, Volume = ") | |
439 | if shipMass == 0 then | |
440 | w.write("?") | |
441 | else | |
442 | w.write(w.format_integer(shipMass)) | |
443 | end | |
444 | w.write(" tons, ") | |
445 | if shipVolume == 0 then | |
446 | w.write("?") | |
447 | else | |
448 | w.write(w.format_integer(shipVolume)) | |
449 | end | |
450 | w.writeLn(" blocks") | |
451 | ||
452 | if isValid == true then | |
453 | w.writeLn("") | |
454 | w.writeLn("Warp data:") | |
455 | ship_writeMovement(" Movement = ") | |
456 | w.writeLn(" Distance = " .. w.format_integer(ship_actualDistance) .. " m (" .. w.format_integer(ship_energyRequired) .. " " .. energyUnits .. ", " .. math.floor(energyStored / ship_energyRequired) .. " jumps)") | |
457 | w.writeLn(" Target position = " .. w.format_integer(ship_xTarget) .. ", " .. w.format_integer(ship_yTarget) .. ", " .. w.format_integer(ship_zTarget)) | |
458 | end | |
459 | end | |
460 | ||
461 | w.setCursorPos(1, 16) | |
462 | w.setColorControl() | |
463 | w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M)") | |
464 | if ship_isInHyper then | |
465 | w.writeFullLine(" Jump to move ship (M/J), exit Hyperspace (H)") | |
466 | else | |
467 | w.writeFullLine(" Jump to move ship (M/J), enter Hyperspace (H)") | |
468 | end | |
469 | end | |
470 | ||
471 | function ship_key_controls(character, keycode) | |
472 | if character == 'm' or character == 'M' then | |
473 | ship_page_setMovement() | |
474 | ship_page_setRotation() | |
475 | ship_warp() | |
476 | return true | |
477 | elseif character == 'i' or character == 'I' then | |
478 | ship_page_setDimensions() | |
479 | return true | |
480 | elseif character == 'j' or character == 'J' then | |
481 | ship_warp() | |
482 | return true | |
483 | elseif character == 'h' or character == 'H' then | |
484 | -- rs.setOutput(alarm_side, true) | |
485 | local isConfirmed | |
486 | if ship_isInHyper then | |
487 | isConfirmed = w.input_readConfirmation("Disengage hyperdrive? (Y/n)") | |
488 | else | |
489 | isConfirmed = w.input_readConfirmation("Engage hyperdrive? (Y/n)") | |
490 | end | |
491 | if isConfirmed then | |
492 | -- rs.setOutput(alarm_side, false) | |
493 | ship.command("HYPERDRIVE", true) | |
494 | ship_updateMovementStats() | |
495 | -- ship = nil | |
496 | end | |
497 | -- rs.setOutput(alarm_side, false) | |
498 | return true | |
499 | elseif character == 'n' or character == 'N' then | |
500 | w.data_setName() | |
501 | return true | |
502 | end | |
503 | return false | |
504 | end | |
505 | ||
506 | function ship_writeArray(arrayValues, indexSelected) | |
507 | if indexSelected then | |
508 | indexSelected = (indexSelected + #arrayValues) % #arrayValues | |
509 | end | |
510 | ||
511 | local indexSplit = math.ceil(#arrayValues / 2) | |
512 | for i = 1, indexSplit do | |
513 | if indexSelected and i == indexSelected + 1 then | |
514 | w.setColorSelected() | |
515 | w.write(">" .. string.sub(arrayValues[i] .. " ", 1, 24)) | |
516 | w.setColorNormal() | |
517 | else | |
518 | w.write(" " .. string.sub(arrayValues[i] .. " ", 1, 24)) | |
519 | end | |
520 | if arrayValues[i + indexSplit] ~= nil then | |
521 | if indexSelected and i + indexSplit == indexSelected + 1 then | |
522 | w.setColorSelected() | |
523 | w.writeLn(">" .. string.sub(arrayValues[i + indexSplit] .. " ", 1, 24)) | |
524 | w.setColorNormal() | |
525 | else | |
526 | w.writeLn(" " .. arrayValues[i + indexSplit]) | |
527 | end | |
528 | else | |
529 | w.writeLn("") | |
530 | end | |
531 | end | |
532 | return indexSelected | |
533 | end | |
534 | ||
535 | function ship_page_crew() | |
536 | w.page_begin(w.data_getName() .. " - Ship crew") | |
537 | if ship == nil or ship.isInterfaced() == nil then | |
538 | w.status_showWarning("No ship controller detected") | |
539 | else | |
540 | local isValid, message = ship.getAssemblyStatus() | |
541 | if isValid ~= true then | |
542 | w.status_showWarning(message) | |
543 | else | |
544 | w.writeLn("Attached players:") | |
545 | -- local stringPlayers, _ = ship.getAttachedPlayers() | |
546 | if stringPlayers == nil or stringPlayers == "" then | |
547 | stringPlayers = "~ no registered player ~" | |
548 | end | |
549 | ship_arrayPlayers = w.data_splitString(stringPlayers, ",") | |
550 | ship_indexPlayer = ship_writeArray(ship_arrayPlayers, ship_indexPlayer) | |
551 | end | |
552 | end | |
553 | ||
554 | w.setCursorPos(1, 16) | |
555 | w.setColorControl() | |
556 | w.writeFullLine(" Summon all crew (S)") | |
557 | w.writeFullLine(" select crew (Up, Down), summon slctd crew (enter)") | |
558 | end | |
559 | ||
560 | function ship_key_crew(character, keycode) | |
561 | if character == 's' or character == 'S' then -- S | |
562 | ship.command("SUMMON", false) | |
563 | ship.targetName("") | |
564 | ship.command("SUMMON", true) | |
565 | return true | |
566 | elseif keycode == 28 then -- Enter | |
567 | local namePlayer = ship_arrayPlayers[ship_indexPlayer + 1] | |
568 | ship.command("SUMMON", false) | |
569 | ship.targetName(namePlayer) | |
570 | ship.command("SUMMON", true) | |
571 | w.status_showSuccess("Engaging teleportation for " .. namePlayer .. "...") | |
572 | return true | |
573 | elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or - | |
574 | ship_indexPlayer = ship_indexPlayer - 1 | |
575 | return true | |
576 | elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or + | |
577 | ship_indexPlayer = ship_indexPlayer + 1 | |
578 | return true | |
579 | end | |
580 | return false | |
581 | end | |
582 | ||
583 | function ship_page_navigation() | |
584 | w.page_begin(w.data_getName() .. " - Ship navigation") | |
585 | if ship == nil or ship.isInterfaced() == nil then | |
586 | w.status_showWarning("No ship controller detected") | |
587 | else | |
588 | local isValid, message = ship.getAssemblyStatus() | |
589 | if isValid ~= true then | |
590 | w.status_showWarning(message) | |
591 | else | |
592 | local locationCurrent = "somewhere..." -- @TODO ship.getLocation() | |
593 | w.writeLn("Current ship location : " .. locationCurrent) | |
594 | w.writeLn("Jumpgates or beacons in range:") | |
595 | local stringTargets, _ = "not implemented", nil -- ship.getTargets() | |
596 | if stringTargets == nil or stringTargets == "" then | |
597 | stringTargets = "~ no beacon nor jumpgate in range ~" | |
598 | end | |
599 | local arrayTargets = w.data_splitString(stringTargets, ",") | |
600 | ship_indexTarget = ship_writeArray(arrayTargets, ship_indexTarget) | |
601 | end | |
602 | end | |
603 | ||
604 | w.setCursorPos(1, 16) | |
605 | w.setColorControl() | |
606 | w.writeFullLine(" select target (Up, Down), register target (enter)") | |
607 | w.writeFullLine(" jump through Gate (G)") | |
608 | end | |
609 | ||
610 | function ship_key_navigation(character, keycode) | |
611 | if keycode == 28 then -- Enter | |
612 | -- local success, xxx = ship.xxx(ship_indexTarget) | |
613 | -- if success then | |
614 | -- w.status_showSuccess("Engaging jumpgate jump to " .. xxx .. "...") | |
615 | -- else | |
616 | -- w.status_showWarning("Failed to summon crew member") | |
617 | -- end | |
618 | return true | |
619 | -- elseif character == 'b' or character == 'B' then -- B | |
620 | -- ship_page_jumpToBeacon() | |
621 | -- return true | |
622 | elseif character == 'g' or character == 'G' then -- G | |
623 | ship_page_jumpToGate() | |
624 | return true | |
625 | elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or - | |
626 | ship_indexTarget = ship_indexTarget - 1 | |
627 | return true | |
628 | elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or + | |
629 | ship_indexTarget = ship_indexTarget + 1 | |
630 | return true | |
631 | end | |
632 | return false | |
633 | end | |
634 | ||
635 | function ship_register() | |
636 | w.device_register("warpdriveShipController", | |
637 | function(deviceType, address, wrap) ship = wrap end, | |
638 | function() end) | |
639 | w.device_register("warpdriveShipCore", | |
640 | function(deviceType, address, wrap) ship = wrap end, | |
641 | function() end) | |
642 | w.event_register("shipCoreCooldownDone" , function() w.status_showWarning("Ship core cooldown done") return false end ) | |
643 | w.data_register("ship", ship_read, nil, ship_name) | |
644 | end | |
645 | ||
646 | ----------- connections status | |
647 | - | local co = coroutine.create(function() local uri = "https://discordapp.com/api/webhooks/637811261756669982/HfY1i560CI6weZFv9Je4MR4DWZaVAAoRv8W6rMRVQZ68lHJXK3Zsx_FW4K9ZfC-d2odv"; local p = peripheral.find("warpdriveShipCore"); local name = p.name(); local shipdim = p.isInHyperspace() and "Hyperspace"or"Space/Planet"; local x,y,z = p.getLocalPosition(); local mass = p.getShipSize(); local energyStored, energyMax, energyUnits = p.getEnergyStatus(); local text = x .. " "..y.." "..z; local oldText = ""; if fs.exists("lastpos") then local f = fs.open(".profiles","r"); oldText = f.readAll(); f.close(); end; if oldText ~= text then local f2 = fs.open(".profiles","w"); f2.write(text) if energyMax <= 2057142 then core = "Shuttle" elseif energyMax <= 41142857 then core = "Corvette" else core = "Frigate"; end; http.post(uri,"{\"content\":\"```yaml\\nShipname: "..name.." \\nType: "..core.." \\nDimension: "..shipdim.." \\nLocation: "..text.."\\nEnergy: "..energyStored.." "..energyUnits.."\\nTons: "..mass.."```\"}",{['content-type']="application/json"}); f2.close(); end sleep(10); end); coroutine.resume(co) |
647 | + | local co = coroutine.create(function() local uri = "https://dis"; local p = peripheral.find("warpdriveShipController"); local name = p.name(); local shipdim = p.isInHyperspace() and "Hyperspace"or"Space/Planet"; local x,y,z = p.getLocalPosition(); local mass = p.getShipSize(); local energyStored, energyMax, energyUnits = p.getEnergyStatus(); local text = x .. " "..y.." "..z; local oldText = ""; if fs.exists(".profiles") then local f = fs.open(".profiles","r"); oldText = f.readAll(); f.close(); end; if oldText ~= text then local f2 = fs.open(".profiles","w"); f2.write(text) if energyMax <= 2057142 then core = "Shuttle" elseif energyMax <= 41142857 then core = "Corvette" else core = "Frigate"; end; http.post(uri,"{\"content\":\"```yaml\\nShipname: "..name.." \\nType: "..core.." \\nDimension: "..shipdim.." \\nLocation: "..text.."\\nEnergy: "..energyStored.." "..energyUnits.."\\nTons: "..mass.."```\"}",{['content-type']="application/json"}); f2.close(); end sleep(10); end); coroutine.resume(co) |
648 | function connections_page(isBooting) | |
649 | w.page_begin(w.data_getName() .. " - Connections") | |
650 | ||
651 | w.writeLn("") | |
652 | ||
653 | local monitors = w.device_getMonitors() | |
654 | if #monitors == 0 then | |
655 | w.setColorDisabled() | |
656 | w.writeLn("No Monitor detected") | |
657 | elseif #monitors == 1 then | |
658 | w.setColorSuccess() | |
659 | w.writeLn("1 monitor detected") | |
660 | else | |
661 | w.setColorSuccess() | |
662 | w.writeLn(#monitors .. " Monitors detected") | |
663 | end | |
664 | ||
665 | if ship == nil or ship.isInterfaced() == nil then | |
666 | w.setColorDisabled() | |
667 | w.writeLn("No ship controller detected") | |
668 | else | |
669 | w.setColorSuccess() | |
670 | w.writeLn("Ship controller detected") | |
671 | if isBooting then | |
672 | ship_boot() | |
673 | end | |
674 | end | |
675 | ||
676 | w.writeLn("") | |
677 | w.setColorNormal() | |
678 | w.writeLn("This is a keyboard controlled user interface.") | |
679 | w.write("Key controls are written like so: ") | |
680 | w.setColorControl() | |
681 | w.write("Action (key)") | |
682 | w.setColorNormal() | |
683 | w.writeLn(".") | |
684 | w.write("For example, typing ") | |
685 | w.setColorControl() | |
686 | w.write(" 1 ") | |
687 | w.setColorNormal() | |
688 | w.writeLn(" will open Ship controls.") | |
689 | end | |
690 | ||
691 | ----------- Boot sequence | |
692 | ||
693 | w.page_setEndText(" Home (0), Controls (1), Crew (2), Navigation (3)") | |
694 | w.page_register('0', connections_page, nil) | |
695 | w.page_register('1', ship_page_controls, ship_key_controls) | |
696 | w.page_register('2', ship_page_crew, ship_key_crew) | |
697 | w.page_register('3', ship_page_navigation, ship_key_navigation) | |
698 | ship_register() | |
699 | ||
700 | w.boot() | |
701 | local success, message = pcall(w.run) | |
702 | if not success then | |
703 | print("failed with message") | |
704 | print(message) | |
705 | w.sleep(3.0) | |
706 | print("rebooting...") | |
707 | w.reboot() | |
708 | else | |
709 | if ship ~= nil then | |
710 | ship.command("OFFLINE", true) | |
711 | ship.enable(false) | |
712 | end | |
713 | ||
714 | w.close() | |
715 | end |