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

The player and goalkeeper objects are similar to the game agents you’ve
already encountered in this book. I’ll be describing them in detail very
shortly, but first I’d like to show you how the soccer pitch, goals, and soc
-
cer ball are implemented. This should give you a feel for the environment
the game agents occupy and then I can move on to the nitty-gritty of the AI
itself.
The Soccer Pitch
The soccer pitch is a rectangular playing area enclosed by walls. At each of
the short ends of the pitch, there is a goal centrally positioned. See Figure
4.1. The small circle at the center of the playing area is referred to as the
center-spot. The ball is positioned on the center-spot prior to the start of the
match. When a goal is scored both teams relinquish control of the ball and
it’s repositioned on the center-spot ready for another “kick-off.” (For those
soccer fans among my readers, please forgive my elaborate descriptions,
but if I didn’t go to this trouble I just know the moment this book is
released I’ll receive a dozen emails from the inhabitants of some hidden
Himalayan valley wondering what on earth I’m talking about!)
Sports Simulation — Simple Soccer | 135
The Simple Soccer Environment and Rules
Figure 4.2. The Simple Soccer high-level object hierarchy
The playing area is encapsulated by the class SoccerPitch. A single
instance of this class is instantiated in main.cpp. The
SoccerPitch object
owns instances of
SoccerTeam, SoccerBall, and Goal objects.
Here’s the class declaration:
class SoccerPitch
{
public:
SoccerBall* m_pBall;
SoccerTeam* m_pRedTeam;
SoccerTeam* m_pBlueTeam;
Goal* m_pRedGoal;
Goal* m_pBlueGoal;
These first few members are self explanatory and I’ll be describing the rel
-
evant classes in detail in a few pages.
//container for the boundary walls
std::vector<Wall2D> m_vecWalls;
The pitch boundaries in the Simple Soccer environment are represented by
Wall2Ds. Walls are described by a line segment with two endpoints and a
normal to the line segment representing the facing direction. You may
remember them from the wall avoidance steering behavior description.
//defines the dimensions of the playing area
Region* m_pPlayingArea;
A Region object is used to describe the dimensions of the soccer pitch. A
Region stores the top left, right bottom, and center positions of the declared
area, and also an identifying number (ID).
std::vector<Region*> m_Regions;
Soccer players have to know where they are on the soccer pitch and
although their x, y coordinates give a very specific position, it’s also useful
to split the pitch up into regions players can make use of to implement
strategies. To facilitate this, the pitch is divided into eighteen areas as
shown in Figure 4.3.
At the beginning of a game, each player is assigned a region to be its
home region. This will be the region it returns to after a goal is scored or
when it has finished making a play with the ball. A players home region
may vary during a game depending on the team strategy. For example,
when attacking, it’s advantageous for a team to occupy positions farther
forward on the field (upfield) than when defending.
136 | Chapter 4
The Simple Soccer Environment and Rules
bool m_bGameOn;
Teams can query this value to see if the game is in play or not. (The game
is not on if a goal has just been scored and all the players are returning to
their kick-off positions.)
bool m_bGoalKeeperHasBall;
This value is set to true if either team’s goalkeeper has the ball. Players can
query this value to help them select an appropriate behavior. For example,
if a goalkeeper has possession of the ball, a nearby opponent will not
attempt to kick it.
/* EXTRANEOUS DETAIL OMITTED */
public:
SoccerPitch(int cxClient, int cyClient);
~SoccerPitch();
void Update();
bool Render();
/* EXTRANEOUS DETAIL OMITTED */
};
The SoccerPitch::Update and SoccerPitch::Render functions are at the top
of the update and render hierarchy. Each update step, these methods are
called from within the main game loop and, in turn, the appropriate
Render
and Update methods of every other game entity is called.
Sports Simulation — Simple Soccer | 137
The Simple Soccer Environment and Rules
Figure 4.3. The pitch divided into regions

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