Deloyar

OC Power Monitor

Sep 16th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. --[[ this based in code from Nonsanity https://www.youtube.com/user/Nonsanity. mosty using the GUI!
  2.     The code was edited and changed by jennifer cally "ebony"
  3.     The code was edited and changed by Deloyar
  4. --]]
  5.  
  6.  
  7. local component = require( "component" )
  8. local gpu = component.gpu
  9. local event = require( "event" )
  10.  
  11. local oldW, oldH = gpu.getResolution()
  12. gpu.setResolution( 160, 50 )
  13.  
  14. function clearScreen()
  15.   local oldColor = gpu.getBackground( false )
  16.   local w,h = gpu.getResolution()
  17.   gpu.setBackground( 0x000000, false )
  18.   gpu.fill( 1, 1, w, h, " " )
  19.   gpu.setBackground( oldColor, false )
  20. end
  21.  
  22.  
  23. function progressBar( label, y, value, maxVal, color, show, unit )
  24.   local oldColor = gpu.getBackground( false )
  25.   gpu.setBackground(0x000000, false)
  26.   gpu.fill( 3, y, 155, 2, " " )
  27.   w = math.floor( value * (155 / maxVal) )
  28.   p = math.floor( (w / 155) * 100 )
  29.   gpu.set( 3, y, label .. ": " .. tostring( p ) .. "%" )
  30.   gpu.setBackground( 0x222222, false )
  31.   gpu.fill( 3, y+1, 155, 1, " " )
  32.   gpu.setBackground( color, false )
  33.   gpu.fill( 3, y+1, w, 1, " " )
  34.   gpu.setBackground( oldColor, false )
  35.   if show then
  36.     local valStr = formatBig( value ) .. unit
  37.     local n = string.len( valStr )
  38.     gpu.set( 158 - n, y, valStr )
  39.   end
  40. end
  41.  
  42.  
  43. function formatBig( value )
  44.   local output = ""
  45.   local valRem = 0
  46.   local valPart = 0
  47.   while value > 0 do
  48.     valRem = math.floor( value / 1000 )
  49.     valPart = value - (valRem * 1000)
  50.     if output == "" then
  51.       output = string.format( "%03d", valPart )
  52.     elseif valRem == 0 then
  53.       output = valPart .. "," .. output
  54.     else
  55.       output = string.format( "%03d", valPart ) .. "," .. output
  56.     end
  57.     value = valRem
  58.   end
  59.   return output
  60. end
  61.  
  62. function getCells()
  63.     local countDcOrb = 0
  64.     local countEcell = 0
  65.     local countRfTCell = 0
  66.     local countBECube = 0
  67.     local countAECube = 0
  68.     local countEECube = 0
  69.     local countUECube = 0
  70.    
  71.     local Ecell = component.list("energy_device")
  72.     local DcOrb = component.list("draconic_rf_storage")
  73.     local RfTCell = component.list("rftools_powercell")
  74.     local BECube = component.list("basic_energy_cube")
  75.    
  76.    
  77.     local cellsID = {} 
  78.     for address, name in pairs(DcOrb) do
  79.         countDcOrb =  countDcOrb + 1
  80.         if countDcOrb > 1 then
  81.             cellsID[address] = "Draconic Power Orb".." "..countDcOrb
  82.         else
  83.             cellsID[address] ="Draconic Power Orb 1"
  84.         end
  85.     end
  86.     for address, name in pairs(Ecell) do
  87.         countEcell =  countEcell + 1
  88.  
  89.         if countEcell > 1 then
  90.             cellsID[address] = "Thermal Expansion Power Cell / Capacitor Bank Multiblock".." "..countEcell
  91.         else
  92.             cellsID[address] = "Thermal Expansion Power Cell / Capacitor Bank Multiblock 1"
  93.         end
  94.     end
  95.     for address, name in pairs(RfTCell) do
  96.         countRfTCell = countRfTCell + 1
  97.  
  98.         if countRfTCell > 1 then
  99.             cellsID[address] = "RfTools Power Cell".." "..countRfTCell
  100.         else
  101.             cellsID[address] = "RfTools Power Cell 1"
  102.         end
  103.     end
  104.     for address, name in pairs(BECube) do
  105.         countBECube = countBECube + 1
  106.  
  107.         if countBECube > 1 then
  108.             cellsID[address] = "Mekanism Energy Cube".." "..countBECube
  109.         else
  110.             cellsID[address] = "Mekanism Energy Cube 1"
  111.         end
  112.     end
  113.   return cellsID
  114. end
  115.  
  116. function getTotal()
  117.     local totalPower = 0
  118.     local totalMaxPower = 0
  119.     local cellid = getCells()
  120.     for address, name in pairs(cellid) do
  121.         local cell = component.proxy( address )
  122.         totalPower = totalPower + cell.getEnergyStored()
  123.         totalMaxPower = totalMaxPower + cell.getMaxEnergyStored()
  124.     end
  125.     return totalPower, totalMaxPower
  126.  
  127. end
  128.  
  129. clearScreen()
  130. gpu.set( 67, 1, "Power Monitor" )
  131. local cellsID = getCells()
  132.  
  133. while true do
  134.   local _,_,x,y = event.pull( 1, "touch" )
  135.   local count = 0
  136.   if x and y then goto quit end
  137.   for address, name in pairs(cellsID) do
  138.     local cell = component.proxy( address )
  139.     count = count + 1
  140.     local t = count * 3
  141.     progressBar( name, t , cell.getEnergyStored(), cell.getMaxEnergyStored() , 0x00bb00, true, " RF" )
  142.     end
  143.    
  144.     local totalPower, totalMaxPower = getTotal()
  145.     progressBar( "TotalPower", 48 - count , totalPower, totalMaxPower, 0x00bb00, true, " RF" )
  146.  
  147.   os.sleep(0.25)
  148. end
  149.  
  150.  
  151. ::quit::
  152. gpu.setResolution( oldW, oldH )
  153. clearScreen()
Add Comment
Please, Sign In to add comment