Advertisement
thezer0th

Untitled

Jan 3rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local os = require("os")
  4.  
  5. local tp_utils = {}
  6.  
  7. function tp_utils.first_of(stacks, pred)
  8. for slot = 1, stacks.count() do
  9. if pred(slot, stacks[slot]) then return slot, stacks[slot] end
  10. end
  11. return -1, nil
  12. end
  13.  
  14. function tp_utils.all(stacks, pred)
  15. local ret = {}
  16. for slot = 1, stacks.count() do
  17. if pred(slot, stacks[slot]) then table.insert(ret, table.pack(slot, stacks[slot])) end
  18. end
  19. return ret
  20. end
  21.  
  22. function tp_utils.is_any(stacks, pred)
  23. return select(2, tp_utils.first_of(stacks, pred)) ~= nil
  24. end
  25.  
  26. function tp_utils.are_all(stacks, pred)
  27. return not tp_utils.is_any(stacks, function(slot, stack) return not pred(slot, stack) end)
  28. end
  29.  
  30. function tp_utils.is_empty(stacks)
  31. return tp_utils.are_all(stacks, function(slot, stack) return stack == nil end)
  32. end
  33.  
  34. function tp_utils.wait_for_empty_slot(tp, side)
  35. local slot = -1
  36. while slot == -1 do
  37. slot, _ = tp_utils.first_of(tp.getAllStacks(side), function(slot, stack) return stack == nil end)
  38. os.sleep(sleep_t)
  39. end
  40. return slot
  41. end
  42.  
  43. function tp_utils.transfer_all(tp, in_side, out_side, pred)
  44. local in_stacks = tp.getAllStacks(in_side)
  45. local out_slot = 1
  46. for in_slot = 1, in_stacks.count() do
  47. while tp.getStackInSlot(out_side, out_slot) ~= nil do out_slot = out_slot + 1 end
  48. if in_stacks[in_slot] ~= nil and pred(in_slot, in_stacks[in_slot]) then tp.transferItem(in_side, out_side, in_stacks[in_slot].count, in_slot, out_slot) end
  49. end
  50. end
  51.  
  52. local function main()
  53. local tps = {}
  54. for addr, type in component.list() do
  55. if type == "transposer" then
  56. local tp = component.proxy(addr)
  57. local sys_in, drop = if tp.getInventorySize(sides.north) > tp.getInventorySize(sides.south) then sides.north, sides.south else sides.south, sides.north end
  58. table.insert(tps, { tp = tp, sys_in = sys_in, drop = drop, sys_out = sides.west, vacuum = sides.east, is_brewing = false, sys_in_size = tp.getInventorySize(sides.north)})
  59. end
  60. end
  61. while true do
  62. for _, stand in pairs(tps) do
  63. if not stand.is_brewing and stand.tp.getStackInSlot(stand.sys_in, 1) ~= nil then
  64. for slot = 1, stand.sys_in_size do
  65. if stand.tp.getStackInSlot(stand.sys_in, slot) ~= nil then
  66. stand.tp.transferItem(stand.sys_in, stand.drop, stand.tp.getStackInSlot(stand.sys_in, slot).count, slot, 1)
  67. while stand.tp.getStackInSlot(stand.drop, 1) ~= nil do os.sleep(0.1) end
  68. end
  69. end
  70. stand.is_brewing = true
  71. elseif stand.tp.getStackInSlot(stand.vacuum, 1) ~= nil then
  72. tp_utils.transfer_all(stand.tp, stand.vacuum, stand.sys_out, function(slot, stack) return true end)
  73. end
  74. end
  75. end
  76. end
  77.  
  78. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement