Of course, the behavior of a game agent is usually much more complex
than a lightbulb (thank goodness!). Here are some examples of how finite
state machines have been used in games.
n
The ghosts’ behavior in PacMan is implemented as a finite state
machine. There is one Evade state, which is the same for all ghosts,
and then each ghost has its own Chase state, the actions of which are
implemented differently for each ghost. The input of the player eat

ing one of the power pills is the condition for the transition from
Chase to Evade. The input of a timer running down is the condition
for the transition from Evade to Chase.
n
Quakestyle bots are implemented as finite state machines. They
have states such as FindArmor, FindHealth, SeekCover, and Run

Away. Even the weapons in Quake implement their own mini finite
state machines. For example, a rocket may implement states such as
Move, TouchObject, and Die.
n
Players in sports simulations such as the soccer game FIFA2002 are
implemented as state machines. They have states such as Strike,
Dribble, ChaseBall, and MarkPlayer. In addition, the teams them
selves are often implemented as FSMs and can have states such as
KickOff, Defend, or WalkOutOnField.
n
The NPCs (nonplayer characters) in RTSs (realtime strategy
games) such as Warcraft make use of finite state machines. They
have states such as MoveToPosition, Patrol, and FollowPath.
Implementing a Finite State Machine
There are a number of ways of implementing finite state machines. A naive
approach is to use a series of ifthen statements or the slightly tidier mecha

nism of a switch statement. Using a switch with an enumerated type to
represent the states looks something like this:
enum StateType{RunAway, Patrol, Attack};
void Agent::UpdateState(StateType CurrentState)
{
switch(CurrentState)
{
case state_RunAway:
EvadeEnemy();
if (Safe())
{
ChangeState(state_Patrol);
}
break;
StateDriven Agent Design  45
Implementing a Finite State Machine