CCCPaste
--[[
  Advanced Turtle Mining Script (v5 - Column-by-Column)
  Mines an NxMxP cuboid one vertical column at a time
  to ensure the turtle is always accessible.
--]]

-------------------------------------------------------------------------------
-- ## Configuration & Argument Parsing
-------------------------------------------------------------------------------

local args = { ... }
if #args < 4 then
  print("Usage: mine    ")
  print("Corner can be: bl, br, tl, tr")
  return
end

local width = assert(tonumber(args[1]), "Invalid width")
local depth = assert(tonumber(args[2]), "Invalid depth")
local height = assert(tonumber(args[3]), "Invalid height")
local startCorner = args[4]:lower()

-- Determine initial mining directions
local mineRight, mineUp
if startCorner == "bl" then
  mineRight = true; mineUp = true
elseif startCorner == "br" then
  mineRight = false; mineUp = true
elseif startCorner == "tl" then
  mineRight = true; mineUp = false
elseif startCorner == "tr" then
  mineRight = false; mineUp = false
else
  error("Invalid corner '" .. startCorner .. "'. Use bl, br, tl, or tr.")
end

-------------------------------------------------------------------------------
-- ## State Tracking and Movement Wrappers
-------------------------------------------------------------------------------

-- Global position and orientation state
local posX, posY, posZ = 0, 0, 0
local orientation = 0 -- 0: +Z (forward), 1: +X (right), 2: -Z (back), 3: -X (left)

-- Wrapped turn functions to track orientation
local function goRight()
  turtle.turnRight()
  orientation = (orientation + 1) % 4
end

local function goLeft()
  turtle.turnLeft()
  orientation = (orientation - 1 + 4) % 4
end

local function goTurn180()
  turtle.turnRight()
  turtle.turnRight()
  orientation = (orientation + 2) % 4
end

-- ** NEW function: Moves WITHOUT digging **
local function justMove(direction)
  local moveAction, success
  if direction == "forward" then moveAction = turtle.forward
  elseif direction == "up" then moveAction = turtle.up
  elseif direction == "down" then moveAction = turtle.down
  else error("Invalid move direction") end

  while true do
    success = moveAction()
    if success then
      -- Update position
      if direction == "forward" then
        if orientation == 0 then posZ = posZ + 1
        elseif orientation == 1 then posX = posX + 1
        elseif orientation == 2 then posZ = posZ - 1
        elseif orientation == 3 then posX = posX - 1
        end
      elseif direction == "up" then posY = posY + 1
      elseif direction == "down" then posY = posY - 1
      end
      break
    else
      print("Movement blocked. Retrying...")
      os.sleep(3)
    end
  end
end

-- ** This function ALWAYS digs, then moves **
local function mineAndMove(direction)
  local digAction, moveAction

  if direction == "forward" then
    digAction = turtle.dig; moveAction = turtle.forward
  elseif direction == "up" then
    digAction = turtle.digUp; moveAction = turtle.up
  elseif direction == "down" then
    digAction = turtle.digDown; moveAction = turtle.down
  else
    error("Invalid move direction: " .. tostring(direction))
  end
  
  while true do
    digAction()
    if moveAction() then
      -- On successful move, update coordinates and break
      if direction == "forward" then
        if orientation == 0 then posZ = posZ + 1
        elseif orientation == 1 then posX = posX + 1
        elseif orientation == 2 then posZ = posZ - 1
        elseif orientation == 3 then posX = posX - 1
        end
      elseif direction == "up" then posY = posY + 1
      elseif direction == "down" then posY = posY - 1
      end
      break -- Exit retry loop
    else
      print("Movement blocked. Retrying...")
      os.sleep(3)
    end
  end
end

-------------------------------------------------------------------------------
-- ## Main Mining Logic (Column-by-Column)
-------------------------------------------------------------------------------

print(string.format("Starting to mine %dx%dx%d cuboid...", width, depth, height))

local x_dir = mineRight and 1 or -1
local y_dir = mineUp and 1 or -1

-- Determine Y move and return directions
local y_move_dir = y_dir == 1 and "up" or "down"
local y_return_dir = y_dir == 1 and "down" or "up"

for z = 1, depth do
  -- 1. Align for the row
  if z == 1 then -- Very first row, turn from +Z to X
    if x_dir == 1 then goRight() else goLeft() end
  else -- Subsequent rows, we are facing +Z after row-change
    if x_dir == 1 then goRight() else goLeft() end
  end
  
  print(string.format("Mining row %d/%d. Facing %s", z, depth, x_dir == 1 and "+X" or "-X"))

  for x = 1, width do
    -- 2. Mine the horizontal block for this column
    if x > 1 or z > 1 then
      -- (The very first block (1,1) is mined by the first *column* move)
      -- (But subsequent horizontal moves must dig)
      if x > 1 then
        mineAndMove("forward")
      end
    end

    -- 3. Mine the vertical column
    -- (We mine height-1 because the horizontal move already cleared 1 block)
    for y = 1, height - 1 do
      mineAndMove(y_move_dir)
    end
    
    -- 4. Return to the starting Y-level (without digging)
    for y = 1, height - 1 do
      justMove(y_return_dir)
    end
  end -- end x loop

  -- 5. Transition to the next row (Z-move)
  if z < depth then
    -- Turn back to face +Z
    if x_dir == 1 then goLeft() else goRight() end
    
    mineAndMove("forward") -- Mine the first block of the next row
    
    -- Flip horizontal direction
    x_dir = -x_dir
  end
end -- end z loop


print("Mining operation complete!")

-------------------------------------------------------------------------------
-- ## Return to Start Sequence (Unchanged)
-------------------------------------------------------------------------------

-- Function to turn in the most efficient direction
local function faceDirection(targetOrientation)
  local rightTurns = (targetOrientation - orientation + 4) % 4
  if rightTurns == 1 then
    goRight()
  elseif rightTurns == 2 then
    goTurn180()
  elseif rightTurns == 3 then
    goLeft()
  end
end

print(string.format("Final position: (%d, %d, %d). Returning home...", posX, posY, posZ))

-- 1. Return to starting elevation (Y=0)
while posY > 0 do justMove("down") end -- Use justMove
while posY < 0 do justMove("up") end   -- Use justMove

-- 2. Return along the Z axis to Z=0
if posZ > 0 then -- We are in front of the start, move back
  faceDirection(2) -- Face -Z
  for i = 1, posZ do justMove("forward") end
elseif posZ < 0 then -- We are behind the start, move forward
  faceDirection(0) -- Face +Z
  for i = 1, -posZ do justMove("forward") end
end
posZ = 0

-- 3. Return along the X axis to X=0
if posX > 0 then -- We are right of the start, move left
  faceDirection(3) -- Face -X
  for i = 1, posX do justMove("forward") end
elseif posX < 0 then -- We are left of the start, move right
  faceDirection(1) -- Face +X
  for i = 1, -posX do justMove("forward") end
end
posX = 0

-- 4. Reset to original orientation (facing forward)
faceDirection(0) -- Face +Z (0)

print("Returned to starting position.")