Creating decision-making AI with FSM
In Chapter 2, Finite State Machines, we saw how to implement a simple FSM. In this section, we are using the same technique, but will apply it to the more complex scenario of this demo.
First, we need an FSM plan. We are interested only in connecting the FSM to the existing game for this demo, so we will keep it simple. The FSM for our tank is composed of just two states – patrolling and shooting.
The plan is nice and straightforward:
- The AI tank starts in the
Patrolstate and wanders around the previously defined patrolling points. - Then, if the players get in range, the tank switches to the
Attackstate. - In the
Attackstate, the tank turns toward the player and starts shooting at it. - Finally, if we are in the
Attackstate and the players leave the AI's range, the tank will return to thePatrolstate.
Figure 12.8 – The simple FSM for the enemy tanks
For the implementation...