Guest User

startup

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