Advertisement
Guest User

edit

a guest
Sep 17th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.13 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_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()
  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.   ship_movement[1] = ship_page_setDistanceAxis(4, "Forward/back", "Forward", "Backward", ship_movement[1], math.abs(ship_front + ship_back  + 1), maxJumpDistance)
  247.   ship_movement[2] = ship_page_setDistanceAxis(6, "Up/down"     , "Up"     , "Down"    , ship_movement[2], math.abs(ship_up    + ship_down  + 1), maxJumpDistance)
  248.   ship_movement[3] = ship_page_setDistanceAxis(8, "Right/left"  , "Right"  , "Left"    , ship_movement[3], math.abs(ship_left  + ship_right + 1), maxJumpDistance)
  249.   ship_movement = { ship.movement(ship_movement[1], ship_movement[2], ship_movement[3]) }
  250.   ship_updateMovementStats()
  251. end
  252.  
  253. function ship_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength, maxJumpDistance)
  254.   local maximumDistance = math.floor(shipLength + maxJumpDistance)
  255.   w.setCursorPos(1, line + 2)
  256.   w.setColorHelp()
  257.   w.writeFullLine(" Enter between " .. math.floor( shipLength + 1) .. " and " ..  maximumDistance .. " to move " ..  positive .. ".")
  258.   w.writeFullLine(" Enter 0 to keep position on this axis.")
  259.   w.writeFullLine(" Enter between " .. -maximumDistance .. " and " .. math.floor(-shipLength - 1) .. " to move " ..  negative .. ".")
  260.  
  261.   repeat
  262.     w.setCursorPos(1, line)
  263.     w.setColorNormal()
  264.     w.write(axis .. " movement: ")
  265.     userEntry = w.input_readInteger(userEntry)
  266.     if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then
  267.       w.status_showWarning("Wrong distance. Try again.")
  268.     end
  269.   until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance)
  270.   w.setCursorPos(1, line + 2)
  271.   w.clearLine()
  272.   w.setCursorPos(1, line + 3)
  273.   w.clearLine()
  274.   w.setCursorPos(1, line + 4)
  275.   w.clearLine()
  276.  
  277.   return userEntry
  278. end
  279.  
  280. function ship_page_setRotation()
  281.   local inputAbort = false
  282.   w.page_begin("<==== Set ship rotation ====>")
  283.   w.setCursorPos(1, 8)
  284.   w.setColorHelp()
  285.   w.writeFullLine(" Select ship rotation (Up, Down, Left, Right).")
  286.   w.writeFullLine(" Select Front to keep current orientation.")
  287.   w.writeFullLine(" Press Enter to save your selection.")
  288.   repeat
  289.     w.setCursorPos(1, 3)
  290.     w.setColorNormal()
  291.     ship_writeRotation()
  292.     local params = { os.pullEventRaw() }
  293.     local eventName = params[1]
  294.     local address = params[2]
  295.     if address == nil then address = "none" end
  296.     if eventName == "key" then
  297.       local keycode = params[2]
  298.       if keycode == 200 then
  299.         ship_rotationSteps = 0
  300.       elseif keycode == 203 then
  301.         ship_rotationSteps = 3
  302.       elseif keycode == 205 then
  303.         ship_rotationSteps = 1
  304.       elseif keycode == 208 then
  305.         ship_rotationSteps = 2
  306.       elseif keycode == 28 then
  307.         inputAbort = true
  308.       else
  309.         w.status_showWarning("Key " .. keycode .. " is invalid")
  310.       end
  311.     elseif eventName == "terminate" then
  312.       inputAbort = true
  313.     elseif not w.event_handler(eventName, params[2]) then
  314.       w.status_showWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  315.     end
  316.   until inputAbort
  317.   ship_rotationSteps = ship.rotationSteps(ship_rotationSteps)
  318. end
  319.  
  320. function ship_page_setDimensions()
  321.   w.page_begin("<==== Set ship dimensions ====>")
  322.   w.setCursorPos(1, 14)
  323.   w.setColorHelp()
  324.   w.writeFullLine(" Enter ship size in blocks (0-9).")
  325.   w.writeFullLine(" First block next to Ship counts as 1.")
  326.   w.writeFullLine(" ")
  327.   w.writeFullLine(" Press Enter to save your selection.")
  328.  
  329.   w.setCursorPos(1, 3)
  330.   w.setColorNormal()
  331.   w.write(" Front (".. w.format_integer(ship_front) ..") : ")
  332.   ship_front = w.input_readInteger(ship_front)
  333.   w.write(" Right (".. w.format_integer(ship_right) ..") : ")
  334.   ship_right = w.input_readInteger(ship_right)
  335.   w.write(" Up    (".. w.format_integer(ship_up) ..") : ")
  336.   ship_up = w.input_readInteger(ship_up)
  337.   w.write(" Back  (".. w.format_integer(ship_back) ..") : ")
  338.   ship_back = w.input_readInteger(ship_back)
  339.   w.write(" Left  (".. w.format_integer(ship_left) ..") : ")
  340.   ship_left = w.input_readInteger(ship_left)
  341.   w.write(" Down  (".. w.format_integer(ship_down) ..") : ")
  342.   ship_down = w.input_readInteger(ship_down)
  343.   w.write("Setting dimensions...")
  344.   ship_front, ship_right, ship_up = ship.dim_positive(ship_front, ship_right, ship_up)
  345.   ship_back, ship_left, ship_down = ship.dim_negative(ship_back, ship_left, ship_down)
  346. end
  347.  
  348. function ship_page_summon() -- no longer used
  349.   w.page_begin("<==== Summon players ====>")
  350.   local stringPlayers = ship.getAttachedPlayers()
  351.   if stringPlayers == "" then
  352.     w.writeLn("~ no players registered ~")
  353.     w.writeLn("")
  354.     w.setColorHelp()
  355.     w.writeFullLine(" Press enter to exit.")
  356.     w.setColorNormal()
  357.     w.input_readInteger("")
  358.     return
  359.   end
  360.   local arrayPlayers = w.data_splitString(stringPlayers, ",")
  361.   for i = 1, #arrayPlayers do
  362.     w.writeLn(i .. ". " .. arrayPlayers[i])
  363.   end
  364.   w.setColorHelp()
  365.   w.writeFullLine(" Enter player number")
  366.   w.writeFullLine(" or press enter to summon everyone.")
  367.   w.setColorNormal()
  368.  
  369.   w.write(":")
  370.   ship.command("SUMMON", false)
  371.   local input = w.input_readInteger("")
  372.   if input == "" then
  373.     ship.targetName("")
  374.   else
  375.     input = tonumber(input)
  376.     ship.targetName(arrayPlayers[input - 1])
  377.   end
  378.   ship.command("SUMMON", true)
  379. end
  380.  
  381. function ship_page_jumpToGate()
  382.   w.page_begin("<==== Jump through Jumpgate ====>")
  383.   w.writeLn("")
  384.   w.writeLn("Your ship should be already inside a jumpgate")
  385.  
  386.   w.setCursorPos(1, 16)
  387.   w.setColorHelp()
  388.   w.writeFullLine(" Enter target jumpgate name (a-z, 0-9).")
  389.   w.writeFullLine(" Press enter to save jumpgate name.")
  390.  
  391.   w.setCursorPos(1, 5)
  392.   w.setColorNormal()
  393.   w.write("Target jumpgate name: ")
  394.   local targetName = w.input_readText("")
  395.   -- rs.setOutput(alarm_side, true)
  396.   if w.input_readConfirmation("Engage gate jumping? (Y/n)") then
  397.     -- rs.setOutput(alarm_side, false)
  398.     ship.command("GATE", false)
  399.     ship.targetName(targetName)
  400.     ship.command("GATE", true)
  401.     -- ship = nil
  402.   end
  403.   -- rs.setOutput(alarm_side, false)
  404. end
  405.  
  406. function ship_page_controls()
  407.   w.page_begin(w.data_getName() .. " - Ship controls")
  408.   if ship == nil or ship.isInterfaced() == nil then
  409.     w.status_showWarning("No ship controller detected")
  410.   else
  411.     local isValid, message = ship.getAssemblyStatus()
  412.     if isValid ~= true then
  413.       w.status_showWarning(message)
  414.     else
  415.       local isEnabled = ship.enable()
  416.       if not isEnabled then
  417.         ship.command("MANUAL", false)
  418.         ship_updateMovementStats()
  419.       end
  420.     end
  421.    
  422.     w.setCursorPos(1, 2)
  423.     w.writeLn("Ship:")
  424.     w.writeLn(" Current position = " .. w.format_integer(ship_x) .. ", " .. w.format_integer(ship_y) .. ", " .. w.format_integer(ship_z))
  425.     local energyStored, energyMax, energyUnits = ship.getEnergyStatus()
  426.     if energyStored == nil then energyStored = 0 end
  427.     if energyMax == nil or energyMax == 0 then energyMax = 1 end
  428.     w.writeLn(" Energy           = " .. math.floor(energyStored / energyMax * 100) .. " % (" .. w.format_integer(energyStored) .. " " .. energyUnits .. ")")
  429.    
  430.     w.writeLn("")
  431.     w.writeLn("Dimensions:")
  432.     w.writeLn(" Front, Right, Up = " .. w.format_integer(ship_front) .. ", " .. w.format_integer(ship_right) .. ", " .. w.format_integer(ship_up) .. " blocks")
  433.     w.writeLn(" Back, Left, Down = " .. w.format_integer(ship_back) .. ", " .. w.format_integer(ship_left) .. ", " .. w.format_integer(ship_down) .. " blocks")
  434.     local shipMass, shipVolume = ship.getShipSize()
  435.     if shipMass == nil then
  436.       shipMass = 0
  437.       shipVolume = 0
  438.     end
  439.     w.write(" Mass, Volume     = ")
  440.     if shipMass == 0 then
  441.       w.write("?")
  442.     else
  443.       w.write(w.format_integer(shipMass))
  444.     end
  445.     w.write(" tons, ")
  446.     if shipVolume == 0 then
  447.       w.write("?")
  448.     else
  449.       w.write(w.format_integer(shipVolume))
  450.     end
  451.     w.writeLn(" blocks")
  452.    
  453.     if isValid == true then
  454.       w.writeLn("")
  455.       w.writeLn("Warp data:")
  456.       ship_writeMovement(" Movement         = ")
  457.       w.writeLn(" Distance         = " .. w.format_integer(ship_actualDistance) .. " m (" .. w.format_integer(ship_energyRequired) .. " " .. energyUnits .. ", " .. math.floor(energyStored / ship_energyRequired) .. " jumps)")
  458.       w.writeLn(" Target position  = " .. w.format_integer(ship_xTarget) .. ", " .. w.format_integer(ship_yTarget) .. ", " .. w.format_integer(ship_zTarget))
  459.     end
  460.   end
  461.  
  462.   w.setCursorPos(1, 16)
  463.   w.setColorControl()
  464.   w.writeFullLine(" set ship Name (N), dImensions (I), Movement (M)")
  465.   if ship_isInHyper then
  466.     w.writeFullLine(" Jump to move ship (M/J), exit Hyperspace (H)")
  467.   else
  468.     w.writeFullLine(" Jump to move ship (M/J), enter Hyperspace (H)")
  469.   end
  470. end
  471.  
  472. function ship_key_controls(character, keycode)
  473.   if character == 'm' or character == 'M' then
  474.     ship_page_setMovement()
  475.     ship_page_setRotation()
  476.     ship_warp()
  477.     return true
  478.   elseif character == 'i' or character == 'I' then
  479.     ship_page_setDimensions()
  480.     return true
  481.   elseif character == 'j' or character == 'J' then
  482.     ship_warp()
  483.     return true
  484.   elseif character == 'h' or character == 'H' then
  485.     -- rs.setOutput(alarm_side, true)
  486.     local isConfirmed
  487.     if ship_isInHyper then
  488.       isConfirmed = w.input_readConfirmation("Disengage hyperdrive? (Y/n)")
  489.     else
  490.       isConfirmed = w.input_readConfirmation("Engage hyperdrive? (Y/n)")
  491.     end
  492.     if isConfirmed then
  493.       -- rs.setOutput(alarm_side, false)
  494.       ship.command("HYPERDRIVE", true)
  495.       ship_updateMovementStats()
  496.       -- ship = nil
  497.     end
  498.     -- rs.setOutput(alarm_side, false)
  499.     return true
  500.   elseif character == 'n' or character == 'N' then
  501.     w.data_setName()
  502.     return true
  503.   end
  504.   return false
  505. end
  506.  
  507. function ship_writeArray(arrayValues, indexSelected)
  508.   if indexSelected then
  509.     indexSelected = (indexSelected + #arrayValues) % #arrayValues
  510.   end
  511.  
  512.   local indexSplit = math.ceil(#arrayValues / 2)
  513.   for i = 1, indexSplit do
  514.     if indexSelected and i == indexSelected + 1 then
  515.       w.setColorSelected()
  516.       w.write(">" .. string.sub(arrayValues[i] .. "                        ", 1, 24))
  517.       w.setColorNormal()
  518.     else
  519.       w.write(" " .. string.sub(arrayValues[i] .. "                        ", 1, 24))
  520.     end
  521.     if arrayValues[i + indexSplit] ~= nil then
  522.       if indexSelected and i + indexSplit == indexSelected + 1 then
  523.         w.setColorSelected()
  524.         w.writeLn(">" .. string.sub(arrayValues[i + indexSplit] .. "                        ", 1, 24))
  525.         w.setColorNormal()
  526.       else
  527.         w.writeLn(" " .. arrayValues[i + indexSplit])
  528.       end
  529.     else
  530.       w.writeLn("")
  531.     end
  532.   end
  533.   return indexSelected
  534. end
  535.  
  536. function ship_page_crew()
  537.   w.page_begin(w.data_getName() .. " - Ship crew")
  538.   if ship == nil or ship.isInterfaced() == nil then
  539.     w.status_showWarning("No ship controller detected")
  540.   else
  541.     local isValid, message = ship.getAssemblyStatus()
  542.     if isValid ~= true then
  543.       w.status_showWarning(message)
  544.     else
  545.       w.writeLn("Attached players:")
  546.       -- local stringPlayers, _ = ship.getAttachedPlayers()
  547.       if stringPlayers == nil or stringPlayers == "" then
  548.         stringPlayers = "~ no registered player ~"
  549.       end
  550.       ship_arrayPlayers = w.data_splitString(stringPlayers, ",")
  551.       ship_indexPlayer = ship_writeArray(ship_arrayPlayers, ship_indexPlayer)
  552.     end
  553.   end
  554.  
  555.   w.setCursorPos(1, 16)
  556.   w.setColorControl()
  557.   w.writeFullLine(" Summon all crew (S)")
  558.   w.writeFullLine(" select crew (Up, Down), summon slctd crew (enter)")
  559. end
  560.  
  561. function ship_key_crew(character, keycode)
  562.   if character == 's' or character == 'S' then -- S
  563.     ship.command("SUMMON", false)
  564.     ship.targetName("")
  565.     ship.command("SUMMON", true)
  566.     return true
  567.   elseif keycode == 28 then -- Enter
  568.     local namePlayer = ship_arrayPlayers[ship_indexPlayer + 1]
  569.     ship.command("SUMMON", false)
  570.     ship.targetName(namePlayer)
  571.     ship.command("SUMMON", true)
  572.     w.status_showSuccess("Engaging teleportation for " .. namePlayer .. "...")
  573.     return true
  574.   elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  575.     ship_indexPlayer = ship_indexPlayer - 1
  576.     return true
  577.   elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  578.     ship_indexPlayer = ship_indexPlayer + 1
  579.     return true
  580.   end
  581.   return false
  582. end
  583.  
  584. function ship_page_navigation()
  585.   w.page_begin(w.data_getName() .. " - Ship navigation")
  586.   if ship == nil or ship.isInterfaced() == nil then
  587.     w.status_showWarning("No ship controller detected")
  588.   else
  589.     local isValid, message = ship.getAssemblyStatus()
  590.     if isValid ~= true then
  591.       w.status_showWarning(message)
  592.     else
  593.       local locationCurrent = "somewhere..."  -- @TODO ship.getLocation()
  594.       w.writeLn("Current ship location        : " .. locationCurrent)
  595.       w.writeLn("Jumpgates or beacons in range:")
  596.       local stringTargets, _ = "not implemented", nil -- ship.getTargets()
  597.       if stringTargets == nil or stringTargets == "" then
  598.         stringTargets = "~ no beacon nor jumpgate in range ~"
  599.       end
  600.       local arrayTargets = w.data_splitString(stringTargets, ",")
  601.       ship_indexTarget = ship_writeArray(arrayTargets, ship_indexTarget)
  602.     end
  603.   end
  604.  
  605.   w.setCursorPos(1, 16)
  606.   w.setColorControl()
  607.   w.writeFullLine(" select target (Up, Down), register target (enter)")
  608.   w.writeFullLine(" jump through Gate (G)")
  609. end
  610.  
  611. function ship_key_navigation(character, keycode)
  612.   if keycode == 28 then -- Enter
  613. --    local success, xxx = ship.xxx(ship_indexTarget)
  614. --    if success then
  615. --      w.status_showSuccess("Engaging jumpgate jump to " .. xxx .. "...")
  616. --    else
  617. --      w.status_showWarning("Failed to summon crew member")
  618. --    end
  619.     return true
  620. --  elseif character == 'b' or character == 'B' then -- B
  621. --    ship_page_jumpToBeacon()
  622. --    return true
  623.   elseif character == 'g' or character == 'G' then -- G
  624.     ship_page_jumpToGate()
  625.     return true
  626.   elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  627.     ship_indexTarget = ship_indexTarget - 1
  628.     return true
  629.   elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  630.     ship_indexTarget = ship_indexTarget + 1
  631.     return true
  632.   end
  633.   return false
  634. end
  635.  
  636. function ship_register()
  637.   w.device_register("warpdriveShipController",
  638.       function(deviceType, address, wrap) ship = wrap end,
  639.       function() end)
  640.   w.device_register("warpdriveShipCore",
  641.       function(deviceType, address, wrap) ship = wrap end,
  642.       function() end)
  643.   w.event_register("shipCoreCooldownDone"  , function() w.status_showWarning("Ship core cooldown done")   return false end )
  644.   w.data_register("ship", ship_read, nil, ship_name)
  645. end
  646.  
  647. ----------- connections status
  648.  
  649. function connections_page(isBooting)
  650.   w.page_begin(w.data_getName() .. " - Connections")
  651.  
  652.   w.writeLn("")
  653.  
  654.   local monitors = w.device_getMonitors()
  655.   if #monitors == 0 then
  656.     w.setColorDisabled()
  657.     w.writeLn("No Monitor detected")
  658.   elseif #monitors == 1 then
  659.     w.setColorSuccess()
  660.     w.writeLn("1 monitor detected")
  661.   else
  662.     w.setColorSuccess()
  663.     w.writeLn(#monitors .. " Monitors detected")
  664.   end
  665.  
  666.   if ship == nil or ship.isInterfaced() == nil then
  667.     w.setColorDisabled()
  668.     w.writeLn("No ship controller detected")
  669.   else
  670.     w.setColorSuccess()
  671.     w.writeLn("Ship controller detected")
  672.     if isBooting then
  673.       ship_boot()
  674.     end
  675.   end
  676.  
  677.   w.writeLn("")
  678.   w.setColorNormal()
  679.   w.writeLn("This is a keyboard controlled user interface.")
  680.   w.write("Key controls are written like so: ")
  681.   w.setColorControl()
  682.   w.write("Action (key)")
  683.   w.setColorNormal()
  684.   w.writeLn(".")
  685.   w.write("For example, typing ")
  686.   w.setColorControl()
  687.   w.write(" 1 ")
  688.   w.setColorNormal()
  689.   w.writeLn(" will open Ship controls.")
  690. end
  691.  
  692. ----------- Boot sequence
  693.  
  694. w.page_setEndText(" Home (0), Controls (1), Crew (2), Navigation (3)")
  695. w.page_register('0', connections_page, nil)
  696. w.page_register('1', ship_page_controls, ship_key_controls)
  697. w.page_register('2', ship_page_crew, ship_key_crew)
  698. w.page_register('3', ship_page_navigation, ship_key_navigation)
  699. ship_register()
  700.  
  701. w.boot()
  702. local success, message = pcall(w.run)
  703. if not success then
  704.   print("failed with message")
  705.   print(message)
  706.   w.sleep(3.0)
  707.   print("rebooting...")
  708.   w.reboot()
  709. else
  710.   if ship ~= nil then
  711.     ship.command("OFFLINE", true)
  712.     ship.enable(false)
  713.   end
  714.  
  715.   w.close()
  716. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement