We also created several specialized AIs for the various campaign missions, and Willem will tell you more about those in the next part of the series.
- defend its own power plants
- advance towards the opponent
- attack whatever is in its way
- finally, blow up the opponent's power plants
For example, if we're figuring out which weapons to charge:
- Can it hit a power plant? +1000 points! Does that power plant only have one hitpoint left? Add another 1000!
- Is it in our way, according to our pathfinding? That's 150 points.
- Is it a weapon? Can it hit one of our power plants? 200 points. It can't? Still scary, 10 points.
- Anything else? Well OK, have 5 points for being the wrong colour.
The AI will use it in two cases: defensively, if it's about to lose a power plant, it'll overcharge it to get some extra juice out of it. Offensively, it will overcharge a power plant only if it has more power plants than the opponent, and it could reach a strategically valuable tile but doesn't quite have enough power to get there. Again, simple rules, but the AI still manages to surprise us with a sudden charge every now and then.
To remedy that, we add a small random amount to every cost we calculate for A*. Not enough to make it choose a suboptimal path, but just random enough so that it'll make slightly different choices every time. And the effect is huge: if you look at the red and blue AIs fighting each other behind the main menu (tip: press F10 to hide the menu for a better look at the action), you'll notice that no two games are the same. In fact, we've been unable to predict which one will win, even though they are both running the same algorithms in every game they play! All because of a tiny, tiny bit of randomness.