Finite State Machines – Video Game Application
One of the more simplest forms of AI logic in games development is built on a set of if then else conditional statements. Although this approach is common and useful for small or indie game AI implementations, it is rather challenging to mimic realistic behaviour.
In certain scenarios, were the AI agent has a predetermined finite number of actions, state machines can be used for behavioural switching. There are various applications were these can be used, for instance, on a guard patrolling a military base gate. The guard can have the following states:
Idle
Patrol
Search
Attack
Death
The initial state for the guard can be idle. After thirty seconds, the guard will start patrolling an area predefined by way points. On each way point, the guard will idle for thirty seconds. The guard will attack if an enemy is in range, and change state to attack. Once the enemy is not within range, the state will change to searching in order to introduce more realism into the scenario, instead of just going back to idle, as if the guard forgot that they were attacking an enemy. The following finite state diagram illustrates how the transitions occur in this scenario:
Another practical example for a finite state machine is the behavior of the ghosts in Pacman (Bourg and Seemann, 2004). The ghosts will chase the player around until a ghost eats the player or the power up is picked up by the player. In the latter, the ghosts will go into a panic state. Once in this state, the player is able to eat the ghosts, were they will change their state to captured, and head back to their starting point.
From a programmatic perspective, opting for a finite state machine in this scenario for game AI will have less overhead. Furthermore, they are easier to implement when compared to other AI techniques. The disadvantage when using FSM as an approach is that it can become complex to transition between states for more believable AI, such as companions and NPC interaction. However, it is possible to create sub-FSMs into each state to manage logic within a particular state, in order to have a system made up several minor FSMs. Each extra state variable can add 2^n extra states. There is no limit using this approach for game AI, however keep in mind that the more states and transitions, the more complex it will be to manage.
References
Bourg, D. and Seemann, G. (2004). AI for game developers. Beijing: O’Reilly, pp.165-167.
Cool post mate, really enjoyed reading it!