Programming a wargame AI

I’m back from vacation, freshly recovered and therefore able to continue working on my hobby projects.

Today I’m devoting myself to what I think is the most difficult topic in wargame development. It is certainly not the graphics. It is common knowledge that wargames are ugly as night. But it’s not the pathfinding either. In fact, pathfinding is the second most difficult issue, as possible paths are constantly changing and zones such as the “zone of control” or the ownership status of a tile are constantly changing. There are also paths for supplies, airplanes, waterways, etc..

But this article is about the AI. There are games, such as Tower Defense, where enemy AIs don’t have to do much. The principle is clear: spawn enemies, let them run, done. In a wargame, there are a lot of decisions that have to be made. Basically, you can differentiate between the following points in my game (no guarantee of completeness):

  • Decisions for individual units
  • Decisions for armies
  • Decisions for armed forces (land, air, water)
  • Global decisions for resource distribution and operational logic

Or to put it another way:

  • Operational decisions
  • Strategic decisions
  • Tactical decisions

For a wargame to actually be played by an AI, these 3 levels must be covered.

How can such an AI be structured?

First of all: I am not an AI programmer. My solution is therefore not a generally valid solution. It could even be that an experienced game developer reads this and face-palms. Nonetheless, I’ve been a programmer for over 15 years now and have already developed one or two pieces of software. One thing that has stuck with me is the use of design patterns. Design patterns help to decide how code should be organized.

Organization of the AI

This already expresses an essential core of my AI: I separate the respective decisions of the AI into different levels. At the same time, I follow a paradigm of object-oriented programming: I try to represent reality using classes. Accordingly, I have concentrated on building the AI into three independent parts:

  • Operational AI: This corresponds roughly to the decisions of the general staff or a head of state. This is where resources are allocated and fundamental decisions are made. For the wargame this means, for example, deciding whether to move large troop units or concentrate them somewhere, which units to produce and whether to go on the defensive or on the offensive. If you go on the offensive, the region in which the offensive takes place is also selected here. The objective is not defined as individual hexes. Instead, for example, it is stated that region “XY” is to be conquered within 10 turns or 75% conquered.
  • Strategic AI: Individual units are organized into armies in my wargame. These armies have an HQ unit. Among other things, this HQ unit organizes the all-important supplies for the units. At AI level, it also makes strategic decisions. It gives marching orders, organizes target fields (e.g. cities or other tiles with victory points) and defines units that should be attacked as a priority.
  • Tactical AI: The individual units (usually regiments, battalions or brigades) make the tactical decisions. They follow orders from headquarters and make decisions on the move by fighting enemy troops along the way. You also request support such as artillery or air support if, for example, the enemy forces are too strong.

How does the logic work now?

So much for the theory. In practice, it currently works as follows, whereby the operational AI only rolls the dice as to whether there should be an offensive or not.

Strategic AI:

The strategic AI selects a target. This could be a specific unit, a town or another victory point relevant location on the map. In this case, the HQ unit selects an enemy HQ unit.

The strategic AI, i.e. the HQ unit, proceeds as follows:

  • The operational AI specifies a target area. The areas are regions on the map that are marked with hexes. For example, a region can consist of 20 hexes.
  • For the game to accept regions as captured, not all hexes need to be captured. It is sufficient if more victory points from these regions belong to your own faction than the other party has. Example: Region XY has three cities that award victory points. The player faction holds city 1 with 10 victory points and city 2 with 10 victory points. The player therefore has 20 victory points. The AI has a city with 10 victory points. This means that the AI has too few points for this region. So when the order comes to capture a region, the AI calculates that it needs a city to win the region. The HQ will therefore look for the city in this region that provides the required points and is closest to its own troops. All subordinate units are given this city as a target.
  • In order to prepare and support the offensive, various support operations are launched (e.g. air strikes). However, so that the individual units can still request support themselves, not all command points are awarded.

Tactical AI:

First, the tactical AI selects the easiest path to the target location, pretending there are no enemies at all. The blue path shows the easiest way to the target.

The unit that is now on the move looks for the easiest route to the objective. There are now various options. In the unlikely event that the objective can be reached directly, the order to march is given immediately. As soon as the unit has reached the objective, the region is considered captured.
More often, however, the AI will encounter resistance. For this purpose, two paths are superimposed. The optimal waypoint route without resistance and the actual route with resistance. All units that are now on this route are saved in a pool. The system now compares which unit has the shortest distance to the unit. If there are several units in the immediate vicinity, the unit that is the weakest – according to the current reconnaissance status – is selected. The unit receives the command status “MOVE_AND_ATTACK”. The enemy unit is saved and the unit follows the path to this unit. As soon as it is in the immediate vicinity (one tile away), the order to fire is given.

In a second step, the enemy takes the real map and checks if any enemies are in the way. If there are any enemies, the unit recieves the command “move and attack”. It moves forward until it reaches an enemy. If possible, it attacks. If there are multiple units in the same range, the unit selects the weakest enemy, regarding position, unit type and strength.

Conclusion

At first, the task of programming a wargame AI seemed impossible to me. However, breaking it down to the individual command levels has not only created a logical progression, but also defined small targets that are easy to approach and, in case of doubt, easy to replace.

Outlook

The AI is far from finished. For example, soft and hard factors such as morale, fatigue, supplies, ammunition and movement points still need to be included in order to make better decisions. However, I’m already quite happy with this simple variant, as it actually allows initial gameplay and makes the game “playable” in terms of gameplay effects for the first time ever.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *