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 Miner::Update()
{
m_iThirst += 1;
if (m_pCurrentState)
{
m_pCurrentState->Execute(this);
}
}
Now that you’ve seen how the Miner class operates, let’s take a look at
each of the states a miner can find itself in.
The Miner States
The gold miner will be able to enter one of four states. Here are the names
of those states followed by a description of the actions and state transitions
that occur within those states:
n
EnterMineAndDigForNugget: If the miner is not located at the
gold mine, he changes location. If already at the gold mine, he digs
for nuggets of gold. When his pockets are full, Bob changes state to
VisitBankAndDepositGold, and if while digging he finds himself
thirsty, he will stop and change state to QuenchThirst.
n
VisitBankAndDepositGold: In this state the miner will walk to the
bank and deposit any nuggets he is carrying. If he then considers
himself wealthy enough, he will change state to GoHomeAnd-
SleepTilRested. Otherwise he will change state to EnterMine-
AndDigForNugget.
n
GoHomeAndSleepTilRested: In this state the miner will return to
his shack and sleep until his fatigue level drops below an acceptable
level. He will then change state to EnterMineAndDigForNugget.
n
QuenchThirst: If at any time the miner feels thirsty (diggin’ for
gold is thusty work, don’t ya know), he changes to this state and vis
-
its the saloon in order to buy a whiskey. When his thirst is quenched,
he changes state to EnterMineAndDigForNugget.
Sometimes it’s hard to follow the flow of the state logic from reading a text
description like this, so it’s often helpful to pick up pen and paper and draw
a state transition diagram for your game agents. Figure 2.2 shows the state
transition diagram for the gold miner. The bubbles represent the individual
states and the lines between them the available transitions.
A diagram like this is better on the eyes and can make it much easier to
spot any errors in the logic flow.
54 | Chapter 2
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