Wednesday, January 9, 2013

Tunnelling Turtle

I've got a new program for you today. This one creates 3x3 tunnels as long as you want. Again, it's not perfect, but it's great for making pathways in the nether. It also automatically places torches for you. So, here is "tunnels":


-- Clearing Terminal
term.clear()
term.setCursorPos(1,1)

-- Setting up Variables
local tArgs = { ... }
local fuel = 1
local torch = 2
local block = 3
local gone = 0

if #tArgs ~= 1 then
    -- Missing Length Argument
    print("Usage: Branch <length>")
    print("Slot 1: Fuel")
    print("Slot 2: Torches")
    print("Slot 3: Blocks")
    error()
end

local length = tArgs[1]

--Missing Items Check
if (turtle.getItemCount(fuel) < 1 or turtle.getItemCount(torch) < 1 or turtle.getItemCount(block) < 1) then
    print("Missing Critical Item")
    error()
end

-- Fuel Checking Function
function tFuel(moved,f)
    if turtle.getFuelLevel() < (moved + 20) then
        turtle.select(f)
        if not turtle.refuel(1) then
            print("Need Fuel")
            finished(moved)
        end
    end
end

-- Drop Items Function
function dump()
    print("Dumping Goods")
    for i = 4, 16 do
        turtle.select(i)
        turtle.drop()
    end
end

--Checks for full inventory
function slotCheck(moved)
    local slots = 0
    for i = 4, 16 do
        turtle.select(i)
        if turtle.getItemCount(i) > 0 then slots = slots + 1 end
    end
    if slots == 12 then finished(moved) end
end

-- Return Function
function finished(m)
    print("Returning after " .. m + 1 .. " blocks")
    turtle.turnRight()
    turtle.turnRight()
    for i = 1, m do
        turtle.forward()
    end
    dump()
    error()
end

print("Starting tunnel " .. length .. " blocks long")

for g = 1, length do
    tFuel(g, fuel)
    while turtle.detect() do
        turtle.dig()
        sleep(0.5)
    end
    turtle.forward()
    if not turtle.detectDown() then
        turtle.select(block)
        turtle.placeDown()
    end
    while turtle.detectUp() do
        turtle.digUp()
        sleep(0.5)
    end
    turtle.turnRight()
    while turtle.detect() do
        turtle.dig()
        sleep(0.5)
    end
    turtle.forward()
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    if not turtle.detectDown() then
        turtle.select(block)
        turtle.placeDown()
    end
    while turtle.detectUp() do
        turtle.digUp()
        sleep(0.5)
    end
    turtle.up()
    if g%5 == 0 then
        turtle.select(torch)
        turtle.placeDown()
    end
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    while turtle.detectUp() do
        turtle.digUp()
        sleep(0.5)
    end
    turtle.up()
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    if not turtle.detectUp() then
        turtle.select(block)
        turtle.placeUp()
    end
    turtle.turnRight()
    turtle.turnRight()
    while turtle.detect() do
        turtle.dig()
        sleep(0.5)
    end
    turtle.forward()
    if not turtle.detectUp() then
        turtle.select(block)
        turtle.placeUp()
    end
    while turtle.detect() do
        turtle.dig()
        sleep(0.5)
    end
    turtle.forward()
    if not turtle.detectUp() then
        turtle.select(block)
        turtle.placeUp()
    end
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    turtle.digDown()
    turtle.down()
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    turtle.digDown()
    turtle.down()
    if not turtle.detect() then
        turtle.select(block)
        turtle.place()
    end
    if not turtle.detectDown() then
        turtle.select(block)
        turtle.placeDown()
    end
    turtle.turnRight()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    gone = g
    slotCheck(gone)
end

finished(gone)

No comments:

Post a Comment