Advertisement
Guest User

ReactorControl

a guest
Feb 7th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. --Turbines
  2. t={}
  3. anzTurb = 0
  4.  
  5. --All peripherals
  6. local peripheralList = peripheral.getNames()
  7. for i=0,#peripheralList do
  8.     if peripheral.getType(peripheralList[i]) == "BigReactors-Turbine" then
  9.         t[i]=peripheral.wrap(peripheralList[i])
  10.         anzTurb = anzTurb + 1
  11.     end
  12.     if peripheral.getType(peripheralList[i]) == "BigReactors-Reactor" then
  13.         r = peripheral.wrap(peripheralList[i])
  14.     end
  15.     if peripheral.getType(peripheralList[i]) == "monitor" then
  16.         mon = peripheral.wrap(peripheralList[i])
  17.     end
  18.     if peripheral.getType(peripheralList[i]) == "tile_blockcapacitorbank_name" then
  19.         v = peripheral.wrap(peripheralList[i])
  20.     end
  21. end
  22.  
  23. optRodLevel = 0
  24. anzTurb = anzTurb - 1
  25.  
  26. --TurbineSpeed
  27. tspeed = {}
  28.  
  29. function init()
  30.     mon.setCursorPos(1,1)
  31.     mon.setBackgroundColor(colors.blue)
  32.     term.clear()
  33.     mon.clear()
  34.  
  35.     checkRotorSpeed()
  36.     findOptimalFuelRodLevel()
  37. end
  38.  
  39. function checkRotorSpeed()
  40.   for i=0,anzTurb,1 do
  41.     tspeed[i] = t[i].getRotorSpeed()
  42.   end
  43. end
  44.  
  45. function reactorOn()
  46.   r.setActive(true)
  47. end
  48.  
  49. function reactorOff()
  50.   r.setActive(false)
  51. end
  52.  
  53. function getEnergy()
  54.   return v.getEnergyStored()
  55. end
  56.  
  57. function getEnergyMax()
  58.   return v.getMaxEnergyStored()
  59. end
  60.  
  61. function getEnergyPer()
  62.   local en = getEnergy()
  63.   local enMax = getEnergyMax()
  64.   local enPer = math.floor(en/enMax*100)
  65.   return enPer
  66. end
  67.  
  68. function allTurbinesOn()
  69.   for i=0,anzTurb,1 do
  70.     t[i].setInductorEngaged(true)
  71.     t[i].setFluidFlowRateMax(2000)
  72.   end
  73. end
  74.  
  75. function allTurbinesOff()
  76.   for i=0,anzTurb,1 do
  77.     t[i].setInductorEngaged(false)
  78.     t[i].FluidFlowRateMax(0)
  79.   end
  80. end
  81.  
  82. function turbineOn(i)
  83.   t[i].setInductorEngaged(true)
  84.   t[i].setFluidFlowRateMax(2000)
  85. end
  86.  
  87. function turbineOff(i)
  88.   t[i].setInductorEngaged(false)
  89.   t[i].setFluidFlowRateMax(0)
  90. end
  91.  
  92. function findOptimalFuelRodLevel()
  93.     reactorOn()
  94.     allTurbinesOn()
  95.    
  96.     local controlRodLevel = 50
  97.     local optRodLevelFound = true
  98.     local steamOutput = r.getHotFluidProducedLastTick()
  99.     local targetSteamOutput = 2000*anzTurb
  100.    
  101.     while optRodLevelFound do
  102.         r.setAllControlRodLevels(controlRodLevel)
  103.         steamOutput = r.getHotFluidProducedLastTick()
  104.         sleep(2)
  105.         if steamOutput < targetSteamOutput then
  106.             r.setAllControlRodLevels(controlRodLevel - 1)
  107.         end
  108.         if steamOutput >= targetSteamOutput then
  109.             r.setAllControlRodLevels(controlRodLevel + 1)
  110.             sleep(5)
  111.             if steamOutput < targetSteamOutput then
  112.                 r.setAllControlRodLevels(controlRodLevel - 1)
  113.                 optRodLevel = controlRodLevel
  114.                 optRodLevelFound = false
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function checkEnergyLevel()
  121.   if getEnergyPer() < 50 then
  122.     for i=0,anzTurb,1 do
  123.       t[i].setFluidFlowRateMax(2000)
  124.     end
  125.     reactorOn()
  126.     getToTargetSpeed()
  127.   end
  128.   if getEnergyPer() >= 90 then
  129.     for i=0,anzTurb,1 do
  130.       if tspeed[i] < 1800 then
  131.         reactorOn()
  132.         t[i].setInductorEngaged(false)
  133.         t[i].setFluidFlowRateMax(2000)
  134.       end
  135.       if tspeed[i] > 1840 then
  136.         turbineOff(i)
  137.       end
  138.     end
  139.     if allAtTargetSpeed() then
  140.       reactorOff()
  141.     end
  142.   end
  143. end
  144.  
  145. function getToTargetSpeed()
  146.   for i=0,anzTurb,1 do
  147.     if tspeed[i] < 1800 then
  148.       t[i].setInductorEngaged(false)
  149.       t[i].setFluidFlowRateMax(2000)
  150.     end
  151.     if tspeed[i] > 1860 then
  152.       t[i].setInductorEngaged(true)
  153.     end
  154.   end
  155. end
  156.  
  157. function allAtTargetSpeed()
  158.   local tmp = {}
  159.   local isTrue = true
  160.   for i=0,anzTurb,1 do
  161.     if tspeed[i] > 1800 then
  162.       tmp[i] = 1
  163.     end
  164.     if tspeed[i] < 1800 then
  165.       tmp[i] = 0
  166.     end
  167.   end  
  168.   for i=0,anzTurb,1 do
  169.     if tmp[i] == 0 then
  170.       isTrue = false
  171.     end
  172.   end
  173.   return isTrue
  174. end
  175.  
  176. --This function is from http://pastebin.com/WTyFveB8 - Lines 346 to 363
  177. function comma_value(amount)
  178.    local formatted = amount
  179.    local swap = false
  180.    if formatted < 0 then
  181.       formatted = formatted*-1
  182.       swap = true
  183.    end
  184.    while true do
  185.       formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
  186.       if k == 0 then
  187.          break
  188.       end
  189.    end
  190.    if swap then
  191.      formatted = "-"..formatted
  192.    end
  193.    return formatted
  194. end
  195.  
  196.  
  197. function printStats()
  198.  
  199.   local rfGen = 0
  200.   local reactorStatus = off
  201.  
  202.   for i=0,30,1 do
  203.     term.clearLine(i)
  204.   end
  205.  
  206.   for i=0,anzTurb,1 do
  207.     rfGen = rfGen + t[i].getEnergyProducedLastTick()
  208.   end  
  209.  
  210.   if r.isActive(true) then
  211.     reactorStatus = on
  212.   end
  213.  
  214.   if r.isActive(false) then
  215.     reactorStatus = off
  216.   end
  217.  
  218.   term.setCursorPos(1,1)
  219.   mon.write("Energy Level: ")
  220.   mon.write(getEnergyPer())
  221.   mon.write("%")
  222.  
  223.   term.setCursorPos(1,2)
  224.   mon.write("RF-Gen: ")
  225.   mon.write(math.floor(rfGen))
  226.   mon.write(" RF/t")
  227.  
  228.   mon.setCursorPos(1,3)
  229.   mon.write("Fuel Consumption: ")  
  230.   mon.write(comma_value(t.getFuelConsumedLastTick())
  231.   mon.write(" RF/t")
  232.  
  233.   mon.setCursorPos(1,5)
  234.   mon.write("Reactor: ")
  235.  
  236.   if reactorStatus == "on" then
  237.     mon.setTextColor(colors.green)
  238.   end
  239.   if reactorStatus == "off" then
  240.     mon.setTextColor(colors.red)
  241.   end
  242.   mon.write(reactorStatus)
  243.   mon.setTextColor(colors.white)
  244.  
  245.   mon.setCursorPos(1,7)
  246.   mon.write("-- Turbine 0 --")
  247.  
  248.   mon.setCursorPos(1,8)
  249.   mon.write("Coils: ")
  250.  
  251.   if t[0].isActive(true) then
  252.     mon.setTextColor(colors.green)
  253.     mon.write("engaged")
  254.   end
  255.   if t[0].isActive(true) then
  256.     mon.setTextColor(colors.red)
  257.     mon.write("disengaged")
  258.   end
  259.   mon.setTextColor(colors.white)
  260.  
  261.   mon.setCursorPos(1,9)
  262.   mon.write("Rotor Speed: ")
  263.   mon.write(comma_value(t[0].getRotorSpeed()))  
  264. end
  265.  
  266.  
  267. --Startet alles
  268. init()
  269. while true do
  270.   checkRotorSpeed()
  271.   checkEnergyLevel()
  272.   printStats()
  273.   sleep(1)
  274. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement