lucasmontec

extreme reactors reactor controller

Jan 25th, 2026 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.01 KB | None | 0 0
  1. -- Configuration
  2. local reactorName = "BigReactors-Reactor_0"
  3. local batteryName = "enderio:vibrant_capacitor_bank_0"
  4.  
  5. -- Graph Settings
  6. local graphScaleSeconds = 1.0 -- How many seconds each pixel/update represents
  7. local turnOnThreshold = 0.30 -- 30%
  8. local turnOffThreshold = 0.90 -- 90%
  9.  
  10. -- Graph Data
  11. local history = {}
  12. local maxHistoryItems = 0
  13.  
  14. -- Peripherals
  15. local reactor = peripheral.wrap(reactorName)
  16. local battery = peripheral.wrap(batteryName)
  17. local monitor = peripheral.find("monitor")
  18.  
  19. -- Wraps the logic to get battery percentage (0.0 to 1.0)
  20. function GetNormalizedBatteryLevel()
  21.     if battery == nil then
  22.         battery = peripheral.wrap(batteryName)
  23.     end
  24.  
  25.     if battery ~= nil then
  26.         local currentEnergy = battery.getEnergy()
  27.         local maxEnergy = battery.getEnergyCapacity()
  28.         return currentEnergy / maxEnergy
  29.     end
  30.  
  31.     return 0
  32. end
  33.  
  34. function DrawCenteredLine(mon, y, text, textColor, bgColor)
  35.     local w, h = mon.getSize()
  36.     local x = math.floor((w - string.len(text)) / 2) + 1
  37.     mon.setCursorPos(x, y)
  38.     mon.setTextColor(textColor)
  39.     mon.setBackgroundColor(bgColor)
  40.     mon.write(text)
  41. end
  42.  
  43. function UpdateDisplay(isReactorActive, batteryPercent)
  44.     if monitor == nil then
  45.         monitor = peripheral.find("monitor")
  46.     end
  47.  
  48.     if monitor ~= nil then
  49.         local w, h = monitor.getSize()
  50.        
  51.         -- Layout Constants
  52.         local headerHeight = 1
  53.         local footerHeight = 1
  54.         local leftAxisWidth = 4
  55.        
  56.         -- Graph Area Calculation
  57.         local graphTopY = headerHeight + 1
  58.         local graphBottomY = h - footerHeight - 1
  59.         local graphHeight = graphBottomY - graphTopY + 1
  60.         local graphWidth = w - leftAxisWidth
  61.  
  62.         -- 1. Reset Screen
  63.         monitor.setBackgroundColor(colors.black)
  64.         monitor.clear()
  65.  
  66.         -- 2. Header
  67.         DrawCenteredLine(monitor, 1, "System Status", colors.white, colors.black)
  68.  
  69.         -- 3. Draw Left Axis (Percentages)
  70.         for y = graphTopY, graphBottomY do
  71.             local pixelRelative = graphBottomY - y
  72.             local safeHeight = math.max(1, graphHeight - 1)
  73.             local pct = math.floor((pixelRelative / safeHeight) * 100)
  74.            
  75.             monitor.setCursorPos(1, y)
  76.             monitor.setTextColor(colors.lightGray)
  77.             monitor.setBackgroundColor(colors.black)
  78.             monitor.write(string.format("%3d", pct) .. "%")
  79.         end
  80.  
  81.         -- 4. Calculate Threshold Y Positions
  82.         local onLineRelative = math.floor(turnOnThreshold * (graphHeight - 1))
  83.         local offLineRelative = math.floor(turnOffThreshold * (graphHeight - 1))
  84.        
  85.         local onLineY = graphBottomY - onLineRelative
  86.         local offLineY = graphBottomY - offLineRelative
  87.  
  88.         -- Pre-calculate Labels for collision detection
  89.         local onLabelStr = math.floor(turnOnThreshold * 100) .. "%"
  90.         local offLabelStr = math.floor(turnOffThreshold * 100) .. "%"
  91.        
  92.         local onLabelX = w - string.len(onLabelStr) + 1
  93.         local offLabelX = w - string.len(offLabelStr) + 1
  94.  
  95.         -- 5. Draw Threshold Lines & Right Labels (Base Layer)
  96.         local function drawThreshold(y, labelStr, labelX)
  97.             -- Draw Line
  98.             monitor.setCursorPos(leftAxisWidth + 1, y)
  99.             monitor.setTextColor(colors.red)
  100.             monitor.setBackgroundColor(colors.black)
  101.             monitor.write(string.rep("-", graphWidth))
  102.            
  103.             -- Draw Right Label
  104.             monitor.setCursorPos(labelX, y)
  105.             monitor.setTextColor(colors.white)
  106.             monitor.setBackgroundColor(colors.red)
  107.             monitor.write(labelStr)
  108.         end
  109.  
  110.         drawThreshold(offLineY, offLabelStr, offLabelX)
  111.         drawThreshold(onLineY, onLabelStr, onLabelX)
  112.  
  113.         -- 6. Draw Graph (Battery Level)
  114.         for i = 1, #history do
  115.             local colX = w - (#history - i)
  116.            
  117.             if colX > leftAxisWidth then
  118.                 local val = history[i]
  119.                 local pixelRelative = math.floor(val * (graphHeight - 1))
  120.                 local pixelY = graphBottomY - pixelRelative
  121.  
  122.                 -- Determine Colors
  123.                 local isCurrent = (i == #history)
  124.                 local barColor = colors.green
  125.                 if isCurrent then
  126.                     barColor = colors.lime
  127.                 end
  128.  
  129.                 -- Default Character
  130.                 local char = " "
  131.                 local fgColor = colors.white
  132.                 local bgColor = barColor
  133.  
  134.                 -- COLLISION LOGIC
  135.                 if pixelY == onLineY then
  136.                     if colX >= onLabelX then
  137.                         -- Collision with "30%" Text
  138.                         -- Extract the specific character we are overwriting
  139.                         local charIndex = colX - onLabelX + 1
  140.                         char = string.sub(onLabelStr, charIndex, charIndex)
  141.                         fgColor = colors.white -- Keep text white
  142.                         -- bgColor is already green/lime, effectively "highlighting" the text
  143.                     else
  144.                         -- Collision with "---" Line
  145.                         char = "-"
  146.                         fgColor = colors.red
  147.                     end
  148.                 elseif pixelY == offLineY then
  149.                     if colX >= offLabelX then
  150.                         -- Collision with "90%" Text
  151.                         local charIndex = colX - offLabelX + 1
  152.                         char = string.sub(offLabelStr, charIndex, charIndex)
  153.                         fgColor = colors.white
  154.                     else
  155.                         -- Collision with "---" Line
  156.                         char = "-"
  157.                         fgColor = colors.red
  158.                     end
  159.                 end
  160.  
  161.                 monitor.setCursorPos(colX, pixelY)
  162.                 monitor.setBackgroundColor(bgColor)
  163.                 monitor.setTextColor(fgColor)
  164.                 monitor.write(char)
  165.             end
  166.         end
  167.  
  168.         -- 7. Footer Layout
  169.         local spacerWidth = 1
  170.        
  171.         -- Reactor Status
  172.         local statusText = " OFF "
  173.         local statusBg = colors.red
  174.         if isReactorActive then
  175.             statusText = " ON "
  176.             statusBg = colors.green
  177.         end
  178.         local statusWidth = string.len(statusText)
  179.  
  180.         -- Battery Bar Width
  181.         local batteryBarWidth = w - spacerWidth - statusWidth
  182.         local filledAmount = math.floor(batteryPercent * batteryBarWidth)
  183.  
  184.         -- Draw Battery Bar Backgrounds
  185.         monitor.setCursorPos(1, h)
  186.         monitor.setBackgroundColor(colors.cyan)
  187.         monitor.write(string.rep(" ", filledAmount))
  188.  
  189.         if filledAmount < batteryBarWidth then
  190.             monitor.setBackgroundColor(colors.gray)
  191.             monitor.write(string.rep(" ", batteryBarWidth - filledAmount))
  192.         end
  193.  
  194.         -- Draw Battery Text OVER the bar
  195.         local batStr = math.floor(batteryPercent * 100) .. "%"
  196.         local textStart = math.floor((batteryBarWidth - string.len(batStr)) / 2) + 1
  197.        
  198.         monitor.setTextColor(colors.white)
  199.        
  200.         for i = 1, string.len(batStr) do
  201.             local char = string.sub(batStr, i, i)
  202.             local screenX = textStart + i - 1
  203.            
  204.             if screenX <= filledAmount then
  205.                 monitor.setBackgroundColor(colors.cyan)
  206.             else
  207.                 monitor.setBackgroundColor(colors.gray)
  208.             end
  209.            
  210.             monitor.setCursorPos(screenX, h)
  211.             monitor.write(char)
  212.         end
  213.  
  214.         -- Draw Spacer
  215.         monitor.setCursorPos(batteryBarWidth + 1, h)
  216.         monitor.setBackgroundColor(colors.lightGray)
  217.         monitor.write(" ")
  218.  
  219.         -- Draw Reactor Status
  220.         monitor.setCursorPos(batteryBarWidth + spacerWidth + 1, h)
  221.         monitor.setBackgroundColor(statusBg)
  222.         monitor.setTextColor(colors.white)
  223.         monitor.write(statusText)
  224.     end
  225. end
  226.  
  227. function Main()
  228.     while true do
  229.         local batteryPercent = GetNormalizedBatteryLevel()
  230.         local isActive = reactor.getActive()
  231.  
  232.         -- Hysteresis Loop
  233.         if batteryPercent <= turnOnThreshold then
  234.             if not isActive then
  235.                 reactor.setActive(true)
  236.                 isActive = true
  237.             end
  238.         end
  239.  
  240.         if batteryPercent >= turnOffThreshold then
  241.             if isActive then
  242.                 reactor.setActive(false)
  243.                 isActive = false
  244.             end
  245.         end
  246.  
  247.         -- Update Graph History
  248.         if monitor ~= nil then
  249.             local w, h = monitor.getSize()
  250.             maxHistoryItems = w - 4
  251.         else
  252.             maxHistoryItems = 50
  253.         end
  254.  
  255.         table.insert(history, batteryPercent)
  256.        
  257.         while #history > maxHistoryItems do
  258.             table.remove(history, 1)
  259.         end
  260.  
  261.         UpdateDisplay(isActive, batteryPercent)
  262.        
  263.         -- Use the configured timescale
  264.         os.sleep(graphScaleSeconds)
  265.     end
  266. end
  267.  
  268. Main()
Advertisement
Add Comment
Please, Sign In to add comment