Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local FORWARD = "F"
- local BACK = "B"
- local LEFT = "L"
- local RIGHT = "R"
- local UP = "U"
- local DOWN = "D"
- -- Position tracking
- local currentX = 0
- local currentY = 0
- local currentZ = 0
- local facing = 0 -- 0=+Y, 1=+X, 2=-Y, 3=-X
- -- Improved mining algorithm
- local function mine_room(L, W, H)
- local full_path = {}
- local base_pattern = "cross"
- -- Pattern selection logic
- if W == 2 then
- base_pattern = "narrow"
- elseif L % 2 == 1 and W % 2 == 1 then
- base_pattern = "spiral"
- elseif math.abs(L - W) <= 2 then
- base_pattern = "square_snake"
- else
- base_pattern = "rectangle_optim"
- end
- for layer = 1, H do
- local layer_path = {}
- local start_x, start_y = currentX, currentY
- -- Layer mining patterns
- if base_pattern == "spiral" then
- local rings = math.floor(math.min(L, W) / 2) -- Correct Lua syntax
- for ring = 0, rings do
- -- Right wall
- for _ = 1, W - (ring*2) - 1 do
- table.insert(layer_path, FORWARD)
- currentX = currentX + 1
- end
- -- Bottom wall
- for _ = 1, L - (ring*2) - 2 do
- table.insert(layer_path, RIGHT)
- table.insert(layer_path, FORWARD)
- facing = (facing + 1) % 4
- currentY = currentY + 1
- end
- -- Left wall
- for _ = 1, W - (ring*2) - 1 do
- table.insert(layer_path, BACK)
- currentX = currentX - 1
- end
- -- Top wall
- for _ = 1, L - (ring*2) - 2 do
- table.insert(layer_path, LEFT)
- table.insert(layer_path, BACK)
- facing = (facing - 1) % 4
- currentY = currentY - 1
- end
- end
- elseif base_pattern == "narrow" then
- for row = 1, L do
- table.insert(layer_path, FORWARD)
- currentX = currentX + 1
- if row < L then
- table.insert(layer_path, RIGHT)
- table.insert(layer_path, FORWARD)
- table.insert(layer_path, RIGHT)
- facing = (facing + 2) % 4
- currentY = currentY + 1
- end
- end
- else
- local horizontal_first = W >= L
- for pass = 1, 2 do
- local main_axis = horizontal_first and W or L
- local cross_axis = horizontal_first and L or W
- for i = 1, cross_axis do
- for _ = 1, main_axis - 1 do
- table.insert(layer_path, horizontal_first and FORWARD or RIGHT)
- if horizontal_first then
- currentX = currentX + 1
- else
- facing = (facing + 1) % 4
- currentY = currentY + 1
- end
- end
- if i < cross_axis then
- table.insert(layer_path, horizontal_first and RIGHT or FORWARD)
- if horizontal_first then
- facing = (facing + 1) % 4
- currentY = currentY + 1
- else
- currentX = currentX + 1
- end
- end
- end
- horizontal_first = not horizontal_first
- end
- end
- -- Optimized layer return
- local x_diff = currentX - start_x
- local y_diff = currentY - start_y
- if x_diff ~= 0 then
- table.insert(layer_path, x_diff > 0 and LEFT or RIGHT)
- facing = (facing + (x_diff > 0 and -1 or 1)) % 4
- for _ = 1, math.abs(x_diff) do
- table.insert(layer_path, BACK)
- currentX = currentX - (x_diff > 0 and 1 or -1)
- end
- end
- if y_diff ~= 0 then
- table.insert(layer_path, y_diff > 0 and LEFT or RIGHT)
- facing = (facing + (y_diff > 0 and -1 or 1)) % 4
- for _ = 1, math.abs(y_diff) do
- table.insert(layer_path, BACK)
- currentY = currentY - (y_diff > 0 and 1 or -1)
- end
- end
- -- Vertical transition
- if layer < H then
- table.insert(layer_path, UP)
- currentZ = currentZ + 1
- end
- table.insert(full_path, table.concat(layer_path))
- end
- -- Final return to base
- for _ = 1, H-1 do
- table.insert(full_path, DOWN)
- currentZ = currentZ - 1
- end
- return {table.concat(full_path, ""), 1}
- end
- -- Updated direction handling
- local function direction_to_value(direction)
- return {
- [FORWARD] = 0,
- [RIGHT] = 1,
- [BACK] = 2,
- [LEFT] = 3,
- [UP] = 4,
- [DOWN] = 5
- }[direction] or 0
- end
- -- Enhanced rotation logic
- local function rotate(current_facing, target_direction)
- local diff = (target_direction - current_facing) % 4
- if diff == 1 then
- turtle.turnRight()
- elseif diff == 2 then
- turtle.turnRight()
- turtle.turnRight()
- elseif diff == 3 then
- turtle.turnLeft()
- end
- end
- -- Improved path executor
- local function dig_by_path(path)
- local current_facing = 0
- for direction in path:gmatch(".") do
- local target_value = direction_to_value(direction)
- if direction == UP then
- turtle.up()
- currentZ = currentZ + 1
- elseif direction == DOWN then
- turtle.down()
- currentZ = currentZ - 1
- else
- if target_value ~= current_facing then
- rotate(current_facing, target_value)
- current_facing = target_value
- end
- if turtle.detect() then
- turtle.dig()
- end
- turtle.forward()
- -- Update position based on facing
- if current_facing == 0 then
- currentY = currentY + 1
- elseif current_facing == 1 then
- currentX = currentX + 1
- elseif current_facing == 2 then
- currentY = currentY - 1
- else
- currentX = currentX - 1
- end
- end
- end
- end
- -- Unified dig function
- local function dig_room(L, W, H)
- currentX, currentY, currentZ = 0, 0, 0 -- Reset position
- local path_data = mine_room(L, W, H)
- print("Optimized Path: "..path_data[1])
- dig_by_path(path_data[1])
- end
- -- Main program remains the same
- local args = {...}
- if #args < 3 then
- print("Usage: digRoom <length> <width> <height>")
- return
- end
- local length = tonumber(args[1])
- local width = tonumber(args[2])
- local height = tonumber(args[3])
- if not (length and width and height) or length < 1 or width < 1 or height < 1 then
- print("Invalid dimensions!")
- return
- end
- dig_room(length, width, height)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement