CCCPaste

cb302d2a578ac

-- Optimized tunnel digger (X width, Y height, Z length)

local function askNumber(label)
  write(label .. ": ")
  return tonumber(read())
end

local Y = askNumber("Y")
local X = askNumber("X")
local Z = askNumber("Z")

-- ---------- helpers ----------
local function digForwardUntilClear()
  -- Handle gravel/sand + mobs
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
    sleep(0.2)
  end
end

local function goUp()
  while not turtle.up() do
    turtle.digUp()
    turtle.attackUp()
    sleep(0.2)
  end
end

local function goDown()
  while not turtle.down() do
    turtle.digDown()
    turtle.attackDown()
    sleep(0.2)
  end
end

local function turnTo(side) -- "left" or "right"
  if side == "left" then turtle.turnLeft() else turtle.turnRight() end
end

-- Move laterally one step in the current facing (after turning once at row start)
local function stepLateral()
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
    sleep(0.1)
  end
end

-- ---------- main ----------
local goingRight = true

for zi = 1, Z do
  -- Carve the X×Y cross-section of this slice with minimal turns
  for y = 1, Y do
    -- Face lateral direction ONCE at row start
    turnTo(goingRight and "right" or "left")

    -- Traverse the row across X-1 steps (no extra turns inside the row)
    for x = 1, X - 1 do
      turtle.dig()       -- clear the next lateral block if any (cheap if air)
      stepLateral()      -- move one to the side
      turtle.digUp()     -- optional: clear ceiling as you go
      turtle.digDown()   -- optional: clear floor as you go
    end

    -- Face forward again at row end (U-turn direction depends on side)
    turnTo(goingRight and "left" or "right")

    -- Next row will go the other way
    goingRight = not goingRight

    -- Climb to next row if needed
    if y ~= Y then
      turtle.digUp()
      goUp()
    end
  end

  -- Return to bottom of slice
  for _ = 1, Y - 1 do
    goDown()
  end

  -- Only now clear the *front* and advance to next slice (one dig + one move)
  if zi ~= Z then
    turtle.dig()
    digForwardUntilClear()
  end
end