Advertisement
Dev_S

CC:T_AE_controller

Jun 6th, 2020
1,779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1.  
  2. -- open turtles
  3. rednet.open("left")
  4.  
  5. -- get list of turtles
  6. local turtles = rednet.lookup("storage")
  7.  
  8. print("Turtles: "..turtles)
  9.  
  10. -- the count of items needed
  11. local count = 0
  12. -- the current requested id
  13. local id = "minecraft:stone"
  14.  
  15. function take(net) -- turtles network id
  16.     -- send item id to turtle
  17.     rednet.send(net, id, "item_request_turtle")
  18.     print("requesting " .. id .. " from " .. net)
  19.     while true do
  20.         -- wait for response from turtle, the turtle returns how much of the item he has found
  21.         trt,msg = rednet.receive("item_request_turtle")
  22.  
  23.         print(msg)
  24.         -- 0 items found, break loop
  25.         if msg == 0 then
  26.             break
  27.         end
  28.  
  29.         if msg >= count then
  30.             msg = count
  31.             count = 0
  32.         else
  33.             count = count - msg
  34.         end
  35.         -- tell turtle to give up as many items as needed
  36.         rednet.send(net, msg, "item_request_turtle")
  37.     end
  38. end
  39.  
  40. -- pre-computed list of functions to execute
  41. local functions = {}
  42.  
  43. if type(turtles) == "table" then
  44.     for i, turtle in pairs(turtles) do
  45.         functions[i] = function()
  46.             take(turtle)
  47.         end
  48.     end
  49. elseif(type(turtles) == "number") then
  50.     functions[1] = function() take(turtles) end
  51. else
  52.     print("no turtles found!")
  53.     return
  54. end
  55.  
  56. -- open network
  57. rednet.open("right")
  58. print("network is ready!")
  59.  
  60. while true do
  61.     from,msg,dis = rednet.receive("item_request_0")
  62.     print("Processing: "..msg[2].. " of ".. msg[1])
  63.     -- set id and count
  64.     id = msg[1]
  65.     count = tonumber(msg[2])
  66.     local original = count
  67.     -- execute order 66
  68.     parallel.waitForAll(unpack(functions))
  69.     write("All turtles have completed search!")
  70.     -- allow time for ender chest to pick up items
  71.     os.sleep(3)
  72.     -- send real amount of items taken
  73.     rednet.broadcast({items = original - count}, "item_request_0")
  74. end
  75.  
  76. rednet.close("right")
  77. rednet.close("left")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement