Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local reactorName = "BigReactors-Reactor_0"
- local batteryName = "enderio:vibrant_capacitor_bank_0"
- -- Graph Settings
- local graphScaleSeconds = 1.0 -- How many seconds each pixel/update represents
- local turnOnThreshold = 0.30 -- 30%
- local turnOffThreshold = 0.90 -- 90%
- -- Graph Data
- local history = {}
- local maxHistoryItems = 0
- -- Peripherals
- local reactor = peripheral.wrap(reactorName)
- local battery = peripheral.wrap(batteryName)
- local monitor = peripheral.find("monitor")
- -- Wraps the logic to get battery percentage (0.0 to 1.0)
- function GetNormalizedBatteryLevel()
- if battery == nil then
- battery = peripheral.wrap(batteryName)
- end
- if battery ~= nil then
- local currentEnergy = battery.getEnergy()
- local maxEnergy = battery.getEnergyCapacity()
- return currentEnergy / maxEnergy
- end
- return 0
- end
- function DrawCenteredLine(mon, y, text, textColor, bgColor)
- local w, h = mon.getSize()
- local x = math.floor((w - string.len(text)) / 2) + 1
- mon.setCursorPos(x, y)
- mon.setTextColor(textColor)
- mon.setBackgroundColor(bgColor)
- mon.write(text)
- end
- function UpdateDisplay(isReactorActive, batteryPercent)
- if monitor == nil then
- monitor = peripheral.find("monitor")
- end
- if monitor ~= nil then
- local w, h = monitor.getSize()
- -- Layout Constants
- local headerHeight = 1
- local footerHeight = 1
- local leftAxisWidth = 4
- -- Graph Area Calculation
- local graphTopY = headerHeight + 1
- local graphBottomY = h - footerHeight - 1
- local graphHeight = graphBottomY - graphTopY + 1
- local graphWidth = w - leftAxisWidth
- -- 1. Reset Screen
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- 2. Header
- DrawCenteredLine(monitor, 1, "System Status", colors.white, colors.black)
- -- 3. Draw Left Axis (Percentages)
- for y = graphTopY, graphBottomY do
- local pixelRelative = graphBottomY - y
- local safeHeight = math.max(1, graphHeight - 1)
- local pct = math.floor((pixelRelative / safeHeight) * 100)
- monitor.setCursorPos(1, y)
- monitor.setTextColor(colors.lightGray)
- monitor.setBackgroundColor(colors.black)
- monitor.write(string.format("%3d", pct) .. "%")
- end
- -- 4. Calculate Threshold Y Positions
- local onLineRelative = math.floor(turnOnThreshold * (graphHeight - 1))
- local offLineRelative = math.floor(turnOffThreshold * (graphHeight - 1))
- local onLineY = graphBottomY - onLineRelative
- local offLineY = graphBottomY - offLineRelative
- -- Pre-calculate Labels for collision detection
- local onLabelStr = math.floor(turnOnThreshold * 100) .. "%"
- local offLabelStr = math.floor(turnOffThreshold * 100) .. "%"
- local onLabelX = w - string.len(onLabelStr) + 1
- local offLabelX = w - string.len(offLabelStr) + 1
- -- 5. Draw Threshold Lines & Right Labels (Base Layer)
- local function drawThreshold(y, labelStr, labelX)
- -- Draw Line
- monitor.setCursorPos(leftAxisWidth + 1, y)
- monitor.setTextColor(colors.red)
- monitor.setBackgroundColor(colors.black)
- monitor.write(string.rep("-", graphWidth))
- -- Draw Right Label
- monitor.setCursorPos(labelX, y)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.red)
- monitor.write(labelStr)
- end
- drawThreshold(offLineY, offLabelStr, offLabelX)
- drawThreshold(onLineY, onLabelStr, onLabelX)
- -- 6. Draw Graph (Battery Level)
- for i = 1, #history do
- local colX = w - (#history - i)
- if colX > leftAxisWidth then
- local val = history[i]
- local pixelRelative = math.floor(val * (graphHeight - 1))
- local pixelY = graphBottomY - pixelRelative
- -- Determine Colors
- local isCurrent = (i == #history)
- local barColor = colors.green
- if isCurrent then
- barColor = colors.lime
- end
- -- Default Character
- local char = " "
- local fgColor = colors.white
- local bgColor = barColor
- -- COLLISION LOGIC
- if pixelY == onLineY then
- if colX >= onLabelX then
- -- Collision with "30%" Text
- -- Extract the specific character we are overwriting
- local charIndex = colX - onLabelX + 1
- char = string.sub(onLabelStr, charIndex, charIndex)
- fgColor = colors.white -- Keep text white
- -- bgColor is already green/lime, effectively "highlighting" the text
- else
- -- Collision with "---" Line
- char = "-"
- fgColor = colors.red
- end
- elseif pixelY == offLineY then
- if colX >= offLabelX then
- -- Collision with "90%" Text
- local charIndex = colX - offLabelX + 1
- char = string.sub(offLabelStr, charIndex, charIndex)
- fgColor = colors.white
- else
- -- Collision with "---" Line
- char = "-"
- fgColor = colors.red
- end
- end
- monitor.setCursorPos(colX, pixelY)
- monitor.setBackgroundColor(bgColor)
- monitor.setTextColor(fgColor)
- monitor.write(char)
- end
- end
- -- 7. Footer Layout
- local spacerWidth = 1
- -- Reactor Status
- local statusText = " OFF "
- local statusBg = colors.red
- if isReactorActive then
- statusText = " ON "
- statusBg = colors.green
- end
- local statusWidth = string.len(statusText)
- -- Battery Bar Width
- local batteryBarWidth = w - spacerWidth - statusWidth
- local filledAmount = math.floor(batteryPercent * batteryBarWidth)
- -- Draw Battery Bar Backgrounds
- monitor.setCursorPos(1, h)
- monitor.setBackgroundColor(colors.cyan)
- monitor.write(string.rep(" ", filledAmount))
- if filledAmount < batteryBarWidth then
- monitor.setBackgroundColor(colors.gray)
- monitor.write(string.rep(" ", batteryBarWidth - filledAmount))
- end
- -- Draw Battery Text OVER the bar
- local batStr = math.floor(batteryPercent * 100) .. "%"
- local textStart = math.floor((batteryBarWidth - string.len(batStr)) / 2) + 1
- monitor.setTextColor(colors.white)
- for i = 1, string.len(batStr) do
- local char = string.sub(batStr, i, i)
- local screenX = textStart + i - 1
- if screenX <= filledAmount then
- monitor.setBackgroundColor(colors.cyan)
- else
- monitor.setBackgroundColor(colors.gray)
- end
- monitor.setCursorPos(screenX, h)
- monitor.write(char)
- end
- -- Draw Spacer
- monitor.setCursorPos(batteryBarWidth + 1, h)
- monitor.setBackgroundColor(colors.lightGray)
- monitor.write(" ")
- -- Draw Reactor Status
- monitor.setCursorPos(batteryBarWidth + spacerWidth + 1, h)
- monitor.setBackgroundColor(statusBg)
- monitor.setTextColor(colors.white)
- monitor.write(statusText)
- end
- end
- function Main()
- while true do
- local batteryPercent = GetNormalizedBatteryLevel()
- local isActive = reactor.getActive()
- -- Hysteresis Loop
- if batteryPercent <= turnOnThreshold then
- if not isActive then
- reactor.setActive(true)
- isActive = true
- end
- end
- if batteryPercent >= turnOffThreshold then
- if isActive then
- reactor.setActive(false)
- isActive = false
- end
- end
- -- Update Graph History
- if monitor ~= nil then
- local w, h = monitor.getSize()
- maxHistoryItems = w - 4
- else
- maxHistoryItems = 50
- end
- table.insert(history, batteryPercent)
- while #history > maxHistoryItems do
- table.remove(history, 1)
- end
- UpdateDisplay(isActive, batteryPercent)
- -- Use the configured timescale
- os.sleep(graphScaleSeconds)
- end
- end
- Main()
Advertisement
Add Comment
Please, Sign In to add comment