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

want it to wave its arms in the air and scream “Arghhhhhhh!” When it
finally escapes and changes state to Patrol, you may want it to emit a sigh,
wipe its forehead, and say “Phew!” These are actions that only occur when
the RunAway state is entered or exited and not during the usual update
step. Consequently, this additional functionality must ideally be built into
your state machine architecture. To do this within the framework of a
switch or if-then architecture would be accompanied by lots of teeth grind
-
ing and waves of nausea, and produce very ugly code indeed.
State Transition Tables
A better mechanism for organizing states and affecting state transitions is a
state transition table. This is just what it says it is: a table of conditions and
the states those conditions lead to. Table 2.1 shows an example of the map
-
ping for the states and conditions shown in the previous example.
Table 2.1. A simple state transition table
Current State Condition State Transition
Runaway Safe Patrol
Attack WeakerThanEnemy RunAway
Patrol Threatened AND StrongerThanEnemy Attack
Patrol Threatened AND WeakerThanEnemy RunAway
This table can be queried by an agent at regular intervals, enabling it to
make any necessary state transitions based on the stimulus it receives from
the game environment. Each state can be modeled as a separate object or
function existing external to the agent, providing a clean and flexible archi
-
tecture. One that is much less prone to spaghettification than the
if-then/switch approach discussed in the previous section.
Someone once told me a vivid and silly visualization can help people to
understand an abstract concept. Let’s see if it works…
Imagine a robot kitten. It’s shiny yet cute, and has wire for whiskers and
a slot in its stomach where cartridges — analogous to its states — can be
plugged in. Each of these cartridges is programmed with logic, enabling
the kitten to perform a specific set of actions. Each set of actions encodes a
different behavior; for example, “play with string,” “eat fish,” or “poo on
carpet.” Without a cartridge stuffed inside its belly the kitten is an inani
-
mate metallic sculpture, only able to sit there and look cute… in a Metal
Mickey kind of way.
The kitten is very dexterous and has the ability to autonomously
exchange its cartridge for another if instructed to do so. By providing the
rules that dictate when a cartridge should be switched, it’s possible to string
together sequences of cartridge insertions permitting the creation of all
State-Driven Agent Design | 47
Implementing a Finite State Machine

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