Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local interval = 1
- local component = require("component")
- local event = require("event")
- local components = {
- gpu = {address = "87abc", type = "gpu"};
- screen = {address = "4ee56", type = "screen"};
- core = {address = "68e1d", type = "draconic_rf_storage"};
- }
- local resolution_x, resolution_y = 30, 15
- local function set_resolution(gpu)
- if not gpu then
- gpu = components.gpu.proxy
- if not gpu then
- return
- end
- end
- gpu.setResolution(resolution_x, resolution_y)
- end
- for i,v in pairs(components) do
- local taddr = component.get(v.address, v.type)
- if not taddr then
- io.stderr:write("Could not find component " .. i)
- else
- v.proxy = component.proxy(taddr)
- end
- end
- if components.gpu.proxy and components.screen.proxy then
- components.gpu.proxy.bind(component.get(components.screen.address))
- set_resolution()
- end
- local lbase = math.log(10)
- local si_prefixes = {
- [-4] = "p";
- [-3] = "n";
- [-2] = "u";
- [-1] = "m";
- [1] = "k";
- [2] = "M";
- [3] = "G";
- [4] = "T";
- [5] = "P";
- [6] = "E";
- [7] = "Z";
- [8] = "Y";
- }
- local si_low = -4
- local si_high = 8
- local function round(number, decimals)
- return math.floor((number * 10^decimals) + 0.5) / 10^decimals
- end
- local function format_si(number, unit)
- local mult = 1
- if number < 0 then
- mult = -1
- number = -number
- end
- local brack = math.floor((math.log(number) / lbase) / 3)
- brack = math.min(math.max(brack, si_low), si_high)
- if brack == 0 then
- return tostring(mult * round(number, 3)) .. " " .. unit
- end
- number = number / 10^(brack * 3)
- return tostring(round(mult * number, 3)) .. " " .. si_prefixes[brack] .. unit
- end
- local function print_center(gpu, text, y)
- local x = math.floor((resolution_x - #text) / 2)
- gpu.set(x, y, text)
- end
- local function check_core()
- local gpu = components.gpu.proxy
- local core = components.core.proxy
- if not gpu or not core then
- return
- end
- local stored_energy, max_energy, transfer = core.getEnergyStored(), core.getMaxEnergyStored(), core.getTransferPerTick()
- local stored_text, max_text, transfer_text = format_si(stored_energy, "RF"), format_si(max_energy, "RF"), format_si(transfer, "RF")
- print_center(gpu, "Stored: " .. stored_text .. " / " .. max_text, math.floor(resolution_y / 2))
- end
- local function on_component_added(event, address, component_type)
- for i,v in pairs(components) do
- if address:sub(1, #v.address) == v.address then
- io.stderr:write("component " .. i .. " added")
- v.proxy = component.proxy(address)
- if component_type == "gpu" or component_type == "screen" then
- if components.gpu.proxy and components.screen.proxy then
- components.gpu.proxy.bind(component.get(components.screen.address))
- set_resolution()
- end
- end
- end
- end
- end
- local function on_component_removed(event, address, component_type)
- for i,v in pairs(components) do
- if address:sub(1, #v.address) == v.address then
- io.stderr:write("component " .. i .. " removed")
- v.proxy = nil
- end
- end
- end
- function start()
- if timer then
- stop()
- end
- timer = event.timer(interval, check_core, math.huge)
- end
- function stop()
- if timer then
- event.cancel(timer)
- timer = nil
- event.ignore("component_added", on_component_added)
- event.ignore("component_removed", on_component_removed)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement