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

[Solved] Tunneling Program (if 3==3 does not return true)?

Started by eximius, 04 May 2012 - 03:50 AM
eximius #1
Posted 04 May 2012 - 05:50 AM
Ok, so it works almost how I want it to so far. You place it, and enter the arguments and it's off! However, when I was testing with tunnels of width 3, it would finish the third width, and then turn as if to make the next width of the tunnel. Somehow, if x==w where x,w are both 3, is not returning true. I am absolutely baffled. It should simply finish the third width and back up to the entrance to the tunnel (if it's a really long tunnel you shouldn't have to go fetch it).

Perhaps it's an obvious oversight, but I cannot see it for the life of me.

On a completely unrelated note, suggestions how to implement the torch feature would be a appreciated, as well as any other advice.



local arg = { ... }

function usage ()
print("Usage: tunnel <length> [options]\n\t<length> - length of tunnel\n\t<width> - width of tunnel, default 1\nt\t<height> - height of tunnel default 3\n\t<torchInterval> - blocks between torches, \n\t\tdefault 8, 0 is no torches")
end

function down(height, length)
for z=1, length do
while not turtle.forward() do
turtle.dig()
end
for a=1,height-1 do
while not turtle.up() do
turtle.digUp()
end
end
for b=1,height-1 do
turtle.down()
end
end
end

w = 1
h = 3
t = 8

if #arg==1 then
l = arg[1]
elseif #arg==2 then
l = arg[1]
w = arg[2]
elseif #arg==3 then
l = arg[1]
w = arg[2]
h = arg[3]
elseif #arg==4 then
l = arg[1]
w = arg[2]
h = arg[3]
t = arg[4]
else
usage()
return 0
end

for x=1,w do
print(x.." == "..w)
down(h,l)
if x==w then
print(x.." == "..w.."!!!!")
if x%2==0 then
print("gothere")
return 0
else
print("wrf")
for y=1,l do
turtle.back()
end
return 0
end
else
print("turn")
if x%2==1 then
turtle.turnLeft()
else
turtle.turnRight()
end
while not turtle.forward() do turtle.dig() end
if x%2==1 then
turtle.turnLeft()
else
turtle.turnRight()
end
turtle.digUp()
turtle.up()
while turtle.detectUp() do
sleep(1)
turtle.digUp()
end
turtle.down()
end
end

Luanub #2
Posted 04 May 2012 - 05:55 AM
I don't see that you've declared x anywhere or assigned it a value either. That's probably where your problem is.
Xtansia #3
Posted 04 May 2012 - 06:29 AM
x is his counter variable in the for loop
eximius #4
Posted 04 May 2012 - 06:50 AM
It is the counter variable like Tom said. I didn't read too much of the Lua spec, so I wasn't sure about counter variable scopes (when you have conflictions) so I just have all the counters different variables.

It should be noted that when I ran this with "tunnel 3" it ran correctly (dug three forward with a height of three and then returned).
Xtansia #5
Posted 04 May 2012 - 07:10 AM
It's because l,w,h,t are not numbers they are strings you need to do like this:
l = tonumber(arg[1])
eximius #6
Posted 04 May 2012 - 07:40 AM
That's it. My friend pointed this out just a minute ago as well. Here is the updated code:


local arg = { ... }
function usage ()
print("Usage: tunnel <length> [options]nt<length> - length of tunnelnt<width> - width of tunnel, default 1ntt<height> - height of tunnel default 3nt<torchInterval> - blocks between torches, nttdefault 8, 0 is no torches")
end
function turn(count, width)
if count==width then
print("dont turn!")
return 0
elseif count%2==1 then
turtle.turnLeft()
while not turtle.forward() do turtle.dig() end
turtle.turnLeft()
turtle.digUp()
turtle.up()
while turtle.detectUp() do
sleep(1)
turtle.digUp()
end
turtle.down()
else
turtle.turnRight()
while not turtle.forward() do turtle.dig() end
turtle.turnRight()
turtle.digUp()
turtle.up()
while turtle.detectUp() do
sleep(1)
turtle.digUp()
end
turtle.down()
end
print("turned")
end
function down(height, length)
for z=1, length do
while not turtle.forward() do
turtle.dig()
end
for a=1,height-1 do
while not turtle.up() do
turtle.digUp()
end
end
for b=1,height-1 do
turtle.down()
end
end
end
l = 0
w = 1
h = 3
t = 8
if #arg==1 then
l = tonumber(arg[1])
elseif #arg==2 then
l = tonumber(arg[1])
w = tonumber(arg[2])
elseif #arg==3 then
l = tonumber(arg[1])
w = tonumber(arg[2])
h = tonumber(arg[3])
elseif #arg==4 then
l = tonumber(arg[1])
w = tonumber(arg[2])
h = tonumber(arg[3])
t = tonumber(arg[4])
else
usage()
return 0
end
for x=1,w do
down(h,l)
if x==w then
if x%2==0 then
return 0
else
for y=1,l do
turtle.back()
end
return 0
end
else
turn(x,w)
end
end