Advertisement
tobast

[CC] nuclear_controller

Jul 26th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. --[[
  2.     Nuclear controller
  3.     Contrôle un réacteur, achemine les items et le sécurise.
  4.     Envoie sur requête la température, l'énergie produite,
  5.     l'état.
  6. --]]
  7.  
  8. -------- OPTIONS ----------
  9. MODEM_SIDE = ''
  10. REACTOR_SIDE = ''
  11. ITEM_INPUT_DIRECTION = ''
  12. ITEM_INPUT_SIDE = ''
  13. ITEM_OUTPUT_DIRECTION = ''
  14. HEAT_THRESHOLD = 90.0 -- percent
  15. DURABILITY_THRESHOLD = 05.0 -- percent
  16. TIMER_DELAY=2 -- s
  17. ------- END OPTIONS -------
  18.  
  19. os.loadAPI('/lib/slot_sig')
  20.  
  21. reactor = nil
  22. nextTimer = nil
  23.  
  24. ------ INCLUDE rednet.lua --------
  25. function sendData(sender)
  26.     data = {}
  27.     data.type='data'
  28.     data.output = reactor.getEUOutput()
  29.     data.heat = reactor.getHeat()
  30.     data.maxHeat = reactor.getMaxHeat()
  31.     data.realState = realState
  32.     data.requiredState = requiredState
  33.  
  34.     rednet.send(sender, data)
  35. end
  36.  
  37. function onRednet(data)
  38.     sender = data[1]
  39.     if(type(data[2]) == 'string') then
  40.         message = textutils.unserialize(data[2])
  41.     else
  42.         message = data[2]
  43.     end
  44.  
  45.     if(message.type == 'data') then
  46.         sendData(sender)
  47.     elseif(message.type == 'setstate') then
  48.         if(type(message.state) == 'boolean') then
  49.             setState(message.state)
  50.         end
  51.     end
  52. end
  53.  
  54. ------ END rednet.lua ------------
  55. ------ INCLUDE corecontrol.lua --------
  56. ENUM_STATES = {OFF=0,ON=1,COOLDOWN=2,EMPTY=3}
  57. requiredState = 0 -- OFF
  58. realState = 0 -- Including cooldown and empty
  59.  
  60. function applyState()
  61.     if(realState == ENUM_STATE.ON) then
  62.         redstone.setOutput(REACTOR_SIDE, true)
  63.     else
  64.         redstone.setOutput(REACTOR_SIDE, false)
  65.     end
  66. end
  67.  
  68. function setState(nState)
  69.     if(nState) then
  70.         requiredState = ENUM_STATES.ON
  71.         if(realState == ENUM_STATES.OFF) then -- Can be activated
  72.             realState = ENUM_STATES.ON
  73.         end
  74.     else
  75.         requiredState = ENUM_STATES.OFF
  76.     end
  77.     applyState()
  78. end
  79.  
  80. function checkResources()
  81.     --TODO
  82.     return true
  83. end
  84.  
  85. function periodicCheck()
  86.     heat = reactor.getHeat()
  87.     maxHeat = reactor.getMaxHeat()
  88.     outEU = reactor.getEUOutput()
  89.     percentHeat = heat / maxHeat
  90.    
  91.     if(percentHeat >= HEAT_THRESHOLD) then
  92.         realState = ENUM_STATES.COOLDOWN
  93.     elseif(checkResources() == false) then
  94.         realState = ENUM_STATES.EMPTY
  95.     else
  96.         realState = requiredState
  97.     end
  98.     applyState()
  99. end
  100.  
  101.  
  102. ------ END corecontrol.lua ------------
  103.  
  104. function onTimer(data)
  105.     if(data[1] == nextTimer) then
  106.         periodicCheck()
  107.     end
  108.     nextTimer = os.startTimer(TIMER_DELAY)
  109. end
  110.  
  111. function main()
  112.     rednet.open(MODEM_SIDE)
  113.     redstone.setOutput(REACTOR_SIDE, false)
  114.  
  115.     nextTimer = os.startTimer(TIMER_DELAY)
  116.  
  117.     reactor = peripheral.wrap(REACTOR_SIDE)
  118.  
  119.     slot_sig.connectSlot('timer', onTimer)
  120.     slot_sig.connectSlot('rednet',onRednet)
  121. end
  122.  
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement