pneisen

Untitled

Feb 17th, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. local powerThreshold = -20000
  2. local interval = 10
  3. local turbineDifferenceThreshold = 200000000
  4. local turbineRunSpeed = 1800
  5. local controlRodPercent = 18
  6.  
  7. local component = require("component")
  8. local term = require("term")
  9. local sides = require("sides")
  10.  
  11. local reactor = component.br_reactor
  12. local turbine = component.br_turbine
  13. local crystal = component.deepresonance_controller
  14. local storage = component.draconic_rf_storage
  15. local redstone = component.redstone
  16.  
  17. local crystalOn = false
  18.  
  19. local function updateRedstone()
  20.   -- This is a hack to support reporting of reactor and crystal status on a RFTools screen when
  21.   -- toggled by this script and manually.
  22.   if reactor.getControlRodLevel(0) < 100 then
  23.     redstone.setOutput(sides.west, 15)
  24.   else
  25.     redstone.setOutput(sides.west, 0)
  26.   end
  27.  
  28.   if crystalOn or redstone.getInput(sides.bottom) > 0 then
  29.     redstone.setOutput(sides.east, 15)
  30.   else
  31.     redstone.setOutput(sides.east, 0)
  32.   end
  33. end
  34.  
  35. local function startReactor()
  36.   reactor.setAllControlRodLevels(controlRodPercent)
  37.   turbine.setInductorEngaged(false)
  38.   print("Turbine spinning up.")
  39.  
  40.   updateRedstone()
  41. end
  42.  
  43. local function startCrystal()
  44.   crystal.activate()
  45.   crystalOn = true
  46.   print("Deep Resonance reactor started.")
  47.  
  48.   updateRedstone()
  49. end
  50.  
  51. local function stopReactor()
  52.   reactor.setAllControlRodLevels(100)
  53.  
  54.   -- If storage is full, free spin the turbine to save momentum. We can't use the spin down power anyway.
  55.   if storage.getEnergyStored() == storage.getMaxEnergyStored() then
  56.     turbine.setInductorEngaged(false)
  57.   end
  58.  
  59.   updateRedstone()
  60. end
  61.  
  62. local function stopCrystal()
  63.   if crystalOn == true then
  64.     crystal.deactivate()
  65.     crystal.setRSControlled(true)
  66.     crystalOn = false
  67.     print("Deep Resonance reactor stopped.")
  68.  
  69.     updateRedstone()
  70.   end
  71. end
  72.  
  73. --
  74. -- Start main logic here
  75. --
  76.  
  77. local highWater = 0
  78. local maxStorage = storage.getMaxEnergyStored()
  79.  
  80. while true do
  81.   rfTick = storage.getTransferPerTick()
  82.  
  83.   if rfTick <= powerThreshold then
  84.     -- Over the threshold. Go into power production mode.
  85.     print("Start power production.")
  86.  
  87.     -- Start up the crystal gen since the turbine takes 5 minutes to spin up.
  88.     startCrystal()
  89.  
  90.     -- How far off the high water mark are we? The crystal gen will put out 162 mil in the time it takes the
  91.     -- turbine to start so we might want to just do that.
  92.     local turbineSpinUp = false
  93.  
  94.     if highWater - storage.getEnergyStored() > turbineDifferenceThreshold then
  95.       -- Start the reactor and spin up the turbine.
  96.       startReactor()
  97.       turbineSpinUp = true
  98.     end
  99.  
  100.  
  101.     -- Power recovery loop
  102.     while storage.getEnergyStored() < highWater do
  103.  
  104.       -- Check to see how big the hole is. We might want to turn on the turbine at this point.
  105.       if (not tubineSpinUp) and (highWater - storage.getEnergyStored() > turbineDifferenceThreshold) then
  106.         -- Start the reactor and spin up the turbine.
  107.         startReactor()
  108.         turbineSpinUp = true
  109.       end
  110.  
  111.       -- Are we spinning up the turbine? If so, keep an eye on that. When it comes up, turn off the crystal gen.
  112.       if turbineSpinUp then
  113.         if turbine.getRotorSpeed() >= turbineRunSpeed then
  114.           turbine.setInductorEngaged(true)
  115.           turbineSpinUp = false
  116.           print("Turbine spun up.")
  117.           stopCrystal()
  118.         end
  119.       end
  120.  
  121.       os.sleep(1)
  122.     end
  123.  
  124.     -- If we fell out of the power recovery loop, we are recovered. Shut down everything.
  125.     stopCrystal()
  126.     stopReactor()
  127.  
  128.     print("Recovered. End power production.")
  129.   end
  130.  
  131.   highWater = storage.getEnergyStored()
  132.   term.clear()
  133.   print("Stored: " .. highWater .. " Transfer: " .. rfTick)
  134.   updateRedstone()
  135.   os.sleep(interval)
  136. end
Add Comment
Please, Sign In to add comment