Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.wrap("left")
- -- Config
- local refreshDelay = 8
- local title = "Dooheeg Power Monitor v1.0"
- local reactorPrefix = "BigReactors-Reactor_"
- local cellPrefix = "tile_thermalexpansion_cell_resonant_name_"
- -- Reactor Config
- local powerReactorThresholdMin = 0.25
- local powerReactorThresholdMax = 0.9
- -- Bar Config
- local barHeight = 5
- local fillPercentHigh = 0.5
- local fillPercentMid = 0.25
- local textColor = colors.white
- local fillColorHigh = colors.green
- local fillColorMid = colors.orange
- local fillColorLow = colors.red
- local nofillColor = colors.gray
- local mWidth, mHeight = monitor.getSize()
- -- Total stored power
- local totalPowerMax = 0
- local totalPowerCurrent = 0
- -- Power use tracking
- local powerUsed = 0
- local powerPerSecond = 0
- local powerPerTick = 0
- local reactors = {}
- local cells = {}
- local reactorsEnabled = false
- function GetReactors(_count)
- for i = 0, _count do
- local reactor = peripheral.wrap(reactorPrefix .. _count)
- table.insert(reactors, reactor)
- end
- end
- function SetReactorState(enabled)
- reactorsEnabled = enabled
- for i = 1, #reactors do
- local reactor = reactors[i]
- reactor.setActive(enabled)
- end
- end
- function GetCells(_count)
- for i = 0, _count do
- local cell = peripheral.wrap(cellPrefix .. i)
- table.insert(cells, cell)
- end
- end
- function CheckMaxPower()
- for i = 1, #cells do
- local cell = cells[i]
- local mPow = cell.getMaxEnergyStored()
- totalPowerMax = totalPowerMax + mPow
- end
- end
- function CheckPower()
- totalPowerCurrent = 0
- for i = 1, #cells do
- local cell = cells[i]
- local cPow = cell.getEnergyStored()
- totalPowerCurrent = totalPowerCurrent + cPow
- end
- end
- function Write(_x, _y, _text)
- WriteC(_x, _y, _text, textColor)
- end
- function WriteC(_x, _y, _text, _tCol)
- monitor.setCursorPos(_x, _y)
- monitor.setTextColor(_tCol)
- monitor.write(_text)
- end
- function Percent(_a, _b)
- return (_a / _b)
- end
- function DrawBar(_y, _height, _fill)
- local barWidth = mWidth - 2
- local filled = math.ceil(barWidth * _fill)
- local notFilled = barWidth - filled
- local fillColor
- if(_fill > fillPercentHigh) then
- fillColor = fillColorHigh
- elseif (_fill > fillPercentMid) then
- fillColor = fillColorMid
- else
- fillColor = fillColorLow
- end
- local by
- for i = 0, _height - 1 do
- by = _y + i
- -- Draw Filled Section
- for bx = 2, filled + 1 do
- WriteC(bx, by, "#", fillColor)
- end
- -- Draw Unfilled Section
- for bx = 2, notFilled + 1 do
- WriteC(bx + filled, by, "#", nofillColor)
- end
- end
- end
- function Round(num, numPlaces)
- local mult = 10 ^ (numplaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function FBN(num, places, space)
- num = math.abs(num)
- local numString
- local placeValue = ("%%.%df"):format(places or 0)
- local rnum
- if (not num) then
- return 0
- elseif (num >= 1000000000000) then
- rnum = math.ceil(num / 1000000000000)
- numString = placeValue:format(rnum)
- if (space == 0) then
- numString = numString .. "T"
- else
- numString = numString .. " T"
- end
- elseif (num >= 1000000000) then
- rnum = math.ceil(num / 1000000000)
- numString = placeValue:format(rnum)
- if (space == 0) then
- numString = numString .. "B"
- else
- numString = numString .. " B"
- end
- elseif (num >= 1000000) then
- rnum = math.ceil(num / 1000000)
- numString = placeValue:format(rnum)
- if (space == 0) then
- numString = numString .. "M"
- else
- numString = numString .. " M"
- end
- elseif (num >= 1000) then
- rnum = math.ceil(num / 1000)
- numString = placeValue:format(rnum)
- if (space == 0) then
- numString = numString .. "K"
- else
- numString = numString .. " K"
- end
- else
- numString = num
- end
- return numString
- end
- -- Initialize
- GetCells(11)
- GetReactors(1)
- CheckMaxPower()
- -- Main Loop
- while (true) do
- monitor.clear()
- CheckPower()
- -- Calculate power use
- powerUsed = powerUsed - totalPowerCurrent
- powerUsed = -powerUsed
- powerPerSecond = powerUsed / refreshDelay
- powerPerTick = powerPerSecond / 20
- -- Calculate power percent
- local powerPercent = Percent(totalPowerCurrent, totalPowerMax)
- DrawBar(7, barHeight, powerPercent)
- Write(2, 2, title .. " | Reactor: " .. tostring(reactorsEnabled))
- Write(2, 4, Round(powerPercent * 100) .. "% : " .. FBN(totalPowerCurrent, 2, 0) .. " / " .. FBN(totalPowerMax, 2, 0))
- -- Check need for reactor, enable/disable
- if (reactorsEnabled) then
- if (powerPercent > powerReactorThresholdMax) then
- SetReactorState(false)
- end
- else
- if (powerPercent < powerReactorThresholdMin) then
- SetReactorState(true)
- end
- end
- -- Set power use color
- local powerColor
- if (powerUsed < 0) then
- powerColor = colors.red
- else
- powerColor = colors.lime
- end
- -- Draw power use
- WriteC(2, 5, FBN(Round(powerPerSecond), 1, 1) .. "RF/s"
- .. " | " .. FBN(Round(powerPerTick), 1, 1) .. "RF/t", powerColor)
- powerUsed = totalPowerCurrent
- sleep(refreshDelay)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement