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

those opponents sensed within recent memory. Here’s what the method
looks like:
std::list<Raven_Bot*>
Raven_SensoryMemory::GetListOfRecentlySensedOpponents()const
{
//this will store all the opponents the bot can remember
std::list<Raven_Bot*> opponents;
double CurrentTime = Clock->GetCurrentTime();
MemoryMap::const_iterator curRecord = m_MemoryMap.begin();
for (curRecord; curRecord!=m_MemoryMap.end(); ++curRecord)
{
//if this bot has been updated in the memory recently, add to list
if ( (CurrentTime - curRecord->second.dTimeLastSensed) <= m_dMemorySpan)
{
opponents.push_back(curRecord->first);
}
}
return opponents;
}
As you can see, if a particular record has not been updated within the last
m_dMemorySpan seconds it is not added to the list, and the bot effectively for-
gets all about that opponent. This ensures that a bot will remember an
opponent for a short while after it has been sensed even if it goes out of
view.
Target Selection
The class that handles target selection is called Raven_TargetingSystem.
Each
Raven_Bot owns an instance of this class and delegates target selection
to it. Its declaration looks like this:
class Raven_TargetingSystem
{
private:
//the owner of this system
Raven_Bot* m_pOwner;
//the current target (this will be null if there is no target assigned)
Raven_Bot* m_pCurrentTarget;
public:
Raven_TargetingSystem(Raven_Bot* owner);
//each time this method is called the opponents in the owner's sensory
//memory are examined and the closest is assigned to m_pCurrentTarget.
//if there are no opponents that have had their memory records updated
Raven: An Overview
| 321
AI Implementation
//within the memory span of the owner then the current target is set
//to zero
void Update();
//returns true if there is a currently assigned target
bool isTargetPresent()const;
//returns true if the target is within the field of view of the owner
bool isTargetWithinFOV()const;
//returns true if there is unobstructed line of sight between the target
//and the owner
bool isTargetShootable()const;
//returns the position where the target was last seen. Throws an exception if
//there is no target currently assigned
Vector2D GetLastRecordedPosition()const;
//returns the amount of time the target has been in the field of view
double GetTimeTargetHasBeenVisible()const;
//returns the amount of time the target has been out of view
double GetTimeTargetHasBeenOutOfView()const;
//returns a pointer to the target. null if no target current.
Raven_Bot* GetTarget()const;
//sets the target pointer to null
void ClearTarget();
};
At a specified interval, the targeting system Update method is called from
Raven_Bot::Update. Update obtains a list of recently perceived opponents
from the sensory memory and selects one of them to be the current target.
The selection criterion used by the Raven bots is very simple: The clos
-
est opponent is assigned to be the current target. This works adequately for
Raven but your game may demand alternative or more rigorous selection
criteria. For instance, you might prefer to design a selection method that
incorporates one or more of the following:
n
An opponent’s angle of deviation from the bot’s heading (in other
words, he’s right in front of you)
n
An opponent’s facing direction (he can’t see you — sneak attack!)
n
The range of the weapon the opponent is carrying (he can’t get me)
n
The range of the weapon the bot is carrying (I can’t get him)
n
Any power-ups the opponents or bot may be using (how tough is
he?)
n
How long an opponent has been visible (he probably knows about
me if I know about him)
322 | Chapter 7
AI Implementation

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