Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Attempt to refuel the turtle
- local function attemptRefuel()
- if(turtle.getFuelLevel() >= 1) then
- return
- end
- local index = 1
- local hasRefueled = false
- while(index <= 16 and not hasRefueled) do
- turtle.select(index)
- if(turtle.refuel(0)) then
- turtle.refuel(1)
- hasRefueled = true
- end
- index = index + 1
- end
- end
- -- Move in a direction 1 or more times
- local function move(direction, distance)
- local distance = tonumber(distance) or 1
- attemptRefuel()
- if(direction == "up") then
- turtle.up()
- elseif(direction == "down") then
- turtle.down()
- elseif(direction == "forward") then
- turtle.forward()
- elseif(direction == "back") then
- turtle.back()
- else
- print("Direction: " .. direction .. " not a valid movement")
- return false
- end
- if(distance > 1) then
- move(direction, distance - 1)
- end
- return true
- end
- -- Dig out a block and move into it
- local function digMove(direction)
- if(direction == "up") then
- turtle.digUp()
- move("up")
- elseif(direction == "down") then
- turtle.digDown()
- move("down")
- elseif(direction == "forward") then
- turtle.dig()
- move("forward")
- elseif(direction == "back") then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- move("back")
- else
- print("Direction: " .. direction .. " not a valid movement")
- return false
- end
- return true
- end
- -- Dig out a 3x3 vertical square
- local function tunnelSquare()
- -- Dig out adjacent left and right blocks
- local function tunnelAdjacentLR()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- end
- digMove("forward")
- for i=1, 3 do
- tunnelAdjacentLR()
- if(i < 3) then
- digMove("up")
- end
- end
- move("down", 2)
- end
- -- Tunnel
- local function tunnel(distance, tunnelFunc)
- -- Don't bother with tunnelling negative distances
- if(distance < 1) then
- return
- end
- -- Default to tunnelSquare
- local tunnelFunc = tunnelFunc or tunnelSquare
- -- Tunnel over a distance using specified tunneling function
- for i=1, distance do
- tunnelFunc()
- end
- end
- -- Display usage info
- local function help()
- local name = "<name>"
- if(shell) then
- name = shell.getRunningProgram()
- end
- print("Usage: " .. name .. " <distance to tunnel >= 3> [shaft center spacing (-1 for no return)]")
- end
- -- Main
- local function main(distance, spacing)
- local distance = tonumber(distance) or 0
- local spacing = tonumber(spacing) or 0
- -- Handle args
- if(not distance) then
- help()
- return
- elseif(distance < 3) then
- help()
- return
- end
- -- Initial tunneling distance
- tunnel(distance)
- -- Don't return if a negative spacing has been given
- if(spacing < 0) then
- return
- end
- -- Tunnel over to return shaft
- move("back")
- turtle.turnLeft()
- if(spacing > 0) then
- tunnel(spacing + 1)
- move("back")
- end
- -- Move to edge of return shaft
- turtle.turnLeft()
- move("forward")
- -- Tunnel/move back to start
- if(spacing == 0) then
- move("forward", distance - 2)
- else
- tunnel(distance - 2)
- end
- -- Spin back around
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local arg = {...}
- main(arg[1], arg[2])
Advertisement
Add Comment
Please, Sign In to add comment