Advertisement
zaboodable

MC - TE PowerMonitor

Feb 15th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.57 KB | None | 0 0
  1. local monitor = peripheral.wrap("left")
  2.  
  3. -- Config
  4. local refreshDelay = 8
  5. local title = "Dooheeg Power Monitor v1.0"
  6. local reactorPrefix = "BigReactors-Reactor_"
  7. local cellPrefix = "tile_thermalexpansion_cell_resonant_name_"
  8.  
  9. -- Reactor Config
  10. local powerReactorThresholdMin = 0.25
  11. local powerReactorThresholdMax = 0.9
  12.  
  13. -- Bar Config
  14. local barHeight = 5
  15. local fillPercentHigh    = 0.5
  16. local fillPercentMid     = 0.25
  17. local textColor          = colors.white
  18. local fillColorHigh      = colors.green
  19. local fillColorMid       = colors.orange
  20. local fillColorLow       = colors.red
  21. local nofillColor        = colors.gray
  22.  
  23. local mWidth, mHeight = monitor.getSize()
  24.  
  25. -- Total stored power
  26. local totalPowerMax      = 0
  27. local totalPowerCurrent  = 0
  28.  
  29. -- Power use tracking
  30. local powerUsed          = 0
  31. local powerPerSecond     = 0
  32. local powerPerTick       = 0
  33.  
  34. local reactors = {}
  35. local cells = {}
  36. local reactorsEnabled = false
  37.  
  38. function GetReactors(_count)
  39.   for i = 0, _count do
  40.     local reactor = peripheral.wrap(reactorPrefix .. _count)
  41.     table.insert(reactors, reactor)
  42.   end
  43. end
  44.  
  45. function SetReactorState(enabled)
  46.     reactorsEnabled = enabled
  47.     for i = 1, #reactors do
  48.         local reactor = reactors[i]
  49.         reactor.setActive(enabled)
  50.     end
  51. end
  52.  
  53. function GetCells(_count)
  54.     for i = 0, _count do
  55.         local cell = peripheral.wrap(cellPrefix .. i)
  56.         table.insert(cells, cell)
  57.     end
  58. end
  59.  
  60. function CheckMaxPower()
  61.     for i = 1, #cells do
  62.         local cell = cells[i]
  63.         local mPow = cell.getMaxEnergyStored()
  64.         totalPowerMax = totalPowerMax + mPow
  65.     end
  66. end
  67.  
  68. function CheckPower()
  69.     totalPowerCurrent = 0
  70.     for i = 1, #cells do
  71.         local cell = cells[i]
  72.         local cPow = cell.getEnergyStored()
  73.         totalPowerCurrent = totalPowerCurrent + cPow
  74.     end
  75. end
  76.  
  77. function Write(_x, _y, _text)
  78.     WriteC(_x, _y, _text, textColor)
  79. end
  80.  
  81. function WriteC(_x, _y, _text, _tCol)
  82.     monitor.setCursorPos(_x, _y)
  83.     monitor.setTextColor(_tCol)
  84.     monitor.write(_text)
  85. end
  86.  
  87. function Percent(_a, _b)
  88.     return (_a / _b)
  89. end
  90.  
  91. function DrawBar(_y, _height, _fill)
  92.     local barWidth = mWidth - 2
  93.     local filled = math.ceil(barWidth * _fill)
  94.     local notFilled = barWidth - filled
  95.     local fillColor
  96.     if(_fill > fillPercentHigh) then
  97.         fillColor = fillColorHigh
  98.     elseif (_fill > fillPercentMid) then
  99.         fillColor = fillColorMid
  100.     else
  101.         fillColor = fillColorLow
  102.     end
  103.  
  104.     local by
  105.     for i = 0, _height - 1 do
  106.         by = _y + i
  107.         -- Draw Filled Section
  108.         for bx = 2, filled + 1 do
  109.             WriteC(bx, by, "#", fillColor)
  110.         end
  111.  
  112.         -- Draw Unfilled Section
  113.         for bx = 2, notFilled + 1 do
  114.             WriteC(bx + filled, by, "#", nofillColor)
  115.         end
  116.     end
  117. end
  118.  
  119. function Round(num, numPlaces)
  120.     local mult = 10 ^ (numplaces or 0)
  121.     return math.floor(num * mult + 0.5) / mult
  122. end
  123.  
  124. function FBN(num, places, space)
  125.     num = math.abs(num)
  126.     local numString
  127.     local placeValue = ("%%.%df"):format(places or 0)
  128.     local rnum
  129.     if (not num) then
  130.         return 0
  131.     elseif (num >= 1000000000000) then
  132.         rnum = math.ceil(num / 1000000000000)
  133.         numString = placeValue:format(rnum)
  134.         if (space == 0) then
  135.             numString = numString .. "T"
  136.         else
  137.             numString = numString .. " T"
  138.         end
  139.     elseif (num >= 1000000000) then
  140.         rnum = math.ceil(num / 1000000000)
  141.         numString = placeValue:format(rnum)
  142.         if (space == 0) then
  143.             numString = numString .. "B"
  144.         else
  145.             numString = numString .. " B"
  146.         end
  147.     elseif (num >= 1000000) then
  148.         rnum = math.ceil(num / 1000000)
  149.         numString = placeValue:format(rnum)
  150.         if (space == 0) then
  151.             numString = numString .. "M"
  152.         else
  153.             numString = numString .. " M"
  154.         end
  155.     elseif (num >= 1000) then
  156.         rnum = math.ceil(num / 1000)
  157.         numString = placeValue:format(rnum)
  158.         if (space == 0) then
  159.             numString = numString .. "K"
  160.         else
  161.             numString = numString .. " K"
  162.         end
  163.     else
  164.         numString = num
  165.     end
  166.     return numString
  167. end
  168.  
  169.  
  170.  
  171.  
  172. -- Initialize
  173. GetCells(11)
  174. GetReactors(1)
  175. CheckMaxPower()
  176.  
  177. -- Main Loop
  178. while (true) do
  179.     monitor.clear()
  180.     CheckPower()
  181.  
  182.     -- Calculate power use
  183.     powerUsed = powerUsed - totalPowerCurrent
  184.     powerUsed = -powerUsed
  185.     powerPerSecond = powerUsed / refreshDelay
  186.     powerPerTick = powerPerSecond / 20
  187.  
  188.     -- Calculate power percent
  189.     local powerPercent = Percent(totalPowerCurrent, totalPowerMax)
  190.     DrawBar(7, barHeight, powerPercent)
  191.     Write(2, 2, title .. "   |   Reactor: " .. tostring(reactorsEnabled))
  192.     Write(2, 4, Round(powerPercent * 100) .. "% : " .. FBN(totalPowerCurrent, 2, 0) .. " / " .. FBN(totalPowerMax, 2, 0))
  193.  
  194.     -- Check need for reactor, enable/disable
  195.     if (reactorsEnabled) then
  196.       if (powerPercent > powerReactorThresholdMax) then
  197.         SetReactorState(false)
  198.       end      
  199.     else
  200.       if (powerPercent < powerReactorThresholdMin) then
  201.         SetReactorState(true)
  202.       end
  203.     end
  204.  
  205.     -- Set power use color
  206.     local powerColor
  207.     if (powerUsed < 0) then
  208.         powerColor = colors.red
  209.     else
  210.         powerColor = colors.lime
  211.     end
  212.  
  213.     -- Draw power use
  214.     WriteC(2, 5, FBN(Round(powerPerSecond), 1, 1) .. "RF/s"
  215.         .. " | " .. FBN(Round(powerPerTick), 1, 1) .. "RF/t", powerColor)
  216.  
  217.     powerUsed = totalPowerCurrent
  218.     sleep(refreshDelay)
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement