Advertisement
neo34rd

craft.lua

Apr 30th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. os.loadAPI("chests")
  2. os.loadAPI("inspectchest")
  3. os.loadAPI("dump")
  4. validChests = chests.loadValidChests()
  5. chestList = chests.getAdjChests(validChests)
  6.  
  7. inputChest = peripheral.wrap("top")
  8.  
  9. loadedRecipes = {}
  10.  
  11. gQty = 0
  12.  
  13. function learnNewCraft()
  14. local recipe = {}
  15. getCurrentRecipe(recipe)
  16. turtle.select(1)
  17. b = turtle.craft()
  18. if b == true then
  19. print("Success!")
  20. print("New recipe learnt:")
  21. sleep(0.6)
  22. d = turtle.getItemDetail()
  23. recipe.name = d.name
  24. recipe.damage = d.damage
  25. printRecipe(recipe)
  26. saveNewRecipe(recipe)
  27. else
  28. print("invalid items")
  29. end
  30. end
  31.  
  32.  
  33. function learnNewCraftWithHint(hint)
  34. local recipe = {}
  35. getCurrentRecipe(recipe)
  36. turtle.select(1)
  37. b = turtle.craft()
  38. if b == true then
  39. print("Success!")
  40. print("New recipe learnt:")
  41. sleep(0.6)
  42. d = turtle.getItemDetail()
  43. recipe.name = d.name
  44. recipe.damage = d.damage
  45. recipe["hint"] = hint
  46. printRecipe(recipe)
  47. saveNewRecipe(recipe)
  48. else
  49. print("invalid items")
  50. end
  51. end
  52.  
  53.  
  54. function saveNewRecipe(recipe)
  55. local file = fs.open("recipes.txt", "a")
  56. h = {}
  57. t = {}
  58. h[#h+1] = tostring(recipe.name)
  59. h[#h+1] = tostring(recipe.damage)
  60. h[#h+1] = tostring(recipe.hint)
  61. for i = 1, 16 do
  62. if recipe.slot[i] ~= nil then
  63. t[#t+1] = tostring(i)
  64. t[#t+1] = tostring(recipe.slot[i].name)
  65. t[#t+1] = tostring(recipe.slot[i].damage)
  66. end
  67. end
  68. s = table.concat(h, " ")
  69. file.write(s .. "\n")
  70. s = table.concat(t, " ")
  71. file.write(s .. "\n")
  72. file.close()
  73. end
  74.  
  75.  
  76. function loadAllRecipes()
  77. print("Loading all recipes in recipes.txt...")
  78. local file = fs.open("recipes.txt", "r")
  79. local fileData = {}
  80. line = nil
  81. repeat
  82. line = file.readLine()
  83. if line ~= nil then
  84. fileData[#fileData+1] = line
  85. end
  86. until line == nil
  87. file.close()
  88. print("Loaded ", #fileData, " line of data")
  89.  
  90. local loadedRecipes = {}
  91. h = ""
  92. b = ""
  93. for i = 1, #fileData do
  94. segment = i % 2
  95. if segment == 1 then
  96. h = fileData[i]
  97. else
  98. b = fileData[i]
  99. r = loadRecipe(h, b)
  100. uid = r.name .. tostring(r.damage)
  101. print("new uid: ", uid)
  102. loadedRecipes[uid] = r
  103. end
  104. end
  105.  
  106. return loadedRecipes
  107. end
  108.  
  109.  
  110. function printAllKnownRecipes()
  111.  
  112. local count = 0
  113. for k, v in pairs(loadedRecipes) do
  114. print(count, ": ", k)
  115. printRecipe(v)
  116. count = count + 1
  117. end
  118.  
  119. if count == 0 then
  120. print("No known recipes")
  121. end
  122. end
  123.  
  124.  
  125. function loadRecipe(header, body)
  126. local r = {}
  127. r["slot"] = {}
  128.  
  129. --- Load header
  130. i = 0
  131. for w in header:gmatch("%S+") do
  132. if i == 0 then
  133. r["name"] = w
  134. elseif i == 1 then
  135. r["damage"] = tonumber(w)
  136. elseif i == 2 then
  137. r["hint"] = w
  138. end
  139. i = i + 1
  140. end
  141.  
  142. --- Load body
  143. i = 0
  144. --currentSlot = {}
  145. slotIdx = 0
  146. for w in body:gmatch("%S+") do
  147. slotStep = i % 3
  148. if slotStep == 0 then
  149. slotIdx = tonumber(w)
  150. r.slot[slotIdx] = {}
  151. elseif slotStep == 1 then
  152. r.slot[slotIdx]["name"] = w
  153. elseif slotStep == 2 then
  154. r.slot[slotIdx]["damage"] = tonumber(w)
  155. end
  156. i = i + 1
  157. end
  158.  
  159. return r
  160. end
  161.  
  162.  
  163. function printRecipe(r)
  164. print(r.name, ", ", r.damage)
  165. print(r.hint)
  166. for i = 1, 16 do
  167. if r[i] ~= nil then
  168. print("slot: ", i, ", ", r.slot[i].name, ", ", r[i].slot.damage)
  169. end
  170. end
  171. end
  172.  
  173.  
  174. function getCurrentRecipe(recipe)
  175. recipe["name"] = ""
  176. recipe["damage"] = nil
  177. recipe["slot"] = {}
  178. recipe["hint"] = "-"
  179. for i = 1, 16 do
  180. itemDetails = turtle.getItemDetail(i)
  181. if itemDetails then
  182. recipe.slot[i] = {}
  183. recipe.slot[i]["name"] = itemDetails.name
  184. recipe.slot[i]["damage"] = itemDetails.damage
  185. end
  186. end
  187. end
  188.  
  189.  
  190. function getUniqueID(slot)
  191. return tostring(slot.name) .. tostring(slot.damage)
  192. end
  193.  
  194.  
  195. function oppositeDirectionStr(dir)
  196. if dir == "left" then
  197. return "EAST"
  198. elseif dir == "right" then
  199. return "WEST"
  200. elseif dir == "top" then
  201. return "DOWN"
  202. elseif dir == "bottom" then
  203. return "UP"
  204. elseif dir == "front" then
  205. return "SOUTH"
  206. elseif dir == "back" then
  207. return "NORTH"
  208. end
  209. end
  210.  
  211.  
  212. function fetchItemFromChest(itemName, itemDamage, turtleSlot, qty)
  213.  
  214. for k, vchest in pairs(chestList) do
  215. i = inspectchest.getSlotWithItem(vchest, itemName, itemDamage)
  216. if i ~= nil then
  217. dir = oppositeDirectionStr(k)
  218. b = vchest.pushItem(dir, i, qty, turtleSlot)
  219. print(b, "/", qty)
  220. if b == nil then
  221. return false
  222. elseif b < qty then
  223. qty = qty - b
  224. else
  225. return true
  226. end
  227. end
  228. end
  229.  
  230. return qty <= 0
  231. end
  232.  
  233.  
  234. function isRecoginizedRecipe(uniqueID)
  235. return loadedRecipes[uniqueID] ~= nil
  236. end
  237.  
  238.  
  239. --- Search nearby chests for ingredients and place
  240. --- them into the appropiate slots in the turtles
  241. --- inventory
  242. function tryCraft(recipe, qty)
  243. for i = 1, 16 do
  244. if recipe.slot[i] ~= nil then
  245.  
  246. local uid = getUniqueID(recipe.slot[i])
  247. print("We need: ", uid)
  248. local b = fetchItemFromChest(recipe.slot[i].name, recipe.slot[i].damage, i, qty)
  249.  
  250. --- fail to get item from chest, try to make one
  251. if b == false then
  252. b = isRecoginizedRecipe(uid)
  253. if b == true then
  254. dump.dump_all_up()
  255. if tryCraft(loadedRecipes[uid], qty) == false then
  256. return false
  257. end
  258. if tryCraft(recipe, qty) == true then
  259. return true
  260. else
  261. return false
  262. end
  263. --fetchItemFromChest(recipe.slot[i].name, recipe.slot[i].damage, i, 1)
  264. else
  265. print("No ingredients to make or ingredients are not recognized as a recipe!")
  266. return false
  267. end
  268. --- Got an item from the chest!
  269. else
  270. end
  271. end
  272. end
  273. turtle.craft()
  274. dump.dump_all_up()
  275. return true
  276. end
  277.  
  278.  
  279. function tryCraftLoop(recipe, totalQty)
  280. gQty = totalQty
  281. repeat
  282. print(gQty, " items left to craft...")
  283. currQty = math.min(gQty, 64)
  284. b = tryCraft(recipe, currQty)
  285. gQty = gQty - currQty
  286. until b == false or gQty <= 0
  287. if b == false then
  288. print("Failed to craft items!")
  289. end
  290. if gQty <= 0 then
  291. print("Finished crafting target quantity!")
  292. end
  293. return b
  294. end
  295.  
  296.  
  297. function tryCraftWithHint(hint, qty)
  298. loadedRecipes = loadAllRecipes()
  299. gQty = qty;
  300. for k, v in pairs(loadedRecipes) do
  301. if v.hint == hint then
  302. b = tryCraftLoop(loadedRecipes[k], gQty)
  303. return b
  304. end
  305. end
  306.  
  307. --- the hint might be a UID
  308. if isRecoginizedRecipe(hint) then
  309. b = tryCraftLoop(loadedRecipes[hint], gQty)
  310. return b
  311. end
  312. end
  313.  
  314.  
  315. function usage()
  316. print("USAGE:")
  317. print(" craft <command> <parameters>")
  318. print("COMMANDS:")
  319. print(" new, list, make <# of items>, help <command>")
  320. print("")
  321. print(" You can display detailed help for each command")
  322. print(" e.g. craft help make")
  323. end
  324.  
  325.  
  326. local args = {...}
  327.  
  328. if #args == 1 then
  329. if args[1] == "new" then
  330. learnNewCraft()
  331. elseif args[1] == "list" then
  332. loadedRecipes = loadAllRecipes()
  333. printAllKnownRecipes()
  334. else
  335. usage()
  336. end
  337. elseif #args == 2 then
  338. if args[1] == "new" then
  339. local hint = args[2]
  340. learnNewCraftWithHint(hint)
  341. else
  342. usage()
  343. end
  344. elseif #args == 3 then
  345. if args[1] == "make" then
  346. local target = args[2]
  347. local qty = tonumber(args[3])
  348. if qty < 1 then
  349. qty = 1
  350. end
  351. tryCraftWithHint(target, qty)
  352. end
  353. else
  354. usage()
  355. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement