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
- -- 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
- local function unload( _bKeepOneFuelStack )
- print( "Unloading items..." )
- 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
- turtle.drop()
- 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 = 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
- 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
- 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
- local fueled = false
- 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 iup
- local idown
- local ifront
- local dup
- local ddown
- local dfront
- local function isBlacklist(name)
- if (name == "minecraft:stone") then
- return true
- end
- if (name == "minecraft:water") then
- return true
- end
- if (name == "minecraft:lava") then
- return true
- end
- if (name == "minecraft:flowing_water") then
- return true
- end
- return false
- end
- local function pick()
- ifront, dfront = turtle.inspect()
- if ifront then
- if not (isBlacklist(dfront.name)) then
- if turtle.dig() then
- if not collect() then
- returnSupplies()
- end
- end
- end
- end
- end
- local function hitBedrock()
- idown, ddown = turtle.inspectDown()
- if idown then
- return (ddown.name == "minecraft:bedrock")
- end
- end
- local function tryUp()
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- local function boreUp()
- while depth > 0 do
- pick()
- tryUp()
- end
- 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
- xPos = xPos + xDir
- zPos = zPos + zDir
- iup, dup = turtle.inspectUp()
- idown, ddown = turtle.inspectDown()
- if iup then
- -- if not (isBlacklist(dup.name)) then
- -- print ("Ooo piece of candy.")
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- end
- -- end
- end
- if idown then
- if not (isBlacklist(ddown.name)) then
- print ("Ooo piece of candy.")
- if turtle.digDown() then
- if not collect() then
- returnSupplies()
- end
- end
- end
- end
- return true
- end
- local function tryDown()
- 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
- if math.fmod( depth, 10 ) == 0 then
- print( "Descended "..depth.." metres." )
- end
- return true
- end
- local function boreDown()
- while not hitBedrock() do
- tryDown()
- pick()
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- local function bore()
- boreDown()
- turnLeft()
- boreUp()
- turnLeft()
- boreDown()
- turnLeft()
- boreUp()
- turnLeft()
- 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
- if not refuel() then
- print( "Out of Fuel" )
- return
- end
- -- print("Finding Excavation Floor...")
- -- while not turtle.detectDown() do
- -- tryDown()
- -- end
- print( "Excavating..." )
- local reseal = false
- turtle.select(1)
- if turtle.digDown() then
- reseal = true
- end
- local alternate = 0
- local done = false
- while not done do
- for n=1,size do
- while true do
- if ((xPos+(5*xDir)) > size) or ((xPos+(5*xDir)) < 0) then
- break
- end
- if ((zPos+(5*zDir)) > size) or ((zPos+(5*zDir)) < 0) then
- break
- end
- bore()
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- 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
- bore()
- turnLeft()
- if not tryForwards() then
- done = true
- break
- end
- turnLeft()
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- else
- bore()
- turnRight()
- if not tryForwards() then
- done = true
- break
- end
- turnRight()
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- if not tryForwards() then
- done = true
- break
- end
- end
- end
- end
- done = true
- 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." )
Add Comment
Please, Sign In to add comment