Finite State Machines have been called the cornerstone of game-AI by many and for good reason. They offer an easy way to take a relatively complex target behaviour and emulate using states and transitions with great results. A simple example follows which demonstrates some basic behaviour that could be found in a game enemy.
Figure 1 - Basic Implementation of FSM
As you can see the model uses particular states and transitions and simply controls the flow of the program to emulate the behaviour. The advantages in using FSM are manifold. First, they are relatively simple to implement which in a time dependent scenario can be a very importation advantage. Due to its nature it also lends itself to being represented by a diagram such as the one above which can make the planning or the debugging of one a more visual matter which could be tackled by non-technical staff. Finally, another advantage as that due to its simple nature it can be extended in many ways to make up for its innate disadvantages or serve a particular purpose in the project.
However, it’s simple nature can be a burden especially when the target behaviour is quite complex or perhaps not linear. In those cases the implementation can get overly large and messy. Another potential flaw is that it can become easily predictable as the transitions tend to be abrupt enough that a user would notice.
As mentioned before FSM’s lend themselves to customisation. Some examples of that would include storing the states in a stack which can make the flow better as it avoids conflicting transition statements. Another would be to attempt to make the transitions less abrupt by having multiple variables governing which state should fire. By going down that road we soon leave FSM’s and enter the land of Utility AI which is a subject for another blog post.
Bevilacqua, Fernando (2013). Finite-State Machines: Theory and Implementation.
Available from: https://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867
Mignano, Marco (2016). Finite State Machines For Game Developers.
Available from: http://gamedevelopertips.com/finite-state-machine-game-developers/