Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- config
- local dataCorrection = 2.5 -- value to divide IM data by.
- local displayMinMax = false -- set to false to hide the "MIN" and "MAX" text.
- local screenRefreshRate = 1 -- how often to display new data in seconds.
- local unitFormatCaps = true -- true = X.X KRF. false = X.XkRF
- local showTooltips = false -- suggests adding providers when input/output is maxed.
- local alarmPercent = 10 -- what % to output a redstone signal. 0 to 100 to enable. -1 to disable.
- local alarmSide = "back" -- What side redstone is emited for alarm
- --Setup
- mon = peripheral.find("monitor")
- mat = peripheral.find("inductionPort")
- term.clear()
- term.setCursorPos(1,1)
- --Draw Function
- function draw(xmin, xmax, ymin, ymax, c)
- mon.setBackgroundColor(c)
- mon.setCursorPos(xmin, ymin)
- if xmax ~= 1 then
- for i = 1, xmax, 1 do
- mon.write(" ")
- mon.setCursorPos(xmin+i, ymin)
- end
- end
- if ymax ~= 1 then
- for k = 1, ymax, 1 do
- mon.write(" ")
- mon.setCursorPos(xmin, ymin+k)
- end
- end
- mon.setBackgroundColor(32768)
- end
- --DrawBar Function
- function drawBar(xmin, xmax, y, r, c)
- for i=1, r, 1 do
- draw(xmin, xmax, y+i-1, 1, c)
- end
- end
- -- error screen
- function displayError(index)
- local x, y = mon.getSize()
- if x ~= 50 or y ~= 19 then return end
- local errorText = "ERROR"
- local errors = {
- "The Induction Matrix has been tampered with.",
- "Induction Port not connected.",
- "The Induction Matrix is incomplete."
- }
- mon.clear()
- -- error
- mon.setTextColour(16384)
- mon.setCursorPos((x/2) - (string.len(errorText)/2), (y/2)-1)
- mon.write(errorText)
- -- msg
- mon.setTextColour(1)
- mon.setCursorPos((x/2) - (string.len(errors[index])/2), y/2)
- mon.write(errors[index])
- printError("ERROR: " .. errors[index])
- local timer = 10
- while timer > -1 do
- local countdownStr = "Next attempt in " .. timer
- mon.setTextColour(1)
- mon.setCursorPos((x/2) - (string.len(countdownStr)/2), (y/2)+2)
- draw((x/2) - (string.len(countdownStr)/2), string.len(countdownStr) + 1, (y/2)+2, 1, 32768)
- mon.setCursorPos((x/2) - (string.len(countdownStr)/2), (y/2)+2)
- mon.write(countdownStr)
- print(countdownStr)
- timer = timer -1
- os.sleep(1)
- end
- os.reboot()
- end
- --Round Function
- function round(float)
- local int, part = math.modf(float)
- if float == math.abs(float) and part >= .5 then return int+1
- elseif part <= -.5 then return int-1
- end
- return int
- end
- --Round with 2 Decimals Function
- function roundD(exact, quantum)
- local quant,frac = math.modf(exact/quantum)
- return quantum * (quant + (frac > 0.5 and 1 or 0))
- end
- -- format time
- local timeUnits = {
- {threshold = 8553600, unit = "99+ days"},
- {threshold = 86400, unit = "days", divisor = 86400},
- {threshold = 3600, unit = "hours", divisor = 3600},
- {threshold = 60, unit = "min", divisor = 60},
- {threshold = 1, unit = "sec", divisor = 1}
- }
- function adjustTime(time)
- for _, unitData in ipairs(timeUnits) do
- if time >= unitData.threshold then
- local adjustedTime
- if unitData.divisor and unitData.divisor ~= 1 then
- adjustedTime = string.format("%.1f", time / unitData.divisor)
- elseif unitData.divisor then
- adjustedTime = math.floor(time / unitData.divisor)
- else
- adjustedTime = ""
- end
- return string.format("%s %s", adjustedTime, unitData.unit)
- end
- end
- return "0 sec." -- fallback, if time is 0 or negative
- end
- --Conversion Function
- function conversion(exact, text, addPlus, afterDecimalPoint)
- afterDecimalPoint = afterDecimalPoint or 0.1
- addPlus = addPlus or false
- local units = unitFormatCaps and {"", "K", "M", "G", "T", "P", "E", "Z", "Y"} or {"", "k", "m", "g", "t", "p", "e", "z", "y"}
- local pot = 1
- local absExact = math.abs(exact)
- while absExact >= (1000^pot) do
- pot = pot + 1
- end
- local value = roundD(exact / (1000^(pot - 1)), afterDecimalPoint)
- local out = tostring(value)
- if addPlus and value > 0 then out = "+" .. out end
- if text then out = out .. (unitFormatCaps and " " .. units[pot] or units[pot]) end
- return out
- end
- -- draw static elements (runs once)
- function drawStatic()
- --Frame Induction Matrix
- draw(2, 2, 2, 1, 128)
- draw(22, 13, 2, 1, 128)
- draw(2, 1, 2, 17, 128)
- draw(2, 33, 18, 1, 128)
- draw(34, 1, 2, 17, 128)
- draw(2, 33, 8, 1, 128)
- mon.setCursorPos(5,2)
- mon.write("Induction Matrix")
- --Status
- draw(36, 2, 2, 1, 128)
- draw(36, 1, 2, 9, 128)
- draw(49, 1, 2, 9, 128)
- draw(36, 13, 10, 1, 128)
- draw(46, 4, 2, 1, 128)
- mon.setCursorPos(39,2)
- mon.write("Status")
- mon.setCursorPos(38,7)
- mon.write("Flow:")
- --Frame Stats
- draw(36, 1, 12, 7, 128)
- draw(49, 1, 12, 7, 128)
- draw(36, 13, 18, 1, 128)
- draw(36, 2, 12, 1, 128)
- draw(45, 4, 12, 1, 128)
- mon.setCursorPos(39,12)
- mon.write("Stats")
- --energyBar
- drawBar(4, 29, 4, 3, 256)
- --inputBar
- drawBar(4, 29, 11, 2, 256)
- mon.setCursorPos(4,10)
- mon.write("Input:")
- if displayMinMax then
- mon.setTextColour(128)
- mon.setCursorPos(4,13)
- mon.write("MIN")
- mon.setCursorPos(30,13)
- mon.write("MAX")
- mon.setTextColour(1)
- end
- --outputBar
- drawBar(4, 29, 15, 2, 256)
- mon.setCursorPos(4,14)
- mon.write("Output:")
- if displayMinMax then
- mon.setTextColour(128)
- mon.setCursorPos(4,17)
- mon.write("MIN")
- mon.setCursorPos(30,17)
- mon.write("MAX")
- mon.setTextColour(1)
- end
- end
- -- draw charts (runs every refresh)
- function drawCharts()
- local percent = tostring(round((energy/dataCorrection)/(maxEnergy/dataCorrection)*100) .. "%")
- local percentPos = (29/2) - (string.len(percent)/2) + 4
- mon.setBackgroundColor(256)
- mon.setCursorPos(percentPos, 5)
- mon.write(percent)
- -- output meter
- drawBar(4, 29, 15, 2, 256)
- drawBar(4, round((lastOutput/transferCap)*29), 15, 2, 16384)
- if showTooltips and lastOutput == transferCap then
- mon.setBackgroundColor(16384)
- mon.setCursorPos(4, 16)
- mon.write("Maxed. Add/upgrade providers")
- end
- -- input meter
- drawBar(4, 29, 11, 2, 256)
- drawBar(4, round((lastInput/transferCap)*29), 11, 2, 32)
- if showTooltips and lastInput == transferCap then
- mon.setBackgroundColor(32)
- mon.setCursorPos(4, 12)
- mon.write("Maxed. Add/upgrade providers")
- end
- -- stored energy
- drawBar(4, 29, 4, 3, 256)
- drawBar(4, round(filledPercentage*29), 4, 3, 8)
- mon.setBackgroundColor(256)
- mon.setCursorPos(percentPos, 5)
- mon.write(percent)
- mon.setBackgroundColor(32768) -- reset to black
- end
- -- draw stats (runs every refresh)
- function drawStats()
- local capacity = maxEnergy/dataCorrection
- local input = lastInput/dataCorrection
- local output = lastOutput/dataCorrection
- local stored = energy/dataCorrection
- local flow = input - output
- -- STATS
- mon.setBackgroundColor(32768)
- mon.setCursorPos(38,14)
- mon.write("Cells: " .. installedCells)
- mon.setCursorPos(38,15)
- mon.write("Prov: " .. installedProviders)
- mon.setCursorPos(38,16)
- mon.write("Cap: " .. conversion(capacity, false))
- -- stored energy
- draw(23, 11, 2, 1, 32768)
- mon.setCursorPos(24,2)
- mon.write(conversion(stored, true) .. "RF")
- -- input
- draw(10, 23, 10, 1, 32768)
- mon.setCursorPos(11,10)
- mon.write(conversion(input, true, true) .. "RF/t")
- -- output
- draw(11, 22, 14, 1, 32768)
- mon.setCursorPos(12,14)
- mon.write(conversion(output*-1, true) .. "RF/t")
- -- status
- local chargeStatus = "Charging"
- local timeStatus = "Full In:"
- local statusColour = 8192 -- green
- if (flow < 0) then
- statusColour = 16384 -- red
- chargeStatus = "Depleting"
- timeStatus = "Empty In:"
- elseif flow == 0 then
- statusColour = 16 -- red
- chargeStatus = "Idle"
- end
- -- Full/Empty In
- draw(38, 10, 5, 1, 32768)
- mon.setCursorPos(38,5)
- mon.write(timeStatus)
- mon.setTextColour(statusColour)
- draw(38, 10, 4, 1, 32768)
- mon.setCursorPos(38,4)
- mon.write(chargeStatus)
- -- time remaining
- local time = "Inf."
- if output < input then
- time = adjustTime((capacity - stored) / (input - output) / 20)
- elseif output > input then
- time = adjustTime(stored / (output - input) / 20)
- end
- draw(38, 10, 6, 1, 32768)
- mon.setCursorPos(38,6)
- mon.write(time)
- -- flow
- draw(38, 10, 8, 1, 32768)
- mon.setCursorPos(38,8)
- mon.write(conversion(flow, true, true, 1) .. "RF/t")
- mon.setTextColour(1) -- reset
- end
- -- protective call
- function protCall(func, errIndex)
- local success, result = pcall(func)
- if success and result then
- -- Function executed successfully, process the result
- return result
- else
- -- Function encountered an error
- displayError(errIndex)
- end
- end
- -- monitor check
- if mon ~= nil then
- local x, y = mon.getSize()
- if x ~= 50 or y ~= 19 then
- print("ERROR: Incorrect monitor size. Monitor should be 5 wide and 3 tall.")
- return
- end
- mon.setBackgroundColor(32768)
- mon.clear()
- mon.setTextColour(1)
- else
- print("No monitor found.")
- return
- end
- -- matrix check
- while not mat or not mat.isFormed() do
- if not mat then
- displayError(2)
- elseif not mat.isFormed() then
- displayError(3)
- end
- end
- function lowBatAlarm()
- -- check the below line
- lowBatAlarmEnabled = (alarmPercent >= 0 and alarmPercent <= 100) and (round((energy/dataCorrection)/(maxEnergy/dataCorrection)*100) <= alarmPercent) -- and (lastInput/dataCorrection) - (lastOutput/dataCorrection) <= 0)
- if lowBatAlarmEnabled and not rs.getOutput(alarmSide) then
- setAlarm(true)
- elseif not lowBatAlarmEnabled and rs.getOutput(alarmSide) then
- setAlarm(false)
- end
- end
- function setAlarm(state)
- for _, side in pairs(rs.getSides()) do
- rs.setOutput(side, state)
- end
- end
- -- runs once
- print("Induction Matrix monitor successfully loaded.")
- print("Script by Ordiance & Broadbent")
- energy, maxEnergy, installedCells, installedProviders, lastInput, lastOutput, filledPercentage, transferCap, lowBatAlarmEnabled = nil
- mon.clear()
- mon.setBackgroundColor(16384)
- drawStatic()
- -- runs every refresh
- while true do
- energy = protCall(mat.getEnergy, 1)
- maxEnergy = protCall(mat.getMaxEnergy, 1)
- installedCells = protCall(mat.getInstalledCells, 1)
- installedProviders = protCall(mat.getInstalledProviders, 1)
- lastInput = protCall(mat.getLastInput, 1)
- lastOutput = protCall(mat.getLastOutput, 1)
- filledPercentage = protCall(mat.getEnergyFilledPercentage, 1)
- transferCap = protCall(mat.getTransferCap, 1)
- drawCharts()
- drawStats()
- lowBatAlarm()
- os.sleep(screenRefreshRate)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement