Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = peripheral.find("monitor")
- if not mon then
- print("Aucun monitor détecté")
- return
- end
- local modemSide
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- modemSide = side
- break
- end
- end
- if not modemSide then
- print("Aucun modem détecté")
- return
- end
- rednet.open(modemSide)
- local cells = {}
- for _, name in ipairs(peripheral.getNames()) do
- local p = peripheral.wrap(name)
- if p.getEnergy and p.getEnergyCapacity then
- table.insert(cells, {
- obj = p,
- lastEnergy = p.getEnergy()
- })
- end
- end
- if #cells == 0 then
- print("Aucune Energy Cell détectée")
- return
- end
- mon.setTextScale(0.5)
- local w, h = mon.getSize()
- -- === BARRE AVEC COULEUR DYNAMIQUE ===
- function getBarColor(percent)
- if percent < 0.3 then
- return colors.red
- elseif percent < 0.6 then
- return colors.orange
- else
- return colors.lime
- end
- end
- function drawBar(x, y, percent, width)
- local filled = math.floor(percent * width)
- local empty = width - filled
- mon.setCursorPos(x, y)
- mon.setBackgroundColor(getBarColor(percent))
- mon.write(string.rep(" ", filled))
- mon.setBackgroundColor(colors.gray)
- mon.write(string.rep(" ", empty))
- mon.setBackgroundColor(colors.black)
- end
- function clearLine(y)
- mon.setCursorPos(1, y)
- mon.setBackgroundColor(colors.black)
- mon.write(string.rep(" ", w))
- end
- function formatTime(seconds)
- if seconds <= 0 then return "--" end
- local m = math.floor(seconds / 60)
- local s = seconds % 60
- return string.format("%d min %02d sec", m, s)
- end
- while true do
- local totalEnergy = 0
- local totalCapacity = 0
- local totalDelta = 0
- for i, cell in ipairs(cells) do
- local y = i * 2 - 1 -- espace entre chaque ligne
- local energy = cell.obj.getEnergy()
- local capacity = cell.obj.getEnergyCapacity()
- local percent = energy / capacity
- local delta = energy - cell.lastEnergy
- cell.lastEnergy = energy
- totalEnergy = totalEnergy + energy
- totalCapacity = totalCapacity + capacity
- totalDelta = totalDelta + delta
- local nomCellule = string.format("Cellule %-2d", i)
- -- Efface ligne
- clearLine(y)
- -- Titre
- mon.setCursorPos(1, y)
- mon.setTextColor(colors.yellow)
- mon.write(string.format("[%s]", nomCellule))
- -- Barre graphique
- drawBar(10, y, percent, 20)
- -- Pourcentage
- mon.setCursorPos(32, y)
- mon.setTextColor(colors.white)
- mon.write(string.format("%3d%%", percent * 100))
- -- RF/t
- mon.setCursorPos(40, y)
- if delta > 0 then
- mon.setTextColor(colors.lime)
- elseif delta < 0 then
- mon.setTextColor(colors.red)
- else
- mon.setTextColor(colors.gray)
- end
- mon.write(string.format("%+4d RF/t ", delta))
- end
- -- === GLOBAL BAR ===
- local globalPercent = totalEnergy / totalCapacity
- local globalBarY = h - 1
- clearLine(globalBarY)
- clearLine(h)
- drawBar(10, globalBarY, globalPercent, 20)
- mon.setCursorPos(1, globalBarY)
- mon.setTextColor(colors.cyan)
- mon.write("[GLOBAL]")
- mon.setCursorPos(32, globalBarY)
- mon.setTextColor(colors.white)
- mon.write(string.format("%3d%%", globalPercent * 100))
- local estTime = "--"
- if totalDelta ~= 0 then
- local missing = totalDelta > 0 and (totalCapacity - totalEnergy) or totalEnergy
- local seconds = missing / math.abs(totalDelta)
- estTime = formatTime(math.floor(seconds))
- end
- mon.setCursorPos(1, h)
- mon.setTextColor(colors.lightGray)
- mon.write("Estimation : " .. estTime .. " ")
- sleep(1)
- local data = {
- cells = {},
- globalPercent = globalPercent,
- estTime = estTime
- }
- for i, cell in ipairs(cells) do
- local energy = cell.obj.getEnergy()
- local capacity = cell.obj.getEnergyCapacity()
- local percent = energy / capacity
- local delta = energy - cell.lastEnergy
- table.insert(data.cells, {
- id = i,
- percent = percent,
- delta = delta
- })
- end
- rednet.broadcast(data, "energy_display")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement