Advertisement
electronx3

DraconicControl-1.0.0

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