Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Dig Down Script with Full Perimeter Safety and Return Logic
- -- Digs down while maintaining blocks in all 4 horizontal directions
- -- Removes any liquids encountered below
- -- Returns to original position when finished
- -- Uses any available blocks for placement
- local tArgs = {...}
- local depth = tonumber(tArgs[1])
- local shouldReturn = tArgs[2] == "return" or tArgs[2] == "true"
- if not depth or depth < 1 then
- print("Usage: digdown <depth> [return]")
- print("Example: digdown 20")
- print("Example with return: digdown 20 return")
- return
- end
- -- Track movements for return trip
- local movements = {}
- local currentY = 0
- -- Function to check if block is liquid
- local function isLiquid(block)
- return block and (block.name:find("water") or block.name:find("lava"))
- end
- -- Function to find any placeable block in inventory
- local function findPlaceableBlock()
- for slot = 1, 16 do
- turtle.select(slot)
- if turtle.getItemCount(slot) > 0 then
- return true
- end
- end
- return false
- end
- -- Function to ensure all 4 directions have blocks
- local function ensurePerimeter()
- -- Check and place blocks in all 4 directions
- for i = 1, 4 do
- local success, data = turtle.inspect()
- if not success or isLiquid(data) then
- -- Try to place any available block
- if findPlaceableBlock() then
- if not turtle.place() then
- print("Warning: Failed to place block in slot "..turtle.getSelectedSlot())
- end
- else
- print("Error: No placeable blocks in inventory!")
- return false
- end
- end
- turtle.turnRight() -- Rotate to next direction
- end
- return true
- end
- -- Function to safely dig down and maintain perimeter
- local function safeDigDown()
- -- First ensure perimeter is solid
- if not ensurePerimeter() then
- return false
- end
- -- Check block below
- local success, data = turtle.inspectDown()
- if success then
- if isLiquid(data) then
- -- Handle liquid below with any available block
- if findPlaceableBlock() then
- if not turtle.placeDown() then
- print("Warning: Failed to place block below")
- end
- else
- print("Error: No blocks to place below!")
- return false
- end
- end
- turtle.digDown()
- end
- -- Move down and record movement
- if turtle.down() then
- currentY = currentY - 1
- table.insert(movements, "down")
- -- Ensure new level has perimeter
- return ensurePerimeter()
- end
- return false
- end
- -- Function to return to surface
- local function returnToSurface()
- print("Returning to surface...")
- -- Reverse movement log
- for i = #movements, 1, -1 do
- local move = movements[i]
- if move == "down" then
- while not turtle.up() do
- turtle.digUp()
- sleep(0.5)
- end
- currentY = currentY + 1
- end
- end
- print("Returned to surface successfully! Current Y: "..currentY)
- end
- -- Check initial fuel requirements
- local requiredFuel = depth * 3 -- Extra for return trip and perimeter building
- if turtle.getFuelLevel() < requiredFuel then
- print("Need at least "..requiredFuel.." fuel units")
- print("Current fuel: "..turtle.getFuelLevel())
- return
- end
- -- Check for any blocks at all
- local hasBlocks = false
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- hasBlocks = true
- break
- end
- end
- if not hasBlocks then
- print("Warning: No blocks in inventory for perimeter building!")
- print("Recommend bringing at least "..depth.." blocks")
- end
- -- Main digging loop
- print("Starting descent to depth "..depth.."...")
- for i = 1, depth do
- if not safeDigDown() then
- print("Stopped at level "..i.." due to error")
- if shouldReturn then returnToSurface() end
- return
- end
- print("Level "..i.." - Fuel: "..turtle.getFuelLevel())
- -- Periodic status report
- if i % 5 == 0 then
- local remaining = depth - i
- if turtle.getFuelLevel() < remaining * 3 then
- print("Warning: Low fuel! "..turtle.getFuelLevel().." left, needs ~"..remaining*3)
- end
- end
- end
- print("Successfully reached depth "..depth.." with full perimeter safety!")
- -- Return to surface if requested
- if shouldReturn then
- returnToSurface()
- end
Advertisement
Add Comment
Please, Sign In to add comment