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,
- }
- BLOCKS_PER_CHUNK = 16
- MAX_LOADED_CHUNKS_PER_DIRECTION = 20
- FUEL_PER_COAL = 80
- FUEL_SAFETY_MARGIN = 1 * FUEL_PER_COAL
- 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 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 * FUEL_PER_COAL
- end
- end
- return available_fuel
- end
- function has_ore_in_vicinity()
- blocks = {}
- table.insert(blocks, select(2, turtle.inspectDown()).name)
- table.insert(blocks, select(2, turtle.inspectUp()).name)
- table.insert(blocks, select(2, turtle.inspect()).name)
- rotate_left()
- table.insert(blocks, select(2, turtle.inspect()).name)
- rotate_right(2)
- table.insert(blocks, select(2, turtle.inspect()).name)
- rotate_left()
- 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 go_down(level)
- move_down(level, true)
- end
- function go_up()
- while current_pos.y ~= start_pos.y do
- move_up(1, true)
- end
- end
- function go_back_to_mine_start()
- rotate_right(2)
- while current_pos.x ~= start_pos.x and current_pos.z ~= start_pos.z do
- move_forward(1, true)
- end
- end
- function should_return_home()
- local distance = manhattan_distance(current_pos, start_pos)
- local available_fuel = get_available_fuel()
- if available_fuel - FUEL_SAFETY_MARGIN <= distance then
- return true
- end
- return false
- end
- function strip_mine(distance)
- distance = distance or BLOCKS_PER_CHUNK * MAX_LOADED_CHUNKS_PER_DIRECTION
- local distance_mined = 0
- while distance_mined < distance and not should_return_home() do
- if has_ore_in_vicinity() then
- mine_vein(99)
- end
- move_forward(1, true)
- distance_mined = distance_mined + 1
- end
- end
- function main()
- go_down()
- strip_mine()
- go_back_to_mine_start()
- go_up()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement