Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --================
- --=INITIALIZATION=
- --================
- print("Initializing configurations")
- os.loadAPI("utils")
- local mon = utils.mon(12)
- local glass = peripheral.wrap("openperipheral_bridge_0")
- local glassX, glassY = 1, 20
- local bankStructs = {
- ["Main power"] = {bank = utils.cb(2),
- showGlass = true,
- maxInRF = 150000,
- maxOutRF = 150000},
- ["Base power"] = {bank = utils.cb(3),
- showGlass = false,
- maxInRF = 5000,
- maxOutRF = 25000},
- ["ME power"] = {bank = utils.cb(4),
- showGlass = false,
- maxInRF = 2500,
- maxOutRF = 25000},
- ["General power"] = {bank = utils.cb(1),
- showGlass = false,
- maxInRF = 15000,
- maxOutRF = 25000},
- ["IRF power"] = {bank = utils.cb(5),
- showGlass = false,
- maxInRF = 30000,
- maxOutRF = 50000},
- ["MekBase power"] = {bank = utils.cb(6),
- showGlass = false,
- maxInRF = 15000,
- maxOutRF = 25000}
- }
- local lastEvent = {event = nil, side = nil, x = nil, y = nil}
- local hasNewEvent = false
- local debugMode = true
- print("Initializing capacitor banks")
- for k, v in pairs(bankStructs) do
- if v ~= nil and v.bank ~= nil then
- function v.isInputEnabled() return (v.bank.getMaxInput() ~= 0) end
- function v.isOutputEnabled() return (v.bank.getMaxOutput() ~= 0) end
- function v.toggleOutput()
- local o = 0
- if not v.isOutputEnabled() then o = v.maxOutRF end
- v.bank.setMaxOutput(o)
- end
- function v.toggleInput()
- local o = 0
- if not v.isInputEnabled() then o = v.maxInRF end
- v.bank.setMaxInput(o)
- end
- end
- end
- --=======================
- --=GRAPHICAL AND UI CODE=
- --=======================
- print("Initializing graphical code")
- --Initialize buttons
- local ctrlButtons = {
- outputCtrl = {
- activeText = "Disable output",
- inactiveText = "Enable output",
- onPress = function(bankStruct) bankStruct.toggleOutput() end,
- isPressed = function(bankStruct) return bankStruct.isOutputEnabled() end,
- getColor = function(bankStruct)
- if bankStruct.isOutputEnabled() then return colors.red else return colors.green end
- end,
- getBackColor = function(bankStruct) return colors.gray end
- },
- inputCtrl = {
- activeText = "Disable input",
- inactiveText = "Enable input",
- onPress = function(bankStruct) bankStruct.toggleInput() end,
- isPressed = function(bankStruct) return bankStruct.isInputEnabled() end,
- getColor = function(bankStruct)
- if bankStruct.isInputEnabled() then return colors.red else return colors.green end
- end,
- getBackColor = function(bankStruct) return colors.gray end
- }
- }
- --Write the fill percentage
- function writeFillPerc(mon, perc)
- local x, y = mon.getCursorPos()
- local percStr = string.format("%i", perc)
- local percCol = colors.red
- if perc >= 95 then percCol = colors.cyan
- elseif perc >= 75 then percCol = colors.green
- elseif perc >= 60 then percCol = colors.yellow
- elseif perc >= 30 then percCol = colors.orange end
- utils.monWrt(mon, percStr .. "%", percCol)
- mon.setCursorPos(x + 5, y)
- end
- --Write buttons and check thier states
- function doButtons(mon, bankStruct, buttons, event)
- for k, btn in pairs(buttons) do
- local text = btn.inactiveText
- local xa, ya = mon.getCursorPos()
- if btn.isPressed(bankStruct) then text = btn.activeText end
- local oldCol, oldBCol = mon.getTextColor(), mon.getBackgroundColor()
- mon.setBackgroundColor(btn.getBackColor(bankStruct))
- utils.monWrt(mon, text, btn.getColor(bankStruct))
- local xb, yb = mon.getCursorPos()
- if event ~= nil then
- local touched = utils.chkBds(event.x, event.y, xa, ya, xb, yb)
- if touched then btn.onPress(bankStruct) end
- end
- mon.setTextColor(oldCol)
- mon.setBackgroundColor(oldBCol)
- mon.write(" ")
- end
- end
- --Draws all the details about a capacitor bank
- --to the specified monitor
- function drawBank(mon, name, bankStruct, event)
- --Variable initialization
- local bank = bankStruct.bank
- local rfI, rfO = bank.getAverageInputPerTick(), bank.getAverageOutputPerTick()
- local rfIstr, rfOstr = utils.getPwrStr(rfI, true, 3), utils.getPwrStr(rfO, true, 3)
- local rfDiffStr = utils.getPwrStr(rfI - rfO, true)
- local rfC, rfT = bank.getEnergyStored(), bank.getMaxEnergyStored()
- local perc = utils.round((rfC / rfT) * 100)
- --Write the name (centered)
- mon.setTextColor(colors.purple)
- utils.printCentLn(mon, name)
- mon.setBackgroundColor(colors.black)
- utils.monCR(mon)
- --Write statistics
- writeFillPerc(mon, perc)
- utils.monWrtFxdLen(mon, 16, "In: " .. rfIstr, colors.green)
- utils.monWrtFxdLen(mon, 16, "Out: " .. rfOstr, colors.red)
- --Write profit/deficit
- local sym, col = rfDiffStr, colors.red
- if rfI > rfO then
- sym = "+" .. rfDiffStr
- col = colors.green
- elseif rfI == rfO then
- sym = "="
- col = colors.blue
- if(perc >= 100) then
- sym = "F"
- col = colors.cyan
- end
- end
- utils.monWrt(mon, " " .. sym, col)
- utils.monNLCR(mon)
- end
- function drawGlass(g, bankStruct, bankName, x, y)
- local bank = bankStruct.bank
- local rfI, rfO = bank.getAverageInputPerTick(), bank.getAverageOutputPerTick()
- local rfIstr, rfOstr = utils.getPwrStr(rfI, true, 3), utils.getPwrStr(rfO, true, 3)
- local rfDiffStr = utils.getPwrStr(rfI - rfO, true)
- local rfC, rfT = bank.getEnergyStored(), bank.getMaxEnergyStored()
- local perc = utils.round((rfC / rfT) * 100)
- g.clear()
- local tw = 8
- x, y = x + 1, y + 1
- g.addText(x, y, bankName, 0xFF00FF)
- y = y + tw + 1
- local col = 0xFF0000
- if perc >= 95 then col = 0x00AAFF
- elseif perc >= 75 then col = 0x00FF00
- elseif perc >= 60 then col = 0xFFFF00
- elseif perc >= 30 then col = 0xFFBB00 end
- g.addText(x, y, "Fill: " .. perc .. "%", col)
- y = y + tw + 1
- g.addText(x, y, "In: " .. rfIstr, 0x00FF00)
- y = y + tw + 1
- g.addText(x, y, "Out: " .. rfOstr, 0xFF0000)
- g.sync()
- end
- --======================
- --=MAIN PROCESSING CODE=
- --======================
- --Debug method
- function debug(msg)
- if debugMode then print(msg) end
- end
- --Main loop
- print("Initializing main loop and input loop")
- function mainLoop()
- print("Main loop started")
- while true do
- utils.rstMon(mon)
- utils.printCentLn(mon, "Power distribution system")
- local e = nil
- if hasNewEvent then e = lastEvent end
- for bankName, bankStruct in pairs(bankStructs) do
- if(bankStruct ~= nil) then
- drawBank(mon, bankName, bankStruct, e)
- doButtons(mon, bankStruct, ctrlButtons, e)
- utils.monNLCR(mon)
- if glass ~= nil and bankStruct.showGlass then drawGlass(glass, bankStruct, bankName, glassX, glassY) end
- else
- print("Severe error, refer to monitor!")
- utils.monWrt(mon, "ERROR: Bank " .. bankName .. " is nil!", colors.red)
- utils.monNLCR(mon)
- end
- end
- hasNewEvent = false
- sleep(0.1)
- end
- print("Main loop break")
- end
- function inputLoop()
- print("Input loop started")
- while true do
- local e, s, x, y = os.pullEvent("monitor_touch")
- lastEvent.event = e
- lastEvent.side = s
- lastEvent.x = x
- lastEvent.y = y
- hasNewEvent = true
- debug("Registered touch event!")
- end
- print("Input loop exited")
- end
- print("Starting main loop, and input loop!")
- parallel.waitForAny(inputLoop, mainLoop)
- print("Program terminated")
Add Comment
Please, Sign In to add comment