Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function formatPower(power)
- local powerString = tostring(math.floor(power))
- local units = { "", "k", "M", "G", "T" }
- local unitIndex = 1
- while #powerString >= 4 do
- powerString = tostring(math.floor(power / 1000))
- power = power / 1000
- unitIndex = unitIndex + 1
- end
- return powerString .. units[unitIndex]
- end
- local function rebootIfTimeIsSixAM()
- local currTime = os.time()
- if currTime == 6.25 then
- local file
- local mode = fs.exists("reboot.log") and "a" or "w"
- file = fs.open("reboot.log", mode)
- file.write("OS Reboot: " .. os.date() .. "\n")
- file.close()
- os.reboot()
- end
- end
- local reactorTypes = {
- "powah:reactor_starter",
- "powah:reactor_basic",
- "powah:reactor_hardened",
- "powah:reactor_blazing",
- "powah:reactor_niotic",
- "powah:reactor_spirited",
- "powah:reactor_nitro",
- }
- local reactorTypeString = table.concat(reactorTypes, ",")
- -- Get the list of all attached peripherals
- local periphList = peripheral.getNames()
- local monitor = peripheral.find("monitor")
- if not monitor then
- error("Monitor not found")
- end
- while true do
- local start = os.clock()
- rebootIfTimeIsSixAM()
- local totalCapacity = 0
- local totalEnergy = 0
- local totalSolarPanels = 0
- local totalReactors = 0
- local reactorCapacity = 0
- local reactorEnergy = 0
- local reactorGeneration = 0
- local solarGenerator = 0
- local solarEnergyAfter = 0
- local elapsedTime = 0
- local realTime = 0
- local iterationTimer = os.clock()
- -- Iterate over all attached peripherals
- for i, periphName in ipairs(periphList) do
- local periphType = peripheral.getType(periphName)
- if periphType == "ad_astra:solar_panel" then
- local panel = peripheral.wrap(periphName)
- local panelEnergy = panel.getEnergy() or 0
- local panelCapacity = panel.getEnergyCapacity() or 0
- totalEnergy = totalEnergy + panelEnergy
- totalCapacity = totalCapacity + panelCapacity
- totalSolarPanels = totalSolarPanels + 1
- local solarEnergyAfter = panel.getEnergy() or 0
- solarGenerator = 0
- elseif string.match(reactorTypeString, periphType) then
- local reactor = peripheral.wrap(periphName)
- reactorEnergy = reactorEnergy + reactor.getEnergy() or 0
- reactorCapacity = reactorCapacity + reactor.getEnergyCapacity() or 0
- totalReactors = totalReactors + 1
- local reactorEnergyAfter = reactor.getEnergy() or 0
- reactorGeneration = 0
- end
- end
- local solarPercentage = math.floor((totalEnergy / totalCapacity) * 100)
- local reactorPercentage = math.floor((reactorEnergy / reactorCapacity) * 100)
- print("Solar Power: " .. totalEnergy .. "\nSolar Capacity: " .. totalCapacity .. "\n" .. solarPercentage .. "\nTotal Panels: " .. totalSolarPanels .. "\nSolar Generation: " .. solarGenerator)
- print("Reactor Power: " .. reactorEnergy .. "\nReactor Capacity: " .. reactorCapacity .. "\n" .. reactorPercentage .. "\nTotal Reactors: " .. totalReactors .. "\nReactor Generation: " .. reactorGeneration)
- monitor.clear()
- local x, y = monitor.getSize()
- for i = 1, x do
- monitor.setCursorPos(i, y - 1)
- monitor.write(string.rep("-", x))
- end
- for i = 1, y do
- monitor.setCursorPos(x / 2 + 1, i)
- monitor.write("|")
- end
- monitor.setCursorPos(1, y)
- monitor.write(formatPower(totalEnergy) ..
- "/" ..
- formatPower(totalCapacity) .. " | " .. formatPower(solarGenerator) .. "/s" .. " | " .. totalSolarPanels .. " panels")
- if totalReactors == nil or reactorCapacity == nil then
- reactorGeneration = 0
- totalReactors = 0
- reactorCapacity = 0
- end
- local reactorString = totalReactors ..
- " reactors | " ..
- formatPower(reactorGeneration) ..
- "/s" .. " | " .. formatPower(reactorEnergy) .. "/" .. formatPower(reactorCapacity)
- local reactorStringLength = string.len(reactorString)
- monitor.setCursorPos(x - reactorStringLength + 1, y)
- monitor.write(reactorString)
- sleep(0.5)
- local duration = os.clock() - start
- print("While loop (48-119): " .. duration)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement