Advertisement
tobast

[CC] Simple reactor controller

Aug 2nd, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --
  2. -- Nuclear reactor controller for Computercraft/Industrialcraft
  3. -- Powers the reactor as long as
  4. -- * Required by redstone input (lever)
  5. -- * The reactor is safe (heat < threshold)
  6. -- * The battery is not full
  7. --
  8.  
  9. ------- PARAMETERS ----------------------------------
  10. REDSTONE_INPUT_SIDE = ''
  11. BATTERY_SIDE = ''
  12. REACTOR_SIDE = ''
  13. REACTOR_REDSTONE_SIDE = ''
  14. MONITOR_SIDE = '' -- Set to NIL to disable
  15.  
  16. HEAT_THRESHOLD = 65 -- %
  17. BATTERY_LOW_THRESHOLD = 80 -- %
  18.  
  19. CHECK_PERIOD = 2 --sec
  20. ------- END PARAMS ----------------------------------
  21.  
  22. local nextTimer = nil
  23. local prevState = false
  24. local reactor = nil
  25. local battery = nil
  26. local monitor = nil
  27. local prevBattCharge = 0
  28.  
  29. os.loadAPI('/lib/slot_sig')
  30.  
  31. function updateScreen(nState, status)
  32.     monitor.clear()
  33.     monitor.setCursorPos(1,1)
  34.    
  35.     if(nState==true) then
  36.         monitor.setTextColor(colors.lime)
  37.     else
  38.         monitor.setTextColor(colors.red)
  39.     end
  40.     monitor.write(status)
  41.     monitor.setTextColor(colors.white)
  42.     monitor.setCursorPos(1,3)
  43.     monitor.write('Heat: '..math.floor(reactor.getHeat())..'/'..math.floor(reactor.getMaxHeat()))
  44.     monitor.setCursorPos(1,4)
  45.     monitor.write('Batt: '..math.floor(battery.getEUStored()/battery.getEUCapacity()*100)..'%')
  46.     monitor.setCursorPos(7,5)
  47.     monitor.write(math.floor((battery.getEUStored()-prevBattCharge)/CHECK_PERIOD)..'EU/s')
  48. end
  49.  
  50. function setReactorState(nState, status)
  51.     prevState = nState
  52.  
  53.     redstone.setOutput(REACTOR_REDSTONE_SIDE, nState)
  54.     updateScreen(nState, status)
  55. end
  56.  
  57. function onTimer(data)
  58.     if(nextTimer ~= data[1]) then
  59.         return
  60.     end
  61.  
  62.     -- Determine whether the reactor should be ON or OFF
  63.     if(redstone.getInput(REDSTONE_INPUT_SIDE) == true) then
  64.         heatPercent = reactor.getHeat() / reactor.getMaxHeat() * 100
  65.         battPercent = battery.getEUStored() / battery.getEUCapacity() * 100
  66.         if(heatPercent > HEAT_THRESHOLD) then
  67.             setReactorState(false, 'Cooldown')
  68.         elseif( (prevState == true and battPercent > 98)
  69.              or (prevState == false and battPercent > BATTERY_LOW_THRESHOLD) ) then
  70.             setReactorState(false, 'Batt. full')
  71.         else
  72.             setReactorState(true, 'Running')
  73.         end
  74.     else
  75.         setReactorState(false, 'Inactive')
  76.     end
  77.  
  78.     prevBattCharge = battery.getEUStored()
  79.     nextTimer = os.startTimer(CHECK_PERIOD)
  80. end
  81.  
  82. function main()
  83.     reactor = peripheral.wrap(REACTOR_SIDE)
  84.     battery = peripheral.wrap(BATTERY_SIDE)
  85.     if(MONITOR_SIDE ~= nil) then
  86.         monitor = peripheral.wrap(MONITOR_SIDE)
  87.     end
  88.  
  89.     slot_sig.connectSlot('timer',onTimer)
  90.  
  91.     nextTimer = os.startTimer(0.5)
  92.     slot_sig.run()
  93. end
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement