Advertisement
Ktlo

[OC] Liquid Monitor

Aug 12th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --Пользовательские настройки
  2. local fluids = {
  3.     Water = 0x1c86ee;
  4.     Lava = 0xee4000;
  5.     Milk = 0xfdf5e6;
  6. }
  7. local name
  8.  
  9. local comp = component
  10. local table_insert, unpack, comp_list = table.insert, table.unpack, component.list
  11. name = name or "Monitor"
  12. local color = 0x1c86ee
  13. local gpu = component.proxy(component.list("gpu")())
  14. gpu.bind(component.list("screen")())
  15. local width, height = gpu.getResolution()
  16. gpu.fill(1, 1, width, height, " ")
  17. local cell_w
  18. local function drawBox(x, y, w, h)
  19.     gpu.fill(x+1, y, w-2, 1, "═")
  20.     gpu.set(x, y, "╔")
  21.     gpu.set(x+w-1, y, "╗")
  22.     for i=1, h-2 do
  23.         gpu.set(x, y+i, "║")
  24.         gpu.set(x+w-1, y+i, "║")
  25.     end
  26.     gpu.fill(x+1, y+h-1, w-2, 1, "═")
  27.     gpu.set(x, y+h-1, "╚")
  28.     gpu.set(x+w-1, y+h-1, "╝")
  29. end
  30.  
  31. local meta = {
  32.     __index = {
  33.         update = function(self)
  34.             local side, tank, n = unpack(self)
  35.             local x, y = cell_w*(n-1)+3, 5
  36.             local info = tank.getFluidInTank(side)[1]
  37.             if not info.label then return end
  38.             local k = info.amount/info.capacity
  39.             gpu.set(x, y-1, ("═"):rep(cell_w-2))
  40.             local name = (info.label or "Empty"):sub(1, cell_w-2)
  41.             gpu.set(x-1+cell_w/2-#name/2, y-1, name)
  42.             local k2 = 1 - k
  43.             local color = fluids[info.label] or color
  44.             local h = height-5
  45.             gpu.setBackground(color)
  46.             gpu.fill(x, y+h*k2, cell_w-2, h*k+0.85, "─")
  47.             gpu.setBackground(0x000000)
  48.             gpu.fill(x, y, cell_w-2, h*k2, " ")
  49.             if k ~= 1 then
  50.                 if info.amount <= (info.capacity/h)*(h/2) then
  51.                     gpu.setBackground(0x000000)
  52.                 else
  53.                     gpu.setBackground(color)
  54.                 end
  55.                 gpu.set(x+cell_w/2-#tostring(info.amount)/2-2, y+h/2-1, info.amount.." mb")
  56.             end
  57.             gpu.setBackground(0x000000)
  58.             gpu.set(x, y+h, ("max "..info.capacity.." mb"):sub(1, cell_w-2))
  59.         end;
  60.         draw = function(self)
  61.             local side, tank, n = unpack(self)
  62.             local x, y = cell_w*(n-1)+2, 4
  63.             drawBox(x, y, cell_w, height-3)
  64.             for i=1, height-5 do
  65.                 gpu.set(x, y+i, "╟")
  66.                 gpu.set(x+cell_w-1, y+i, "╢")
  67.             end
  68.             self:update()
  69.         end;
  70.     };
  71. }
  72.  
  73. local tank_list = { }
  74. for address, name in comp_list("tank_controller") do
  75.     local comp = comp.proxy(address)
  76.     for i=1, 5 do
  77.         if comp.getTankCapacity(i) then
  78.             table_insert(tank_list, setmetatable({i, comp, #tank_list+1}, meta))
  79.         end
  80.     end
  81. end
  82. cell_w = math.floor(width/(#tank_list)-1)
  83.  
  84. gpu.setBackground(0x000000)
  85. gpu.setForeground(0xffffff)
  86. drawBox(math.floor(width/2-name:len()/2), 1, #name+2, 3)
  87. gpu.set(math.floor(width/2-name:len()/2)+1, 2, name)
  88. for key, tank in ipairs(tank_list) do
  89.     tank:draw()
  90. end
  91.  
  92. while true do
  93.     for key, tank in ipairs(tank_list) do
  94.         tank:update()
  95.     end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement