Advertisement
knayvik

OC RFTools energy monitor

Jun 4th, 2022 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local sides = require("sides")
  5.  
  6. local monitor = component.screen
  7. local powercell = component.rftools_powercell
  8. local gpu = component.gpu
  9.  
  10. local W, H = nil
  11. local prevEnergy = nil
  12. local minPerTick = 0
  13. local maxPerTick = 0
  14. local curColor = 0xF0F0F0
  15. local maxPower = 0
  16. local startTime = os.time()
  17. local startPower = powercell.getEnergy()
  18.  
  19. local sections = {}
  20. local stats = {}
  21. local buttons = {}
  22.  
  23.  
  24. local colors = {
  25.     white = 0xF0F0F0,
  26.     orange = 0xFF6A00,
  27.     magenta = 0xE57FD8,
  28.     lightBlue = 0x99B2F2,
  29.     yellow = 0xFFEE00,
  30.     lime = 0x7FCC19,
  31.     pink = 0xF2B2CC,
  32.     gray = 0x4C4C4C,
  33.     lightGray = 0x999999,
  34.     cyan = 0x4C99B2,
  35.     purple = 0xB266E5,
  36.     blue = 0x3366CC,
  37.     brown = 0x7F664C,
  38.     green = 0x4CFF00,
  39.     red = 0xFF0000,
  40.     black = 0
  41. }
  42.  
  43. function setSections()
  44.     sections["power"] = { x = 2, y = 2, width = W-4, height = H/3, title = "  POWER  "}
  45.     sections["stats"] = { x = 2, y = H/3 + 4, width = W-4, height = H/2, title = "  STATS  "}
  46. end
  47.  
  48. function setStats(w, h)
  49.     stats["currentRF"] = { x = w/15, y = h + 3, width = 15, height = 1, title = "RF: " }
  50.     stats["maxRF"] = { x = w/15, y = h + 5, width = 15, height = 1, title = "Max RF: " }
  51.     stats["tick"] = { x = w/15, y = h + 7, width = 15, height = 1, title = "RF/T: "}
  52.     stats["minDelta"] = { x = w/3, y = h + 4, width = 10, height = 1, title = ""}
  53.     stats["zeroDelta"] = { x = w/1.6-1, y = h + 4, width = 10, height = 1, title = ""}
  54.     stats["maxDelta"] = { x = w/1.2, y = h + 4, width = 10, height = 1, title = ""}
  55.    
  56. end
  57.  
  58. function setButtons(w, h)
  59.     buttons["resetButton"] = { x = w/1.2, y = h + 10, width = 13, height = 3, title = "    RESET    "}
  60. end
  61.  
  62. function init()
  63.     minPerTick = 0
  64.     maxPerTick = 0
  65.     prevEnergy = nil
  66.     gpu.setResolution(80, 30)
  67.     term.setCursorBlink(false)
  68.     term.clear()
  69.     W, H = gpu.getResolution()
  70.     curColor = colors.white
  71.     setSections()
  72.     setStats(sections["stats"].width, sections["stats"].height)
  73.    
  74.     for name, data in pairs(sections) do
  75.         printBorders(name)
  76.     end
  77.    
  78.     --[[ setButtons(sections["stats"].width, sections["stats"].height)
  79.     local button = buttons["resetButton"]
  80.     drawBar(colors.red, button.x, button.y, button.width, button.height)
  81.     gpu.setForeground(curColor)
  82.     gpu.set(button.x, button.y + 1, button.title) --]]
  83. end
  84.  
  85.  
  86.  
  87. function printBorders(sectionName)
  88.     local s = sections[sectionName]
  89.    
  90.     gpu.setBackground(colors.gray)
  91.     gpu.setForeground(curColor)
  92.     gpu.fill(s.x, s.y, s.width, 1, " ")
  93.     gpu.fill(s.x, s.y, 1, s.height, " ")
  94.     gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
  95.     gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
  96.    
  97.     gpu.setBackground(colors.black)
  98.     gpu.set(s.x + 2, s.y, s.title)
  99. end
  100.  
  101. function convert(power)
  102.     local greatest = 1
  103.     local units = {}
  104.     units[" "] = 1
  105.     units["K"] = 10^3
  106.     units["M"] = 10^6
  107.     units["G"] = 10^9
  108.     suffix = " "
  109.    
  110.     for name, value in pairs(units) do
  111.         if (math.abs(power) >= value) then
  112.             if (value > greatest) then
  113.                 greatest = value
  114.                 suffix = name
  115.             end
  116.         end
  117.     end
  118.     return string.format("%.2f%s", power/units[suffix], suffix)
  119. end
  120.  
  121. function getColor(level)
  122.     if (level < 0.25) then
  123.     return colors.red
  124.     end
  125.     if (level > 0.25) and (level < 0.50) then
  126.     return colors.orange
  127.     end
  128.     if (level > 0.50) and (level < 0.75) then
  129.     return colors.yellow
  130.     end
  131.     return colors.green
  132. end
  133.  
  134. function drawBar(color, x, y, w, h)
  135.     gpu.setBackground(color)
  136.     gpu.fill(x, y, w, h, " ")
  137. end
  138.  
  139. function emitPower(percentage)
  140.     if (percentage < 0.5) then
  141.         component.redstone.setOutput(sides.front, 10)
  142.     elseif (percentage > 0.95) then
  143.         component.redstone.setOutput(sides.front, 0)
  144.     end
  145. end
  146.  
  147. function drawPower(w, h)
  148.     if (maxPower ~= powercell.getMaxEnergy()) then
  149.         init()
  150.         maxPower = powercell.getMaxEnergy()
  151.     end
  152.     percentage = powercell.getEnergy()/powercell.getMaxEnergy()
  153.     emitPower(percentage)
  154.     barX = w/15
  155.     barY = h/1.8
  156.     barW = w-barX+1
  157.     barH = h/2
  158.     drawBar(colors.gray, barX, barY, barW, barH)
  159.     drawBar(getColor(percentage), barX+2, barY+1, (barW-4)*percentage, barH-2)
  160.     gpu.setForeground(colors.black)
  161.     gpu.set(barX+((barW-4)*percentage)/2, barY+2, string.format("%.2f%%", percentage*100))
  162. end
  163.  
  164. function getRFTColor(power)
  165.     if (power > 0) then
  166.         curColor = colors.green
  167.     end
  168.     if (power < 0) then
  169.         curColor = colors.red
  170.     end
  171. end
  172.  
  173. function printDeltaBar(energyDelta)
  174.     local minDelta = stats["minDelta"]
  175.     local zeroDelta = stats["zeroDelta"]
  176.     local maxDelta = stats["maxDelta"]
  177.     local percentage = nil
  178.     local width = nil
  179.     drawBar(colors.gray, minDelta.x + 2, minDelta.y + 2, W - minDelta.x - (W - maxDelta.x), 3)
  180.     if (energyDelta > 0) then
  181.         percentage = energyDelta/maxPerTick
  182.         if (percentage > 0.5) then
  183.             curColor = colors.green
  184.         elseif (percentage < 0.5) then
  185.             curColor = colors.yellow
  186.         end
  187.         width = percentage*(maxDelta.x - zeroDelta.x)
  188.         drawBar(curColor, zeroDelta.x, zeroDelta.y + 3, width, 1)
  189.     elseif (energyDelta < 0) then
  190.         percentage = energyDelta/minPerTick
  191.         if (percentage > 0.5) then
  192.             curColor = colors.red
  193.         elseif (percentage < 0.5) then
  194.             curColor = colors.orange
  195.         end
  196.         width = math.floor(percentage*(zeroDelta.x - minDelta.x))
  197.         drawBar(curColor, zeroDelta.x-width+3, zeroDelta.y + 3, width-2, 1)
  198.     end
  199. end
  200.  
  201. function getStat(statName)
  202.     curColor = colors.white
  203.     if (statName == "currentRF") then
  204.         return convert(powercell.getEnergy())
  205.     end
  206.     if (statName == "maxRF") then
  207.        
  208.         return convert(powercell.getMaxEnergy())
  209.     end
  210.     if (statName == "tick") then
  211.         if (prevEnergy ~= nil) then
  212.             local energyDelta = (powercell.getEnergy()-startPower)/((os.time()-startTime)/6)
  213.         if (energyDelta > maxPerTick) then
  214.             maxPerTick = energyDelta
  215.         end
  216.         if (energyDelta < minPerTick) then
  217.             minPerTick = energyDelta
  218.         end
  219.         printDeltaBar(energyDelta)
  220.         getRFTColor(energyDelta)
  221.         prevEnergy = powercell.getEnergy()
  222.         return convert(energyDelta)
  223.         end
  224.         prevEnergy = powercell.getEnergy()
  225.         return "N/A"
  226.     end
  227.     if (statName == "minDelta") then
  228.         curColor = colors.red
  229.         return convert(minPerTick)
  230.     end
  231.     if (statName == "zeroDelta") then
  232.         return 0
  233.     end
  234.     if (statName == "maxDelta") then
  235.         curColor = colors.green
  236.         return convert(maxPerTick)
  237.     end
  238. end
  239.  
  240. function printStats(statName)
  241.     local s = stats[statName]
  242.     local stat = getStat(statName)
  243.     gpu.setBackground(colors.black)
  244.     drawBar(colors.black, s.x, s.y, s.width, s.height)
  245.     gpu.setForeground(curColor)
  246.     gpu.set(s.x, s.y, s.title..stat)
  247. end
  248.  
  249. function drawStats(w, h)
  250.     for name, data in pairs(stats) do
  251.         printStats(name)
  252.     end
  253. end
  254.  
  255. function getClick()
  256.     local _, _, x, y = event.pull(1,touch)
  257.     if (x == nil) or (y == nil) then
  258.         return nil
  259.     end
  260.     local button = buttons["resetButton"]
  261.     if (x >= button.x) and (y >= button.y) then
  262.         if (x <= button.x + button.width) and (y <= button.y + button.height) then
  263.             init()
  264.         end
  265.     end
  266. end
  267.  
  268. init()
  269. while true do
  270.     startTime = os.time()
  271.     startPower = powercell.getEnergy()
  272.     drawPower(sections["power"].width, sections["power"].height)
  273.     drawStats(sections["stats"].width, sections["stats"].height)
  274.     --getClick()
  275.     os.sleep(1/20)
  276. end
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement