Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Excavate+ by Yggdresil
- -- http://turtlescripts.com/project/gjdi1p-Excavate-
- local tArgs = { ... }
- local excludeItems = {}
- if #tArgs < 1 then
- print("Usage: excavate <diameter> [<excluded items>]")
- return
- elseif #tArgs > 1 then
- if tArgs[2] == "None" then
- excludeItems = nil
- else
- for i = 2, #tArgs do
- excludeItems[tArgs[i]] = true
- end
- end
- else
- print('Using default exclude list (Cobblestone, Dirt, Granite, Gravel)...')
- excludeItems = {
- ["minecraft:cobblestone"] = true,
- ["minecraft:dirt"] = true,
- ["chisel:granite"] = true,
- ["minecraft:gravel"] = true
- }
- 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
- local depth = 0
- local unloaded = 0
- local collected = 0
- local xPos, zPos = 0, 0
- local xDir, zDir = 0, 1
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- -- Empty inventory and update count of total items
- local function unload(_bKeepOneFuelStack)
- print("Unloading items...")
- for n = 1, 16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- if _bKeepOneFuelStack and turtle.refuel(0) then
- _bKeepOneFuelStack = false
- else
- turtle.drop()
- unloaded = unloaded + nCount
- end
- end
- end
- collected = 0
- turtle.select(1)
- end
- -- Return to surface, call unload and refuel if necessary before returning to previous position
- 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 = 2 * (x + y + z + 1)
- if not refuel(fuelNeeded) then
- unload(true)
- print("Waiting for fuel")
- while not refuel(fuelNeeded) do
- os.pullEvent("turtle_inventory")
- end
- else
- unload(true)
- end
- print("Resuming mining...")
- goTo(x, y, z, xd, zd)
- end
- -- Check if inventory full, update count of collected items
- 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
- elseif excludeItems[turtle.getItemDetail(n).name] then
- local nPreviousSlot = turtle.getSelectedSlot()
- turtle.select(n)
- turtle.drop()
- turtle.select(nPreviousSlot)
- bFull = false
- nCount = 0
- 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
- -- Check fuel level, refill if necessary and possible
- function refuel(amount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = amount or (xPos + zPos + depth + 2) -- +2 = move 1 and back
- if fuelLevel < needed then
- for n = 1, 16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount() > 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
- -- Attempt to dig/attack in front of the turtle
- local function dig()
- 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
- return true
- end
- -- Attempt to dig/attack below the turtle
- local function digDown()
- 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
- return true
- end
- -- Attempt to dig/attack above the turtle
- local function digUp()
- if turtle.detectUp() then
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attackUp() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep(0.5)
- end
- return true
- end
- -- Try moving the turtle forwards, digging above and below the turtle prior to moving
- local function tryForwards()
- if not refuel() then
- print("Not enough Fuel")
- returnSupplies()
- end
- while not turtle.forward() do
- if not dig() then
- return false
- end
- end
- if depth ~= 0 then
- digUp()
- end
- digDown()
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- -- Self explanatory
- local function tryDown()
- if not refuel() then
- print("Not enough Fuel")
- returnSupplies()
- end
- while not turtle.down() do
- if not digDown() then
- return false
- end
- end
- depth = depth + 1
- if math.fmod(depth, 10) == 0 then
- print("Descended " .. depth .. " metres.")
- end
- return true
- end
- -- Self explanatory
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- -- Self explanatory
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- -- Go to coordinates x,y,z facing the direction indicated by xd or xz
- -- Coordinates are in a Cartesian coordinate system where the turtle's starting point is the origin
- function goTo(x, y, z, xd, zd)
- while depth > y do
- if turtle.up() then
- depth = depth - 1
- else
- digUp()
- end
- end
- if xPos > x then
- while xDir ~= -1 do
- if zDir == -1 then
- turnRight()
- else
- turnLeft()
- end
- end
- while xPos > x do
- tryForwards()
- end
- elseif xPos < x then
- while xDir ~= 1 do
- if zDir == 1 then
- turnRight()
- else
- turnLeft()
- end
- end
- while xPos < x do
- tryForwards()
- end
- end
- if zPos > z then
- while zDir ~= -1 do
- turnLeft()
- end
- while zPos > z do
- tryForwards()
- end
- elseif zPos < z then
- while zDir ~= 1 do
- turnLeft()
- end
- while zPos < z do
- tryForwards()
- end
- end
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- else
- digDown()
- end
- end
- -- TODO: Optimize to avoid useless turns
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- if not refuel() then
- print("Out of Fuel")
- while not refuel() do
- os.pullEvent("turtle_inventory")
- end
- end
- print("Excavating...")
- local reseal = false
- turtle.select(1)
- if turtle.digDown() then
- reseal = true
- end
- local alternate = 0
- local done = false
- -- Blocks above the turtle will also be dug out so begin one block lower
- if not tryDown() then
- done = true
- end
- -- Additional blocks are dug when calling tryForwards so for this instance, dig the bottom block manually
- digDown()
- 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
- for n = 1, 3 do
- if not tryDown() then
- done = true
- break
- end
- end
- -- Additional blocks are dug when calling tryForwards so for this instance, dig the bottom block manually
- digDown()
- end
- print("Returning to surface...")
- -- Return to where we started
- goTo(0, 0, 0, 0, -1)
- unload(false)
- goTo(0, 0, 0, 0, 1)
- -- Seal the hole
- if reseal then
- turtle.placeDown()
- end
- print("Mined " .. (collected + unloaded) .. " items total.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement