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

n
How much damage an opponent has inflicted on the bot in the last
few seconds (that makes me mad!)
n
How many times an opponent has been killed by the bot (ha, ha!)
n
How many times the bot has been killed by an opponent (meanie!)
Weapon Handling
The Raven bots use the Raven_WeaponSystem class to manage all operations
specific to weapons and their deployment. This class owns a
std::map of
instances of weapons, keyed into by their type, a pointer to the currently
held weapon, and variables denoting the aiming accuracy of the bot and the
bot’s reaction time. These last two variables are used by the weapon aiming
logic to prevent a bot from hitting its target 100 percent of the time or
shooting at an opponent the instant it comes into view. This is important
because if an AI performs too well most players will quickly become frus
-
trated and stop playing the game. These values allow the game testers to
adjust the bots’ skill level until they put up a tough battle, but lose more
often than win. This, for most players, will provide the most enjoyable
gaming experience.
In addition to the member variables, the class has methods for adding
weapons, changing the current weapon, aiming and shooting the current
weapon, and selecting the best weapon for the current game state.
Here’s the declaration for you to look over.
class Raven_WeaponSystem
{
private:
//a map of weapon instances keyed into by type
typedef std::map<int, Raven_Weapon*> WeaponMap;
private:
Raven_Bot* m_pOwner;
//pointers to the weapons the bot is carrying (a bot may only carry one
//instance of each weapon)
WeaponMap m_WeaponMap;
//a pointer to the weapon the bot is currently holding
Raven_Weapon* m_pCurrentWeapon;
//this is the minimum amount of time a bot needs to see an opponent before
//it can react to it. This variable is used to prevent a bot from shooting at
//an opponent the instant it becomes visible.
double m_dReactionTime;
Raven: An Overview | 323
AI Implementation
//each time the current weapon is fired a certain amount of random noise is
//added to the angle of the shot. This prevents the bots from hitting
//their opponent 100% of the time. The lower this value the more accurate
//a bot's aim will be. Recommended values are between 0 and 0.2 (the value
//represents the max deviation in radians that can be added to each shot).
double m_dAimAccuracy;
//the amount of time a bot will continue aiming at the position of the target
//even if the target disappears from view.
double m_dAimPersistance;
//predicts where the target will be by the time it takes the current weapon's
//projectile type to reach it. Used by TakeAimAndShoot
Vector2D PredictFuturePositionOfTarget()const;
//adds a random deviation to the firing angle not greater than m_dAimAccuracy
//rads
void AddNoiseToAim(Vector2D& AimingPos)const;
public:
Raven_WeaponSystem(Raven_Bot* owner,
double ReactionTime,
double AimAccuracy,
double AimPersistance);
~Raven_WeaponSystem();
//sets up the weapon map with just one weapon: the blaster
void Initialize();
//this method aims the bot's current weapon at the target (if there is a
//target) and, if aimed correctly, fires a round. (Called each update step
//from Raven_Bot::Update)
void TakeAimAndShoot()const;
//this method determines the most appropriate weapon to use given the current
//game state. (Called every n update steps from Raven_Bot::Update)
void SelectWeapon();
//this will add a weapon of the specified type to the bot's inventory.
//If the bot already has a weapon of this type only the ammo is added.
//(called by the weapon giver-triggers to give a bot a weapon)
void AddWeapon(unsigned int weapon_type);
//changes the current weapon to one of the specified type (provided that type
//is in the bot's possession)
void ChangeWeapon(unsigned int type);
//returns a pointer to the current weapon
Raven_Weapon* GetCurrentWeapon()const{return m_pCurrentWeapon;}
324 | 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