tommy2805

Reactor control client

Oct 21st, 2025 (edited)
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. local configPath = "config"
  2. if not fs.exists("/logo") then
  3.     shell.run("pastebin get DhmpXRyw logo")
  4. end
  5. if not fs.exists("/editor") then
  6.     shell.run("pastebin get z9RARx0t editor")
  7. end
  8. local logoImg = paintutils.loadImage("/logo")
  9. local termSizeX, termSizeY = term.getSize()
  10.  
  11. -- ========= TROVA PERIFERICHE =========
  12. local function findPeripheral(t)
  13.     for _, side in ipairs(rs.getSides()) do
  14.         if peripheral.isPresent(side) and peripheral.getType(side) == t then
  15.             return peripheral.wrap(side), side
  16.         end
  17.     end
  18.     return nil
  19. end
  20.  
  21. -- ========= CONFIGURAZIONE =========
  22. local function loadConfig()
  23.     if not fs.exists(configPath) then
  24.         return nil
  25.     end
  26.     local f = fs.open(configPath, "r")
  27.     local data = f.readAll()
  28.     f.close()
  29.     local func = loadstring(data)
  30.     if func then
  31.         return func()
  32.     else
  33.         error("Errore nel config")
  34.     end
  35. end
  36.  
  37. term.setTextColor(colors.yellow)
  38. print("Avvio modulo reattore...")
  39. sleep(0.3)
  40. local config = loadConfig()
  41.  
  42. if not config then
  43.     local validSides = {left = true, right = true, top = true, bottom = true, front = true, back = true}
  44.  
  45.     term.setTextColor(colors.red)
  46.     print("Configurazione non trovata. Creazione guidata...\n")
  47.  
  48.     local myID, serverID, outputSide
  49.     repeat
  50.         write("ID di questo reattore: ")
  51.         myID = tonumber(read())
  52.     until myID
  53.     repeat
  54.         write("ID del server: ")
  55.         serverID = tonumber(read())
  56.     until serverID
  57.     repeat
  58.         write("Lato per l'uscita (left/right/top/bottom/front/back): ")
  59.         outputSide = read()
  60.     until validSides[outputSide]
  61.  
  62.     config = {
  63.         id = myID,
  64.         server_id = serverID,
  65.         output_side = outputSide,
  66.         update_interval = 2
  67.     }
  68.  
  69.     local f = fs.open(configPath, "w")
  70.     f.write("return " .. textutils.serialize(config))
  71.     f.close()
  72. end
  73.  
  74. if type(config.update_interval) ~= "number" then
  75.     config.update_interval = 2
  76. end
  77. if not config.output_side then
  78.     config.output_side = "top"
  79. end
  80.  
  81. term.setTextColor(colors.cyan)
  82. print("\n[ Inizializzazione periferiche ]")
  83. sleep(0.3)
  84.  
  85. local modem, modemSide = findPeripheral("modem")
  86. if not modem then
  87.     error("Nessun modem trovato!")
  88. end
  89. rednet.open(modemSide)
  90. term.setTextColor(colors.lime)
  91. print("✔ Modem su " .. modemSide)
  92. sleep(0.2)
  93.  
  94. local reader, readerSide = findPeripheral("nuclearReader")
  95. if not reader then
  96.     error("Advanced Nuclear Information Reader non trovato!")
  97. end
  98. term.setTextColor(colors.lime)
  99. print("✔ Sensore su " .. readerSide)
  100. sleep(0.4)
  101.  
  102. term.setTextColor(colors.yellow)
  103. print("\nIn attesa di comandi dal server " .. config.server_id .. "...")
  104. sleep(1)
  105. term.clear()
  106. term.setCursorPos(1, 1)
  107.  
  108. -- ========ANIMATIONS=========
  109. local function showBlack()
  110.     term.setBackgroundColor(colors.black)
  111.     term.clear()
  112. end
  113.  
  114. local function showHome()
  115.     term.clear()
  116.     if logoImg then
  117.         paintutils.drawImage(logoImg, 1, 1)
  118.         term.setBackgroundColor(colors.yellow)
  119.  
  120.         term.setTextColor(colors.magenta)
  121.         term.setCursorPos(termSizeX/2 - 5, termSizeY/2+5)
  122.         term.write("G.B Enterprise")
  123.  
  124.         term.setTextColor(colors.cyan)
  125.         term.setCursorPos(termSizeX/2 - 7, termSizeY/2+6)
  126.         term.write("Nuclear Department")
  127.     end
  128. end
  129.  
  130. local function animateLoad(times, delay)
  131.     for i = 0, times, 1 do
  132.         showHome()
  133.         sleep(delay)
  134.         showBlack()
  135.         sleep(delay)
  136.     end
  137.     term.setCursorPos(0,0)
  138. end
  139.  
  140.  
  141. -- ========= FUNZIONE PULSO =========
  142. local function pulseColor(colorMask, duration)
  143.     rs.setBundledOutput(config.output_side, colorMask)
  144.     sleep(duration or 1)
  145.     rs.setBundledOutput(config.output_side, 0)
  146. end
  147.  
  148. -- ========= FUNZIONE DATI =========
  149. local function leggiDati()
  150.     local _, _, _, info = reader.get(1)
  151.     local _, _, _, infoShunt = reader.get(2)
  152.     local inputMask = rs.getBundledInput(config.output_side)
  153.     local scram = (bit.band(inputMask, colors.lightBlue) ~= 0)
  154.  
  155.     local data = {
  156.         id = config.id,
  157.         temp = tonumber(info["heat"] or 0),
  158.         maxHeat = tonumber(info["maxHeat"] or 1),
  159.         maxOutput = tonumber(info["output"] or 0),
  160.         effectiveOutput = tonumber(infoShunt["average"] or 0),
  161.         active = info["reactorPoweredB"] or false,
  162.         SCRAMState = scram
  163.     }
  164.     return data
  165. end
  166.  
  167. -- ========= LOOP PRINCIPALE =========
  168. animateLoad(2,1)
  169. print("Aspetto master...")
  170. while true do
  171.     term.setTextColor(colors.white)
  172.     local sender, msgTmp = rednet.receive()
  173.     local msg = textutils.unserialize(msgTmp)
  174.  
  175.     if sender == config.server_id and type(msg) == "table" then
  176.         if msg.command == "reactor_info" then
  177.             term.setTextColor(colors.lightBlue)
  178.             print("Richiesta dati ricevuta dal server.")
  179.             local data = leggiDati()
  180.             term.setTextColor(colors.cyan)
  181.             print(string.format("Temp:%d  Max:%d  EU:%d MAXEU:%d Stato:%s SCRAM:%s", data.temp, data.maxHeat,
  182.                 data.effectiveOutput, data.maxOutput, tostring(data.active), tostring(data.SCRAMState)))
  183.             term.setTextColor(colors.yellow)
  184.             print("Invio dati al server...")
  185.             local result = textutils.serialize({
  186.                 type = "info",
  187.                 data = data
  188.             })
  189.             rednet.send(config.server_id, result)
  190.             term.setTextColor(colors.lime)
  191.             print("Dati inviati con successo!\n")
  192.  
  193.         elseif msg.command == "reactor_control" then
  194.             term.setTextColor(colors.blue)
  195.             print("Comando di controllo ricevuto.")
  196.             local state = msg.newState
  197.             if type(state) == "boolean" then
  198.                 if state then
  199.                     pulseColor(colors.green, 1)
  200.                 else
  201.                     pulseColor(colors.red, 1)
  202.                 end
  203.                 local result = textutils.serialize({
  204.                     type = "control_result",
  205.                     success = true
  206.                 })
  207.                 rednet.send(config.server_id, result)
  208.             end
  209.  
  210.         elseif msg.command == "reset_SCRAM" then
  211.             term.setTextColor(colors.orange)
  212.             print("[SCRAM] Reset SCRAM")
  213.             pulseColor(colors.orange, 1)
  214.         end
  215.     end
  216.  
  217.     sleep(0.1)
  218. end
  219.  
Advertisement
Add Comment
Please, Sign In to add comment