Advertisement
jdmbone003

Multi Turbine Reactor.lua

May 23rd, 2023 (edited)
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.94 KB | None | 0 0
  1. --Automatic control program for Extreme Reactors mod for Minecraft
  2. --Theoretically works with infinite number of each but display only supports 20 of each
  3. --Prefers external storage, designed for enderIO capacitors, but can fall back to internal energy storage
  4. --cannot manage active cooled reactors yet, by default reactors are used as backup power generation
  5. --written by jdmbone003
  6. --to keep up to date run with the command os.execute("pastebin run 3g979aUy")
  7.  
  8. component=require("component")      --needed to work with mod components
  9. computer=require("computer")        --needed to set a timer
  10. term=require("term")                --needed to write to screen
  11. os=require("os")                    --needed to sleep if we execute everything before the timer is up
  12.  
  13. gpu=component.gpu           --set resolution and use color
  14. term.clear()                --clear terminal
  15. gpu.setResolution(100,35)   --set resolution
  16.  
  17. set={50,80,99.5}
  18. reactorstats={{}}       --array to store the stats for all reactors
  19. turbinestats={{}}       --turbines
  20. numreactors=0           --count of reactors
  21. numturbines=0           --turbines
  22. running=true
  23.  
  24. Reactors={}
  25. if component.isAvailable("br_reactor") then     --if we have reactors
  26.   for addr in component.list("br_reactor") do   --get a proxy for each one
  27.     local r=component.proxy(addr)
  28.     table.insert(Reactors,r)                    --store it in this table
  29.     numreactors=numreactors+1                   --increment the number of reactors
  30.   end
  31. print("Found "..numreactors.." reactors.")
  32. end
  33.  
  34. Turbines={}
  35. if component.isAvailable("br_turbine") then     --same as with the turbines
  36.   for addr in component.list('br_turbine') do
  37.     local t=component.proxy(addr)
  38.     table.insert(Turbines,t)
  39.     numturbines=numturbines+1
  40.   end
  41. print("Found "..numturbines.." turbines.")
  42. end
  43.  
  44. if component.isAvailable("energy_device") then
  45.   capacitor=component.energy_device
  46.   print("External energy storage initialized. Capacity: "..capacitor.getMaxEnergyStored())
  47. else
  48.   print("No external energy storage found!")
  49. end
  50.  
  51. for i=1,numreactors do  --setup initial values for all reactors
  52.   rc=Reactors[i]
  53.   rc.setAllControlRodLevels(100)
  54.   rc.setActive(true)
  55.   --reactorstats {Rod Level,Min Rod Level,Fuel Temp,Case Temp,Reactivity,Energy Produced,Max Energy Produced}
  56.   reactorstats[i]={100,0,0,0,0,0,100}
  57. end
  58. print("Reactors initialized.")
  59.  
  60. for i=1,numturbines do  --setup initial values for all turbines
  61.   tb=Turbines[i]
  62.   tb.setActive(true)
  63.   --turbinestats {on,activ,hold,Fluid_Flow_Rate_Max,Max_Efficient_Flow_Rate,Blade Speed,Energy Produced,Max Energy Produced}
  64.   turbinestats[i]={true,true,0,0,tb.getNumberOfBlades()*25,math.floor(tb.getRotorSpeed()),math.floor(tb.getEnergyProducedLastTick()),100}
  65.   print(tb.getNumberOfBlades())
  66. end
  67. print("Turbines initialized.")
  68.  
  69. os.sleep(2)
  70. term.clear()
  71.  
  72. --main loop
  73. while running do
  74.   timer=computer.uptime()+0.5   --limit updates to every .5 seconds
  75.   if component.isAvailable("energy_device") then    --if external energy device detected reference it for control
  76.     cappct=math.floor((capacitor.getEnergyStored()/capacitor.getMaxEnergyStored())*1000)/10
  77.   elseif numturbines > 0 then                   --otherwise use the first turbines
  78.     tb=Turbines[1]
  79.     cappct=math.floor((tb.getEnergyStored()/tb.getEnergyCapacity())*1000)/10
  80.   elseif numreactors > 0 then                   --otherwise use the first Reactors
  81.     rc=Reactors[1]
  82.     cappct=math.floor((rc.getEnergyStored()/rc.getEnergyCapacity())*1000)/10
  83.   else                                      --otherwise nothing is connected so break out of the main loop
  84.     break
  85.   end
  86.  
  87.   for i=1,numreactors do    --iterate through each reactor
  88.     rc=Reactors[i]          --get the proxy of the reactor
  89.     minrodlvl=reactorstats[i][2]    --what is the minimum rod setting?
  90.     ftemp=math.floor(rc.getFuelTemperature()+0.5)   --fuel temp
  91.     ctemp=math.floor(rc.getCasingTemperature()+0.5) --casing temp
  92.     reactiv=math.floor(rc.getFuelReactivity())      --fuel reactivity
  93.     enprod=math.floor(rc.getEnergyProducedLastTick())   --energy produced
  94.     maxprod=reactorstats[i][7]                  --record highest energy production
  95.     if enprod>maxprod then maxprod=enprod end   --if we produced more energy than we have before update the highest
  96.     if cappct>set[1] then setrodlvl=100 end     --if energy storage is above set limit, drop the rods to stop reaction
  97.     if cappct<=set[1] then setrodlvl=math.floor(cappct*(100/set[1])) end    --drop rods if storage is below set limit
  98.     if ftemp>2000 then minrodlvl=setrodlvl+1 end    --restrict min rod level if fuel gets above 2000
  99.     if setrodlvl==minrodlvl and ftemp<2000 then minrodlvl=minrodlvl-1 end  
  100.     if setrodlvl<minrodlvl then setrodlvl=minrodlvl end
  101.     rc.setAllControlRodLevels(setrodlvl)    --set the rods
  102.     reactorstats[i]={setrodlvl,minrodlvl,ftemp,ctemp,reactiv,enprod,maxprod}    --save this reactors stats to the array
  103.     if not rc.getActive() then running=false end    --if reactor is off, set main loop to exit
  104.   end
  105.  
  106.   for i=1,numturbines do    --do all that again but for he turbines
  107.     tb=Turbines[i]          --proxy or turbine
  108.     on,activ,hold,FFRM,MEFR,maxprod=turbinestats[i][1],tb.getInductorEngaged(),turbinestats[i][3],turbinestats[i][4],turbinestats[i][5],turbinestats[i][8]   --get select stats of current turbine from array
  109.     speed=math.floor(tb.getRotorSpeed())    --current rotor speed
  110.     enprod=math.floor(tb.getEnergyProducedLastTick())   --energy produced
  111.    
  112.     if cappct<set[3] and speed>0 then activ=true end    --set coils to activate
  113.     if cappct<set[2] then on=true activ=true end        --set steam flow to activate
  114.     if cappct>=set[3] then on=false activ=false end     --set both to deactivate
  115.     if cappct<=set[1] and hold==1800 and speed<1650 then on=true activ=false end    --to speed up faster keep the coils off
  116.     if cappct<=set[1] and hold== 900 and speed< 730 then on=true activ=false end    --until we get closer to set speed
  117.     tb.setInductorEngaged(activ)                        --(de)activate coils from above logic      
  118.     if cappct<set[2] and MEFR>=500 then hold=1800 end   --if we have enough blades and above storage % hold at 1800RPM
  119.     if cappct>=set[2] then hold=900 end                 --if below storage % hold at 900RPM
  120.  
  121.     if speed<hold then
  122.       if hold==1800 then FFRM=MEFR end  --if speed is below target set steam to max effective for 1800
  123.       if hold==900 then FFRM=MEFR/2 end --1/2 effective for 900
  124.     end
  125.     if speed>=hold then
  126.       if hold==1800 then FFRM=MEFR/2 end    --1/2 effective should also hold around 1800
  127.       if hold==900 then
  128.         if speed>950 then FFRM=0 end
  129.         if speed<950 then FFRM=MEFR/4 end   --1/4 effective should hold close to 900
  130.       end
  131.     end
  132.     if speed>1850 then FFRM=0 end           --if speed is too high cut the steam flow
  133.     if not on then FFRM=0 end               --if steam is not set to be on cut the steam flow
  134.     tb.setFluidFlowRateMax(FFRM)            --set steam flow from above logic
  135.     if enprod>maxprod then maxprod=enprod end   --record highest energy production
  136.     turbinestats[i]={on,activ,hold,FFRM,MEFR,speed,enprod,maxprod}  --save turbine stats to array
  137.     if not tb.getActive() then running=false end    --if turbine is off set main loop to exit
  138.   end
  139.  
  140.   --graphs
  141.   gpu.setForeground(0xFFFFFF)
  142.   gpu.setBackground(0x990099)
  143.   gpu.fill(1,1,cappct,1," ")
  144.   gpu.set(1,1,""..cappct.."%")
  145.  
  146.   for tick=1,numreactors do
  147.     rodlvl,minrodlvl,ftemp,ctemp,reavtiv,enprod,maxprod=reactorstats[tick][1],reactorstats[tick][2],reactorstats[tick][3],reactorstats[tick][4],reactorstats[tick][5],reactorstats[tick][6],reactorstats[tick][7]
  148.     gpu.setForeground(0xFFFFFF)
  149.     if tick<=20 then x=2 end
  150.     if tick>20 and tick<=40 then x=10 end
  151.     gpu.setBackground(0xAAAAAA)
  152.     y=math.floor(6*tick-4)
  153.     gpu.fill(y,x,5,7," ")
  154.     gpu.set(y+1,x,""..tick)
  155.     gpu.set(y,x+6,""..rodlvl)
  156.     gpu.setBackground(0x000000)
  157.     gpu.fill(y+1,x+1,3,5," ")
  158.    
  159.     gpu.setBackground(0x00FF00)
  160.     gpu.set(y,x," ")
  161.     if rodlvl<100 then gpu.setBackground(0x00FF00) end
  162.     if rodlvl==100 then gpu.setBackground(0xFF0000) end
  163.     gpu.set(y+4,x," ")
  164.    
  165.     gpu.setBackground(0x0000AA)
  166.     if ftemp>20 then gpu.set(y+1,x+5," ") end
  167.     if ftemp>=500 then gpu.set(y+1,x+4," ") end
  168.     if ftemp>=1000 then gpu.set(y+1,x+3," ") end
  169.     if ftemp>=1500 then gpu.set(y+1,x+2," ") end
  170.     if ftemp>=2000 then gpu.setBackground(0xAA0000) gpu.set(y+1,x+1," ") end
  171.    
  172.     gpu.setBackground(0x0000AA)
  173.     if ctemp>20 then gpu.set(y+2,x+5," ") end
  174.     if ctemp>=500 then gpu.set(y+2,x+4," ") end
  175.     if ctemp>=1000 then gpu.set(y+2,x+3," ") end
  176.     if ctemp>=1500 then gpu.set(y+2,x+2," ") end
  177.     if ctemp>=2000 then gpu.setBackground(0xAA0000) gpu.set(y+2,x+1," ") end
  178.    
  179.     gpu.setBackground(0x009900)
  180.     if enprod>0 then gpu.set(y+3,x+5," ") end
  181.     if enprod>(maxprod/100)*25 then gpu.set(y+3,x+4," ") end
  182.     if enprod>(maxprod/100)*50 then gpu.set(y+3,x+3," ") end
  183.     if enprod>(maxprod/100)*75 then gpu.set(y+3,x+2," ") end
  184.     if enprod>(maxprod/100)*95 then gpu.set(y+3,x+1," ") end
  185.   end
  186.  
  187.   for tick=1,numturbines do
  188.     on,activ,hold,FFRM,MEFR,speed,enprod,maxprod=turbinestats[tick][1],turbinestats[tick][2],turbinestats[tick][3],turbinestats[tick][4],turbinestats[tick][5],turbinestats[tick][6],turbinestats[tick][7],turbinestats[tick][8]
  189.     gpu.setForeground(0xFFFFFF)
  190.     if tick<=20 then x=18 end
  191.     if tick>20 and tick<=40 then x=26 end
  192.     gpu.setBackground(0xAAAAAA)
  193.     y=math.floor(6*tick-4)
  194.     gpu.fill(y,x,5,7," ")
  195.     gpu.set(y+1,x,""..tick)
  196.     gpu.set(y,x+6,""..speed)
  197.     gpu.setBackground(0x000000)
  198.     gpu.fill(y+1,x+1,3,5," ")
  199.  
  200.     if on then gpu.setBackground(0x00FF00) end
  201.     if not on then gpu.setBackground(0xFF0000) end
  202.     gpu.set(y,x," ")
  203.     if activ then gpu.setBackground(0x00FF00) end
  204.     if not activ then gpu.setBackground(0xFF0000) end
  205.     gpu.set(y+4,x," ")
  206.  
  207.     if speed>0 then gpu.setBackground(0x0000AA) gpu.set(y+1,x+5," ") end
  208.     if speed>=750 then gpu.setBackground(0x00AA00) gpu.set(y+1,x+4," ") end
  209.     if speed>=1100 then gpu.setBackground(0x0000AA) gpu.set(y+1,x+3," ") end
  210.     if speed>=1650 then gpu.setBackground(0x00AA00) gpu.set(y+1,x+2," ") end
  211.     if speed>1850 then gpu.setBackground(0xAA0000) gpu.set(y+1,x+1," ") end
  212.  
  213.     tbppct=math.floor(((enprod/maxprod)*100)/20)
  214.  
  215.     gpu.setBackground(0xFF0000)
  216.     if FFRM==0 then gpu.set(y+2,x+5," ") end
  217.     gpu.setBackground(0x0000FF)
  218.     if hold==900 then gpu.set(y+2,x+4," ") end
  219.     if hold==1800 then gpu.set(y+2,x+2," ") end
  220.  
  221.     gpu.setBackground(0x009900)
  222.     if tbppct>0 then gpu.set(y+3,x+5," ") end
  223.     if tbppct>1 then gpu.set(y+3,x+4," ") end
  224.     if tbppct>2 then gpu.set(y+3,x+3," ") end
  225.     if tbppct>3 then gpu.set(y+3,x+2," ") end
  226.     if tbppct>4 then gpu.set(y+3,x+1," ") end
  227.   end
  228.  
  229.   rc=Reactors[1]
  230.   if not rc.getActive() then break end
  231.   if computer.uptime()<timer then os.sleep(timer-computer.uptime()) end
  232. end --end of main loop
  233. for i=1,numreactors do
  234.   rc=Reactors[i]
  235.   rc.setAllControlRodLevels(100)    --drop all rods
  236.   rc.setActive(false)               --turn off all reactors
  237. end
  238.  
  239. for i=1,numturbines do
  240.   tb=Turbines[i]
  241.   tb.setFluidFlowRateMax(0)         --cut all steam flow
  242.   tb.setInductorEngaged(false)      --turn off all coils
  243.   tb.setActive(false)               --turn off all turbines
  244. end
  245. gpu.setResolution(gpu.maxResolution())
  246. gpu.setBackground(0x000000)
  247. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement