Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("settings.lua")
- os.loadAPI("graphics.lua")
- os.loadAPI("sliders.lua")
- os.loadAPI("buttons.lua")
- os.loadAPI("notifications.lua")
- os.loadAPI("redstone.lua")
- controlBtn = nil
- autoPilotBtn = nil
- redstoneOn = false
- oldPowerLevel = 0
- energyPercentage = 0
- maxEnergy = 0
- totalEnergy = 0
- energyNeeded = 0
- cycles = 0
- local powerCells = {}
- found = false
- for _, name in pairs(peripheral.getNames()) do
- if peripheral.getType(name) == "peripheralProxy:mekanismMachine" then --# change "monitor" to your peripheral type.
- periph = peripheral.wrap(name)
- table.insert(powerCells, periph)
- found = true
- end
- end
- if found == false then
- error("There must be at least one PeripheralProxy attached to a Mekanism Energy Cube hooked up to this computer via wired modem!")
- end
- function getPowerCellsInfo()
- maxEnergy = 0
- totalEnergy = 0
- energyNeeded = 0
- energyPercentage = 0
- for k,v in pairs(powerCells) do
- if v ~= nill then
- maxEnergy = maxEnergy + v.getTotalMaxEnergy()
- totalEnergy = totalEnergy + v.getTotalEnergy()
- energyNeeded = energyNeeded + v.getTotalEnergyNeeded()
- end
- end
- energyPercentage = (totalEnergy / maxEnergy) * 100
- end
- function setupPowerBars()
- w, h = term.getSize()
- sliders.createSlider("powerSlider", 2, 8, w-2, 1, colors.lime, colors.red)
- end
- function activateRedstone(txt, bkgColor, txtColor)
- notifications.addNotification("generator_switch", 4, txt, bkgColor, txtColor)
- redstone.toggleRedstone(settings.redstonePowerSide, true)
- redstoneOn = true
- controlBtn.text = "Stop"
- controlBtn.bkgColor = colors.red
- end
- function deactivateRedstone(txt, bkgColor, txtColor)
- notifications.addNotification("generator_switch", 4, txt, bkgColor, txtColor)
- redstone.toggleRedstone(settings.redstonePowerSide, false)
- redstoneOn = false
- controlBtn.text = "Start"
- controlBtn.bkgColor = colors.lime
- end
- function toggleMachine()
- checked = autoPilotBtn.checked
- if checked == true and redstoneOn == false then
- notifications.addNotification("generator_switch", 4, "Cannot activate, auto pilot enabled!", colors.red, colors.black)
- return
- elseif checked == true and redstoneOn == true then
- notifications.addNotification("generator_switch", 4, "Cannot deactivate, auto pilot enabled!", colors.red, colors.black)
- return
- elseif checked == false then
- if energyNeeded == 0 then
- notifications.addNotification("generator_switch", 4, "Cannot activate, power is full!", colors.red, colors.black)
- return
- elseif redstoneOn == false then
- activateRedstone("Power gen activated", colors.lime, colors.white)
- return
- elseif redstoneOn == true then
- deactivateRedstone("Power gen deactivated", colors.red, colors.white)
- return
- end
- end
- end
- function autoPilot()
- checked = autoPilotBtn.checked
- if checked == true then
- notifications.addNotification("autopilot", 4, "Auto pilot enabled", colors.lime, colors.white)
- if energyPercentage < settings.lowPowerThreshold then
- activateRedstone("Power gen auto activated", colors.lime, colors.white)
- else
- deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
- end
- else
- notifications.addNotification("autopilot", 4, "Auto pilot disabled", colors.red, colors.white)
- deactivateRedstone("Auto pilot disabled", colors.red, colors.white)
- end
- end
- function setupControlBtns()
- controlBtn = buttons.createBtn("Start",colors.lime,3,7, w - 9, 2)
- controlBtn.trigger = function()
- toggleMachine()
- end
- buttons.addButton(controlBtn)
- -- Autopilot checkbox/ button
- autoPilotBtn = buttons.createCheckbox("Auto Pilot",w - 22,3,settings.autoPilot,settings.backgroundColor)
- autoPilotBtn.trigger = function()
- autoPilot()
- end
- buttons.addButton(autoPilotBtn)
- if settings.autoPilot == true then
- autoPilot()
- end
- end
- function displayPowerInfo()
- local w, h = term.getSize()
- powerPercentage = energyPercentage
- powerLevel = totalEnergy
- maxPowerLevel = maxEnergy
- if powerLevel ~= oldPowerLevel then
- inputPower = (powerLevel - oldPowerLevel) / 20
- else
- inputPower = 0
- end
- oldPowerLevel = powerLevel
- term.setCursorPos(2,6)
- term.setBackgroundColor( settings.backgroundColor )
- term.clearLine()
- notification = graphics.label:new()
- notification.text = "Power: " .. powerLevel .. "/" .. maxPowerLevel
- lenght = string.len(notification.text)
- notification.x = (w/2) - (lenght/2) + 1
- notification.y = 6
- notification.bkgColor = settings.backgroundColor
- notification.draw(notification)
- term.setCursorPos(2,7)
- term.setBackgroundColor( settings.backgroundColor )
- term.clearLine()
- notification = graphics.label:new()
- notification.text = "I/O: " .. inputPower .. " rf/t"
- lenght = string.len(notification.text)
- notification.x = (w/2) - (lenght/2) + 1
- notification.y = 7
- notification.bkgColor = settings.backgroundColor
- notification.draw(notification)
- sliders.updateSlider("powerSlider", powerPercentage)
- sliders.draw()
- term.setCursorPos(2,h - 1)
- term.setBackgroundColor( settings.backgroundColor )
- term.clearLine()
- notification = graphics.label:new()
- notification.text = "Cycle Count: " .. cycles
- lenght = string.len(notification.text)
- notification.x = (w/2) - (lenght/2) + 1
- notification.y = h - 1
- notification.bkgColor = settings.backgroundColor
- notification.draw(notification)
- end
- function initPower()
- getPowerCellsInfo()
- setupPowerBars()
- setupControlBtns()
- end
- function update()
- getPowerCellsInfo()
- displayPowerInfo()
- powerPercentage = energyPercentage
- -- manual update
- if redstoneOn == true and autoPilotBtn.checked == false then
- if powerPercentage >= 100 then
- deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
- cycles = cycles + 1
- end
- -- autopilot update
- elseif redstoneOn == true and autoPilotBtn.checked == true then
- if powerPercentage >= settings.highPowerThreshold then
- deactivateRedstone("Power gen auto deactivated", colors.lime, colors.white)
- cycles = cycles + 1
- end
- elseif redstoneOn == false and autoPilotBtn.checked == true then
- if powerPercentage < settings.lowPowerThreshold then
- activateRedstone("Power gen auto activated", colors.lime, colors.white)
- end
- end
- end
Add Comment
Please, Sign In to add comment