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

void Attacking::Execute(SoccerTeam* team)
{
//if this team is no longer in control change states
if (!team->InControl())
{
team->ChangeState(team, Defending::Instance()); return;
}
//calculate the best position for any supporting attacker to move to
team->DetermineBestSupportingPosition();
}
That’s enough about the SoccerTeam class for now. Let’s take a look at how
the players are implemented.
Field Players
The field players are the guys who run around the field, passing the ball
and taking shots at their opponent’s goal. There are two types of field play-
ers: attackers and defenders. Both are instantiated as objects of the same
class,
FieldPlayer, but an enumerated member variable is set to determine
their role. Defenders mainly stay to the rear of the field protecting the
home goal, and attackers are given more freedom to move up the field,
toward the opponent’s goal.
Field Player Motion
A field player has a velocity-aligned heading and utilizes steering behav-
iors to move into position and to chase the ball. When motionless, a field
player rotates to face the ball. It doesn’t do this to perceive the ball, as it
always knows where the ball is (from querying the game world directly),
but because it has a better chance of passing immediately after an intercept
and because it looks better to our human eyes. Remember, this is about
creating the illusion of intelligence, and not hard-core AI as studied by
academics. Most human players will assume that if a computer player is
tracking the ball with its head, then it must be “watching” the ball. By cre
-
ating players that always track the ball we also ensure nothing odd happens
— like a player receiving and controlling the ball when it’s facing in the
opposite direction. That sort of thing would break the illusion, leaving a
human player feeling cheated and dissatisfied. I’m sure you have experi
-
enced this feeling yourself when playing games. It only takes a small
dodgy-looking event to damage a players confidence in the AI.
The field players move around the pitch utilizing the arrive and seek
behaviors to steer toward the steering behavior target or using pursuit to
chase the ball’s predicted future position. Any required steering behavior is
typically switched on in a state’s
Enter method and switched off in its Exit
method, which brings me nicely around to discussing the states a field
player can occupy.
Sports Simulation — Simple Soccer | 155
Designing the AI
Field Player States
In real life, soccer players must learn a set of skills in order to control the
ball well enough to coordinate team play and to score goals. They do this
by endless hours of practice and repetition of the same moves. Simple Soc
-
cer players don’t have to practice, but they do rely on you, the programmer,
to bestow them with the skills they need to play well.
A field players finite state machine utilizes eight states:
n
GlobalPlayerState
n
Wait
n
ReceiveBall
n
KickBall
n
Dribble
n
ChaseBall
n
ReturnToHomeRegion
n
SupportAttacker
State changes are made either in the logic of a state itself or when a player
is sent a message by another player (to receive a ball for example).
GlobalPlayerState
The main purpose of the field players global state is to be a message
router. Although much of a players behavior is implemented by the logic
contained within each of its states, it’s also desirable to implement some
form of player cooperation via a communication system. A good example
of this is when a supporting player finds itself in an advantageous position
and requests a pass from a teammate. To facilitate player communication,
the trusty messaging system you learned about in Chapter 2 is implemented.
There are five messages used in Simple Soccer. They are:
n
Msg_SupportAttacker
n
Msg_GoHome
n
Msg_ReceiveBall
n
Msg_PassToMe
n
Msg_Wait
The messages are enumerated in the file SoccerMessages.h. Let’s take a
look at how each of them is processed.
bool GlobalPlayerState::OnMessage(FieldPlayer* player, const Telegram& telegram)
{
switch(telegram.Msg)
{
case Msg_ReceiveBall:
{
//set the target
player->Steering()->SetTarget(*(Vector2D*)(telegram.ExtraInfo));
//change state
player->ChangeState(player, ReceiveBall::Instance());
156 | Chapter 4
Designing the AI
return true;
}
break;
Msg_ReceiveBall is sent to the receiving player when a pass is made. The
position of the pass target is stored as the receiver’s steering behavior tar
-
get. The receiving player acknowledges the message by changing state to
ReceiveBall.
case Msg_SupportAttacker:
{
//if already supporting just return
if (player->CurrentState() == SupportAttacker::Instance()) return true;
//set the target to be the best supporting position
player->Steering()->SetTarget(player->Team()->GetSupportSpot());
//change the state
player->ChangeState(player, SupportAttacker::Instance());
return true;
}
break;
Msg_SupportAttacker is sent by the controlling player to request support as
it attempts to move the ball farther up the field. When a player receives this
message, it sets its steering target to the best supporting spot and then
changes state to SupportAttacker.
case Msg_GoHome:
{
player->SetDefaultHomeRegion();
player->ChangeState(player, ReturnToHomeRegion::Instance());
return true;
}
break;
When a player receives this message, it moves back to its home region. It’s
frequently broadcast by the goalkeepers prior to goal kicks and by the
“pitch” to move the players back into their kick-off positions between
goals.
case Msg_Wait:
{
//change the state
player->ChangeState(player, Wait::Instance());
return true;
}
break;
Sports Simulation — Simple Soccer
| 157
Designing the AI

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