O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2
State-Driven Agent Design
F
inite state machines, or FSMs as they are usually referred to, have for
many years been the AI coders instrument of choice to imbue a game
agent with the illusion of intelligence. You will find FSMs of one kind or
another in just about every game to hit the shelves since the early days of
video games, and despite the increasing popularity of more esoteric agent
architectures, they are going to be around for a long time to come. Here are
just some of the reasons why:
They are quick and simple to code. There are many ways of program-
ming a finite state machine and almost all of them are reasonably simple to
implement. You’ll see several alternatives described in this chapter together
with the pros and cons of using them.
They are easy to debug. Because a game agent’s behavior is broken
down into easily manageable chunks, if an agent starts acting strangely, it
can be debugged by adding tracer code to each state. In this way, the AI
programmer can easily follow the sequence of events that precedes the
buggy behavior and take action accordingly.
They have little computational overhead. Finite state machines use
hardly any precious processor time because they essentially follow hard-
coded rules. There is no real “thinking” involved beyond the if-this-then-
that sort of thought process.
They are intuitive. It’s human nature to think about things as being in
one state or another and we often refer to ourselves as being in such and
such a state. How many times have you “got yourself into a state” or found
yourself in “the right state of mind”? Humans don’t really work like finite
state machines of course, but sometimes we find it useful to think of our
behavior in this way. Similarly, it is fairly easy to break down a game
agent’s behavior into a number of states and to create the rules required for
manipulating them. For the same reason, finite state machines also make it
easy for you to discuss the design of your AI with non-programmers (with
game producers and level designers for example), providing improved
communication and exchange of ideas.
They are flexible. A game agent’s finite state machine can easily be
adjusted and tweaked by the programmer to provide the behavior required
by the game designer. It’s also a simple matter to expand the scope of an
agent’s behavior by adding new states and rules. In addition, as your AI
43

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required