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

Saving Turtle Location

Started by Buho, 09 September 2013 - 07:00 AM
Buho #1
Posted 09 September 2013 - 09:00 AM
I've got a tough problem that I can't figure out.

In the program that my mining turtles run, I save to file their x,y,z and direction. Every time they move or turn.

The idea is that, in case the chunk is unloaded and the turtles shut off, when the chunk is reloaded, they know how to return how to return home and aren't lost in the world. (I haven't written a GPS.)

In general, this works fine. The location is recorded accurately and the turtles return precisely home if they get rebooted through chunk loading.

However, I've found that sometimes the direction isn't recorded correctly and the turtles get even more lost! I'm really stumped by this.


function turnRight()
	turtle.turnRight()
	curPos.d = curPos.d + 1
	if curPos.d > 3 then curPos.d = curPos.d - 4 end
	savePosition(curPos)
end

Likely the chunk is unloaded between the actual turn and the saving to file, but with the saving of the position is so close to the actual turn, I'm not sure how to improve on this.
jay5476 #2
Posted 09 September 2013 - 10:34 AM
full code please
Lyqyd #3
Posted 09 September 2013 - 10:41 AM
Save the new heading immediately before the turn. Turns will always succeed. If the chunk unloads while the turtle is yielded to turn, it will be in the new position when the chunk is loaded.
Buho #4
Posted 09 September 2013 - 11:20 AM
Lyqyd: that makes total sense, and thanks for confirming the state mid-turn. I'll try that out!