Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local configPath = "config"
- if not fs.exists("/logo") then
- shell.run("pastebin get DhmpXRyw logo")
- end
- if not fs.exists("/editor") then
- shell.run("pastebin get z9RARx0t editor")
- end
- local logoImg = paintutils.loadImage("/logo")
- local termSizeX, termSizeY = term.getSize()
- -- ========= 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 modulo reattore...")
- sleep(0.3)
- local config = loadConfig()
- if not config then
- local validSides = {left = true, right = true, top = true, bottom = true, front = true, back = true}
- term.setTextColor(colors.red)
- print("Configurazione non trovata. Creazione guidata...\n")
- local myID, serverID, outputSide
- repeat
- write("ID di questo reattore: ")
- myID = tonumber(read())
- until myID
- repeat
- write("ID del server: ")
- serverID = tonumber(read())
- until serverID
- repeat
- write("Lato per l'uscita (left/right/top/bottom/front/back): ")
- outputSide = read()
- until validSides[outputSide]
- config = {
- id = myID,
- server_id = serverID,
- output_side = outputSide,
- 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
- if not config.output_side then
- config.output_side = "top"
- 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 reader, readerSide = findPeripheral("nuclearReader")
- if not reader then
- error("Advanced Nuclear Information Reader non trovato!")
- end
- term.setTextColor(colors.lime)
- print("✔ Sensore su " .. readerSide)
- sleep(0.4)
- term.setTextColor(colors.yellow)
- print("\nIn attesa di comandi dal server " .. config.server_id .. "...")
- sleep(1)
- term.clear()
- term.setCursorPos(1, 1)
- -- ========ANIMATIONS=========
- local function showBlack()
- term.setBackgroundColor(colors.black)
- term.clear()
- end
- local function showHome()
- term.clear()
- if logoImg then
- paintutils.drawImage(logoImg, 1, 1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.magenta)
- term.setCursorPos(termSizeX/2 - 5, termSizeY/2+5)
- term.write("G.B Enterprise")
- term.setTextColor(colors.cyan)
- term.setCursorPos(termSizeX/2 - 7, termSizeY/2+6)
- term.write("Nuclear Department")
- end
- end
- local function animateLoad(times, delay)
- for i = 0, times, 1 do
- showHome()
- sleep(delay)
- showBlack()
- sleep(delay)
- end
- term.setCursorPos(0,0)
- end
- -- ========= FUNZIONE PULSO =========
- local function pulseColor(colorMask, duration)
- rs.setBundledOutput(config.output_side, colorMask)
- sleep(duration or 1)
- rs.setBundledOutput(config.output_side, 0)
- end
- -- ========= FUNZIONE DATI =========
- local function leggiDati()
- local _, _, _, info = reader.get(1)
- local _, _, _, infoShunt = reader.get(2)
- local inputMask = rs.getBundledInput(config.output_side)
- local scram = (bit.band(inputMask, colors.lightBlue) ~= 0)
- local data = {
- id = config.id,
- temp = tonumber(info["heat"] or 0),
- maxHeat = tonumber(info["maxHeat"] or 1),
- maxOutput = tonumber(info["output"] or 0),
- effectiveOutput = tonumber(infoShunt["average"] or 0),
- active = info["reactorPoweredB"] or false,
- SCRAMState = scram
- }
- return data
- end
- -- ========= LOOP PRINCIPALE =========
- animateLoad(2,1)
- print("Aspetto master...")
- while true do
- term.setTextColor(colors.white)
- local sender, msgTmp = rednet.receive()
- local msg = textutils.unserialize(msgTmp)
- if sender == config.server_id and type(msg) == "table" then
- if msg.command == "reactor_info" then
- term.setTextColor(colors.lightBlue)
- print("Richiesta dati ricevuta dal server.")
- local data = leggiDati()
- term.setTextColor(colors.cyan)
- print(string.format("Temp:%d Max:%d EU:%d MAXEU:%d Stato:%s SCRAM:%s", data.temp, data.maxHeat,
- data.effectiveOutput, data.maxOutput, tostring(data.active), tostring(data.SCRAMState)))
- term.setTextColor(colors.yellow)
- print("Invio dati al server...")
- local result = textutils.serialize({
- type = "info",
- data = data
- })
- rednet.send(config.server_id, result)
- term.setTextColor(colors.lime)
- print("Dati inviati con successo!\n")
- elseif msg.command == "reactor_control" then
- term.setTextColor(colors.blue)
- print("Comando di controllo ricevuto.")
- local state = msg.newState
- if type(state) == "boolean" then
- if state then
- pulseColor(colors.green, 1)
- else
- pulseColor(colors.red, 1)
- end
- local result = textutils.serialize({
- type = "control_result",
- success = true
- })
- rednet.send(config.server_id, result)
- end
- elseif msg.command == "reset_SCRAM" then
- term.setTextColor(colors.orange)
- print("[SCRAM] Reset SCRAM")
- pulseColor(colors.orange, 1)
- end
- end
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment