Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local configPath = "config"
- -- ========= TROVA PERIFERICHE =========
- local function findPeripheral(t)
- for _, side in ipairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == t then
- return peripheral.wrap(side), side
- end
- end
- return nil
- end
- -- ========= CONFIGURAZIONE =========
- local function loadConfig()
- if not fs.exists(configPath) then return nil end
- local f = fs.open(configPath,"r")
- local data=f.readAll() f.close()
- local func=loadstring(data)
- if func then return func() else error("Errore nel config") end
- end
- term.setTextColor(colors.yellow)
- print("Avvio centrale di controllo...")
- sleep(0.3)
- local config = loadConfig()
- if not config then
- term.setTextColor(colors.white)
- print("Configurazione non trovata. Creazione guidata...\n")
- local validColors = {}
- for k, v in pairs(colors) do
- validColors[k] = v
- end
- local reactors = {}
- while true do
- local id
- local color
- write("Nome reattore: ") local name = read()
- repeat
- write("ID rednet: ") id = tonumber(read())
- until id
- repeat
- write("Colore asseganto al reattore(white,blue ...): \n") color = read()
- until validColors[color]
- table.insert(reactors,{name=name,id=id,color = color})
- if table.maxn(reactors) == 8 then break end
- write("Altro reattore? (s/n): ") if read():lower()~="s" then break end
- end
- config = {reactors=reactors, update_interval=2}
- local f = fs.open(configPath,"w")
- f.write("return " .. textutils.serialize(config))
- f.close()
- end
- if type(config.update_interval)~="number" then config.update_interval=2 end
- term.setTextColor(colors.cyan)
- print("\n[ Inizializzazione periferiche ]")
- sleep(0.3)
- local modem, modemSide = findPeripheral("modem")
- if not modem then error("Nessun modem trovato!") end
- rednet.open(modemSide)
- term.setTextColor(colors.lime)
- print("Modem su "..modemSide)
- sleep(0.2)
- local mon, monSide = findPeripheral("monitor")
- if not mon then error("Nessun monitor trovato!") end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- local monitorSizeX, monitorSizeY = mon.getSize()
- term.setTextColor(colors.lime)
- print("Monitor su "..monSide)
- sleep(0.5)
- term.setTextColor(colors.yellow)
- print("Inizializzazione completata.")
- sleep(1)
- term.clear()
- term.setCursorPos(1,1)
- -- ========= VARIABILI =========
- local reactors = {}
- -- ========= UTILITY =========
- local function colorByTemp(p)
- if p < 60 then return colors.green
- elseif p < 85 then return colors.orange
- else return colors.red end
- end
- local function drawBar(x,y,w,perc,color)
- local filled = math.floor(w*perc/100)
- mon.setCursorPos(x,y)
- mon.setBackgroundColor(colors.gray)
- mon.write(string.rep(" ",w))
- mon.setCursorPos(x,y)
- mon.setBackgroundColor(color)
- mon.write(string.rep(" ",filled))
- mon.setBackgroundColor(colors.black)
- end
- local function getReactorsInfo(config)
- local reactors = {}
- for _,r in ipairs(config.reactors) do
- term.setTextColor(colors.lightBlue)
- print("Invio richiesta a "..r.name.." (ID "..r.id..")...")
- local command = textutils.serialize({command = "reactor_info"})
- rednet.send(r.id, command)
- local start=os.clock()
- local sender,msg
- repeat
- sender,msgTmp=rednet.receive(5)
- if msgTmp then
- msg = textutils.unserialize(msgTmp)
- end
- until sender==r.id or os.clock()-start>5
- if sender==r.id and type(msg)=="table" and msg.type=="info" then
- term.setTextColor(colors.lime)
- print("Dati ricevuti da "..r.name)
- reactors[r.id]=msg.data
- else
- term.setTextColor(colors.red)
- print("Nessuna risposta da "..r.name)
- reactors[r.id]=nil
- end
- end
- return reactors
- end
- local function drawUI()
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- mon.setCursorPos((monitorSizeX/2)-12,1)
- mon.write("=== CENTRALE NUCLEARE ===")
- mon.setCursorPos((monitorSizeX/2)-9,2)
- mon.write("Aggiornato: " .. textutils.formatTime(os.time(), true))
- local y=4
- local x=2
- local reactorCount = 0
- local reactorPerPage = math.floor(((monitorSizeY-3)/6)+0.5)
- for _,r in ipairs(config.reactors) do
- reactorCount = reactorCount+1
- local info = reactors[r.id]
- mon.setCursorPos(x,y)
- mon.setTextColor(colors.cyan)
- mon.write(r.name)
- y=y+1
- if info and type(info) == "table" then
- mon.setCursorPos(x+15,y-1)
- local perc = math.floor((info.temp/info.maxHeat)*100)
- if perc >= 80 then
- mon.setTextColor(colors.red) mon.write("STATO: SCRAM")
- elseif info.active then
- mon.setTextColor(colors.lime) mon.write("STATO: ATTIVO")
- else
- mon.setTextColor(colors.red) mon.write("STATO: SPENTO")
- end
- mon.setTextColor(colors.yellow)
- mon.setCursorPos(x+2,y)
- mon.write(string.format("MaxOut: %d/t | Output: %d/t", info.maxOutput, info.effectiveOutput))
- y=y+1
- mon.setCursorPos(x+2,y)
- mon.write(string.format("MaxTemp: %d C | Temp: %d%%", info.maxHeat, perc))
- y=y+1
- drawBar(x+2,y,22,perc+30,colorByTemp(perc))
- y=y+1
- mon.setCursorPos(x+2,y)
- else
- mon.setTextColor(colors.red)
- mon.write(" Nessuna risposta")
- y=y+1
- end
- y=y+2
- if reactorCount% reactorPerPage == 0 then
- mon.setTextColor(colors.white)
- for i = 4, monitorSizeY, 1 do
- mon.setCursorPos(monitorSizeX/2,i)
- mon.write("|")
- end
- x= x+(monitorSizeX/2)
- y = 4
- end
- end
- end
- local previousStates = {}
- local function checkInput()
- local activeColors = rs.getBundledInput("right")
- term.setTextColor(colors.yellow)
- print("---------------------------------\nCheck Inputs...")
- for _, r in ipairs(config.reactors) do
- local isActive = colors.test(activeColors, colors[r.color])
- if isActive then
- term.setTextColor(colors.lime)
- print(r.name .. " ATTIVO")
- else
- term.setTextColor(colors.red)
- print(r.name .. " SPENTO")
- end
- if previousStates[r.id] ~= isActive then
- local command
- if isActive then
- command = textutils.serialize({command = "reactor_control", newState = true})
- else
- command = textutils.serialize({command = "reactor_control", newState = false})
- end
- rednet.send(r.id, command)
- local start=os.clock()
- local sender,msg
- repeat
- sender,msgTmp=rednet.receive(5)
- if msgTmp then
- msg = textutils.unserialize(msgTmp)
- end
- until sender==r.id or os.clock()-start>5
- if msg and msg.success then
- term.setTextColor(colors.lime)
- print(r.name .. " Impostato al nuovo stato")
- previousStates[r.id] = isActive
- else
- term.setTextColor(colors.red)
- print("Nessuna risposta da "..r.name)
- end
- end
- end
- term.setTextColor(colors.yellow)
- print("---------------------------------")
- end
- local function checkSCRAM()
- term.setTextColor(colors.yellow)
- print("---------------------------------\nCheck SCRAM...")
- for _,r in ipairs(config.reactors) do
- local info = reactors[r.id]
- if info and type(info) == "table" then
- local perc = math.floor((info.temp/info.maxHeat)*100)
- local activeColors = rs.getBundledOutput("back")
- if perc >= 80 then
- term.setTextColor(colors.red)
- print(r.name .. " in SCRAM!!")
- if not colors.test(activeColors,colors[r.color]) then
- rs.setBundledOutput("back", activeColors + colors[r.color])
- end
- else
- term.setTextColor(colors.lime)
- print(r.name .. " ok")
- if colors.test(activeColors,colors[r.color]) then
- rs.setBundledOutput("back", activeColors - colors[r.color])
- end
- end
- end
- end
- term.setTextColor(colors.yellow)
- print("---------------------------------")
- end
- -- ========= CICLO DI POLLING =========
- while true do
- reactors = getReactorsInfo(config)
- sleep(0.5)
- checkInput()
- checkSCRAM()
- drawUI()
- term.setTextColor(colors.gray)
- print("Aggiornamento completato.\n")
- sleep(config.update_interval)
- end
Advertisement
Add Comment
Please, Sign In to add comment