Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local reactor
- local mon
- local alarmPos = 'back'
- local monX
- local monY
- local pathConfig = '/config/reactor.cfg'
- local burnRate = 0
- -------------------FORMATTING-------------------------------
- term.clear()
- function clear()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setCursorPos(1,1)
- end
- --display text on computer's terminal screen
- function draw_text_term(x, y, text, text_color, bg_color)
- term.setTextColor(text_color)
- term.setBackgroundColor(bg_color)
- term.setCursorPos(x,y)
- write(text)
- end
- --display text text on monitor, "mon" peripheral
- function draw_text(x, y, text, text_color, bg_color)
- mon.setBackgroundColor(bg_color)
- mon.setTextColor(text_color)
- mon.setCursorPos(x,y)
- mon.write(text)
- end
- --draw line on computer terminal
- function draw_line(x, y, length, color)
- mon.setBackgroundColor(color)
- mon.setCursorPos(x,y)
- mon.write(string.rep(" ", length))
- end
- --draw line on computer terminal
- function draw_line_term(x, y, length, color)
- term.setBackgroundColor(color)
- term.setCursorPos(x,y)
- term.write(string.rep(" ", length))
- end
- --main line of bar_color as a percentage of minVal/maxVal
- function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line(x, y, length, bg_color) --background bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line(x, y, barSize, bar_color) --progress so far
- end
- --same as above but on the computer terminal
- function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line_term(x, y, length, bg_color) --background bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line_term(x, y, barSize, bar_color) --progress so far
- end
- ----------------------------------------------------------
- -- search for monitor and return it
- function monitorSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "monitor" then
- test = name
- return peripheral.wrap(name)
- else
- --return null
- end
- end
- end
- -- search for reactor and return it
- function reactorSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "fissionReactorLogicAdapter" then
- return peripheral.wrap(name)
- end
- end
- end
- ----- REACTOR SETUP -----------------------------------
- reactor = reactorSearch()
- mon = monitorSearch()
- function operationMonitor()
- if mon == null then
- draw_text_term(1, 7, "Error:", colors.red, colors.black)
- draw_text_term(1, 8, "Could not connect to a monitor. Place a 3x3 advanced monitor", colors.red, colors.black)
- draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
- wait = read()
- setup_wizard()
- else
- monX, monY = mon.getSize()
- draw_text_term(27, 6, "success", colors.lime, colors.black)
- sleep(0.5)
- end
- end
- -- Enable or disable reactor - true = ON | false = OFF
- function enableReactor(state)
- if not reactor then
- return false
- end
- local minPercentageToStart = 20 -- 20% minimum
- local currentWastePercentage = math.floor(reactor.getWasteFilledPercentage() * 100)
- local coolantFilled = math.floor(reactor.getCoolantFilledPercentage()* 100)
- local fuelFilled = math.floor(reactor.getFuelFilledPercentage()* 100)
- local currentTemp = math.floor(reactor.getTemperature())
- -- Condições de segurança para ativação
- local canOperate = coolantFilled >= minPercentageToStart and fuelFilled >= minPercentageToStart and currentWastePercentage <= 90 and currentTemp < 800
- -- Condições de desligamento de emergência
- local emergencyShutdown = currentTemp >= 1000 or
- currentWastePercentage > 90 or
- coolantFilled < minPercentageToStart or
- fuelFilled < minPercentageToStart
- local currentStateReactor = reactor.getStatus()
- if state and canOperate then
- if not currentStateReactor then
- reactor.activate()
- return true
- end
- elseif emergencyShutdown or not state then
- if currentStateReactor then
- reactor.scram()
- return false
- end
- end
- return currentStateReactor
- end
- function controlBurnRate()
- if not reactor then
- return
- end
- local currentTemp = math.floor(reactor.getTemperature())
- local currentPercentDamage = reactor.getDamagePercent()
- local targetBurnRate = burnRate -- valor padrão
- if currentTemp >= 1300 or currentPercentDamage >= 30 then
- targetBurnRate = 0
- elseif currentTemp >= 1200 or currentPercentDamage >= 20 then
- targetBurnRate = math.min(10, burnRate)
- elseif currentTemp >= 1150 or currentPercentDamage >= 10 then
- targetBurnRate = math.min(20, burnRate)
- elseif currentTemp >= 1000 or currentPercentDamage >= 5 then
- targetBurnRate = math.min(30, burnRate)
- end
- if reactor.getActualBurnRate() ~= targetBurnRate then
- reactor.setBurnRate(targetBurnRate)
- end
- end
- function waste()
- if not reactor then
- return
- end
- local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
- local currentStateReactor = reactor.getStatus()
- if currentPercentageWaste >= 90 and currentStateReactor then
- reactor.scram()
- return false
- end
- return true
- end
- -- Loading config
- function loadConfig()
- local config = {}
- sr = fs.open(pathConfig, "r")
- burnRate = tonumber(sr.readLine())
- sr.close()
- end
- -- Set burn rate
- function setBurnRate(rate)
- reactor.setBurnRate(rate)
- end
- -- Waste control
- function waste()
- if not reactor then
- return
- end
- local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
- local currentStateReactor = reactor.getStatus()
- if currentPercentageWaste >= 90 and currentStateReactor then
- reactor.scram()
- return false
- end
- return true
- end
- -- Enable or disable siren melt alert
- function meltAlert(state)
- redstone.setOutput(alarmPos, state)
- end
- enableReactor(false) -- Reactor OFF
- -- Reactor monitoring
- function homepage()
- while true do
- if not reactor then
- draw_text_term(1, 1, "Error: Reactor not found!", colors.red, colors.black)
- sleep(1)
- return
- end
- local currentStateReactor = reactor.getStatus()
- local currentPercentageFuel = math.floor(reactor.getFuelFilledPercentage() * 100)
- local currentPercentageCoolant = math.floor(reactor.getCoolantFilledPercentage() * 100)
- local currentPercentageWaste = math.floor(reactor.getWasteFilledPercentage() * 100)
- local currentTemp = math.floor(reactor.getTemperature())
- local currentPercentDamage = reactor.getDamagePercent()
- local tempVariable = 950
- local shouldBeRunning = currentTemp < tempVariable
- local shouldBeOff = currentTemp >= tempVariable
- if shouldBeRunning then
- enableReactor(true)
- elseif shouldBeOff then
- enableReactor(false)
- end
- -- Control burn rate
- controlBurnRate()
- -- Waste control
- waste()
- -- Melt Alert
- if currentTemp >= 1200 or currentPercentDamage >= 30 then
- meltAlert(true)
- draw_text(1,14,'Melt Alert!!!', colors.red, colors.black)
- draw_text_term(1,14,'Melt Alert!!!', colors.red, colors.black)
- else
- meltAlert(false)
- end
- -- Display Status
- local stateReactorStr = currentStateReactor and 'ONLINE' or 'OFFLINE'
- term.clear()
- clear()
- draw_line(1, 1, monX, currentStateReactor and colors.lime or colors.red)
- draw_line_term(1, 1, 55, currentStateReactor and colors.lime or colors.red)
- draw_text_term(2,3,'State:', colors.lime, colors.black)
- draw_text_term(9,3,stateReactorStr, colors.white, colors.black)
- draw_text(2,3,'State:', colors.lime, colors.black)
- draw_text(9,3,stateReactorStr, colors.white, colors.black)
- draw_text_term(2,4,'Fuel Level:', colors.lime, colors.black)
- draw_text_term(14,4,currentPercentageFuel ..'%', colors.white, colors.black)
- draw_text(2, 4, "Fuel Level:", colors.lime, colors.black)
- draw_text(14, 4, currentPercentageFuel ..'%', colors.white, colors.black)
- draw_text_term(2,5,'Coolant:', colors.lime, colors.black)
- draw_text_term(11,5,currentPercentageCoolant .. '%', colors.white, colors.black)
- draw_text(2, 5, "Coolant:", colors.lime, colors.black)
- draw_text(11, 5, currentPercentageCoolant .. '%', colors.white, colors.black)
- draw_text_term(2,6,'Temp:', colors.lime, colors.black)
- draw_text_term(8,6,currentTemp .. 'K', colors.white, colors.black)
- draw_text(2, 6, "Temp:", colors.lime, colors.black)
- draw_text(8, 6, currentTemp .. 'K', colors.white, colors.black)
- draw_text_term(2,7,'Damage:', colors.lime, colors.black)
- draw_text_term(10,7,currentPercentDamage..'%', colors.white, colors.black)
- draw_text(2, 7, "Damage:", colors.lime, colors.black)
- draw_text(10, 7, currentPercentDamage..'%', colors.white, colors.black)
- draw_text_term(2,8,'Burn Rate:', colors.lime, colors.black)
- draw_text_term(13,8,reactor.getActualBurnRate() ..' mB/t', colors.white, colors.black)
- draw_text(2, 8, "Burn Rate:", colors.lime, colors.black)
- draw_text(13, 8, reactor.getActualBurnRate() ..' mB/t', colors.white, colors.black)
- draw_text_term(2,9,'Heating Rate:', colors.lime, colors.black)
- draw_text_term(16,9,reactor.getHeatingRate() ..' mB/t', colors.white, colors.black)
- draw_text(2, 9, "Heating Rate:", colors.lime, colors.black)
- draw_text(16, 9, reactor.getHeatingRate() ..' mB/t', colors.white, colors.black)
- draw_text_term(2,10,'Waster Level:', colors.lime, colors.black)
- draw_text_term(16,10,currentPercentageWaste ..'%', colors.white, colors.black)
- draw_text(2, 10, "Waster Level:", colors.lime, colors.black)
- draw_text(16, 10, currentPercentageWaste ..'%', colors.white, colors.black)
- sleep(0.5)
- end
- end
- -- Inicialização
- function initialize()
- reactor = reactorSearch()
- if not reactor then
- error("No reactor found!")
- return false
- end
- mon = monitorSearch()
- loadConfig()
- operationMonitor()
- -- Começa com o reator desligado por segurança
- enableReactor(false)
- return true
- end
- -- Start the program
- if initialize() then
- homepage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement