mlard811

reactor_control

Feb 1st, 2026
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.53 KB | None | 0 0
  1. -- Mekanism Fission Reactor Safety Control Script
  2. -- Monitors reactor conditions and shuts down if dangerous thresholds are exceeded
  3.  
  4. -- Configuration
  5. local LOGIC_ADAPTER_SIDE = "back"  -- Change to the side where your logic adapter is connected
  6. local CHECK_INTERVAL = 2  -- Seconds between checks
  7.  
  8. -- Safety thresholds
  9. local MAX_TEMPERATURE = 1200  -- Kelvin (reactor melts at 1200K)
  10. local MAX_DAMAGE = 95  -- Percent damage before shutdown
  11. local MIN_COOLANT = 10  -- Minimum coolant percentage
  12. local MAX_WASTE = 95  -- Maximum waste percentage before shutdown
  13.  
  14. -- Color codes for status display
  15. local COLOR_SAFE = colors.green
  16. local COLOR_WARNING = colors.yellow
  17. local COLOR_DANGER = colors.red
  18.  
  19. -- Initialize the logic adapter
  20. local adapter = peripheral.wrap(LOGIC_ADAPTER_SIDE)
  21.  
  22. if not adapter then
  23.     error("Logic Adapter not found on side: " .. LOGIC_ADAPTER_SIDE)
  24. end
  25.  
  26. -- Function to clear the screen
  27. local function clearScreen()
  28.     term.clear()
  29.     term.setCursorPos(1, 1)
  30. end
  31.  
  32. -- Function to print colored status
  33. local function printStatus(text, color)
  34.     if term.isColor() then
  35.         term.setTextColor(color)
  36.     end
  37.     print(text)
  38.     if term.isColor() then
  39.         term.setTextColor(colors.white)
  40.     end
  41. end
  42.  
  43. -- Function to get reactor status
  44. local function getReactorStatus()
  45.     local status = {
  46.         active = adapter.getStatus(),
  47.         temperature = adapter.getTemperature(),
  48.         damage = adapter.getDamagePercent(),
  49.         coolantFilled = adapter.getCoolantFilledPercentage(),
  50.         heatedCoolantFilled = adapter.getHeatedCoolantFilledPercentage(),
  51.         fuelFilled = adapter.getFuelFilledPercentage(),
  52.         wasteFilled = adapter.getWasteFilledPercentage(),
  53.     }
  54.     return status
  55. end
  56.  
  57. -- Function to check if conditions are dangerous
  58. local function checkDangerousConditions(status)
  59.     local dangers = {}
  60.    
  61.     if status.temperature >= MAX_TEMPERATURE then
  62.         table.insert(dangers, "CRITICAL: Temperature at " .. math.floor(status.temperature) .. "K!")
  63.     end
  64.    
  65.     if status.damage >= MAX_DAMAGE then
  66.         table.insert(dangers, "CRITICAL: Reactor damage at " .. math.floor(status.damage) .. "%!")
  67.     end
  68.    
  69.     if status.coolantFilled <= MIN_COOLANT then
  70.         table.insert(dangers, "WARNING: Low coolant (" .. math.floor(status.coolantFilled) .. "%)!")
  71.     end
  72.    
  73.     if status.wasteFilled >= MAX_WASTE then
  74.         table.insert(dangers, "WARNING: Waste tank nearly full (" .. math.floor(status.wasteFilled) .. "%)!")
  75.     end
  76.    
  77.     return dangers
  78. end
  79.  
  80. -- Function to display reactor information
  81. local function displayReactorInfo(status, dangers)
  82.     clearScreen()
  83.    
  84.     print("====================================")
  85.     print("  Fission Reactor Control System")
  86.     print("====================================")
  87.     print("")
  88.    
  89.     -- Reactor status
  90.     if status.active then
  91.         printStatus("Status: ACTIVE", COLOR_SAFE)
  92.     else
  93.         printStatus("Status: OFFLINE", COLOR_WARNING)
  94.     end
  95.    
  96.     print("")
  97.     print("Reactor Conditions:")
  98.     print("----------------------------------")
  99.    
  100.     -- Temperature
  101.     local tempColor = COLOR_SAFE
  102.     if status.temperature >= MAX_TEMPERATURE * 0.9 then
  103.         tempColor = COLOR_DANGER
  104.     elseif status.temperature >= MAX_TEMPERATURE * 0.75 then
  105.         tempColor = COLOR_WARNING
  106.     end
  107.     term.write("Temperature: ")
  108.     printStatus(math.floor(status.temperature) .. "K / " .. MAX_TEMPERATURE .. "K", tempColor)
  109.    
  110.     -- Damage
  111.     local damageColor = COLOR_SAFE
  112.     if status.damage >= MAX_DAMAGE * 0.9 then
  113.         damageColor = COLOR_DANGER
  114.     elseif status.damage >= MAX_DAMAGE * 0.5 then
  115.         damageColor = COLOR_WARNING
  116.     end
  117.     term.write("Damage: ")
  118.     printStatus(math.floor(status.damage) .. "%", damageColor)
  119.    
  120.     -- Coolant
  121.     local coolantColor = COLOR_SAFE
  122.     if status.coolantFilled <= MIN_COOLANT then
  123.         coolantColor = COLOR_DANGER
  124.     elseif status.coolantFilled <= MIN_COOLANT * 2 then
  125.         coolantColor = COLOR_WARNING
  126.     end
  127.     term.write("Coolant: ")
  128.     printStatus(math.floor(status.coolantFilled) .. "%", coolantColor)
  129.    
  130.     -- Heated Coolant
  131.     print("Heated Coolant: " .. math.floor(status.heatedCoolantFilled) .. "%")
  132.    
  133.     -- Fuel
  134.     print("Fuel: " .. math.floor(status.fuelFilled) .. "%")
  135.    
  136.     -- Waste
  137.     local wasteColor = COLOR_SAFE
  138.     if status.wasteFilled >= MAX_WASTE then
  139.         wasteColor = COLOR_DANGER
  140.     elseif status.wasteFilled >= MAX_WASTE * 0.75 then
  141.         wasteColor = COLOR_WARNING
  142.     end
  143.     term.write("Waste: ")
  144.     printStatus(math.floor(status.wasteFilled) .. "%", wasteColor)
  145.    
  146.     print("")
  147.    
  148.     -- Display dangers
  149.     if #dangers > 0 then
  150.         printStatus("ALERTS:", COLOR_DANGER)
  151.         print("----------------------------------")
  152.         for _, danger in ipairs(dangers) do
  153.             printStatus("* " .. danger, COLOR_DANGER)
  154.         end
  155.     else
  156.         printStatus("All systems nominal", COLOR_SAFE)
  157.     end
  158.    
  159.     print("")
  160.     print("Press 'q' to quit, 't' to toggle")
  161. end
  162.  
  163. -- Function to safely shutdown reactor
  164. local function emergencyShutdown(reason)
  165.     adapter.setStatus(false)
  166.     printStatus("\n!!! EMERGENCY SHUTDOWN ACTIVATED !!!", COLOR_DANGER)
  167.     printStatus("Reason: " .. reason, COLOR_DANGER)
  168.     os.sleep(5)
  169. end
  170.  
  171. -- Main control loop
  172. local function mainLoop()
  173.     print("Starting Fission Reactor Safety System...")
  174.     os.sleep(1)
  175.    
  176.     while true do
  177.         -- Get current reactor status
  178.         local success, status = pcall(getReactorStatus)
  179.        
  180.         if not success then
  181.             printStatus("ERROR: Cannot communicate with Logic Adapter!", COLOR_DANGER)
  182.             os.sleep(CHECK_INTERVAL)
  183.         else
  184.             -- Check for dangerous conditions
  185.             local dangers = checkDangerousConditions(status)
  186.            
  187.             -- Display information
  188.             displayReactorInfo(status, dangers)
  189.            
  190.             -- If reactor is active and conditions are dangerous, shut it down
  191.             if status.active and #dangers > 0 then
  192.                 for _, danger in ipairs(dangers) do
  193.                     if string.find(danger, "CRITICAL") then
  194.                         emergencyShutdown(danger)
  195.                         break
  196.                     end
  197.                 end
  198.             end
  199.         end
  200.        
  201.         -- Check for user input (non-blocking)
  202.         local timer = os.startTimer(CHECK_INTERVAL)
  203.         while true do
  204.             local event, param = os.pullEvent()
  205.            
  206.             if event == "timer" and param == timer then
  207.                 break
  208.             elseif event == "char" then
  209.                 if param == "q" then
  210.                     clearScreen()
  211.                     print("Reactor monitoring stopped.")
  212.                     return
  213.                 elseif param == "t" then
  214.                     -- Toggle reactor
  215.                     local currentStatus = adapter.getStatus()
  216.                     adapter.setStatus(not currentStatus)
  217.                     printStatus("\nReactor toggled " .. (currentStatus and "OFF" or "ON"), COLOR_WARNING)
  218.                     os.sleep(1)
  219.                     break
  220.                 end
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226. -- Run the main loop with error handling
  227. local success, error = pcall(mainLoop)
  228.  
  229. if not success then
  230.     clearScreen()
  231.     printStatus("FATAL ERROR: " .. tostring(error), COLOR_DANGER)
  232.     print("\nAttempting emergency shutdown...")
  233.     pcall(function() adapter.setStatus(false) end)
  234. end
Advertisement
Add Comment
Please, Sign In to add comment