borg286

autocrafty turtle

Feb 18th, 2017
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. args = {...}
  2. -- Read what our current recipe is and store the slots each item belongs in
  3. myinventory = {}
  4. for i=1,12 do
  5.     turtle.select(i)
  6.     item = turtle.getItemDetail()
  7.     if item then
  8.         name = item["name"]..":"..item["damage"]
  9.         if not myinventory[name] then
  10.             myinventory[name] = {}
  11.         end
  12.         table.insert(myinventory[name], i)
  13.         print("I found a "..name.." in slot "..i)
  14.     end
  15. end
  16.  
  17. -- Start off with having at most 2 items. As we move items into the turtle increase this rate
  18. max_count_per_slot = 4
  19.  
  20. -- Consider the inventory (jabba barrel or chest...) below us has all the items we need
  21. resources = peripheral.wrap(args[1])
  22. directions = {["bottom"] = "up", ["up"] = "bottom", ["left"] = "right", ["right"] = "left", ["back"]="front", ["front"]="back"}
  23. direction_to_pull = directions[args[1]]
  24. while true do
  25.     -- Assume that it has a method getAllStacks which returns a table, wherein each element has a "id" and "dmg" field
  26.     contents = resources.getAllStacks(false)
  27.  
  28.     -- Search bottom inventory for items and fill our inventory up with needed items
  29.     --print(textutils.serialize(contents))
  30.     for i, v in pairs(contents) do
  31.         name = v["id"]..":"..v["dmg"]
  32.         print("Found a "..name)
  33.         for j, s in pairs(myinventory[name]) do
  34.             print("Checking against slot "..s.."")
  35.             turtle.select(s)
  36.             diff = max_count_per_slot - turtle.getItemCount()
  37.             if diff > 0 then
  38.                 moved = resources.pushItemIntoSlot(direction_to_pull, i, diff, s)
  39.                 -- Adapt to a higher product rate if supply is good enough
  40.                 if moved == diff then
  41.                     max_count_per_slot = math.min(64, max_count_per_slot + 5)
  42.                 end
  43.                 print("Topping up on "..name)
  44.             end
  45.         end
  46.     end
  47.  
  48.     -- figure out the bottleneck item in recipe
  49.     bottleneck = 64
  50.     bottleneck_slot = 1
  51.     for name, slots in pairs(myinventory) do
  52.         for i, slot in pairs(slots) do
  53.             turtle.select(slot)
  54.             cnt = turtle.getItemCount()
  55.             if cnt < bottleneck then
  56.                 bottleneck = cnt
  57.                 bottleneck_slot = slot
  58.             end
  59.         end
  60.     end
  61.     --print("The bottleneck is "..name.." with only "..bottleneck.." left")
  62.  
  63.     left = bottleneck
  64.     repeat
  65.         -- craft items into slot 16 and push them forward.
  66.         turtle.select(16)
  67.         print("Crafting "..(left-1).." times")
  68.         turtle.craft(left-1)
  69.         able_to_drop = turtle.drop()
  70.         -- Check the bottleneck item.
  71.         turtle.select(bottleneck_slot)
  72.         left = turtle.getItemCount()
  73.         print("I see "..left.." left in slot "..bottleneck_slot)
  74.         -- keep going as long as we have any left
  75.     until left < 2 or not able_to_drop
  76.  
  77.     --
  78.     if able_to_drop then
  79.         os.sleep(1)
  80.     else
  81.         print("No space left for depositing product")
  82.         os.sleep(10)
  83.     end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment