Advertisement
drpepper240

ascii mod

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