Advertisement
xX_Nobody_Xx

Draconic Reactor Control

Mar 10th, 2021
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.30 KB | None | 0 0
  1. -- I DO NOT take credit for this script it is owned by Electronx3
  2. -- Youtube https://www.youtube.com/watch?v=Vxf3MHo2sA0
  3. -- I simple have it here to have it saved
  4.  
  5. --peripheral config
  6. local sideGateOut = "right"
  7. local sideReactor = "back"
  8.  
  9. --other config
  10. local autoRestart = true
  11. local maxRestartTrys = 3
  12.  
  13.  
  14. --Do not change anything below
  15.  
  16.  
  17. --program info
  18. local version = "1.0.0" --by electronx3 for MC 1.7.10 and 1.12.2
  19.  
  20. --constants
  21. local val_1div3 = 1.0/3.0
  22.  
  23. --emergency shutdown parameters
  24. local maxTemp = 8020.0
  25. local minField = 0.005
  26. local minFuel = 0.18
  27.  
  28. --default parameters
  29. local defaultTemp = 8000.0
  30. local defaultField = 0.01
  31.  
  32. local maxTempInc = 400.0     --max. temperature increase per tick
  33. local maxOutflow = 16000000.0
  34.  
  35. local restartTemp = 7500.0
  36. local restartTrys = 0
  37.  
  38. local chargeInflow = -1
  39. local shutDownField = 0.1
  40.  
  41. --peripherals
  42. local reactor
  43. local gateIn
  44. local gateOut
  45.  
  46. --info
  47. local info
  48.  
  49. local currentField
  50. local currentFuel
  51.  
  52. local stableTicks = 0
  53.  
  54.  
  55. --the program
  56.  
  57. function updateInfo()
  58.     info = reactor.getReactorInfo()
  59.    
  60.     if info == nil then
  61.       error("Reactor has an invalid setup!")
  62.     end
  63.    
  64.     currentField = info.fieldStrength / info.maxFieldStrength
  65.     currentFuel = 1.0 - (info.fuelConversion / info.maxFuelConversion)
  66. end
  67.  
  68. function isEmergency()
  69.     return (info.status == "running" or info.status == "online" or info.status == "stopping") and (info.temperature > maxTemp or currentField < minField or currentFuel < minFuel)
  70. end
  71.  
  72. function calcInflow(targetStrength, fieldDrainRate)
  73.     return fieldDrainRate / (1.0 - targetStrength)
  74. end
  75.  
  76. --creators of the setupPeripherals function: https://github.com/acidjazz/drmon/blob/master/drmon.lua
  77. function setupPeripherals()
  78.     reactor = peripheral.wrap(sideReactor)
  79.     gateIn = periphSearch("flux_gate")
  80.     gateOut = peripheral.wrap(sideGateOut)
  81.     --monitor = periphSearch("monitor")
  82.    
  83.     if reactor == null then
  84.         error("No valid reactor was found!")
  85.     end
  86.     if gateIn == null then
  87.         error("No valid input fluxgate was found!")
  88.     end  
  89.     if gateOut == null then
  90.         error("No valid output fluxgate was found!")
  91.     end
  92.    
  93.     gateIn.setOverrideEnabled(true)
  94.     gateIn.setFlowOverride(0)
  95.     gateOut.setOverrideEnabled(true)
  96.     gateOut.setFlowOverride(0)
  97. end
  98.  
  99. --creators of the periphSearch function: https://github.com/acidjazz/drmon/blob/master/drmon.lua
  100. function periphSearch(type)
  101.    local names = peripheral.getNames()
  102.    local i, name
  103.    for i, name in pairs(names) do
  104.       if peripheral.getType(name) == type then
  105.          return peripheral.wrap(name)
  106.       end
  107.    end
  108.    return null
  109. end
  110.  
  111. function setup()
  112.     if chargeInflow == -1 then
  113.         chargeInflow = 20000000
  114.     end
  115.    
  116.     stableTicks = 0
  117.    
  118.     setupPeripherals()
  119. end
  120.  
  121. function update()
  122.     local newInflow = 0.0
  123.     local newOutflow = 0.0
  124.  
  125.     while true do
  126.         term.clear()
  127.        
  128.         updateInfo()
  129.        
  130.         local isStable = false
  131.        
  132.         if peripheral.wrap(sideGateOut) == null then
  133.             reactor.stopReactor()
  134.             newInflow = calcInflow(shutDownField, info.fieldDrainRate)
  135.             error("Output gate missing!")
  136.         end
  137.        
  138.         if isEmergency() == true then
  139.             reactor.stopReactor()
  140.             newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
  141.             newOutflow = 0.0
  142.         elseif info.status == "cold" or info.status == "offline" then
  143.             newInflow = 0.0
  144.             newOutflow = 0.0
  145.             restartTrys = 0
  146.         elseif info.status == "warming_up" or info.status == "charging" or info.status == "charged" then
  147.             newInflow = chargeInflow
  148.             newOutflow = 0.0
  149.             reactor.activateReactor()
  150.         elseif info.status == "running" or info.status == "online" then
  151.             local temp = info.temperature
  152.            
  153.             if temp > 7994.0 and temp < 8002.0 then
  154.                 stableTicks = stableTicks + 1
  155.                 if stableTicks > 100 then
  156.                     isStable = true
  157.                     print("stable")
  158.                 end
  159.             else
  160.                 stableTicks = 0
  161.             end
  162.        
  163.             if temp < defaultTemp then              
  164.                 local tempInc
  165.                 if temp < defaultTemp - 50.0 then
  166.                     tempInc = math.sqrt(defaultTemp - temp)
  167.                 elseif temp < defaultTemp - 0.5 then
  168.                     tempInc = math.sqrt(defaultTemp - temp)/2.0
  169.                 end
  170.            
  171.                 if tempInc == nil or tempInc < 0.0 then
  172.                     tempInc = 0
  173.                 elseif tempInc > maxTempInc then
  174.                     tempInc = maxTempInc
  175.                 end
  176.                
  177.                
  178.                 local t50 = temp/200.0
  179.                 local convLvl = (info.fuelConversion / info.maxFuelConversion)*1.3 - 0.3
  180.                
  181.                 local y = (t50^4)/(100.0-t50)*(1-convLvl) + 1000.0*(tempInc-convLvl) - 444.7
  182.                 local dSqrt = math.sqrt((50.0*y)^2 + (y/3.0)^3)
  183.                
  184.                 local x            
  185.                 local tmpValRoot = dSqrt - 50.0*y
  186.                 if tmpValRoot > 0.0 then
  187.                     x = math.pow(dSqrt + 50.0*y, val_1div3) - math.pow(tmpValRoot, val_1div3)
  188.                 else
  189.                     x = math.pow(dSqrt + 50.0*y, val_1div3) + math.pow(0.0-tmpValRoot, val_1div3)
  190.                 end
  191.                
  192.                 newOutflow = info.maxEnergySaturation*x/99.0 + info.energySaturation - info.maxEnergySaturation
  193.                 if newOutflow > maxOutflow then
  194.                     newOutflow = maxOutflow
  195.                 end
  196.             else
  197.                 newOutflow = 0.0
  198.             end
  199.        
  200.             if isStable == true then
  201.                 if currentField > defaultField + 0.03 then
  202.                     newInflow = 0.0
  203.                 elseif currentField > defaultField*1.2 then
  204.                     newInflow = calcInflow(defaultField*0.98, info.fieldDrainRate)
  205.                 elseif currentField > defaultField*0.97 then
  206.                     newInflow = calcInflow(defaultField, info.fieldDrainRate)
  207.                 else
  208.                     newInflow = calcInflow(defaultField*1.5, info.fieldDrainRate)
  209.                 end
  210.             else
  211.                 if currentField > shutDownField then
  212.                     newInflow = calcInflow(shutDownField, info.fieldDrainRate)
  213.                 else
  214.                     newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
  215.                 end
  216.             end
  217.         elseif info.status == "stopping" then
  218.             if currentField > shutDownField then
  219.                 newInflow = calcInflow(shutDownField, info.fieldDrainRate)
  220.             else
  221.                 newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
  222.             end
  223.             newOutflow = 0.0
  224.            
  225.             if autoRestart and info.temperature < restartTemp and restartTrys < maxRestartTrys then
  226.                 restartTrys = restartTrys + 1
  227.                 reactor.activateReactor()
  228.             end
  229.         end
  230.        
  231.         if newInflow < 0.0 then
  232.             newInflow = 0.0
  233.         end
  234.         if newOutflow < 0.0 then
  235.             newOutflow = 0.0
  236.         end
  237.        
  238.         if newInflow > 0.0 then
  239.             newInflow = math.floor(newInflow)
  240.         else
  241.             newInflow = 0
  242.         end
  243.         if newOutflow > 0.0 then
  244.             newOutflow = math.floor(newOutflow)
  245.         else
  246.             newOutflow = 0
  247.         end
  248.         gateIn.setFlowOverride(newInflow)
  249.         gateOut.setFlowOverride(newOutflow)
  250.        
  251.         print("version: " .. version)
  252.         if isStable then
  253.             print("status:  " .. info.status .. " (stable)")
  254.         else
  255.             print("status:  " .. info.status)
  256.         end
  257.         print("temp:      " .. math.floor(info.temperature) .. " K")
  258.         print("inflow:    " .. newInflow .. " RF/t")
  259.         print("outflow:   " .. newOutflow .. " RF/t")
  260.         print("gain:      " .. math.floor((newOutflow - newInflow)/100.0)/10.0 .. " kRF/t")
  261.        
  262.         sleep(0.02)
  263.     end
  264. end
  265.  
  266.  
  267. --------------------------
  268.  
  269. function main()
  270.     setup()
  271.     update()
  272. end
  273.  
  274. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement