Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NRG
- -- Energy control system for Big Reactors / Mekanism
- -- in 1.7.10 Pack
- -- version 0.1
- -- Alex Shaffer : alex@nosuch.org
- local nrg = {}
- function nrg.gridManageStandby()
- nrg.reactorShouldBeOff()
- nrg.turbineCoilsShouldBeOff()
- if nrg.energyPercentCapacity <= nrg.config.spinUpPercent then
- nrg.status.current = nrg.const.gridStatus.activate
- end
- end
- function nrg.gridManageActivate()
- nrg.reactorShouldBeOn()
- nrg.turbineCoilsShouldBeOff()
- nrg.status.current = nrg.const.gridStatus.spinUp
- end
- function nrg.gridManageSpinUp()
- nrg.reactorShouldBeOn()
- if nrg.energyPercentCapacity >= nrg.config.spinDownPercent then
- nrg.status.current = nrg.const.gridStatus.spinDown
- else
- if nrg.turbinesShouldBeRPM(nrg.const.coilOnThreshold, true) then
- nrg.status.current = nrg.const.gridStatus.charging
- end
- end
- end
- function nrg.gridManageCharging()
- nrg.reactorShouldBeOn()
- nrg.turbinesShouldBeRPM(nrg.const.coilOffThreshold, true)
- if nrg.energyPercentCapacity >= nrg.config.spinDownPercent then
- nrg.status.current = nrg.const.gridStatus.spinDown
- end
- end
- function nrg.gridManageSpinDown()
- nrg.reactorShouldBeOn()
- nrg.turbineCoilsShouldBeOff()
- if nrg.turbinesShouldBeRPM(nrg.const.coilSpinUpThreshhold) then
- nrg.status.current = nrg.const.gridStatus.deactivate
- end
- end
- function nrg.gridManageDeactivate()
- nrg.reactorShouldBeOff()
- nrg.turbineCoilsShouldBeOff()
- end
- function nrg.constInit()
- nrg.const = {}
- nrg.const.BRmaxEnergyStorage = 10000000
- nrg.const.gridStatus = {}
- nrg.const.gridStatus.standby = { name = "Standing by", action = nrg.gridManageStandby }
- nrg.const.gridStatus.activate = { name = "Activating", action = nrg.gridManageActivate }
- nrg.const.gridStatus.spinUp = { name = "Spinning Up", action = nrg.gridManageSpinUp }
- nrg.const.gridStatus.charging = { name = "Charging", action = nrg.gridManageCharging }
- nrg.const.gridStatus.spinDown = { name = "Spinning Down", action = nrg.gridManageSpinDown }
- nrg.const.gridStatus.deactivate = { name = "Deactivating", action = nrg.gridManageDeactivate }
- nrg.const.coilOnThreshold = 1775
- nrg.const.coilOffThreshold = 1750
- nrg.const.coilSpinUpThreshhold = 1800
- nrg.const.BRreactor = "BigReactors-Reactor"
- nrg.const.BRturbine = "BigReactors-Turbine"
- nrg.const.MKcube = "mekanism_machine"
- end
- function nrg.configInit(options)
- nrg.config = {}
- nrg.status = {}
- if options == nil then options = {} end
- nrg.config.enabled = true
- nrg.config.CC = (_CC_VERSION ~= nil)
- nrg.status.gridValid = false
- nrg.config.spinUpPercent = options.spinUpPercent or 5
- nrg.config.spinDownPercent = options.spinDownPercent or 95
- nrg.status.current = nrg.const.gridStatus.standby
- nrg.status.lastStatus = nil
- end
- function nrg.bridgingInit()
- nrg.fn = {}
- if nrg.config.CC then
- nrg.fn.findReactors = function() return {peripheral.find(nrg.const.BRreactor)} end
- nrg.fn.findTurbines = function() return {peripheral.find(nrg.const.BRturbine)} end
- nrg.fn.findStorage = function() return {peripheral.find(nrg.const.MKcube)} end
- nrg.fn.isEnergyCube = function(e) return (e.getMaxEnergy ~= nil and e.getMaxEnergy() > 0) end
- nrg.fn.getReactorActive = function() return nrg.reactor.getActive() end
- nrg.fn.setReactorActive = function(a) nrg.reactor.setActive(a) end
- else
- nrg.fn.testData = {}
- nrg.fn.testData.reactorActive = false
- nrg.fn.findReactors = function() return {} end
- nrg.fn.findTurbines = function() return {} end
- nrg.fn.findStorage = function() return {} end
- nrg.fn.isEnergyCube = function(e) return (e.getMaxEnergy ~=nil and e.getMaxEnergy() > 0 ) end
- nrg.fn.getReactorActive = function() return nrg.fn.testData.reactorActive end
- nrg.fn.setReactorActive = function(a) nrg.fn.testData.reactorActive = a end
- end
- end
- -- Finds power equipment hooked up to the network
- function nrg.gridInit()
- nrg.energyStorageMax = 0
- nrg.energyStorageCurrent = 0
- nrg.reactor = nil
- nrg.reactors = gridInitReactorsFind()
- nrg.turbines = gridInitTurbinesFind()
- nrg.storage = gridInitStorageFind(nrg.turbines)
- nrg.config.gridValid = ((#nrg.reactors == 1) and (#nrg.turbines > 0))
- if nrg.config.gridValid then
- nrg.reactor = nrg.reactors[1]
- nrg.gridUpdate()
- end
- print("Grid summary:")
- print("Reactors: "..#nrg.reactors)
- print("Turbines: "..#nrg.turbines)
- print("Storage Devices: "..#nrg.storage)
- print("Storage Capacity: "..math.floor(nrg.energyStorageMax/1000).." krf")
- print("Current Energy: "..math.floor(nrg.energyStorageCurrent/1000).." krf")
- print("Valid: "..tostring(nrg.config.gridValid))
- end
- function gridInitReactorsFind()
- local reactors = nrg.fn.findReactors()
- print("Reactors found: "..#reactors)
- return reactors
- end
- function gridInitTurbinesFind()
- local turbines = nrg.fn.findTurbines()
- print("Turbines found: "..#turbines)
- return turbines
- end
- function gridInitStorageFind(turbines)
- local storage = nrg.fn.findStorage()
- print("Additional storage found: "..#storage)
- for k,turbine in pairs(turbines) do
- -- BR devices don't have a getMaxEnergy function, so we add one, based on a constant
- if turbine.getMaxEnergy == nil then
- turbine.getMaxEnergy = function() return nrg.const.BRmaxEnergyStorage end
- end
- if turbine.getEnergy == nil then
- turbine.getEnergy = turbine.getEnergyStored
- end
- storage[#storage+1]=turbine
- end
- nrg.energyStorageMax = 0
- for k,storageDevice in pairs(storage) do
- nrg.energyStorageMax = nrg.energyStorageMax + storageDevice.getMaxEnergy()
- end
- return storage
- end
- -- Loads data about the current state of the power grid
- function nrg.gridUpdate()
- local energyStored = 0
- for k, storageDevice in pairs(nrg.storage) do
- energyStored = energyStored + storageDevice.getEnergy()
- end
- nrg.energyStorageCurrent = energyStored
- nrg.energyPercentCapacity = math.floor(nrg.energyStorageCurrent / nrg.energyStorageMax * 100)
- nrg.reactorActive = nrg.fn.getReactorActive()
- end
- function nrg.gridManage()
- nrg.gridUpdate()
- if nrg.status.current ~= nrg.status.lastStatus then
- print(nrg.status.current.name)
- end
- nrg.status.current.action()
- end
- function nrg.reactorShouldBeOff()
- if nrg.reactor.getConnected() and nrg.reactor.getActive() then
- nrg.reactor.setActive(false)
- end
- end
- function nrg.reactorShouldBeOn()
- if nrg.reactor.getConnected() and not nrg.reactor.getActive() then
- nrg.reactor.setActive(true)
- end
- end
- function nrg.turbineCoilsShouldBeOff()
- for k, turbine in pairs(nrg.turbines) do
- if turbine.getConnected() and turbine.getActive() then
- turbine.setActive(false)
- end
- end
- end
- function nrg.turbinesShouldBeRPM(rpmTarget, makeActive)
- if makeActive == nil then makeActive = false end
- local allAtTarget = true
- for k, turbine in pairs(nrg.turbines) do
- if turbine.getConnected() and turbine.getRotorSpeed() >= rpmTarget then
- if makeActive then
- turbine.setInductorEngaged(true)
- end
- else
- allAtTarget = false
- if makeActive then
- turbine.setInductorEngaged(false)
- end
- end
- end
- return allAtTarget
- end
- -- Test code here
- nrg.constInit()
- nrg.configInit()
- nrg.bridgingInit()
- nrg.gridInit()
- if nrg.config.gridValid then
- print("Valid grid.")
- print("Managing grid...")
- while true do
- nrg.gridManage()
- os.sleep(2)
- end
- else
- print("Grid is not valid. Needs 1 reactor and at least 1 turbine.")
- end
- return nrg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement