Skip to main content

Fighting Bugs

This lesson discusses what happens when the ants confront their mortal enemies, bugs. A bug in AntMe! is much stronger than any single ant. Worse, their hit points regenerate over time. So you need to be careful when fighting them. You can't afford to let too many of your ants die, but you also need to ensure that they manage to defeat the bug.

Find the "SeeEnemy (bug)" method under "Combat".

Add the following lines of code to the method:

public override void SiehtFeind(Wanze wanze)
{
LasseNahrungFallen();
if(AnzahlAmeisenInSichtweite >= 2)
{
GreifeAn(wanze);
}
else
{
GeheWegVon(wanze);
}
}

Now find the “UnderAttack (bug)” method. Add the following lines of code to it:

public override void WirdAngegriffen(Wanze wanze)
{
if(AktuelleEnergie < MaximaleEnergie / 2)
{
GeheZuBau();
}
}

What do these lines of code do? If an ant sees a bug, first it checks if at least two of its friends are nearby so that all three can attack the bug together. If the ant determines that there are enough friends in the area, then it attacks the bug and hopes that the other ants will do the same. If it doesn't have enough friends in the area, then the ant runs away. In both cases it drops any food that it's carrying so that it can move more quickly.

During battle the ant constantly checks its energy value, which works like hit points in a game. If it only has a little bit of energy left, it will run back to the anthill to recover.

You earn 100 points for every bug you kill.

There's something else we can do to improve the behavior of our ants. Try to imagine what happens when an ant finds itself between a mound of sugar and a bug and both are in sight. The ant might decide to go towards the sugar one moment and then towards the bug the next, and so on. Since the ant doesn't know which option it should prefer, we want to specify that it should go fight the bug.

Find the two “See” methods for sugar and fruit that you modified earlier. Add the following condition to the existing source code (it is important that this condition encloses the already existing source code with { } brackets):

if(!(Ziel is Wanze))
{
...
}

This code commands your ants to only react to food in their vicinity if they aren't already running towards a bug or fighting one.

It should look like this in the editor after you've added the condition:

If you start the game now by clicking the green play button, you'll notice that the ants rarely win a fight with a bug. Basically, they only win if, by chance, there are already a lot of ants near the bug. Usually they just run away. Why is this? The instructions in the source code dealing with combat (drop food, attack together) sound so sensible! The reason is that the probability that enough ants will find themselves near each other when a bug is in visual range is currently very low. In the next lesson (lesson six) we'll discuss a way to summon more ants together. Lesson seven will teach you how to make your ants stronger so that they can defeat bugs faster.