\b;Objective
Bring down the flying wasps in a more efficient way than with the previous program.

\b;Program
Here is again the program of the previous exercise that shoots down all the wasps after many, many unsuccessful attempts:
\c;
\s;extern void object::Wasp1()
\s;{
\s;	\l;object\u cbot\type;    item;
\s;	
\s;	\l;aim\u cbot\aim;(0);
\s;	
\s;	\l;while\u cbot\while; (true)
\s;	{
\s;		\l;while\u cbot\while; (\l;radar\u cbot\radar;(AlienWasp, 0, 360, 0, 20) == null)
\s;		{
\s;			item = \l;radar\u cbot\radar;(AlienWasp);
\s;			\l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));
\s;			\l;motor\u cbot\motor;(1,1);
\s;			
\s;			\l;jet\u cbot\jet;(0);
\s;			\l;if\u cbot\if; (position.z > item.position.z)
\s;			{
\s;				\l;jet\u cbot\jet;(-0.3);
\s;			}
\s;			
\s;			\l;if\u cbot\if; (position.z < item.position.z - 1)
\s;			{
\s;				\l;jet\u cbot\jet;(0.3);
\s;			}
\s;			
\s;			\l;wait\u cbot\wait;(0.2);
\s;		}
\s;		item = \l;radar\u cbot\radar;(AlienWasp);
\s;		\l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));
\s;		\l;fire\u cbot\fire;(1);
\s;	}
\s;}
\n;
The many failures are due to the fact that the wasp is already gone before the bullets can reach it. The only way to improve the program consists in setting the power of the two motors and of the jet in such a way that the bot follows the movement of the target during the burst.

Just before the shot, the program adjusts a last time the direction with \c;turn(direction(item.position));\n;. In order to follow the wasp during the burst, you have to "remember" the angle of this last rotation: if the angle was positive (rotation to the left), the bot must continue to turn left during the burst; if the angle was negative, the bot must continue to turn right.

In order to "remember" the angle of the last rotation, we need a variable that can contain just one number. If we choose to call it \c;angle\n;, we must define the variable with the following line at the beginning of the program:
\c;
\s;	\l;float\u cbot\type; angle;
\n;
The variable type \l;float\u cbot\type; is the variable type that can contain any number, i.e. whole numbers or real numbers. Please refer to the \l;text about variable types\u cbot\type; if you want to know more about the different types of variables and what they can contain.

Just before the instruction \c;fire(1);\n;, instead of writing \c;\l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));\n;, we will put the rotation angle into the variable \c;angle\n;:
\c;
\s;	angle = direction(item.position);
\n;
Then we perform the rotation, and we set the power of the motors so that the bot continues the movement:
\c;
\s;	turn(angle);
\s;	if (angle < 0)
\s;	{
\s;		motor(1,0.5);
\s;	}
\s;	else
\s;	{
\s;		motor(0.5,1);
\s;	}
\n;
The instruction \c;else\n; determines what instructions the program should execute if the condition stated in the \c;if\n; instruction is false.

Then we must set the power of the jet so that the bot follows the wasp also in the vertical direction:
\c;
\s;	jet(0);
\s;	if(position.z > item.position.z)
\s;	{
\s;		jet(-0.3);
\s;	}
\s;	
\s;	if(position.z < item.position.z - 1)
\s;	{
\s;		jet(0.3);
\s;	}
\n;
As you will see, this program is much more efficient than the previous one!

\t;See also
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
