Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Advertisement
temposabel

asd

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