Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local serialization = require("serialization")
- local event = require("event")
- local term = require("term")
- local gpu = component.gpu
- local zn = require("zn")
- local charts = require("charts")
- local maxGas = 245000000
- local _current,_max,_out,_in = 0,0,0,0
- local _stillDraw, _stillWait
- local _format = 1
- local W,H=160,50
- local coords = {
- chartPowerX = 1,
- chartPowerY = 2,
- lastY = H,
- chartHeight = math.floor((H-3)/2),
- chartWidth = math.floor(W*2/3),
- progressWidth = 5,
- }
- coords["chartPowerInY"]= coords.chartHeight + 4
- coords["powerY"] = coords.chartHeight+2
- function params(color)
- return {
- x = coords.chartPowerX,
- y = coords.chartPowerY,
- width = coords.chartWidth,
- height = coords.chartHeight,
- payload = charts.Histogram {
- max = 1,
- align = charts.sides.RIGHT,
- colorFunc = function(index, norm, value, self, container)
- return color
- end
- }
- }
- end
- local _params = params(0x20ff20)
- local containerOut = charts.Container(_params)
- local payloadOut = containerOut.payload
- _params = params(0xff0000)
- _params.y = coords.chartPowerInY
- local containerIn = charts.Container(_params)
- local payloadIn = containerIn.payload
- function colorFunc(_, perc)
- if perc >= .9 then
- return 0x20afff
- elseif perc >= .75 then
- return 0x20ff20
- elseif perc >= .5 then
- return 0xafff20
- elseif perc >= .25 then
- return 0xffff20
- elseif perc >= .1 then
- return 0xffaf20
- else
- return 0xff2020
- end
- end
- function progress(x)
- return {
- x = x,
- y = coords.chartPowerY,
- width = coords.progressWidth,
- height = coords.chartHeight,
- payload = charts.ProgressBar {
- direction = charts.sides.TOP,
- colorFunc = colorFunc
- }
- }
- end
- local containerPower = charts.Container(progress(coords.chartWidth+11))
- local payloadPower = containerPower.payload
- local containerDeuterium = charts.Container(progress(coords.chartWidth+11 + coords.progressWidth*2))
- local payloadDeuterium = containerDeuterium.payload
- local containerTritium = charts.Container(progress(coords.chartWidth+11 + coords.progressWidth*4))
- local payloadTritium = containerTritium.payload
- function getData(message)
- if type(message) == "table" then
- if message.from == "power_matrix" then
- local data = message.data
- _out = math.floor(data.output)
- if payloadOut.max < _out then
- payloadOut.max = _out
- end
- table.insert(payloadOut.values, _out)
- _in = math.floor(data.input)
- if payloadIn.max < _in then
- payloadIn.max = _in
- end
- table.insert(payloadIn.values, _in)
- local power = 0
- if data.max > 0 then
- power = data.current/data.max
- end
- payloadPower.value = power
- if (#payloadOut.values > containerOut.width+1) then
- table.remove(payloadOut.values, 1)
- table.remove(payloadIn.values, 1)
- end
- _current = math.floor(data.current)
- _max = data.max
- elseif message.from == "gases_reactor" then
- local data = message.data
- payloadDeuterium.value = (data.deuterium or 0)/maxGas
- payloadTritium.value = (data.tritium or 0)/maxGas
- end
- end
- end
- function formatRF(RF, mode)
- units = {'RF', 'kRF', 'MRF', 'GRF', 'TRF', 'PRF', 'ERF', 'ZRF', 'YRF', 'inf'}
- mode = mode or #units
- local n = 1
- while RF > 1 do
- if mode == n or n >= #units then
- break
- end
- RF = RF/1000
- n = n + 1
- end
- if n>1 then
- RF = RF * 1000
- n = n - 1
- end
- RF = math.floor(RF*100)/100
- return RF .. units[n]
- end
- function draw()
- if _stillDraw then
- return
- end
- _stillDraw = true
- containerIn:draw()
- containerOut:draw()
- containerPower:draw()
- containerDeuterium:draw()
- containerTritium:draw()
- gpu.set(coords.chartPowerX, coords.powerY, "Output: " .. formatRF(_out, _format) .. "/sec ")
- gpu.set(coords.chartPowerX, coords.lastY, "Input: " .. formatRF(_in, _format) .. "/sec ")
- gpu.set(coords.chartWidth+1, coords.powerY, "Stored: " .. formatRF(_current, _format) .. " ")
- gpu.set(coords.chartWidth+1, coords.chartPowerY - 1, "Max: " .. formatRF(_max, _format) .. " ")
- _stillDraw = false
- end
- function main()
- local oldW, oldH = gpu.getResolution()
- gpu.setResolution(W,H)
- term.clear()
- zn.connect()
- local evTimer = event.timer(1, draw, math.huge)
- local _wait = true
- while _wait do
- local ev,message,_,key = event.pull()
- if ev == "zn_message" then
- message = serialization.unserialize(message)
- getData(message)
- elseif ev == "key_down" then
- if key == 16 then
- _wait = false
- elseif key == 50 then
- _format = _format + 1
- if _format > 10 then
- _format = 1
- end
- end
- end
- end
- event.cancel(evTimer)
- gpu.setResolution(oldW, oldH)
- term.clear()
- zn.disconnect()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement