Advertisement
Guest User

Power.lua

a guest
May 10th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.18 KB | None | 0 0
  1. local term = require("term")
  2. local component = require( "component" )
  3. local gpu = component.gpu
  4. local event = require( "event" )
  5.  
  6. local oldW, oldH = gpu.getResolution()
  7. gpu.setResolution( 160, 50 )
  8.  
  9. colors = {}
  10. colors.red = 0xC14141
  11. colors.green = 0xDA841
  12.  
  13. function clearScreen()
  14.   local oldColor = gpu.getBackground( false )
  15.   local w,h = gpu.getResolution()
  16.   gpu.setBackground( 0x000000, false )
  17.   gpu.fill( 1, 1, w, h, " " )
  18.   gpu.setBackground( oldColor, false )
  19. end
  20.  
  21. function progressBar( label, y, value, maxVal, color, show, unit )
  22.   local oldColor = gpu.getBackground( false )
  23.   gpu.setBackground(0x000000, false)
  24.   gpu.fill( 3, y, 155, 2, " " )
  25.   w = math.floor( value * (155 / maxVal) )
  26.   p = math.floor( (w / 155) * 100 )
  27.   gpu.set( 3, y, label .. ": " .. tostring( p ) .. "%" )
  28.   gpu.setBackground( 0x222222, false )
  29.   gpu.fill( 3, y+1, 155, 1, " " )
  30.   gpu.setBackground( color, false )
  31.   gpu.fill( 3, y+1, w, 1, " " )
  32.   gpu.setBackground( oldColor, false )
  33.   if show then
  34.     local valStr = formatBig( value ) .. unit
  35.     local n = string.len( valStr )
  36.     gpu.set( 158 - n, y, valStr )
  37.   end
  38. end
  39.  
  40.  
  41. function formatBig( value )
  42.   local output = ""
  43.   local valRem = 0
  44.   local valPart = 0
  45.   while value > 0 do
  46.     valRem = math.floor( value / 1000 )
  47.     valPart = value - (valRem * 1000)
  48.     if output == "" then
  49.       output = string.format( "%03d", valPart )
  50.     elseif valRem == 0 then
  51.       output = valPart .. "," .. output
  52.     else
  53.       output = string.format( "%03d", valPart ) .. "," .. output
  54.     end
  55.     value = valRem
  56.   end
  57.   return output
  58. end
  59.  
  60. function getCells()
  61.   local countDcOrb = 0
  62.   local countTEcell = 0
  63.   local countRfTCell = 0
  64.  
  65.   local TEcell = component.list( "energy_device" )
  66.   local DcOrb = component.list("energy_cube")
  67.   local RfTCell = component.list("rftools_powercell")
  68.  
  69.   local cellsID = {}  
  70.   for address, name in pairs(DcOrb) do
  71.     countDcOrb =  countDcOrb + 1
  72.     if countDcOrb > 1 then
  73.       cellsID[address] = "Energy Cube".." "..countDcOrb
  74.     else
  75.       cellsID[address] ="Energy Cube"
  76.     end
  77.   end
  78.   for address, name in pairs(TEcell) do
  79.     countTEcell =  countTEcell + 1
  80.  
  81.     if countTEcell > 1 then
  82.       cellsID[address] = "Thermal Expansion Power Cell".." "..countTEcell
  83.     else
  84.       cellsID[address] = "Thermal Expansion Power Cell"
  85.     end
  86.   end
  87.   for address, name in pairs(RfTCell) do
  88.     countRfTCell = countRfTCell + 1
  89.  
  90.     if countRfTCell > 1 then
  91.       cellsID[address] = "RfTools Power Cell".." "..countRfTCell
  92.     else
  93.       cellsID[address] = "RfTools Power Cell"
  94.     end
  95.   end
  96.   return cellsID
  97. end
  98.  
  99. function getReactors()
  100.   local countReactor = 0
  101.  
  102.   local brReactor = component.list("br_reactor")
  103.  
  104.   local reactorsID = {}
  105.  
  106.   for address, name in pairs(brReactor) do
  107.     if countReactor > 1 then
  108.       reactorsID[address] = "Reactor".." "..countReactor
  109.     else
  110.       reactorsID[address] = "Reactor"
  111.     end
  112.   end
  113.   return reactorsID
  114. end
  115.      
  116.      
  117. function upReactor()
  118.   local brPower = 0
  119.   local brMaxPower = 0
  120.   local brEnergyLastTick = 0
  121.   --local brFuel = 0
  122.   --local brFuelMax = 0
  123.   local reactorid = getReactors()
  124.   for address, name in pairs(reactorid) do
  125.     local reactor = component.proxy(address)
  126.     brPower = brPower + reactor.getEnergyStored()
  127.     brMaxPower = brMaxPower + reactor.getEnergyCapacity()
  128.     brEnergyLastTick = brEnergyLastTick + reactor.getEnergyProducedLastTick()
  129.     --brFuel = brFuel + reactor.getFuelAmount()
  130.     --brMaxFuel = brMaxFuel + reactor.getFuelAmountMax()
  131.   end
  132.   return brPower, brMaxPower, brEnergyLastTick, brFuel, brFuelMax
  133. end
  134.  
  135.  
  136. function getTotal()
  137.   local totalPower = 0
  138.   local totalMaxPower = 0
  139.   local cellid = getCells()
  140.   for address, name in pairs(cellid) do
  141.     local cell = component.proxy( address )
  142.     totalPower = totalPower + cell.getEnergyStored()
  143.     totalMaxPower = totalMaxPower + cell.getMaxEnergyStored()
  144.   end
  145.   return totalPower, totalMaxPower
  146. end      
  147.  
  148. clearScreen()
  149. gpu.set( 67, 1, "Power Monitor" )
  150. local cellsID = getCells()
  151. local reactorsID = getReactors()
  152.  
  153. repeat
  154.   local _,_,x,y = event.pull( 1, "touch" )
  155.   local count = 0
  156.   local countR = 0
  157.   if x and y then goto quit end
  158.   for address, name in pairs(cellsID) do
  159.     local cell = component.proxy( address )
  160.     count = count + 1
  161.     local t = count * 3
  162.     progressBar( name, t , cell.getEnergyStored(), cell.getMaxEnergyStored() , 0x00bb00, true, "RF" )
  163.   end
  164.  
  165.   local brPower, brMaxPower = upReactor()
  166.   local totalPower, totalMaxPower = getTotal()
  167.   for address, name in pairs(reactorsID) do
  168.     local reactor = component.proxy(address)
  169.     countR = countR + 1
  170.     local tR = (countR + count) * 3
  171.     progressBar( name, tR, reactor.getEnergyStored(), reactor.getEnergyCapacity(), 0x00bb00, true, "RF")
  172.     progressBar( "Reactor(Energy Last Tick)", tR + 3, math.ceil(reactor.getEnergyProducedLastTick()), 6200, 0x00bb00, true, "RF")
  173.     if brPower + totalPower < brMaxPower + totalMaxPower then
  174.       reactor.setActive(True)
  175.     else
  176.       reactor.setActive(False)
  177.     end
  178.   end
  179.  
  180.   local brPower, brMaxPower = upReactor()
  181.   local totalPower, totalMaxPower = getTotal()
  182.   progressBar( "TotalPower", 48 - count , totalPower + brPower, totalMaxPower + brMaxPower, 0x00bb00, true,"RF" )
  183. until event.pull(0.1) == "interrupted"
  184.  
  185. ::quit::
  186. gpu.setResolution( oldW, oldH )
  187. clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement