Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- open turtles
- rednet.open("left")
- -- get list of turtles
- local turtles = rednet.lookup("storage")
- print("Turtles: "..turtles)
- -- the count of items needed
- local count = 0
- -- the current requested id
- local id = "minecraft:stone"
- function take(net) -- turtles network id
- -- send item id to turtle
- rednet.send(net, id, "item_request_turtle")
- print("requesting " .. id .. " from " .. net)
- while true do
- -- wait for response from turtle, the turtle returns how much of the item he has found
- trt,msg = rednet.receive("item_request_turtle")
- print(msg)
- -- 0 items found, break loop
- if msg == 0 then
- break
- end
- if msg >= count then
- msg = count
- count = 0
- else
- count = count - msg
- end
- -- tell turtle to give up as many items as needed
- rednet.send(net, msg, "item_request_turtle")
- end
- end
- -- pre-computed list of functions to execute
- local functions = {}
- if type(turtles) == "table" then
- for i, turtle in pairs(turtles) do
- functions[i] = function()
- take(turtle)
- end
- end
- elseif(type(turtles) == "number") then
- functions[1] = function() take(turtles) end
- else
- print("no turtles found!")
- return
- end
- -- open network
- rednet.open("right")
- print("network is ready!")
- while true do
- from,msg,dis = rednet.receive("item_request_0")
- print("Processing: "..msg[2].. " of ".. msg[1])
- -- set id and count
- id = msg[1]
- count = tonumber(msg[2])
- local original = count
- -- execute order 66
- parallel.waitForAll(unpack(functions))
- write("All turtles have completed search!")
- -- allow time for ender chest to pick up items
- os.sleep(3)
- -- send real amount of items taken
- rednet.broadcast({items = original - count}, "item_request_0")
- end
- rednet.close("right")
- rednet.close("left")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement