Advertisement
temposabel

fsfsefoipujnfseoijusef

Apr 4th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. DIRECTIONS = {"north", "east", "south", "west"}
  2. NORTH, EAST, SOUTH, WEST, UP, DOWN = 1, 2, 3, 4, 5, 6
  3. DELTA = {vector.new(0, 0, -1), vector.new(1, 0, 0), vector.new(0, 0, 1), vector.new(-1, 0, 0), vector.new(0, 1, 0), vector.new(0, -1, 0)}
  4.  
  5. BLOCKS_TO_MINE = {
  6. ["minecraft:diamond_ore"]=true,
  7. ["minecraft:coal_ore"]=true,
  8. }
  9.  
  10. BLOCKS_PER_CHUNK = 16
  11. MAX_LOADED_CHUNKS_PER_DIRECTION = 20
  12. FUEL_PER_COAL = 80
  13. FUEL_SAFETY_MARGIN = 1 * FUEL_PER_COAL
  14.  
  15. local start_pos = vector.new() -- FILL IN BEFORE START
  16. local current_direction = NORTH -- FILL IN BEFORE START
  17. local current_pos = start_pos
  18.  
  19.  
  20. function info()
  21. print("-------------------------")
  22. print("XYZ:", current_pos.x, "/", current_pos.y, "/", current_pos.z)
  23. print("Facing: "..DIRECTIONS[current_direction])
  24. print("Fuel Level: "..turtle.getFuelLevel())
  25. print("-------------------------")
  26. end
  27.  
  28. function calculate_rotation(dir, amount)
  29. local d = dir - 1
  30. d = (d + amount) % 4
  31. return d + 1
  32. end
  33.  
  34. function rotate_right(amount)
  35. local amount = amount or 1
  36.  
  37. for i=1, amount do
  38. turtle.turnRight()
  39. end
  40.  
  41. current_direction = calculate_rotation(current_direction, amount)
  42. end
  43.  
  44. function rotate_left(amount)
  45. local amount = amount or 1
  46.  
  47. for i=1, amount do
  48. turtle.turnLeft()
  49. end
  50.  
  51. current_direction = calculate_rotation(current_direction, -amount)
  52. end
  53.  
  54. function face(direction)
  55. if current_direction == direction then
  56. return
  57. elseif calculate_rotation(current_direction, 1) == direction then
  58. rotate_right()
  59. elseif calculate_rotation(current_direction, -1) == direction then
  60. rotate_left()
  61. else
  62. rotate_right(2)
  63. end
  64. end
  65.  
  66. function move_forward(amount, dig)
  67. local amount = amount or 1
  68. local dig = dig or false
  69.  
  70. for i=1, amount do
  71. while turtle.detect() do
  72. if dig then
  73. turtle.dig()
  74. else
  75. print("Stepbro I'm Stuck! (in front)")
  76. end
  77. end
  78.  
  79. turtle.forward()
  80. current_pos = current_pos + DELTA[current_direction]
  81. info()
  82. end
  83. end
  84.  
  85. function move_backward(amount, dig)
  86. rotate_right(2)
  87. move_forward(amount, dig)
  88. rotate_left(2)
  89. end
  90.  
  91. function move_right(amount, dig)
  92. rotate_right()
  93. move_forward(amount, dig)
  94. rotate_left()
  95. end
  96.  
  97. function move_left(amount, dig)
  98. rotate_left()
  99. move_forward(amount, dig)
  100. rotate_right()
  101. end
  102.  
  103. function move_up(amount, dig)
  104. local amount = amount or 1
  105. local dig = dig or false
  106.  
  107. for i=1, amount do
  108. while turtle.detectUp() do
  109. if dig then
  110. turtle.digUp()
  111. else
  112. print("Stepbro I'm Stuck! (above)")
  113. end
  114. end
  115.  
  116. turtle.up()
  117. current_pos = current_pos + DELTA[UP]
  118. info()
  119. end
  120. end
  121.  
  122. function move_down(amount, dig)
  123. local amount = amount or 1
  124. local dig = dig or 1
  125.  
  126. for i=1, amount do
  127. while turtle.detectDown() do
  128. if dig then
  129. turtle.digDown()
  130. else
  131. print("Stepbro I'm Stuck! (below)")
  132. end
  133. end
  134.  
  135. turtle.down()
  136. current_pos = current_pos + DELTA[DOWN]
  137. info()
  138. end
  139. end
  140.  
  141. function manhattan_distance(pos1, pos2)
  142. return math.abs(pos1.x - pos2.x) + math.abs(pos1.y - pos2.y) + math.abs(pos1.z - pos2.z)
  143. end
  144.  
  145. function get_available_fuel()
  146. local available_fuel = turtle.getFuelLevel()
  147.  
  148. for i=1, 16 do
  149. local item = turtle.getItemDetail(i)
  150.  
  151. if item and item.name == "minecraft:coal" then
  152. available_fuel = available_fuel + item.count * FUEL_PER_COAL
  153. end
  154. end
  155.  
  156. return available_fuel
  157. end
  158.  
  159. function has_ore_in_vicinity()
  160. blocks = {}
  161.  
  162. table.insert(blocks, select(2, turtle.inspectDown()).name)
  163. table.insert(blocks, select(2, turtle.inspectUp()).name)
  164.  
  165. table.insert(blocks, select(2, turtle.inspect()).name)
  166. rotate_left()
  167. table.insert(blocks, select(2, turtle.inspect()).name)
  168. rotate_right(2)
  169. table.insert(blocks, select(2, turtle.inspect()).name)
  170. rotate_left()
  171.  
  172. for _, block in ipairs(blocks) do
  173. if BLOCKS_TO_MINE[block] ~= nil then
  174. return true
  175. end
  176. end
  177.  
  178. return false
  179. end
  180.  
  181. function mine_vein(depth, visited)
  182. local visited = visited or {}
  183. if depth == 0 then return end
  184.  
  185. local local_start_dir = current_direction
  186.  
  187. -- Forward/Backward/Left/Right
  188. for i=0, 3 do
  189. local new_dir = calculate_rotation(local_start_dir, i)
  190. local block = (current_pos + DELTA[new_dir]):tostring()
  191.  
  192. if visited[block] == nil then
  193. visited[block] = true
  194.  
  195. face(new_dir)
  196.  
  197. local not_air, block_data = turtle.inspect()
  198. if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
  199. move_forward(1, true)
  200. mine_vein(depth - 1, visited)
  201. face(calculate_rotation(new_dir, 2))
  202. move_forward(1, true)
  203. end
  204. end
  205. end
  206.  
  207. -- Up/Down
  208. local block = (current_pos + DELTA[UP]):tostring()
  209. if visited[block] == nil then
  210. visited[block] = true
  211.  
  212. local not_air, block_data = turtle.inspectUp()
  213. if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
  214. move_up(1, true)
  215. mine_vein(depth - 1, visited)
  216. move_down(1, true)
  217. end
  218. end
  219.  
  220. local block = (current_pos + DELTA[DOWN]):tostring()
  221. if visited[block] == nil then
  222. visited[block] = true
  223.  
  224. local not_air, block_data = turtle.inspectDown()
  225. if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
  226. move_down(1, true)
  227. mine_vein(depth - 1, visited)
  228. move_up(1, true)
  229. end
  230. end
  231.  
  232. face(local_start_dir)
  233. end
  234.  
  235. function go_down(level)
  236. move_down(level, true)
  237. end
  238.  
  239. function go_up()
  240. while current_pos.y ~= start_pos.y do
  241. move_up(1, true)
  242. end
  243. end
  244.  
  245. function go_back_to_mine_start()
  246. rotate_right(2)
  247.  
  248. while current_pos.x ~= start_pos.x and current_pos.z ~= start_pos.z do
  249. move_forward(1, true)
  250. end
  251. end
  252.  
  253. function should_return_home()
  254. local distance = manhattan_distance(current_pos, start_pos)
  255. local available_fuel = get_available_fuel()
  256.  
  257. if available_fuel - FUEL_SAFETY_MARGIN <= distance then
  258. return true
  259. end
  260.  
  261. return false
  262. end
  263.  
  264. function strip_mine(distance)
  265. distance = distance or BLOCKS_PER_CHUNK * MAX_LOADED_CHUNKS_PER_DIRECTION
  266. local distance_mined = 0
  267.  
  268. while distance_mined < distance and not should_return_home() do
  269. if has_ore_in_vicinity() then
  270. mine_vein(99)
  271. end
  272.  
  273. move_forward(1, true)
  274. distance_mined = distance_mined + 1
  275. end
  276. end
  277.  
  278. function main()
  279. go_down()
  280. strip_mine()
  281. go_back_to_mine_start()
  282. go_up()
  283. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement