-- Robust Multi-Layer 9x9 Quarry Down to Y=-53
-- Fully auto-refuels, auto-empties, handles falling blocks, safe movement
-- Chest behind turtle for fuel and storage
local quarrySize = 9
local targetY = -53
local fuelSlot = 16
local tossGarbage = true
local MAX_ATTEMPTS = 20 -- max dig/move attempts per block
-- Track turtle's current relative Y
local currentY = 0
-- -------------------------
-- Utility Functions
-- -------------------------
local function tryRefuel()
turtle.select(fuelSlot)
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 100 then
turtle.suck()
if turtle.getItemCount(fuelSlot) > 0 then turtle.refuel() end
end
end
local function emptyInventory()
for s=1,16 do
turtle.select(s)
turtle.drop()
end
end
local function checkInventory()
local fullSlots = 0
for s=1,16 do
if turtle.getItemCount(s) > 0 then
if tossGarbage then
local detail = turtle.getItemDetail(s)
if detail then
local name = detail.name
if name=="minecraft:cobblestone" or name=="minecraft:stone" or name=="minecraft:dirt" or name=="minecraft:gravel" then
turtle.select(s)
turtle.drop()
end
end
end
if turtle.getItemCount(s) > 0 then fullSlots = fullSlots + 1 end
end
end
if fullSlots >= 16 then emptyInventory() end
end
-- Safe movement functions
local function safeForward()
local attempts = 0
while not turtle.forward() do
turtle.dig()
attempts = attempts + 1
sleep(0.1)
if attempts >= MAX_ATTEMPTS then error("Blocked forward") end
end
end
local function safeDown()
local attempts = 0
while not turtle.down() do
turtle.digDown()
attempts = attempts + 1
sleep(0.1)
if attempts >= MAX_ATTEMPTS then error("Blocked down") end
end
currentY = currentY - 1
end
local function safeUp()
local attempts = 0
while not turtle.up() do
turtle.digUp()
attempts = attempts + 1
sleep(0.1)
if attempts >= MAX_ATTEMPTS then error("Blocked up") end
end
currentY = currentY + 1
end
-- -------------------------
-- Mine a single 9x9 layer
-- -------------------------
local function mineLayer()
local turn = 0
for row=1,quarrySize do
for col=1,quarrySize-1 do
safeForward()
checkInventory()
tryRefuel()
end
if row < quarrySize then
if turn == 0 then
turtle.turnRight()
safeForward()
turtle.turnRight()
turn = 1
else
turtle.turnLeft()
safeForward()
turtle.turnLeft()
turn = 0
end
checkInventory()
tryRefuel()
end
end
end
-- -------------------------
-- Return to chest behind
-- -------------------------
local function returnToChest()
turtle.turnLeft()
turtle.turnLeft()
for i=1,quarrySize-1 do safeForward() end
turtle.turnLeft()
turtle.turnLeft()
emptyInventory()
end
-- -------------------------
-- Main Quarry Loop: mine down to targetY
-- -------------------------
while currentY > targetY do
print("Mining layer at Y="..currentY)
mineLayer()
if currentY > targetY then
for i=1,quarrySize-1 do safeDown() end
turtle.turnLeft()
turtle.turnLeft() -- face starting row for next layer
end
end
-- Final empty and done
returnToChest()
print("All done! Mined down to Y="..targetY)