Advertisement
Shaka01

Draconic Reactor Controller

Nov 11th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.54 KB | None | 0 0
  1. ---user input required
  2. ---connect both flow gates via a modem, right click modem to activate and see name
  3. local injector = "flow_gate_3"
  4. local output = "flow_gate_2"
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. ---define constants
  14. local maxTemp = 10000
  15. local targetTemp = 7000
  16. local fieldStrengthMin = 15
  17. local fieldStrengthMax = 25
  18. local energySaturationMin = 15
  19. local energySaturationMax = 25
  20. -- initialize variables
  21. local reactor = nil
  22. local reactorName = nil
  23. local monitor = nil
  24. local inCheck = false
  25. local outCheck = false
  26. local warmup = false
  27. local readyForActivation = false
  28. local inputEnergy = 1300000
  29. local outputEnergy = 3000000
  30. local tempStable = false
  31. local satStable = false
  32. local shieldStable = false
  33.  
  34. ---get API
  35. if fs.exists("API") == false then
  36. shell.run("pastebin", "get", "EzkfU5ZM", "API")
  37. end
  38. shaka = require("API")
  39.  
  40. ---connect monitor and reactor
  41. availableConnections = peripheral.getNames()
  42. for k, v in pairs(availableConnections) do
  43.     if shaka.stringFind(v, "reactor") then
  44.         reactorName = v
  45.         reactor = peripheral.wrap(v)
  46.     end
  47.     if shaka.stringFind(v, "monitor") then
  48.         monitor = peripheral.wrap(v)
  49.     end
  50. end
  51. shaka.changeColors(colors.black, colors.white)
  52. shaka.clearScreen()
  53.  
  54. ---check monitor and reactor connection success
  55. if monitor == nil then
  56.     printError("No monitor connected, connect one via modem and hit any key.")
  57.     os.pullEvent("key")
  58.     os.reboot()
  59. else
  60.     shaka.changeColors(colors.black, colors.green)
  61.     print("> Monitor connected")
  62. end
  63.  
  64. if reactor == nil then
  65.     printError("No reactor connected, connect one via modem and hit any key.")
  66.     os.pullEvent("key")
  67.     os.reboot()
  68. else
  69.     shaka.changeColors(colors.black, colors.green)
  70.     print("> Reactor initialized.")
  71. end
  72. monitor.setBackgroundColor(colors.black)
  73. monitor.clear()
  74.  
  75. ---check if names were entered correctly and connections are established
  76. for k, v in pairs(availableConnections) do
  77.     if v == injector then
  78.         inCheck = true
  79.     elseif v == output then
  80.         outCheck = true
  81.     end
  82. end
  83. if inCheck == false or outCheck == false then
  84.     term.clear()
  85.     shaka.changeColors(colors.black, colors.red)
  86.     shaka.centerText("Please configure! Opening file..", 3, term)
  87.  
  88.     for i = 1, 3 do
  89.         shaka.changeColors(colors.black, colors.yellow)
  90.         shaka.centerText("in " ..4-i, 5, term)
  91.         sleep(1)
  92.     end
  93.     shell.run("edit", "startup")
  94. end
  95.  
  96. --wrap remaining peripherals
  97. local energyIn = peripheral.wrap(injector)
  98. local energyOut = peripheral.wrap(output)
  99.  
  100.  
  101. -- for k, v in pairs(peripheral.getMethods(reactorName)) do
  102.     -- print(v)
  103. -- end
  104.  
  105. local function updateMonitor(message, colorBG, line, colorFG)
  106.     monitor.setCursorPos(1, line)
  107.     if colorFG == nil then
  108.         colorFG = colors.black
  109.     end
  110.     shaka.changeColors(colorBG, colorFG, monitor)
  111.     monitor.clearLine()
  112.     monitor.write(message)
  113. end
  114.  
  115. local function checkReadyToStart()
  116. local running = false
  117. ---check if reactor is ready to activate
  118.     for k, v in pairs(reactor.getReactorInfo()) do
  119.         -- print(k, v)
  120.         if k == "failSafe" and v == false then
  121.             reactor.toggleFailSafe()
  122.         elseif k == "status" and v == "warming_up" then
  123.             warmup = true
  124.             energyOut.setSignalLowFlow(outputEnergy)
  125.         elseif k == "status" and v =="running" then
  126.             local currEnergy = energyOut.getSignalLowFlow()
  127.             local currInput = energyIn.getSignalLowFlow()
  128.             if currEnergy > outputEnergy then
  129.                 -- energyOut.setSignalLowFlow(currEnergy)
  130.                 outputEnergy = currEnergy
  131.             end
  132.             if currInput > inputEnergy then
  133.                 inputEnergy = currInput
  134.             end
  135.             return true
  136.         elseif k == "temperature" and v >= 2000 then
  137.             readyForActivation = true
  138.         end
  139.     end
  140.  
  141.     if warmup and readyForActivation then
  142.         shaka.changeColors(colors.black, colors.green)
  143.         print("Ready to start reactor")
  144.         return true
  145.     else
  146.         return false
  147.     end
  148. end
  149.  
  150. checkReadyToStart()
  151. energyIn.setSignalLowFlow(inputEnergy)
  152.  
  153. local function checkStability()
  154.     local initialStatus = reactor.getReactorInfo()
  155.     local initialSat = initialStatus.energySaturation
  156.     local initialTemp = initialStatus.temperature
  157.     local initialShield = initialStatus.fieldStrength
  158.    
  159.     sleep(0.5)
  160.    
  161.     local newStatus = reactor.getReactorInfo()
  162.     local newSat = newStatus.energySaturation
  163.     local newTemp = newStatus.temperature
  164.     local newShield = newStatus.fieldStrength
  165.    
  166.     if initialSat < newSat then
  167.         satStable = true
  168.     else
  169.         satStable = false
  170.     end
  171.    
  172.     if initialShield < newShield then
  173.         shieldStable = true
  174.     else
  175.         shieldStable = false
  176.     end
  177.    
  178.     if initialTemp > newTemp and newTemp < targetTemp then
  179.         tempStable = true
  180.     else
  181.         tempStable = false
  182.     end
  183.    
  184.     if satStable and shieldStable and tempStable then
  185.         return true
  186.     else
  187.         return false
  188.     end
  189. end
  190.  
  191.  
  192. local function changePower(amount)
  193.     outputEnergy = outputEnergy * amount
  194.     energyOut.setSignalLowFlow(outputEnergy)
  195. end
  196.  
  197. local function changeInput(amount)
  198.     inputEnergy = inputEnergy * amount
  199.     energyIn.setSignalLowFlow(inputEnergy)
  200. end
  201.  
  202. function round(num)
  203.     return tonumber(string.format("%.1f", num))
  204. end
  205.  
  206.  
  207. function doitall()
  208.     while true do
  209.         local stable = false
  210.         local data = reactor.getReactorInfo()
  211.         local fillPercent = (round(data.energySaturation/10000000))
  212.         -- local shieldPercent = (round(data.fieldStrength/1000000))
  213.         local shield = (data.fieldStrength/data.maxFieldStrength) * 100
  214.         local shieldPercent = (round(shield))
  215.        
  216.         ----safety stuff
  217.         if data.status == "running" and (shieldPercent < 10 or fillPercent < 10 or data.temperature > maxTemp) then
  218.             reactor.stopReactor()
  219.             print("EMERGENCY STOP")
  220.         end
  221.        
  222.         ----state dependant scripts
  223.         if data.status == "running" then
  224.             if checkStability() then stable = true end
  225.            
  226.             if stable and fillPercent >= energySaturationMax and shieldPercent >= fieldStrengthMax and data.status == "running" then
  227.                 updateMonitor("Output ++", colors.gray, 8, colors.cyan)
  228.                 changePower(1.05)
  229.                 print("raised output to "..outputEnergy)
  230.             end
  231.            
  232.             -- if stable and fillPercent - shieldPercent > 1 then
  233.                 -- print("Added input")
  234.                 -- updateMonitor("Input +", colors.gray, 8, colors.cyan)
  235.                 -- changeInput(1.01)
  236.             -- elseif stable and shieldPercent - fillPercent > 1 then
  237.                 -- updateMonitor("Input -", colors.gray, 8, colors.cyan)
  238.                 -- changeInput(0.99)
  239.                 -- print("Reduced input")
  240.             -- end
  241.            
  242.             -- if stable and shieldPercent < fillPercent and fillPercent > energySaturationMax and shieldPercent > fieldStrengthMax then
  243.                 -- changePower(1.01)
  244.                 -- changeInput(1.005)
  245.                 -- print("Raised output slightly")
  246.                 -- updateMonitor("Output +", colors.gray, 8, colors.cyan)
  247.             -- end
  248.            
  249.            
  250.             local en = math.floor(outputEnergy)
  251.             local niceNumber = shaka.formatNumber(en)
  252.             local x, y = monitor.getSize()
  253.            
  254.             updateMonitor("+ "..niceNumber, colors.gray, 1, colors.yellow)
  255.             updateMonitor("= ".. shaka.formatNumber(math.floor(en - inputEnergy)), colors.gray, 3, colors.green)
  256.             updateMonitor("- "..shaka.formatNumber(math.floor(inputEnergy)), colors.gray, 2, colors.red)
  257.             -- updateMonitor(" ", colors.blue, 4, colors.black)
  258.            
  259.             updateMonitor("Shield "..(shieldPercent).."/"..fieldStrengthMax.."%", colors.black, y-2, colors.blue)
  260.             monitor.setCursorPos(x, y-2)
  261.             if shieldStable then
  262.                 monitor.setTextColor(colors.green)
  263.                 monitor.write("^")
  264.             else
  265.                 monitor.setTextColor(colors.red)
  266.                 monitor.write("v")
  267.             end
  268.  
  269.             updateMonitor("Energy "..fillPercent.."/"..energySaturationMax.. "%", colors.black, y-1, colors.purple)
  270.             monitor.setCursorPos(x, y-1)
  271.             if satStable then
  272.                 monitor.setTextColor(colors.green)
  273.                 monitor.write("^")
  274.             else
  275.                 monitor.setTextColor(colors.red)
  276.                 monitor.write("v")
  277.             end
  278.  
  279.             updateMonitor("Temp: "..math.floor(data.temperature), colors.black, y, colors.orange)
  280.            
  281.             monitor.setCursorPos(1, 6)
  282.             if stable == false then
  283.                 updateMonitor("Stabilizing", colors.lightGray, 6, colors.black)
  284.             elseif fillPercent <= energySaturationMax then
  285.                 updateMonitor("Waiting: Energy", colors.gray, 6, colors.purple)
  286.             elseif shieldPercent <= fieldStrengthMax then
  287.                 updateMonitor("Waiting: Shield", colors.gray, 6, colors.blue)
  288.             end
  289.        
  290.        
  291.         elseif data.status == "cooling" or data.status == "stopping" then
  292.             monitor.clear()
  293.             updateMonitor("Reactor cooling down", colors.blue, 6)
  294.            
  295.         elseif data.status == "cold" then
  296.             monitor.clear()
  297.             updateMonitor("Reactor stopped", colors.white, 6)
  298.        
  299.         elseif data.status == "warming_up" then
  300.             monitor.clear()
  301.             if data.temperature < 2000 then
  302.                 updateMonitor("Reactor warming up", colors.orange, 6)
  303.             else
  304.                 updateMonitor("Reactor ready to start", colors.red, 6)
  305.             end
  306.         end
  307.         -- print(data.temperature)
  308.     sleep(1)
  309.     end
  310. end
  311.  
  312. doitall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement