Advertisement
Guest User

startup

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