Advertisement
HarvDad

craft

May 20th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. -- craft: Craft the named item
  2. -- Version 2.0
  3.  
  4. usage = "usage: craft <itemName>"
  5.  
  6. recipes = {
  7. {"2x2", 1, 1, 0, 1, 1, 0, 0, 0, 0},
  8. {"3x3", 1, 1, 1, 1, 1, 1, 1, 1, 1},
  9. {"chest", 1, 1, 1, 1, 0, 1, 1, 1, 1},
  10. {"furnace", 1, 1, 1, 1, 0, 1, 1, 1, 1},
  11. {"stairs", 1, 0, 0, 1, 1, 0, 1, 1, 1},
  12. {"slab", 1, 1, 1, 0, 0, 0, 0, 0, 0},
  13. {"door", 1, 1, 0, 1, 1, 0, 1, 1, 0},
  14. {"wall", 1, 1, 1, 1, 1, 1, 0, 0, 0},
  15. {"fence", 1, 1, 1, 1, 1, 1, 0, 0, 0},
  16. }
  17.  
  18. -- getIndex: Return the array index of the entry containing <name>
  19.  
  20. function printList()
  21. for i=1,#recipes do
  22. print(" ", recipes[i][1])
  23. end
  24. end
  25.  
  26. function getIndex(name)
  27. rv = 0 -- return value
  28. nItems = #recipes
  29.  
  30. for i= 1,nItems do
  31. if name == recipes[i][1] then
  32. rv = i
  33. break
  34. end
  35. end
  36.  
  37. return(rv)
  38. end
  39.  
  40. function requiredQty(recipe)
  41. qty = 0
  42.  
  43. for i=2,10 do
  44. if recipe[i] > 0 then
  45. qty = qty + 1
  46. end
  47. end
  48.  
  49. return qty
  50. end
  51.  
  52. function occupiedSlotCount()
  53. count = 0
  54. for i=1,16 do
  55. if turtle.getItemCount(i) > 0 then
  56. count = count + 1
  57. end
  58. end
  59.  
  60. return count
  61. end
  62.  
  63. function highestMultipleOf(number, multiplier)
  64. return number - (number % multiplier)
  65. end
  66.  
  67. function forceGoodMultiple(recipe)
  68. req = requiredQty(recipe)
  69. maxSlotCount = 64
  70. slotCount = 0
  71. maxSlotMultiple = highestMultipleOf(maxSlotCount, req)
  72. slotMultiple = 0
  73.  
  74. turtle.select(1)
  75. turtle.dropUp() -- clear this slot for a fresh start
  76.  
  77. turtle.suckUp()
  78. slotCount = turtle.getItemCount(1) -- see how much we got
  79. if slotCount > 64 then
  80. print("Holy crap! It gave us ", slotCount)
  81. return
  82. else
  83. -- print("Actual items from chest: ", slotCount)
  84. end
  85.  
  86. slotMultiple = highestMultipleOf(slotCount, req)
  87. -- overage = maxSlotMultiple - slotMultiple
  88. overage = slotCount % req
  89. -- print("slotCount: ", slotCount, ", req: ", req, ", slotMultiple: ", slotMultiple)
  90. -- print(" overage = ", overage)
  91. if overage > 0 then
  92. turtle.dropUp(overage)
  93. count = turtle.getItemCount(1)
  94. end
  95. -- print("Forced count = ", slotCount)
  96. return count
  97. end
  98.  
  99. function getMaterial(recipe)
  100. count = forceGoodMultiple(recipe)
  101. -- print(" Slot now contains: ", count)
  102. sleep(3)
  103.  
  104. return count
  105. end
  106.  
  107. function loadTable(recipe, qty)
  108. turtle.select(1)
  109.  
  110. if recipe[2] > 0 then turtle.transferTo(5, qty) end
  111. if recipe[3] > 0 then turtle.transferTo(6, qty) end
  112. if recipe[4] > 0 then turtle.transferTo(7, qty) end
  113.  
  114. if recipe[5] > 0 then turtle.transferTo(9, qty) end
  115. if recipe[6] > 0 then turtle.transferTo(10, qty) end
  116. if recipe[7] > 0 then turtle.transferTo(11, qty) end
  117.  
  118. if recipe[8] > 0 then turtle.transferTo(13, qty) end
  119. if recipe[9] > 0 then turtle.transferTo(14, qty) end
  120. if recipe[10] > 0 then turtle.transferTo(15, qty) end
  121. end
  122.  
  123. function clearInventory()
  124. for i=1,16 do
  125. turtle.select(i)
  126. turtle.dropUp()
  127. end
  128. turtle.select(1)
  129. end
  130.  
  131. function craftRecipe(recipe)
  132. req = requiredQty(recipe)
  133. count = getMaterial(recipe)
  134. occupiedSlots = occupiedSlotCount()
  135. slotCount = turtle.getItemCount(1)
  136.  
  137. if occupiedSlots ~= req then
  138. qty = slotCount / req
  139. loadTable(recipe, qty)
  140. slotCount = turtle.getItemCount(1)
  141. if slotCount ~= 0 then
  142. print("Wait. Slot 1 still has ", slotCount)
  143. end
  144. end
  145.  
  146. occupiedSlots = occupiedSlotCount()
  147. -- print("occupied slots: ", occupiedSlots, ", req: ", req)
  148. if occupiedSlotCount() == req then
  149. turtle.select(16)
  150. if not turtle.craft() then
  151. print("Crafting failed. Wrong material?")
  152. turtle.select(1)
  153. return false
  154. else
  155. turtle.select(16)
  156. turtle.drop()
  157. turtle.select(1)
  158. end
  159. end
  160.  
  161. return true
  162. end
  163.  
  164. -- Main program
  165.  
  166. args = {...}
  167. nArgs = #args
  168.  
  169. if nArgs == 0 then
  170. print(usage)
  171. printList()
  172. return
  173. end
  174.  
  175. if nArgs ~= 1 then
  176. print(usage)
  177. return
  178. end
  179.  
  180. itemName = args[1]
  181. index = getIndex(itemName)
  182. if index == 0 then
  183. print("Can't find \"", itemName, "\"")
  184. return
  185. else
  186. recipe = recipes[index]
  187. req = requiredQty(recipe)
  188. print("Crafting recipe: ", recipe[1])
  189. print(req, " blocks material required for each.")
  190. print("Run must be terminated manually.")
  191. clearInventory()
  192. turtle.select(1)
  193. while true do
  194. if not craftRecipe(recipe) then
  195. break
  196. end
  197. sleep(0)
  198. end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement