Adapt the program \c;Tower1\n; in order to avoid wasting energy when shooting at ants that are out of range.
\t;Program
The program \c;Tower1\n; shoots in the direction of the closest ant, even if it is still out of range:
\c;
\s;extern void object::Tour1()
\s;{
\s; object item;
\s;
\s; jet(0.2);
\s; while(position.z < 20)
\s; {
\s; wait(0.2);
\s; }
\s; jet(0);
\s;
\s; while(true)
\s; {
\s; item = radar(AlienAnt);
\s; turn(direction(item.position));
\s; fire(1);
\s; }
\s;}
\n;
If the bot must perform the same task than before with only a \l;regular power cell\u object\power;, you can insert a loop that waits until the ant is close enough, i.e. until the ant is at less than 40 meters. Only one burst will be needed for every ant then.
You can give the instruction \c;\l;radar\u cbot\radar();\n; more parameters (numbers) indicating where the radar should look for the ants. For example \c;radar(AlienAnt,0,360,0,40);\n; will only look for ants that are closer than 40 meters. The two first numbers tell the radar to search in all directions, the two last numbers tell the radar to detect only ants that are at a distance between 0 and 40 meters. If no ant is found that is closer than 40 meters, the instruction \c;\l;radar\u cbot\radar();\n; returns the value \c;null\n;. We can then write a conditional loop similar to the one written in the last exercise that waits as long as there is no ant closer than 40 meters:
\c;
\s; while (radar(AlienAnt, 0, 360, 0, 40) == null)
\s; {
\s; wait(0.2);
\s; }
\n;
Notice that two equal signs "==" are needed to perform a comparison of equality.
Just insert these lines before the instruction \c;fire(0);\n;, and the bot will wait before shooting until the ant is closer than 40 meters. Like this a regular power cell is enough to kill all ants.
\t;See also
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.