Advertisement
infiniteblock

Untitled

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