Guest User

startup

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