Advertisement
Ktlo

[OC] Liquid Monitor v2

Aug 14th, 2015
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 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 or require "component"
  10. local computer = computer or require "computer"
  11. local table_insert, unpack, comp_list = table.insert, table.unpack, comp.list
  12. name = name or "Monitor"
  13. local color = 0x1c86ee
  14. local gpu = comp.proxy(comp.list("gpu")())
  15. gpu.bind(comp.list("screen")())
  16. local width, height = gpu.getResolution()
  17. gpu.fill(1, 1, width, height, " ")
  18. local cell_w
  19. local function drawBox(x, y, w, h)
  20.     gpu.fill(x+1, y, w-2, 1, "═")
  21.     gpu.set(x, y, "╔")
  22.     gpu.set(x+w-1, y, "╗")
  23.     for i=1, h-2 do
  24.         gpu.set(x, y+i, "║")
  25.         gpu.set(x+w-1, y+i, "║")
  26.     end
  27.     gpu.fill(x+1, y+h-1, w-2, 1, "═")
  28.     gpu.set(x, y+h-1, "╚")
  29.     gpu.set(x+w-1, y+h-1, "╝")
  30. end
  31.  
  32. local meta = {
  33.     __index = {
  34.         update = function(self, n)
  35.             local side, tank = unpack(self)
  36.             local x, y = cell_w*(n-1)+3, 5
  37.             local info = tank.getFluidInTank(side)[1]
  38.             if not info.label then return end
  39.             local k = info.amount/info.capacity
  40.             gpu.set(x, y-1, ("═"):rep(cell_w-2))
  41.             local name = (info.label or "Empty"):sub(1, cell_w-2)
  42.             gpu.set(x-1+cell_w/2-#name/2, y-1, name)
  43.             local k2 = 1 - k
  44.             local color = fluids[info.label] or color
  45.             local h = height-5
  46.             gpu.setBackground(color)
  47.             gpu.fill(x, y+h*k2, cell_w-2, h*k+0.85, "─")
  48.             gpu.setBackground(0x000000)
  49.             gpu.fill(x, y, cell_w-2, h*k2, " ")
  50.             if k ~= 1 then
  51.                 if info.amount <= (info.capacity/h)*(h/2) then
  52.                     gpu.setBackground(0x000000)
  53.                 else
  54.                     gpu.setBackground(color)
  55.                 end
  56.                 gpu.set(x+cell_w/2-#tostring(info.amount)/2-2, y+h/2-1, info.amount.." mb")
  57.             end
  58.             gpu.setBackground(0x000000)
  59.             gpu.set(x, y+h, ("max "..info.capacity.." mb"):sub(1, cell_w-2))
  60.         end;
  61.         draw = function(self, n)
  62.             local side, tank = unpack(self)
  63.             local x, y = cell_w*(n-1)+2, 4
  64.             drawBox(x, y, cell_w, height-3)
  65.             for i=1, height-5 do
  66.                 gpu.set(x, y+i, "╟")
  67.                 gpu.set(x+cell_w-1, y+i, "╢")
  68.             end
  69.             self:update(n)
  70.         end;
  71.     };
  72. }
  73.  
  74. function find()
  75. local tank_list = { }
  76. for address, name in comp_list("tank_controller") do
  77.     local comp = comp.proxy(address)
  78.     for i=1, 5 do
  79.         if comp.getTankCapacity(i) then
  80.             table_insert(tank_list, setmetatable({i, comp, #tank_list+1}, meta))
  81.         end
  82.     end
  83. end
  84. return tank_list
  85. end
  86. local tank_list = find()
  87. local s = 0
  88. gpu.setBackground(0x000000)
  89. gpu.setForeground(0xffffff)
  90. drawBox(math.floor(width/2-name:len()/2), 1, #name+2, 3)
  91. gpu.set(math.floor(width/2-name:len()/2)+1, 2, name)
  92. function red()
  93.     gpu.fill(1, 4, width, height-3, " ")
  94.     cell_w = math.floor(width/(#tank_list)-1)
  95.     cell_w = cell_w < 15 and 15 or cell_w
  96.     for n, tank in ipairs(tank_list) do
  97.         tank:draw(n-s)
  98.         if n-s > width/cell_w then break end
  99.     end
  100. end
  101. red()
  102. while true do
  103.     local e = { computer.pullSignal(0.1) }
  104.     local list = find()
  105.     if #list ~= #tank_list then
  106.         tank_list = list
  107.         red()
  108.     end
  109.     for n, tank in ipairs(tank_list) do
  110.         if n-s > width/cell_w+1 then break end
  111.         pcall(tank.update,tank,n-s)
  112.     end
  113.     if cell_w == 15 then
  114.         if e[1] == "touch" then
  115.             if e[3] > width/2 then
  116.                 s = s >= #tank_list-width/cell_w and s or s+1
  117.             else
  118.                 s = s == 0 and 0 or s-1
  119.             end
  120.             red()
  121.         end
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement