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

Turtles are not behaving.

Started by kylezffz, 27 May 2013 - 01:53 PM
kylezffz #1
Posted 27 May 2013 - 03:53 PM
I have a sugar cane farm, and I coded a program to harvest the sugar cane with 4 separate turtles.
Screenshot: http://puu.sh/32tb5/23570845fb.jpg
This is the program: http://pastebin.com/tk9eTMXY
T
he turtles occasionally break fences, then continue to derp for the rest of their harvest until they're broken and replaced.
They seem to somehow get off by one row and think fences are part of the sugar cane.
I can't seem to figure out why this continues, even with the addition of the dig function to stop them from breaking fences.
Can anybody help me?

Thank you,
-Kyle

Edit: Forgot to mention, this happens randomly, not every time.
Bomb Bloke #2
Posted 28 May 2013 - 07:43 AM
Your script relies somewhat on luck - Say a turtle has an empty plot in front of it. It fires the dig function (which does nothing 'cause there's nothing there), then the plant happens to grow, then the turtle tries to move forward… That movement will fail and the turtle now thinks it's somewhere it's not.

You might think the odds are pretty low, but as the script runs for longer periods they still approach 100%. I'm also suspecting turtles may collide in some instances which'd have the same result.

So, make sure the turtles move when you want them to move. For example, you can re-write this:


dig()
turtle.suck()
turtle.attack()
turtle.forward()

To this:

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

The turtle will try to move forward, and if it fails, try to clear the way, and repeat until the move is a success.

There are many other incidences where you'll want to make similar changes (eg, when trying to go down).