Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local metaturbine={__index=function(self,key) return self.subTurbine[key] end}
- interval=0.5
- --[[
- States:
- 0: No reactor running. On standby (freely spinning, no steam) until power is needed.
- 1: Generating power. Will turn on and off reactor as needed to keep turbine at maximum efficiency.
- 2: Spinning up. Will keep reactor on and coils off until the turbine is above ideal and sufficiently inefficient, at which point it will switch to 1.
- ]]
- function percentify(num)
- return tostring(math.floor(((num)*1000)+0.5)/10)..'%'
- end
- function buildTurbine(turbinePeripheral)
- local superTurbine={subTurbine=turbinePeripheral}
- superTurbine.getEfficiency=function(self)
- local rotorSpeed=self.getRotorSpeed()
- return rotorSpeed<500 and 0.5 or 0.25*(math.cos(rotorSpeed/(45.5*math.pi))+3)
- end
- superTurbine.turbineActivities=function(self)
- local RPM=self.subTurbine.getRotorSpeed()
- local energyStored=self.subTurbine.getEnergyStored()
- if energyStored<100000 then
- reactorState=1
- elseif energyStored>900000 then
- reactorState=0
- end
- local efficiency=self:getEfficiency()
- if reactorState==0 then
- self.setInductorEngaged(false)
- term.setCursorPos(1,3)
- self.standbyTicks=self.standbyTicks+1
- term.write('Current status: standby')
- powerMode=false
- elseif reactorState==1 then
- if efficiency<0.999 then
- if RPM<self.idealAmount then
- term.setCursorPos(1,3)
- term.write('Current status: spinning up')
- self.setInductorEngaged(false)
- self.spinUpTicks=self.spinUpTicks+1
- self.subTurbine.setFluidFlowRateMax(self.subTurbine.getFluidFlowRateMaxMax())
- powerMode=true
- reactorState=2
- else
- term.setCursorPos(1,3)
- term.write('Current status: slowing down')
- self.slowTicks=self.slowTicks+1
- powerMode=false
- self.setInductorEngaged(true)
- end
- else
- term.setCursorPos(1,3)
- term.write('Current status: generating power')
- self.upTicks=self.upTicks+1
- powerMode=true
- self.setInductorEngaged(true)
- end
- self.rotorNumber=math.max(self.rotorNumber,math.floor(self.getFluidFlowRate()/25))
- else
- term.setCursorPos(1,3)
- term.write('Current status: spinning up')
- self.setInductorEngaged(false)
- powerMode=true
- self.spinUpTicks=self.spinUpTicks+1
- self.subTurbine.setFluidFlowRateMax(self.subTurbine.getFluidFlowRateMaxMax())
- if RPM>self.idealAmount and efficiency<0.999 then
- reactorState=1
- end
- end
- term.setCursorPos(1,4)
- term.write('Turbine powergen time: ' .. percentify(self.upTicks/tickCount))
- term.setCursorPos(1,6)
- term.write('Turbine slowdown time: ' .. percentify(self.slowTicks/tickCount))
- term.setCursorPos(1,5)
- term.write('Turbine spinup time: ' .. percentify(self.spinUpTicks/tickCount))
- term.setCursorPos(1,7)
- term.write('Turbine standby time: ' .. percentify(self.standbyTicks/tickCount))
- end
- superTurbine.idealAmount=1800
- superTurbine.rotorNumber=0
- superTurbine.spinUpTicks=0
- superTurbine.standbyTicks=0
- superTurbine.upTicks=0
- superTurbine.slowTicks=0
- setmetatable(superTurbine,metaturbine)
- return superTurbine
- end
- local function findPeripherals()
- local reactor,turbines=nil,{}
- turbines={}
- for k,v in pairs(peripheral.getNames()) do
- if (peripheral.getType(v) == "BigReactors-Reactor") then
- reactor = peripheral.wrap(v)
- print('reactor found!')
- elseif (peripheral.getType(v) == "BigReactors-Turbine") then
- local turbine=buildTurbine(peripheral.wrap(v))
- table.insert(turbines,turbine)
- print('turbine ' .. #turbines .. ' found!')
- end
- end
- turbines.getConnected=function(self)
- for _,turbineTable in pairs(self) do
- if type(turbineTable)=='table' then
- if not turbineTable.turbine.getConnected() then return false end
- end
- end
- return true
- end
- turbines.getEnergyStored=function(self)
- local sum=0
- for _,turbineTable in pairs(self) do
- if type(turbineTable)=='table' then
- sum=sum+turbineTable.turbine.getEnergyStored()
- end
- end
- return sum
- end
- turbines.getHotFluidRequired=function(self)
- local sum=0
- for _,turbinetable in pairs(self) do
- if type(turbineTable)=='table' then
- sum=sum+turbineTable.getFluidFlowRateMax()
- end
- end
- return sum
- end
- return reactor, turbines
- end
- local reactor,turbines=findPeripherals()
- tickCount,reactorUpTicks=0,0
- local function reactorActivities()
- if not powerMode then
- reactor.setActive(false)
- else
- reactorUpTicks=reactorUpTicks+1
- reactor.setActive(true)
- end
- term.setCursorPos(1,1)
- term.write('The reactor is currently: ' .. (powerMode and 'on' or 'off'))
- term.setCursorPos(1,2)
- term.write('Tick count: '..tickCount)
- term.setCursorPos(1,8)
- term.write('Reactor uptime percent: '..percentify(reactorUpTicks/tickCount))
- end
- local function activities()
- term.clear()
- for k,turbine in ipairs(turbines) do turbine:turbineActivities(tickCount) end
- reactorActivities()
- end
- while true do
- os.sleep(interval)
- activities()
- tickCount=tickCount+1
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement