Advertisement
Guest User

DR.lua

a guest
Jun 26th, 2024
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.34 KB | None | 0 0
  1. function PeriphSearch(type)
  2.     local names = peripheral.getNames()
  3.     for _, name in pairs(names) do
  4.         if peripheral.getType(name) == type then
  5.             return peripheral.wrap(name)
  6.         end
  7.     end
  8.     return nil
  9. end
  10.  
  11. ---@diagnostic disable: undefined-global
  12.  
  13. -- Peripherals
  14. local monitor = PeriphSearch("monitor")
  15. local reactor = peripheral.wrap("back")
  16. local output_gate = peripheral.wrap("right")
  17. local input_gate = peripheral.wrap("flux_gate_0")
  18.  
  19. -- Important variables
  20. local temperature
  21. local fuelAmmountLeft
  22. local energySaturation
  23. local fieldStrength
  24. local status
  25. local generationRate
  26. local autoControl
  27. local fieldDrainRate
  28. local fuelConversionRate
  29.  
  30. local tempChange = 0 -- It has to be initialized
  31.  
  32. local emergencyCharge = false
  33.  
  34. local currentAction
  35.  
  36.  
  37. -- safety / autocontrol variables (adjustable however you like)
  38.  
  39. local maxSafeTemperature = 8000 -- Maximum safe temperature in Celsius
  40. local minFieldStrength = 0.3 -- Minimum field strength to trigger input increase
  41. local minEnergySaturation = 0.075 -- Minimum energy saturation to trigger input increase
  42. local minFuelAmmount = 0.1 -- Mininum ammount of fuel required to operate
  43. local maxTempChange = 250 --Maximum change of temperature per second (negative means temp is falling per second)
  44.  
  45. --startup calls
  46.  
  47. if monitor == nil then error("No Monitor found!") end
  48. if reactor == nil then error("Invalid reactor setup!") end
  49. if output_gate == nil then error("No power output found!") end
  50. if input_gate == nil then error("No power input found!") end
  51.  
  52. input_gate.setOverrideEnabled(true)
  53. output_gate.setOverrideEnabled(true)
  54.  
  55. term.redirect(monitor)
  56. monitor.clear()
  57. monitor.setCursorPos(1, 1)
  58.  
  59. reactor.setFailSafe(true)
  60.  
  61.  
  62. -- Other Variables
  63. local output_rate = 500000
  64. local input_rate = 300000
  65.  
  66. local bar_length = 25
  67.  
  68. local tempColor = colors.green
  69. local autoColor = colors.red
  70.  
  71. -- Startup checks
  72. local x_size, y_size = monitor.getSize()
  73. if x_size ~= 29 or y_size ~= 19 then
  74.     error("Invalid screen size (3x3 required)")
  75. end
  76.  
  77. function DrawText(x, y, text, textColor)
  78.     monitor.setCursorPos(x, y)
  79.     monitor.setTextColor(textColor)
  80.     monitor.write(text)
  81. end
  82.  
  83. function DrawButtons(y)
  84.     -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
  85.     -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
  86.  
  87.     if autoControl then return end
  88.  
  89.     DrawText(2, y, "<<<", colors.white)
  90.     DrawText(6, y, "<< ", colors.white)
  91.     DrawText(10, y, " < ", colors.white)
  92.  
  93.     DrawText(17, y, " > ", colors.white)
  94.     DrawText(21, y, " >>", colors.white)
  95.     DrawText(25, y, ">>>", colors.white)
  96. end
  97.  
  98. function Buttons()
  99.     while true do
  100.         local _, _, xPos, yPos = os.pullEvent("monitor_touch")
  101.  
  102.         -- input controls
  103.         if yPos == 7 and autoControl ~= true then
  104.             if xPos >= 2 and xPos <= 4 then
  105.                 output_rate = output_rate - 100000
  106.                 if output_rate < 0 then output_rate = 0 end
  107.             elseif xPos >= 6 and xPos <= 8 then
  108.                 output_rate = output_rate - 10000
  109.                 if output_rate < 0 then output_rate = 0 end
  110.             elseif xPos >= 10 and xPos <= 12 then
  111.                 output_rate = output_rate - 1000
  112.                 if output_rate < 0 then output_rate = 0 end
  113.             elseif xPos >= 17 and xPos <= 19 then
  114.                 output_rate = output_rate + 1000
  115.                 if output_rate < 0 then output_rate = 0 end
  116.             elseif xPos >= 21 and xPos <= 23 then
  117.                 output_rate = output_rate + 10000
  118.                 if output_rate < 0 then output_rate = 0 end
  119.             elseif xPos >= 25 and xPos <= 27 then
  120.                 output_rate = output_rate + 100000
  121.                 if output_rate < 0 then output_rate = 0 end
  122.             end
  123.  
  124.         elseif yPos == 10 and autoControl ~= true then
  125.             if xPos >= 2 and xPos <= 4 then
  126.                 input_rate = input_rate - 100000
  127.                 if input_rate < 0 then input_rate = 0 end
  128.             elseif xPos >= 6 and xPos <= 8 then
  129.                 input_rate = input_rate - 10000
  130.                 if input_rate < 0 then input_rate = 0 end
  131.             elseif xPos >= 10 and xPos <= 12 then
  132.                 input_rate = input_rate - 1000
  133.                 if input_rate < 0 then input_rate = 0 end
  134.             elseif xPos >= 17 and xPos <= 19 then
  135.                 input_rate = input_rate + 1000
  136.                 if input_rate < 0 then input_rate = 0 end
  137.             elseif xPos >= 21 and xPos <= 23 then
  138.                 input_rate = input_rate + 10000
  139.                 if input_rate < 0 then input_rate = 0 end
  140.             elseif xPos >= 25 and xPos <= 27 then
  141.                 input_rate = input_rate + 100000
  142.                 if input_rate < 0 then input_rate = 0 end
  143.             end
  144.  
  145.         -- startup control
  146.         elseif yPos == 18 then
  147.             if xPos >= 20 and status == "cold" or status == "cooling" then
  148.                 reactor.chargeReactor()
  149.             elseif xPos > 20 and status == "warming_up" then
  150.                 reactor.stopReactor()
  151.             elseif xPos > 2 and xPos < 10 and status == "warming_up" and temperature >= 2000 and fuelAmmountLeft > minFuelAmmount and fieldStrength > minFieldStrength then
  152.                 reactor.activateReactor()
  153.             elseif xPos > 20 and status == "running" then
  154.                 reactor.stopReactor()
  155.             end
  156.  
  157.         -- auto control
  158.         elseif yPos == 5 then
  159.             if xPos > 19 and xPos < 21 then
  160.                 if autoControl == true then
  161.                     autoControl = false
  162.                 else
  163.                     autoControl = true
  164.                 end
  165.             end
  166.  
  167.         -- exit
  168.         elseif xPos > 13 and xPos < 17 and yPos == 1 and status ~= "running" then
  169.             monitor.clear()
  170.             return
  171.         end
  172.     end
  173. end
  174.  
  175. function Render()
  176.     while true do
  177.         -- Reactor Variables
  178.         local ri = reactor.getReactorInfo()
  179.  
  180.         -- values in % (for example energySaturation 0 - 1)
  181.         temperature = ri.temperature
  182.         fuelAmmountLeft = (ri.maxFuelConversion - ri.fuelConversion) / ri.maxFuelConversion
  183.         energySaturation = ri.energySaturation / ri.maxEnergySaturation
  184.         fieldStrength = ri.fieldStrength / ri.maxFieldStrength
  185.         status = ri.status
  186.         generationRate = ri.generationRate
  187.         fieldDrainRate = ri.fieldDrainRate
  188.         fuelConversionRate = ri.fuelConversionRate
  189.  
  190.         -- I figured this factor out by just abit of testing, may be unsafe
  191.         -- Calculates an output based off of some temperature value you give beforehand (in this case maxTemp - 500 so 7500°C)
  192.  
  193.         output_gate.setFlowOverride(output_rate)
  194.         input_gate.setFlowOverride(input_rate)
  195.  
  196.         monitor.clear()
  197.         monitor.setBackgroundColor(colors.black)
  198.  
  199.         -- Only allow exit when reactor is not running
  200.         if status ~= "running" then
  201.             DrawText(14, 1, "Exit", colors.red)
  202.         end
  203.  
  204.         -- Status of reactor draw
  205.         DrawText(2, 2, "Status:", colors.white)
  206.         if status == "cold" then
  207.             DrawText(20, 2, "cold", colors.red)
  208.         elseif status == "warming_up" then
  209.             DrawText(20, 2, "Charging", colors.orange)
  210.         elseif status == "running" then
  211.             DrawText(20, 2, "Active", colors.green)
  212.         elseif status == "stopping" then
  213.             DrawText(20, 2, "Stopping", colors.orange)
  214.         end
  215.  
  216.         DrawText(2, 3, "Temperature:", colors.white)
  217.         DrawText(20, 3, tostring(temperature).."°C", tempColor)
  218.  
  219.         DrawText(2, 4, "Generation:", colors.white)
  220.         DrawText(20, 4, tostring(generationRate), colors.purple)
  221.  
  222.         DrawText(2, 5, "Autocontrol?: ", colors.white)
  223.         DrawText(20, 5, "X", autoColor)
  224.  
  225.         if autoControl == true then
  226.             DrawText(2, 7, "Output: ", colors.white)
  227.             DrawText(20, 7, tostring(output_rate), colors.red)
  228.    
  229.             DrawButtons(7)
  230.    
  231.             DrawText(2, 8, "Input:", colors.white)
  232.             DrawText(20, 8, tostring(input_rate), colors.green)
  233.  
  234.             DrawButtons(10)
  235.         else
  236.             DrawText(2, 6, "Output: ", colors.white)
  237.             DrawText(20, 6, tostring(output_rate), colors.red)
  238.    
  239.             DrawButtons(7)
  240.    
  241.             DrawText(2, 9, "Input:", colors.white)
  242.             DrawText(20, 9, tostring(input_rate), colors.green)
  243.  
  244.             DrawButtons(10)
  245.         end
  246.  
  247.         DrawText(2, 12, "Energy Saturation:", colors.white)
  248.         DrawText(25, 12, tostring(energySaturation*100).."%", colors.white)
  249.  
  250.         monitor.setBackgroundColor(colors.blue)
  251.         paintutils.drawFilledBox(2, 13, 2 + bar_length * energySaturation, 13, colors.blue)
  252.         monitor.setBackgroundColor(colors.black)
  253.  
  254.         DrawText(2, 15, "Field Strength:", colors.white)
  255.         DrawText(25, 15, tostring(fieldStrength*100).."%", colors.white)
  256.  
  257.         monitor.setBackgroundColor(colors.green)
  258.         paintutils.drawFilledBox(2, 16, 2 + bar_length * fieldStrength, 16, colors.green)
  259.         monitor.setBackgroundColor(colors.black)
  260.  
  261.         DrawText(7, 19, currentAction, colors.red)
  262.  
  263.         -- Button draw on different states
  264.         if status == "running" then
  265.             DrawText(23, 18, "Stop", colors.red)
  266.         elseif status == "warming_up" then
  267.             if temperature >= 2000 and fuelAmmountLeft > minFuelAmmount and fieldStrength > minFieldStrength then
  268.                 DrawText(2, 18, "Activate", colors.green)
  269.             end
  270.             DrawText(23, 18, "Stop", colors.red)
  271.  
  272.         elseif status == "cold" or status == "cooling" then
  273.             DrawText(23, 18, "Warm up", colors.orange)
  274.         end
  275.  
  276.         -- auto control
  277.         if autoControl == true then
  278.             -- auto control logic
  279.                 -- Adjust input rate based on field strength
  280.                 input_rate = 2 * fieldDrainRate
  281.  
  282.                 -- Adjust output rate based on temperature and fuel conversion rate
  283.                 -- temp / maxTemp to the power of -1 will result in higher value the furhter the temp is away form the max temp
  284.                 output_rate = math.ceil(((((maxSafeTemperature) * 450 ) / 7) + ((1 / (fuelConversionRate)) * 15000000)))
  285.  
  286.         end
  287.  
  288.         if autoControl then
  289.             autoColor = colors.green
  290.         else autoColor = colors.red
  291.         end
  292.  
  293.         -- Temp color
  294.         if temperature < 2500 then
  295.             tempColor = colors.blue
  296.         elseif temperature > 4000 and temperature < 6000 then
  297.             tempColor = colors.yellow
  298.         elseif temperature > 6000 and temperature < 7000 then
  299.             tempColor = colors.orange
  300.         elseif temperature > 7500 then
  301.             tempColor = colors.red
  302.         end
  303.  
  304.         -- Safety checks:
  305.         if emergencyCharge == true then
  306.             input_rate = 3 * fieldDrainRate
  307.             output_rate = 0
  308.             reactor.stopReactor()
  309.             currentAction = "Emergency shutdown!"
  310.         end
  311.  
  312.         --If temperature rises way too much (250°C per second, defined in the safety variables) then shutdown
  313.         if tempChange >= maxTempChange then
  314.             emergencyCharge = true
  315.         end
  316.  
  317.         if temperature >= maxSafeTemperature then
  318.             emergencyCharge = true
  319.         end
  320.  
  321.         if energySaturation < minEnergySaturation then
  322.             emergencyCharge = true
  323.         end
  324.  
  325.         if fieldStrength < minFieldStrength and status == "running" then
  326.             emergencyCharge = true
  327.         end
  328.  
  329.         if fuelAmmountLeft < minFuelAmmount and status == "running" then
  330.             reactor.stopReactor()
  331.             output_rate = 0
  332.             input_rate = 300000
  333.         end
  334.  
  335.         if temperature < 2000 and status ~= "running" then
  336.             emergencyCharge = false
  337.             input_rate = 300000
  338.             output_rate = 500000
  339.             currentAction = ""
  340.         end
  341.        
  342.         sleep(0.1)
  343.     end
  344. end
  345.  
  346. function CalcTempChange()
  347.     while true do
  348.         -- Temp before 1 second passes
  349.         -- Basically calculates temperature change in 1 second
  350.         local curTemp = temperature
  351.        
  352.         sleep(1)
  353.  
  354.         -- calculated difference
  355.         tempChange = math.ceil(temperature - curTemp)
  356.     end
  357. end
  358.  
  359. parallel.waitForAny(Buttons, Render, CalcTempChange)
  360.  
  361. -- Cleanup
  362. input_gate.setOverrideEnabled(false)
  363. output_gate.setOverrideEnabled(false)
  364.  
  365. --TODO: Seperate autocontrol
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement