From 836743e7f9b983339059102a9689fce1f533df10 Mon Sep 17 00:00:00 2001 From: rt Date: Fri, 26 Oct 2018 00:23:17 +0200 Subject: [PATCH] fix #6057 --- rts/Game/SelectedUnitsHandler.cpp | 12 +++--------- rts/Sim/Units/CommandAI/CommandAI.cpp | 11 ++++------- rts/Sim/Units/UnitDef.h | 5 ++++- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/rts/Game/SelectedUnitsHandler.cpp b/rts/Game/SelectedUnitsHandler.cpp index 9092f946c37..581cb719f92 100644 --- a/rts/Game/SelectedUnitsHandler.cpp +++ b/rts/Game/SelectedUnitsHandler.cpp @@ -745,12 +745,6 @@ static const CUnit* targetUnit = nullptr; static const CFeature* targetFeature = nullptr; -static inline bool CanDamage(const UnitDef* ud) -{ - return ((ud->canAttack && !ud->weapons.empty()) || ud->canKamikaze); -} - - static inline bool IsBetterLeader(const UnitDef* newDef, const UnitDef* oldDef) { // There is a lot more that could be done here to make better @@ -758,14 +752,14 @@ static inline bool IsBetterLeader(const UnitDef* newDef, const UnitDef* oldDef) if (targetUnit != nullptr) { if (targetIsEnemy) { - const bool newCanDamage = CanDamage(newDef); - const bool oldCanDamage = CanDamage(oldDef); + const bool newCanDamage = newDef->CanDamage(); + const bool oldCanDamage = oldDef->CanDamage(); if ( newCanDamage && !oldCanDamage) return true; if (!newCanDamage && oldCanDamage) return false; - if (!CanDamage(targetUnit->unitDef)) { + if (!targetUnit->unitDef->CanDamage()) { if ( newDef->canReclaim && !oldDef->canReclaim) return true; if (!newDef->canReclaim && oldDef->canReclaim) diff --git a/rts/Sim/Units/CommandAI/CommandAI.cpp b/rts/Sim/Units/CommandAI/CommandAI.cpp index b0b6c30a1f6..7c90cd892e1 100644 --- a/rts/Sim/Units/CommandAI/CommandAI.cpp +++ b/rts/Sim/Units/CommandAI/CommandAI.cpp @@ -476,10 +476,7 @@ void CCommandAI::AddCommandDependency(const Command& c) { bool CCommandAI::IsAttackCapable() const { - const UnitDef* ud = owner->unitDef; - const bool b = (!ud->weapons.empty() || ud->canKamikaze || (ud->IsFactoryUnit())); - - return (ud->canAttack && b); + return (owner->unitDef->CanAttack()); } @@ -1499,13 +1496,13 @@ void CCommandAI::SlowUpdate() int CCommandAI::GetDefaultCmd(const CUnit* pointed, const CFeature* feature) { - if (pointed) { + if (pointed != nullptr) { if (!teamHandler.Ally(gu->myAllyTeam, pointed->allyteam)) { - if (IsAttackCapable()) { + if (IsAttackCapable()) return CMD_ATTACK; - } } } + return CMD_STOP; } diff --git a/rts/Sim/Units/UnitDef.h b/rts/Sim/Units/UnitDef.h index 35e8adafbca..181d7d3c0b1 100644 --- a/rts/Sim/Units/UnitDef.h +++ b/rts/Sim/Units/UnitDef.h @@ -70,12 +70,15 @@ struct UnitDef: public SolidObjectDef bool DontLand() const { return (dlHoverFactor >= 0.0f); } bool RequireMoveDef() const { return (canmove && speed > 0.0f && !canfly); } - bool CanChangeFireState() const { return (canFireControl && (HasWeapons() || canKamikaze || IsFactoryUnit())); } + bool CanChangeFireState() const { return (canFireControl && (canKamikaze || HasWeapons() || IsFactoryUnit())); } bool HasWeapons() const { return (HasWeapon(0)); } bool HasWeapon(unsigned int idx) const { return (weapons[idx].def != nullptr); } bool HasBomberWeapon(unsigned int idx) const; + bool CanAttack() const { return (canAttack && (canKamikaze || HasWeapons() || IsFactoryUnit())); } + bool CanDamage() const { return (canKamikaze || (canAttack && HasWeapons())); } + unsigned int NumWeapons() const { unsigned int n = 0;