SHARE
TWEET

Untitled

infiniteblock Nov 14th, 2019 (edited) 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if not term.isColor() then
  2.   print("Advanced computer required")
  3.   error()
  4. end
  5. print("loading...")
  6.  
  7. monitor_textScale = 0.5
  8.  
  9. Style = {
  10.   CDefault = colors.black,
  11.   BGDefault = colors.lightGray,
  12.  
  13.   CTitle = colors.orange,
  14.   BGTitle = colors.black,
  15.  
  16.   CFooter =  colors.white,
  17.   BGFooter = colors.blue,
  18.  
  19.   CWarning = colors.white,
  20.   BGWarning = colors.red,
  21.  
  22.   CSuccess = colors.white,
  23.   BGSuccess = colors.lime,
  24.  
  25.   CDisabled = colors.gray,
  26.   BGDisabled = colors.blue,
  27.  
  28.   CRadarmap = colors.gray,
  29.   BGRadarmap = colors.green,
  30.  
  31.   CRadarborder = colors.white,
  32.   BGRadarborder = colors.black,
  33.  
  34.   CRadarself = colors.white,
  35.   BGRadarself = colors.lime,
  36.   TextRadarself = "R",
  37.  
  38.   CRadarother = colors.black,
  39.   BGRadarother = colors.red,
  40.   TextRadarother = "#"
  41. }
  42.  
  43. ----------- Monitor support
  44.  
  45. function SetMonitorColorFrontBack(frontColor, backgroundColor)
  46.   term.setBackgroundColor(backgroundColor)
  47.   term.setTextColor(frontColor)
  48.   if monitors ~= nil then
  49.     for key,monitor in pairs(monitors) do
  50.       monitor.setTextColor(frontColor)
  51.       monitor.setBackgroundColor(backgroundColor)
  52.     end
  53.   end
  54. end
  55.  
  56. function Write(text)
  57.   term.write(text)
  58.   if monitors ~= nil then
  59.     for key,monitor in pairs(monitors) do
  60.       if key ~= data.radar_monitorIndex then
  61.         monitor.write(text)
  62.       end
  63.     end
  64.   end
  65. end
  66. local function getCursorPos()
  67.   local x, y = term.getCursorPos()
  68.   return x, y
  69. end
  70.  
  71. function SetCursorPos(x, y)
  72.   term.setCursorPos(x, y)
  73.   if monitors ~= nil then
  74.     for key,monitor in pairs(monitors) do
  75.       if key ~= data.radar_monitorIndex then
  76.         monitor.setCursorPos(x, y)
  77.       end
  78.     end
  79.   end
  80. end
  81.  
  82. local function getResolution()
  83.   local sizeX, sizeY = term.getSize()
  84.   return sizeX, sizeY
  85. end
  86.  
  87. function SetColorDefault()
  88.   SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  89. end
  90.  
  91. function SetColorTitle()
  92.   SetMonitorColorFrontBack(Style.CTitle, Style.BGTitle)
  93. end
  94.  
  95. function SetColorFooter()
  96.   SetMonitorColorFrontBack(Style.CFooter, Style.BGFooter)
  97. end
  98.  
  99. function SetColorWarning()
  100.   SetMonitorColorFrontBack(Style.CWarning, Style.BGWarning)
  101. end
  102.  
  103. function SetColorSuccess()
  104.   SetMonitorColorFrontBack(Style.CSuccess, Style.BGSuccess)
  105. end
  106.  
  107. function SetColorDisabled()
  108.   SetMonitorColorFrontBack(Style.CDisabled, Style.BGDisabled)
  109. end
  110.  
  111. function SetColorNormal()
  112.   SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  113. end
  114.  
  115. function SetColorRadarmap()
  116.   SetMonitorColorFrontBack(Style.CRadarmap, Style.BGRadarmap)
  117. end
  118.  
  119. function SetColorRadarborder()
  120.   SetMonitorColorFrontBack(Style.CRadarborder, Style.BGRadarborder)
  121. end
  122.  
  123. local function clear(colorFront, colorBack)
  124.   clearWarningTick = -1
  125.   if CDefault == nil or BGDefault == nil then
  126.   SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  127.   else
  128.       SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  129.   end
  130.   term.clear()
  131.   if monitors ~= nil then
  132.     for key, monitor in pairs(monitors) do
  133.       if key ~= data.radar_monitorIndex then
  134.         monitor.clear()
  135.       end
  136.     end
  137.   end
  138.   SetCursorPos(1, 1)
  139. end
  140.  
  141. local function clearLine()
  142.   term.clearLine()
  143.   if monitors ~= nil then
  144.     for key, monitor in pairs(monitors) do
  145.       if key ~= data.radar_monitorIndex then
  146.         monitor.clearLine()
  147.       end
  148.     end
  149.   end
  150.   local x, y = getCursorPos()
  151.   SetCursorPos(1, y)
  152. end
  153.  
  154. function WriteLn(text)
  155.   Write(text)
  156.   local x, y = term.getCursorPos()
  157.   local width, height = term.getSize()
  158.   if y > height - 1 then
  159.     y = 1
  160.   end
  161.   SetCursorPos(1, y + 1)
  162. end
  163.  
  164. function WriteCentered(y, text)
  165.   local unused
  166.   if text == nil then
  167.     text = y
  168.     unused, y = getCursorPos()
  169.   end
  170.  
  171.   SetCursorPos((51 - text:len()) / 2, y)
  172.   term.write(text)
  173.   if monitors ~= nil then
  174.     for key,monitor in pairs(monitors) do
  175.       if key ~= data.radar_monitorIndex then
  176.         local sizeX, sizeY = monitor.getSize()
  177.         if sizeX ~= nil then
  178.         monitor.setCursorPos((sizeX - text:len()) / 2, y)
  179.         monitor.write(text)
  180.        end
  181.   end
  182.   end
  183.  end
  184.   local xt, yt = term.getCursorPos()
  185.   SetCursorPos(1, y + 1)
  186. end
  187. local function page_begin(text)
  188.   clear()
  189.   SetColorTitle()
  190.   SetCursorPos(1, 1)
  191.   SetColorTitle()
  192.   clearLine()
  193.   WriteCentered(1, text)
  194.   SetCursorPos(1, 2)
  195.   SetColorNormal()
  196. end
  197. function ShowTitle(text)
  198.   clear()
  199.   SetCursorPos(0, 1)
  200.   SetColorTitle()
  201.   clearLine()
  202.   WriteCentered(1, text)
  203.   SetCursorPos(1, 2)
  204.   SetColorDefault()
  205. end
  206.  
  207. function ShowMenu(text)
  208.   Write(text)
  209.   local sizeX, sizeY = term.getSize()
  210.   local xt, yt = term.getCursorPos()
  211.   for i = xt, sizeX do
  212.     Write(" ")
  213.   end
  214.   SetCursorPos(1, yt + 1)
  215. end
  216.  
  217. local clearWarningTick = -1
  218. function ShowWarning(text)
  219.   local sizeX, sizeY = term.getSize()
  220.   SetCursorPos(1, sizeY)
  221.   clearLine()
  222.   SetColorWarning()
  223.   SetCursorPos((sizeX - text:len() - 2) / 2, sizeY)
  224.   Write(" " .. text .. " ")
  225.   SetColorDefault()
  226.   clearWarningTick = 5
  227. --  clearLine()
  228. end
  229. function ClearWarning()
  230.   if clearWarningTick > 0 then
  231.     clearWarningTick = clearWarningTick - 1
  232.   elseif clearWarningTick == 0 then
  233.     SetColorDefault()
  234.     local sizeX, sizeY = term.getSize()
  235.     SetCursorPos(1, sizeY)
  236.     clearLine()
  237.     clearWarningTick = -1
  238.   end
  239. end
  240.  
  241. ----------- Formatting & popups
  242.  
  243. function FormatFloat(value, nbchar)
  244.   local str = "?"
  245.   if value ~= nil then
  246.     str = string.format("%g", value)
  247.   end
  248.   if nbchar ~= nil then
  249.     str = string.sub("               " .. str, -nbchar)
  250.   end
  251.   return str
  252. end
  253. function FormatInteger(value, nbchar)
  254.   local str = "?"
  255.   if value ~= nil then
  256.     str = string.format("%d", value)
  257.   end
  258.   if nbchar ~= nil then
  259.     str = string.sub("               " .. str, -nbchar)
  260.   end
  261.   return str
  262. end
  263.  
  264. function boolToYesNo(bool)
  265.   if bool then
  266.     return "YES"
  267.   else
  268.     return "no"
  269.   end
  270. end
  271.  
  272. function readInputNumber(currentValue)
  273.   local inputAbort = false
  274.   local input = string.format(currentValue)
  275.   if input == "0" then
  276.     input = ""
  277.   end
  278.   local x, y = term.getCursorPos()
  279.   repeat
  280.     ClearWarning()
  281.     SetColorDefault()
  282.     SetCursorPos(x, y)
  283.     Write(input .. "            ")
  284.     input = string.sub(input, -9)
  285.    
  286.     local params = { os.pullEventRaw() }
  287.     local eventName = params[1]
  288.     local address = params[2]
  289.     if address == nil then address = "none" end
  290.     if eventName == "key" then
  291.       local keycode = params[2]
  292.       if keycode >= 2 and keycode <= 10 then -- 1 to 9
  293.         input = input .. string.format(keycode - 1)
  294.       elseif keycode == 11 or keycode == 82 then -- 0 & keypad 0
  295.         input = input .. "0"
  296.       elseif keycode >= 79 and keycode <= 81 then -- keypad 1 to 3
  297.         input = input .. string.format(keycode - 78)
  298.       elseif keycode >= 75 and keycode <= 77 then -- keypad 4 to 6
  299.         input = input .. string.format(keycode - 71)
  300.       elseif keycode >= 71 and keycode <= 73 then -- keypad 7 to 9
  301.         input = input .. string.format(keycode - 64)
  302.       elseif keycode == 14 then -- Backspace
  303.         input = string.sub(input, 1, string.len(input) - 1)
  304.       elseif keycode == 211 then -- Delete
  305.         input = ""
  306.       elseif keycode == 28 then -- Enter
  307.         inputAbort = true
  308.       elseif keycode == 74 or keycode == 12 or keycode == 49 then -- - on numeric keypad or - on US top or n letter
  309.         if string.sub(input, 1, 1) == "-" then
  310.           input = string.sub(input, 2)
  311.         else
  312.           input = "-" .. input
  313.         end
  314.       elseif char == 43 or keycode == 78 then -- +
  315.         if string.sub(input, 1, 1) == "-" then
  316.           input = string.sub(input, 2)
  317.         end
  318.       else
  319.         ShowWarning("Key " .. keycode .. " is invalid")
  320.       end
  321.     elseif eventName == "terminate" then
  322.       inputAbort = true
  323.     elseif not common_event(eventName, params[2]) then
  324.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  325.     end
  326.   until inputAbort
  327.   SetCursorPos(1, y + 1)
  328.   if input == "" or input == "-" then
  329.     return currentValue
  330.   else
  331.     return tonumber(input)
  332.   end
  333. end
  334. function readInput(currentValue)
  335.   local inputAbort = false
  336.   local input = string.format(currentValue)
  337.   local x, y = term.getCursorPos()
  338.   Write(input)
  339.   os.pullEventRaw() -- skip first char event
  340.   repeat
  341.     ClearWarning()
  342.     SetColorDefault()
  343.     SetCursorPos(x, y)
  344.     Write(input .. "                              ")
  345.     input = string.sub(input, -123)
  346.    
  347.     local params = { os.pullEventRaw() }
  348.     local eventName = params[1]
  349.     local address = params[2]
  350.     if address == nil then address = "none" end
  351.     if eventName == "key" then
  352.       local keycode = params[2]
  353.       if keycode == 14 then -- Backspace
  354.         input = string.sub(input, 1, string.len(input) - 1)
  355.       elseif keycode == 211 then -- Delete
  356.         input = ""
  357.       elseif keycode == 28 then -- Enter
  358.         inputAbort = true
  359.         end
  360.     elseif eventName == "char" then
  361.       local char = params[2]
  362.       if char >= ' ' and char <= '~' then -- 1 to 9
  363.         input = input .. char
  364.       end
  365.     elseif eventName == "terminate" then
  366.       inputAbort = true
  367.     elseif eventName == "paste" then
  368.       input = params[2]
  369.     elseif not common_event(eventName, params[2]) then
  370.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  371.     end
  372.   until inputAbort
  373.   SetCursorPos(1, y + 1)
  374.   if input == "" then
  375.     return currentValue
  376.   else
  377.     return input
  378.   end
  379. end
  380. function readInputText(currentValue)
  381.   local inputAbort = false
  382.   local input = string.format(currentValue)
  383.   local x, y = term.getCursorPos()
  384.   Write(input)
  385.   os.pullEventRaw() -- skip first char event
  386.   repeat
  387.     ClearWarning()
  388.     SetColorDefault()
  389.     SetCursorPos(x, y)
  390.     Write(input .. "                              ")
  391.     input = string.sub(input, -30)
  392.    
  393.     local params = { os.pullEventRaw() }
  394.     local eventName = params[1]
  395.     local address = params[2]
  396.     if address == nil then address = "none" end
  397.     if eventName == "key" then
  398.       local keycode = params[2]
  399.       if keycode == 14 then -- Backspace
  400.         input = string.sub(input, 1, string.len(input) - 1)
  401.       elseif keycode == 211 then -- Delete
  402.         input = ""
  403.       elseif keycode == 28 then -- Enter
  404.         inputAbort = true
  405.       else
  406.         ShowWarning("Key " .. keycode .. " is invalid")
  407.       end
  408.     elseif eventName == "char" then
  409.       local char = params[2]
  410.       if char >= ' ' and char <= '~' then -- 1 to 9
  411.         input = input .. char
  412.       else
  413.         ShowWarning("Char #" .. string.byte(char) .. " is invalid")
  414.       end
  415.     elseif eventName == "terminate" then
  416.       inputAbort = true
  417.     elseif not common_event(eventName, params[2]) then
  418.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  419.     end
  420.   until inputAbort
  421.   SetCursorPos(1, y + 1)
  422.   if input == "" then
  423.     return currentValue
  424.   else
  425.     return input
  426.   end
  427. end
  428.  
  429. function readConfirmation(msg)
  430.   if msg == nil then
  431.     ShowWarning("Are you sure? (y/n)")
  432.   else
  433.     ShowWarning(msg)
  434.   end
  435.   repeat
  436.     local params = { os.pullEventRaw() }
  437.     local eventName = params[1]
  438.     local address = params[2]
  439.     if address == nil then address = "none" end
  440.     if eventName == "key" then
  441.       local keycode = params[2]
  442.       if keycode == 21 then -- Y
  443.         return true
  444.       else
  445.         return false
  446.       end
  447.     elseif eventName == "terminate" then
  448.       return false
  449.     elseif not common_event(eventName, params[2]) then
  450.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  451.     end
  452.   until false
  453. end
  454.  
  455. ----------- commons: menu, event handlers, etc.
  456.  
  457. function common_event(eventName, param)
  458.   if eventName == "redstone" then
  459.     redstone_event(param)
  460.   elseif eventName == "timer" then
  461.     if param == radar_timerId then
  462.       radar_timerEvent()
  463.     end
  464.   elseif eventName == "shipCoreCooldownDone" then
  465.     ShowWarning("Ship core cooldown done")
  466.   elseif eventName == "reactorPulse" then
  467.     reactor_pulse(param)
  468. --  elseif eventName == "reactorDeactivation" then
  469. --    ShowWarning("Reactor deactivated")
  470. --  elseif eventName == "reactorActivation" then
  471. --    ShowWarning("Reactor activated")
  472.   elseif eventName == "char" then
  473.   elseif eventName == "key_up" then
  474.   elseif eventName == "mouse_click" then
  475.   elseif eventName == "mouse_up" then
  476.   elseif eventName == "mouse_drag" then
  477.   elseif eventName == "monitor_touch" then
  478.   elseif eventName == "monitor_resize" then
  479.   elseif eventName == "peripheral" then
  480.   elseif eventName == "peripheral_detach" then
  481.   else
  482.     return false
  483.   end
  484.   return true
  485. end
  486.  
  487. function menu_common()
  488.   SetCursorPos(1, 17)
  489.   SetColorFooter()
  490.   ShowMenu("0 Home 1 Ship 2 Cloak 3 Shield 4 Radar 5 Mining                      ")
  491.   ShowMenu("6 Transport 7 Reactor 8 Hooks X eXit                                   ")
  492.   SetColorDefault()
  493. end
  494.  
  495. ----------- Redstone support
  496.  
  497. local tblRedstoneState = {-- Remember redstone state on each side
  498.   ["top"] = rs.getInput("top"),
  499.   ["front"] = rs.getInput("front"),
  500.   ["left"] = rs.getInput("left"),
  501.   ["right"] = rs.getInput("right"),
  502.   ["back"] = rs.getInput("back"),
  503.   ["bottom"] = rs.getInput("bottom"),
  504. }
  505. local tblSides = {-- list all sides and offset coordinates
  506.   ["top"   ] = { 3, 1},
  507.   ["front" ] = { 1, 3},
  508.   ["left"  ] = { 3, 3},
  509.   ["right" ] = { 5, 3},
  510.   ["back"  ] = { 5, 5},
  511.   ["bottom"] = { 3, 5},
  512. }
  513.  
  514. function redstone_event()
  515.   -- Event only returns nil so we need to check sides manually
  516.   local message = ""
  517.   for side, state in pairs(tblRedstoneState) do
  518.     if rs.getInput(side) ~= state then
  519.       -- print(side .. " is now " .. tostring(rs.getInput(side)))
  520.       message = message .. side .. " "
  521.       tblRedstoneState[side] = rs.getInput(side)
  522.     end
  523.   end
  524.   if message ~= "" then
  525.     message = "Redstone changed on " .. message
  526.     showWarning(message)
  527.   end
  528. end
  529.  
  530. ----------- Configuration
  531.  
  532. function data_save()
  533.   local file = fs.open("shipdata.txt", "w")
  534.   if file ~= nil then
  535.     file.writeLine(textutils.serialize(data))
  536.     file.close()
  537.   else
  538.     ShowWarning("No file system")
  539.     os.sleep(3)
  540.   end
  541. end
  542.  
  543. function data_read()
  544.   data = { }
  545.   if fs.exists("shipdata.txt") then
  546.     local file = fs.open("shipdata.txt", "r")
  547.     data = textutils.unserialize(file.readAll())
  548.     file.close()
  549.     if data == nil then data = {}; end
  550.   end
  551.   if data.core_summon == nil then data.core_summon = false; end
  552.   if data.reactor_mode == nil then data.reactor_mode = 0; end
  553.   if data.reactor_rate == nil then data.reactor_rate = 100; end
  554.   if data.reactor_targetStability == nil then data.reactor_targetStability = 50; end
  555.   if data.reactor_laserAmount == nil then data.reactor_laserAmount = 10000; end
  556.   if data.radar_monitorIndex == nil then data.radar_monitorIndex = 0; end
  557.   if data.radar_radius == nil then data.radar_radius = 500; end
  558.   if data.radar_autoscan == nil then data.radar_autoscan = false; end
  559.   if data.radar_autoscanDelay == nil then data.radar_autoscanDelay = 3; end
  560.   if data.radar_results == nil then data.radar_results = {}; end
  561.   if data.radar_scale == nil then data.radar_scale = 500; end
  562.   if data.radar_offsetX == nil then data.radar_offsetX = 0; end
  563.   if data.radar_offsetY == nil then data.radar_offsetY = 0; end
  564.   if data.gps_hook == nil then data.gps_hook = 0; end
  565.   if data.radar_hook == nil then data.radar_hook = 0; end
  566.   if data.auto_hook == nil then data.auto_hook = 0; end
  567. end
  568.  
  569. function data_setName()
  570.   if ship ~= nil then
  571.     ShowTitle("<==== Set ship name ====>")
  572.   else
  573.     ShowTitle("<==== Set name ====>")
  574.   end
  575.  
  576.   SetCursorPos(1, 2)
  577.   Write("Enter ship name: ")
  578.   label = readInputText(label)
  579.   os.setComputerLabel(label)
  580.   if ship ~= nil then
  581.     ship.coreFrequency(label)
  582.   end
  583.   os.reboot()
  584. end
  585.  
  586. function string_split(source, sep)
  587.   local sep = sep or ":"
  588.   local fields = {}
  589.   local pattern = string.format("([^%s]+)", sep)
  590.   source:gsub(pattern, function(c) fields[#fields + 1] = c end)
  591.   return fields
  592. end
  593.  
  594. ----------- Radar support
  595.  
  596. radar_listOffset = 0
  597. radar_timerId = -1
  598. radar_timerLength = 1
  599. radar_x = 0
  600. radar_y = 0
  601. radar_z = 0
  602.  
  603. function radar_boot()
  604.   if radar ~= nil then
  605.     WriteLn("Booting Radar...")
  606.     if data.radar_monitorIndex > 0 then
  607.       local _, _, radar_x, radar_y, radar_z = radar.getGlobalPosition()
  608.       radar_drawMap()
  609.     end
  610.     if data.radar_autoscan then
  611.       radar_scan()
  612.     end
  613.   end
  614. end
  615.  
  616. function radar_key(char, keycode)
  617.   if char == 83 or char == 115 or keycode == 31 then -- S
  618.     data.radar_autoscan = false
  619.     radar_scan()
  620.     return true
  621.   elseif keycode == 30 then -- A
  622.     data.radar_autoscan = true
  623.     radar_scan()
  624.     return true
  625.   elseif char == 80 or char == 112 or keycode == 25 then -- P
  626.     data.radar_autoscan = false
  627.     return true
  628.   elseif keycode == 49 then -- N
  629.     radar_setMonitorIndex(data.radar_monitorIndex + 1)
  630.     data_save()
  631.     radar_drawMap()
  632.     return true
  633.   elseif char == 71 or char == 103 or keycode == 34 then -- G
  634.     radar_setRadius(data.radar_radius - 100)
  635.     data_save()
  636.     return true
  637.   elseif char == 84 or char == 116 or keycode == 20 then -- T
  638.     if data.radar_radius < 100 then
  639.       radar_setRadius(100)
  640.     else
  641.       radar_setRadius(data.radar_radius + 100)
  642.     end
  643.     data_save()
  644.     return true
  645.   elseif keycode == 36 then -- J
  646.     radar_listOffset = math.max(0, radar_listOffset - 3)
  647.     return true
  648.   elseif keycode == 22 then -- U
  649.     radar_listOffset = math.min(#data.radar_results - 1, radar_listOffset + 3)
  650.     return true
  651.   elseif char == 45 or keycode == 74 then -- -
  652.     data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.2 + 0.5))
  653.     data_save()
  654.     radar_drawMap()
  655.     return true
  656.   elseif char == 43 or keycode == 78 then -- +
  657.     data.radar_scale = math.max(20, math.floor(data.radar_scale * 0.8 + 0.5))
  658.     data_save()
  659.     radar_drawMap()
  660.     return true
  661.   elseif keycode == 199 then -- home
  662.     data.radar_offsetX = 0
  663.     data.radar_offsetY = 0
  664.     data.radar_scale = 20
  665.     for i = 0, #data.radar_results - 1 do
  666.       data.radar_scale = math.max(data.radar_scale, math.max(math.abs(radar_x - data.radar_results[i].x), math.abs(radar_z - data.radar_results[i].z)))
  667.     end
  668.     data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.1 + 0.5))
  669.     data_save()
  670.     radar_drawMap()
  671.     return true
  672.   elseif keycode == 203 then -- left
  673.     data.radar_offsetX = data.radar_offsetX - 0.20 * data.radar_scale
  674.     data_save()
  675.     radar_drawMap()
  676.     return true
  677.   elseif keycode == 205 then -- right
  678.     data.radar_offsetX = data.radar_offsetX + 0.20 * data.radar_scale
  679.     data_save()
  680.     radar_drawMap()
  681.     return true
  682.   elseif keycode == 200 then -- up
  683.     data.radar_offsetY = data.radar_offsetY - 0.20 * data.radar_scale
  684.     data_save()
  685.     radar_drawMap()
  686.     return true
  687.   elseif keycode == 208 then -- down
  688.     data.radar_offsetY = data.radar_offsetY + 0.20 * data.radar_scale
  689.     data_save()
  690.     radar_drawMap()
  691.     return true
  692.   elseif keycode == 46 then -- C
  693.     radar_page_config()
  694.     data_save()
  695.     return true
  696.   end
  697.   return false
  698. end
  699.  
  700. function radar_page()
  701.   local radar_resultsPerPage = 9
  702.  
  703.   ShowTitle(label .. " - Radar map")
  704.  
  705.   SetCursorPos(1, 2)
  706.   if radar == nil or radar.isInterfaced() == nil then
  707.     SetColorDisabled()
  708.     Write("Radar not detected")
  709.   else
  710.     SetColorDefault()
  711.     if #data.radar_results == 0 then
  712.       Write("No contacts in range...")
  713.     else
  714.       local lastResultShown = radar_listOffset + radar_resultsPerPage - 1
  715.       if lastResultShown >= #data.radar_results then
  716.         lastResultShown = #data.radar_results - 1
  717.       end
  718.       Write("Displaying results " .. (radar_listOffset + 1) .. " to " .. (lastResultShown + 1) .. " of " .. #data.radar_results)
  719.       for i = radar_listOffset, lastResultShown do
  720.         SetCursorPos(4, 3 + i - radar_listOffset)
  721.         if data.radar_results ~= nil then
  722.           Write(FormatInteger(data.radar_results[i].x, 7) .. ", " .. FormatInteger(data.radar_results[i].y, 4) .. ", " .. FormatInteger(data.radar_results[i].z, 7))
  723.         else
  724.           Write("~nil~")
  725.         end
  726.       end
  727.     end
  728.  
  729.     SetColorDefault()
  730.     local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  731.     local success, result = radar.getEnergyRequired(data.radar_radius)
  732.     local energyRequired = 0
  733.     if success then
  734.       energyRequired = result
  735.     end
  736.     SetCursorPos(1, 12)
  737.     Write("Energy: ")
  738.     if energyStored > energyRequired then
  739.       SetColorSuccess()
  740.     else
  741.       SetColorWarning()
  742.     end
  743.     Write(FormatInteger(energyStored, 10) .. " " .. energyUnits)
  744.     SetColorDefault()
  745.     SetCursorPos(1, 13)
  746.     Write("Radius: " .. data.radar_radius)
  747.  
  748.     SetCursorPos(30, 13)
  749.     Write("Monitor# " .. data.radar_monitorIndex .. "/" .. #monitors)
  750.  
  751.     SetCursorPos(1, 14)
  752.     Write("Autoscan: ")
  753.     if data.radar_autoscan then SetColorSuccess() else SetColorDefault() end
  754.     Write(boolToYesNo(data.radar_autoscan))
  755.  
  756.     SetColorDefault()
  757.     SetCursorPos(30, 14)
  758.     Write("Delay " .. data.radar_autoscanDelay .. "s")
  759.   end
  760.  
  761.   SetColorFooter()
  762.   SetCursorPos(1, 15)
  763.   ShowMenu(" S - Scan once, A - Autoscan, P - Stop autoscan")
  764.   SetCursorPos(1, 16)
  765.   ShowMenu(" T/G - Scan radius, U/J - Scroll list")
  766.   SetCursorPos(1, 17)
  767.   ShowMenu(" +/- - Scale, N - Next monitor, C - Configuration")
  768. end
  769.  
  770. function radar_page_config()
  771.   ShowTitle(label .. " - Radar configuration")
  772.  
  773.   SetCursorPos(1, 2)
  774.   if radar == nil or radar.isInterfaced() == nil then
  775.     SetColorDisabled()
  776.     Write("No radar detected")
  777.   else
  778.     SetColorDefault()
  779.     SetCursorPos(1, 3)
  780.     Write("Radar scan radius (" .. data.radar_radius .. " blocks): ")
  781.     radar_setRadius(readInputNumber(data.radar_radius))
  782.    
  783.     SetCursorPos(1, 5)
  784.     Write("Autoscan delay in seconds (" .. data.radar_autoscanDelay .. "): ")
  785.     radar_setAutoscanDelay(readInputNumber(data.radar_autoscanDelay))
  786.    
  787.     SetCursorPos(1, 7)
  788.     Write("Output monitor (" .. data.radar_monitorIndex .. "/" .. #monitors .. "): ")
  789.     radar_setMonitorIndex(readInputNumber(data.radar_monitorIndex))
  790.    
  791.     data_save()
  792.   end
  793. end
  794. function hooks_page_config()
  795.   ShowTitle(label .. " - Webhook configuration")
  796.  
  797.   SetCursorPos(1, 2)
  798.     SetColorDefault()
  799.     SetCursorPos(1, 3)
  800.     Write("Ship GPS Webhook: ")
  801.     hook_gpsset(readInput(data.gps_hook))
  802.    
  803.     SetCursorPos(1, 5)
  804.     Write("Radar Results Webhook: ")
  805.     hook_radset(readInput(data.radar_hook))
  806.    
  807.     SetCursorPos(1, 7)
  808.     Write("AutoPilot Webhook: ")
  809.     hook_autoset(readInput(data.auto_hook))
  810.    
  811.     data_save()
  812. end
  813. function hook_gpsset(newGps)
  814.     data.gps_hook = newGps
  815. end
  816. function hook_radset(newRad)
  817.     data.radar_hook = newRad
  818. end
  819. function hook_autoset(newAuto)
  820.     data.auto_hook = newAuto
  821. end
  822. function radar_setRadius(newRadius)
  823.   if newRadius < 1 then
  824.     data.radar_radius = 1
  825.   elseif newRadius >= 10000 then
  826.     data.radar_radius = 10000
  827.   else
  828.     data.radar_radius = newRadius
  829.   end
  830. end
  831.  
  832. function radar_setAutoscanDelay(newAutoscanDelay)
  833.   if newAutoscanDelay < 1 then
  834.     data.radar_autoscanDelay = 1
  835.   elseif newAutoscanDelay >= 3600 then  -- 1 hour
  836.     data.radar_autoscanDelay = 3600
  837.   else
  838.     data.radar_autoscanDelay = newAutoscanDelay
  839.   end
  840. end
  841.  
  842. function radar_setMonitorIndex(newIndex)
  843.   if #monitors == 0 or newIndex < 0 or newIndex > #monitors then
  844.     data.radar_monitorIndex = 0
  845.   else
  846.     data.radar_monitorIndex = newIndex
  847.   end
  848. end
  849.  
  850. function radar_getMonitor()
  851.   if data.radar_monitorIndex > 0 and data.radar_monitorIndex <= #monitors then
  852.     return monitors[data.radar_monitorIndex]
  853.   else
  854.     return nil
  855.   end
  856. end
  857.  
  858. function radar_scan()
  859.   local monitor = radar_getMonitor()
  860.   if radar == nil or radar.isInterfaced() == nil then
  861.     draw_warning(monitor, "No radar")
  862.     return false
  863.   end
  864.   local energyStored, energyMax, _ = radar.getEnergyStatus()
  865.   if energyStored == nil then energyStored = 0 end
  866.   if energyMax == nil or energyMax == 0 then energyMax = 1 end
  867.  
  868.   radar.radius(radius)
  869.   local success, result = radar.getEnergyRequired()
  870.   if not success then
  871.     showErrorAndExit(result)
  872.   end
  873.   local energyRequired = result
  874.  
  875.   if energyRequired <= 0 or energyStored < energyRequired then
  876.     textOut((w / 2) - 7, 1, " /!\\  LOW POWER ", colors.white, colors.red)
  877.     os.sleep(1)
  878.    
  879.     return 0
  880.   end
  881.   if radar_timerId ~= -1 and radar.getResultsCount() == -1 then
  882.     draw_warning(monitor, "Already scanning...")
  883.     return false
  884.   end
  885.   radar_timerId = os.startTimer(radar_timerLength)
  886.  
  887.   radar.radius(data.radar_radius)
  888.   if radar.start() then
  889.     draw_warning(monitor, "Scanning...")
  890.   end
  891.   return false
  892. end
  893.  
  894. function radar_scanDone()
  895.   local numResults = radar.getResultsCount()
  896.   data.radar_results = {}
  897.   if (numResults ~= 0) then
  898.     for i = 0, numResults do
  899.       local success, type, name, x, y, z = radar.getResult(i)
  900.       if success then
  901.         if name == "" then
  902.           name = "?"
  903.         end
  904.         data.radar_results[i] = { x = x, y = y, z = z, name = name, type = type }
  905.       end
  906.     end
  907.     data.radar_scale = data.radar_radius
  908.   end
  909.   data_save()
  910.   radar_drawMap()
  911. end
  912.  
  913. function draw_text(monitor, x, y, text, textColor, backgroundColor)
  914.   if monitor == nil then
  915.     term.setCursorPos(x, y)
  916.     if textColor ~= nil then term.setTextColor(textColor) end
  917.     if backgroundColor ~= nil then term.setBackgroundColor(backgroundColor) end
  918.     term.write(text)
  919.     local xt, yt = term.getCursorPos()
  920.     term.setCursorPos(1, yt + 1)
  921.   else
  922.     monitor.setCursorPos(x, y)
  923.     if textColor ~= nil then monitor.setTextColor(textColor) end
  924.     if backgroundColor ~= nil then monitor.setBackgroundColor(backgroundColor) end
  925.     monitor.write(text)
  926.     local xt, yt = monitor.getCursorPos()
  927.     monitor.setCursorPos(1, yt + 1)
  928.   end
  929. end  
  930.  
  931. function draw_warning(monitor, text)
  932.   local screenWidth, screenHeight
  933.   if monitor == nil then
  934.     screenWidth, screenHeight = term.getSize()
  935.   else
  936.     screenWidth, screenHeight = monitor.getSize()
  937.   end
  938.   local centerX = math.floor(screenWidth / 2)
  939.   local centerY = math.floor(screenHeight / 2)
  940.   local halfWidth = math.ceil(string.len(text) / 2)
  941.   local blank = string.sub("                              ", - (string.len(text) + 2))
  942.  
  943.   draw_text(monitor, centerX - halfWidth - 1, centerY - 1, blank, colors.white, colors.red)
  944.   draw_text(monitor, centerX - halfWidth - 1, centerY    , " " .. text .. " ", colors.white, colors.red)
  945.   draw_text(monitor, centerX - halfWidth - 1, centerY + 1, blank, colors.white, colors.red)
  946. end
  947.  
  948. function draw_centeredText(monitor, y, text)
  949.   local screenWidth, screenHeight
  950.   if monitor == nil then
  951.     screenWidth, screenHeight = term.getSize()
  952.   else
  953.     screenWidth, screenHeight = monitor.getSize()
  954.   end
  955.   local x = math.floor(screenWidth / 2 - string.len(text) / 2 + 0.5)
  956.  
  957.   draw_text(monitor, x, y, text, nil, nil)
  958. end
  959.  
  960. function radar_drawContact(monitor, contact)
  961.   local screenWidth, screenHeight
  962.   if monitor == nil then
  963.     screenWidth, screenHeight = term.getSize()
  964.   else
  965.     screenWidth, screenHeight = monitor.getSize()
  966.   end
  967.  
  968.   local screenX = (radar_x + data.radar_offsetX - contact.x) / data.radar_scale
  969.   local screenY = (radar_z + data.radar_offsetY - contact.z) / data.radar_scale
  970.   local visible = true
  971.  
  972.   if screenX <= -1 or screenX >= 1 or screenY <= -1 or screenY >= 1 then
  973.     screenX = math.min(1, math.max(-1, screenX))
  974.     screenY = math.min(1, math.max(-1, screenY))
  975.     visible = false
  976.   end
  977.  
  978.   screenX = math.floor(screenX * (screenWidth  - 3) / 2 + ((screenWidth  - 1)  / 2) + 1.5)
  979.   screenY = math.floor(screenY * (screenHeight - 3) / 2 + ((screenHeight - 1) / 2) + 1.5)
  980.  
  981.   if contact.type == "self" then
  982.     draw_text(monitor, screenX, screenY, Style.TextRadarself, Style.CRadarself, Style.BGRadarself)
  983.   else
  984.     draw_text(monitor, screenX, screenY, Style.TextRadarother, Style.CRadarother, Style.BGRadarother)
  985.   end
  986.   if visible then
  987.     local text = contact.name
  988.     screenX = math.min(screenWidth - 1 - string.len(text), math.max(2, math.floor(screenX - string.len(text) / 2 + 0.5)))
  989.     if screenY == (screenHeight - 1) then
  990.       screenY = screenY - 1
  991.     else
  992.       screenY = screenY + 1
  993.     end
  994.     draw_text(monitor, screenX, screenY, text, Style.CRadarother, Style.BGRadarother)
  995.   end
  996. end
  997.  
  998. function radar_drawMap()
  999.   local screenWidth, screenHeight, x, y
  1000.   local monitor = radar_getMonitor()
  1001.   -- center area
  1002.   SetColorRadarmap()
  1003.   if monitor == nil then
  1004.     term.clear()
  1005.     screenWidth, screenHeight = term.getSize()
  1006.   else
  1007.     monitor.clear()
  1008.     screenWidth, screenHeight = monitor.getSize()
  1009.   end
  1010.   -- borders
  1011.   SetColorRadarborder()
  1012.   for x = 1, screenWidth do
  1013.     if monitor == nil then
  1014.       term.setCursorPos(x, 1)
  1015.       term.write(" ")
  1016.       term.setCursorPos(x, screenHeight)
  1017.       term.write(" ")
  1018.     else
  1019.       monitor.setCursorPos(x, 1)
  1020.       monitor.write(" ")
  1021.       monitor.setCursorPos(x, screenHeight)
  1022.       monitor.write(" ")
  1023.     end
  1024.   end
  1025.   for y = 2, screenHeight - 1 do
  1026.     if monitor == nil then
  1027.       term.setCursorPos(1, y)
  1028.       term.write(" ")
  1029.       term.setCursorPos(screenWidth, y)
  1030.       term.write(" ")
  1031.     else
  1032.       monitor.setCursorPos(1, y)
  1033.       monitor.write(" ")
  1034.       monitor.setCursorPos(screenWidth, y)
  1035.       monitor.write(" ")
  1036.     end
  1037.   end
  1038.   -- title
  1039.   local text = label .. " - Radar map"
  1040.   if #data.radar_results == 0 then
  1041.     text = text .. " (no contacts)"
  1042.   else
  1043.     text = text .. " (" .. #data.radar_results .. " contacts)"
  1044.   end
  1045.      draw_centeredText(monitor, 1, text)
  1046.   -- status
  1047.   local text = "Scan radius: " .. data.radar_radius
  1048.   if radar ~= nil then
  1049.     local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  1050.     text = text .. " | Energy: " .. energyStored .. " " .. energyUnits
  1051.   end
  1052.   text = text .. " | Scale: " .. data.radar_scale
  1053.   draw_centeredText(monitor, screenHeight, text)
  1054.   -- results
  1055.   SetCursorPos(1, 12)
  1056.   radar_drawContact(monitor, {x = radar_x, y = radar_y, z = radar_z, name = "", type = "self"})
  1057.   for i = 0, #data.radar_results - 1 do
  1058.     radar_drawContact(monitor, data.radar_results[i])
  1059.   end
  1060.  
  1061.   -- restore defaults
  1062.   SetColorDefault()
  1063. end
  1064.  
  1065. radar_waitingNextScan = false
  1066. function radar_timerEvent()
  1067.   radar_timerId = -1
  1068.   if radar_waitingNextScan then
  1069.     radar_waitingNextScan = false
  1070.     radar_scan() -- will restart timer
  1071.   else
  1072.     local numResults = radar.getResultsCount()
  1073.     if numResults ~= -1 then
  1074.       radar_scanDone()
  1075.       if data.radar_autoscan then
  1076.         radar_waitingNextScan = true
  1077.         radar_timerId = os.startTimer(data.radar_autoscanDelay)
  1078.       end
  1079.     else -- still scanning
  1080.       radar_timerId = os.startTimer(radar_timerLength)
  1081.     end
  1082.   end
  1083. end
  1084.  
  1085. ----------- Boot sequence
  1086. math.randomseed(os.time())
  1087. label = os.getComputerLabel()
  1088. if not label then
  1089.   label = "" .. os.getComputerID()
  1090. end
  1091.  
  1092. -- read configuration
  1093. data_read()
  1094. clear()
  1095. print("data_read...")
  1096.  
  1097. -- initial scanning
  1098. monitors = {}
  1099. ShowTitle(label .. " - Connecting...")
  1100. WriteLn("")
  1101.  
  1102. sides = peripheral.getNames()
  1103. reactor = nil
  1104. mininglasers = {}
  1105. reactorlasers = {}
  1106. cloakingcores = {}
  1107. forcefieldprojectors = {}
  1108. warpdrivetransportercores = {}
  1109. shipcontrollers = {}
  1110. ship = nil
  1111. radar = nil
  1112. for key,side in pairs(sides) do
  1113.   os.sleep(0)
  1114.   Write("Checking " .. side .. " ")
  1115.   local componentType = peripheral.getType(side)
  1116.   Write(componentType .. " ")
  1117.   if componentType == "warpdriveShipController" then
  1118.     Write("wrapping!")
  1119.     table.insert(shipcontrollers, peripheral.wrap(side))
  1120.   elseif componentType == "warpdriveShipCore" then
  1121.     Write("wrapping!")
  1122.     ship = peripheral.wrap(side)
  1123.   elseif componentType == "warpdriveEnanReactorCore" then
  1124.     Write("wrapping!")
  1125.     reactor = peripheral.wrap(side)
  1126.   elseif componentType == "warpdriveEnanReactorLaser" then
  1127.     Write("wrapping!")
  1128.     local wrap = peripheral.wrap(side)
  1129.     table.insert(reactorlasers, { side = wrap.side(), wrap = wrap })
  1130.   elseif componentType == "warpdriveMiningLaser" then
  1131.     WriteLn("Wrapping " .. side)
  1132.     table.insert(mininglasers, peripheral.wrap(side))
  1133.   elseif componentType == "warpdriveCloakingCore" then
  1134.     Write("wrapping!")
  1135.     table.insert(cloakingcores, peripheral.wrap(side))
  1136.   elseif componentType == "warpdriveForceFieldProjector" then
  1137.     Write("wrapping!")
  1138.     table.insert(forcefieldprojectors, peripheral.wrap(side))
  1139.   elseif componentType == "warpdriveTransporterCore" then
  1140.     Write("wrapping!")
  1141.     table.insert(warpdrivetransportercores, peripheral.wrap(side))
  1142.   elseif componentType == "warpdriveRadar" then
  1143.     Write("wrapping!")
  1144.     radar = peripheral.wrap(side)
  1145.   elseif componentType == "monitor" then
  1146.     Write("wrapping!")
  1147.     lmonitor = peripheral.wrap(side)
  1148.     table.insert(monitors, lmonitor)
  1149.     lmonitor.setTextScale(monitor_textScale)
  1150.   end
  1151.   WriteLn("")
  1152. end
  1153.  
  1154. if not os.getComputerLabel() and (ship ~= nil or reactor ~= nil) then
  1155.   data_setName()
  1156. end
  1157.  
  1158. -- peripherals status
  1159. function connections_page()
  1160.   SetCursorPos(0, 1)
  1161.   SetColorTitle()
  1162.   ShowTitle(label .. " - Connections")
  1163.  
  1164.   WriteLn("")
  1165.   if #monitors == 0 then
  1166.     SetColorWarning()
  1167.     WriteLn("No Monitor detected")
  1168.   elseif #monitors == 1 then
  1169.     SetColorSuccess()
  1170.     WriteLn("1 monitor detected")
  1171.   else
  1172.     SetColorSuccess()
  1173.     WriteLn(#monitors .. " Monitors detected")
  1174.   end
  1175.    if ship == nil or ship.isInterfaced() == nil then
  1176.     SetColorDisabled()
  1177.     WriteLn("No ship core detected")
  1178.   else
  1179.     SetColorSuccess()
  1180.     WriteLn("Ship core detected")
  1181.   end
  1182.   if #shipcontrollers == 0 then
  1183.     SetColorWarning()
  1184.     WriteLn("No Ship controllers detected - Multi Core Disabled")
  1185.   elseif #shipcontrollers == 1 then
  1186.     SetColorSuccess()
  1187.     WriteLn("1 Ship controller detected")
  1188.   else
  1189.     SetColorSuccess()
  1190.     WriteLn(#shipcontrollers .. " Ship controllers detected - Multi Core Enabled!")
  1191.   end
  1192.  
  1193.   if reactor == nil or reactor.isInterfaced() == nil then
  1194.     SetColorWarning()
  1195.     WriteLn("No Enantiomorphic reactor detected")
  1196.   else
  1197.     SetColorSuccess()
  1198.     WriteLn("Enantiomorphic reactor detected")
  1199.   end
  1200.  
  1201.   if #reactorlasers == 0 then
  1202.     SetColorWarning()
  1203.     WriteLn("No reactor stabilisation laser detected")
  1204.   elseif #reactorlasers == 1 then
  1205.     SetColorSuccess()
  1206.     WriteLn("1 reactor stabilisation laser detected")
  1207.   else
  1208.     SetColorSuccess()
  1209.     WriteLn(#reactorlasers .. " reactor stabilisation lasers detected")
  1210.   end
  1211.  
  1212.   if #mininglasers == 0 then
  1213.     SetColorWarning()
  1214.     WriteLn("No mining laser detected")
  1215.   elseif #mininglasers == 1 then
  1216.     SetColorSuccess()
  1217.     WriteLn("1 mining laser detected")
  1218.   else
  1219.     SetColorSuccess()
  1220.     WriteLn(#mininglasers .. " mining lasers detected")
  1221.   end
  1222.  
  1223.   if #cloakingcores == 0 then
  1224.     SetColorWarning()
  1225.     WriteLn("No cloaking core detected")
  1226.   elseif #cloakingcores == 1 then
  1227.     SetColorSuccess()
  1228.     WriteLn("1 cloaking core detected")
  1229.   else
  1230.     SetColorSuccess()
  1231.     WriteLn(#cloakingcores .. " cloaking cores detected")
  1232.   end
  1233.     if #forcefieldprojectors == 0 then
  1234.     SetColorWarning()
  1235.     WriteLn("No Force Fieldp Pojectors detected")
  1236.   elseif #forcefieldprojectors == 1 then
  1237.     SetColorSuccess()
  1238.     WriteLn(#"1 Force Field Projector detected")
  1239.   else
  1240.     SetColorSuccess()
  1241.     WriteLn(#forcefieldprojectors .. " Force Field Projectors detected")
  1242.   end
  1243.     if #warpdrivetransportercores == 0 then
  1244.     SetColorWarning()
  1245.     WriteLn("No Transporter Rooms detected")
  1246.   elseif #warpdrivetransportercores == 1 then
  1247.     SetColorSuccess()
  1248.     WriteLn(#"1 Transporter Room detected")
  1249.   else
  1250.     SetColorSuccess()
  1251.     WriteLn(#warpdrivetransportercores .. " Transporter Rooms detected")
  1252.   end
  1253.  
  1254.   if radar == nil or radar.isInterfaced() == nil then
  1255.     SetColorWarning()
  1256.     WriteLn("No radar detected")
  1257.   else
  1258.     SetColorSuccess()
  1259.     WriteLn("Radar detected")
  1260.   end
  1261.  
  1262.   WriteLn("")
  1263.   SetColorDefault()
  1264.   WriteLn("Please refer to below menu for keyboard controls")
  1265.   WriteLn("For example, press 1 to access Ship page")
  1266. end
  1267.  
  1268. -- peripheral boot up
  1269. clear()
  1270. connections_page()
  1271. SetColorDefault()
  1272. WriteLn("")
  1273. os.sleep(0)
  1274. radar_boot()
  1275. os.sleep(0)
  1276.  
  1277. -- main loop
  1278. abort = false
  1279. refresh = true
  1280. page = connections_page
  1281. keyHandler = nil
  1282. repeat
  1283.   ClearWarning()
  1284.   if refresh then
  1285.     clear()
  1286.     page()
  1287.     menu_common()
  1288.     refresh = false
  1289.   end
  1290.   params = { os.pullEventRaw() }
  1291.   eventName = params[1]
  1292.   address = params[2]
  1293.   if address == nil then address = "none" end
  1294.   -- WriteLn("...")
  1295.   -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. ", " .. params[4] .. " received")
  1296.   -- os.sleep(0.2)
  1297.   if eventName == "key" then
  1298.     keycode = params[2]
  1299.     if char == 88 or char == 120 or keycode == 45 then -- x for eXit
  1300.       os.pullEventRaw()
  1301.       abort = true
  1302.     elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
  1303.       page = connections_page
  1304.       keyHandler = nil
  1305.       refresh = true
  1306.     elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
  1307.       page = core_page
  1308.       keyHandler = core_key
  1309.       refresh = true
  1310.     elseif char == 50 or keycode == 3 or keycode == 80 then -- 2
  1311.       page = cloaking_page
  1312.       keyHandler = cloaking_key
  1313.       refresh = true
  1314.     elseif char == 51 or keycode == 4 or keycode == 81 then -- 3
  1315.       page = shield_page
  1316.       keyHandler = shield_key
  1317.       refresh = true
  1318.     elseif char == 52 or keycode == 5 or keycode == 75 then -- 4
  1319.       page = radar_page
  1320.       keyHandler = radar_key
  1321.       refresh = true
  1322.     elseif char == 53 or keycode == 6  or keycode == 76 then -- 5
  1323.       page = mining_page
  1324.       keyHandler = mining_key
  1325.       refresh = true
  1326.     elseif char == 54 or keycode == 7  or keycode == 77 then -- 6
  1327.       page = mining_page
  1328.       keyHandler = mining_key
  1329.       refresh = true
  1330.     elseif char == 55 or keycode == 8  then -- 7
  1331.       page = reactor_page
  1332.       keyHandler = reactor_key
  1333.       refresh = true
  1334.     elseif char == 56 or keycode == 9  or keycode == 79 then -- 8
  1335.       page = hooks_page_config
  1336.       keyHandler = hooks_page_key
  1337.       refresh = true
  1338.     elseif keyHandler ~= nil and keyHandler(char, keycode) then
  1339.       refresh = true
  1340.       os.sleep(0)
  1341.     elseif char == 0 then -- control chars
  1342.       refresh = false
  1343.       os.sleep(0)
  1344.     else
  1345.       ShowWarning("Key " .. keycode .. " is invalid")
  1346.       os.sleep(0.2)
  1347.     end
  1348.   elseif eventName == "reactorPulse" then
  1349.     reactor_pulse(params[2])
  1350.     refresh = (page == reactor_page)
  1351.   elseif eventName == "terminate" then
  1352.     abort = true
  1353.   elseif not common_event(eventName, params[2]) then
  1354.     ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  1355.     refresh = true
  1356.     os.sleep(0.2)
  1357.   end
  1358. until abort
  1359.  
  1360. -- exiting
  1361. if data.core_summon then
  1362.   data.core_summon = false
  1363.   data_save()
  1364. end
  1365.  
  1366. if ship ~= nil then
  1367.   ship.enable()
  1368. end
  1369.  
  1370. -- clear screens on exit
  1371. SetMonitorColorFrontBack(colors.white, colors.black)
  1372. term.clear()
  1373. if monitors ~= nil then
  1374.   for key,monitor in pairs(monitors) do
  1375.     monitor.clear()
  1376.   end
  1377. end
  1378. SetCursorPos(1, 1)
  1379. WriteLn("Program terminated")
  1380. WriteLn("Type startup to restart it")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top