AI for Rush for Glory

It’s been about 3 weeks since our playable demo of Rush for Glory is ready. It has 2 main mechanics. Create and place towers, and shoot at enemies. I improved some features of the engine during the demo development process. AI was one of these features.
Currently, the AI includes pathfinding, mission-based decision making, and Hierarchical State-Driven Agent Behavior from “Programming Game AI by Example by Mat Buckland“. Now I try to explain it briefly to give you some concepts.

States
Each state is a class that inherits from the standard state class interface. each agent in the game has a primary state. in addition, agents can also have a parallel state to do some parallel operations. we can create a state and replace it with the current state slot of the agent. These states will control the behavior of the agent. In other words, each agent is similar to an Atari game consul and each state is similar to a cartridge, by replacing states, the behavior of the agent will be changed.
in Buckland’s design, each state can be atomic or composite. atomic states have no sub-states and just do simple operations. for instance, pressing a button in the game to open a door. but composite states have a list of sub-states. depending on the circumstances in the game, composite states may create sub-states and add them to the list. substates will process consecutively. each sub-state will removed when the operation is completed.
For example, when an agent is traversing a path that is closed by a door, the agent can create a sub-state that defines a new mission, “Pressing a key to open the door” and push it to the list. this new sub-state may include some more sub-states to find a path to the key, push the key, etc. This process of decomposing and satisfying states continues until the entire hierarchy has been traversed and the agent arrives at the goal.

here is a shot to describe traversing a path which closed by a door :

Hierarchical State-Driven Agent Behavior

On www.ai-junkie.com you can find more books, tutorials, source codes, and …

Missions
In this game, each Mission is a simple data structure containing position, time, behavior flag, etc. The flag of the mission can be a combination of different behaviors. the brain of the agent uses this data structure to make a decision depending on the mission flag. there is a queue of missions in the brain of the agent. we can push new missions to the brain and the brain will perform each mission respectively.
here is a simple sample that I used in one of our game prototypes to set the enemy’s behavior. the game is in the tower defense genre and enemies try to reach the base which is placed in the center of the game world. also, enemies can attack the towers and when they arrive at the base, they must remove themselves from the game :

Mission m;
m.flag      = MISSION_GOTO_POSITION | MISSION_KILL_ENEMY;
m.pos       = float3(0, 0, 0);
m.status    = Mission::MS_ACTIVE;
pAgent->GetBrain()->AddMission(m);

m.flag = MISSION_SUICIDE;
pAgent->GetBrain()->AddMission(m);

In this example, I added two missions to the brain of the enemy agent. In the first mission, the flag is the combination of two behaviors including “go to specified position” and “kill the enemies”. the brain creates one state for agents to move to the specified position and set that as the main state. also, the brain creates the other state to find and kill enemies and set that as a parallel state. finally, if the first mission is completed the brain will process the next mission. the flag of the next mission is “suicide” which causes the brain to remove his agent from the game.