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

mikanianian

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