Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Advanced Quarry Turtle for CC: Tweaked
- -- Mines 5x5 area down to bedrock with state persistence
- -- Configuration
- local config = {
- quarryWidth = 5, -- Width of quarry
- quarryLength = 5, -- Length of quarry
- minFuelLevel = 500, -- Minimum fuel before returning
- torchInterval = 5, -- Place torch every N layers
- chestCheckInterval = 5 -- Check inventory every N layers
- }
- -- State tracking
- local state = {
- x = 0, y = 0, z = 0,
- facing = 0, -- 0=north, 1=east, 2=south, 3=west
- layer = 0,
- rowInLayer = 0,
- blockInRow = 0,
- startX = 0, startY = 0, startZ = 0,
- quarrying = true,
- returningForFuel = false,
- miningPattern = "rows" -- rows or spiral
- }
- -- Utility functions
- local function saveState()
- local file = fs.open("quarry_state", "w")
- file.write(textutils.serialize(state))
- file.close()
- end
- local function loadState()
- if fs.exists("quarry_state") then
- local file = fs.open("quarry_state", "r")
- state = textutils.unserialize(file.readAll())
- file.close()
- return true
- end
- return false
- end
- local function deleteState()
- if fs.exists("quarry_state") then
- fs.delete("quarry_state")
- end
- end
- -- Movement tracking functions
- local function forward()
- if turtle.forward() then
- if state.facing == 0 then state.z = state.z - 1
- elseif state.facing == 1 then state.x = state.x + 1
- elseif state.facing == 2 then state.z = state.z + 1
- elseif state.facing == 3 then state.x = state.x - 1
- end
- return true
- end
- return false
- end
- local function back()
- if turtle.back() then
- if state.facing == 0 then state.z = state.z + 1
- elseif state.facing == 1 then state.x = state.x - 1
- elseif state.facing == 2 then state.z = state.z - 1
- elseif state.facing == 3 then state.x = state.x + 1
- end
- return true
- end
- return false
- end
- local function up()
- if turtle.up() then
- state.y = state.y + 1
- return true
- end
- return false
- end
- local function down()
- if turtle.down() then
- state.y = state.y - 1
- return true
- end
- return false
- end
- local function turnLeft()
- turtle.turnLeft()
- state.facing = (state.facing - 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- state.facing = (state.facing + 1) % 4
- end
- local function turnAround()
- turnRight()
- turnRight()
- end
- -- Safe digging functions
- local function digSafe()
- while turtle.detect() do
- if not turtle.dig() then
- return false -- Hit bedrock or unbreakable block
- end
- sleep(0.5) -- Handle falling blocks
- end
- return true
- end
- local function digUpSafe()
- while turtle.detectUp() do
- if not turtle.digUp() then
- return false
- end
- sleep(0.5)
- end
- return true
- end
- local function digDownSafe()
- while turtle.detectDown() do
- if not turtle.digDown() then
- return false -- Hit bedrock
- end
- sleep(0.5)
- end
- return true
- end
- -- Movement with digging
- local function forwardDig()
- digSafe()
- local attempts = 0
- while not forward() and attempts < 10 do
- digSafe()
- turtle.attack() -- Clear mobs
- attempts = attempts + 1
- sleep(0.2)
- end
- return attempts < 10
- end
- local function upDig()
- digUpSafe()
- local attempts = 0
- while not up() and attempts < 10 do
- digUpSafe()
- turtle.attackUp()
- attempts = attempts + 1
- sleep(0.2)
- end
- return attempts < 10
- end
- local function downDig()
- if not digDownSafe() then
- return false -- Hit bedrock
- end
- local attempts = 0
- while not down() and attempts < 10 do
- if not digDownSafe() then
- return false -- Hit bedrock
- end
- turtle.attackDown()
- attempts = attempts + 1
- sleep(0.2)
- end
- return attempts < 10
- end
- -- Inventory management
- local function findItem(name)
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == name then
- return i
- end
- end
- return nil
- end
- local function countEmptySlots()
- local empty = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- empty = empty + 1
- end
- end
- return empty
- end
- local function depositItems()
- print("Depositing items...")
- for i = 1, 16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- -- Keep fuel and torches
- if item and
- item.name ~= "minecraft:coal" and
- item.name ~= "minecraft:charcoal" and
- item.name ~= "minecraft:lava_bucket" and
- item.name ~= "minecraft:torch" then
- turtle.dropUp()
- end
- end
- turtle.select(1)
- end
- -- Fuel management
- local function getFuelLevel()
- local level = turtle.getFuelLevel()
- if level == "unlimited" then
- return 999999
- end
- return level
- end
- local function refuel()
- print("Checking fuel...")
- local fuelLevel = getFuelLevel()
- if fuelLevel < config.minFuelLevel then
- print("Fuel low. Refueling...")
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then -- Check if item is fuel
- local needed = config.minFuelLevel * 2 - getFuelLevel()
- turtle.refuel(math.ceil(needed / 80)) -- Coal gives 80 fuel
- if getFuelLevel() >= config.minFuelLevel then
- break
- end
- end
- end
- -- Check for lava buckets
- local lavaBucket = findItem("minecraft:lava_bucket")
- if lavaBucket and getFuelLevel() < config.minFuelLevel then
- turtle.select(lavaBucket)
- turtle.refuel(1)
- end
- end
- turtle.select(1)
- return getFuelLevel() >= config.minFuelLevel
- end
- -- Torch placement
- local function placeTorch()
- local torchSlot = findItem("minecraft:torch")
- if torchSlot then
- local currentSlot = turtle.getSelectedSlot()
- turtle.select(torchSlot)
- -- Try to place on wall
- turnLeft()
- if not turtle.place() then
- turnRight()
- turnRight()
- turtle.place()
- turnLeft()
- else
- turnRight()
- end
- turtle.select(currentSlot)
- end
- end
- -- Navigation functions
- local function goToPosition(targetX, targetY, targetZ, targetFacing)
- print(string.format("Going to position: X:%d Y:%d Z:%d", targetX, targetY, targetZ))
- -- Move to Y level first (go up if needed)
- while state.y < targetY do
- upDig()
- end
- -- Move X
- while state.x < targetX do
- while state.facing ~= 1 do turnRight() end
- forwardDig()
- end
- while state.x > targetX do
- while state.facing ~= 3 do turnRight() end
- forwardDig()
- end
- -- Move Z
- while state.z < targetZ do
- while state.facing ~= 2 do turnRight() end
- forwardDig()
- end
- while state.z > targetZ do
- while state.facing ~= 0 do turnRight() end
- forwardDig()
- end
- -- Move to Y level (go down if needed)
- while state.y > targetY do
- downDig()
- end
- -- Face correct direction
- if targetFacing then
- while state.facing ~= targetFacing do
- turnRight()
- end
- end
- end
- local function returnToStart()
- print("Returning to start position...")
- local savedState = {
- x = state.x,
- y = state.y,
- z = state.z,
- facing = state.facing
- }
- goToPosition(state.startX, state.startY, state.startZ, 0)
- return savedState
- end
- local function returnToMining(savedPosition)
- print("Returning to mining position...")
- goToPosition(savedPosition.x, savedPosition.y, savedPosition.z, savedPosition.facing)
- end
- -- Quarry patterns
- local function mineLayer()
- -- Always start from the same corner with same orientation
- local layerStartX = state.startX + 1 -- One block forward from start
- local layerStartZ = state.startZ
- local layerY = state.y
- -- Ensure we're at the correct starting position and facing
- goToPosition(layerStartX, layerY, layerStartZ, 0)
- for row = 1, config.quarryLength do
- -- Mine one row
- for block = 1, config.quarryWidth - 1 do
- forwardDig()
- state.blockInRow = block
- saveState()
- end
- -- Move to next row if not last
- if row < config.quarryLength then
- if row % 2 == 1 then
- -- Odd row, turn right
- turnRight()
- forwardDig()
- turnRight()
- else
- -- Even row, turn left
- turnLeft()
- forwardDig()
- turnLeft()
- end
- end
- state.rowInLayer = row
- state.blockInRow = 0
- saveState()
- end
- -- Always return to the starting corner of this layer facing north
- goToPosition(layerStartX, layerY, layerStartZ, 0)
- end
- -- Main quarry function
- local function quarry()
- print("Starting quarry operation...")
- print(string.format("Size: %dx%d", config.quarryWidth, config.quarryLength))
- print("Mining down to bedrock...")
- while state.quarrying do
- -- Check fuel before each layer
- if getFuelLevel() < config.minFuelLevel then
- print("Low fuel! Current level: " .. getFuelLevel())
- state.returningForFuel = true
- saveState()
- local miningPos = returnToStart()
- -- Try to refuel from inventory
- if not refuel() then
- print("Cannot refuel from inventory!")
- print("Please add fuel to the turtle")
- print("Fuel needed: " .. config.minFuelLevel)
- print("Current fuel: " .. getFuelLevel())
- print("Press any key after adding fuel...")
- os.pullEvent("key")
- refuel()
- end
- -- Deposit items while we're here
- depositItems()
- -- Return to mining
- returnToMining(miningPos)
- state.returningForFuel = false
- saveState()
- end
- -- Mine current layer
- print("Mining layer " .. state.layer)
- mineLayer()
- -- Place torch if needed
- if state.layer % config.torchInterval == 0 then
- placeTorch()
- end
- -- Check inventory every few layers
- if state.layer % config.chestCheckInterval == 0 and countEmptySlots() <= 2 then
- print("Inventory nearly full, returning to deposit...")
- local miningPos = returnToStart()
- depositItems()
- returnToMining(miningPos)
- end
- -- Try to go down
- if not downDig() then
- print("Hit bedrock at layer " .. state.layer)
- state.quarrying = false
- else
- state.layer = state.layer + 1
- state.rowInLayer = 0
- state.blockInRow = 0
- saveState()
- end
- end
- -- Return to start and deposit final items
- returnToStart()
- depositItems()
- print("Quarry complete!")
- print("Mined " .. state.layer .. " layers")
- deleteState()
- end
- -- Resume function for interrupted mining
- local function resume()
- print("Resuming quarry operation...")
- print(string.format("Current position: X:%d Y:%d Z:%d", state.x, state.y, state.z))
- print("Current layer: " .. state.layer)
- if state.returningForFuel then
- print("Was returning for fuel...")
- returnToStart()
- refuel()
- depositItems()
- -- Go back to the starting position of the current layer
- local layerStartX = state.startX + 1
- local layerStartZ = state.startZ
- local layerY = state.startY - state.layer
- goToPosition(layerStartX, layerY, layerStartZ, 0)
- state.returningForFuel = false
- end
- quarry()
- end
- -- Setup function
- local function setup()
- print("Advanced Quarry Turtle")
- print("===========================")
- print("Setup Instructions:")
- print("1. Place chest ABOVE turtle")
- print("2. Add fuel (coal/lava buckets)")
- print("3. Add torches (optional)")
- print("4. Turtle will mine 5x5 area")
- print(" in front of it (north)")
- print(" extending to the right (east)")
- print("===========================")
- print("Current fuel: " .. getFuelLevel())
- print("Press any key to start...")
- os.pullEvent("key")
- -- Check if resuming
- if loadState() then
- print("Found saved state!")
- resume()
- else
- -- New quarry
- state.startX = state.x
- state.startY = state.y
- state.startZ = state.z
- -- Check initial fuel
- if not refuel() then
- print("ERROR: Not enough fuel!")
- print("Add at least " .. config.minFuelLevel .. " fuel")
- return
- end
- -- Move forward to start quarry
- print("Moving to quarry start position...")
- forwardDig()
- state.startX = state.x - 1 -- Store original position (one block back)
- state.startY = state.y
- state.startZ = state.z
- downDig() -- Start first layer
- state.layer = 1
- saveState()
- quarry()
- end
- end
- -- Error handling wrapper
- local function main()
- local success, error = pcall(setup)
- if not success then
- print("ERROR: " .. error)
- print("State saved. Run program again to resume.")
- saveState()
- end
- end
- -- Run the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement