Advertisement
temposabel

dawdawdfsfgsef

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