SHARE
TWEET

Untitled

infiniteblock Nov 14th, 2019 115 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.radar_monitorIndex == nil then data.radar_monitorIndex = 0; end
  552.   if data.radar_radius == nil then data.radar_radius = 500; end
  553.   if data.radar_autoscan == nil then data.radar_autoscan = false; end
  554.   if data.radar_autoscanDelay == nil then data.radar_autoscanDelay = 3; end
  555.   if data.radar_results == nil then data.radar_results = {}; end
  556.   if data.radar_scale == nil then data.radar_scale = 500; end
  557.   if data.radar_offsetX == nil then data.radar_offsetX = 0; end
  558.   if data.radar_offsetY == nil then data.radar_offsetY = 0; end
  559.   if data.radar_hook == nil then data.radar_hook = 0; end
  560. end
  561.  
  562.  
  563. function string_split(source, sep)
  564.   local sep = sep or ":"
  565.   local fields = {}
  566.   local pattern = string.format("([^%s]+)", sep)
  567.   source:gsub(pattern, function(c) fields[#fields + 1] = c end)
  568.   return fields
  569. end
  570.  
  571. ----------- Radar support
  572.  
  573. radar_listOffset = 0
  574. radar_timerId = -1
  575. radar_timerLength = 1
  576. radar_x = 0
  577. radar_y = 0
  578. radar_z = 0
  579.  
  580. function radar_boot()
  581.   if radar ~= nil then
  582.     WriteLn("Booting Radar...")
  583.     if data.radar_monitorIndex > 0 then
  584.       local _, _, radar_x, radar_y, radar_z = radar.getGlobalPosition()
  585.       radar_drawMap()
  586.     end
  587.     if data.radar_autoscan then
  588.       radar_scan()
  589.     end
  590.   end
  591. end
  592.  
  593. function radar_key(char, keycode)
  594.   if char == 83 or char == 115 or keycode == 31 then -- S
  595.     data.radar_autoscan = false
  596.     radar_scan()
  597.     return true
  598.   elseif keycode == 30 then -- A
  599.     data.radar_autoscan = true
  600.     radar_scan()
  601.     return true
  602.   elseif char == 80 or char == 112 or keycode == 25 then -- P
  603.     data.radar_autoscan = false
  604.     return true
  605.   elseif keycode == 49 then -- N
  606.     radar_setMonitorIndex(data.radar_monitorIndex + 1)
  607.     data_save()
  608.     radar_drawMap()
  609.     return true
  610.   elseif char == 71 or char == 103 or keycode == 34 then -- G
  611.     radar_setRadius(data.radar_radius - 100)
  612.     data_save()
  613.     return true
  614.   elseif char == 84 or char == 116 or keycode == 20 then -- T
  615.     if data.radar_radius < 100 then
  616.       radar_setRadius(100)
  617.     else
  618.       radar_setRadius(data.radar_radius + 100)
  619.     end
  620.     data_save()
  621.     return true
  622.   elseif keycode == 36 then -- J
  623.     radar_listOffset = math.max(0, radar_listOffset - 3)
  624.     return true
  625.   elseif keycode == 22 then -- U
  626.     radar_listOffset = math.min(data.radar_results - 1, radar_listOffset + 3)
  627.     return true
  628.   elseif char == 45 or keycode == 74 then -- -
  629.     data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.2 + 0.5))
  630.     data_save()
  631.     radar_drawMap()
  632.     return true
  633.   elseif char == 43 or keycode == 78 then -- +
  634.     data.radar_scale = math.max(20, math.floor(data.radar_scale * 0.8 + 0.5))
  635.     data_save()
  636.     radar_drawMap()
  637.     return true
  638.   elseif keycode == 199 then -- home
  639.     data.radar_offsetX = 0
  640.     data.radar_offsetY = 0
  641.     data.radar_scale = 20
  642.     for i = 0, data.radar_results - 1 do
  643.       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)))
  644.     end
  645.     data.radar_scale = math.min(10000, math.floor(data.radar_scale * 1.1 + 0.5))
  646.     data_save()
  647.     radar_drawMap()
  648.     return true
  649.   elseif keycode == 203 then -- left
  650.     data.radar_offsetX = data.radar_offsetX - 0.20 * data.radar_scale
  651.     data_save()
  652.     radar_drawMap()
  653.     return true
  654.   elseif keycode == 205 then -- right
  655.     data.radar_offsetX = data.radar_offsetX + 0.20 * data.radar_scale
  656.     data_save()
  657.     radar_drawMap()
  658.     return true
  659.   elseif keycode == 200 then -- up
  660.     data.radar_offsetY = data.radar_offsetY - 0.20 * data.radar_scale
  661.     data_save()
  662.     radar_drawMap()
  663.     return true
  664.   elseif keycode == 208 then -- down
  665.     data.radar_offsetY = data.radar_offsetY + 0.20 * data.radar_scale
  666.     data_save()
  667.     radar_drawMap()
  668.     return true
  669.   elseif keycode == 46 then -- C
  670.     radar_page_config()
  671.     data_save()
  672.     return true
  673.   end
  674.   return false
  675. end
  676.  
  677. function radar_page()
  678.   local radar_resultsPerPage = 9
  679.  
  680.   ShowTitle(label .. " - Radar map")
  681.  
  682.   SetCursorPos(1, 2)
  683.   if radar == nil or radar.isInterfaced() == nil then
  684.     SetColorDisabled()
  685.     Write("Radar not detected")
  686.   else
  687.     SetColorDefault()
  688.     if data.radar_results == 0 then
  689.       Write("No contacts in range...")
  690.     else
  691.       local lastResultShown = radar_listOffset + radar_resultsPerPage - 1
  692.       if lastResultShown >= data.radar_results then
  693.         lastResultShown = data.radar_results - 1
  694.       end
  695.       Write("Displaying results " .. (radar_listOffset + 1) .. " to " .. (lastResultShown + 1) .. " of " .. data.radar_results)
  696.       for i = radar_listOffset, lastResultShown do
  697.         SetCursorPos(4, 3 + i - radar_listOffset)
  698.         if data.radar_results ~= nil then
  699.           Write(FormatInteger(data.radar_results[i].x, 7) .. ", " .. FormatInteger(data.radar_results[i].y, 4) .. ", " .. FormatInteger(data.radar_results[i].z, 7))
  700.         else
  701.           Write("~nil~")
  702.         end
  703.       end
  704.     end
  705.  
  706.     SetColorDefault()
  707.     local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  708.     local success, result = radar.getEnergyRequired(data.radar_radius)
  709.     local energyRequired = 0
  710.     if success then
  711.       energyRequired = result
  712.     end
  713.     SetCursorPos(1, 12)
  714.     Write("Energy: ")
  715.     if energyStored > energyRequired then
  716.       SetColorSuccess()
  717.     else
  718.       SetColorWarning()
  719.     end
  720.     Write(FormatInteger(energyStored, 10) .. " " .. energyUnits)
  721.     SetColorDefault()
  722.     SetCursorPos(1, 13)
  723.     Write("Radius: " .. data.radar_radius)
  724.  
  725.     SetCursorPos(30, 13)
  726.     Write("Monitor# " .. data.radar_monitorIndex .. "/" .. #monitors)
  727.  
  728.     SetCursorPos(1, 14)
  729.     Write("Autoscan: ")
  730.     if data.radar_autoscan then SetColorSuccess() else SetColorDefault() end
  731.     Write(boolToYesNo(data.radar_autoscan))
  732.  
  733.     SetColorDefault()
  734.     SetCursorPos(30, 14)
  735.     Write("Delay " .. data.radar_autoscanDelay .. "s")
  736.   end
  737.  
  738.   SetColorFooter()
  739.   SetCursorPos(1, 15)
  740.   ShowMenu(" S - Scan once, A - Autoscan, P - Stop autoscan")
  741.   SetCursorPos(1, 16)
  742.   ShowMenu(" T/G - Scan radius, U/J - Scroll list")
  743.   SetCursorPos(1, 17)
  744.   ShowMenu(" +/- - Scale, N - Next monitor, C - Configuration")
  745. end
  746.  
  747. function radar_page_config()
  748.   ShowTitle(label .. " - Radar configuration")
  749.  
  750.   SetCursorPos(1, 2)
  751.   if radar == nil or radar.isInterfaced() == nil then
  752.     SetColorDisabled()
  753.     Write("No radar detected")
  754.   else
  755.     SetColorDefault()
  756.     SetCursorPos(1, 3)
  757.     Write("Radar scan radius (" .. data.radar_radius .. " blocks): ")
  758.     radar_setRadius(readInputNumber(data.radar_radius))
  759.    
  760.     SetCursorPos(1, 5)
  761.     Write("Autoscan delay in seconds (" .. data.radar_autoscanDelay .. "): ")
  762.     radar_setAutoscanDelay(readInputNumber(data.radar_autoscanDelay))
  763.    
  764.     SetCursorPos(1, 7)
  765.     Write("Output monitor (" .. data.radar_monitorIndex .. "/" .. #monitors .. "): ")
  766.     radar_setMonitorIndex(readInputNumber(data.radar_monitorIndex))
  767.    
  768.     data_save()
  769.   end
  770. end
  771. function hooks_page_config()
  772.   ShowTitle(label .. " - Webhook configuration")
  773.  
  774.   SetCursorPos(1, 2)
  775.     SetColorDefault()
  776.     SetCursorPos(1, 3)
  777.     Write("Ship GPS Webhook: ")
  778.     hook_gpsset(readInput(data.gps_hook))
  779.    
  780.     SetCursorPos(1, 5)
  781.     Write("Radar Results Webhook: ")
  782.     hook_radset(readInput(data.radar_hook))
  783.    
  784.     SetCursorPos(1, 7)
  785.     Write("AutoPilot Webhook: ")
  786.     hook_autoset(readInput(data.auto_hook))
  787.    
  788.     data_save()
  789. end
  790. function hook_gpsset(newGps)
  791.     data.gps_hook = newGps
  792. end
  793. function hook_radset(newRad)
  794.     data.radar_hook = newRad
  795. end
  796. function hook_autoset(newAuto)
  797.     data.auto_hook = newAuto
  798. end
  799. function radar_setRadius(newRadius)
  800.   if newRadius < 1 then
  801.     data.radar_radius = 1
  802.   elseif newRadius >= 10000 then
  803.     data.radar_radius = 10000
  804.   else
  805.     data.radar_radius = newRadius
  806.   end
  807. end
  808.  
  809. function radar_setAutoscanDelay(newAutoscanDelay)
  810.   if newAutoscanDelay < 1 then
  811.     data.radar_autoscanDelay = 1
  812.   elseif newAutoscanDelay >= 3600 then  -- 1 hour
  813.     data.radar_autoscanDelay = 3600
  814.   else
  815.     data.radar_autoscanDelay = newAutoscanDelay
  816.   end
  817. end
  818.  
  819. function radar_setMonitorIndex(newIndex)
  820.   if #monitors == 0 or newIndex < 0 or newIndex > #monitors then
  821.     data.radar_monitorIndex = 0
  822.   else
  823.     data.radar_monitorIndex = newIndex
  824.   end
  825. end
  826.  
  827. function radar_getMonitor()
  828.   if data.radar_monitorIndex > 0 and data.radar_monitorIndex <= #monitors then
  829.     return monitors[data.radar_monitorIndex]
  830.   else
  831.     return nil
  832.   end
  833. end
  834.  
  835. function radar_scan()
  836.   local monitor = radar_getMonitor()
  837.   if radar == nil or radar.isInterfaced() == nil then
  838.     draw_warning(monitor, "No radar")
  839.     return false
  840.   end
  841.   local energyStored, energyMax, _ = radar.getEnergyStatus()
  842.   if energyStored == nil then energyStored = 0 end
  843.   if energyMax == nil or energyMax == 0 then energyMax = 1 end
  844.  
  845.   radar.radius(radius)
  846.   local success, result = radar.getEnergyRequired()
  847.   if not success then
  848.     showErrorAndExit(result)
  849.   end
  850.   local energyRequired = result
  851.  
  852.   if energyRequired <= 0 or energyStored < energyRequired then
  853.     textOut((w / 2) - 7, 1, " /!\\  LOW POWER ", colors.white, colors.red)
  854.     os.sleep(1)
  855.    
  856.     return 0
  857.   end
  858.   if radar_timerId ~= -1 and radar.getResultsCount() == -1 then
  859.     draw_warning(monitor, "Already scanning...")
  860.     return false
  861.   end
  862.   radar_timerId = os.startTimer(radar_timerLength)
  863.  
  864.   radar.radius(data.radar_radius)
  865.   if radar.start() then
  866.     draw_warning(monitor, "Scanning...")
  867.   end
  868.   return false
  869. end
  870.  
  871. function radar_scanDone()
  872.   local numResults = radar.getResultsCount()
  873.   data.radar_results = {}
  874.   if (numResults ~= 0) then
  875.     for i = 0, numResults do
  876.       local success, type, name, x, y, z = radar.getResult(i)
  877.       if success then
  878.         if name == "" then
  879.           name = "?"
  880.         end
  881.         data.radar_results[i] = { x = x, y = y, z = z, name = name, type = type }
  882.       end
  883.     end
  884.     data.radar_scale = data.radar_radius
  885.   end
  886.   data_save()
  887.   radar_drawMap()
  888. end
  889.  
  890. function draw_text(monitor, x, y, text, textColor, backgroundColor)
  891.   if monitor == nil then
  892.     term.setCursorPos(x, y)
  893.     if textColor ~= nil then term.setTextColor(textColor) end
  894.     if backgroundColor ~= nil then term.setBackgroundColor(backgroundColor) end
  895.     term.write(text)
  896.     local xt, yt = term.getCursorPos()
  897.     term.setCursorPos(1, yt + 1)
  898.   else
  899.     monitor.setCursorPos(x, y)
  900.     if textColor ~= nil then monitor.setTextColor(textColor) end
  901.     if backgroundColor ~= nil then monitor.setBackgroundColor(backgroundColor) end
  902.     monitor.write(text)
  903.     local xt, yt = monitor.getCursorPos()
  904.     monitor.setCursorPos(1, yt + 1)
  905.   end
  906. end  
  907.  
  908. function draw_warning(monitor, text)
  909.   local screenWidth, screenHeight
  910.   if monitor == nil then
  911.     screenWidth, screenHeight = term.getSize()
  912.   else
  913.     screenWidth, screenHeight = monitor.getSize()
  914.   end
  915.   local centerX = math.floor(screenWidth / 2)
  916.   local centerY = math.floor(screenHeight / 2)
  917.   local halfWidth = math.ceil(string.len(text) / 2)
  918.   local blank = string.sub("                              ", - (string.len(text) + 2))
  919.  
  920.   draw_text(monitor, centerX - halfWidth - 1, centerY - 1, blank, colors.white, colors.red)
  921.   draw_text(monitor, centerX - halfWidth - 1, centerY    , " " .. text .. " ", colors.white, colors.red)
  922.   draw_text(monitor, centerX - halfWidth - 1, centerY + 1, blank, colors.white, colors.red)
  923. end
  924.  
  925. function draw_centeredText(monitor, y, text)
  926.   local screenWidth, screenHeight
  927.   if monitor == nil then
  928.     screenWidth, screenHeight = term.getSize()
  929.   else
  930.     screenWidth, screenHeight = monitor.getSize()
  931.   end
  932.   local x = math.floor(screenWidth / 2 - string.len(text) / 2 + 0.5)
  933.  
  934.   draw_text(monitor, x, y, text, nil, nil)
  935. end
  936.  
  937. function radar_drawContact(monitor, contact)
  938.   local screenWidth, screenHeight
  939.   if monitor == nil then
  940.     screenWidth, screenHeight = term.getSize()
  941.   else
  942.     screenWidth, screenHeight = monitor.getSize()
  943.   end
  944.  
  945.   local screenX = (radar_x + data.radar_offsetX - contact.x) / data.radar_scale
  946.   local screenY = (radar_z + data.radar_offsetY - contact.z) / data.radar_scale
  947.   local visible = true
  948.  
  949.   if screenX <= -1 or screenX >= 1 or screenY <= -1 or screenY >= 1 then
  950.     screenX = math.min(1, math.max(-1, screenX))
  951.     screenY = math.min(1, math.max(-1, screenY))
  952.     visible = false
  953.   end
  954.  
  955.   screenX = math.floor(screenX * (screenWidth  - 3) / 2 + ((screenWidth  - 1)  / 2) + 1.5)
  956.   screenY = math.floor(screenY * (screenHeight - 3) / 2 + ((screenHeight - 1) / 2) + 1.5)
  957.  
  958.   if contact.type == "self" then
  959.     draw_text(monitor, screenX, screenY, Style.TextRadarself, Style.CRadarself, Style.BGRadarself)
  960.   else
  961.     draw_text(monitor, screenX, screenY, Style.TextRadarother, Style.CRadarother, Style.BGRadarother)
  962.   end
  963.   if visible then
  964.     local text = contact.name
  965.     screenX = math.min(screenWidth - 1 - string.len(text), math.max(2, math.floor(screenX - string.len(text) / 2 + 0.5)))
  966.     if screenY == (screenHeight - 1) then
  967.       screenY = screenY - 1
  968.     else
  969.       screenY = screenY + 1
  970.     end
  971.     draw_text(monitor, screenX, screenY, text, Style.CRadarother, Style.BGRadarother)
  972.   end
  973. end
  974.  
  975. function radar_drawMap()
  976.   local screenWidth, screenHeight, x, y
  977.   local monitor = radar_getMonitor()
  978.   -- center area
  979.   SetColorRadarmap()
  980.   if monitor == nil then
  981.     term.clear()
  982.     screenWidth, screenHeight = term.getSize()
  983.   else
  984.     monitor.clear()
  985.     screenWidth, screenHeight = monitor.getSize()
  986.   end
  987.   -- borders
  988.   SetColorRadarborder()
  989.   for x = 1, screenWidth do
  990.     if monitor == nil then
  991.       term.setCursorPos(x, 1)
  992.       term.write(" ")
  993.       term.setCursorPos(x, screenHeight)
  994.       term.write(" ")
  995.     else
  996.       monitor.setCursorPos(x, 1)
  997.       monitor.write(" ")
  998.       monitor.setCursorPos(x, screenHeight)
  999.       monitor.write(" ")
  1000.     end
  1001.   end
  1002.   for y = 2, screenHeight - 1 do
  1003.     if monitor == nil then
  1004.       term.setCursorPos(1, y)
  1005.       term.write(" ")
  1006.       term.setCursorPos(screenWidth, y)
  1007.       term.write(" ")
  1008.     else
  1009.       monitor.setCursorPos(1, y)
  1010.       monitor.write(" ")
  1011.       monitor.setCursorPos(screenWidth, y)
  1012.       monitor.write(" ")
  1013.     end
  1014.   end
  1015.   -- title
  1016.   local text = label .. " - Radar map"
  1017. --  if data.radar_results == 0 then
  1018. --    text = text .. " (no contacts)"
  1019. --  else
  1020.     text = text .. " (" .. data.radar_results .. " contacts)"
  1021. --  end
  1022. local delay = 0
  1023. local count
  1024. repeat
  1025.   count = radar.getResultsCount()
  1026.   os.sleep(0.1)
  1027.   delay = delay + 1
  1028. until (count ~= nil and count ~= -1) or delay > 10
  1029.  
  1030. if count ~= nil and count > 0 then
  1031.   for i=0, count-1 do
  1032.     local success, type, name, x, y, z = radar.getResult(i)
  1033.     if success then
  1034.       print(type .. " " .. name .. " @ (" .. x .. " " .. y .. " " .. z .. ")")
  1035.     else
  1036.       showError("Error " .. type)
  1037.     end
  1038.   end
  1039. else
  1040.   print("Nothing was found =(")
  1041.      draw_centeredText(monitor, 1, text)
  1042.   -- status
  1043.   local text = "Scan radius: " .. data.radar_radius
  1044.   if radar ~= nil then
  1045.     local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  1046.     text = text .. " | Energy: " .. energyStored .. " " .. energyUnits
  1047.   end
  1048.   text = text .. " | Scale: " .. data.radar_scale
  1049.   draw_centeredText(monitor, screenHeight, text)
  1050.   -- results
  1051.   SetCursorPos(1, 12)
  1052.   radar_drawContact(monitor, {x = radar_x, y = radar_y, z = radar_z, name = "", type = "self"})
  1053.   for i = 0, data.radar_results - 1 do
  1054.     radar_drawContact(monitor, data.radar_results[i])
  1055.   end
  1056.  
  1057.   -- restore defaults
  1058.   SetColorDefault()
  1059. end
  1060.  
  1061. radar_waitingNextScan = false
  1062. function radar_timerEvent()
  1063.   radar_timerId = -1
  1064.   if radar_waitingNextScan then
  1065.     radar_waitingNextScan = false
  1066.     radar_scan() -- will restart timer
  1067.   else
  1068.     local numResults = radar.getResultsCount()
  1069.     if numResults ~= -1 then
  1070.       radar_scanDone()
  1071.       if data.radar_autoscan then
  1072.         radar_waitingNextScan = true
  1073.         radar_timerId = os.startTimer(data.radar_autoscanDelay)
  1074.       end
  1075.     else -- still scanning
  1076.     --  radar_timerId = os.startTimer(data.radar_autoscanDelay)
  1077.     end
  1078.   end
  1079. end
  1080.  
  1081. ----------- Boot sequence
  1082. math.randomseed(os.time())
  1083. label = os.getComputerLabel()
  1084. if not label then
  1085.   label = "" .. os.getComputerID()
  1086. end
  1087.  
  1088. -- read configuration
  1089. data_read()
  1090. clear()
  1091. print("data_read...")
  1092.  
  1093. -- initial scanning
  1094. monitors = {}
  1095. ShowTitle(label .. " - Connecting...")
  1096. WriteLn("")
  1097.  
  1098. sides = peripheral.getNames()
  1099. reactor = nil
  1100. mininglasers = {}
  1101. reactorlasers = {}
  1102. cloakingcores = {}
  1103. forcefieldprojectors = {}
  1104. warpdrivetransportercores = {}
  1105. shipcontrollers = {}
  1106. ship = nil
  1107. radar = nil
  1108. for key,side in pairs(sides) do
  1109.   os.sleep(0)
  1110.   Write("Checking " .. side .. " ")
  1111.   local componentType = peripheral.getType(side)
  1112.   Write(componentType .. " ")
  1113.   if componentType == "warpdriveShipController" then
  1114.     Write("wrapping!")
  1115.     table.insert(shipcontrollers, peripheral.wrap(side))
  1116.   elseif componentType == "warpdriveShipCore" then
  1117.     Write("wrapping!")
  1118.     ship = peripheral.wrap(side)
  1119.   elseif componentType == "warpdriveEnanReactorCore" then
  1120.     Write("wrapping!")
  1121.     reactor = peripheral.wrap(side)
  1122.   elseif componentType == "warpdriveEnanReactorLaser" then
  1123.     Write("wrapping!")
  1124.     local wrap = peripheral.wrap(side)
  1125.     table.insert(reactorlasers, { side = wrap.side(), wrap = wrap })
  1126.   elseif componentType == "warpdriveMiningLaser" then
  1127.     WriteLn("Wrapping " .. side)
  1128.     table.insert(mininglasers, peripheral.wrap(side))
  1129.   elseif componentType == "warpdriveCloakingCore" then
  1130.     Write("wrapping!")
  1131.     table.insert(cloakingcores, peripheral.wrap(side))
  1132.   elseif componentType == "warpdriveForceFieldProjector" then
  1133.     Write("wrapping!")
  1134.     table.insert(forcefieldprojectors, peripheral.wrap(side))
  1135.   elseif componentType == "warpdriveTransporterCore" then
  1136.     Write("wrapping!")
  1137.     table.insert(warpdrivetransportercores, peripheral.wrap(side))
  1138.   elseif componentType == "warpdriveRadar" then
  1139.     Write("wrapping!")
  1140.     radar = peripheral.wrap(side)
  1141.   elseif componentType == "monitor" then
  1142.     Write("wrapping!")
  1143.     lmonitor = peripheral.wrap(side)
  1144.     table.insert(monitors, lmonitor)
  1145.     lmonitor.setTextScale(monitor_textScale)
  1146.   end
  1147.   WriteLn("")
  1148. end
  1149.  
  1150. if not os.getComputerLabel() and (ship ~= nil or reactor ~= nil) then
  1151.   data_setName()
  1152. end
  1153.  
  1154. -- peripherals status
  1155. function connections_page()
  1156.   SetCursorPos(0, 1)
  1157.   SetColorTitle()
  1158.   ShowTitle(label .. " - Connections")
  1159.  
  1160.   WriteLn("")
  1161.   if #monitors == 0 then
  1162.     SetColorWarning()
  1163.     WriteLn("No Monitor detected")
  1164.   elseif #monitors == 1 then
  1165.     SetColorSuccess()
  1166.     WriteLn("1 monitor detected")
  1167.   else
  1168.     SetColorSuccess()
  1169.     WriteLn(#monitors .. " Monitors detected")
  1170.   end
  1171.   if radar == nil or radar.isInterfaced() == nil then
  1172.     SetColorWarning()
  1173.     WriteLn("No radar detected")
  1174.   else
  1175.     SetColorSuccess()
  1176.     WriteLn("Radar detected")
  1177.   end
  1178.  
  1179.   WriteLn("")
  1180.   SetColorDefault()
  1181.   WriteLn("Please refer to below menu for keyboard controls")
  1182.   WriteLn("For example, press 1 to access Ship page")
  1183. end
  1184.  
  1185. -- peripheral boot up
  1186. clear()
  1187. connections_page()
  1188. SetColorDefault()
  1189. WriteLn("")
  1190. os.sleep(0)
  1191. radar_boot()
  1192. os.sleep(0)
  1193.  
  1194. -- main loop
  1195. abort = false
  1196. refresh = true
  1197. page = connections_page
  1198. keyHandler = nil
  1199. repeat
  1200.   ClearWarning()
  1201.   if refresh then
  1202.     clear()
  1203.     page()
  1204.     menu_common()
  1205.     refresh = false
  1206.   end
  1207.   params = { os.pullEventRaw() }
  1208.   eventName = params[1]
  1209.   address = params[2]
  1210.   if address == nil then address = "none" end
  1211.   -- WriteLn("...")
  1212.   -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. ", " .. params[4] .. " received")
  1213.   -- os.sleep(0.2)
  1214.   if eventName == "key" then
  1215.     keycode = params[2]
  1216.     if char == 88 or char == 120 or keycode == 45 then -- x for eXit
  1217.       os.pullEventRaw()
  1218.       abort = true
  1219.     elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
  1220.       page = connections_page
  1221.       keyHandler = nil
  1222.       refresh = true
  1223.     elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
  1224.       page = core_page
  1225.       keyHandler = core_key
  1226.       refresh = true
  1227.     elseif char == 50 or keycode == 3 or keycode == 80 then -- 2
  1228.       page = cloaking_page
  1229.       keyHandler = cloaking_key
  1230.       refresh = true
  1231.     elseif char == 51 or keycode == 4 or keycode == 81 then -- 3
  1232.       page = shield_page
  1233.       keyHandler = shield_key
  1234.       refresh = true
  1235.     elseif char == 52 or keycode == 5 or keycode == 75 then -- 4
  1236.       page = radar_page
  1237.       keyHandler = radar_key
  1238.       refresh = true
  1239.     elseif char == 53 or keycode == 6  or keycode == 76 then -- 5
  1240.       page = mining_page
  1241.       keyHandler = mining_key
  1242.       refresh = true
  1243.     elseif char == 54 or keycode == 7  or keycode == 77 then -- 6
  1244.       page = mining_page
  1245.       keyHandler = mining_key
  1246.       refresh = true
  1247.     elseif char == 55 or keycode == 8  then -- 7
  1248.       page = reactor_page
  1249.       keyHandler = reactor_key
  1250.       refresh = true
  1251.     elseif char == 56 or keycode == 9  or keycode == 79 then -- 8
  1252.       page = hooks_page_config
  1253.       keyHandler = hooks_page_key
  1254.       refresh = true
  1255.     elseif keyHandler ~= nil and keyHandler(char, keycode) then
  1256.       refresh = true
  1257.       os.sleep(0)
  1258.     elseif char == 0 then -- control chars
  1259.       refresh = false
  1260.       os.sleep(0)
  1261.     else
  1262.       ShowWarning("Key " .. keycode .. " is invalid")
  1263.       os.sleep(0.2)
  1264.     end
  1265.   elseif eventName == "terminate" then
  1266.     abort = true
  1267.   elseif not common_event(eventName, params[2]) then
  1268.     ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  1269.     refresh = true
  1270.     os.sleep(0.2)
  1271.   end
  1272. until abort
  1273.  
  1274. -- clear screens on exit
  1275. SetMonitorColorFrontBack(colors.white, colors.black)
  1276. term.clear()
  1277. if monitors ~= nil then
  1278.   for key,monitor in pairs(monitors) do
  1279.     monitor.clear()
  1280.   end
  1281. end
  1282. SetCursorPos(1, 1)
  1283. WriteLn("Program terminated")
  1284. 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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top