Advertisement
drpepper240

RadarControl v1

Jun 29th, 2022 (edited)
465
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.67 KB | None
  1. -- settings
  2. local defaultSettings = {
  3. -- minerRefreshSeconds = 0.2,
  4. -- inactiveMinerRefreshSeconds = 5,
  5. -- silkTouch = false,
  6. -- onlyOres = true,
  7. -- laserOffset = 8,
  8. globalCoordinates = true,
  9. fontSize = 5,   -- times 10
  10. redstoneOut = false
  11. }
  12.  
  13.  
  14. -- program state
  15. local isQuitting = false
  16. local isScanning = false
  17. -- local startMining = false
  18. -- local stopMining = false
  19. -- local isActive = false
  20. -- local laserCnt = 0
  21. -- local laserStartingH = 0
  22. -- local laserSettingsChanged = true
  23. local energyUnits = nil
  24. local showPositions = false
  25.  
  26. local radar = {}
  27. local settings = {}
  28. local monitor = nil
  29. local globalToLocalX, globalToLocalY
  30.  
  31. -- functions
  32. local BoolToRedGreen, IncFontSize, RedstoneOutput
  33. local Init, Shutdown, ReadSettings, WriteSettings
  34. local DrawAll, DrawHeader, DrawTable, DrawFooter
  35.  
  36. -- loop functions
  37. local UserInputLoop, RedrawLoop, RadarRefreshLoop
  38.  
  39. -- windows
  40. local mainTerm = term.native()
  41. local radarMapWindow, radarStatusWindow, radarResultsWindow
  42.  
  43. function BoolToRedGreen(value)
  44.     if value then
  45.         return colors.green
  46.     else
  47.         return colors.red
  48.     end
  49. end
  50.  
  51.  
  52. function IncFontSize(currentSize)
  53.     if currentSize >= 50 or currentSize < 5 then
  54.         return 5
  55.     else
  56.         return currentSize + 5
  57.     end
  58. end
  59.  
  60.  
  61. function RedstoneOutput(emit)
  62.     for _, side in pairs(redstone.getSides()) do
  63.         rs.setOutput(side, emit)
  64.     end
  65. end
  66.  
  67.  
  68. function ReadSettings()
  69.     local file = fs.open("radarcontrol.cfg","r")
  70.     if file ~= nil then
  71.         settings = textutils.unserialize(file.readAll())
  72.     end
  73.     for k,v in pairs (defaultSettings) do
  74.         if settings[k] == nil then
  75.             settings[k] = defaultSettings[k]
  76.         end
  77.     end
  78.     for k,v in pairs (settings) do
  79.         print("Settings: " .. tostring(k) .. " : " .. tostring(v))
  80.     end
  81. end
  82.  
  83.  
  84. function WriteSettings()
  85.     local file = fs.open("radarcontrol.cfg","w")
  86.     file.write(textutils.serialize(settings))
  87.     file.close()
  88. end
  89.  
  90.  
  91. function Init()
  92.     if not term.isColor() then
  93.         print("Advanced computer required. Go find some gold.")
  94.     end
  95.     ReadSettings()
  96.     WriteSettings()
  97.     local sides = peripheral.getNames()
  98.     for _, side in pairs(sides) do
  99.         if peripheral.getType(side) == "warpdriveRadar" then
  100.             radar.peripheral = peripheral.wrap(side)
  101.             print("Wrapped warpdriveRadar on " .. side .. " side")
  102.             if energyUnits == nil then
  103.                 energyUnits = radar.peripheral.energyDisplayUnits()
  104.             end
  105.             local ok, galaxy, gx, gy, gz, planet = radar.peripheral.getGlobalPosition()
  106.             if ok then
  107.                 radar.x = gx
  108.                 radar.y = gy
  109.                 radar.z = gz
  110.             else
  111.                 print("Radar position failure")
  112.                 return
  113.             end
  114.             local lx, ly, lz = radar.peripheral.getLocalPosition()
  115.             globalToLocalX = lx - gx
  116.             globalToLocalY = ly - gy
  117.         elseif monitor == nil and peripheral.getType(side) == "monitor" then
  118.             print("Wrapping monitor on " .. side .. " side")
  119.             monitor = peripheral.wrap(side)
  120.             monitor.setBackgroundColor(colors.black)
  121.             monitor.clear()
  122.             monitor.setTextScale(settings.fontSize / 10.0)
  123.             monitor.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
  124.             radarMapWindow = window.create(monitor, 1, 1, 1, 1)
  125.         end
  126.     end
  127.    
  128.     radarStatusWindow = window.create(mainTerm, 1, 2, 25, 10)
  129.     radarResultsWindow = window.create(mainTerm, 26, 2, 25, 10)
  130.    
  131.     sleep(1)
  132.     term.clear()
  133.     term.setPaletteColor(colors.lightGray, 0.4, 0.4, 0.4)
  134.     os.queueEvent("redraw")
  135. end
  136.  
  137.  
  138. function Shutdown()
  139.     term.setTextColor(colors.white)
  140.     term.setBackgroundColor(colors.black)
  141.     term.clear()
  142.     term.setPaletteColor(colors.lightGray, term.nativePaletteColor(colors.lightGray))
  143.     term.setCursorPos(1,1)
  144.     RedstoneOutput(false)
  145. end
  146.  
  147.  
  148. function RadarRefreshLoop()
  149.     while not isQuitting do
  150.         local event = os.pullEvent("updateRadarEvent")
  151.         if radar == nil then return end
  152.         isScanning = true
  153.         radar.peripheral.start()
  154.         sleep(radar.peripheral.getScanDuration() + 1)
  155.         isScanning = false
  156.         radar.results = {}
  157.         for i=1, radar.peripheral.getResultsCount() do
  158.             local success, _, name, cx, cy, cz = radar.peripheral.getResult(i)
  159.             if success then
  160.                 table.insert(radar.results, {name = tostring(name), gx = tonumber(x), gy = tonumber(y), gz = tonumber(z)})
  161.             end
  162.         end
  163.         os.queueEvent("redraw")
  164.     end
  165. end
  166.  
  167.  
  168. function UserInputLoop()
  169.     while not isQuitting do
  170.         local event, p2, p3, p4, p5 = os.pullEvent()
  171.         if event == "key" then
  172.             if keys.getName(p2) == "q" then
  173.                 isQuitting = true
  174.             elseif keys.getName(p2) == "p" then
  175.                 showPositions = not showPositions
  176.             elseif keys.getName(p2) == "s" then
  177.                 os.queueEvent("updateRadarEvent")
  178.             elseif keys.getName(p2) == "f" then
  179.                 if monitor ~= nil then
  180.                     settings.fontSize = IncFontSize(settings.fontSize)
  181.                     monitor.setTextScale(settings.fontSize / 10.0)
  182.                 end
  183.             elseif keys.getName(p2) == "down" then
  184.                 radar.peripheral.radius(radar.peripheral.radius() - 1000)
  185.                 laserSettingsChanged = true
  186.             elseif keys.getName(p2) == "up" then
  187.                 radar.peripheral.radius(radar.peripheral.radius() + 1000)
  188.             elseif keys.getName(p2) == "right" then
  189.                 radar.peripheral.radius(radar.peripheral.radius() + 100)
  190.             elseif keys.getName(p2) == "left" then
  191.                 radar.peripheral.radius(radar.peripheral.radius() - 100)
  192.             elseif keys.getName(p2) == "r" then
  193.                 settings.redstoneOut = not settings.redstoneOut
  194.             end
  195.         elseif event == "mouse_scroll" then
  196.             if p2 == 1 then
  197.                 radarResultsWindow.properties.startPosition = radarResultsWindow.properties.startPosition + 1
  198.             elseif p2 == -1 then
  199.                 radarResultsWindow.properties.startPosition = radarResultsWindow.properties.startPosition - 1
  200.             end
  201.         end
  202.         --debug
  203.         term.setCursorPos(1,14)
  204.         term.clearLine()
  205.         term.write("IL: " .. " FS: " .. settings.fontSize .." " .. os.time("ingame"))
  206.         --enddebug
  207.         WriteSettings()
  208.         os.queueEvent("redraw")
  209.     end
  210. end
  211.  
  212.  
  213. function RedrawLoop()
  214.     while not isQuitting do
  215.         local event = os.pullEvent("redraw")
  216.         DrawAll()
  217.     end
  218. end
  219.  
  220.  
  221. function DrawAll()
  222.     DrawHeader(term)
  223.     -- DrawHeader(monitor)
  224.     DrawFooter(term)
  225.     DrawMonitor(monitor)
  226.     DrawRadarStatus(radarStatusWindow)
  227.     DrawRadarResults(radarResultsWindow)
  228. end
  229.  
  230.  
  231. function DrawRadarStatus(drawDevice)
  232.     drawDevice.setTextColor(colors.black)
  233.     drawDevice.setBackgroundColor(colors.orange)
  234.     drawDevice.clear()
  235.     drawDevice.setCursorPos(1,1)
  236.     if radar == nil then
  237.         drawDevice.setTextColor(colors.red)
  238.         drawDevice.write("NO RADAR")
  239.         return
  240.     end
  241.     local eAvail, eFull, eUnits = radar.peripheral.getEnergyStatus()
  242.     local ok, eReq = radar.peripheral.getEnergyRequired()
  243.     local radius = radar.peripheral.radius()
  244.     local duration = radar.peripheral.getScanDuration()
  245.     if (eAvail == nil or eFull == nil or eReq == nil or duration == nil) then return end
  246.     drawDevice.write("Radius: ".. radius .. " m")
  247.     drawDevice.setCursorPos(1,2)
  248.     drawDevice.write(string.format("Needed: %dM EU %d%%", eReq / 1000000, eAvail / eReq * 100))
  249.     drawDevice.setCursorPos(1,3)
  250.     drawDevice.write(string.format("Total: %d%%", eAvail / eFull * 100))
  251.     drawDevice.setCursorPos(1,4)
  252.     drawDevice.write(string.format("Scan duration: %ds", duration))
  253.     drawDevice.setCursorPos(1,5)
  254.     drawDevice.write(string.format("Results: %d", radar.peripheral.getResultsCount()))
  255.  
  256. end
  257.  
  258.  
  259. function DrawRadarResults(drawDevice)
  260.     drawDevice.setTextColor(colors.black)
  261.     drawDevice.setBackgroundColor(colors.yellow)
  262.     drawDevice.clear()
  263.     drawDevice.setCursorPos(1,1)
  264.     if radar == nil or radar.results == nil then
  265.         drawDevice.write("No radar or results")
  266.         return
  267.     end
  268.     local maxX, maxY = drawDevice.getSize()
  269.     local resultsCount = table.getn(radar.results)
  270.     --window properties
  271.     if radarResultsWindow.properties == nil then radarResultsWindow.properties = {startPosition = 1} end
  272.     if radarResultsWindow.properties.startPosition > (resultsCount - maxY) then radarResultsWindow.properties.startPosition = math.max(resultsCount - maxY, 1) end
  273.     --table
  274.     for py = 1, maxY do
  275.         position = py + radarResultsWindow.properties.startPosition
  276.         if position <= resultsCount then
  277.             drawDevice.setCursorPos(1, py)
  278.             drawDevice.write(string.format("%d %s", py, radar.results[position].name))
  279.         end
  280.     end
  281.     --scrollbar
  282.     if radarResultsWindow.properties.startPosition > 1 then
  283.         drawDevice.setCursorPos(maxX, 1)
  284.         drawDevice.write("\30")
  285.     end
  286.     if (resultsCount - radarResultsWindow.properties.startPosition - maxY) > 0 then
  287.         drawDevice.setCursorPos(maxX, maxY)
  288.         drawDevice.write("\31")
  289.     end
  290. end
  291.  
  292.  
  293. function DrawHeader(drawDevice)
  294.     if drawDevice == nil then
  295.         return
  296.     end
  297.     local maxX, maxY = drawDevice.getSize()
  298.     local str = "RadarControl v1 by DrPepper"
  299.     drawDevice.setCursorPos(math.ceil((maxX - string.len(str)) / 2 + 1), 1)
  300.     drawDevice.setTextColor(colors.orange)
  301.     drawDevice.setBackgroundColor(colors.black)
  302.     drawDevice.clearLine()
  303.     drawDevice.write(str)
  304.  
  305.     if isQuitting then
  306.         drawDevice.setCursorPos(1,1)
  307.         drawDevice.setTextColor(colors.white)
  308.         drawDevice.setBackgroundColor(colors.black)
  309.         drawDevice.clearLine()
  310.         drawDevice.write("Quitting...")
  311.     end
  312. end
  313.  
  314. --###################################################
  315. --#    Status                   Energy    Hei Min/Tot
  316. --99 o Status message           123456 EU 999 123/456
  317.  
  318. function DrawTable(drawDevice)
  319.     if drawDevice == nil then
  320.         return
  321.     end
  322.     local maxX, maxY = drawDevice.getSize()
  323.     drawDevice.setCursorPos(1, 3)
  324.     drawDevice.setTextColor(colors.black)
  325.     drawDevice.setBackgroundColor(colors.orange)
  326.     drawDevice.clearLine()
  327.     drawDevice.write("#    Status")
  328.     local xAlign = maxX - 21
  329.     if xAlign < 5 then xAlign = 5 end
  330.     drawDevice.setCursorPos(xAlign, 3)
  331.     drawDevice.write(string.format(" %-9.9s %-3.3s %-3.3s/%-3.3s", "Energy", "Height", "Mined", "Total"))
  332.    
  333.     drawDevice.setCursorPos(1, 4)
  334.     for k,v in pairs (lasers) do
  335.         local cx, cy = drawDevice.getCursorPos()
  336.         if cy % 2 == 1 then
  337.             drawDevice.setBackgroundColor(colors.gray)
  338.         else
  339.             drawDevice.setBackgroundColor(colors.lightGray)
  340.         end
  341.         drawDevice.setTextColor(colors.white)
  342.         drawDevice.clearLine()
  343.         drawDevice.write(string.format("%2d", tonumber(k)))
  344.         drawDevice.setTextColor(BoolToRedGreen(v.isActive))
  345.         drawDevice.write(" \7 ")
  346.         drawDevice.setTextColor(colors.white)
  347.         local status = tostring(v.status)
  348.         if showPositions then
  349.             status = tostring(v.x) .. "; " .. tostring(v.y) .. "; " .. tostring(v.z)
  350.         end
  351.         drawDevice.write(status)
  352.         drawDevice.setCursorPos(xAlign, cy)
  353.         drawDevice.write(string.format(" %-6s %-2.2s %3s %3.3s/%-3.3s",
  354.             tostring(v.energy),
  355.             energyUnits,
  356.             tostring(v.currentLayer),
  357.             tostring(v.mined),
  358.             tostring(v.total)))
  359.         drawDevice.setCursorPos(1, cy + 1)
  360.     end
  361. end
  362.  
  363.  
  364. function DrawFooter(drawDevice)
  365.     local maxX, maxY = drawDevice.getSize()
  366.     drawDevice.setTextColor(colors.white)
  367.     drawDevice.setBackgroundColor(colors.gray)
  368.     drawDevice.setCursorPos(1, maxY-2)
  369.     drawDevice.clearLine()
  370.     drawDevice.blit(    "[P] Toggle position   [F] Font size   [R] Redstone",
  371.                         "01000000000000000000000100000000000000010000000000",
  372.                         "77777777777777777777777777777777777777777777777777"    )
  373.     drawDevice.setBackgroundColor(colors.gray)
  374.     drawDevice.setCursorPos(1, maxY-1)
  375.     drawDevice.clearLine()
  376.     drawDevice.blit(    "                [ ]              [\24\25\26\27] Offset:",
  377.                         "01000000000000000100000000000000001111000000000",
  378.                         "77777777777777777777777777777777777777777777777"   )
  379.     drawDevice.setBackgroundColor(colors.black)
  380.     drawDevice.write(string.format(" %3s", settings.laserOffset))
  381.     drawDevice.setBackgroundColor(colors.gray)
  382.     drawDevice.setCursorPos(1, maxY)
  383.     drawDevice.clearLine()
  384.     drawDevice.blit(    "[M]        [S] Scan   [Q] Exit",
  385.                         "010000000000100000000001000000",
  386.                         "777777777777777777777777777777"    )
  387. end
  388.  
  389.  
  390. function DrawMonitor(m)
  391.     if m == nil then
  392.         return
  393.     end
  394.     local maxX, maxY = m.getSize()
  395.     --assuming landscape orientation
  396.     -- DrawMap(m, 1, 1, maxY)
  397.     radarMapWindow.reposition(1, 1, maxY, maxY)
  398.     DrawMapWindow(radarMapWindow)
  399. end
  400.  
  401.  
  402. function DrawMapWindow(m)
  403.     local size = math.min(m.getSize())
  404.     if size % 2 == 0 then
  405.         size = size - 1
  406.     end
  407.     local mid = math.ceil(size/2)
  408.     local tColor = "1"
  409.     local bgColor = "7"
  410.     m.setBackgroundColor(colors.gray)
  411.     m.clear()
  412.    
  413.     --debug
  414.     mainTerm.setCursorPos(1,15)
  415.     mainTerm.clearLine()
  416.     mainTerm.write("size:" .. size .. " mid:" .. mid .." " .. os.time("ingame"))
  417.     --enddebug
  418.    
  419.     -- horizontal lines
  420.     for px = 2, size-1 do
  421.         m.setCursorPos(px, 1)
  422.         m.blit("\131", tColor, bgColor)
  423.         m.setCursorPos(px, size)
  424.         m.blit("\143", bgColor, tColor)
  425.         m.setCursorPos(px, mid)
  426.         m.blit("\140", tColor, bgColor)
  427.     end
  428.    
  429.     -- vertical lines
  430.     for py = 2, size-1 do
  431.         m.setCursorPos(1, py)
  432.         m.blit("\149", tColor, bgColor)
  433.         m.setCursorPos(size, py)
  434.         m.blit("\149", bgColor, tColor)
  435.         m.setCursorPos(mid, py)
  436.         m.blit("\149", tColor, bgColor)
  437.     end
  438.    
  439.     --corners
  440.     m.setCursorPos(1, 1)
  441.     m.blit("\151", tColor, bgColor)
  442.     m.setCursorPos(size, 1)
  443.     m.blit("\148", bgColor, tColor)
  444.     m.setCursorPos(1, size)
  445.     m.blit("\138", bgColor, tColor)
  446.     m.setCursorPos(size, size)
  447.     m.blit("\133", bgColor, tColor)
  448.     m.setCursorPos(mid, 1)
  449.     m.blit("\151", tColor, bgColor)
  450.     m.setCursorPos(1, mid)
  451.     m.blit("\157", tColor, bgColor)
  452.     m.setCursorPos(mid, mid)
  453.     m.blit("\157", tColor, bgColor)
  454.     m.setCursorPos(size, mid)
  455.     m.blit("\145", bgColor, tColor)
  456.     m.setCursorPos(mid, size)
  457.     m.blit("\138", bgColor, tColor)
  458. end
  459.  
  460. -- drawDevice, top left x, top lefty, size (width and height) in symbols
  461. function DrawMap(m, x, y, size)
  462.     if not (size % 2) then
  463.         size = size - 1
  464.     end
  465.     if size < 3 then return end
  466.     local midX = x + math.floor(size/2)
  467.     local midY = y + math.floor(size/2)
  468.     local tColor = "0"
  469.     local bgColor = "7"
  470.    
  471.     --fill
  472.     for py = y+1, y+size-2 do
  473.         m.setCursorPos(x+1, py)
  474.         for px = x+1, x+size-2 do
  475.             m.blit("\128", tColor, bgColor)
  476.         end
  477.     end
  478.    
  479.     -- horizontal lines
  480.     for px = x+1, x+size-1 do
  481.         m.setCursorPos(px, y)
  482.         m.blit("\131", tColor, bgColor)
  483.         m.setCursorPos(px, y + size - 1)
  484.         m.blit("\143", bgColor, tColor)
  485.         m.setCursorPos(px, midY)
  486.         m.blit("\140", tColor, bgColor)
  487.     end
  488.    
  489.     -- vertical lines
  490.     for py = y+1, y+size-1 do
  491.         m.setCursorPos(x, py)
  492.         m.blit("\149", tColor, bgColor)
  493.         m.setCursorPos(x + size - 1, py)
  494.         m.blit("\149", bgColor, tColor)
  495.         m.setCursorPos(midX, py)
  496.         m.blit("\149", tColor, bgColor)
  497.     end
  498.    
  499.     --corners
  500.     m.setCursorPos(x, y)
  501.     m.blit("\151", tColor, bgColor)
  502.     m.setCursorPos(x+size-1, y)
  503.     m.blit("\148", bgColor, tColor)
  504.     m.setCursorPos(x, y+size-1)
  505.     m.blit("\138", bgColor, tColor)
  506.     m.setCursorPos(x+size-1, y+size-1)
  507.     m.blit("\133", bgColor, tColor)
  508.     m.setCursorPos(midX, y)
  509.     m.blit("\151", tColor, bgColor)
  510.     m.setCursorPos(x, midY)
  511.     m.blit("\157", tColor, bgColor)
  512.     m.setCursorPos(midX, midY)
  513.     m.blit("\157", tColor, bgColor)
  514.     m.setCursorPos(x+size-1, midY)
  515.     m.blit("\145", bgColor, tColor)
  516.     m.setCursorPos(midX, y+size-1)
  517.     m.blit("\138", bgColor, tColor)
  518. end
  519.  
  520. Init()
  521. parallel.waitForAny(RedrawLoop, UserInputLoop, RadarRefreshLoop)
  522. Shutdown()
Advertisement
RAW Paste Data Copied
Advertisement