Game Development Reference
In-Depth Information
double CAgent::TimeToKill( USHORT Health,
USHORT Damage,
double Accuracy )
{
USHORT TimeToKill;
double DamagePerSec = ( Accuracy * Damage );
// avoid divide by zero
if ( DamagePerSec != 0 ) {
TimeToKill = Health / DamagePerSec;
} else {
// if damage/sec is 0, clamp time to 1000
TimeToKill = 1000;
} // end if
return TimeToKill;
}
Notice that the process in this function is broken into two steps so that we can
avoid “division by zero� errors. We are serving another purpose here. If the amount
of damage per second that we can inflict is 0, there is no way that we can injure,
much less
kill
the target with the selected weapon. We want to exclude this target-
weapon combination from
reasonable
consideration. By setting
TimeToKill
to
an absurd value such as 1,000, we are ensuring that it will significantly reduce any
further calculations that we perform with it. However, we are
not
removing it from
consideration entirely. If we find ourselves in a situation where
none
of the weapons
are effective against
any
of the targets, we would still want to use the best combina-
tion to at least do
some
damage.
Processing this relationship is as simple as sending the appropriate data into the
function. The data comes from a few simple functions as well. First, we have to retrieve
the accuracy and damage rates based on the weapon and distance. The following
functions provide that information for us.
USHORT CAgent::GetDamage( CWeapon* pWeapon, USHORT Dist )
{
USHORT Damage = pWeapon->GetDamage( Dist );