Advertisement
serafim7

жидкостный монитор [OpenComputers]

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