Advertisement
minecraftboii

reactor_monitor.lua

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