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

//given item. The farther the item, the higher the rating. If there is no
//item of the given type present in the game world at the time this method
//is called the value returned is 1
static double DistanceToItem(Raven_Bot* pBot, int ItemType);
//returns a value between 0 and 1 based on how much ammo the bot has for
//the given weapon, and the maximum amount of ammo the bot can carry. The
//closer the amount carried is to the max amount, the higher the score
static double IndividualWeaponStrength(Raven_Bot* pBot, int WeaponType);
//returns a value between 0 and 1 based on the total amount of ammo the
//bot is carrying for each of the weapons. Each of the three weapons a bot
//can pick up can contribute a third to the score. In other words, if a bot
//is carrying an RL and an RG and has max ammo for the RG but only half max
//for the RL, the rating will be 1/3 + 1/6+0=0.5
static double TotalWeaponStrength(Raven_Bot* pBot);
};
I’ll not list the method bodies here, but you may find it interesting to check
them out at some point. You can find them in the file
Raven/goals/Raven_Feature.cpp.
Now that we have some helper functions let’s take a look at how they
can be used to calculate the desirability scores for each strategy, which are
also standardized to be in the range 0 to 1.
Calculating the Desirability of Locating a Health Item
Generally speaking, the desirability of locating a health item is propor-
tional to the current health level of a bot and is inversely proportional to the
distance away from the closest instance. Because each of these features is
extracted by the methods discussed earlier and represented as a number in
the range 0 to 1, this can be written as:
where k is a constant used to tweak the result. This relationship makes
sense because the farther you have to travel to retrieve an item the less you
desire it, whereas the lower your health level, the greater your desire. (Note
that we don’t have to worry about a divide by zero error because it’s
impossible for an agent to get closer than its bounding radius away from an
item before the item is triggered.)
Here is the source code from Raven that implements this algorithm.
double GetHealthGoal_Evaluator::CalculateDesirability(Raven_Bot* pBot)
{
//first grab the distance to the closest instance of a health item
double Distance = Raven_Feature::DistanceToItem(pBot, type_health);
//if the distance feature is rated with a value of 1 it means that the
400 | Chapter 9
Goal Arbitration
1
health
Health
Desirability k
DistToHealth
-
æö
ç÷
èø

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