This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gotan333's profile picture

I am Noob plz help (turtle tunneling program)

Started by gotan333, 25 December 2012 - 02:24 AM
gotan333 #1
Posted 25 December 2012 - 03:24 AM
i made (took and changed) some code, it makes a 3x3 tunnel.

i want to have it return to the begining, but it doesnt return, it just stays is spot (does turn, but turns back)

also i want to add a torch placeing thingy but not sure how/what the code is

also im a noob at this (i just started today) if something seems obvious for you, its not for me

my code:


local tArgs = { ... }
if #tArgs ~= 1 then
	print( "Usage: tunnel <length>" )
	return
end
local length = tonumber( tArgs[1] )
if length < 1 then

	print( "Tunnel length must be positive" )
	return
end	
local depth = 0
local collected = 0
local function collect()
	collected = collected + 1
	if math.fmod(collected, 25) == 0 then
		print( "Mined "..collected.." blocks." )
	end
end
local function tryDig()
	while turtle.dig() do
		collect()
		sleep(0.5)
		if not turtle.detect() then
			return true
		end
	end
	return not turtle.detect()
end
local function tryDigUp()
	while turtle.digUp() do
		collect()
		sleep(0.5)
		if not turtle.detectUp() then
			return true
		end
	end
	return not turtle.detectUp()
end
print( "Tunnelling..." )
for n=1,length do
	tryDigUp()
	turtle.turnLeft()
	tryDig()
	turtle.up()
	tryDig()
	tryDigUp()
	turtle.up()
	tryDig()
	turtle.turnRight()
	turtle.turnRight()
	tryDig()
	turtle.down()
	tryDig()
	turtle.down()
	tryDig()
	turtle.turnLeft()
	if n<length then
		tryDig()
		if not turtle.forward() then
			print( "Aborting Tunnel." )
			break
		end
	else
		print( "Tunnel complete." )
	end
end
print( "Returning to start..." )
turtle.turnLeft()
turtle.turnLeft()
while depth > 0 do
	if turtle.forward() then
		depth = depth - 1
	else
		turtle.dig()
	end
end
turtle.turnRight()
turtle.turnRight()
print( "Tunnel complete." )
print( "Mined "..collected.." blocks total." )
remiX #2
Posted 25 December 2012 - 04:35 AM

local tArgs = { ... }
if #tArgs ~= 1 then
	    print( "Usage: tunnel <length>" )
	    return
end
local length = tonumber( tArgs[1] )
if length < 1 then

	    print( "Tunnel length must be positive" )
	    return
end     
local depth = 0
local collected = 0
local function collect()
	    collected = collected + 1
	    if math.fmod(collected, 25) == 0 then
			    print( "Mined "..collected.." blocks." )
	    end
end
local function tryDig()
	    while turtle.dig() do
			    collect()
			    sleep(0.5)
			    if not turtle.detect() then
					    return true
			    end
	    end
	    return not turtle.detect()
end
local function tryDigUp()
	    while turtle.digUp() do
			    collect()
			    sleep(0.5)
			    if not turtle.detectUp() then
					    return true
			    end
	    end
	    return not turtle.detectUp()
end
print( "Tunnelling..." )
for n=1,length do
	    tryDigUp()
	    turtle.turnLeft()
	    tryDig()
	    turtle.up()
	    tryDig()
	    tryDigUp()
	    turtle.up()
	    tryDig()
	    turtle.turnRight()
	    turtle.turnRight()
	    tryDig()
	    turtle.down()
	    tryDig()
	    turtle.down()
	    tryDig()
	    turtle.turnLeft()
        if math.fmod(i, 5) == 0 then  -- It will place torches every 5 blocks
		    turtle.turnLeft()
            turtle.select(1) -- select the slot with the torches
            turtle.place()
		    turtle.turnRight()
        end
	    if n<length then
			    tryDig()
			    if not turtle.forward() then
					    print( "Aborting Tunnel." )
					    break
			    end
	    else
			    print( "Tunnel complete." )
	    end
end
print( "Returning to start..." )
for i = 1, length do  -- just does turtle.back() the same amount of times it went forward
    turtle.back()
end
print( "Tunnel complete." )
print( "Mined "..collected.." blocks total." )
gotan333 #3
Posted 25 December 2012 - 04:42 PM
thank you that really helps