When we talk about AI for games, we are in fact talking about all decision making
units of logic.
It's improtant to understand that decision making, or AI, isn't suited only for
agents in the game - it can be and already is widely used for animation systems,
events happening in game world, or even to control sounds and music.
You can notice update function - this is what controls this enemy simple AI.
You might not have noticed it but you have used Finite State Machine for this
problem, hardcore way thought.
Btw. That snippet was really hard to read, right? Yeah, that's how our naive
"simple" AI implementations usually end up growing in complexity.
We can do better, let me refactor it to prove it to you:
Now we can clearly see what is the actual behavior of this AI:
wait few turns
change direction
move forward for few turns
And we have got rid of a bug where our previous magic didn't actually do what we
aimed for the way we wanted it to do. This is the simplest state machine we have
learned to make, most likely at this point you're already making them this way.
Now imagine we get more states to handle, like shooting, taking cover when on
low health, finding ammo when it ends - number of state changes and branching
grows exponentially with number of states and our code starts to look more like
a spaghetti than code with clear and easily understandable intent. Have you
encountered that frustration yet?
This is the exact reason why AI systems have been invented in first place - to
simplify readability of and iteration over AI logic.
In next chapters we will learn about AI systems and gradually work our way out
from naive AI logic to more managable solutions.