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