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

How Raven Uses the Fuzzy Logic Classes
Each Raven weapon owns an instance of a fuzzy module, which is initial
-
ized with FLVs and rules specific to the weapon. All weapons are derived
from the
Raven_Weapon abstract base class and implement the method
GetDesirability, which updates the fuzzy module and returns a crisp desir
-
ability score.
Here are the relevant parts of
Raven_Weapon:
class Raven_Weapon
{
protected:
FuzzyModule m_FuzzyModule;
/* EXTRANEOUS DETAIL OMITTED */
public:
virtual double GetDesirability(double DistToTarget)=0;
/* EXTRANEOUS DETAIL OMITTED */
};
Every few update cycles (twice a second by default) the bots query each of
the weapons in their inventory to determine which of them is the most
desirable given the distance to the bot’s target and the ammo remaining,
and selects the one with the highest desirability score. The code that imple-
ments this logic is listed below.
void Raven_Bot::SelectWeapon()
{
//only need to run this code if a target is present
if (m_pTargSys->isTargetPresent())
{
//calculate the distance to the target
double DistToTarget = Vec2DDistance(Pos(), m_pTargSys->GetTarget()->Pos());
//for each weapon in the inventory calculate its desirability given the
//current situation. The most desirable weapon is selected
double BestSoFar = MinDouble;
std::vector<Raven_Weapon*>::const_iterator curWeap;
for (curWeap = m_Weapons.begin(); curWeap != m_Weapons.end(); ++curWeap)
{
//grab the desirability of this weapon (desirability is based upon
//distance to target and ammo remaining)
double score = (*curWeap)->GetDesirability(DistToTarget);
//if it is the most desirable so far select it
if (score > BestSoFar)
{
BestSoFar = score;
Fuzzy Logic | 451
How Raven Uses the Fuzzy Logic Classes

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