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 size < 1 then
- print("Excavate diameter must be positive")
- return
- end
- local depth = 0
- local unloaded = 0
- local collected = 0
- local xPos, zPos = 0, 0
- local xDir, zDir = 0, 1
- local goTo
- local function unload()
- print("Unloading items to ender chest...")
- -- Refuel from all fuel sources first
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel(64)
- end
- end
- end
- -- Clear the block above
- while turtle.detectUp() do
- if not turtle.digUp() then
- print("Unable to clear space above to place ender chest.")
- return
- end
- end
- -- Locate ender chest
- local chestSlot = nil
- for i = 1, 16 do
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == "enderstorage:ender_chest" then
- chestSlot = i
- break
- end
- end
- if not chestSlot then
- print("No ender chest found in inventory!")
- return
- end
- turtle.select(chestSlot)
- if not turtle.placeUp() then
- print("Failed to place ender chest above.")
- return
- end
- -- Drop all items except the chest itself
- for i = 1, 16 do
- if i ~= chestSlot then
- local count = turtle.getItemCount(i)
- if count > 0 then
- turtle.select(i)
- if turtle.dropUp() then
- unloaded = unloaded + count
- end
- end
- end
- end
- -- Pick up chest
- turtle.select(chestSlot)
- if not turtle.digUp() then
- print("Warning: Failed to retrieve ender chest.")
- end
- turtle.select(1)
- collected = 0
- end
- function refuel(ammount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = ammount or (xPos + zPos + depth + 2)
- if turtle.getFuelLevel() >= needed then
- return true
- end
- local refueled = false
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(0) then
- if turtle.refuel(64) then
- refueled = true
- end
- end
- end
- end
- turtle.select(1)
- return turtle.getFuelLevel() >= needed or refueled
- end
- local function returnSupplies()
- local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
- print("Handling inventory and fuel...")
- unload()
- local fuelNeeded = 2 * (x + y + z) + 1
- if not refuel(fuelNeeded) then
- print("Waiting for fuel...")
- while not refuel(fuelNeeded) do
- os.pullEvent("turtle_inventory")
- end
- end
- print("Resuming mining...")
- goTo(x, y, z, xd, zd)
- end
- local function collect()
- local bFull = true
- local nTotalItems = 0
- for n = 1, 16 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- bFull = false
- end
- nTotalItems = nTotalItems + nCount
- end
- if nTotalItems > collected then
- collected = nTotalItems
- if math.fmod(collected + unloaded, 50) == 0 then
- print("Mined " .. (collected + unloaded) .. " items.")
- end
- end
- if bFull then
- print("No empty slots left.")
- return false
- end
- return true
- end
- local function tryForwards()
- if not refuel() then
- print("Not enough Fuel")
- returnSupplies()
- end
- while not turtle.forward() do
- if turtle.detect() then
- if turtle.dig() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attack() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep(0.5)
- end
- end
- -- Dig up and down
- if turtle.detectUp() then
- if turtle.digUp() then
- collect()
- end
- end
- if turtle.detectDown() then
- if turtle.digDown() then
- collect()
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- local function tryDown()
- for i = 1, 3 do
- if not refuel() then
- print("Not enough Fuel")
- returnSupplies()
- end
- while not turtle.down() do
- if turtle.detectDown() then
- if turtle.digDown() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attackDown() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep(0.5)
- end
- end
- depth = depth + 1
- -- Dig above and below
- if turtle.detectUp() then
- if turtle.digUp() then
- collect()
- end
- end
- if turtle.detectDown() then
- if turtle.digDown() then
- collect()
- end
- end
- end
- if math.fmod(depth, 10) == 0 then
- print("Descended " .. depth .. " metres.")
- end
- return true
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- 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
- -- Initial fuel check
- if not refuel() then
- print("Out of Fuel")
- 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...")
- goTo(0, 0, 0, 0, -1)
- unload()
- goTo(0, 0, 0, 0, 1)
- print("Mined " .. (collected + unloaded) .. " items total.")
Add Comment
Please, Sign In to add comment