Advertisement
Proaxel

ValuableFinderv3

May 29th, 2020
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. depth = 0
  2. maxOperations = 1
  3. fuelThreshold = 30
  4. abortDueToFuel = false
  5. useChests = true
  6. descending = true
  7. ascending = true
  8. dropJunk = true
  9.  
  10. function forceMove(direction)
  11. if direction == "up" then
  12. while not turtle.up() do
  13. turtle.digUp()
  14. end
  15. end
  16. if direction == "down" then
  17. while not turtle.down() do
  18. turtle.digDown()
  19. end
  20. end
  21. if direction == "forward" then
  22. while not turtle.forward() do
  23. turtle.dig()
  24. end
  25. end
  26. end
  27.  
  28. function forcePlace(direction)
  29. if direction == "up" then
  30. while not turtle.placeUp() do
  31. turtle.digUp()
  32. end
  33. end
  34. if direction == "down" then
  35. while not turtle.placeDown() do
  36. turtle.digDown()
  37. end
  38. end
  39. if direction == "forward" then
  40. while not turtle.place() do
  41. turtle.dig()
  42. end
  43. end
  44. end
  45.  
  46. function fuel()
  47. print("Attempting to refuel with something in inventory...")
  48. local fuelTable = {"minecraft:coal", "minecraft:lava_bucket"}
  49. local successfulRefuel = false
  50. for i = 1, 16, 1 do
  51. local itemData = turtle.getItemDetail(i)
  52. if itemData then
  53. for k, v in ipairs(fuelTable) do
  54. if itemData.name == v then
  55. turtle.select(i)
  56. turtle.refuel()
  57. turtle.select(1)
  58. successfulRefuel = true
  59. end
  60. end
  61. end
  62. end
  63.  
  64. return successfulRefuel
  65. end
  66.  
  67. function isValuable()
  68. local junkTable = {"minecraft:stone", "minecraft:dirt", "minecraft:gravel", "minecraft:sand", "minecraft:torch"}
  69. local valuable = true
  70.  
  71. local isBlock, blockData = turtle.inspect()
  72. if isBlock == true then
  73. --print("There's something here. Block data:")
  74. --print(blockData.name)
  75. --print(blockData.metadata)
  76. for k, v in ipairs(junkTable) do
  77. if blockData.name == v then
  78. --print("This is junk.")
  79. valuable = false
  80. break
  81. end
  82. end
  83. else
  84. --print("There isn't anything here.")
  85. valuable = false
  86. end
  87. if valuable then
  88. --print("There's something valuable here!")
  89. return true
  90.  
  91. else
  92. --print("Nothing interesting here. Moving on")
  93. return false
  94. end
  95. end
  96.  
  97. function checkFreeSlot()
  98. for i = 1, 16, 1 do
  99. if turtle.getItemCount(i) <= 0 then
  100. return i
  101. end
  102. end
  103. return -1
  104. end
  105.  
  106. function throwAwayJunk() --Finds junk in inventory and throws it away
  107. local junkTable = {"minecraft:stone", "minecraft:dirt", "minecraft:gravel", "minecraft:sand"}
  108. local successful = false
  109. for i = 1, 16, 1 do
  110. local itemData = turtle.getItemDetail(i)
  111. if itemName then
  112. for k, v in ipairs(fuelTable) do
  113. if itemData.name == v then
  114. print("Throwing away ", itemData.name)
  115. turtle.select(i)
  116. turtle.dropDown()
  117. turtle.select(1)
  118. successful = true
  119. end
  120. if successful then break end
  121. end
  122. end
  123. end
  124. return successful
  125. end
  126.  
  127.  
  128.  
  129. function checkSides()
  130. for i = 0, 3, 1 do
  131. if isValuable() then
  132. turtle.dig()
  133. end
  134. turtle.turnRight()
  135. end
  136. end
  137.  
  138. function isBottomEmpty()
  139. local isBlock, blockData = turtle.inspect()
  140. if isBlock then
  141. return true
  142. else
  143. return false
  144. end
  145. end
  146.  
  147. function isBottomBedrock()
  148. local isBlock, blockData = turtle.inspectDown()
  149. if isBlock then
  150. if blockData.name == "minecraft:bedrock" then
  151. print("We've hit bedrock!")
  152. return true
  153. else
  154. return false
  155. end
  156. else
  157. return false
  158. end
  159. end
  160.  
  161. function knightMove()
  162. print("Knight move")
  163. forceMove("forward")
  164. forceMove("forward")
  165.  
  166. turtle.turnRight()
  167.  
  168. forceMove("forward")
  169. turtle.turnLeft()
  170. end
  171.  
  172. function findChestInInventory()
  173. local chestSlot = -1
  174. for i = 1, 16, 1 do
  175. local data = turtle.getItemDetail(i)
  176. if data then
  177. if data.name == "minecraft:chest" then
  178. chestSlot = i
  179. print("Chest found in slot: " , chestSlot)
  180. break
  181. end
  182. end
  183. end
  184. return chestSlot
  185. end
  186.  
  187. function isChestInInventory()
  188. local foundChest = false
  189. for i = 1, 16, 1 do
  190. local data = turtle.getItemDetail(i)
  191. if data then
  192. if data.name == "minecraft:chest" then
  193. foundChest = true
  194. break
  195. end
  196. end
  197. end
  198. return foundChest
  199. end
  200.  
  201. function returnToSurface()
  202. print("Let's go back up...")
  203.  
  204. while depth ~= 0 do
  205. if turtle.up() then
  206. depth = depth - 1
  207. else
  208. print("Obstruction detected...")
  209. turtle.digUp()
  210. end
  211. end
  212. end
  213.  
  214. function closeHole()
  215. local junkTable = {"minecraft:stone", "minecraft:dirt"}
  216. local successful = false
  217. for i = 1, 16, 1 do
  218. local itemData = turtle.getItemDetail(i)
  219. if itemData then
  220. for k, v in ipairs(junkTable) do
  221. if itemData.name == v then
  222. turtle.select(i)
  223. turtle.placeDown()
  224. turtle.select(1)
  225. successful = true
  226. end
  227. end
  228. end
  229. end
  230.  
  231. return successful
  232. end
  233.  
  234. function closeHoleAbove()
  235. local junkTable = {"minecraft:stone", "minecraft:dirt"}
  236. local successful = false
  237. for i = 1, 16, 1 do
  238. local itemData = turtle.getItemDetail(i)
  239. if itemData then
  240. for k, v in ipairs(junkTable) do
  241. if itemData.name == v then
  242. turtle.select(i)
  243. turtle.placeUp()
  244. turtle.select(1)
  245. successful = true
  246. end
  247. end
  248. end
  249. end
  250.  
  251. return successful
  252. end
  253.  
  254. function chestDump()
  255. local itemsDeposited = 0
  256. print("Chest dumping")
  257. turtle.select(findChestInInventory())
  258. forcePlace("down")
  259. for a = 1, 16 do
  260. turtle.select(a)
  261. local data = turtle.getItemDetail()
  262. if data then
  263. if data.name ~= "minecraft:chest" then
  264. turtle.dropDown()
  265. itemsDeposited = itemsDeposited + 1
  266. end
  267. end
  268. end
  269.  
  270. print(itemsDeposited, " different items deposited.")
  271. if not isChestInInventory() then
  272. printError("That was the last chest! I won't use anymore for the rest of the operation")
  273. useChests = false
  274. end
  275.  
  276. turtle.select(1)
  277. end
  278.  
  279. ----------------Program Starts Here----------------
  280.  
  281. print("Checking Fuel")
  282. print("Fuel level is ", turtle.getFuelLevel())
  283. if turtle.getFuelLevel() <= fuelThreshold then
  284. print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
  285. if not fuel() then
  286. repeat
  287. printError("There is not enough fuel to run an operation. Please add more and press enter")
  288. print("Or, type a to abort")
  289. local decision = io.read()
  290. if decision == "a" then error("Aborted") end
  291. until fuel()
  292. else
  293. print("Refuel successful: ", turtle.getFuelLevel())
  294. end
  295. else
  296. print("Fuel level OK")
  297. end
  298.  
  299. print("Checking for chests")
  300. if not isChestInInventory() then
  301. repeat
  302. printError("No chests found! Please add chests to inventory and press enter to continue.")
  303. print("Type c to continue without using chests, or a to abort")
  304.  
  305. local decision = io.read()
  306. if decision == "c" then
  307. useChests = false
  308. break
  309. end
  310. if decision == "a" then
  311. error("Aborted")
  312. end
  313. until findChestInInventory() ~= -1
  314. else
  315. print("Chest found in ", findChestInInventory())
  316. end
  317.  
  318.  
  319.  
  320. print("Please specify number of operations. It must be a positive number")
  321. repeat
  322. maxOperations = io.read()
  323. maxOperations = tonumber(maxOperations)
  324. if maxOperations == nil or maxOperations <= 0 then
  325. print("Invalid entry!")
  326. end
  327. until maxOperations ~= nil and maxOperations > 0
  328.  
  329. print("Let's go!")
  330.  
  331. for operations = 1, maxOperations do
  332. print("Current operation: ", operations)
  333. while descending do
  334. if depth == 2 then
  335. print("Closing Hole")
  336. if not closeHoleAbove() then
  337. print("I could not close the hole for some reason.")
  338. end
  339. end
  340. checkSides()
  341.  
  342. if isBottomBedrock() then
  343. descending = false
  344. break
  345. end
  346.  
  347. turtle.digDown()
  348.  
  349. if turtle.getFuelLevel() <= fuelThreshold then
  350. print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
  351. if not fuel() then
  352. abortDueToFuel = true
  353. returnToSurface()
  354. break
  355. else
  356. abortDueToFuel = false
  357. print("Refuel successful")
  358. end
  359. end
  360.  
  361. if dropJunk then
  362. if checkFreeSlot() == -1 then throwAwayJunk() end
  363. end
  364. turtle.down()
  365. depth = depth + 1
  366. end
  367.  
  368. if not abortDueToFuel then
  369. knightMove()
  370.  
  371. while ascending do
  372. if turtle.getFuelLevel() <= fuelThreshold then
  373. print("Fuel low (", turtle.getFuelLevel(), ")! Attempting refuel")
  374. if not fuel() then
  375. abortDueToFuel = true
  376. print("No fuel to refuel with! Stopping after this ascent operation")
  377. else
  378. abortDueToFuel = false
  379. print("Refuel successful")
  380. end
  381. end
  382.  
  383. checkSides()
  384.  
  385. if depth <= 0 then
  386. ascending = false
  387. break
  388. end
  389.  
  390. turtle.digUp()
  391. if turtle.up() then
  392. depth = depth - 1
  393. else
  394. print("Obstruction detected...")
  395. turtle.digUp()
  396. end
  397. end
  398. end
  399.  
  400. if not closeHole() then
  401. print("Uh oh, I have nothing to close the hole with")
  402. end
  403.  
  404.  
  405.  
  406. if operations >= maxOperations or abortDueToFuel == true then
  407. if useChests then
  408. forceMove("up")
  409. chestDump()
  410. break
  411. else
  412. break
  413. end
  414. else
  415. if useChests then
  416. forceMove("up")
  417. chestDump()
  418. knightMove()
  419. forceMove("down")
  420. else
  421. knightMove()
  422. end
  423. end
  424.  
  425. descending = true
  426. ascending = true
  427. end
  428.  
  429. print("Done! Remaining fuel: ", turtle.getFuelLevel())
  430.  
  431. if abortDueToFuel then
  432. print("The operation may be incomplete due to lack of fuel.")
  433. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement