Advertisement
Guest User

Untitled

a guest
May 10th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. local component = require("component")
  2. local serialization = require("serialization")
  3. local event = require("event")
  4. local term = require("term")
  5. local gpu = component.gpu
  6. local zn = require("zn")
  7. local charts = require("charts")
  8. local maxGas = 245000000
  9. local _current,_max,_out,_in = 0,0,0,0
  10. local _stillDraw, _stillWait
  11. local _format = 1
  12. local W,H=160,50
  13. local coords = {
  14.     chartPowerX = 1,
  15.     chartPowerY = 2,
  16.     lastY = H,
  17.     chartHeight = math.floor((H-3)/2),
  18.     chartWidth = math.floor(W*2/3),
  19.     progressWidth = 5,
  20. }
  21. coords["chartPowerInY"]= coords.chartHeight + 4
  22. coords["powerY"] = coords.chartHeight+2
  23.  
  24. function params(color)
  25.     return {
  26.         x = coords.chartPowerX,
  27.         y = coords.chartPowerY,
  28.         width = coords.chartWidth,
  29.         height = coords.chartHeight,
  30.         payload = charts.Histogram {
  31.             max = 1,
  32.             align = charts.sides.RIGHT,
  33.             colorFunc = function(index, norm, value, self, container)
  34.                 return color
  35.             end
  36.         }
  37.     }
  38. end
  39.  
  40. local _params = params(0x20ff20)
  41. local containerOut = charts.Container(_params)
  42. local payloadOut = containerOut.payload
  43.  
  44.  
  45. _params = params(0xff0000)
  46. _params.y = coords.chartPowerInY
  47. local containerIn = charts.Container(_params)
  48. local payloadIn = containerIn.payload
  49.  
  50.  
  51. function colorFunc(_, perc)
  52.     if perc >= .9 then
  53.         return 0x20afff
  54.     elseif perc >= .75 then
  55.         return 0x20ff20
  56.     elseif perc >= .5 then
  57.         return 0xafff20
  58.     elseif perc >= .25 then
  59.         return 0xffff20
  60.     elseif perc >= .1 then
  61.         return 0xffaf20
  62.     else
  63.         return 0xff2020
  64.     end
  65. end
  66.  
  67.  
  68. function progress(x)
  69.     return {
  70.         x = x,
  71.         y = coords.chartPowerY,
  72.         width = coords.progressWidth,
  73.         height = coords.chartHeight,
  74.         payload = charts.ProgressBar {
  75.             direction = charts.sides.TOP,
  76.             colorFunc = colorFunc
  77.         }
  78.     }
  79. end
  80.  
  81. local containerPower = charts.Container(progress(coords.chartWidth+11))
  82. local payloadPower = containerPower.payload
  83.  
  84. local containerDeuterium = charts.Container(progress(coords.chartWidth+11 + coords.progressWidth*2))
  85. local payloadDeuterium = containerDeuterium.payload
  86.  
  87. local containerTritium = charts.Container(progress(coords.chartWidth+11 + coords.progressWidth*4))
  88. local payloadTritium = containerTritium.payload
  89.  
  90.  
  91. function getData(message)
  92.     if type(message) == "table" then
  93.         if message.from == "power_matrix" then
  94.             local data = message.data
  95.             _out = math.floor(data.output)
  96.             if payloadOut.max < _out then
  97.                 payloadOut.max = _out
  98.             end
  99.             table.insert(payloadOut.values, _out)
  100.  
  101.             _in = math.floor(data.input)
  102.             if payloadIn.max < _in then
  103.                 payloadIn.max = _in
  104.             end
  105.             table.insert(payloadIn.values, _in)
  106.  
  107.             local power = 0
  108.             if data.max > 0 then
  109.                 power = data.current/data.max
  110.             end
  111.             payloadPower.value = power
  112.  
  113.             if (#payloadOut.values > containerOut.width+1) then
  114.                 table.remove(payloadOut.values, 1)
  115.                 table.remove(payloadIn.values, 1)
  116.             end
  117.  
  118.             _current = math.floor(data.current)
  119.             _max = data.max
  120.         elseif message.from == "gases_reactor" then
  121.             local data = message.data
  122.             payloadDeuterium.value = (data.deuterium or 0)/maxGas
  123.             payloadTritium.value = (data.tritium or 0)/maxGas
  124.         end
  125.     end
  126. end
  127.  
  128.  
  129. function formatRF(RF, mode)
  130.     units = {'RF', 'kRF', 'MRF', 'GRF', 'TRF', 'PRF', 'ERF', 'ZRF', 'YRF', 'inf'}
  131.  
  132.     mode = mode or #units
  133.     local n = 1
  134.     while RF > 1 do
  135.         if mode == n or n >= #units then
  136.             break
  137.         end
  138.         RF = RF/1000
  139.         n = n + 1
  140.     end
  141.     if n>1 then
  142.       RF = RF * 1000
  143.       n = n - 1
  144.     end
  145.     RF = math.floor(RF*100)/100
  146.     return RF .. units[n]
  147. end
  148.  
  149.  
  150. function draw()
  151.     if _stillDraw then
  152.         return
  153.     end
  154.     _stillDraw = true
  155.  
  156.     containerIn:draw()
  157.     containerOut:draw()
  158.     containerPower:draw()
  159.     containerDeuterium:draw()
  160.     containerTritium:draw()
  161.  
  162.     gpu.set(coords.chartPowerX, coords.powerY, "Output: " .. formatRF(_out, _format) .. "/sec       ")
  163.     gpu.set(coords.chartPowerX, coords.lastY, "Input: " .. formatRF(_in, _format) .. "/sec     ")
  164.     gpu.set(coords.chartWidth+1, coords.powerY, "Stored: " .. formatRF(_current, _format) .. "      ")
  165.     gpu.set(coords.chartWidth+1, coords.chartPowerY - 1, "Max: " .. formatRF(_max, _format) .. "    ")
  166.     _stillDraw = false
  167. end
  168.  
  169.  
  170. function main()
  171.     local oldW, oldH = gpu.getResolution()
  172.     gpu.setResolution(W,H)
  173.     term.clear()
  174.     zn.connect()
  175.  
  176.     local evTimer = event.timer(1, draw, math.huge)
  177.  
  178.     local _wait = true
  179.     while _wait do
  180.         local ev,message,_,key = event.pull()
  181.         if ev == "zn_message" then
  182.             message = serialization.unserialize(message)
  183.             getData(message)
  184.         elseif ev == "key_down" then
  185.             if key == 16 then
  186.                 _wait = false
  187.             elseif key == 50 then
  188.                 _format = _format + 1
  189.                 if _format > 10 then
  190.                     _format = 1
  191.                 end
  192.             end
  193.         end
  194.     end
  195.  
  196.     event.cancel(evTimer)
  197.     gpu.setResolution(oldW, oldH)
  198.     term.clear()
  199.     zn.disconnect()
  200. end
  201.  
  202. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement