Advertisement
hevohevo

ComputerCraft Tutorial: auto_craft0_4

Dec 15th, 2013
1,833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. -- #################################
  2. -- auto_craft <recipe_number>
  3. -- version 0.4.2 (Collaborate with Applied Energistics)
  4. -- http://hevohevo.hatena.com/
  5.  
  6.  
  7. -- #################################
  8. -- Turtle position (Side view)
  9. --  E: ME Precious Export Bus (Redstone Mode: "Active without signal"),  
  10. --  c: cable for sending redstone signal (use MFR2's rednet-cable, or RedPower's cable)
  11. --  B: chest for materials (buffer), T: this crafty turtle, I: ME Interface
  12.  
  13. -- EB
  14. -- CT
  15. --  I
  16.  
  17.  
  18. -- ################################
  19. -- Config
  20.  
  21. -- item slots
  22. MATERIAL_SLOT = 1
  23. PRODUCT_SLOT = 4
  24.  
  25. -- Waits for items to accumulate in the material chest
  26. SUSPENDED_TIME = 30 -- (sec)
  27.  
  28. -- For Minefactory Reloaded2 RedNet cable
  29. -- Sets a redstone signal while crafting
  30. CABLE_DIR = "left"
  31. CABLE_COLOR = colors.white --Colors API
  32.  
  33. -- For setting recipe
  34. -- RECIPE[<number>] = table: <numbers of slots>
  35. N_DEFAULT_RECIPE = 1
  36. RECIPE = {}
  37. RECIPE[1] = {1}    -- 1x1
  38. RECIPE[2] = {1,2,5,6}             -- 2x2
  39. RECIPE[3] = {1,2,3,5,6,7,9,10,11} -- 3x3
  40. RECIPE[4] = {1,2,3,5,7,9,10,11}   -- 3x3donut
  41.  
  42.  
  43. -- ################################
  44. -- functions
  45. function dropItems(begin_slot, end_slot)
  46.   print('drop items: slots ',begin_slot,'-',end_slot)
  47.   for i=begin_slot, end_slot do
  48.     turtle.select(i)
  49.     turtle.dropDown()
  50.   end
  51.   turtle.select(1)
  52. end
  53.  
  54. function getMaterials()
  55.   turtle.select(MATERIAL_SLOT)
  56.   if turtle.getItemCount(MATERIAL_SLOT) ==0 and turtle.suckUp() then
  57.     print(' Success: suckUp items')
  58.     return true
  59.   else
  60.     print(' Failed: suckUp items')
  61.     return false
  62.   end
  63. end
  64.  
  65. -- distMaterials({1,2,5,6}) -- 2x2
  66. -- distMaterials({1,2,3,5,7,9,10,11}) -- 3x3donut
  67. function distMaterials(slots)
  68.   local nTotal = turtle.getItemCount(MATERIAL_SLOT)
  69.   local n = nTotal/(#slots)
  70.  
  71.   turtle.select(MATERIAL_SLOT)
  72.   if n >=1 then -- succeed
  73.     for i,v in pairs(slots) do
  74.       turtle.transferTo(v,n)
  75.     end
  76.     return true
  77.   else -- failed
  78.     turtle.dropDown()
  79.     return false
  80.   end
  81. end
  82.  
  83. function auto_craft()
  84.   while  turtle.getItemCount(MATERIAL_SLOT) >0 do
  85.     if turtle.craft(0) == false then break end
  86.  
  87.     turtle.select(PRODUCT_SLOT)
  88.     turtle.craft()
  89.     turtle.dropDown()
  90.     sleep(0) -- sleep 1 tick(0.05sec)
  91.   end
  92. end
  93.  
  94. -- #################################
  95. -- main
  96. args = {...}
  97. if args[1] and RECIPE[tonumber(args[1])] then
  98.   slots_pattern = RECIPE[tonumber(args[1])]
  99.   print('Recipe No.',args[1])
  100. else
  101.   slots_pattern = RECIPE[N_DEFAULT_RECIPE]
  102.   print("Use Default Recipe.")
  103. end
  104.  
  105. print("Push CTRL+T to terminate.")
  106. dropItems(1,16)
  107.  
  108. while true do
  109.   rs.setBundledOutput(CABLE_DIR, CABLE_COLOR) -- turn ON
  110.  
  111.   while getMaterials() and distMaterials(slots_pattern) do
  112.     auto_craft()
  113.     dropItems(1,4)
  114.     sleep(0)
  115.   end
  116.  
  117.   rs.setBundledOutput(CABLE_DIR, 0) -- turn OFF
  118.   sleep(SUSPENDED_TIME)  
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement