Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DIRECTIONS = {"north", "east", "south", "west"}
- NORTH, EAST, SOUTH, WEST, UP, DOWN = 1, 2, 3, 4, 5, 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)}
- BLOCKS_TO_MINE = {
- ["minecraft:diamond_ore"]=true,
- ["minecraft:coal_ore"]=true
- }
- local start_pos = vector.new() -- FILL IN BEFORE START
- local current_direction = NORTH -- FILL IN BEFORE START
- local current_pos = start_pos
- function info()
- print("-------------------------")
- print("XYZ:", current_pos.x, "/", current_pos.y, "/", current_pos.z)
- print("Facing: "..DIRECTIONS[current_direction])
- print("Fuel Level: "..turtle.getFuelLevel())
- print("-------------------------")
- end
- function calculate_rotation(dir, amount)
- local d = dir - 1
- d = (d + amount) % 4
- return d + 1
- end
- function rotate_right(amount)
- local amount = amount or 1
- for i=1, amount do
- turtle.turnRight()
- end
- current_direction = calculate_rotation(current_direction, amount)
- end
- function rotate_left(amount)
- local amount = amount or 1
- for i=1, amount do
- turtle.turnLeft()
- end
- current_direction = calculate_rotation(current_direction, -amount)
- end
- function face(direction)
- if current_direction == direction then
- return
- elseif calculate_rotation(current_direction, 1) == direction then
- rotate_right()
- elseif calculate_rotation(current_direction, -1) == direction then
- rotate_left()
- else
- rotate_right(2)
- end
- end
- function move_forward(amount, dig)
- local amount = amount or 1
- local dig = dig or false
- for i=1, amount do
- while turtle.detect() do
- if dig then
- turtle.dig()
- else
- print("Stepbro I'm Stuck! (in front)")
- end
- end
- turtle.forward()
- current_pos = current_pos + DELTA[current_direction]
- info()
- end
- end
- function move_backward(amount, dig)
- rotate_right(2)
- move_forward(amount, dig)
- rotate_left(2)
- end
- function move_right(amount, dig)
- rotate_right()
- move_forward(amount, dig)
- rotate_left()
- end
- function move_left(amount, dig)
- rotate_left()
- move_forward(amount, dig)
- rotate_right()
- end
- function move_up(amount, dig)
- local amount = amount or 1
- local dig = dig or false
- for i=1, amount do
- while turtle.detectUp() do
- if dig then
- turtle.digUp()
- else
- print("Stepbro I'm Stuck! (above)")
- end
- end
- turtle.up()
- current_pos = current_pos + DELTA[UP]
- info()
- end
- end
- function move_down(amount, dig)
- local amount = amount or 1
- local dig = dig or 1
- for i=1, amount do
- while turtle.detectDown() do
- if dig then
- turtle.digDown()
- else
- print("Stepbro I'm Stuck! (below)")
- end
- end
- turtle.down()
- current_pos = current_pos + DELTA[DOWN]
- info()
- end
- end
- function go_down_to(level)
- for i=0,level do
- turtle.digDown()
- turtle.down()
- end
- end
- function manhattan_distance(pos1, pos2)
- return math.abs(pos1.x - pos2.x) + math.abs(pos1.y - pos2.y) + math.abs(pos1.z - pos2.z)
- end
- function get_available_fuel()
- local available_fuel = turtle.getFuelLevel()
- for i=1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == "minecraft:coal" then
- available_fuel = available_fuel + item.count * 80
- end
- end
- return available_fuel
- end
- function has_ore_in_vicinity()
- blocks = {}
- table.insert(select(2, blocks,turtle.inspectDown()).name)
- table.insert(select(2, blocks,turtle.inspectUp()).name)
- table.insert(select(2, blocks,turtle.inspect()).name)
- turtle.turnLeft()
- table.insert(select(2, blocks,turtle.inspect()).name)
- turtle.turnRight(2)
- table.insert(select(2, blocks,turtle.inspect()).name)
- turtle.turnLeft()
- for _, block in ipairs(blocks) do
- if BLOCKS_TO_MINE[block] ~= nil then
- return true
- end
- end
- return false
- end
- function mine_vein(depth, visited)
- local visited = visited or {}
- if depth == 0 then return end
- local local_start_dir = current_direction
- -- Forward/Backward/Left/Right
- for i=0, 3 do
- local new_dir = calculate_rotation(local_start_dir, i)
- local block = (current_pos + DELTA[new_dir]):tostring()
- if visited[block] == nil then
- visited[block] = true
- face(new_dir)
- local not_air, block_data = turtle.inspect()
- if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
- move_forward(1, true)
- mine_vein(depth - 1, visited)
- face(calculate_rotation(new_dir, 2))
- move_forward(1, true)
- end
- end
- end
- -- Up/Down
- local block = (current_pos + DELTA[UP]):tostring()
- if visited[block] == nil then
- visited[block] = true
- local not_air, block_data = turtle.inspectUp()
- if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
- move_up(1, true)
- mine_vein(depth - 1, visited)
- move_down(1, true)
- end
- end
- local block = (current_pos + DELTA[DOWN]):tostring()
- if visited[block] == nil then
- visited[block] = true
- local not_air, block_data = turtle.inspectDown()
- if not_air and BLOCKS_TO_MINE[block_data.name] ~= nil then
- move_down(1, true)
- mine_vein(depth - 1, visited)
- move_up(1, true)
- end
- end
- face(local_start_dir)
- end
- function main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement