CCCPaste

Mining Turtle Paste

-- Mine Area based on current turtle position
-- Usage: mineArea_XYZ   

local args = {...}
local width = tonumber(args[1]) or 5
local length = tonumber(args[2]) or 5
local depth = tonumber(args[3]) or 3

-- Track turtle position and facing
local facing = 0 -- 0=North, 1=East, 2=South, 3=West

-- Function to turn left
local function turnLeft()
  turtle.turnLeft()
  facing = (facing - 1) % 4
end

-- Function to turn right
local function turnRight()
  turtle.turnRight()
  facing = (facing + 1) % 4
end

-- Function to move forward with digging
local function digForward()
  while turtle.detect() do
    turtle.dig()
    os.sleep(0.5)
  end
  turtle.forward()
end

-- Function to mine a single layer
local function mineLayer()
  for z = 1, length do
    for x = 1, width - 1 do
      digForward()
    end
    
    -- Move to next row if not at the end
    if z < length then
      if z % 2 == 1 then
        turnRight()
        digForward()
        turnRight()
      else
        turnLeft()
        digForward()
        turnLeft()
      end
    end
  end
end

-- Function to return to start position of layer
local function returnToStart()
  if length % 2 == 0 then
    turnRight()
    turnRight()
    for i = 1, width - 1 do
      turtle.forward()
    end
    turnLeft()
  else
    turnRight()
  end
  
  for i = 1, length - 1 do
    turtle.forward()
  end
  turnRight()
end

-- Main mining routine
print("Mining area: " .. width .. "x" .. length .. "x" .. depth)

for currentDepth = 1, depth do
  print("Mining layer " .. currentDepth .. " of " .. depth)
  
  -- Mine current layer
  mineLayer()
  
  -- Return to start position
  returnToStart()
  
  -- Go down if not finished
  if currentDepth < depth then
    while turtle.detectDown() do
      turtle.digDown()
      os.sleep(0.5)
    end
    turtle.down()
  end
end

-- Return to surface
print("Returning to surface...")
for i = 1, depth - 1 do
  turtle.up()
end

print("Mining complete!")