Advertisement
minecraftboii

reactor_monitor.lua

Jul 17th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. local reactorName = "BigReactors-Reactor_0"
  2. local monitorName = "monitor_0"
  3.  
  4. -- util
  5. function percentage(value, max)
  6.     return math.ceil(value / max * 10000) * 0.01
  7. end
  8.  
  9. function convertTimeToTick(time)
  10.     return (os.time() * 1000 + 18000) % 24000
  11. end
  12.  
  13. function getDeltaTicks(lastTick)
  14.     local now = os.time()
  15.     now = convertTimeToTick(now)
  16.     if now < lastTick then lastTick = lastTick - 24000 end
  17.     return now - lastTick
  18. end
  19.  
  20. -- formatting
  21. function format_int(number)
  22.     if number == nil then number = 0 end
  23.   local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  24.  
  25.   -- reverse the int-string and append a comma to all blocks of 3 digits
  26.   int = int:reverse():gsub("(%d%d%d)", "%1,")
  27.   -- reverse the int-string back remove an optional comma and put the
  28.   -- optional minus and fractional part back
  29.   return minus .. int:reverse():gsub("^,", "") .. fraction
  30. end
  31.  
  32. -- monitor related
  33.  
  34. --display text text on monitor, "mon" peripheral
  35. function draw_text(mon, x, y, text, text_color, bg_color)
  36.   mon.monitor.setBackgroundColor(bg_color)
  37.   mon.monitor.setTextColor(text_color)
  38.   mon.monitor.setCursorPos(x,y)
  39.   mon.monitor.write(text)
  40. end
  41.  
  42. function draw_text_right(mon, offset, y, text, text_color, bg_color)
  43.   mon.monitor.setBackgroundColor(bg_color)
  44.   mon.monitor.setTextColor(text_color)
  45.   mon.monitor.setCursorPos(mon.X-string.len(tostring(text))-offset,y)
  46.   mon.monitor.write(text)
  47. end
  48.  
  49. function draw_text_lr(mon, x, y, offset, text1, text2, text1_color, text2_color, bg_color)
  50.     draw_text(mon, x, y, text1, text1_color, bg_color)
  51.     draw_text_right(mon, offset, y, text2, text2_color, bg_color)
  52. end
  53.  
  54. --draw line on computer terminal
  55. function draw_line(mon, x, y, length, color)
  56.     if length < 0 then
  57.       length = 0
  58.     end
  59.     mon.monitor.setBackgroundColor(color)
  60.     mon.monitor.setCursorPos(x,y)
  61.     mon.monitor.write(string.rep(" ", length))
  62. end
  63.  
  64. --create progress bar
  65. --draws two overlapping lines
  66. --background line of bg_color
  67. --main line of bar_color as a percentage of minVal/maxVal
  68. function progress_bar(mon, x, y, length, minVal, maxVal, bar_color, bg_color)
  69.   draw_line(mon, x, y, length, bg_color) --backgoround bar
  70.   local barSize = math.floor((minVal/maxVal) * length)
  71.   draw_line(mon, x, y, barSize, bar_color) --progress so far
  72. end
  73.  
  74. function clear(mon)
  75.   term.clear()
  76.   term.setCursorPos(1,1)
  77.   mon.monitor.setBackgroundColor(colors.black)
  78.   mon.monitor.clear()
  79.   mon.monitor.setCursorPos(1,1)
  80. end
  81.  
  82. -- monitor
  83. local mon, monitor, monX, monY
  84. monitor = peripheral.wrap(monitorName)
  85. monX, monY = monitor.getSize()
  86. mon = {}
  87. mon.monitor, mon.X, mon.Y = monitor, monX, monY
  88.  
  89. -- reactor
  90. local reactor = peripheral.wrap(reactorName)
  91. local maxHeat = 2000
  92. local maxEnergy = 10000000
  93.  
  94. function update()
  95.     local lastTick = convertTimeToTick(os.time())
  96.     local previousEnergy = 0
  97.     while true do
  98.         print("clearing")
  99.         clear(mon)
  100.         print("working")
  101.        
  102.         if reactor == nil or not reactor.getConnected() then
  103.             draw_text(mon, 1, 1, "No reactor found", colors.red, colors.black)
  104.             return
  105.         end
  106.        
  107.         -- local castingTemperature = reactor.getCasingTemperature()
  108.         -- local fuelAmount = reactor.getFuelAmount()
  109.         -- local wasteAmount = reactor.getWasteAmount()
  110.         -- local maxFuelAmount = reactor.getFuelAmountMax()
  111.         -- local energyProduced = reactor.getEnergyProducedLastTick()
  112.         -- local fuelConsumed = reactor.getFuelConsumedLastTick()
  113.  
  114.         -- Reactor status
  115.         local status = "INACTIVE"
  116.         local statusColor = colors.red
  117.         local active = reactor.getActive()
  118.         if active then
  119.             status = "ACTIVE"
  120.             statusColor = colors.lime
  121.         end
  122.         draw_text_lr(mon, 2, 2, 1, "Reactor Status", status, colors.white, statusColor, colors.black)
  123.  
  124.         -- Power generation
  125.         local energy = reactor.getEnergyStored()
  126.         if energy >= maxEnergy - maxEnergy * 0.001 then
  127.             draw_text_lr(mon, 2, 4, 1, "Delta power", "max", colors.white, colors.green, colors.black)
  128.         elseif energy <= maxEnergy * 0.0001 then
  129.             draw_text_lr(mon, 2, 4, 1, "Delta power", "empty", colors.white, colors.red, colors.black)
  130.         else
  131.             local deltaTicks = getDeltaTicks(lastTick)
  132.             local deltaEnergy = (energy - previousEnergy) / deltaTicks
  133.             local energyColor = colors.red
  134.             if deltaEnergy >= 0 then energyColor = colors.green end
  135.             draw_text_lr(mon, 2, 4, 1, "Delta power", format_int(deltaEnergy) .. " rf/t", colors.white, energyColor, colors.black)
  136.         end
  137.         previousEnergy = energy
  138.         lastTick = convertTimeToTick(os.time())
  139.  
  140.         -- Reactivity
  141.         local reactivity = reactor.getFuelReactivity()
  142.         draw_text_lr(mon, 2, 6, 1, "Reactivity", reactivity .. "%", colors.white, colors.lightBlue, colors.black)
  143.  
  144.         -- Fuel to waste ratio
  145.  
  146.         -- Fuel temperature
  147.         local fuelTemperature = reactor.getFuelTemperature()
  148.         local fuelColor = colors.red
  149.         if fuelTemperature < 1200 then fuelColor = colors.orange end
  150.         if fuelTemperature < 1000 then fuelColor = colors.yellow end
  151.         if fuelTemperature < 750 then fuelColor = colors.lime end
  152.  
  153.         -- Casing temperature
  154.         sleep(0.1)
  155.     end
  156. end
  157.  
  158. update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement