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

//item is either not present on the map or too far away to be worth
//considering, therefore the desirability is zero
if (Distance == 1)
{
return 0;
}
else
{
//value used to tweak the desirability
const double Tweaker = 0.2;
//the desirability of finding a health item is proportional to the amount
//of health remaining and inversely proportional to the distance from the
//nearest instance of a health item.
double Desirability = Tweaker * (1-Raven_Feature::Health(pBot)) /
(Raven_Feature::DistanceToItem(pBot, type_health));
//ensure the value is in the range 0 to 1
Clamp(Desirability, 0, 1);
return Desirability;
}
}
Calculating the Desirability of Locating a Specific Weapon
This is very similar to the previous algorithm. The desirability of locating a
specific weapon can be given as:
Notice how both the weapon strength and health features contribute to the
desirability of retrieving a weapon. This is sensible because as a bot
becomes more badly damaged or the amount of ammo it’s carrying for that
particular weapon increases, its desire for retrieving it should diminish.
This is how the algorithm looks in code:
double GetWeaponGoal_Evaluator::CalculateDesirability(Raven_Bot* pBot)
{
//grab the distance to the closest instance of the weapon type
double Distance = Raven_Feature::DistanceToItem(pBot, m_iWeaponType);
//if the distance feature is rated with a value of 1 it means that the
//item is either not present on the map or too far away to be worth
//considering, therefore the desirability is zero
if (Distance == 1)
{
return 0;
}
else
{
//value used to tweak the desirability
Goal-Driven Agent Behavior
| 401
Goal Arbitration
()
1
weapon
Health WeaponStrength
Desirability k
DistToWeapon
´-
æö
ç÷
èø
const double Tweaker = 0.15f;
double Health, WeaponStrength;
Health = Raven_Feature::Health(pBot);
WeaponStrength = Raven_Feature::IndividualWeaponStrength(pBot, m_iWeaponType);
double Desirability = (Tweaker * Health * (1-WeaponStrength)) / Distance;
//ensure the value is in the range 0 to 1
Clamp(Desirability, 0, 1);
return Desirability;
}
}
The bonus of using distance as a factor in the desirability calculation for
picking up weapon and health items is that, given the correct circum
-
stances, bots will temporarily change strategy and divert their course to
pick up nearby items.
z
TIP The influence of distance in the desirability algorithms we’ve examined so
far is linear. In other words, the desirability is directly proportional to the dis-
tance from the item. However, you may prefer the “pull” of the item on the bot
to become stronger more quickly as the bot approaches (like the force you feel
when you move two magnets toward each other), instead of at a constant rate
(like the force you feel when you stretch a spring). This is best explained with a
graph. See Figure 9.9.
To create an algorithm that produces a desirability-distance curve similar to the
graph on the right you must divide by the square (or even cube) of the distance.
In other words, the equation is changed to:
Don’t forget that you will also have to tweak k to give you the results you desire.
402 | Chapter 9
Goal Arbitration
Figure 9.9. The graph on the left shows a linear relationship with distance; the one on
the right is non-linear.
()
2
1
weapon
Health WeaponStrength
Desirability k
DistToWeapon
´-
æö
ç÷
èø

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