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

Not all the methods of every class are shown, but it gives a good idea of
the design. The majority of the methods that are listed for
PlayerBase and
SoccerTeam comprise the interface a players state machine uses to route its
AI logic. (I’ve omitted each method’s parameters to permit me to fit the
diagram on one page!)
Notice how a
SoccerTeam also owns a StateMachine, giving a team the
ability to change its behavior depending on the current state of play. Imple
-
menting AI at the team level in addition to the player level creates what is
known as tiered AI. This type of AI is used in all sorts of computer games.
You will often find tiered AI in real-time strategy (RTS) games where the
enemy AI is commonly implemented in several layers at, say, the unit,
troop, and commander levels.
Notice also how the players and their teams have the ability to send
messages. Messages may be passed from player to player (including goal
-
keepers) or from soccer team to player. In this demo players do not pass
messages to their team. (Although there is no reason why they couldn’t. If
you have a good reason for your players messaging their team, go ahead
and do it.) All messages dispatched to field players or goalkeepers are han-
dled via each class’s respective global state, as you shall see later on in the
chapter.
Since a players team state dictates to some extent how the player should
behave, your journey into the guts of the AI of Simple Soccer is probably
best commenced with a description of the
SoccerTeam class. After you
understand what makes a team tick, I’ll move on to describe how the play-
ers and goalkeepers work their soccer magic.
The SoccerTeam Class
The SoccerTeam class owns instances of the players that comprise the soc
-
cer team. It has pointers to the soccer pitch, the opposing team, the team’s
home goal, and its opponent’s goal. Additionally, it has pointers to the
“key” players on the pitch. Individual players can query their soccer team
and use this information in their state machine logic.
First of all, I’ll describe the roles of these key players and then move on
to discuss the various states a Simple Soccer team utilizes. Here’s how the
key player pointers are declared in the class prototype:
class SoccerTeam
{
private:
/* EXTRANEOUS DETAIL OMITTED */
//pointers to "key" players
PlayerBase* m_pReceivingPlayer;
PlayerBase* m_pPlayerClosestToBall;
PlayerBase* m_pControllingPlayer;
Sports Simulation — Simple Soccer
| 145
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