Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local math = require("math")
- local bit32 = require("bit32")
- local string = require("string")
- local gpu = component.gpu
- local width, height = gpu.getResolution()
- function clear()
- gpu.setBackground(0x000000)
- local width, height = gpu.getResolution()
- gpu.fill(1, 1, width, height, " ")
- end
- function singleBorderBox(x, y, w, h)
- gpu.fill(x, y, w, 1, "█")
- gpu.fill(x, y + h - 1, w, 1, "█")
- gpu.fill(x, y + 1, 1, h - 1, "█")
- gpu.fill(x + w - 1, y + 1, 1, h - 1, "█")
- end
- function doubleBorderBox(x, y, w, h)
- singleBorderBox(x, y, w, h)
- singleBorderBox(x + 2, y + 2, w - 4, h - 4)
- end
- function horizontalProgressBar(x, y, w, h, progress, color, showPercent)
- -- 0 >= progress <= 1
- singleBorderBox(x,y,w,h)
- local oldFG = gpu.getForeground()
- gpu.setForeground(color)
- gpu.fill(x + 2, y + 2, math.floor(((w - 4) * progress) + 0.5), h - 4, "█")
- if showPercent then
- -- Figure out placement of the percent label
- local percent = string.format("%.2f", progress * 100) .. "%"
- local len = string.len(percent)
- local labelX = math.floor((((w - 2) / 2) - (len / 2)) + 0.5) + x + 1
- local labelY = math.floor(((h - 2) / 2) + 0.5) + y
- -- Display the label with appropriate coloring.
- for i = 1, len do
- local c, fg, bg, _, _ = gpu.get(labelX + i - 1, labelY)
- if c == "█" then
- gpu.setForeground(bg)
- gpu.setBackground(fg)
- gpu.set(labelX + i - 1, labelY, string.sub(percent, i, i))
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- else
- gpu.set(labelX + i - 1, labelY, string.sub(percent, i, i))
- end
- end
- end
- gpu.setForeground(oldFG)
- end
- function adjustShade(color, factor)
- local red = bit32.band(16711680, color)
- local green = bit32.band(65280, color)
- local blue = bit32.band(255, color)
- red = red * factor
- green = green * factor
- blue = blue * factor
- local result = bit32.bor(blue, 0)
- result = bit32.bor(green, result)
- result = bit32.bor(red, result)
- return result
- end
- function maxAndMinFromArray(data)
- local max = data[1]
- local min = data[1]
- if #data > 1 then
- for i=2, #data do
- if data[i] > max then
- max = data[i]
- end
- if data[i] < min then
- min = data[i]
- end
- end
- end
- return max, min
- end
- function verticalBarChart(x, y, w, h, data, color)
- -- Draw the axies
- gpu.fill(x, y, 1, h, "█")
- gpu.fill(x + 1, y + h - 1, w - 1, 1, "█")
- local max, min = maxAndMinFromArray(data)
- local oldFG = gpu.getForeground()
- gpu.setForeground(color)
- -- Draw the bars
- local numberOfBars = math.min(#data, math.floor((w - 2) / 2))
- for i=1, numberOfBars do
- if i >= 2 then
- gpu.setForeground(adjustShade(color, .75))
- end
- local heightOfBar = math.floor(((((h - 2) * .95) / max) * data[i]) +0.5)
- gpu.fill(x + w - (2 * i), (h - 2) - heightOfBar + y, 2, heightOfBar, "█")
- end
- gpu.setForeground(oldFG)
- end
- function format_int(number)
- -- stolen from here: https://stackoverflow.com/questions/10989788/lua-format-integer
- local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
- -- reverse the int-string and append a comma to all blocks of 3 digits
- int = int:reverse():gsub("(%d%d%d)", "%1,")
- -- reverse the int-string back remove an optional comma and put the
- -- optional minus and fractional part back
- return minus .. int:reverse():gsub("^,", "") .. fraction
- end
- function condenseInt(number)
- if number >= 1000000000 then
- return string.format("%.2f", number / 1000000000) .. "B"
- elseif number >=1000000 then
- return string.format("%.2f", number / 1000000) .. "M"
- elseif number >= 1000 then
- return string.format("%.2f", number / 1000) .. "K"
- else
- return number
- end
- end
- local currentRF = 30000000000
- local RFStorage = 59300000000
- local supplyData = {1700000, 1700000, 1200000, 1200000, 0, 0, 0, 0, 1700000, 1700000, 1210000}
- local demandData = {13000,13000,25000,13000, 13000, 13000, 26000, 27000, 13000, 13000}
- function updateScreen(currentRF, RFStorage, supplyData, demandData)
- clear()
- gpu.setBackground(0x000000)
- gpu.setForeground(0x696969)
- doubleBorderBox(1, 1, width, height)
- gpu.setForeground(0xD2D2D2)
- horizontalProgressBar(6, 6, width - 10, 9, currentRF/RFStorage, 0xCC0000, true)
- verticalBarChart(23, 19, 46, 23, supplyData, 0x00DB00)
- verticalBarChart(91, 19, 46, 23, demandData, 0xCC0000)
- gpu.setForeground(0xFFFFFF)
- local rf = "Redstone Flux: " .. format_int(currentRF) .. " / " .. format_int(RFStorage)
- gpu.set(math.floor(((width - #rf) / 2) + 0.5), 5, rf)
- local supplyTitle = "RF Supply"
- gpu.set(math.floor(((46 - #supplyTitle) / 2) + 0.5 + 23), 18, supplyTitle)
- local demandTitle = "RF Demand"
- gpu.set(math.floor(((46 - #demandTitle) / 2) + 0.5 + 91), 18, demandTitle)
- local currentSupply = supplyData[1]
- local maxSupply, minSupply = maxAndMinFromArray(supplyData)
- gpu.set(34, 43, "Current Supply: " .. condenseInt(currentSupply) .. " RF/t")
- gpu.set(34, 44, "Max Supply: " .. condenseInt(maxSupply) .. " RF/t")
- gpu.set(34, 45, "Min Supply: " .. condenseInt(minSupply) .. " RF/t")
- local currentDemand = demandData[1]
- local maxDemand, minDemand = maxAndMinFromArray(demandData)
- gpu.set(103, 43, "Current Demand: " .. condenseInt(currentDemand) .. " RF/t")
- gpu.set(103, 44, "Max Demand: " .. condenseInt(maxDemand) .. " RF/t")
- gpu.set(103, 45, "Min Demand: " .. condenseInt(minDemand) .. " RF/t")
- end
- updateScreen(currentRF, RFStorage, supplyData, demandData)
- event.pull("interrupted")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement