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

Good, eh? The remainder of this chapter will demonstrate how agents can
be given the ability to handle messages like this. But before we can figure
out how to transmit them and handle them, the first thing to do is to define
exactly what a message is.
The Telegram Structure
A message is simply an enumerated type. This could be just about any
-
thing. You could have agents sending messages like
Msg_ReturnToBase,
Msg_MoveToPosition,orMsg_HelpNeeded. Additional information also needs
to be packaged along with the message. For example, we should record
information about who sent it, who the recipient is, what the actual mes
-
sage is, a time stamp, and so forth. To do this, all the relevant information
is kept together in a structure called
Telegram. The code is shown below.
Examine each member variable and get a feel for what sort of information
the game agents will be passing around.
struct Telegram
{
//the entity that sent this telegram
int Sender;
//the entity that is to receive this telegram
int Receiver;
//the message itself. These are all enumerated in the file
//"MessageTypes.h"
int Msg;
//messages can be dispatched immediately or delayed for a specified amount
//of time. If a delay is necessary, this field is stamped with the time
//the message should be dispatched.
double DispatchTime;
//any additional information that may accompany the message
void* ExtraInfo;
/* CONSTRUCTORS OMITTED */
};
The Telegram structure should be reusable, but because it’s impossible to
know in advance what sort of additional information future game designs
will need to pass in a message, a void pointer
ExtraInfo is provided. This
can be used to pass any amount of additional information between charac
-
ters. For example, if a platoon leader sends the message
Msg_MoveToPosi
-
tion
to all his men, ExtraInfo can be used to store the coordinates of that
position.
70 | Chapter 2
Adding Messaging Capabilities to Your FSM

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