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

Okay, let’s see how everything fits together by examining the complete
code for one of the miner states.
The EnterMineAndDigForNugget State
In this state the miner should change location to be at the gold mine.
Once at the gold mine he should dig for gold until his pockets are full,
when he should change state to VisitBankAndDepositNugget.Ifthe
miner gets thirsty while digging he should change state to
QuenchThirst.
Because concrete states simply implement the interface defined in the vir
-
tual base class
State, their declarations are very straightforward:
class EnterMineAndDigForNugget : public State
{
private:
EnterMineAndDigForNugget(){}
/* copy ctor and assignment op omitted */
public:
//this is a singleton
static EnterMineAndDigForNugget* Instance();
virtual void Enter(Miner* pMiner);
virtual void Execute(Miner* pMiner);
virtual void Exit(Miner* pMiner);
};
As you can see, it’s just a formality. Let’s take a look at each of the meth
-
ods in turn.
EnterMineAndDigForNugget::Enter
The code for the
Enter method of EnterMineAndDigForNugget is as follows:
void EnterMineAndDigForNugget::Enter(Miner* pMiner)
{
//if the miner is not already located at the gold mine, he must
//change location to the gold mine
if (pMiner->Location() != goldmine)
{
cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": "
<< "Walkin' to the gold mine";
pMiner->ChangeLocation(goldmine);
}
}
This method is called when a miner first enters the EnterMineAndDig
-
ForNugget state. It ensures that the gold miner is located at the gold mine.
60 | Chapter 2
The West World Project
An agent stores its location as an enumerated type and the ChangeLocation
method changes this value to switch locations.
EnterMineAndDigForNugget::Execute
The
Execute method is a little more complicated and contains logic that can
change a miners state. (Don’t forget that
Execute is the method called each
update step from
Miner::Update.)
void EnterMineAndDigForNugget::Execute(Miner* pMiner)
{
//the miner digs for gold until he is carrying in excess of MaxNuggets.
//If he gets thirsty during his digging he stops work and
//changes state to go to the saloon for a whiskey.
pMiner->AddToGoldCarried(1);
//diggin' is hard work
pMiner->IncreaseFatigue();
cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": "
<< "Pickin' up a nugget";
//if enough gold mined, go and put it in the bank
if (pMiner->PocketsFull())
{
pMiner->ChangeState(VisitBankAndDepositGold::Instance());
}
//if thirsty go and get a whiskey
if (pMiner->Thirsty())
{
pMiner->ChangeState(QuenchThirst::Instance());
}
}
Note here how the Miner::ChangeState method is called using
QuenchThirst’s or VisitBankAndDepositGold’s Instance member, which
provides a pointer to the unique instance of that class.
EnterMineAndDigForNugget::Exit
The
Exit method of EnterMineAndDigForNugget outputs a message telling us
that the gold miner is leaving the mine.
void EnterMineAndDigForNugget::Exit(Miner* pMiner)
{
cout << "\n" << GetNameOfEntity(pMiner->ID()) << ": "
<< "Ah'm leavin' the gold mine with mah pockets full o' sweet gold";
}
I hope an examination of the preceding three methods helps clear up any
confusion you may have been experiencing and that you can now see how
each state is able to modify the behavior of an agent or effect a transition
into another state. You may find it useful at this stage to load up the
WestWorld1 project into your IDE and scan the code. In particular, check
State-Driven Agent Design | 61
The West World Project

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