Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- -- Read what our current recipe is and store the slots each item belongs in
- myinventory = {}
- for i=1,12 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if item then
- name = item["name"]..":"..item["damage"]
- if not myinventory[name] then
- myinventory[name] = {}
- end
- table.insert(myinventory[name], i)
- print("I found a "..name.." in slot "..i)
- end
- end
- -- Start off with having at most 2 items. As we move items into the turtle increase this rate
- max_count_per_slot = 4
- -- Consider the inventory (jabba barrel or chest...) below us has all the items we need
- resources = peripheral.wrap(args[1])
- directions = {["bottom"] = "up", ["up"] = "bottom", ["left"] = "right", ["right"] = "left", ["back"]="front", ["front"]="back"}
- direction_to_pull = directions[args[1]]
- while true do
- -- Assume that it has a method getAllStacks which returns a table, wherein each element has a "id" and "dmg" field
- contents = resources.getAllStacks(false)
- -- Search bottom inventory for items and fill our inventory up with needed items
- --print(textutils.serialize(contents))
- for i, v in pairs(contents) do
- name = v["id"]..":"..v["dmg"]
- print("Found a "..name)
- for j, s in pairs(myinventory[name]) do
- print("Checking against slot "..s.."")
- turtle.select(s)
- diff = max_count_per_slot - turtle.getItemCount()
- if diff > 0 then
- moved = resources.pushItemIntoSlot(direction_to_pull, i, diff, s)
- -- Adapt to a higher product rate if supply is good enough
- if moved == diff then
- max_count_per_slot = math.min(64, max_count_per_slot + 5)
- end
- print("Topping up on "..name)
- end
- end
- end
- -- figure out the bottleneck item in recipe
- bottleneck = 64
- bottleneck_slot = 1
- for name, slots in pairs(myinventory) do
- for i, slot in pairs(slots) do
- turtle.select(slot)
- cnt = turtle.getItemCount()
- if cnt < bottleneck then
- bottleneck = cnt
- bottleneck_slot = slot
- end
- end
- end
- --print("The bottleneck is "..name.." with only "..bottleneck.." left")
- left = bottleneck
- repeat
- -- craft items into slot 16 and push them forward.
- turtle.select(16)
- print("Crafting "..(left-1).." times")
- turtle.craft(left-1)
- able_to_drop = turtle.drop()
- -- Check the bottleneck item.
- turtle.select(bottleneck_slot)
- left = turtle.getItemCount()
- print("I see "..left.." left in slot "..bottleneck_slot)
- -- keep going as long as we have any left
- until left < 2 or not able_to_drop
- --
- if able_to_drop then
- os.sleep(1)
- else
- print("No space left for depositing product")
- os.sleep(10)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment