gravitowl

windmill_mining

Mar 6th, 2021 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. local millDepth = 0
  2.  
  3. local INVENTORY_SIZE = 16
  4.  
  5. local mine_blocks = {
  6. "minecraft:coal_ore",
  7. "minecraft:iron_ore",
  8. "minecraft:gold_ore",
  9. "minecraft:redstone_ore",
  10. "minecraft:lapis_ore",
  11. "minecraft:emerald_ore",
  12. "minecraft:diamond_ore"
  13. }
  14.  
  15. local Dir = {
  16. Front = 0,
  17. Back = 1,
  18. Up = 2,
  19. Down = 3,
  20. Left = 4,
  21. Right = 5
  22. }
  23.  
  24. local turnTrack = {}
  25. local moveTrack = {}
  26. local depth = 0
  27.  
  28. local function hasValue( tbl, str )
  29. local f = false
  30. for i = 1, #tbl do
  31. if type( tbl[i] ) == "table" then
  32. f = hasValue( tbl[i], str ) -- return value from recursion
  33. if f then break end -- if it returned true, break out of loop
  34. elseif tbl[i] == str then
  35. return true
  36. end
  37. end
  38. return f
  39. end
  40.  
  41. function trackValue(depth, dir)
  42. return {depth=depth, dir=dir}
  43. end
  44.  
  45. function moveAndDig(dir)
  46.  
  47. if dir == Dir.Front then
  48. while turtle.forward() == false do
  49. turtle.dig()
  50. end
  51. table.insert(moveTrack, trackValue(depth, Dir.Front))
  52. elseif dir == Dir.Back then
  53. turtle.back()
  54. table.insert(moveTrack, trackValue(depth, Dir.Back))
  55. elseif dir == Dir.Up then
  56. while turtle.up() == false do
  57. turtle.digUp()
  58. end
  59. table.insert(moveTrack, trackValue(depth, Dir.Up))
  60. elseif dir == Dir.Down then
  61. while turtle.down() == false do
  62. turtle.digDown()
  63. end
  64. table.insert(moveTrack, trackValue(depth, Dir.Down))
  65. end
  66. end
  67.  
  68. function alignDir(dir)
  69.  
  70. if dir == Dir.Left then
  71. turtle.turnLeft()
  72. table.insert(turnTrack, trackValue(depth, Dir.Left))
  73.  
  74. elseif dir == Dir.Right then
  75. turtle.turnRight()
  76. table.insert(turnTrack, trackValue(depth, Dir.Right))
  77.  
  78. elseif dir == Dir.Back then
  79. turtle.turnRight()
  80. turtle.turnRight()
  81. table.insert(turnTrack, trackValue(depth, Dir.Right))
  82. table.insert(turnTrack, trackValue(depth, Dir.Right))
  83.  
  84. end
  85. end
  86.  
  87. function undoAlignDir(dir)
  88.  
  89. if dir == Dir.Left then
  90. turtle.turnRight()
  91. print("Right")
  92.  
  93. elseif dir == Dir.Right then
  94. turtle.turnLeft()
  95. print("Left")
  96.  
  97. elseif dir == Dir.Back then
  98. turtle.turnLeft()
  99. turtle.turnLeft()
  100. print("Left Left")
  101.  
  102. end
  103. end
  104.  
  105. function backtrack(targetDepth)
  106. local depth = depth - 1
  107. while depth >= targetDepth do
  108. while #moveTrack > 0 and moveTrack[#moveTrack].depth == depth do
  109. local vec = table.remove(moveTrack)
  110. if vec.dir == Dir.Front then
  111. turtle.back()
  112. print("Back")
  113. elseif vec.dir == Dir.Up then
  114. turtle.down()
  115. print("Down")
  116. elseif vec.dir == Dir.Down then
  117. turtle.up()
  118. print("Up")
  119. end
  120. end
  121.  
  122. while #turnTrack > 0 and turnTrack[#turnTrack].depth == depth do
  123. local vec = table.remove(turnTrack)
  124. undoAlignDir(vec.dir)
  125. end
  126. depth = depth - 1
  127. end
  128. end
  129.  
  130. function detectOres()
  131. local succes, blockInfo
  132. local returnValue = {}
  133.  
  134. succes, blockInfo = turtle.inspect()
  135. if succes and hasValue(mine_blocks, blockInfo.name) then
  136. table.insert(returnValue, Dir.Front)
  137. return returnValue
  138. end
  139.  
  140. turtle.turnLeft()
  141. succes, blockInfo = turtle.inspect()
  142. if succes and hasValue(mine_blocks, blockInfo.name) then
  143. table.insert(returnValue, Dir.Left)
  144. turtle.turnRight()
  145. return returnValue
  146. end
  147.  
  148. turtle.turnRight()
  149. turtle.turnRight()
  150. succes, blockInfo = turtle.inspect()
  151. if succes and hasValue(mine_blocks, blockInfo.name) then
  152. table.insert(returnValue, Dir.Right)
  153. turtle.turnLeft()
  154. return returnValue
  155. end
  156.  
  157. turtle.turnLeft()
  158.  
  159. succes, blockInfo = turtle.inspectUp()
  160. if succes and hasValue(mine_blocks, blockInfo.name) then
  161. table.insert(returnValue, Dir.Up)
  162. return returnValue
  163. end
  164.  
  165. succes, blockInfo = turtle.inspectDown()
  166. if succes and hasValue(mine_blocks, blockInfo.name) then
  167. table.insert(returnValue, Dir.Down)
  168. return returnValue
  169. end
  170.  
  171. return returnValue
  172. end
  173.  
  174. function dfs()
  175. print("[TURTLE] > Entered DFS mode...")
  176. local ores = detectOres()
  177.  
  178. while #ores ~= 0 do
  179. ores = detectOres()
  180. alignDir(ores[1])
  181. print(ores[1])
  182.  
  183. if ores[1] == Dir.Up then
  184. moveAndDig(Dir.Up)
  185. elseif ores[1] == Dir.Down then
  186. moveAndDig(Dir.Down)
  187. else
  188. moveAndDig(Dir.Front)
  189. end
  190. depth = depth + 1
  191. end
  192.  
  193. while depth > 0 do
  194. backtrack(depth - 1)
  195. ores = detectOres()
  196. if #ores ~= 0 then
  197. dfs()
  198. break
  199. end
  200. depth = depth - 1
  201. end
  202. print("[TURTLE] > Left DFS Mode...")
  203. end
  204.  
  205. function moveUpAndDig()
  206. while turtle.up() == false do
  207. turtle.digUp()
  208. end
  209. end
  210.  
  211. function moveDownAndDig()
  212. while turtle.down() == false do
  213. turtle.digDown()
  214. end
  215. end
  216.  
  217. function moveForwardAndDig()
  218. while turtle.forward() == false do
  219. turtle.dig()
  220. end
  221. end
  222.  
  223. function setup()
  224. print("[TURTLE] > Setting up windmill mine...")
  225. for i = 1, 4 do
  226. local ores = detectOres()
  227. if #ores ~= 0 then
  228. dfs()
  229. end
  230. moveForwardAndDig()
  231. end
  232. turtle.turnRight()
  233.  
  234. for i = 1, 4 do
  235. for i = 1, 4 do
  236. local ores = detectOres()
  237. if #ores ~= 0 then
  238. dfs()
  239. end
  240. moveForwardAndDig()
  241. end
  242.  
  243. turtle.turnLeft()
  244.  
  245. for i = 1, 4 do
  246. local ores = detectOres()
  247. if #ores ~= 0 then
  248. dfs()
  249. end
  250. moveForwardAndDig()
  251. end
  252.  
  253. turtle.turnRight()
  254. turtle.turnRight()
  255.  
  256. for i = 1, 8 do
  257. local ores = detectOres()
  258. if #ores ~= 0 then
  259. dfs()
  260. end
  261. moveForwardAndDig()
  262. end
  263. end
  264. end
  265.  
  266. function mine()
  267. print("[TURTLE] > Entering mining mode...")
  268. turtle.turnRight()
  269. turtle.turnRight()
  270. for i = 1, 8 do
  271. local ores = detectOres()
  272. if #ores ~= 0 then
  273. dfs()
  274. end
  275. moveForwardAndDig()
  276. end
  277.  
  278. for cd = 1, millDepth do
  279. turtle.turnRight()
  280. for i = 1, 4 do
  281. for j = 1, 4 do
  282. local ores = detectOres()
  283. if #ores ~= 0 then
  284. dfs()
  285. end
  286. moveForwardAndDig()
  287. end
  288. turtle.turnLeft()
  289. for j = 1, 4 do
  290. local ores = detectOres()
  291. if #ores ~= 0 then
  292. dfs()
  293. end
  294. moveForwardAndDig()
  295. end
  296. turtle.turnRight()
  297. turtle.turnRight()
  298. for j = 1, 8 + 8 * cd do
  299. local ores = detectOres()
  300. if #ores ~= 0 then
  301. dfs()
  302. end
  303. moveForwardAndDig()
  304. end
  305. end
  306.  
  307. for i = 1, 4 do
  308. local ores = detectOres()
  309. if #ores ~= 0 then
  310. dfs()
  311. end
  312. moveForwardAndDig()
  313. end
  314. turtle.turnLeft()
  315. for i = 1, 4 do
  316. local ores = detectOres()
  317. if #ores ~= 0 then
  318. dfs()
  319. end
  320. moveForwardAndDig()
  321. end
  322. end
  323. end
  324.  
  325. function returnHome()
  326. print("[TURTLE] > Returning to homebase...")
  327. turtle.turnRight()
  328. turtle.turnRight()
  329.  
  330. for i = 1, millDepth do
  331. for j = 1, 4 do
  332. moveForwardAndDig()
  333. end
  334. turtle.turnRight()
  335. for j = 1, 4 do
  336. moveForwardAndDig()
  337. end
  338. turtle.turnLeft()
  339. end
  340.  
  341. for i = 1, 8 do
  342. moveForwardAndDig()
  343. end
  344.  
  345. turtle.turnRight()
  346.  
  347. for i = 1, 4 do
  348. moveForwardAndDig()
  349. end
  350.  
  351. turtle.turnRight()
  352. turtle.turnRight()
  353. end
  354.  
  355. if #arg == 1 then
  356. millDepth = tonumber(arg[1])
  357.  
  358. if turtle.getFuelLevel() > 0 then
  359. setup()
  360. mine()
  361. returnHome()
  362. end
  363. else
  364. print("Please input a depth for the turtle to use.")
  365. return
  366. end
  367.  
Add Comment
Please, Sign In to add comment