Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not turtle then
- printError("Requires a Turtle")
- return
- end
- local tArgs = { ... }
- if #tArgs < 1 or #tArgs > 4 then
- local programName = arg[0] or fs.getName(shell.getRunningProgram())
- print("Usage: " .. programName .. " <diameter> [startDepth] [maxDepth] [startForward]")
- return
- end
- -- Mine in a quarry pattern until we hit something we can't dig
- local size = tonumber(tArgs[1])
- if size < 1 then
- print("Excavate diameter must be positive")
- return
- end
- -- Mine straight down this far first
- local startDepth = 0
- if #tArgs > 1 then
- startDepth = tonumber(tArgs[2])
- end
- -- Max distance to mine down before stopping, even if no bedrock reached
- local maxDepth = 999
- if #tArgs > 2 then
- maxDepth = tonumber(tArgs[3])
- end
- -- Extra distance to travel forwards before starting mining
- local wanderDist = 0
- if #tArgs > 3 then
- wanderDist = tonumber(tArgs[4])
- end
- local depth = 0
- local unloaded = 0
- local collected = 0
- local triedTerminate = false
- local xPos, zPos = 0, 0
- local xDir, zDir = 0, 1
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- local _await, _awaitRefuel, _exit, _tryTerm -- Filled in further down
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- local function unload(_bKeepOneFuelStack)
- print("Unloading items...")
- --Never hold fuel stack if fuel is infinite
- if turtle.getFuelLevel() == "unlimited" then
- _bKeepOneFuelStack = false
- end
- for n = 1, 16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- local bDrop = true
- if _bKeepOneFuelStack and turtle.refuel(0) then
- bDrop = false
- _bKeepOneFuelStack = false
- end
- if bDrop then
- --First drop into any container above
- if turtle.detectUp() then
- turtle.dropUp()
- end
- if turtle.getItemCount(n) > 0 then
- --Then to each side, if there's any left
- for _ = 1,4 do
- if turtle.detect() and turtle.getItemCount(n) > 0 then
- turtle.drop()
- end
- turnLeft()
- end
- end
- --finally, if anything remains, just drop it on the ground
- if turtle.getItemCount(n) > 0 then
- turtle.drop()
- end
- unloaded = unloaded + nCount
- end
- end
- end
- collected = 0
- turtle.select(1)
- end
- local function returnSupplies()
- local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
- print("Returning to surface...")
- goTo(0, 0, 0, 0, -1)
- local fuelNeeded = 3 * (x + y + z) + 1
- if not refuel(fuelNeeded) then
- unload(true)
- print("Waiting for fuel")
- redstone.setOutput("top", true)
- _awaitRefuel()
- redstone.setOutput("top", false)
- unload(true) --prevent >1 stack of fuel from being stored
- else
- unload(true)
- end
- if triedTerminate then
- print("Paused at surface. Press 'c' to continue, terminate to exit.")
- repeat
- local evt, c = os.pullEvent("char")
- until evt == "char" and c == "c"
- triedTerminate = false
- 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 countSlots()
- local slotcount = 0
- for n = 1, 16 do
- if turtle.getItemCount(n) == 0 then
- slotcount = slotcount + 1
- end
- end
- return slotcount
- end
- function refuel(ammount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = ammount or xPos + zPos + depth + 64
- if turtle.getFuelLevel() < needed then
- for n = 1, 16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end
- local function fuelCheck()
- if turtle.getFuelLevel() < 16 then
- refuel(16)
- end
- _await(0)
- 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
- _await()
- end
- fuelCheck()
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- local function tryDown()
- if not refuel() then
- print("Not enough Fuel")
- returnSupplies()
- end
- if depth + 1 > maxDepth then
- return false
- 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
- _await()
- end
- fuelCheck()
- end
- depth = depth + 1
- if math.fmod(depth, 10) == 0 then
- print("Descended " .. depth .. " metres.")
- end
- return true
- end
- local function goY(y)
- while depth > y do
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- _await()
- end
- fuelCheck()
- end
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- _await()
- end
- fuelCheck()
- end
- end
- local function goX(x)
- 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
- _await()
- end
- fuelCheck()
- 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
- _await()
- end
- fuelCheck()
- end
- end
- end
- local function goZ(z)
- 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
- _await()
- end
- fuelCheck()
- 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
- _await()
- end
- fuelCheck()
- end
- end
- end
- local function goXZ(x,z)
- goZ(z)
- goX(x)
- end
- function goTo(x, y, z, xd, zd)
- if startDepth >= y and depth > startDepth then
- goY(startDepth)
- goXZ(0,0)
- goY(y)
- goXZ(x,z)
- elseif startDepth <= y and depth < startDepth then
- goXZ(0, 0)
- goY(startDepth)
- goXZ(x,z)
- goY(y)
- else
- goXZ(x,z)
- goY(y)
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- -- End of basic functions
- -- Exit handler stuff
- function _tryTerm()
- if triedTerminate then
- _exit()
- else
- triedTerminate = true
- print("Terminate recieved; attempting to return to surface")
- returnSupplies()
- end
- end
- local oldPullEvent = os.pullEvent
- os.pullEvent = function(_filter)
- eventData = {os.pullEventRaw(_filter)}
- if eventData[1] == "terminate" then
- _tryTerm()
- end
- return unpack(eventData)
- end
- function _await(s)
- os.startTimer(s or 0.5)
- os.pullEvent("timer")
- end
- function _awaitRefuel()
- local t = triedTerminate
- triedTerminate = true
- repeat
- os.pullEvent("turtle_inventory")
- until refuel(fuelNeeded)
- triedTerminate = t
- end
- function _exit()
- print("Exiting program. Goodbye!")
- os.pullEvent = oldPullEvent
- error()
- end
- if not refuel(256) then
- print("Waiting for fuel...")
- redstone.setOutput("top", true)
- while not refuel(256) do
- os.pullEvent("turtle_inventory")
- end
- redstone.setOutput("top", false)
- end
- if countSlots() < 15 then
- print("Depositing down to 1 stack of fuel...")
- unload(true)
- end
- print("Excavating...")
- turtle.select(1)
- if startDepth > 0 then
- goTo(0, startDepth, 0, 0, 1)
- end
- if wanderDist ~= 0 then
- goTo(0, startDepth, wanderDist, 0, 1)
- end
- local alternate = 0
- local done = false
- while not done do
- for n = 1, size do
- for _ = 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...")
- -- Return to where we started
- goTo(0, 0, 0, 0, -1)
- unload(false)
- goTo(0, 0, 0, 0, 1)
- print("Mined " .. collected + unloaded .. " items total - Excavate complete.")
Add Comment
Please, Sign In to add comment