My recent efforts on developing the combat system has been the most complicated part of the AI code so far, but fortunately I had a good idea of how I wanted the characters to behave.
Enemies will spawn at night on either the left or right edge of the map and will start walking towards the center. On the way, they will look for and attack friendly units. They will have a preference for soldiers over villagers. I wanted the enemies to move in a group as much as possible, but then when they encounter a group of friendly units, to spread out and attack instead of them all attacking the same character. As night falls, friendly soldiers will run to the edge of the town and form a group to protect the town. They will try and hold their ground but might venture out slightly if threatened, or if they detect an enemy inside the town.
To make this work without putting to much strain on the CPU and frame rate, I decided to split the logic into three threads.
- The first thread will constantly search close to characters for possible targets to attack, and will coordinate this with surrounding allies to make sure targets are spread out as much as possible.
- The second thread is responsible for deciding the character’s actions based on whether it has a target, its position in the world, whether it is being attacked or whether it should attack. Outcomes of this thread could be, for example, for the character to run to a location. The logic involved in the first two threads is very intense and keeps track of a variety of parameters.
- Finally, the third thread is responsible for keeping the outcome from the first two in sync, together with the main thread and also with the physics engine.
Units may randomly block an incoming attack. They may interrupt their own attack to block. Archers can attack from a distance, but they cannot block any incoming attacks, and no other units can block an archer attack.
New building: Hospital
I’ve added a new hospital building. Having this building will allow friendly units to regenerate full health every day, instead of regenerating just one health daily. This will allow your soldiers to stay alive much longer. Upgrading the hospital will also give more health to all units. But it’s up to the player to decide whether they want to consume their building limit with a hospital or whether to keep recruiting soldiers at a higher cost. It’s a balancing act!