Advertisement
thezer0th

Untitled

Dec 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local os = require("os")
  4. local thread = require("thread")
  5. local event = require("event")
  6. local shell = require("shell")
  7.  
  8. local modem = component.modem
  9. function setup_wlan()
  10. modem.open(666)
  11. modem.setStrength(512)
  12. end
  13.  
  14. function transfer_to(tp, in_side, in_slot, out_side, out_slot)
  15. tp.transferItem(in_side, out_side, tp.getStackInSlot(in_side, in_slot).count, in_slot, out_slot)
  16. end
  17.  
  18. function transfer_all(tp, in_side, out_side)
  19. local in_stacks = tp.getAllStacks(in_side)
  20. local out_slots = tp.getAllStacks(out_side)
  21. local out_slot = 1
  22. for in_slot = 1, in_stacks.count() do
  23. while out_slots[out_slot] != nil do out_slot = out_slot + 1 end
  24. if in_stacks[in_slot] != nil then tp.transferItem(in_side, out_side, in_stacks[idx].count, in_slot, out_slot) end
  25. end
  26. end
  27.  
  28. function is_empty(tp, side)
  29. local stacks = tp.getAllStacks(side)
  30. for slot = 1, stacks.count() do
  31. if stacks[slot] != nil then return false end
  32. end
  33. return true
  34. end
  35.  
  36. function master_loop()
  37. local tp = component.transposer
  38. local rc_side = nil
  39. setup_wlan()
  40. while true do
  41. while tp.getStackInSlot(rc_side, 1) == nil do os.sleep(sleep_t) end
  42.  
  43. local prim_side, auxl_side = nil, nil
  44. local stacks = tp.getAllStacks(rc_side)
  45. local prim_idx = (local function ()
  46. function idx_of_first(label)
  47. for idx = 1, stacks.count() do
  48. if stacks[idx] != nil && stacks[idx].label == label then return idx end
  49. end
  50. return nil
  51. end
  52. if idx_of_first("Dragonstone") != nil then return idx_of_first("Dragonstone")
  53. elseif idx_of_first("Nether Star") != nil then return idx_of_first("Nether Star")
  54. elseif idx_of_first("Mirrored Glass") != nil then return idx_of_first("Mirrored Glass")
  55. elseif idx_of_first("Magic Mirror") != nil then return idx_of_first("Magic Mirror")
  56. elseif idx_of_first("ME Interface") != nil then return idx_of_first("ME Interface")
  57. elseif idx_of_first("Thaumonomicon") != nil then return idx_of_first("Thaumonomicon")
  58. elseif idx_of_first("Alchemical Furnace") != nil then return idx_of_first("Alchemical Furnace")
  59. elseif idx_of_first("Witches Oven") != nil then return idx_of_first("Witches Oven")
  60. else return nil end
  61. end) ()
  62.  
  63. transfer_to(tp, rc_side, idx, prim_side, 1)
  64. transfer_all(tp, rc_side, auxl_side)
  65.  
  66. local altar_addr = nil
  67. function wait_for_free_altar(event_name, ...)
  68. if event_name == "modem_message" then
  69. if select(3, ...) == "altar_available" then return true end
  70. end
  71. return false
  72. end
  73. while altar_addr == nil do
  74. modem.broadcast(port, "repl_if_available")
  75. altar_addr = select(1 + 1, event.pullFiltered(timeout_t, wait_for_free_altar))
  76. end
  77.  
  78. modem.send(altar_addr, port, "proceed")
  79. modem.broadcast(port, "cease")
  80. function wait_for_free_buffers(event_name, ...)
  81. if event_name == "modem_message" then
  82. if select(3, ...) == "buffers_freed" then return true end
  83. end
  84. return false
  85. end
  86. event.pullFiltered(wait_for_free_buffers)
  87. end
  88. end
  89.  
  90. function altar_loop()
  91. local tp = component.transposer
  92. local prim_in_side = sides.west
  93. local prim_out_side = sides.down
  94. local auxl_in_side = sides.east
  95. local auxl_out_side = sides.south
  96. local output_side = sides.north
  97. setup_wlan()
  98. while true do
  99. function wait_for_crafting_request(event_name, ...)
  100. if event_name == "modem_message" then
  101. if select(3, ...) == "repl_if_available" then return true end
  102. end
  103. return false
  104. end
  105. local master_addr = select(1 + 1, event.pullFiltered(wait_for_crafting_request))
  106.  
  107. modem.send(master_addr, port, "altar_available")
  108. function wait_for_master_response(event_name, ...)
  109. if event_name == "modem_message" then
  110. if select(3, ...) == "proceed" || select(3, ...) == "cease" then return true end
  111. end
  112. return false
  113. end
  114. if select(1 + 3, event.pullFiltered(wait_for_master_response)) == "proceed" then
  115. transfer_all(tp, auxl_in_side, auxl_out_side)
  116. while not is_empty(tp, auxl_out_side) do os.sleep(sleep_t) end
  117.  
  118. local orig_prim_stack = tp.getStackInSlot(prim_in_side, 1)
  119. transfer_all(tp, prim_in_side, prim_out_side)
  120. while not is_empty(tp, prim_out_side) do os.sleep(sleep_t) end
  121.  
  122. modem.send(master_addr, port, "buffers_freed")
  123. while not has_center_changed(orig_prim_stack) do os.sleep(sleep_t) end
  124.  
  125. transfer_all(tp, prim_out_side, output_side)
  126. while not is_empty(tp, prim_out_side) do os.sleep(sleep_t) end
  127. end
  128. end
  129. end
  130.  
  131. function main()
  132. local args, _ = shell.parse(...)
  133. if #args == 1 then
  134. if args[0] == "master" then master_loop()
  135. else altar_loop() end
  136. end
  137. end
  138.  
  139. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement