Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print("Usage: excavate <diameter>")
- return
- end
- local size = tonumber(tArgs[1])
- if not size or size < 1 or size % 1 ~= 0 then
- print("Excavate diameter must be a positive whole number")
- return
- end
- local ENDER_CHEST_ID = "enderstorage:ender_chest"
- local STORAGE_RECHECK_SECONDS = 15
- local depth = 0
- local unloaded = 0
- local collected = 0
- local xPos, zPos = 0, 0
- local xDir, zDir = 0, 1
- local storageMode = nil
- local goTo
- local returnSupplies
- local function findEnderChestSlot()
- for i = 1, 16 do
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == ENDER_CHEST_ID then
- return i
- end
- end
- return nil
- end
- local function isInventory(side)
- if peripheral.hasType and peripheral.hasType(side, "inventory") then
- return true
- end
- local wrapped = peripheral.wrap(side)
- return wrapped ~= nil and type(wrapped.list) == "function"
- end
- local function waitForRearStorage()
- while not isInventory("back") do
- print("ERROR: No valid storage found.")
- print("Place a chest, barrel, or other inventory behind the turtle's initial starting location.")
- print("Checking again in " .. STORAGE_RECHECK_SECONDS .. " seconds...")
- sleep(STORAGE_RECHECK_SECONDS)
- end
- end
- local function chooseStorageMode()
- if findEnderChestSlot() then
- storageMode = "ender"
- print("EnderStorage chest found. Using portable unloading.")
- return
- end
- storageMode = "rear"
- waitForRearStorage()
- print("Storage found behind starting location.")
- end
- local function inventoryTotal()
- local total = 0
- for i = 1, 16 do
- total = total + turtle.getItemCount(i)
- end
- return total
- end
- local function hasEmptySlot()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return true
- end
- end
- return false
- end
- local function fuelLevel()
- return turtle.getFuelLevel()
- end
- local function distanceFromStart()
- return math.abs(xPos) + math.abs(zPos) + depth
- end
- local function refuelTo(requiredLevel)
- local level = fuelLevel()
- if level == "unlimited" then
- return true
- end
- if level >= requiredLevel then
- return true
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel(turtle.getItemCount(i))
- level = fuelLevel()
- if level == "unlimited" or level >= requiredLevel then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- level = fuelLevel()
- return level == "unlimited" or level >= requiredLevel
- end
- local function burnFuelToFreeSlot()
- if hasEmptySlot() then
- return true
- end
- for i = 1, 16 do
- local before = turtle.getItemCount(i)
- if before > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel(before)
- local after = turtle.getItemCount(i)
- if after < before then
- print("Inventory full: used " .. (before - after) .. " collected fuel item(s).")
- end
- if after == 0 then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return hasEmptySlot()
- end
- local function collect()
- local total = inventoryTotal()
- if total > collected then
- collected = total
- if math.fmod(collected + unloaded, 50) == 0 then
- print("Mined " .. (collected + unloaded) .. " items.")
- end
- end
- return hasEmptySlot()
- end
- local function waitForFuel(requiredLevel)
- while not refuelTo(requiredLevel) do
- print("Not enough fuel to continue safely.")
- print("Add burnable fuel directly to the turtle. Checking again in 15 seconds...")
- sleep(15)
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- local function unloadToEnderChest()
- local chestSlot = findEnderChestSlot()
- if not chestSlot then
- return false
- end
- print("Unloading items to EnderStorage chest...")
- while turtle.detectUp() do
- if not turtle.digUp() then
- print("Unable to clear space above to place EnderStorage chest.")
- return false
- end
- end
- turtle.select(chestSlot)
- if not turtle.placeUp() then
- print("Failed to place EnderStorage chest above.")
- return false
- end
- for i = 1, 16 do
- if i ~= chestSlot then
- local before = turtle.getItemCount(i)
- if before > 0 then
- turtle.select(i)
- turtle.dropUp()
- local moved = before - turtle.getItemCount(i)
- unloaded = unloaded + moved
- end
- end
- end
- turtle.select(chestSlot)
- if not turtle.digUp() then
- print("Warning: Failed to retrieve EnderStorage chest.")
- end
- turtle.select(1)
- collected = inventoryTotal()
- return true
- end
- local function unloadToRearStorage()
- waitForRearStorage()
- turnLeft()
- turnLeft()
- while true do
- if not isInventory("front") then
- turnLeft()
- turnLeft()
- waitForRearStorage()
- turnLeft()
- turnLeft()
- end
- local remaining = false
- for i = 1, 16 do
- local before = turtle.getItemCount(i)
- if before > 0 then
- turtle.select(i)
- turtle.drop()
- local after = turtle.getItemCount(i)
- local moved = before - after
- unloaded = unloaded + moved
- if after > 0 then
- remaining = true
- end
- end
- end
- if not remaining then
- break
- end
- print("Storage is full or rejected an item.")
- print("Make room in the storage behind the starting location.")
- print("Trying again in " .. STORAGE_RECHECK_SECONDS .. " seconds...")
- sleep(STORAGE_RECHECK_SECONDS)
- end
- turtle.select(1)
- turnLeft()
- turnLeft()
- collected = inventoryTotal()
- end
- local function ensureInventorySpace()
- if hasEmptySlot() then
- return true
- end
- if burnFuelToFreeSlot() then
- return true
- end
- print("Inventory is full and no burnable items can free a slot.")
- returnSupplies()
- return hasEmptySlot()
- end
- local function ensureFuelForMove()
- local required = distanceFromStart() + 2
- if refuelTo(required) then
- return true
- end
- print("Fuel reserve is too low. Returning for supplies...")
- returnSupplies()
- return refuelTo(distanceFromStart() + 2)
- end
- function goTo(x, y, z, xd, zd)
- while depth > y do
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep(0.5)
- end
- end
- if xPos > x then
- while xDir ~= -1 do turnLeft() end
- while xPos > x do
- if turtle.forward() then
- xPos = xPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep(0.5)
- end
- end
- elseif xPos < x then
- while xDir ~= 1 do turnLeft() end
- while xPos < x do
- if turtle.forward() then
- xPos = xPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep(0.5)
- end
- end
- end
- if zPos > z then
- while zDir ~= -1 do turnLeft() end
- while zPos > z do
- if turtle.forward() then
- zPos = zPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep(0.5)
- end
- end
- elseif zPos < z then
- while zDir ~= 1 do turnLeft() end
- while zPos < z do
- if turtle.forward() then
- zPos = zPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep(0.5)
- end
- end
- end
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep(0.5)
- end
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- function returnSupplies()
- local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
- local travelDistance = math.abs(x) + math.abs(z) + y
- print("Handling inventory and fuel...")
- if findEnderChestSlot() then
- storageMode = "ender"
- refuelTo(travelDistance + 2)
- if unloadToEnderChest() then
- waitForFuel(travelDistance + 2)
- print("Resuming mining...")
- return true
- end
- end
- storageMode = "rear"
- if not refuelTo(travelDistance) then
- print("The turtle does not have enough fuel to reach the starting location.")
- waitForFuel(travelDistance)
- end
- print("Returning to starting storage...")
- goTo(0, 0, 0, 0, 1)
- -- Burn collected fuel before depositing. This keeps enough internal fuel
- -- to return to the saved mining position whenever possible.
- refuelTo(travelDistance + 2)
- unloadToRearStorage()
- if not refuelTo(travelDistance + 2) then
- waitForFuel(travelDistance + 2)
- end
- print("Resuming mining...")
- goTo(x, y, z, xd, zd)
- return true
- end
- local function tryForwards()
- if not ensureFuelForMove() then
- return false
- end
- while not turtle.forward() do
- if turtle.detect() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.dig() then
- collect()
- else
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep(0.5)
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- if turtle.detectUp() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.digUp() then
- collect()
- end
- end
- if turtle.detectDown() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.digDown() then
- collect()
- end
- end
- return true
- end
- local function tryDown()
- for i = 1, 3 do
- if not ensureFuelForMove() then
- return false
- end
- while not turtle.down() do
- if turtle.detectDown() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.digDown() then
- collect()
- else
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep(0.5)
- end
- end
- depth = depth + 1
- if turtle.detectUp() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.digUp() then
- collect()
- end
- end
- if turtle.detectDown() then
- if not ensureInventorySpace() then
- return false
- end
- if turtle.digDown() then
- collect()
- end
- end
- end
- if math.fmod(depth, 10) == 0 then
- print("Descended " .. depth .. " metres.")
- end
- return true
- end
- chooseStorageMode()
- if not refuelTo(2) then
- print("Out of fuel. Add burnable fuel to the turtle.")
- return
- end
- print("Excavating...")
- local alternate = 0
- local done = false
- while not done do
- for n = 1, size do
- for m = 1, size - 1 do
- if not tryForwards() then
- done = true
- break
- end
- end
- if done then
- break
- end
- if n < size then
- if math.fmod(n + alternate, 2) == 0 then
- turnLeft()
- if not tryForwards() then
- done = true
- break
- end
- turnLeft()
- else
- turnRight()
- if not tryForwards() then
- done = true
- break
- end
- turnRight()
- end
- end
- end
- if done then
- break
- end
- if size > 1 then
- if math.fmod(size, 2) == 0 then
- turnRight()
- else
- if alternate == 0 then
- turnLeft()
- else
- turnRight()
- end
- alternate = 1 - alternate
- end
- end
- if not tryDown() then
- done = true
- break
- end
- end
- print("Returning to surface...")
- local finalDistance = distanceFromStart()
- if not refuelTo(finalDistance) then
- waitForFuel(finalDistance)
- end
- goTo(0, 0, 0, 0, 1)
- if findEnderChestSlot() then
- unloadToEnderChest()
- else
- storageMode = "rear"
- unloadToRearStorage()
- end
- goTo(0, 0, 0, 0, 1)
- print("Mined " .. (collected + unloaded) .. " items total.")
Advertisement
Add Comment
Please, Sign In to add comment