Margresse404

reactorScriptForMultipleReactors

Jun 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. os.loadAPI("scripts/functions")
  2. local m
  3. m = peripheral.wrap("right")
  4.  
  5. -- Some constants
  6. energyTreshold = 2000000
  7. justTurnedAllReactorsOff = false
  8. offlineTime = 60
  9. sleepTime = 5
  10.  
  11. -- Read settings file for reactor numbers
  12. local reactorNumbers = {}
  13. local reactorOnline = {}
  14. local reactors = {}
  15. local h = fs.open("disk/reactorSettings", 'r')
  16. local line = h.readLine()
  17. counter1 = 1
  18. while line ~= nil do
  19. reactorNumbers[counter1] = line
  20. reactorOnline[counter1] = true
  21. reactors[counter1] = peripheral.wrap("BigReactors-Reactor_"..reactorNumbers[counter1])
  22. line = h.readLine()
  23. counter1 = counter1 + 1
  24. end
  25.  
  26. -- Wrap all the reactors connected to network
  27.  
  28. for i = 1, #reactorNumbers, 1 do
  29.  
  30. end
  31.  
  32. --Function that checks what to do with a reactor
  33. -- 1 : reactor off, no energy stored
  34. -- 2: reactor off, energy stored > treshold
  35. --
  36. function handleReactor(reactor, index)
  37. local active = reactor.getActive()
  38. local energy = reactor.getEnergyStored()
  39.  
  40. if active then
  41. if energy > energyTreshold then
  42. reactor.setActive(false)
  43. reactorOnline[index] = false
  44. print("Stopped reactor "..index)
  45. end
  46. else
  47. if energy < energyTreshold then
  48. reactor.setActive(true)
  49. reactorOnline[index] = true
  50. print("Started reactor "..index)
  51. end
  52. end
  53. end
  54.  
  55. function checkAllReactorsOffline()
  56. for i = 1, #reactorNumbers, 1 do
  57. if reactorOnline[i] == true then
  58. return false
  59. end
  60. end
  61. return true
  62. end
  63.  
  64. local waitLonger = false
  65. --Main loop
  66. while true do
  67.  
  68. for i = 1, #reactors, 1 do
  69. handleReactor(reactors[i], i)
  70. end
  71.  
  72. waitLonger = checkAllReactorsOffline()
  73.  
  74. if waitLonger then
  75. sleep(offlineTime)
  76. else
  77. sleep(sleepTime)
  78. end
  79.  
  80. end
  81.  
  82.  
  83. --Display something to test if this works so far
  84. for i = 1, #reactorNumbers, 1 do
  85. print(reactors[i].getEnergyStored())
  86. end
Advertisement
Add Comment
Please, Sign In to add comment