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

turtle digs 1 line too many

Started by subzero22, 05 April 2013 - 11:38 PM
subzero22 #1
Posted 06 April 2013 - 01:38 AM
Ok well the script works but I can't figure out why when I put lets say 5 long it digs 6 long.

Also is there a way to make it so when lets say I put 2 wide it doesn't dig 3 wide?

Well writeing the code out I found out why it goes 3 wide. But when I take out the extra part of the code it starts screwing up once it goes up. it's the last for x=1,numx part of the code that makes the extra wide line.

The code works but if there's a spelling error it's because I'm having to write it out as http is disabled on the server. I'm also playing on tekkit classic. Here's what I got.


write("How long do you need it: ")
local numx = tostring( read() )
write("How wide do you need it: ")
local numz = tostring( read() )
write("How tall do you need it: ")
local numy = tostring( read() )


numx = tonumber(numx)
numz = tonumber(numz)
numy = tonumber(numy)

-- get turtle into position

while not turtle.forward() do
turtle.dig()
end


-- the flatten/room script

for y=1,numy do
for z=2,numz do
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
  turtle.turnLeft()
  while not turtle.forward() do
  turtle.dig()
  end
  turtle.turnLeft()
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
  turtle.turnRight()
  while not turtle.forward() do
  turtle.dig()
  end
  turtle.turnRight()
end
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
turtle.turnLeft()
turtle.turnLeft()
while not turtle.up() do
turtle.digUp()
end
end
Telokis #2
Posted 06 April 2013 - 02:06 AM
Ok well the script works but I can't figure out why when I put lets say 5 long it digs 6 long.

Also is there a way to make it so when lets say I put 2 wide it doesn't dig 3 wide?

Well writeing the code out I found out why it goes 3 wide. But when I take out the extra part of the code it starts screwing up once it goes up. it's the last for x=1,numx part of the code that makes the extra wide line.

The code works but if there's a spelling error it's because I'm having to write it out as http is disabled on the server. I'm also playing on tekkit classic. Here's what I got.


write("How long do you need it: ")
local numx = tostring( read() )
write("How wide do you need it: ")
local numz = tostring( read() )
write("How tall do you need it: ")
local numy = tostring( read() )


numx = tonumber(numx)
numz = tonumber(numz)
numy = tonumber(numy)

-- get turtle into position

while not turtle.forward() do
turtle.dig()
end


-- the flatten/room script

for y=1,numy do
for z=2,numz do
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
  turtle.turnLeft()
  while not turtle.forward() do
  turtle.dig()
  end
  turtle.turnLeft()
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
  turtle.turnRight()
  while not turtle.forward() do
  turtle.dig()
  end
  turtle.turnRight()
end
for x=1,numx do
	while not turtle.forward() do
	turtle.dig()
	end
end
turtle.turnLeft()
turtle.turnLeft()
while not turtle.up() do
turtle.digUp()
end
end

Instead of using for loops, you can use something like that:


x = 1
while x < numx do
  -- Whatever you want
  x = x + 1
end

I think it won't dig too many anymore.
PixelToast #3
Posted 06 April 2013 - 05:06 AM
.-. thats the same as a for loop
Engineer #4
Posted 06 April 2013 - 05:24 AM
-snip-

Just the proof that it is the same as a for loop, in Java or php if you know it:

--Java:
for(int i;i < 5; i++){
    // do something
}
--php, only variable changes really
for($i = 2; $i < 5: $i++){
   // do something
}

But on topic:

Maybe this helps: you are essentialy doing this:

for i = 1, numx * numy * numz do
  -- Your code
end

Maybe that helps?:S
Telokis #5
Posted 06 April 2013 - 05:38 AM
.-. thats the same as a for loop

It's not the same because the for loop does an inclusive comparison. It would be the same as :


local a = 1
while (a <= max) do
  --Anything
  a = a + 1
end

Do you see what I mean ?
Engineer #6
Posted 06 April 2013 - 06:13 AM
.-. thats the same as a for loop

It's not the same because the for loop does an inclusive comparison. It would be the same as :


local a = 1
while (a <= max) do
  --Anything
  a = a + 1
end

Do you see what I mean ?
Im going to qoute myself on this one.
-snip-

Just the proof that it is the same as a for loop, in Java or php if you know it:

--Java:
for(int i;i < 5; i++){
	// do something
}
--php, only variable changes really
for($i = 2; $i < 5: $i++){
   // do something
}

But on topic:

Maybe this helps: you are essentialy doing this:

for i = 1, numx * numy * numz do
  -- Your code
end

Maybe that helps?:S

That is what you with a for loop, only in lua the syntax is different.
Telokis #7
Posted 06 April 2013 - 06:22 AM
.-. thats the same as a for loop
It's not the same because the for loop does an inclusive comparison. It would be the same as :
 local a = 1 while (a <= max) do --Anything a = a + 1 end 
Do you see what I mean ?
Im going to qoute myself on this one.
-snip-
Just the proof that it is the same as a for loop, in Java or php if you know it:
 --Java: for(int i;i < 5; i++){ // do something } --php, only variable changes really for($i = 2; $i < 5: $i++){ // do something } 
But on topic: Maybe this helps: you are essentialy doing this:
 for i = 1, numx * numy * numz do -- Your code end 
Maybe that helps?:S
That is what you with a for loop, only in lua the syntax is different.

In lua it changes your for line by this one :


for (int i; i <= 5; i++) {

It is not a "<" but a "<=". Am I wrong ?
subzero22 #8
Posted 06 April 2013 - 07:42 AM
I thought in lua it was while x <= numx do or do you need the = in there?

Well anyway problem is that it digs an extra line ling and wide. Is there any way to look at the excavate code that comes built in with the turtles to get some ideas?
Telokis #9
Posted 06 April 2013 - 07:45 AM
I thought in lua it was while x <= numx do or do you need the = in there?

Well anyway problem is that it digs an extra line ling and wide. Is there any way to look at the excavate code that comes built in with the turtles to get some ideas?

Clearly, you are doing too many iterations. You want to dig 5 but it digs 6. So, change the code : if it reads 5 it reads 4 ! It's a simple change but it should work, I think.
subzero22 #10
Posted 06 April 2013 - 03:50 PM
well with the code below when numx is 5 it should only dig 5 long right? it digs 6 long for some reason.


for x=1,numx do
while not turtle.forward() do
turtle.dig()
end
end
Telokis #11
Posted 07 April 2013 - 01:45 AM
well with the code below when numx is 5 it should only dig 5 long right? it digs 6 long for some reason.


for x=1,numx do
while not turtle.forward() do
turtle.dig()
end
end

Maybe you could change your while with an if ?

for x=1,numx do
	    if not turtle.forward() then
		  turtle.dig() -- You should attack too, I think
		  turtle.forward()
	    end
end
And add a print in your for, please.


for x = 1, numx do
  print("x = ", x)
  while not turtle.forward() do
	turtle.dig()
  end
end
The_Awe35 #12
Posted 07 April 2013 - 04:31 AM
I don't think you don't need the

while not turtle.forward() do
turtle.dig()
end

Its going one too far isn't it? so getting rid of that should work.
ForTheKremlin #13
Posted 07 April 2013 - 07:43 AM
I don't think you don't need the

while not turtle.forward() do
turtle.dig()
end

Its going one too far isn't it? so getting rid of that should work.
It's actually a good preventative measure against sand etc. that can block your path.

The reason why you're going one too far is that you're starting in position 1 and going forward 5, ending up in position 6. Just change your for loops to

for x = 1,numx-1 do
for all of the variables. This should solve the problem.
The_Awe35 #14
Posted 07 April 2013 - 04:27 PM
that was what I was talking about….
I suppose I wasn't very clear. Anyway.
subzero22 #15
Posted 09 April 2013 - 02:48 AM
Thanks I'll test it out. And if I don't do the starting position first it still digs 6 blocks long even if I put 5. But ya I didn't know you could throw a -1 in there. I'll test it out now.