Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- -- Config for refueling itself
- local refuelSelf = false
- -- Various checks and balances. You can skip this part.
- -- check usage of arguments
- if #tArgs < 1 or #tArgs > 2 then
- print( "Usage: "..shell.getRunningProgram().." <length> [height]" )
- return
- end
- -- check if height argument was specified
- -- if not, default 3x3 tunnel will be dug out
- if not tArgs[2] then
- height = 3
- else
- height = tonumber(tArgs[2])
- end
- -- why would you run this on anything other than a turtle?
- if not turtle then
- print( "You must run this on a turtle." )
- return
- end
- -- check if length is positive
- local length = tonumber( tArgs[1] )
- if length < 1 then
- print( "Tunnel length must be positive" )
- return
- end
- -- Various variables
- local depth = 0
- local collected = 0
- local facing = 0
- local currHeight = 0
- local oldRight = turtle.turnRight()
- local oldLeft = turtle.turnLeft()
- local oldDig = turtle.dig()
- local oldDigUp = turtle.digUp()
- local oldDigDown = turtle.digDown()
- -- Redefining some of the turtle functions
- local function turtle.turnRight()
- facing = facing + 1
- if facing == 4 then
- facing = 0
- end
- return oldRight()
- end
- local function turtle.turnLeft()
- facing = facing - 1
- if facing == -4 then
- facing = 0
- end
- return oldLeft()
- end
- -- Refuel function
- 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 refuelSelf then
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- print( "Add more fuel to continue." )
- while not tryRefuel() do
- sleep(1)
- end
- print( "Resuming Tunnel." )
- end
- end
- -- Return to base if full
- local function rtb()
- local returnHeight = currHeight
- local returnFacing = facing
- while currHeight ~= 0 do
- turtle.down()
- currHeight = currHeight - 1
- end
- if facing < 0 then
- while facing ~= 0 do
- turtle.turnRight()
- facing = facing + 1
- end
- elseif facing > 0 then
- while facing ~= 0 do
- turtle.turnLeft()
- facing = facing - 1
- end
- end
- turtle.turnRight()
- turtle.turnRight()
- for i = 1, depth do
- refuel()
- turtle.forward()
- end
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.turnRight()
- turtle.turnRight()
- for i = 1, depth do
- refuel()
- while not turtle.forward()
- turtle.dig()
- end
- end
- for i = 1, returnHeight do
- turtle.up()
- end
- if returnFacing < 0 then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- local function turtle.dig()
- for i = 1, 16 do
- if turtle.compare(i) then
- turtle.select(i)
- return oldDig()
- end
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) < 64 then
- turtle.select(i)
- return oldDig()
- end
- end
- rtb()
- end
- local function turtle.digUp()
- for i = 1, 16 do
- if turtle.compareUp(i) then
- turtle.select(i)
- return oldDigUp()
- end
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) < 64 then
- turtle.select(i)
- return oldDigUp()
- end
- end
- rtb()
- end
- local function turtle.digDown()
- for i = 1, 16 do
- if turtle.compareDown(i) then
- turtle.select(i)
- return oldDigDown()
- end
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) < 64 then
- turtle.select(i)
- return oldDigDown()
- end
- end
- rtb()
- end
- -- Count of items collected
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." items." )
- end
- end
- -- Try digging something
- 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 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
- -- Start tunneling
- print( "Tunnelling..." )
- for n=1,length do
- tryDigUp()
- turtle.turnLeft()
- tryDig()
- for i = 1, height do
- tryUp()
- currHeight = currHeight + 1
- tryDig()
- end
- turtle.turnRight()
- turtle.turnRight()
- tryDig()
- for i = 1, height do
- tryDown()
- currHeight = currHeight - 1
- tryDig()
- end
- turtle.turnLeft()
- if n<length then
- tryDig()
- if not tryForward() then
- print( "Aborting Tunnel." )
- break
- else
- depth = depth + 1
- end
- else
- print( "Tunnel complete." )
- end
- end
- print( "Returning to start..." )
- -- Return to where we started
- turtle.turnLeft()
- turtle.turnLeft()
- while depth > 0 do
- if turtle.forward() then
- depth = depth - 1
- else
- turtle.dig()
- end
- end
- -- Drop items into the chest
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.turnRight()
- turtle.turnRight()
- print( "Tunnel complete." )
- print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment