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