tommy2805

Reactor control server

Oct 21st, 2025 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.02 KB | None | 0 0
  1. local configPath = "config"
  2.  
  3. -- ========= TROVA PERIFERICHE =========
  4. local function findPeripheral(t)
  5.     for _, side in ipairs(rs.getSides()) do
  6.         if peripheral.isPresent(side) and peripheral.getType(side) == t then
  7.             return peripheral.wrap(side), side
  8.         end
  9.     end
  10.     return nil
  11. end
  12.  
  13. -- ========= CONFIGURAZIONE =========
  14. local function loadConfig()
  15.     if not fs.exists(configPath) then return nil end
  16.     local f = fs.open(configPath,"r")
  17.     local data=f.readAll() f.close()
  18.     local func=loadstring(data)
  19.     if func then return func() else error("Errore nel config") end
  20. end
  21.  
  22. term.setTextColor(colors.yellow)
  23. print("Avvio centrale di controllo...")
  24. sleep(0.3)
  25. local config = loadConfig()
  26. if not config then
  27.     term.setTextColor(colors.white)
  28.     print("Configurazione non trovata. Creazione guidata...\n")
  29.     local validColors = {}
  30.     for k, v in pairs(colors) do
  31.         validColors[k] = v
  32.     end
  33.     local reactors = {}
  34.     while true do
  35.         local id
  36.         local color
  37.         write("Nome reattore: ") local name = read()
  38.         repeat
  39.             write("ID rednet: ") id = tonumber(read())
  40.         until id
  41.         repeat
  42.             write("Colore asseganto al reattore(white,blue ...): \n") color = read()
  43.         until validColors[color]
  44.         table.insert(reactors,{name=name,id=id,color = color})
  45.         if table.maxn(reactors) == 8 then break end
  46.         write("Altro reattore? (s/n): ") if read():lower()~="s" then break end
  47.     end
  48.     config = {reactors=reactors, update_interval=2}
  49.     local f = fs.open(configPath,"w")
  50.     f.write("return " .. textutils.serialize(config))
  51.     f.close()
  52. end
  53. if type(config.update_interval)~="number" then config.update_interval=2 end
  54.  
  55. term.setTextColor(colors.cyan)
  56. print("\n[ Inizializzazione periferiche ]")
  57. sleep(0.3)
  58.  
  59. local modem, modemSide = findPeripheral("modem")
  60. if not modem then error("Nessun modem trovato!") end
  61. rednet.open(modemSide)
  62. term.setTextColor(colors.lime)
  63. print("Modem su "..modemSide)
  64. sleep(0.2)
  65.  
  66. local mon, monSide = findPeripheral("monitor")
  67. if not mon then error("Nessun monitor trovato!") end
  68. mon.setBackgroundColor(colors.black)
  69. mon.setTextColor(colors.white)
  70. mon.clear()
  71. local monitorSizeX, monitorSizeY = mon.getSize()
  72. term.setTextColor(colors.lime)
  73. print("Monitor su "..monSide)
  74. sleep(0.5)
  75.  
  76. term.setTextColor(colors.yellow)
  77. print("Inizializzazione completata.")
  78. sleep(1)
  79. term.clear()
  80. term.setCursorPos(1,1)
  81.  
  82. -- ========= VARIABILI =========
  83. local reactors = {}
  84.  
  85. -- ========= UTILITY =========
  86. local function colorByTemp(p)
  87.     if p < 60 then return colors.green
  88.     elseif p < 85 then return colors.orange
  89.     else return colors.red end
  90. end
  91. local function drawBar(x,y,w,perc,color)
  92.     local filled = math.floor(w*perc/100)
  93.     mon.setCursorPos(x,y)
  94.     mon.setBackgroundColor(colors.gray)
  95.     mon.write(string.rep(" ",w))
  96.     mon.setCursorPos(x,y)
  97.     mon.setBackgroundColor(color)
  98.     mon.write(string.rep(" ",filled))
  99.     mon.setBackgroundColor(colors.black)
  100. end
  101.  
  102. local function getReactorsInfo(config)
  103.     local reactors = {}
  104.     for _,r in ipairs(config.reactors) do
  105.         term.setTextColor(colors.lightBlue)
  106.         print("Invio richiesta a "..r.name.." (ID "..r.id..")...")
  107.         local command = textutils.serialize({command = "reactor_info"})
  108.         rednet.send(r.id, command)
  109.         local start=os.clock()
  110.         local sender,msg
  111.         repeat
  112.             sender,msgTmp=rednet.receive(5)
  113.             if msgTmp then
  114.                 msg = textutils.unserialize(msgTmp)
  115.             end
  116.         until sender==r.id or os.clock()-start>5
  117.        
  118.  
  119.         if sender==r.id and type(msg)=="table" and msg.type=="info" then
  120.             term.setTextColor(colors.lime)
  121.             print("Dati ricevuti da "..r.name)
  122.             reactors[r.id]=msg.data
  123.         else
  124.             term.setTextColor(colors.red)
  125.             print("Nessuna risposta da "..r.name)
  126.             reactors[r.id]=nil
  127.         end
  128.     end
  129.     return reactors
  130. end
  131.  
  132. local function drawUI()
  133.     mon.setBackgroundColor(colors.black)
  134.     mon.setTextColor(colors.white)
  135.     mon.clear()
  136.     mon.setCursorPos((monitorSizeX/2)-12,1)
  137.     mon.write("=== CENTRALE NUCLEARE ===")
  138.     mon.setCursorPos((monitorSizeX/2)-9,2)
  139.     mon.write("Aggiornato: " .. textutils.formatTime(os.time(), true))
  140.     local y=4
  141.     local x=2
  142.     local reactorCount = 0
  143.     local reactorPerPage = math.floor(((monitorSizeY-3)/6)+0.5)
  144.     for _,r in ipairs(config.reactors) do
  145.         reactorCount = reactorCount+1
  146.         local info = reactors[r.id]
  147.         mon.setCursorPos(x,y)
  148.         mon.setTextColor(colors.cyan)
  149.         mon.write(r.name)
  150.         y=y+1
  151.         if info and type(info) == "table" then
  152.             mon.setCursorPos(x+15,y-1)
  153.             local perc = math.floor((info.temp/info.maxHeat)*100)
  154.             if perc >= 80 then
  155.                 mon.setTextColor(colors.red) mon.write("STATO: SCRAM")
  156.             elseif info.active then
  157.                 mon.setTextColor(colors.lime) mon.write("STATO: ATTIVO")
  158.             else
  159.                 mon.setTextColor(colors.red) mon.write("STATO: SPENTO")
  160.             end
  161.             mon.setTextColor(colors.yellow)
  162.             mon.setCursorPos(x+2,y)
  163.             mon.write(string.format("MaxOut: %d/t | Output: %d/t", info.maxOutput, info.effectiveOutput))
  164.             y=y+1
  165.             mon.setCursorPos(x+2,y)
  166.             mon.write(string.format("MaxTemp: %d C | Temp: %d%%", info.maxHeat, perc))
  167.             y=y+1
  168.             drawBar(x+2,y,22,perc+30,colorByTemp(perc))
  169.             y=y+1
  170.             mon.setCursorPos(x+2,y)
  171.  
  172.         else
  173.             mon.setTextColor(colors.red)
  174.             mon.write("  Nessuna risposta")
  175.             y=y+1
  176.         end
  177.         y=y+2
  178.         if reactorCount% reactorPerPage == 0 then
  179.             mon.setTextColor(colors.white)
  180.             for i = 4, monitorSizeY, 1 do
  181.                 mon.setCursorPos(monitorSizeX/2,i)
  182.                 mon.write("|")
  183.             end
  184.             x= x+(monitorSizeX/2)
  185.             y = 4
  186.         end
  187.     end
  188. end
  189.  
  190.  
  191. local previousStates = {}
  192.  
  193. local function checkInput()
  194.     local activeColors = rs.getBundledInput("right")
  195.  
  196.     term.setTextColor(colors.yellow)
  197.     print("---------------------------------\nCheck Inputs...")
  198.     for _, r in ipairs(config.reactors) do
  199.         local isActive = colors.test(activeColors, colors[r.color])
  200.  
  201.         if isActive then
  202.             term.setTextColor(colors.lime)
  203.             print(r.name .. " ATTIVO")
  204.         else
  205.             term.setTextColor(colors.red)
  206.             print(r.name .. " SPENTO")
  207.         end
  208.  
  209.         if previousStates[r.id] ~= isActive then
  210.             local command
  211.             if isActive then
  212.                 command = textutils.serialize({command = "reactor_control", newState = true})
  213.             else
  214.                 command = textutils.serialize({command = "reactor_control", newState = false})
  215.             end
  216.  
  217.             rednet.send(r.id, command)
  218.             local start=os.clock()
  219.             local sender,msg
  220.             repeat
  221.                 sender,msgTmp=rednet.receive(5)
  222.                 if msgTmp then
  223.                     msg = textutils.unserialize(msgTmp)
  224.                 end
  225.             until sender==r.id or os.clock()-start>5
  226.             if msg and msg.success then
  227.                 term.setTextColor(colors.lime)
  228.                 print(r.name .. " Impostato al nuovo stato")
  229.                 previousStates[r.id] = isActive
  230.             else
  231.                 term.setTextColor(colors.red)
  232.                 print("Nessuna risposta da "..r.name)
  233.             end
  234.         end
  235.     end
  236.     term.setTextColor(colors.yellow)
  237.     print("---------------------------------")
  238. end
  239.  
  240. local function checkSCRAM()
  241.     term.setTextColor(colors.yellow)
  242.     print("---------------------------------\nCheck SCRAM...")
  243.     for _,r in ipairs(config.reactors) do
  244.         local info = reactors[r.id]
  245.         if info and type(info) == "table" then
  246.             local perc = math.floor((info.temp/info.maxHeat)*100)
  247.             local activeColors = rs.getBundledOutput("back")
  248.             if perc >= 80 then
  249.                 term.setTextColor(colors.red)
  250.                 print(r.name .. " in SCRAM!!")
  251.                 if not colors.test(activeColors,colors[r.color]) then
  252.                     rs.setBundledOutput("back", activeColors + colors[r.color])
  253.                 end
  254.             else
  255.                 term.setTextColor(colors.lime)
  256.                 print(r.name .. " ok")
  257.                 if colors.test(activeColors,colors[r.color]) then
  258.                     rs.setBundledOutput("back", activeColors - colors[r.color])
  259.                 end
  260.             end
  261.         end
  262.     end
  263.     term.setTextColor(colors.yellow)
  264.     print("---------------------------------")
  265. end
  266.  
  267. -- ========= CICLO DI POLLING =========
  268. while true do
  269.     reactors = getReactorsInfo(config)
  270.     sleep(0.5)
  271.     checkInput()
  272.     checkSCRAM()
  273.     drawUI()
  274.     term.setTextColor(colors.gray)
  275.     print("Aggiornamento completato.\n")
  276.     sleep(config.update_interval)
  277. end
  278.  
Advertisement
Add Comment
Please, Sign In to add comment