Advertisement
OmegaRogue

warpdrive stand

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