Skip to content

Commit

Permalink
Prefer targets not too close to fire, and prefer very damaged targets.
Browse files Browse the repository at this point in the history
Previously, preferred damaged over non-damaged, but not very damaged over lightly
damaged, due to integer truncation.
  • Loading branch information
Cyp committed May 2, 2012
1 parent fb49891 commit fb8efec
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/ai.cpp
Expand Up @@ -62,6 +62,8 @@
#define EMP_DISABLED_PENALTY_F 10 //EMP shouldn't attack emped targets again
#define EMP_STRUCT_PENALTY_F (EMP_DISABLED_PENALTY_F * 2) //EMP don't attack structures, should be bigger than EMP_DISABLED_PENALTY_F

#define TOO_CLOSE_PENALTY_F 20

//Some weights for the units attached to a commander
#define WEIGHT_CMD_RANK (WEIGHT_DIST_TILE * 4) //A single rank is as important as 4 tiles distance
#define WEIGHT_CMD_SAME_TARGET WEIGHT_DIST_TILE //Don't want this to be too high, since a commander can have many units assigned
Expand Down Expand Up @@ -326,6 +328,13 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
//See if attacker is using an EMP weapon
bEmpWeap = (attackerWeapon->weaponSubClass == WSC_EMP);

int dist = iHypot(removeZ(psAttacker->pos - psTarget->pos));
bool tooClose = dist <= attackerWeapon->minRange;
if (tooClose)
{
dist = psAttacker->sensorRange; // If object is too close to fire at, consider it to be at maximum range.
}

/* Calculate attack weight */
if(psTarget->type == OBJ_DROID)
{
Expand All @@ -347,7 +356,7 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
}
else
{
damageRatio = 1 - targetDroid->body / targetDroid->originalBody;
damageRatio = 100 - 100*targetDroid->body / targetDroid->originalBody;
}
assert(targetDroid->originalBody != 0); // Assert later so we get the info from above

Expand Down Expand Up @@ -385,8 +394,8 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
attackWeight = asWeaponModifier[weaponEffect][(asPropulsionStats + targetDroid->asBits[COMP_PROPULSION].nStat)->propulsionType] // Our weapon's effect against target
+ asWeaponModifierBody[weaponEffect][(asBodyStats + targetDroid->asBits[COMP_BODY].nStat)->size]
+ WEIGHT_DIST_TILE_DROID * psAttacker->sensorRange/TILE_UNITS
- WEIGHT_DIST_TILE_DROID * map_coord(iHypot(psAttacker->pos.x - targetDroid->pos.x, psAttacker->pos.y - targetDroid->pos.y)) // farer droids are less attractive
+ WEIGHT_HEALTH_DROID * damageRatio // we prefer damaged droids
- WEIGHT_DIST_TILE_DROID * dist/TILE_UNITS // farther droids are less attractive
+ WEIGHT_HEALTH_DROID * damageRatio/100 // we prefer damaged droids
+ targetTypeBonus; // some droid types have higher priority

/* If attacking with EMP try to avoid targets that were already "EMPed" */
Expand All @@ -402,7 +411,7 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
targetStructure = (STRUCTURE *)psTarget;

/* Calculate damage this target suffered */
damageRatio = 1 - targetStructure->body / structureBody(targetStructure);
damageRatio = 100 - 100*targetStructure->body / structureBody(targetStructure);

/* See if this type of a structure should be prioritized */
switch(targetStructure->pStructureType->type)
Expand All @@ -427,8 +436,8 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
/* Now calculate the overall weight */
attackWeight = asStructStrengthModifier[weaponEffect][targetStructure->pStructureType->strength] // Our weapon's effect against target
+ WEIGHT_DIST_TILE_STRUCT * psAttacker->sensorRange/TILE_UNITS
- WEIGHT_DIST_TILE_STRUCT * map_coord(iHypot(psAttacker->pos.x - targetStructure->pos.x, psAttacker->pos.y - targetStructure->pos.y)) // farer structs are less attractive
+ WEIGHT_HEALTH_STRUCT * damageRatio // we prefer damaged structures
- WEIGHT_DIST_TILE_STRUCT * dist/TILE_UNITS // farther structs are less attractive
+ WEIGHT_HEALTH_STRUCT * damageRatio/100 // we prefer damaged structures
+ targetTypeBonus; // some structure types have higher priority

/* Go for unfinished structures only if nothing else found (same for non-visible structures) */
Expand All @@ -454,6 +463,11 @@ static SDWORD targetAttackWeight(BASE_OBJECT *psTarget, BASE_OBJECT *psAttacker,
attackWeight /= WEIGHT_NOT_VISIBLE_F;
}

if (tooClose)
{
attackWeight /= TOO_CLOSE_PENALTY_F;
}

/* Commander-related criterias */
if(bCmdAttached) //attached to a commander and don't have a target assigned by some order
{
Expand Down

0 comments on commit fb8efec

Please sign in to comment.