-- dig5x5.lua -- Mines a 5x5 shaft downward until bedrock. -- Start at one corner of the 5x5, facing across the area. -- Put a chest behind the turtle. Slot 16 is reserved for fuel. local SIZE = 5 local FUEL_SLOT = 16 local START_FUEL_MIN = 50 -- Internal position tracking. -- x/z are relative to the start corner. -- depth is blocks below the start level. -- dir: 0=east/forward from start, 1=south, 2=west, 3=north local x, z, depth, dir = 0, 0, 0, 0 local inUnload = false local goTo, safeDown, safeUp, checkInventory local function isBedrock(data) return data and data.name == "minecraft:bedrock" end local function isLava(data) return data and ( data.name == "minecraft:lava" or data.name == "minecraft:flowing_lava" ) end local function isUnlimitedFuel() return turtle.getFuelLevel() == "unlimited" end local function ensureFuel(want) if isUnlimitedFuel() then return end while turtle.getFuelLevel() < want do local before = turtle.getFuelLevel() local selected = turtle.getSelectedSlot() for i = 1, 16 do if turtle.getItemCount(i) > 0 then turtle.select(i) turtle.refuel(1) end if turtle.getFuelLevel() >= want then break end end turtle.select(selected) if turtle.getFuelLevel() <= before then error("Out of fuel. Put coal/charcoal/etc in slot 16.", 0) end end end local function hasEmptyMiningSlot() for i = 1, FUEL_SLOT - 1 do if turtle.getItemCount(i) == 0 then return true end end return false end local function unloadItems() print("Unloading...") for i = 1, FUEL_SLOT - 1 do if turtle.getItemCount(i) > 0 then turtle.select(i) local ok, err = turtle.drop() if not ok and turtle.getItemCount(i) > 0 then error("Unload failed. Chest behind turtle may be full/missing: " .. tostring(err), 0) end end end turtle.select(1) end local function updateForwardPosition() if dir == 0 then x = x + 1 elseif dir == 1 then z = z + 1 elseif dir == 2 then x = x - 1 elseif dir == 3 then z = z - 1 end end local function turnLeft() turtle.turnLeft() dir = (dir + 3) % 4 end local function turnRight() turtle.turnRight() dir = (dir + 1) % 4 end local function face(want) while dir ~= want do local diff = (want - dir) % 4 if diff == 1 then turnRight() elseif diff == 3 then turnLeft() else turnRight() turnRight() end end end local function clearFront(allowUnload) while turtle.detect() do local ok, data = turtle.inspect() if ok and isBedrock(data) then return false, "bedrock in front" end if ok and isLava(data) then error("Lava in front. Stopping for safety.", 0) end if allowUnload then checkInventory() end local dug, err = turtle.dig() if not dug and turtle.detect() then return false, "cannot dig front: " .. tostring(err) end sleep(0.2) end return true end local function safeForward(allowUnload) ensureFuel(1) local tries = 0 while true do local ok, err = turtle.forward() if ok then updateForwardPosition() return true end local cleared, reason = clearFront(allowUnload) if not cleared then return false, reason end turtle.attack() tries = tries + 1 if tries > 20 then error("Cannot move forward: " .. tostring(err), 0) end sleep(0.2) end end safeUp = function(allowUnload) ensureFuel(1) local tries = 0 while true do local ok, err = turtle.up() if ok then depth = depth - 1 return true end if turtle.detectUp() then local hasBlock, data = turtle.inspectUp() if hasBlock and isLava(data) then error("Lava above. Stopping for safety.", 0) end if allowUnload then checkInventory() end local dug, digErr = turtle.digUp() if not dug and turtle.detectUp() then error("Cannot dig up: " .. tostring(digErr), 0) end else turtle.attackUp() tries = tries + 1 if tries > 20 then error("Cannot move up: " .. tostring(err), 0) end end sleep(0.2) end end safeDown = function(allowUnload) ensureFuel(1) local tries = 0 while true do local ok, err = turtle.down() if ok then depth = depth + 1 return true end if turtle.detectDown() then local hasBlock, data = turtle.inspectDown() if hasBlock and isBedrock(data) then return false, "bedrock below" end if hasBlock and isLava(data) then error("Lava below. Stopping for safety.", 0) end if allowUnload then checkInventory() end local dug, digErr = turtle.digDown() if not dug and turtle.detectDown() then hasBlock, data = turtle.inspectDown() if hasBlock and isBedrock(data) then return false, "bedrock below" end error("Cannot dig down: " .. tostring(digErr), 0) end else turtle.attackDown() tries = tries + 1 if tries > 20 then error("Cannot move down: " .. tostring(err), 0) end end sleep(0.2) end end goTo = function(tx, tz, allowUnload) while x < tx do face(0) local ok, reason = safeForward(allowUnload) if not ok then error("Path blocked east: " .. tostring(reason), 0) end end while x > tx do face(2) local ok, reason = safeForward(allowUnload) if not ok then error("Path blocked west: " .. tostring(reason), 0) end end while z < tz do face(1) local ok, reason = safeForward(allowUnload) if not ok then error("Path blocked south: " .. tostring(reason), 0) end end while z > tz do face(3) local ok, reason = safeForward(allowUnload) if not ok then error("Path blocked north: " .. tostring(reason), 0) end end end checkInventory = function() if inUnload then return end if hasEmptyMiningSlot() then return end inUnload = true local savedX = x local savedZ = z local savedDepth = depth local savedDir = dir print("Inventory full. Returning to unload.") goTo(0, 0, false) face(0) while depth > 0 do safeUp(false) end -- Chest should be behind the turtle's start position. face(2) unloadItems() -- Return to where we were. face(0) while depth < savedDepth do local ok, reason = safeDown(false) if not ok then error("Could not return to mining depth: " .. tostring(reason), 0) end end goTo(savedX, savedZ, false) face(savedDir) inUnload = false end local function mineStep() checkInventory() local ok, reason = safeForward(true) if not ok then return false, reason end return true end local function mineLayer() face(0) for row = 1, SIZE do for col = 1, SIZE - 1 do local ok, reason = mineStep() if not ok then return false, reason end end if row < SIZE then if row % 2 == 1 then turnRight() local ok, reason = mineStep() if not ok then return false, reason end turnRight() else turnLeft() local ok, reason = mineStep() if not ok then return false, reason end turnLeft() end end end return true end print("Starting 5x5 bedrock miner.") print("Start at Y=60 corner, facing into the 5x5.") print("Chest behind turtle. Fuel in slot 16.") ensureFuel(START_FUEL_MIN) while true do print("Mining layer, depth " .. depth) local fullLayer, reason = mineLayer() goTo(0, 0, false) face(0) checkInventory() if not fullLayer then print("Stopped while mining layer: " .. tostring(reason)) break end local movedDown, downReason = safeDown(true) if not movedDown then print("Reached bedrock: " .. tostring(downReason)) break end end print("Returning to surface...") goTo(0, 0, false) face(0) while depth > 0 do safeUp(false) end face(2) unloadItems() face(0) print("Done. Turtle is back at the start.")