Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mekanism Fission Reactor Safety Control Script
- -- Monitors reactor conditions and shuts down if dangerous thresholds are exceeded
- -- Configuration
- local LOGIC_ADAPTER_SIDE = "back" -- Change to the side where your logic adapter is connected
- local CHECK_INTERVAL = 2 -- Seconds between checks
- -- Safety thresholds
- local MAX_TEMPERATURE = 1200 -- Kelvin (reactor melts at 1200K)
- local MAX_DAMAGE = 95 -- Percent damage before shutdown
- local MIN_COOLANT = 10 -- Minimum coolant percentage
- local MAX_WASTE = 95 -- Maximum waste percentage before shutdown
- -- Color codes for status display
- local COLOR_SAFE = colors.green
- local COLOR_WARNING = colors.yellow
- local COLOR_DANGER = colors.red
- -- Initialize the logic adapter
- local adapter = peripheral.wrap(LOGIC_ADAPTER_SIDE)
- if not adapter then
- error("Logic Adapter not found on side: " .. LOGIC_ADAPTER_SIDE)
- end
- -- Function to clear the screen
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Function to print colored status
- local function printStatus(text, color)
- if term.isColor() then
- term.setTextColor(color)
- end
- print(text)
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- end
- -- Function to get reactor status
- local function getReactorStatus()
- local status = {
- active = adapter.getStatus(),
- temperature = adapter.getTemperature(),
- damage = adapter.getDamagePercent(),
- coolantFilled = adapter.getCoolantFilledPercentage(),
- heatedCoolantFilled = adapter.getHeatedCoolantFilledPercentage(),
- fuelFilled = adapter.getFuelFilledPercentage(),
- wasteFilled = adapter.getWasteFilledPercentage(),
- }
- return status
- end
- -- Function to check if conditions are dangerous
- local function checkDangerousConditions(status)
- local dangers = {}
- if status.temperature >= MAX_TEMPERATURE then
- table.insert(dangers, "CRITICAL: Temperature at " .. math.floor(status.temperature) .. "K!")
- end
- if status.damage >= MAX_DAMAGE then
- table.insert(dangers, "CRITICAL: Reactor damage at " .. math.floor(status.damage) .. "%!")
- end
- if status.coolantFilled <= MIN_COOLANT then
- table.insert(dangers, "WARNING: Low coolant (" .. math.floor(status.coolantFilled) .. "%)!")
- end
- if status.wasteFilled >= MAX_WASTE then
- table.insert(dangers, "WARNING: Waste tank nearly full (" .. math.floor(status.wasteFilled) .. "%)!")
- end
- return dangers
- end
- -- Function to display reactor information
- local function displayReactorInfo(status, dangers)
- clearScreen()
- print("====================================")
- print(" Fission Reactor Control System")
- print("====================================")
- print("")
- -- Reactor status
- if status.active then
- printStatus("Status: ACTIVE", COLOR_SAFE)
- else
- printStatus("Status: OFFLINE", COLOR_WARNING)
- end
- print("")
- print("Reactor Conditions:")
- print("----------------------------------")
- -- Temperature
- local tempColor = COLOR_SAFE
- if status.temperature >= MAX_TEMPERATURE * 0.9 then
- tempColor = COLOR_DANGER
- elseif status.temperature >= MAX_TEMPERATURE * 0.75 then
- tempColor = COLOR_WARNING
- end
- term.write("Temperature: ")
- printStatus(math.floor(status.temperature) .. "K / " .. MAX_TEMPERATURE .. "K", tempColor)
- -- Damage
- local damageColor = COLOR_SAFE
- if status.damage >= MAX_DAMAGE * 0.9 then
- damageColor = COLOR_DANGER
- elseif status.damage >= MAX_DAMAGE * 0.5 then
- damageColor = COLOR_WARNING
- end
- term.write("Damage: ")
- printStatus(math.floor(status.damage) .. "%", damageColor)
- -- Coolant
- local coolantColor = COLOR_SAFE
- if status.coolantFilled <= MIN_COOLANT then
- coolantColor = COLOR_DANGER
- elseif status.coolantFilled <= MIN_COOLANT * 2 then
- coolantColor = COLOR_WARNING
- end
- term.write("Coolant: ")
- printStatus(math.floor(status.coolantFilled) .. "%", coolantColor)
- -- Heated Coolant
- print("Heated Coolant: " .. math.floor(status.heatedCoolantFilled) .. "%")
- -- Fuel
- print("Fuel: " .. math.floor(status.fuelFilled) .. "%")
- -- Waste
- local wasteColor = COLOR_SAFE
- if status.wasteFilled >= MAX_WASTE then
- wasteColor = COLOR_DANGER
- elseif status.wasteFilled >= MAX_WASTE * 0.75 then
- wasteColor = COLOR_WARNING
- end
- term.write("Waste: ")
- printStatus(math.floor(status.wasteFilled) .. "%", wasteColor)
- print("")
- -- Display dangers
- if #dangers > 0 then
- printStatus("ALERTS:", COLOR_DANGER)
- print("----------------------------------")
- for _, danger in ipairs(dangers) do
- printStatus("* " .. danger, COLOR_DANGER)
- end
- else
- printStatus("All systems nominal", COLOR_SAFE)
- end
- print("")
- print("Press 'q' to quit, 't' to toggle")
- end
- -- Function to safely shutdown reactor
- local function emergencyShutdown(reason)
- adapter.setStatus(false)
- printStatus("\n!!! EMERGENCY SHUTDOWN ACTIVATED !!!", COLOR_DANGER)
- printStatus("Reason: " .. reason, COLOR_DANGER)
- os.sleep(5)
- end
- -- Main control loop
- local function mainLoop()
- print("Starting Fission Reactor Safety System...")
- os.sleep(1)
- while true do
- -- Get current reactor status
- local success, status = pcall(getReactorStatus)
- if not success then
- printStatus("ERROR: Cannot communicate with Logic Adapter!", COLOR_DANGER)
- os.sleep(CHECK_INTERVAL)
- else
- -- Check for dangerous conditions
- local dangers = checkDangerousConditions(status)
- -- Display information
- displayReactorInfo(status, dangers)
- -- If reactor is active and conditions are dangerous, shut it down
- if status.active and #dangers > 0 then
- for _, danger in ipairs(dangers) do
- if string.find(danger, "CRITICAL") then
- emergencyShutdown(danger)
- break
- end
- end
- end
- end
- -- Check for user input (non-blocking)
- local timer = os.startTimer(CHECK_INTERVAL)
- while true do
- local event, param = os.pullEvent()
- if event == "timer" and param == timer then
- break
- elseif event == "char" then
- if param == "q" then
- clearScreen()
- print("Reactor monitoring stopped.")
- return
- elseif param == "t" then
- -- Toggle reactor
- local currentStatus = adapter.getStatus()
- adapter.setStatus(not currentStatus)
- printStatus("\nReactor toggled " .. (currentStatus and "OFF" or "ON"), COLOR_WARNING)
- os.sleep(1)
- break
- end
- end
- end
- end
- end
- -- Run the main loop with error handling
- local success, error = pcall(mainLoop)
- if not success then
- clearScreen()
- printStatus("FATAL ERROR: " .. tostring(error), COLOR_DANGER)
- print("\nAttempting emergency shutdown...")
- pcall(function() adapter.setStatus(false) end)
- end
Advertisement
Add Comment
Please, Sign In to add comment