CanTarget take refence instead of pointer.

This commit is contained in:
Joris 2012-11-09 13:50:58 +01:00
parent 711d2bdd1e
commit b79c7732fd
8 changed files with 28 additions and 28 deletions

View file

@ -688,7 +688,7 @@ void AiHelpMe(const CUnit *attacker, CUnit &defender)
// if brother is idle or attack no-agressive target and
// can attack our attacker then ask for help
// FIXME ad support for help from Coward type units
if (aiunit.IsAgressive() && CanTarget(aiunit.Type, attacker->Type)
if (aiunit.IsAgressive() && CanTarget(*aiunit.Type, *attacker->Type)
&& aiunit.CurrentOrder()->GetGoal() != attacker) {
bool shouldAttack = aiunit.IsIdle() && aiunit.Threshold == 0;

View file

@ -76,7 +76,7 @@ public:
|| pos.y < unit->tilePos.y || pos.y >= unit->tilePos.y + type.TileHeight) {
return;
}
if (!CanTarget(source->Type, &type)) {
if (!CanTarget(*source->Type, type)) {
return;
}
if (!source->Player->IsEnemy(*unit)) { // a friend or neutral

View file

@ -185,7 +185,7 @@ public:
bool operator()(const CUnit *unit) const {
return unit->IsVisibleAsGoal(*player)
&& unit->IsEnemy(*player)
&& CanTarget(unit->Type, type);
&& CanTarget(*unit->Type, *type);
}
private:
const CPlayer *player;

View file

@ -549,7 +549,7 @@ extern int ViewPointDistance(const Vec2i &pos);
extern int ViewPointDistanceToUnit(const CUnit &dest);
/// Can this unit-type attack the other (destination)
extern int CanTarget(const CUnitType *type, const CUnitType *dest);
extern int CanTarget(const CUnitType &type, const CUnitType &dest);
/// Can transporter transport the other unit
extern int CanTransport(const CUnit &transporter, const CUnit &unit);

View file

@ -832,11 +832,11 @@ void Missile::MissileHit(CUnit *unit)
for (size_t i = 0; i != table.size(); ++i) {
CUnit &goal = *table[i];
//
// Can the unit attack the this unit-type?
// Can the unit attack this unit-type?
// NOTE: perhaps this should be come a property of the missile.
// Also check CorrectSphashDamage so land explosions can't hit the air units
//
if (CanTarget(this->SourceUnit->Type, goal.Type)
if (CanTarget(*this->SourceUnit->Type, *goal.Type)
&& (mtype.FriendlyFire == false || goal.Player->Index != this->SourceUnit->Player->Index)) {
bool shouldHit = true;

View file

@ -296,7 +296,7 @@ static void DoRightButton_Attack(CUnit &unit, CUnit *dest, const Vec2i &pos, int
}
SendCommandSpellCast(unit, pos, dest, spellnum, flush);
} else {
if (CanTarget(&type, dest->Type)) {
if (CanTarget(type, *dest->Type)) {
SendCommandAttack(unit, pos, dest, flush);
} else { // No valid target
SendCommandAttack(unit, pos, NoUnitP, flush);
@ -1080,7 +1080,7 @@ static int SendAttack(const Vec2i &tilePos)
CUnit &unit = *Selected[i];
if (unit.Type->CanAttack) {
if (!dest || (dest != &unit && CanTarget(unit.Type, dest->Type))) {
if (!dest || (dest != &unit && CanTarget(*unit.Type, *dest->Type))) {
if (dest) {
dest->Blink = 4;
}

View file

@ -2385,7 +2385,7 @@ int ThreatCalculate(const CUnit &unit, const CUnit &dest)
}
// Unit can attack back.
if (CanTarget(&dtype, &type)) {
if (CanTarget(dtype, type)) {
cost -= CANATTACK_BONUS;
}
return cost;
@ -2627,7 +2627,7 @@ void HitUnit(CUnit *attacker, CUnit &target, int damage, const Missile *missile)
CUnit *oldgoal = target.CurrentOrder()->GetGoal();
CUnit *goal, *best = oldgoal;
if (RevealAttacker && CanTarget(target.Type, attacker->Type)) {
if (RevealAttacker && CanTarget(*target.Type, *attacker->Type)) {
// Reveal Unit that is attacking
goal = attacker;
} else {
@ -2643,7 +2643,7 @@ void HitUnit(CUnit *attacker, CUnit &target, int damage, const Missile *missile)
if (!best || (goal && (ThreatCalculate(target, *goal) < ThreatCalculate(target, *best)))) {
best = goal;
}
if (CanTarget(target.Type, attacker->Type)
if (CanTarget(*target.Type, *attacker->Type)
&& (!best || (attacker && goal != attacker
&& (ThreatCalculate(target, *attacker) < ThreatCalculate(target, *best))))) {
best = attacker;
@ -2765,27 +2765,27 @@ int ViewPointDistanceToUnit(const CUnit &dest)
**
** @return 0 if attacker can't target the unit, else a positive number.
*/
int CanTarget(const CUnitType *source, const CUnitType *dest)
int CanTarget(const CUnitType &source, const CUnitType &dest)
{
for (unsigned int i = 0; i < UnitTypeVar.GetNumberBoolFlag(); i++) {
if (source->BoolFlag[i].CanTargetFlag != CONDITION_TRUE) {
if ((source->BoolFlag[i].CanTargetFlag == CONDITION_ONLY) ^
(dest->BoolFlag[i].value)) {
if (source.BoolFlag[i].CanTargetFlag != CONDITION_TRUE) {
if ((source.BoolFlag[i].CanTargetFlag == CONDITION_ONLY) ^
(dest.BoolFlag[i].value)) {
return 0;
}
}
}
if (dest->UnitType == UnitTypeLand) {
if (dest->ShoreBuilding) {
return source->CanTarget & (CanTargetLand | CanTargetSea);
if (dest.UnitType == UnitTypeLand) {
if (dest.ShoreBuilding) {
return source.CanTarget & (CanTargetLand | CanTargetSea);
}
return source->CanTarget & CanTargetLand;
return source.CanTarget & CanTargetLand;
}
if (dest->UnitType == UnitTypeFly) {
return source->CanTarget & CanTargetAir;
if (dest.UnitType == UnitTypeFly) {
return source.CanTarget & CanTargetAir;
}
if (dest->UnitType == UnitTypeNaval) {
return source->CanTarget & CanTargetSea;
if (dest.UnitType == UnitTypeNaval) {
return source.CanTarget & CanTargetSea;
}
return 0;
}

View file

@ -605,7 +605,7 @@ CUnit *TargetOnMap(const CUnit &source, const Vec2i &pos1, const Vec2i &pos2)
if (!unit.IsVisibleAsGoal(*source.Player)) {
continue;
}
if (!CanTarget(source.Type, unit.Type)) {
if (!CanTarget(*source.Type, *unit.Type)) {
continue;
}
@ -703,7 +703,7 @@ private:
if (!player.IsEnemy(*dest) // a friend or neutral
|| !dest->IsVisibleAsGoal(player)
|| !CanTarget(&type, &dtype)) {
|| !CanTarget(type, dtype)) {
return INT_MAX;
}
// Unit in range ?
@ -743,7 +743,7 @@ private:
}
// Unit can attack back.
if (CanTarget(&dtype, &type)) {
if (CanTarget(dtype, type)) {
cost -= CANATTACK_BONUS;
}
return cost;
@ -810,7 +810,7 @@ public:
const CUnitType &type = *attacker->Type;
const CUnitType &dtype = *dest->Type;
// won't be a target...
if (!CanTarget(&type, &dtype)) { // can't be attacked.
if (!CanTarget(type, dtype)) { // can't be attacked.
dest->CacheLock = 1;
return;
}
@ -876,7 +876,7 @@ public:
cost += -effective_hp * HEALTH_FACTOR;
// Unit can attack back.
if (CanTarget(&dtype, &type)) {
if (CanTarget(dtype, type)) {
cost += CANATTACK_BONUS;
}