Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Nuclear reactor controller for Computercraft/Industrialcraft
- -- Powers the reactor as long as
- -- * Required by redstone input (lever)
- -- * The reactor is safe (heat < threshold)
- -- * The battery is not full
- --
- ------- PARAMETERS ----------------------------------
- REDSTONE_INPUT_SIDE = ''
- BATTERY_SIDE = ''
- REACTOR_SIDE = ''
- REACTOR_REDSTONE_SIDE = ''
- MONITOR_SIDE = '' -- Set to NIL to disable
- HEAT_THRESHOLD = 65 -- %
- BATTERY_LOW_THRESHOLD = 80 -- %
- CHECK_PERIOD = 2 --sec
- ------- END PARAMS ----------------------------------
- local nextTimer = nil
- local prevState = false
- local reactor = nil
- local battery = nil
- local monitor = nil
- local prevBattCharge = 0
- os.loadAPI('/lib/slot_sig')
- function updateScreen(nState, status)
- monitor.clear()
- monitor.setCursorPos(1,1)
- if(nState==true) then
- monitor.setTextColor(colors.lime)
- else
- monitor.setTextColor(colors.red)
- end
- monitor.write(status)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(1,3)
- monitor.write('Heat: '..math.floor(reactor.getHeat())..'/'..math.floor(reactor.getMaxHeat()))
- monitor.setCursorPos(1,4)
- monitor.write('Batt: '..math.floor(battery.getEUStored()/battery.getEUCapacity()*100)..'%')
- monitor.setCursorPos(7,5)
- monitor.write(math.floor((battery.getEUStored()-prevBattCharge)/CHECK_PERIOD)..'EU/s')
- end
- function setReactorState(nState, status)
- prevState = nState
- redstone.setOutput(REACTOR_REDSTONE_SIDE, nState)
- updateScreen(nState, status)
- end
- function onTimer(data)
- if(nextTimer ~= data[1]) then
- return
- end
- -- Determine whether the reactor should be ON or OFF
- if(redstone.getInput(REDSTONE_INPUT_SIDE) == true) then
- heatPercent = reactor.getHeat() / reactor.getMaxHeat() * 100
- battPercent = battery.getEUStored() / battery.getEUCapacity() * 100
- if(heatPercent > HEAT_THRESHOLD) then
- setReactorState(false, 'Cooldown')
- elseif( (prevState == true and battPercent > 98)
- or (prevState == false and battPercent > BATTERY_LOW_THRESHOLD) ) then
- setReactorState(false, 'Batt. full')
- else
- setReactorState(true, 'Running')
- end
- else
- setReactorState(false, 'Inactive')
- end
- prevBattCharge = battery.getEUStored()
- nextTimer = os.startTimer(CHECK_PERIOD)
- end
- function main()
- reactor = peripheral.wrap(REACTOR_SIDE)
- battery = peripheral.wrap(BATTERY_SIDE)
- if(MONITOR_SIDE ~= nil) then
- monitor = peripheral.wrap(MONITOR_SIDE)
- end
- slot_sig.connectSlot('timer',onTimer)
- nextTimer = os.startTimer(0.5)
- slot_sig.run()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement