--[[
Programma di controllo per Extreme Reactors (V4 - Finale)
Mostra:
- Stato (Attivo/Spento)
- Produzione FE/t e Temperatura
- Barre per Calore, Energia, Carburante, Control Rod
- Pulsanti: [ ON ], [ OFF ], [ - ] (10% step), [ + ] (10% step)
]]
--- --- ---
-- 1. CONFIGURAZIONE DEI PERIPHERAL
--- --- ---
-- Avvolge il monitor (presunto sul lato 'bottom')
local mon = peripheral.wrap("bottom")
if mon == nil then
print("Errore: Monitor non trovato sul lato 'bottom'.")
return
end
-- Trova il reattore su qualsiasi lato connesso
local reactor = peripheral.find("reactor")
if reactor == nil then
print("Errore: Nessun reattore 'Extreme Reactor' trovato in rete.")
return
end
--- --- ---
-- 2. VARIABILI DI STATO E DISEGNO
--- --- ---
local monWidth, monHeight = mon.getSize()
-- Livello target delle rod (inizializzato al livello attuale)
local targetRodLevel = reactor.getAverageControlRodLevel()
local TEMP_MAX_VISUAL = 2000 -- Max visivo per la barra temperatura
-- Definiamo le aree dei pulsanti
-- Pulsanti ON/OFF (penultima riga)
local btnOff = { y = monHeight - 1, x1 = 2, x2 = 7 } -- [ OFF ]
local btnOn = { y = monHeight - 1, x1 = monWidth - 4, x2 = monWidth - 1 } -- [ ON ]
-- Pulsanti Control Rod (ultima riga)
local btnMinus = { y = monHeight, x1 = 2, x2 = 6 } -- [ - ]
local btnPlus = { y = monHeight, x1 = monWidth - 5, x2 = monWidth - 1 } -- [ + ]
-- Funzione helper per disegnare una barra di progresso
local function drawProgressBar(y, label, current, max, color)
mon.setCursorPos(1, y)
mon.write(label)
local percent = 0
if max > 0 then
percent = current / max
end
local width = monWidth - 2
local filledWidth = math.floor(width * percent)
local percentText = string.format(" %.1f%% ", percent * 100)
-- Sfondo
mon.setCursorPos(2, y + 1)
mon.setBackgroundColor(colors.gray)
mon.write(string.rep(" ", width))
-- Barra piena
if filledWidth > 0 then
mon.setCursorPos(2, y + 1)
mon.setBackgroundColor(color)
mon.write(string.rep(" ", filledWidth))
end
-- Testo percentuale
mon.setCursorPos(math.floor(monWidth / 2 - #percentText / 2), y + 1)
mon.setTextColor(colors.black)
mon.write(percentText)
-- Reset
mon.setBackgroundColor(colors.black)
mon.setTextColor(colors.white)
end
-- Funzione principale per disegnare tutto
local function drawDisplay()
-- Puliamo lo schermo
mon.clear()
mon.setCursorPos(math.floor(monWidth / 2 - 10), 1)
mon.write("--- STATO REATTORE ---")
--- --- Dati Reattore --- ---
local status = reactor.isMachineActive()
local energyProd = reactor.getEnergyGeneratedLastTick()
local energyStored = reactor.getEnergyStored()
local energyMax = reactor.getMaxEnergyStored()
local temp = reactor.getCoreTemperature()
local fuelAmount = reactor.getFuelAmount()
local wasteAmount = reactor.getWasteAmount()
local fuelCapacity = fuelAmount + wasteAmount
local currentRods = reactor.getAverageControlRodLevel()
--- --- Disegno UI --- ---
-- 1. Produzione e Temperatura (Testo)
mon.setCursorPos(1, 3)
mon.write(string.format("Produzione: %.0f FE/t", energyProd))
mon.setCursorPos(1, 4)
mon.write(string.format("Temperatura: %.0f C", temp))
-- 2. Stato (Attivo/Spento)
mon.setCursorPos(1, 5)
if status then
mon.setTextColor(colors.lime)
mon.write("Stato: [ATTIVO]")
else
mon.setTextColor(colors.red)
mon.write("Stato: [SPENTO]")
end
mon.setTextColor(colors.white) -- Reset
-- 3. Barra Temperatura
local tempPercent = temp / TEMP_MAX_VISUAL
local tempColor = colors.green
if tempPercent >= 0.8 then
tempColor = colors.red
elseif tempPercent >= 0.5 then
tempColor = colors.orange
end
drawProgressBar(7, "Calore Core:", temp, TEMP_MAX_VISUAL, tempColor)
-- 4. Barra Energia Immagazzinata
drawProgressBar(10, "Energia:", energyStored, energyMax, colors.cyan)
-- 5. Barra Carburante
drawProgressBar(13, "Carburante:", fuelAmount, fuelCapacity, colors.yellow)
-- 6. Barra Control Rod
drawProgressBar(16, "Control Rods (Target: " .. targetRodLevel .. "%)", currentRods, 100, colors.orange)
-- 7. Pulsanti ON / OFF (penultima riga)
mon.setBackgroundColor(colors.red)
mon.setCursorPos(btnOff.x1, btnOff.y)
mon.write("[ OFF ]")
mon.setBackgroundColor(colors.lime)
mon.setCursorPos(btnOn.x1, btnOn.y)
mon.write("[ ON ]")
-- 8. Pulsanti Control Rod (ultima riga)
mon.setBackgroundColor(colors.red)
mon.setCursorPos(btnMinus.x1, btnMinus.y)
mon.write("[ - ]")
mon.setBackgroundColor(colors.lime)
mon.setCursorPos(btnPlus.x1, btnPlus.y)
mon.write("[ + ]")
mon.setBackgroundColor(colors.black) -- Reset
end
--- --- ---
-- 3. LOOP PRINCIPALE (EVENTI E AGGIORNAMENTO)
--- --- ---
print("Programma avviato. Disegno l'interfaccia sul monitor...")
mon.clear()
drawDisplay()
while true do
-- Aspetta un evento (touch o timer)
local event, param1, x, y = os.pullEvent("monitor_touch", "timer")
local modified = false
if event == "timer" then
-- Aggiorna il display ogni secondo
drawDisplay()
elseif event == "monitor_touch" then
-- Controlli Control Rod (ultima riga)
if y == btnMinus.y and x >= btnMinus.x1 and x <= btnMinus.x2 then
-- Diminuisce di 10%
targetRodLevel = math.max(0, targetRodLevel - 10)
reactor.setAllControlRodLevels(targetRodLevel)
modified = true
elseif y == btnPlus.y and x >= btnPlus.x1 and x <= btnPlus.x2 then
-- Aumenta di 10%
targetRodLevel = math.min(100, targetRodLevel + 10)
reactor.setAllControlRodLevels(targetRodLevel)
modified = true
-- Controlli Accensione (penultima riga)
elseif y == btnOff.y and x >= btnOff.x1 and x <= btnOff.x2 then
reactor.deactivate()
modified = true
elseif y == btnOn.y and x >= btnOn.x1 and x <= btnOn.x2 then
reactor.activate()
modified = true
end
-- Se abbiamo premuto un pulsante, aggiorniamo subito
if modified then
drawDisplay()
end
end
-- Riavvia il timer (per il prossimo aggiornamento)
if not timer.isStarted() then
timer.start(1)
end
end