Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Globals
- local arg = {...}
- local SQUARE_SIZE = 3
- local DOWN_SQUARE = 0
- local UP_SQUARE = 1
- local squareType = DOWN_SQUARE
- local CONTAINER_SUBSTRINGS = {"chest"}
- -- Determines if the block in front of a turtle is a chest
- local function isChest()
- local status, data = turtle.inspect()
- if(not status) then
- print("No chest available")
- return false
- end
- local itemName = data["name"]:match(":(.+)"):lower()
- for index, container in pairs(CONTAINER_SUBSTRINGS) do
- if(itemName:find(container)) then
- return true
- end
- end
- return false
- end
- -- 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
- -- Attempt to dump non-fuel items into a chest in front of the turtle
- local function attemptDumpInv()
- if(not isChest()) then
- return
- end
- for index=1, 16 do
- turtle.select(index)
- if(not turtle.refuel(0)) then
- turtle.drop()
- end
- end
- end
- -- Move in a direction 1 or more times
- local function move(direction, distance)
- local distance = tonumber(distance) or 1
- attemptRefuel()
- local result
- if(direction == "up") then
- result = turtle.up()
- elseif(direction == "down") then
- result = turtle.down()
- elseif(direction == "forward") then
- result = turtle.forward()
- elseif(direction == "back") then
- result = turtle.back()
- else
- print("Direction: " .. direction .. " not a valid movement")
- result = false
- end
- if(not result) then
- return result
- 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)
- local result
- if(direction == "up") then
- turtle.digUp()
- result = move("up")
- elseif(direction == "down") then
- turtle.digDown()
- result = move("down")
- elseif(direction == "forward") then
- turtle.dig()
- result = move("forward")
- elseif(direction == "back") then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- result = move("back")
- else
- print("Direction: " .. direction .. " not a valid movement")
- return false
- end
- -- Usually gets stuck on sand/gravel so dig it back out
- if(not result) then
- digMove(direction)
- end
- return true
- end
- -- Dig out a 3x3 vertical square
- local function tunnelSquare()
- -- Dig out adjacent left and right blocks
- local function tunnelAdjacentLR()
- local function verifyDig()
- local status = turtle.inspect()
- while(status) do
- turtle.dig()
- status = turtle.inspect()
- end
- end
- turtle.turnLeft()
- verifyDig()
- turtle.turnLeft()
- turtle.turnLeft()
- verifyDig()
- turtle.turnLeft()
- end
- digMove("forward") -- Move into next vertical square
- for i=1, SQUARE_SIZE do
- tunnelAdjacentLR()
- if(i < 3) then
- if(DOWN_SQUARE == squareType) then
- digMove("up")
- else
- digMove("down")
- end
- end
- end
- if(DOWN_SQUARE == squareType) then
- squareType = UP_SQUARE
- else
- squareType = DOWN_SQUARE
- end
- 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 (- left, + right)]\nAlso, if a chest is placed to the turtle's right it'll dump non-fuel items into that")
- end
- -- Main
- local function main(distance, spacing)
- -- Handle args
- local distance = tonumber(distance) or 0
- local spacing = tonumber(spacing) or 0
- if(distance < 3) then
- help()
- return
- end
- -- Initial tunneling distance
- tunnel(distance)
- -- Tunnel over to return shaft
- move("back")
- if(spacing > 0) then
- turtle.turnRight()
- move("forward")
- tunnel(spacing)
- move("back")
- turtle.turnRight()
- elseif(spacing < 0) then
- turtle.turnLeft()
- move("forward")
- tunnel(spacing * -1)
- move("back")
- turtle.turnLeft()
- else
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Move to edge of return shaft
- move("forward")
- -- Tunnel/move back to start
- if(spacing == 0) then
- move("forward", distance - 2)
- else
- tunnel(distance - 3)
- move("forward")
- end
- if(UP_SQUARE == squareType) then
- move("down", 2)
- end
- -- Spin back around, dump non-fuel into adjacent chest
- turtle.turnLeft()
- attemptDumpInv()
- turtle.turnLeft()
- end
- main(arg[1], arg[2])
Advertisement
Add Comment
Please, Sign In to add comment