SHARE
TWEET

ComputerCraft Tutorial: auto_craft0_3

hevohevo Dec 8th, 2013 985 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ###########################
  2. -- auto_craft <recipe_number>
  3. -- version 0.3
  4. -- http://hevohevo.hatena.com/
  5.  
  6.  
  7. -- ###########################
  8. -- Turtle position (Side view)
  9. -- T: crafty turtle, M: chest for materials, P: chest for products
  10.  
  11. -- M
  12. -- T
  13. -- P
  14.  
  15.  
  16. -- ##########################
  17. -- config
  18. MATERIAL_SLOT = 1
  19. PRODUCT_SLOT = 4
  20.  
  21. N_DEFAULT_RECIPE = 1
  22.  
  23. -- RECIPE[<number>] = table: <numbers of slots>
  24. RECIPE = {}
  25. RECIPE[1] = {1}    -- 1x1
  26. RECIPE[2] = {1,2,5,6}             -- 2x2
  27. RECIPE[3] = {1,2,3,5,6,7,9,10,11} -- 3x3
  28. RECIPE[4] = {1,2,3,5,7,9,10,11}   -- 3x3donut
  29.  
  30.  
  31. -- ##########################
  32. -- functions
  33. function dropItems(begin_slot, end_slot)
  34.   print('drop items: slots ',begin_slot,'-',end_slot)
  35.   for i=begin_slot, end_slot do
  36.     turtle.select(i)
  37.     turtle.dropDown()
  38.   end
  39.   turtle.select(1)
  40. end
  41.  
  42. function getMaterials()
  43.   turtle.select(MATERIAL_SLOT)
  44.   if turtle.getItemCount(MATERIAL_SLOT) ==0 and turtle.suckUp() then
  45.     print(' Success: suckUp items')
  46.     return true
  47.   else
  48.     print(' Failed: suckUp items')
  49.     return false
  50.   end
  51. end
  52.  
  53. -- distMaterials({1,2,5,6}) -- 2x2
  54. -- distMaterials({1,2,3,5,7,9,10,11}) -- 3x3donut
  55. function distMaterials(slots)
  56.   local nTotal = turtle.getItemCount(MATERIAL_SLOT)
  57.   local n = nTotal/(#slots)
  58.  
  59.   turtle.select(MATERIAL_SLOT)
  60.   for i,v in pairs(slots) do
  61.     turtle.transferTo(v,n)
  62.   end
  63. end
  64.  
  65. function auto_craft()
  66.   while  turtle.getItemCount(MATERIAL_SLOT) >0 do
  67.     if turtle.craft(0) == false then break end
  68.  
  69.     turtle.select(PRODUCT_SLOT)
  70.     turtle.craft()
  71.     turtle.dropDown()
  72.     sleep(0.1)
  73.   end
  74. end
  75.  
  76.  
  77. -- ###########################
  78. -- main
  79. args = {...}
  80. if args[1] and RECIPE[tonumber(args[1])] then
  81.   slots_pattern = RECIPE[tonumber(args[1])]
  82.   print('Recipe No.',args[1])
  83. else
  84.   slots_pattern = RECIPE[N_DEFAULT_RECIPE]
  85.   print("Use Default Recipe.")
  86. end
  87.  
  88. dropItems(1,16)
  89.  
  90. while getMaterials() do
  91.   distMaterials(slots_pattern)
  92.   auto_craft()
  93.   dropItems(1,4)
  94.   sleep(1)
  95. end
  96.  
  97. dropItems(1,16)
RAW Paste Data
Top