tommy2805

Reactor control server V2

Oct 30th, 2025 (edited)
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.01 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 pAHgeAnJ 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 return nil end
  24.     local f = fs.open(configPath,"r")
  25.     local data=f.readAll() f.close()
  26.     local func=loadstring(data)
  27.     if func then return func() else error("Errore nel config") end
  28. end
  29.  
  30. term.setTextColor(colors.yellow)
  31. print("Avvio centrale di controllo...")
  32. sleep(0.3)
  33. local config = loadConfig()
  34. if not config then
  35.     term.setTextColor(colors.white)
  36.     print("Configurazione non trovata. Creazione guidata...\n")
  37.     local validColors = {}
  38.     for k, v in pairs(colors) do
  39.         validColors[k] = v
  40.     end
  41.     local reactors = {}
  42.     while true do
  43.         local id
  44.         local color
  45.         local scramColor
  46.  
  47.         write("Nome reattore: ") local name = read()
  48.         repeat write("ID rednet: ") id = tonumber(read()) until id
  49.         repeat write("Colore principale (white, blue, ...): ") color = read() until validColors[color]
  50.         repeat write("Colore reset SCRAM: ") scramColor = read() until validColors[scramColor]
  51.  
  52.         table.insert(reactors,{name=name,id=id,color=color,scram_color=scramColor})
  53.         if table.maxn(reactors) == 8 then break end
  54.         write("Altro reattore? (s/n): ") if read():lower()~="s" then break end
  55.     end
  56.     config = {reactors=reactors, update_interval=2}
  57.     local f = fs.open(configPath,"w")
  58.     f.write("return " .. textutils.serialize(config))
  59.     f.close()
  60. end
  61. if type(config.update_interval)~="number" then config.update_interval=2 end
  62.  
  63. term.setTextColor(colors.cyan)
  64. print("\n[ Inizializzazione periferiche ]")
  65. sleep(0.3)
  66.  
  67. local modem, modemSide = findPeripheral("modem")
  68. if not modem then error("Nessun modem trovato!") end
  69. rednet.open(modemSide)
  70. term.setTextColor(colors.lime)
  71. print("Modem su "..modemSide)
  72. sleep(0.2)
  73.  
  74. local mon, monSide = findPeripheral("monitor")
  75. if not mon then error("Nessun monitor trovato!") end
  76. mon.setBackgroundColor(colors.black)
  77. mon.setTextColor(colors.white)
  78. mon.clear()
  79. local monitorSizeX, monitorSizeY = mon.getSize()
  80. term.setTextColor(colors.lime)
  81. print("Monitor su "..monSide)
  82. sleep(0.5)
  83.  
  84. term.setTextColor(colors.yellow)
  85. print("Inizializzazione completata.")
  86. sleep(1)
  87. term.clear()
  88. term.setCursorPos(1,1)
  89.  
  90. -- ========= VARIABILI =========
  91. local reactors = {}
  92.  
  93. -- ========ANIMATIONS=========
  94. local function showBlack()
  95.     term.setBackgroundColor(colors.black)
  96.     term.clear()
  97. end
  98.  
  99. local function showHome()
  100.     term.clear()
  101.     if logoImg then
  102.         paintutils.drawImage(logoImg, 1, 1)
  103.         term.setBackgroundColor(colors.yellow)
  104.  
  105.         term.setTextColor(colors.magenta)
  106.         term.setCursorPos(termSizeX/2 - 5, termSizeY/2+5)
  107.         term.write("G.B Enterprise")
  108.  
  109.         term.setTextColor(colors.cyan)
  110.         term.setCursorPos(termSizeX/2 - 7, termSizeY/2+6)
  111.         term.write("Nuclear Department")
  112.     end
  113. end
  114.  
  115. local function animateLoad(times, delay)
  116.     for i = 0, times, 1 do
  117.         showHome()
  118.         sleep(delay)
  119.         showBlack()
  120.         sleep(delay)
  121.     end
  122.     term.setCursorPos(0,0)
  123. end
  124.  
  125. -- ========= UTILITY =========
  126. local function colorByTemp(p)
  127.     if p < 60 then return colors.green
  128.     elseif p < 85 then return colors.orange
  129.     else return colors.red end
  130. end
  131.  
  132. local function drawBar(x,y,w,perc,color)
  133.     local filled = math.floor(w*perc/100)
  134.     mon.setCursorPos(x,y)
  135.     mon.setBackgroundColor(colors.gray)
  136.     mon.write(string.rep(" ",w))
  137.     mon.setCursorPos(x,y)
  138.     mon.setBackgroundColor(color)
  139.     mon.write(string.rep(" ",filled))
  140.     mon.setBackgroundColor(colors.black)
  141. end
  142.  
  143. local function getReactorsInfo(config)
  144.     local reactors = {}
  145.     for _,r in ipairs(config.reactors) do
  146.         term.setTextColor(colors.lightBlue)
  147.         print("Invio richiesta a "..r.name.." (ID "..r.id..")...")
  148.         local command = textutils.serialize({command = "reactor_info"})
  149.         rednet.send(r.id, command)
  150.         local start=os.clock()
  151.         local sender,msg
  152.         repeat
  153.             sender,msgTmp=rednet.receive(1)
  154.             if msgTmp then
  155.                 msg = textutils.unserialize(msgTmp)
  156.             end
  157.         until sender==r.id or os.clock()-start>5
  158.        
  159.  
  160.         if sender==r.id and type(msg)=="table" and msg.type=="info" then
  161.             term.setTextColor(colors.lime)
  162.             print("Dati ricevuti da "..r.name)
  163.             reactors[r.id]=msg.data
  164.         else
  165.             term.setTextColor(colors.red)
  166.             print("Nessuna risposta da "..r.name)
  167.             reactors[r.id]=nil
  168.         end
  169.     end
  170.     return reactors
  171. end
  172.  
  173. local function drawUI()
  174.     mon.setBackgroundColor(colors.black)
  175.     mon.setTextColor(colors.white)
  176.     mon.clear()
  177.     mon.setCursorPos((monitorSizeX/2)-12,1)
  178.     mon.write("=== CENTRALE NUCLEARE ===")
  179.     mon.setCursorPos((monitorSizeX/2)-9,2)
  180.     mon.write("Aggiornato: " .. textutils.formatTime(os.time(), true))
  181.     local y=4
  182.     local x=2
  183.     local reactorCount = 0
  184.     local reactorPerPage = math.floor(((monitorSizeY-3)/6)+0.5)
  185.     for _,r in ipairs(config.reactors) do
  186.         reactorCount = reactorCount+1
  187.         local info = reactors[r.id]
  188.         mon.setCursorPos(x,y)
  189.         mon.setTextColor(colors.cyan)
  190.         mon.write(r.name)
  191.         y=y+1
  192.         if info and type(info) == "table" then
  193.             mon.setCursorPos(x+15,y-1)
  194.             if info.SCRAMState then
  195.                 mon.setTextColor(colors.red) mon.write("STATO: SCRAM")
  196.             elseif info.active then
  197.                 mon.setTextColor(colors.lime) mon.write("STATO: ATTIVO")
  198.             else
  199.                 mon.setTextColor(colors.red) mon.write("STATO: SPENTO")
  200.             end
  201.             mon.setTextColor(colors.yellow)
  202.             mon.setCursorPos(x+2,y)
  203.             mon.write(string.format("MaxOut: %d/t | Output: %d/t", info.maxOutput, info.effectiveOutput))
  204.             y=y+1
  205.             mon.setCursorPos(x+2,y)
  206.             local perc = math.floor((info.temp/info.maxHeat)*100)
  207.             mon.write(string.format("MaxTemp: %d C | Temp: %d%%", info.maxHeat, perc))
  208.             y=y+1
  209.             drawBar(x+2,y,22,perc+30,colorByTemp(perc))
  210.             y=y+1
  211.             mon.setCursorPos(x+2,y)
  212.  
  213.         else
  214.             mon.setTextColor(colors.red)
  215.             mon.write("  Nessuna risposta")
  216.             y=y+1
  217.         end
  218.         y=y+2
  219.         if reactorCount% reactorPerPage == 0 then
  220.             mon.setTextColor(colors.white)
  221.             for i = 4, monitorSizeY, 1 do
  222.                 mon.setCursorPos(monitorSizeX/2,i)
  223.                 mon.write("|")
  224.             end
  225.             x= x+(monitorSizeX/2)
  226.             y = 4
  227.         end
  228.     end
  229. end
  230.  
  231.  
  232. local previousStates = {}
  233.  
  234. local function checkInput()
  235.     local activeColors = rs.getBundledInput("left")
  236.  
  237.     term.setTextColor(colors.yellow)
  238.     print("---------------------------------\nCheck Inputs...")
  239.     for _, r in ipairs(config.reactors) do
  240.         local isActive = colors.test(activeColors, colors[r.color])
  241.  
  242.         if isActive then
  243.             term.setTextColor(colors.lime)
  244.             print(r.name .. " ATTIVO")
  245.         else
  246.             term.setTextColor(colors.red)
  247.             print(r.name .. " SPENTO")
  248.         end
  249.  
  250.         if previousStates[r.id] ~= isActive then
  251.             local command
  252.             if isActive then
  253.                 command = textutils.serialize({command = "reactor_control", newState = true})
  254.             else
  255.                 command = textutils.serialize({command = "reactor_control", newState = false})
  256.             end
  257.  
  258.             rednet.send(r.id, command)
  259.             local start=os.clock()
  260.             local sender,msg
  261.             repeat
  262.                 sender,msgTmp=rednet.receive(5)
  263.                 if msgTmp then
  264.                     msg = textutils.unserialize(msgTmp)
  265.                 end
  266.             until sender==r.id or os.clock()-start>5
  267.             if msg and msg.success then
  268.                 term.setTextColor(colors.lime)
  269.                 print(r.name .. " Impostato al nuovo stato")
  270.                 previousStates[r.id] = isActive
  271.             else
  272.                 term.setTextColor(colors.red)
  273.                 print("Nessuna risposta da "..r.name)
  274.             end
  275.         end
  276.     end
  277.     term.setTextColor(colors.yellow)
  278.     print("---------------------------------")
  279. end
  280.  
  281. local scramStates = {}
  282.  
  283. local function checkSCRAM()
  284.     term.setTextColor(colors.yellow)
  285.     print("---------------------------------\nCheck SCRAM...")
  286.     for _,r in ipairs(config.reactors) do
  287.         local info = reactors[r.id]
  288.         if info and type(info) == "table" then
  289.             if info.SCRAMState then
  290.                 term.setTextColor(colors.red)
  291.                 print(r.name .. " in SCRAM!!")
  292.                 scramActive = true
  293.                 if not scramStates[r.id] then
  294.                     rs.setBundledOutput("right", colors[r.color])
  295.                     sleep(0.2)
  296.                     rs.setBundledOutput("right", 0)
  297.                     scramStates[r.id] = true
  298.                 end
  299.             else
  300.                 term.setTextColor(colors.lime)
  301.                 print(r.name .. " ok")
  302.                 scramStates[r.id] = false  
  303.             end
  304.         end
  305.     end
  306.     term.setTextColor(colors.yellow)
  307.     print("---------------------------------")
  308. end
  309.  
  310. -- ========= CONTROLLO INPUT SCRAM =========
  311. local function checkInputSCRAM()
  312.     if not scramActive then return end
  313.     print("---------------------------------\nCheck SCRAM reset buttons...")
  314.     for _,r in ipairs(config.reactors) do
  315.         local info = reactors[r.id]
  316.         if info and type(info) == "table" then
  317.             if info.SCRAMState then
  318.                 if rs.testBundledInput("right", colors[r.scram_color]) then
  319.                     term.setTextColor(colors.lime)
  320.                     print("Ricevuto segnale reset per "..r.name)
  321.                     rednet.send(r.id, textutils.serialize({command="reset_SCRAM"}))
  322.                 else
  323.                     term.setTextColor(colors.red)
  324.                     print("Nessun segnale reset per "..r.name)
  325.                 end
  326.             end
  327.         end
  328.     end
  329.     term.setTextColor(colors.yellow)
  330.     print("---------------------------------")
  331. end
  332.  
  333. -- ========= CICLO PRINCIPALE =========
  334. animateLoad(2,1)
  335. while true do
  336.     scramActive = false
  337.  
  338.     reactors = getReactorsInfo(config)
  339.     sleep(0.5)
  340.     checkInput()
  341.     checkSCRAM()
  342.     checkInputSCRAM()
  343.     drawUI()
  344.     term.setTextColor(colors.gray)
  345.     print("Aggiornamento completato.\n")
  346.     sleep(config.update_interval)
  347. end
Advertisement
Add Comment
Please, Sign In to add comment