extern void object::Move( )
{
	float	speed, delay;
	point	lastswitch;
	int		front, left, right, choice;

	errmode(0);  // ne stoppe pas si erreur
	while ( ismovie() != 0 )  wait(1);

	if ( tstarter() == 1 )
	{
		wait(5);  // attend 3,2,1,go
	}

	speed = 0.8;
	delay = 0.1;

	lastswitch = position;
	while ( true )
	{
		front = trajrank(0, 120, 5, 40);
		if ( front == -1 or distance2d(position, lastswitch) > 50 )
		{
			left  = trajrank( 67, 45, 10, 30);
			right = trajrank(-67, 45, 10, 30);

			if ( front == -1 and left == -1 and right == -1 )
			{
//				message("panic !!!");
				front = trajrank(0, 360, 0, 10000);
				if ( front == -1 )  return;
			}

			while ( true )
			{
				choice = rand()*3;
				if ( choice == 0 and front != -1 )  break;
				if ( choice == 1 and left  != -1 )  break;
				if ( choice == 2 and right != -1 )  break;
			}

			if ( choice == 0 and (left != -1 or right != -1) )
			{
//				message("continue");
				lastswitch = position;
			}

			if ( choice == 1 )
			{
//				message("left");
				approch(trajpos(left), speed, delay);
				lastswitch = position;
				continue;
			}

			if ( choice == 2 )
			{
//				message("right");
				approch(trajpos(right), speed, delay);
				lastswitch = position;
				continue;
			}
		}

		if ( front == -1 )  continue;
		go(trajpos(front), speed, delay);
	}
}

// Va vers un point sans attendre.
void object::go(point target, float speed, float delay)
{
	float   power, dist;

	dist = distance2d(position, target);

//	dist = 5+dist*5;  // bon
//	dist = 10+dist*1;  // bon
//	dist = 20+dist*0.5;  // bof
//	dist = 90;

	dist = 5+dist*5;
	if ( dist < 45 )  dist = 45;

	power = direction(target)/dist;

	if ( power > 0 )
	{
		if ( power > speed )  power = speed;
		motor(speed-power, speed);
	}
	else
	{
		power = -power;
		if ( power > speed )  power = speed;
		motor(speed, speed-power);
	}
//	wait(delay);
}

// S'approche jusqu'à 10 mètres d'un point.
void object::approch(point target, float speed, float delay)
{
	float   power, dist;

	while ( true )
	{
		dist = distance2d(position, target);
		dist = 5+dist*5;
		power = direction(target)/dist;

		if ( power > 0 )
		{
			motor(speed-power, speed);
		}
		else
		{
			motor(speed, speed+power);
		}
		if ( dist < 10 )  break;
//		wait(delay);
	}
}
