Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Program: digToDepth
- -- Digs a 2-block tall shaft (1x1 base, 2 high) to a specified depth, handling liquids and compressing cobblestone
- -- Get depth from command line argument
- local args = {...}
- if #args < 1 then
- print("Usage: digToDepth <depth>")
- return
- end
- local depth = tonumber(args[1])
- if not depth or depth < 1 then
- print("Error: Depth must be a positive number")
- return
- end
- -- Maximum attempts to move forward or down (safety for unbreakable blocks)
- local MAX_ATTEMPTS = 100
- -- Function to ensure turtle has enough fuel
- local function checkFuel()
- if turtle.getFuelLevel() < 1 then
- print("Out of fuel!")
- return false
- end
- return true
- end
- -- Function to find a placeable block in inventory (for sealing liquids)
- local function findPlaceableBlock()
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and turtle.select(slot) then
- -- Assume most mined blocks are placeable (cobblestone, dirt, etc.)
- return true
- end
- end
- print("No placeable blocks in inventory!")
- return false
- end
- -- Function to place a block in a specific direction
- local function placeBlock(direction)
- if not findPlaceableBlock() then
- return false
- end
- if direction == "forward" then
- return turtle.place()
- elseif direction == "up" then
- return turtle.placeUp()
- elseif direction == "down" then
- return turtle.placeDown()
- elseif direction == "back" then
- return turtle.place() -- After moving forward, place behind
- end
- return false
- end
- -- Function to check if a block is a liquid (source or flowing)
- local function isLiquid(data)
- return data and data.name and (
- data.name:match("water") or
- data.name:match("lava") or
- data.name:match("oil") or
- data.name:match("fluid")
- )
- end
- -- Function to compress cobblestone when 9 or more are in inventory
- local function compressCobblestone()
- -- Count total cobblestone in inventory
- local cobbleCount = 0
- local cobbleSlots = {} -- Track slots containing cobblestone
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and item.name == "minecraft:cobblestone" then
- cobbleCount = cobbleCount + turtle.getItemCount(slot)
- table.insert(cobbleSlots, {slot = slot, count = turtle.getItemCount(slot)})
- end
- end
- -- If 9 or more cobblestone, compress them
- while cobbleCount >= 9 do
- -- Clear crafting slots (1-9)
- for slot = 1, 9 do
- turtle.select(slot)
- if turtle.getItemCount(slot) > 0 then
- -- Move items to slots 10-16
- for targetSlot = 10, 16 do
- if turtle.getItemCount(targetSlot) == 0 then
- turtle.transferTo(targetSlot)
- break
- end
- end
- end
- end
- -- Arrange 9 cobblestone in slots 1-9
- local remaining = 9
- for _, cobble in ipairs(cobbleSlots) do
- if remaining > 0 then
- turtle.select(cobble.slot)
- local toTransfer = math.min(remaining, cobble.count)
- for slot = 1, 9 do
- if turtle.getItemCount(slot) == 0 then
- turtle.select(cobble.slot)
- turtle.transferTo(slot, toTransfer)
- remaining = remaining - toTransfer
- cobble.count = cobble.count - toTransfer
- break
- end
- end
- end
- end
- -- Craft Compressed Cobblestone
- turtle.select(1)
- if not turtle.craft() then
- print("Failed to craft Compressed Cobblestone!")
- return false
- end
- -- Update cobbleCount after crafting (9 cobblestone → 1 Compressed Cobblestone)
- cobbleCount = cobbleCount - 9
- -- Recount cobblestone for the next iteration
- cobbleSlots = {}
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and item.name == "minecraft:cobblestone" then
- table.insert(cobbleSlots, {slot = slot, count = turtle.getItemCount(slot)})
- cobbleCount = cobbleCount + turtle.getItemCount(slot)
- end
- end
- end
- return true
- end
- -- Function to check for liquids and seal them (used for up direction only now)
- local function checkForLiquid()
- -- Check above
- local success, data = turtle.inspectUp()
- if success and isLiquid(data) then
- if not placeBlock("up") then
- print("Cannot seal liquid above!")
- return false
- end
- end
- return true
- end
- -- Function to dig forward until no blocks remain, handling liquids
- local function digForward()
- local attempts = 0
- local success = false
- -- Keep trying to move forward until success or max attempts reached
- repeat
- attempts = attempts + 1
- -- Check for a block or liquid in front
- local hasBlock = turtle.detect()
- local successInspect, data = turtle.inspect()
- if hasBlock and not (successInspect and isLiquid(data)) then
- -- If it's a solid block (not a liquid), dig it
- turtle.dig() -- Dig block in front
- sleep(0.5) -- Wait for falling blocks like sand/gravel
- elseif successInspect and isLiquid(data) then
- -- If it's a liquid (source or flowing), move into it and seal it
- if not turtle.forward() then
- print("Cannot move into liquid block forward!")
- return false
- end
- -- Place a block behind to seal the liquid
- turtle.turnLeft()
- turtle.turnLeft() -- Face backward
- if not placeBlock("back") then
- print("Cannot seal liquid behind!")
- turtle.turnLeft()
- turtle.turnLeft() -- Face forward again
- turtle.back() -- Move back to original position
- return false
- end
- turtle.turnLeft()
- turtle.turnLeft() -- Face forward again
- turtle.back() -- Move back to original position
- -- Continue the loop to check the next block
- else
- -- No block or liquid, try to move forward
- success = turtle.forward()
- if not success then
- print("Forward blocked, retrying (" .. attempts .. "/" .. MAX_ATTEMPTS .. ")")
- sleep(0.5) -- Wait before retrying
- end
- end
- -- Attempt to compress cobblestone after each forward attempt
- compressCobblestone()
- until success or attempts >= MAX_ATTEMPTS
- if not success then
- print("Stopped: Cannot move forward after " .. MAX_ATTEMPTS .. " attempts")
- return false
- end
- return true
- end
- -- Function to dig up until no blocks remain
- local function digUp()
- while turtle.detectUp() do
- turtle.digUp() -- Dig block above
- sleep(0.5) -- Wait for falling blocks
- end
- -- Check for liquids above
- if not checkForLiquid() then
- print("Stopped: Cannot handle liquid above")
- return false
- end
- -- Attempt to compress cobblestone after digging up
- compressCobblestone()
- return true
- end
- -- Function to dig down until no blocks remain, handling liquids
- local function digDown()
- local attempts = 0
- local success = false
- -- Keep trying to move down until success or max attempts reached
- repeat
- attempts = attempts + 1
- -- Check for a block or liquid below
- local hasBlock = turtle.detectDown()
- local successInspect, data = turtle.inspectDown()
- if hasBlock and not (successInspect and isLiquid(data)) then
- -- If it's a solid block (not a liquid), dig it
- turtle.digDown() -- Dig block below
- sleep(0.5) -- Wait for falling blocks like sand/gravel
- elseif successInspect and isLiquid(data) then
- -- If it's a liquid (source or flowing), move into it and seal it
- if not turtle.down() then
- print("Cannot move into liquid block below!")
- return false
- end
- -- Place a block above to seal the liquid
- if not placeBlock("up") then
- print("Cannot seal liquid above!")
- turtle.up() -- Move back to original position
- return false
- end
- turtle.up() -- Move back to original position
- -- Continue the loop to check the next block
- else
- -- No block or liquid, try to move down
- success = turtle.down()
- if not success then
- print("Down blocked, retrying (" .. attempts .. "/" .. MAX_ATTEMPTS .. ")")
- sleep(0.5) -- Wait before retrying
- end
- end
- -- Attempt to compress cobblestone after each down attempt
- compressCobblestone()
- until success or attempts >= MAX_ATTEMPTS
- if not success then
- print("Stopped: Cannot move down after " .. MAX_ATTEMPTS .. " attempts")
- return false
- end
- return true
- end
- -- Main digging loop
- local function digToDepth(targetDepth)
- local currentDepth = 0
- while currentDepth < targetDepth do
- -- Check fuel
- if not checkFuel() then
- return
- end
- -- Step 1: Dig and move forward
- if not digForward() then
- print("Stopped: Cannot proceed forward")
- return
- end
- -- Step 2: Dig up to clear 2-block height
- if not digUp() then
- print("Stopped: Cannot dig up")
- return
- end
- -- Step 3: Dig and move down
- if not digDown() then
- print("Stopped: Cannot proceed down")
- return
- end
- currentDepth = currentDepth + 1
- print("Depth reached: " .. currentDepth .. "/" .. targetDepth)
- end
- print("Reached target depth: " .. targetDepth)
- end
- -- Run the program
- digToDepth(depth)
Add Comment
Please, Sign In to add comment