Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs < 1 then
- print( "Usage: diggit <diameter> <0 or 1 for blacklist enabled>" )
- 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 black = false
- if (#tArgs >= 2) then
- black = (tArgs[2] == "1")
- else
- tArgs[2] = "0"
- end
- local uhand = fs.open("update", "w")
- local ustr = "shell.run(\"rm posdata\")\n" .. "shell.run(\"rm diggit\")\n" .. "shell.run(\"pastebin get L5c3MGas diggit\")"
- uhand.write(ustr)
- uhand.close()
- local shand = fs.open("startup", "w")
- shand.write("shell.run(\"diggit " .. tArgs[1] .. " " .. tArgs[2] .. "\")")
- shand.close()
- local posinfo = {
- ["xPos"] = 0,
- ["zPos"] = 0,
- ["xDir"] = 0,
- ["zDir"] = 1,
- ["depth"] = 0,
- ["done"] = false,
- ["maxdepth"] = 0,
- ["dxPos"] = 0,
- ["dzPos"] = 0,
- ["dxDir"] = 0,
- ["dzDir"] = 1,
- ["ddepth"] = 0
- }
- --local depth = 0
- local unloaded = 0
- local collected = 0
- --local xPos,zPos = 0,0
- --local xDir,zDir = 0,1
- local hRead
- local hWrite
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- local function printPos()
- print(posinfo.xPos .. "," .. posinfo.depth .. "," .. posinfo.zPos .. " : " .. posinfo.xDir .. "," .. posinfo.zDir)
- end
- local function wasDestroyed()
- count = 0
- for i = 1,16 do
- count = count + turtle.getItemCount(i)
- end
- return (count == 0)
- end
- local function savePos()
- hWrite = fs.open("posdata", "w")
- hWrite.write(textutils.serialize(posinfo))
- hWrite.close()
- --printPos()
- end
- local function loadPos()
- hRead = fs.open("posdata", "r")
- posinfo = textutils.unserialize(hRead.readAll())
- hRead.close()
- end
- local function turnLeft()
- xd,zd = posinfo.xDir, posinfo.zDir
- posinfo.dxDir = -zd
- posinfo.dzDir = xd
- savePos()
- turtle.turnLeft()
- posinfo.xDir = -zd
- posinfo.zDir = xd
- savePos()
- end
- local function turnRight()
- xd,zd = posinfo.xDir, posinfo.zDir
- posinfo.dxDir = zd
- posinfo.dzDir = -xd
- savePos()
- turtle.turnRight()
- posinfo.xDir = zd
- posinfo.zDir = -xd
- savePos()
- end
- local function trackForward()
- if turtle.forward() then
- posinfo.xPos = posinfo.xPos + posinfo.xDir
- posinfo.zPos = posinfo.zPos + posinfo.zDir
- savePos()
- return true
- else
- return false
- end
- end
- local function trackDown()
- if turtle.down() then
- posinfo.depth = posinfo.depth + 1
- savePos()
- if (posinfo.maxdepth < posinfo.depth) then
- posinfo.maxdepth = posinfo.depth
- savePos()
- end
- return true
- else
- return false
- end
- end
- local function trackUp()
- if turtle.up() then
- posinfo.depth = posinfo.depth - 1
- savePos()
- return true
- else
- return false
- end
- end
- 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 lookingAtChest()
- s,d = turtle.inspect()
- if (s) then
- return string.find(d.name, "chest")
- end
- return false
- end
- local function chestUp()
- s,d = turtle.inspectUp()
- if (s) then
- return string.find(d.name, "chest")
- end
- return false
- end
- local function chestDown()
- s,d = turtle.inspectDown()
- if (s) then
- return string.find(d.name, "chest")
- end
- return false
- end
- local function panic()
- local x,y,z,xd,zd = posinfo.xPos,posinfo.depth,posinfo.zPos,posinfo.xDir,posinfo.zDir
- print( "Panic to surface..." )
- if goCareTo( 0,0,0,0,-1) then
- else
- return false
- end
- if lookingAtChest() then
- print("Alignment seems correct...")
- else
- return false
- end
- 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
- goTo( 0,0,0,0,1)
- print( "I think I'm home..." )
- return true
- end
- local function returnSupplies()
- local x,y,z,xd,zd = posinfo.xPos,posinfo.depth,posinfo.zPos,posinfo.xDir,posinfo.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 (posinfo.xPos + posinfo.zPos + posinfo.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 dup
- local ddown
- local function isBlacklist(name)
- if not black then
- return false
- end
- if (name == "minecraft:stone") then
- return true
- end
- if (name == "minecraft:netherrack") then
- return true
- end
- if (name == "minecraft:dirt") 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
- print ("Ooo piece of candy: " .. name)
- return false
- end
- local function tryForwards()
- if not refuel() then
- print( "Not enough Fuel" )
- returnSupplies()
- end
- while not trackForward() 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
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- end
- end
- end
- if idown then
- if not (isBlacklist(ddown.name)) then
- 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 trackDown() 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 careForward()
- if trackForward() then
- elseif lookingAtChest() then
- print("I think I found my chest. I almost tried to break it!")
- turnLeft()
- turnLeft()
- return false
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- return true
- end
- function goCareTo( x, y, z, xd, zd )
- while posinfo.depth > y do
- if trackUp() then
- elseif chestUp() then
- print("I think I found my chest.")
- return false
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- if posinfo.xPos > x then
- while posinfo.xDir ~= -1 do
- turnLeft()
- end
- while posinfo.xPos > x do
- if careForward() then
- sleep( 0.5 )
- else
- return false
- end
- end
- elseif posinfo.xPos < x then
- while posinfo.xDir ~= 1 do
- turnLeft()
- end
- while posinfo.xPos < x do
- if careForward() then
- sleep( 0.5 )
- else
- return false
- end
- end
- end
- if posinfo.zPos > z then
- while posinfo.zDir ~= -1 do
- turnLeft()
- end
- while posinfo.zPos > z do
- if careForward() then
- sleep( 0.5 )
- else
- return false
- end
- end
- elseif posinfo.zPos < z then
- while posinfo.zDir ~= 1 do
- turnLeft()
- end
- while posinfo.zPos < z do
- if careForward() then
- sleep( 0.5 )
- else
- return false
- end
- end
- end
- while posinfo.depth < y do
- if trackDown() then
- --depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- while posinfo.zDir ~= zd or posinfo.xDir ~= xd do
- turnLeft()
- end
- return true
- end
- function goTo( x, y, z, xd, zd )
- while posinfo.depth > y do
- if trackUp() then
- --depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- if posinfo.xPos > x then
- while posinfo.xDir ~= -1 do
- turnLeft()
- end
- while posinfo.xPos > x do
- if trackForward() then
- --xPos = xPos - 1
- --elseif turtle.suck() then
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- elseif posinfo.xPos < x then
- while posinfo.xDir ~= 1 do
- turnLeft()
- end
- while posinfo.xPos < x do
- if trackForward() then
- --xPos = xPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- if posinfo.zPos > z then
- while posinfo.zDir ~= -1 do
- turnLeft()
- end
- while posinfo.zPos > z do
- if trackForward() then
- --zPos = zPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- elseif posinfo.zPos < z then
- while posinfo.zDir ~= 1 do
- turnLeft()
- end
- while posinfo.zPos < z do
- if trackForward() then
- --zPos = zPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- while posinfo.depth < y do
- if trackDown() then
- --depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- while posinfo.zDir ~= zd or posinfo.xDir ~= xd do
- turnLeft()
- end
- end
- if not refuel() then
- print( "Out of Fuel" )
- return
- end
- print("Booting up...")
- if (fs.exists("posdata")) then
- loadPos()
- if (posinfo.done == true) then
- print("Turtle is done.")
- return
- else
- print("Turtle was interrupted...")
- if wasDestroyed() then
- print("I think I was picked up.")
- return
- end
- if panic() then
- goTo(0,posinfo.maxdepth,0,0,1)
- else
- bsuc,bdat = turtle.inspectUp()
- if (string.find(bdat.name, "cobble")) then
- if (lookingAtChest()) then
- turnLeft()
- turnLeft()
- print("Aligned!")
- else
- turnLeft()
- turnLeft()
- if (lookingAtChest()) then
- turnLeft()
- turnLeft()
- print("Aligned!")
- else
- turnLeft()
- turnLeft()
- print("I'm close as home as possible, couldn't find chest.")
- return
- end
- end
- else
- print("I'm close as home as possible, alignment failed.")
- return
- end
- end
- end
- else
- print("Turtle is ready to work...")
- savePos()
- print("Finding Excavation Floor...")
- while not turtle.detectDown() do
- tryDown()
- end
- 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
- tryDown()
- tryDown()
- if turtle.detectDown() then
- if turtle.digDown() then
- if not collect() then
- returnSupplies()
- end
- end
- 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 )
- posinfo.done = true
- savePos()
- print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement