Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: tunnel <length>" )
- return
- end
- -- Mine in a quarry pattern until we hit something we can't dig
- local length = tonumber( tArgs[1] )
- if length < 1 then
- print( "Tunnel length must be positive" )
- return
- end
- local depth = 0
- local collected = 0
- local xPos = 0
- local yPos = 0
- local zPos = 0
- local xDir = 0
- local zDir = 1
- local goTo
- local function collect()
- collected = collected + 1
- 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 math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." items." )
- end
- if (bFull) then
- returnSupplies(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()
- end
- end
- end
- collected = 0
- turtle.select(1)
- end
- local function returnSupplies(stop)
- local x,y,z,xd,zd = xPos,yPos,zPos,xDir,zDir
- print( "Returning to surface..." )
- goTo( 0,0,0,0,-1 )
- unload( true )
- print( "Resuming mining..." )
- goTo( x,y,z,xd,zd )
- end
- local function tryDig()
- while turtle.detect() do
- if turtle.dig() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while turtle.detectDown() do
- if turtle.digDown() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.he(1)
- return false
- end
- if not tryRefuel() then
- print( "Add more fuel to continue." )
- while not tryRefuel() do
- os.pullEvent( "turtle_inventory" )
- end
- print( "Resuming Tunnel." )
- end
- end
- local function tryUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryForward()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- function goTo( x, y, z, xd, zd )
- print("found")
- turtle.turnLeft()
- turtle.turnLeft()
- while yPos < y do
- if turtle.up() then
- yPos = yPos + 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- if xPos > x then
- -- while xDir ~= -1 do
- -- print("loop 1")
- -- turtle.turnLeft()
- -- xDir, zDir = -zDir, xDir
- -- 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
- -- print("loop 2")
- -- turtle.turnLeft()
- -- xDir, zDir = -zDir, xDir
- -- 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
- -- print("loop 3")
- -- turtle.turnLeft()
- -- xDir, zDir = -zDir, xDir
- -- 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
- -- print("loop 4")
- -- turtle.turnLeft()
- -- xDir, zDir = -zDir, xDir
- -- 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 yPos > y do
- if turtle.down() then
- yPos = yPos - 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- -- while zDir ~= zd or xDir ~= xd do
- -- print("loop 5")
- -- turtle.turnLeft()
- -- xDir, zDir = -zDir, xDir
- -- end
- print ("goTo is done")
- end
- print( "Tunnelling..." )
- for n=1,length do
- turtle.placeDown()
- tryDigUp()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- tryDig()
- tryUp()
- yPos = yPos + 1
- tryDig()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- tryDig()
- tryDown()
- yPos = yPos - 1
- tryDig()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- if n<length then
- tryDig()
- if not tryForward() then
- print( "Aborting Tunnel." )
- returnSupplies(true)
- goTo(0, 0, 0, 0, 0)
- break
- else
- xPos = xPos + 1
- end
- else
- print( "Tunnel complete." )
- returnSupplies(true)
- goTo(0, 0, 0, 0, 0)
- end
- end
- print( "Tunnel complete." )
- print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement