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

Turtle turning too much and going too high

Started by The_Awe35, 06 February 2013 - 04:47 PM
The_Awe35 #1
Posted 06 February 2013 - 05:47 PM
I am making a land clearing program. I have it mostly done, but I've found that it will turn one time too many. I want it to clear out, say, an 5x5 area, it will do the fifth line, and then turn again.


function plane()
  for i = 1,width do
  line() -- just does how far to go and digging
	 if length%2==0 then
	 nextLeft() -- runs the function to turn left
	 else
	 nextRight() -- runs the function to turn right
	end
  end
end

Also, for efficiency, I have my turtle breaking the blocks in front, above, and below it, then moving up three spaces. This makes the problem of if I want it ten high, it will go to twelve (or possibly 9, I haven't thoroughly tested it)


high = 10

function uplvl()
  for i = 1,high,3 do
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  end
end

Any suggestions?
Shnupbups #2
Posted 06 February 2013 - 05:58 PM
Can we see your nextLeft() and nextRight() functions' code?
Pharap #3
Posted 06 February 2013 - 06:13 PM
Make the turtle figure out how much it needs to break.
So if it's a 5x5x5 block you're trying to cut, the turtle needs to know it can do up, down and forward breaks for part 1, but when it goes up 3, it's only allowed to break in front of it and below it (assuming it's travelling up). It can figure that out by doing number of blocks to break along Y % 3. for example 5%3 gives out 2, so it knows on its last leg it goes up 2 instead of 3, and then obviously if you have something like 4%3 (1) then it only goes up 1. Something along those lines.
At any rate you see how you can figure out how to treat the last stage of the procedure using modulus.
The_Awe35 #4
Posted 07 February 2013 - 03:42 AM
Can we see your nextLeft() and nextRight() functions' code?
It isn't really anything big


function nextRight()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
turtle.digUp()
turtle.digDown()
And substitute turtle.turnRight() for turtle.turnLeft() and you have nextLeft()
shiphorns #5
Posted 07 February 2013 - 05:46 AM
I am making a land clearing program. I have it mostly done, but I've found that it will turn one time too many. I want it to clear out, say, an 5x5 area, it will do the fifth line, and then turn again.


function plane()
  for i = 1,width do
  line() -- just does how far to go and digging
	 if length%2==0 then
	 nextLeft() -- runs the function to turn left
	 else
	 nextRight() -- runs the function to turn right
	end
  end
end

It's doing exactly what you've told it to do, which is repeat the cycle of digging a line, then turning around, 5 times. If on the last line you don't want it to turn, you either need your loop to do this:

function plane()
  for i=1,width-1 do  -- Note that it is width - 1
	line()
	doEndOfRowTurn()
  end
  line() --last row
end

or you can loop 'width' times and use a check inside the loop like this:

function plane()
  for i=1,width do
	line()
	if (i<width) then  -- if this is not the last row in the layer...
	  doEndOfRowTurn()
	end
  end
end

Also, for efficiency, I have my turtle breaking the blocks in front, above, and below it, then moving up three spaces. This makes the problem of if I want it ten high, it will go to twelve (or possibly 9, I haven't thoroughly tested it)


high = 10

function uplvl()
  for i = 1,high,3 do
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  end
end

Any suggestions?

Instead of just always going up 3 levels, you should do the math to figure out how many more levels the turtle is supposed to do, and only go up as much as necessary. For example, if you've asked him to clear 10 layers, he should do 3 passes clearing 3 layers, and then a final pass that clears only 1 layer.

When your turtle is told to clear a number of layers, TotalLayers, he's going to have to do math.floor(TotalLayers/3) passes that remove 3 layers at a time, followed by a final pass to clear the remaining (TotalLayers % 3) layers (the remainder of the number of total number of layers divided by 3). Either of these expressions could be zero (the former is zero when TotalLayers<3 and the latter will be zero if TotalLayers is a multiple of 3).
The_Awe35 #6
Posted 08 February 2013 - 03:25 PM
Thanks alot!