Skiret

startup

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