Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local size = 16
- local depth = 0
- local unloaded = 0
- local collected = 0
- local xPos,zPos = 0,0
- local xDir,zDir = 0,1
- local Dir = {
- Front = 0,
- Back = 1,
- Left = 2,
- Right = 3,
- Up = 4,
- Down = 5
- }
- local goTo
- local refuel
- local function printr( message )
- print(message)
- rednet.broadcast(message)
- end
- local function myVec(depth, dir)
- return {depth=depth, dir=dir}
- end
- local function alignDir(dir, depth, track)
- if (dir == Dir.Left) then
- turtle.turnLeft()
- table.insert(track, myVec(depth, Dir.Left))
- elseif (dir == Dir.Right) then
- turtle.turnRight()
- table.insert(track, myVec(depth, Dir.Right))
- elseif (dir == Dir.Back) then
- turtle.turnRight()
- turtle.turnRight()
- table.insert(track, myVec(depth, Dir.Right))
- table.insert(track, myVec(depth, Dir.Right))
- end
- end
- local function undoAlignDir(depth, track)
- while (#track > 0 and track[#track].depth == depth) do
- local vec = table.remove(track)
- if (vec.dir == Dir.Left) then
- turtle.turnRight()
- elseif (vec.dir == Dir.Right) then
- turtle.turnLeft()
- end
- end
- end
- local function isMine(hasBlock, blockInfo)
- local mine = hasBlock and string.find(blockInfo.name, "ore")
- if mine then
- printr("Found "..blockInfo.name)
- end
- return mine
- end
- local function inspectDirIsMine(dir)
- local hasBlock, blockInfo
- if (dir == Dir.Up) then
- hasBlock, blockInfo = turtle.inspectUp()
- elseif (dir == Dir.Down) then
- hasBlock, blockInfo = turtle.inspectDown()
- else
- hasBlock, blockInfo = turtle.inspect()
- end
- return isMine(hasBlock, blockInfo)
- end
- local function excavateDir(dir, depth, track)
- if (dir == Dir.Up) then
- turtle.digUp()
- turtle.up()
- table.insert(track, myVec(depth, Dir.Up))
- elseif (dir == Dir.Down) then
- turtle.digDown()
- turtle.down()
- table.insert(track, myVec(depth, Dir.Down))
- else
- local counter = 0
- repeat
- turtle.dig()
- counter = counter + 1
- until turtle.forward()
- table.insert(track, myVec(depth, Dir.Front))
- -- if counter > 1 then
- -- printr("WARNING: encountered gravity block during DFS")
- -- end
- end
- end
- local function selectFillBlock()
- for i = 1, 16 do
- local info = turtle.getItemDetail(i)
- if info and info.name == FILL_BLOCK then
- turtle.select(i)
- return
- end
- end
- -- printr("[W] no fill blocks")
- end
- local function backtrack(currentDepth, targetDepth, moveTrack, turnTrack)
- while (currentDepth ~= targetDepth) do
- while (#moveTrack > 0 and moveTrack[#moveTrack].depth == currentDepth - 1) do
- local vec = table.remove(moveTrack)
- if (vec.dir == Dir.Front) then
- turtle.back()
- selectFillBlock()
- turtle.place()
- elseif (vec.dir == Dir.Up) then
- turtle.down()
- selectFillBlock()
- turtle.placeUp()
- elseif (vec.dir == Dir.Down) then
- turtle.up()
- selectFillBlock()
- turtle.placeDown()
- end
- end
- undoAlignDir(currentDepth - 1, turnTrack)
- currentDepth = currentDepth - 1
- end
- end
- local function dfs(initDir)
- local stack = {}
- local turnTrack = {}
- local moveTrack = {}
- local depth = 0
- table.insert(stack, myVec(depth, initDir))
- while (#stack > 0) do
- local vec = table.remove(stack)
- backtrack(depth, vec.depth, moveTrack, turnTrack)
- depth = vec.depth
- alignDir(vec.dir, depth, turnTrack)
- if (inspectDirIsMine(vec.dir)) then
- excavateDir(vec.dir, depth, moveTrack)
- depth = depth + 1
- if isMine(turtle.inspect()) then
- table.insert(stack, myVec(depth, Dir.Front))
- end
- if isMine(turtle.inspectUp()) then
- table.insert(stack, myVec(depth, Dir.Up))
- end
- if isMine(turtle.inspectDown()) then
- table.insert(stack, myVec(depth, Dir.Down))
- end
- turtle.turnRight()
- if isMine(turtle.inspect()) then
- table.insert(stack, myVec(depth, Dir.Right))
- end
- turtle.turnRight()
- if isMine(turtle.inspect()) then
- table.insert(stack, myVec(depth, Dir.Back))
- end
- turtle.turnRight()
- if isMine(turtle.inspect()) then
- table.insert(stack, myVec(depth, Dir.Left))
- end
- turtle.turnRight()
- else
- undoAlignDir(depth, turnTrack)
- end
- end
- backtrack(depth, 0, moveTrack, turnTrack)
- end
- local function triggerDfs(dir)
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- if inspectDirIsMine(dir) then
- dfs(dir)
- end
- goTo( x,y,z,xd,zd )
- end
- local function organize()
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 and turtle.getItemCount(i) < 64 then
- turtle.select(i)
- for j = i+1,16 do
- if turtle.compareTo(j) then
- turtle.select(j)
- turtle.transferTo(i)
- turtle.select(i)
- end
- end
- end
- end
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 then
- for j = 1,i do
- if turtle.getItemCount(j) == 0 then
- turtle.select(i)
- turtle.transferTo(j)
- break
- end
- end
- end
- end
- end
- local function unload( _bKeepOneFuelStack )
- 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
- goTo( 0,0,0,0,-1 )
- local fuelNeeded = 2*(x+y+z) + 1
- if not refuel( fuelNeeded ) then
- unload( false )
- printr( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- os.pullEvent( "turtle_inventory" )
- end
- else
- unload( false )
- end
- 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
- printr( "Mined "..(collected + unloaded).." items." )
- end
- end
- if bFull then
- printr( "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 function checkInv()
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- if turtle.getItemDetail(n).name == "minecraft:cobblestone" then
- turtle.select(n)
- turtle.dropUp(64)
- end
- end
- end
- organize()
- end
- local function tryForwards()
- if not refuel() then
- printr( "Not enough Fuel" )
- returnSupplies()
- end
- while not turtle.forward() do
- if turtle.detect() then
- if turtle.dig() then
- if not collect() then
- checkInv()
- if not collect() then
- returnSupplies()
- end
- end
- else
- return false
- end
- end
- end
- triggerDfs(Dir.Front)
- triggerDfs(Dir.Up)
- triggerDfs(Dir.Down)
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- local function tryDown()
- if not refuel() then
- printr( "Not enough Fuel" )
- returnSupplies()
- end
- while not turtle.down() do
- if turtle.detectDown() then
- if turtle.digDown() then
- if not collect() then
- checkInv()
- if not collect() then
- returnSupplies()
- end
- end
- else
- return false
- end
- end
- end
- depth = depth + 1
- if math.fmod( depth, 10 ) == 0 then
- printr( "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() 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() 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() 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() 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()then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown()then
- collect()
- else
- sleep( 0.5 )
- end
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- if not refuel() then
- printr( "Out of Fuel" )
- return
- end
- rednet.open("left")
- goTo( 0,0,0,0,-1 )
- unload( false )
- goTo( 0,0,0,0,1 )
- printr( "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
- printr( "Returning to surface..." )
- -- Return to where we started
- goTo( 0,0,0,0,-1 )
- unload( false )
- goTo( 0,0,0,0,1 )
- printr( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment