This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheKevroar's profile picture

AutoCrafting Items with Turtles

Started by TheKevroar, 05 January 2013 - 07:30 AM
TheKevroar #1
Posted 05 January 2013 - 08:30 AM
Godd evening,

I have a little problme with this program… I tried to create a program to craft every item from vanilla…
I made a table for the items and later i try to craft them. To avoid making an entry for every item i tried to regroup item with the same pattern in the crafting grid.
Ex.: recipe of torch = recipe of lever

When I try to run my program I get an error on line 246. An unexpected symbol. But I don't know how to solve this.

Here is my code
Spoiler

--[[Recipe List]]--
recipes = {

["Stick"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 0, [10]= 1, [11]= 0
  },
},
["Torch"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Minecart"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Axe"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 0,
   [5] = 1, [6] = 2, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Pickaxe"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 0, [6] = 2, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Shovel"] = {
  map = {
   [1] = 0, [2] = 1, [3] = 0,
   [5] = 0, [6] = 2, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Hoe"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 0,
   [5] = 0, [6] = 2, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Flint and Steel"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 0, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Bucket"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 0, [10]= 1, [11]= 0
  },
},
["Compass"] = {
  map = {
   [1] = 0, [2] = 1, [3] = 0,
   [5] = 1, [6] = 2, [7] = 1,
   [9] = 0, [10]= 1, [11]= 0
  },
},
["Map"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 1, [6] = 2, [7] = 1,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Fishing Rod"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 1,
   [5] = 0, [6] = 1, [7] = 2,
   [9] = 1, [10]= 0, [11]= 2
  },
},
["Shears"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 1, [10]= 0, [11]= 0
  },
},
["Fire Charge"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 2, [7] = 0,
   [9] = 0, [10]= 3, [11]= 0
  },
},
["Sword"] = {
  map = {
   [1] = 0, [2] = 1, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Bow"] = {
  map = {
   [1] = 0, [2] = 1, [3] = 2,
   [5] = 1, [6] = 0, [7] = 2,
   [9] = 0, [10]= 1, [11]= 2
  },
},
["Arrows"] = {
  map = {
   [1] = 0, [2] = 1, [3] = 0,
   [5] = 0, [6] = 2, [7] = 0,
   [9] = 0, [10]= 3, [11]= 0
  },
},
["Helmet"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 1, [7] = 1,
   [9] = 0, [10]= 0, [11]= 1
  },
},
["Chestplate"] = {
  map = {
   [1] = 1, [2] = 0, [3] = 1,
   [5] = 1, [6] = 1, [7] = 1,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Leggings"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 1, [10]= 0, [11]= 1
  },
},
["Boots"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 1, [10]= 0, [11]= 1
  },
},
["Door"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 0,
   [5] = 1, [6] = 1, [7] = 0,
   [9] = 1, [10]= 1, [11]= 0
  },
},
["Redstone Repeater"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 2, [7] = 1,
   [9] = 3, [10]= 3, [11]= 3
  },
},
[" Bowl"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 0, [10]= 1, [11]= 0
  },
},
["Bread"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 0, [7] = 0,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Sugar Cane"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 0, [10]= 0, [11]= 0
  },
},
["Cake"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 2, [6] = 3, [7] = 2,
   [9] = 4, [10]= 4, [11]= 4
  },
},
["Cookie"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 0, [7] = 0,
   [9] = 1, [10]= 2, [11]= 1
  },
},
["Melon Block"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 1, [6] = 1, [7] = 1,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Sign"] = {
  map = {
   [1] = 1, [2] = 1, [3] = 1,
   [5] = 1, [6] = 1, [7] = 1,
   [9] = 0, [10]= 2, [11]= 0
  },
},
["Book"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 1, [7] = 0,
   [9] = 1, [10]= 2, [11]= 0
  },
},
["Beds"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 1, [6] = 1, [7] = 1,
   [9] = 2, [10]= 2, [11]= 2
  },
},
["Cauldron"] = {
  map = {
   [1] = 1, [2] = 0, [3] = 1,
   [5] = 1, [6] = 0, [7] = 1,
   [9] = 1, [10]= 1, [11]= 1
  },
},
["Brewing Stand"] = {
  map = {
   [1] = 0, [2] = 0, [3] = 0,
   [5] = 0, [6] = 1, [7] = 0,
   [9] = 2, [10]= 2, [11]= 2
  },
},
}
--[[Equivalence]]--
recipes["Minecart with Furnace"] = recipes["Minecart with Chest"] = recipes["Redstone Torch"] = recipes["Lever"] = recipes["Eye of Ender"] = recipes["Light Gray Dye"] = recipes["Gray Dye"] = recipes["Rose Red"] = recipes["Orange Dye"] = recipes["Dandelion Yellow"] = recipes["Lime Dye"] = recipes["Light Blue Dye"] = recipes["Cyan Dye"] = recipes["Purple Dye"] = recipes["Magenta Dye"] = recipes["Pink Dye"] = recipes["Magma Cream"] = recipes["Glistering Melon"] = recipes["Torch"]
recipes["Minecart"] = recipes["Boat"]
recipes["Compass"] = recipes["Clock"]
recipes["Flint and Steel"] = recipes["Carrot on a Stick"]
recipes["Fire Charge"] = recipes["Mushroom Stew"] = recipes["Pumpkin Pie"] = recipes["Book and Quill"] = recipes["Fermented Spider Eye"]
recipes["Map"] = recipes["Golden Apple"] = recipes["Enchanted Golden Apple"] = recipes["Golden Carrot"] = recipes["Painting"] = recipes["Item Frame"]
recipes["Sugar Cane"] = recipes["Melon Seeds"] = recipes["Pumpkin Seeds"] = recipes["Minerals"] = recipes["Bone Meal"] = recipes["Blaze Powder"] = recipes["Gold Nugget"]
recipes["Bread"] = recipes["Paper"]
recipes["Melon Block"] = recipes["Gold Ingot"]
recipes["Bucket"] = recipes["Flower Pot"] = recipes["Glass Bottle"]
--[[Craft]]--
function craftRecipe(recipe)
for k,v in pairs(recipe.map) do
  turtle.select(k)
  if v==1 then
   turtle.select(4)
   turtle.transferTo(k, 1)
  elseif v==2 then
   turtle.select(8)
   turtle.transferTo(k, 1)
  elseif v==3 then
   turtle.select(12)
   turtle.transferTo(k, 1)
  elseif v==4 then
   turtle.select(13)
   turtle.transferTo(k, 1)
  elseif v==5 then
   turtle.select(14)
   turtle.transferTo(k, 1)
  elseif v==6 then
   turtle.select(15)
   turtle.transferTo(k, 1)
  elseif v==7 then
   turtle.select(16)
   turtle.transferTo(k, 1)
  
  end
end
turtle.select(1)
end

--[[Run Function]]--
craftRecipe(recipes["Torch"])


or the version of pastebin

http://pastebin.com/EpEPB8ps

Thank you for your attention and help.
Have a nice evening everybody!!!!
Doyle3694 #2
Posted 05 January 2013 - 08:57 AM
first of all, you can't like quadruple assign stuff.

Second, you are just setting torch to nil there.
ChunLing #3
Posted 05 January 2013 - 09:49 AM
Also, you probably want to be careful about assigning subtables like that. Table assignments assign the reference to the table, they don't create a copy of the table, so you could have some unexpected behavior from that. But perhaps not.
TheKevroar #4
Posted 05 January 2013 - 10:21 AM
Any ideas on how to solve this??
ChunLing #5
Posted 05 January 2013 - 10:38 AM
I'm not entirely sure you need to, as long as you won't be altering the crafting maps, there is no reason that sharing the maps by reference should be a problem. It's just something to be careful about.

If you do need to copy a table, then you need to use a for k,v in pairs(table) loop (or just have a set table generating function or something).

In this case, I'm puzzled that your map tables do so little that they can be copied. Why not have different values in the table to indicate the different ingredients needed? You'll just have to store that data somewhere else anyway, right?
TheKevroar #6
Posted 06 January 2013 - 11:45 AM
You are right, i still don't know how to "assign" the materials in my program… maybe i will re-doit from scratch and implement the materials right into the recipe…
ChunLing #7
Posted 06 January 2013 - 03:19 PM
That would seem the most natural solution. Otherwise you'd have to have another table or something to input which set of ingredients would be used for each recipie, which seems cumbersome.