Xeyseir

Untitled

Dec 23rd, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. local startX, startY, startZ, startDir = 0, 0, 0, "north"
  2. local currX, currY, currZ, currDir = startX, startY, startZ, startDir
  3.  
  4. -- Initialize the unmined table
  5. unmined = {}
  6. for x = -26, 26 do
  7. unmined[x] = {}
  8. for y = 0, 4 do
  9. unmined[x][y] = {}
  10. for z = -26, 26 do
  11. unmined[x][y][z] = true
  12. end
  13. end
  14. end
  15.  
  16. function move(dir, calledByReturnHome)
  17. -- If the function was not called by returnHome, check if there is an item in slot 16
  18. if not calledByReturnHome and turtle.getItemCount(16) > 0 then
  19. -- If there is, run the returnHome function
  20. returnHome(true)
  21. end
  22. if dir == "up" or dir == "down" then
  23. -- Check if we are at y0 and trying to move down, or at y4 and trying to move up
  24. if (currY == 0 and dir == "down") or (currY == 4 and dir == "up") then
  25. -- Do not move in the specified direction if we are at y0 and trying to move down, or at y4 and trying to move up
  26. return
  27. end
  28. if dir == "up" then
  29. if turtle.detectUp() then
  30. turtle.digUp()
  31. turtle.up()
  32. updatePosition(dir)
  33. else
  34. turtle.up()
  35. updatePosition(dir)
  36. end
  37. elseif dir == "down" then
  38. if turtle.detectDown() then
  39. turtle.digDown()
  40. turtle.down()
  41. updatePosition(dir)
  42. else
  43. turtle.down()
  44. updatePosition(dir)
  45. end
  46. end
  47. else
  48. if turtle.detect() then
  49. turtle.dig()
  50. if dir == "forward" then
  51. turtle.forward()
  52. updatePosition(dir)
  53. else
  54. turtle.back()
  55. updatePosition(dir)
  56. end
  57. else
  58. if dir == "forward" then
  59. turtle.forward()
  60. updatePosition(dir)
  61. else
  62. turtle.back()
  63. updatePosition(dir)
  64. end
  65. end
  66. end
  67. -- Check if we are at the outer bounds of the unmined table on the x or z axis
  68. if currX == -25 or currX == 25 or currZ == -25 or currZ == 25 then
  69. -- If we are, move to the unmined block closest to 0, 0, 0
  70. moveToUnmined()
  71. end
  72. end
  73.  
  74.  
  75.  
  76.  
  77. function updatePosition(dir)
  78. if dir == "forward" then
  79. currZ = currZ + (currDir == "north" and 1 or (currDir == "south" and -1 or 0))
  80. currX = currX + (currDir == "east" and 1 or (currDir == "west" and -1 or 0))
  81. elseif dir == "backward" then
  82. currZ = currZ + (currDir == "south" and 1 or (currDir == "north" and -1 or 0))
  83. currX = currX + (currDir == "west" and 1 or (currDir == "east" and -1 or 0))
  84. elseif dir == "up" then
  85. currY = currY + 1
  86. elseif dir == "down" then
  87. currY = currY - 1
  88. end
  89. -- Check if unmined[currX][currY][currZ] is nil before setting it to false
  90. if unmined[currX][currY][currZ] == nil then
  91. -- Print an error and call moveToUnmined()
  92. print("Error: unmined[" .. currX .. "][" .. currY .. "][" .. currZ .. "] is nil")
  93. moveToUnmined()
  94. else
  95. unmined[currX][currY][currZ] = false
  96. end
  97. end
  98.  
  99. function returnHome()
  100. local atHome = false
  101. while not atHome do
  102. if currX > startX then
  103. turnTo("west")
  104. move("forward", true)
  105. elseif currX < startX then
  106. turnTo("east")
  107. move("forward", true)
  108. elseif currY > startY then
  109. move("down", true)
  110. elseif currY < startY then
  111. move("up", true)
  112. elseif currZ > startZ then
  113. turnTo("south")
  114. move("forward", true)
  115. elseif currZ < startZ then
  116. turnTo("north")
  117. move("forward", true)
  118. else
  119. atHome = true
  120. end
  121. end
  122. end
  123.  
  124.  
  125.  
  126. function turnTo(dir)
  127. if currDir == "north" then
  128. if dir == "east" then
  129. turn("right")
  130. elseif dir == "south" then
  131. turn("left")
  132. turn("left")
  133. elseif dir == "west" then
  134. turn("left")
  135. end
  136. elseif currDir == "east" then
  137. if dir == "south" then
  138. turn("right")
  139. elseif dir == "west" then
  140. turn("left")
  141. turn("left")
  142. elseif dir == "north" then
  143. turn("left")
  144. end
  145. elseif currDir == "south" then
  146. if dir == "west" then
  147. turn("right")
  148. elseif dir == "north" then
  149. turn("left")
  150. turn("left")
  151. elseif dir == "east" then
  152. turn("left")
  153. end
  154. elseif currDir == "west" then
  155. if dir == "north" then
  156. turn("right")
  157. elseif dir == "east" then
  158. turn("left")
  159. turn("left")
  160. elseif dir == "south" then
  161. turn("left")
  162. end
  163. end
  164.  
  165. currDir = dir
  166. end
  167.  
  168. function turn(dir)
  169. if dir == "left" then
  170. turtle.turnLeft()
  171. else
  172. turtle.turnRight()
  173. end
  174. if currDir == "north" then
  175. currDir = (dir == "left") and "west" or "east"
  176. elseif currDir == "east" then
  177. currDir = (dir == "left") and "north" or "south"
  178. elseif currDir == "south" then
  179. currDir = (dir == "left") and "east" or "west"
  180. elseif currDir == "west" then
  181. currDir = (dir == "left") and "south" or "north"
  182. end
  183. end
  184.  
  185. function refuel()
  186. for i = 1, 16 do
  187. turtle.select(i)
  188. if turtle.refuel(1) then
  189. return
  190. end
  191. end
  192. returnHome()
  193. turtle.suckUp()
  194. refuel()
  195. end
  196.  
  197. function mine()
  198. local blocksMined = 0
  199. local consecutiveNoBlocks = 0
  200. while blocksMined < 256 do
  201. if turtle.getFuelLevel() < 1000 then
  202. returnHome()
  203. refuel()
  204. end
  205. if turtle.getItemCount(16) > 0 then
  206. returnHome()
  207. for i = 1, 16 do
  208. turtle.select(i)
  209. turtle.dropDown()
  210. turtle.select(1)
  211. end
  212. end
  213. if currY > 4 then
  214. move("down")
  215. elseif currY < 0 then
  216. move("up")
  217. elseif math.random(1,5) == 1 then
  218. turn(math.random(1,2) == 1 and "left" or "right")
  219. elseif math.random(1,20) == 1 then
  220. if math.random(1,2) == 1 then
  221. move("up")
  222. else
  223. move("down")
  224. end
  225. else
  226. local moved = false
  227. for i = 1, 10 do
  228. if turtle.detect() then
  229. move("forward")
  230. blocksMined = blocksMined + 1
  231. consecutiveNoBlocks = 0
  232. moved = true
  233. break
  234. else
  235. move("forward")
  236. consecutiveNoBlocks = consecutiveNoBlocks + 1
  237. if consecutiveNoBlocks >= 10 then
  238. moveToUnmined()
  239. consecutiveNoBlocks = 0
  240. break
  241. end
  242. end
  243. end
  244. if not moved then
  245. consecutiveNoBlocks = 0
  246. end
  247. end
  248. end
  249. returnHome()
  250. end
  251.  
  252.  
  253. function moveToUnmined()
  254. -- Find the unmined block closest to the starting position (0, 0, 0)
  255. local closestX, closestY, closestZ = 0, 0, 0
  256. local closestDistance = math.huge
  257. for x = -26, 26 do
  258. for y = 0, 4 do
  259. for z = -26, 26 do
  260. if unmined[x][y][z] then
  261. -- Calculate the distance from the starting position to the unmined block
  262. local distance = math.sqrt(x^2 + y^2 + z^2)
  263. if distance < closestDistance then
  264. -- Update the closest unmined block and distance
  265. closestX, closestY, closestZ = x, y, z
  266. closestDistance = distance
  267. end
  268. end
  269. end
  270. end
  271. end
  272.  
  273. -- Move to the closest unmined block and mine it
  274. local xDiff = currX - closestX
  275. local yDiff = currY - closestY
  276. local zDiff = currZ - closestZ
  277.  
  278. -- Move horizontally towards the closest unmined block
  279. while xDiff ~= 0 or zDiff ~= 0 do
  280. if xDiff > 0 then
  281. move("left")
  282. xDiff = xDiff - 1
  283. elseif xDiff < 0 then
  284. move("right")
  285. xDiff = xDiff + 1
  286. elseif zDiff > 0 then
  287. move("forward")
  288. zDiff = zDiff - 1
  289. elseif zDiff < 0 then
  290. move("back")
  291. zDiff = zDiff + 1
  292. end
  293. end
  294.  
  295. -- Move vertically towards the closest unmined block
  296. while yDiff ~= 0 do
  297. if yDiff > 0 then
  298. move("down")
  299. yDiff = yDiff - 1
  300. elseif yDiff < 0 then
  301. move("up")
  302. yDiff = yDiff + 1
  303. end
  304. end
  305.  
  306. -- Mine the closest unmined block
  307. turtle.dig()
  308.  
  309. -- Update the unmined table
  310. unmined[closestX][closestY][closestZ] = false
  311. end
  312.  
  313.  
  314.  
  315. mine()
Advertisement
Add Comment
Please, Sign In to add comment