Advertisement
Noobyhead99

reactor

Jul 17th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local function wrapPeripherals()
  2.     local attachedReactor = peripheral.find("BigReactors-Reactor")
  3.     local attachedCubes = {}
  4.     for k,v in pairs(peripheral.getNames()) do
  5.         if string.sub(peripheral.getType(v),-11) == "energy_cube" then
  6.             table.insert(attachedCubes,peripheral.wrap(v))
  7.         end
  8.     end
  9.     return attachedReactor, attachedCubes
  10. end
  11.  
  12. local reactor, cubes = wrapPeripherals()
  13.  
  14. --[[if not reactor then
  15.     print("No Reactor Computer Port found!")
  16.     return
  17. end
  18. if not cubes[1] then
  19.     print("No energy cube found!")
  20.     return
  21. end
  22. if not reactor.getConnected() then
  23.     print("No valid reactor connected!")
  24.     return
  25. end--]]
  26.  
  27. local function checkReactor()
  28.     if not reactor.getConnected() then
  29.         repeat
  30.             print("Connected reactor invalid! Please rectify and press any key to continue.")
  31.             os.pullEvent("key")
  32.         until reactor.getConnected()
  33.         return true
  34.     end
  35. end
  36.  
  37. local function checkPeripherals()
  38.     reactor, cubes = wrapPeripherals()
  39.     if not reactor or not cubes[1] then
  40.         print("Peripheral missing! Please ensure that both a Reactor Computer Port and an energy cube are attached to continue operation.")
  41.         repeat
  42.             os.pullEvent()
  43.             reactor, cubes = wrapPeripherals()
  44.         until reactor and cubes[1]
  45.         checkReactor()
  46.         print("Peripherals attached. Resuming normal operation.")
  47.     end
  48. end
  49.  
  50. local function addCubes(cubesTable,getMax)
  51.     local val = 0
  52.     for k,v in pairs(cubesTable) do
  53.         if getMax then
  54.             val = val + v.getMaxEnergyStored("")
  55.         else
  56.             val = val + v.getEnergyStored("")
  57.         end
  58.     end
  59.     return val
  60. end
  61.  
  62. local timer
  63. local eventParamters
  64. while true do
  65.     if addCubes(cubes,false) < addCubes(cubes,true) and not reactor.getActive() then
  66.         reactor.setActive(true)
  67.     elseif addCubes(cubes,false) == addCubes(cubes,true) and reactor.getActive() then
  68.         reactor.setActive(false)
  69.     end
  70.     timer = os.startTimer(1)
  71.     repeat
  72.         eventParameters = {os.pullEvent()}
  73.     until (eventParameters[1] == "timer" and eventParameters[2] == timer) or string.sub(eventParameters[1],1,10) == "peripheral"
  74.     if string.sub(eventParameters[1],1,10) == "peripheral" then
  75.         checkPeripherals()
  76.     end
  77.     if checkReactor() then
  78.         print("Resuming normal operation.")
  79.     end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement