Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Nuclear controller
- Contrôle un réacteur, achemine les items et le sécurise.
- Envoie sur requête la température, l'énergie produite,
- l'état.
- --]]
- -------- OPTIONS ----------
- MODEM_SIDE = ''
- REACTOR_SIDE = ''
- ITEM_INPUT_DIRECTION = ''
- ITEM_INPUT_SIDE = ''
- ITEM_OUTPUT_DIRECTION = ''
- HEAT_THRESHOLD = 90.0 -- percent
- DURABILITY_THRESHOLD = 05.0 -- percent
- TIMER_DELAY=2 -- s
- ------- END OPTIONS -------
- os.loadAPI('/lib/slot_sig')
- reactor = nil
- nextTimer = nil
- ------ INCLUDE rednet.lua --------
- function sendData(sender)
- data = {}
- data.type='data'
- data.output = reactor.getEUOutput()
- data.heat = reactor.getHeat()
- data.maxHeat = reactor.getMaxHeat()
- data.realState = realState
- data.requiredState = requiredState
- rednet.send(sender, data)
- end
- function onRednet(data)
- sender = data[1]
- if(type(data[2]) == 'string') then
- message = textutils.unserialize(data[2])
- else
- message = data[2]
- end
- if(message.type == 'data') then
- sendData(sender)
- elseif(message.type == 'setstate') then
- if(type(message.state) == 'boolean') then
- setState(message.state)
- end
- end
- end
- ------ END rednet.lua ------------
- ------ INCLUDE corecontrol.lua --------
- ENUM_STATES = {OFF=0,ON=1,COOLDOWN=2,EMPTY=3}
- requiredState = 0 -- OFF
- realState = 0 -- Including cooldown and empty
- function applyState()
- if(realState == ENUM_STATE.ON) then
- redstone.setOutput(REACTOR_SIDE, true)
- else
- redstone.setOutput(REACTOR_SIDE, false)
- end
- end
- function setState(nState)
- if(nState) then
- requiredState = ENUM_STATES.ON
- if(realState == ENUM_STATES.OFF) then -- Can be activated
- realState = ENUM_STATES.ON
- end
- else
- requiredState = ENUM_STATES.OFF
- end
- applyState()
- end
- function checkResources()
- --TODO
- return true
- end
- function periodicCheck()
- heat = reactor.getHeat()
- maxHeat = reactor.getMaxHeat()
- outEU = reactor.getEUOutput()
- percentHeat = heat / maxHeat
- if(percentHeat >= HEAT_THRESHOLD) then
- realState = ENUM_STATES.COOLDOWN
- elseif(checkResources() == false) then
- realState = ENUM_STATES.EMPTY
- else
- realState = requiredState
- end
- applyState()
- end
- ------ END corecontrol.lua ------------
- function onTimer(data)
- if(data[1] == nextTimer) then
- periodicCheck()
- end
- nextTimer = os.startTimer(TIMER_DELAY)
- end
- function main()
- rednet.open(MODEM_SIDE)
- redstone.setOutput(REACTOR_SIDE, false)
- nextTimer = os.startTimer(TIMER_DELAY)
- reactor = peripheral.wrap(REACTOR_SIDE)
- slot_sig.connectSlot('timer', onTimer)
- slot_sig.connectSlot('rednet',onRednet)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement