diff --git a/src/server/game/AI/CoreAI/GuardAI.cpp b/src/server/game/AI/CoreAI/GuardAI.cpp index abce4d1d39..f2ea7442f2 100644 --- a/src/server/game/AI/CoreAI/GuardAI.cpp +++ b/src/server/game/AI/CoreAI/GuardAI.cpp @@ -57,7 +57,7 @@ void GuardAI::EnterEvadeMode(EvadeReason /*why*/) { me->GetMotionMaster()->MoveIdle(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); return; } @@ -65,7 +65,7 @@ void GuardAI::EnterEvadeMode(EvadeReason /*why*/) me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); // Remove ChaseMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == CHASE_MOTION_TYPE) diff --git a/src/server/game/AI/CoreAI/PassiveAI.h b/src/server/game/AI/CoreAI/PassiveAI.h index bf593f83c2..4e80510e7f 100644 --- a/src/server/game/AI/CoreAI/PassiveAI.h +++ b/src/server/game/AI/CoreAI/PassiveAI.h @@ -39,6 +39,9 @@ class TC_GAME_API PossessedAI : public CreatureAI void MoveInLineOfSight(Unit*) override { } void AttackStart(Unit* target) override; + void JustEnteredCombat(Unit* who) override { EngagementStart(who); } + void JustExitedCombat() override { EngagementOver(); } + void JustStartedThreateningMe(Unit*) override { } void UpdateAI(uint32) override; void EnterEvadeMode(EvadeReason /*why*/) override { } @@ -57,6 +60,8 @@ class TC_GAME_API NullCreatureAI : public CreatureAI void MoveInLineOfSight(Unit*) override { } void AttackStart(Unit*) override { } + void JustStartedThreateningMe(Unit*) override { } + void JustEnteredCombat(Unit*) override { } void UpdateAI(uint32) override { } void JustAppeared() override { } void EnterEvadeMode(EvadeReason /*why*/) override { } diff --git a/src/server/game/AI/CoreAI/TotemAI.cpp b/src/server/game/AI/CoreAI/TotemAI.cpp index 533e489d85..7e083b04e7 100644 --- a/src/server/game/AI/CoreAI/TotemAI.cpp +++ b/src/server/game/AI/CoreAI/TotemAI.cpp @@ -32,16 +32,9 @@ int32 TotemAI::Permissible(Creature const* creature) return PERMIT_BASE_NO; } -TotemAI::TotemAI(Creature* c) : CreatureAI(c), i_victimGuid() +TotemAI::TotemAI(Creature* creature) : NullCreatureAI(creature), _victimGUID() { - ASSERT(c->IsTotem()); -} - -void TotemAI::MoveInLineOfSight(Unit* /*who*/) { } - -void TotemAI::EnterEvadeMode(EvadeReason /*why*/) -{ - me->CombatStop(true); + ASSERT(creature->IsTotem(), "TotemAI: AI assigned to a non-totem creature (%s)!", creature->GetGUID().ToString().c_str()); } void TotemAI::UpdateAI(uint32 /*diff*/) @@ -63,7 +56,7 @@ void TotemAI::UpdateAI(uint32 /*diff*/) // SPELLMOD_RANGE not applied in this place just because not existence range mods for attacking totems // pointer to appropriate target if found any - Unit* victim = i_victimGuid ? ObjectAccessor::GetUnit(*me, i_victimGuid) : nullptr; + Unit* victim = _victimGUID ? ObjectAccessor::GetUnit(*me, _victimGUID) : nullptr; // Search victim if no, not attackable, or out of range, or friendly (possible in case duel end) if (!victim || @@ -80,14 +73,14 @@ void TotemAI::UpdateAI(uint32 /*diff*/) if (victim) { // remember - i_victimGuid = victim->GetGUID(); + _victimGUID = victim->GetGUID(); // attack me->SetInFront(victim); // client change orientation by self me->CastSpell(victim, me->ToTotem()->GetSpell(), false); } else - i_victimGuid.Clear(); + _victimGUID.Clear(); } void TotemAI::AttackStart(Unit* /*victim*/) diff --git a/src/server/game/AI/CoreAI/TotemAI.h b/src/server/game/AI/CoreAI/TotemAI.h index d28ede8a25..95362a8079 100644 --- a/src/server/game/AI/CoreAI/TotemAI.h +++ b/src/server/game/AI/CoreAI/TotemAI.h @@ -19,26 +19,24 @@ #define TRINITY_TOTEMAI_H #include "CreatureAI.h" +#include "PassiveAI.h" #include "Timer.h" class Creature; class Totem; -class TC_GAME_API TotemAI : public CreatureAI +class TC_GAME_API TotemAI : public NullCreatureAI { public: explicit TotemAI(Creature* c); - void MoveInLineOfSight(Unit* who) override; void AttackStart(Unit* victim) override; - void JustAppeared() override { } - void EnterEvadeMode(EvadeReason /*why*/) override; void UpdateAI(uint32 diff) override; static int32 Permissible(Creature const* creature); private: - ObjectGuid i_victimGuid; + ObjectGuid _victimGUID; }; #endif diff --git a/src/server/game/AI/CoreAI/UnitAI.cpp b/src/server/game/AI/CoreAI/UnitAI.cpp index c6ce618776..317734c64b 100644 --- a/src/server/game/AI/CoreAI/UnitAI.cpp +++ b/src/server/game/AI/CoreAI/UnitAI.cpp @@ -254,7 +254,7 @@ void UnitAI::SortByDistance(std::list list, bool ascending) } DefaultTargetSelector::DefaultTargetSelector(Unit const* unit, float dist, bool playerOnly, bool withTank, int32 aura) - : me(unit), m_dist(dist), m_playerOnly(playerOnly), except(!withTank ? unit->GetThreatManager().GetCurrentVictim() : nullptr), m_aura(aura) + : me(unit), m_dist(dist), m_playerOnly(playerOnly), except(!withTank ? unit->GetThreatManager().GetLastVictim() : nullptr), m_aura(aura) { } diff --git a/src/server/game/AI/CreatureAI.cpp b/src/server/game/AI/CreatureAI.cpp index 98a018e9ed..cc941f3fc7 100644 --- a/src/server/game/AI/CreatureAI.cpp +++ b/src/server/game/AI/CreatureAI.cpp @@ -46,7 +46,7 @@ void CreatureAI::OnCharmed(bool apply) AISpellInfoType* UnitAI::AISpellInfo; AISpellInfoType* GetAISpellInfo(uint32 i) { return &UnitAI::AISpellInfo[i]; } -CreatureAI::CreatureAI(Creature* creature) : UnitAI(creature), me(creature), _boundary(nullptr), _negateBoundary(false), m_MoveInLineOfSight_locked(false) +CreatureAI::CreatureAI(Creature* creature) : UnitAI(creature), me(creature), _boundary(nullptr), _negateBoundary(false), _isEngaged(false), _moveInLOSLocked(false) { } @@ -96,11 +96,11 @@ void CreatureAI::DoZoneInCombat(Creature* creature /*= nullptr*/) // MoveInLineOfSight can be called inside another MoveInLineOfSight and cause stack overflow void CreatureAI::MoveInLineOfSight_Safe(Unit* who) { - if (m_MoveInLineOfSight_locked == true) + if (_moveInLOSLocked == true) return; - m_MoveInLineOfSight_locked = true; + _moveInLOSLocked = true; MoveInLineOfSight(who); - m_MoveInLineOfSight_locked = false; + _moveInLOSLocked = false; } void CreatureAI::MoveInLineOfSight(Unit* who) @@ -183,7 +183,7 @@ static bool ShouldFollowOnSpawn(SummonPropertiesEntry const* properties) } void CreatureAI::JustAppeared() { - if (!me->IsInCombat()) + if (!IsEngaged()) { if (TempSummon* summon = me->ToTempSummon()) { @@ -200,6 +200,12 @@ void CreatureAI::JustAppeared() } } +void CreatureAI::JustEnteredCombat(Unit* who) +{ + if (!IsEngaged() && !me->CanHaveThreatList()) + EngagementStart(who); +} + void CreatureAI::EnterEvadeMode(EvadeReason why) { if (!_EnterEvadeMode(why)) @@ -225,7 +231,7 @@ void CreatureAI::EnterEvadeMode(EvadeReason why) bool CreatureAI::UpdateVictim() { - if (!me->IsEngaged()) + if (!IsEngaged()) return false; if (!me->HasReactState(REACT_PASSIVE)) @@ -247,15 +253,44 @@ bool CreatureAI::UpdateVictim() return true; } +void CreatureAI::EngagementStart(Unit* who) +{ + if (_isEngaged) + { + TC_LOG_ERROR("scripts.ai", "CreatureAI::EngagementStart called even though creature is already engaged."); + return; + } + _isEngaged = true; + + me->AtEngage(who); +} + +void CreatureAI::EngagementOver() +{ + if (!_isEngaged) + { + TC_LOG_ERROR("scripts.ai", "CreatureAI::EngagementOver called even though creature is not currently engaged."); + return; + } + _isEngaged = false; + + me->AtDisengage(); +} + bool CreatureAI::_EnterEvadeMode(EvadeReason /*why*/) { - if (!me->IsAlive()) + if (me->IsInEvadeMode()) return false; + if (!me->IsAlive()) + { + EngagementOver(); + return false; + } + me->RemoveAurasOnEvade(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); me->LoadCreaturesAddon(); me->SetLootRecipient(nullptr); me->ResetPlayerDamageReq(); @@ -265,7 +300,9 @@ bool CreatureAI::_EnterEvadeMode(EvadeReason /*why*/) me->GetSpellHistory()->ResetAllCooldowns(); me->SetTarget(ObjectGuid::Empty); - return !me->IsInEvadeMode(); + EngagementOver(); + + return true; } const uint32 BOUNDARY_VISUALIZE_CREATURE = 15425; diff --git a/src/server/game/AI/CreatureAI.h b/src/server/game/AI/CreatureAI.h index 99a76425f2..fdb2869a59 100644 --- a/src/server/game/AI/CreatureAI.h +++ b/src/server/game/AI/CreatureAI.h @@ -96,6 +96,8 @@ class TC_GAME_API CreatureAI : public UnitAI ~CreatureAI(); + bool IsEngaged() const { return _isEngaged; } + void Talk(uint8 id, WorldObject const* whisperTarget = nullptr); /// == Reactions At ================================= @@ -109,11 +111,17 @@ class TC_GAME_API CreatureAI : public UnitAI // Called for reaction at stopping attack at no attackers or targets virtual void EnterEvadeMode(EvadeReason why = EVADE_REASON_OTHER); + // Called for reaction whenever we start being in combat (overridden from base UnitAI) + void JustEnteredCombat(Unit* /*who*/) override; + + // Called for reaction whenever a new non-offline unit is added to the threat list + virtual void JustStartedThreateningMe(Unit* who) { if (!IsEngaged()) EngagementStart(who); } + // Called for reaction when initially engaged virtual void JustEngagedWith(Unit* /*who*/) { } // Called when the creature is killed - virtual void JustDied(Unit* /*killer*/) { } + virtual void JustDied(Unit* /*killer*/) { if (IsEngaged()) EngagementOver(); } // Called when the creature kills a unit virtual void KilledUnit(Unit* /*victim*/) { } @@ -227,6 +235,8 @@ class TC_GAME_API CreatureAI : public UnitAI // intended for encounter design/debugging. do not use for other purposes. expensive. int32 VisualizeBoundary(uint32 duration, Unit* owner = nullptr, bool fill = false) const; + + // boundary system methods virtual bool CheckInRoom(); CreatureBoundary const* GetBoundary() const { return _boundary; } void SetBoundary(CreatureBoundary const* boundary, bool negativeBoundaries = false); @@ -234,6 +244,8 @@ class TC_GAME_API CreatureAI : public UnitAI static bool IsInBounds(CreatureBoundary const& boundary, Position const* who); protected: + void EngagementStart(Unit* who); + void EngagementOver(); virtual void MoveInLineOfSight(Unit* /*who*/); bool _EnterEvadeMode(EvadeReason why = EVADE_REASON_OTHER); @@ -242,7 +254,8 @@ class TC_GAME_API CreatureAI : public UnitAI bool _negateBoundary; private: - bool m_MoveInLineOfSight_locked; + bool _isEngaged; + bool _moveInLOSLocked; void _OnOwnerCombatInteraction(Unit* target); }; diff --git a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp index 26b7d042cb..f1f59505e7 100644 --- a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp +++ b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp @@ -132,9 +132,10 @@ void EscortAI::EnterEvadeMode(EvadeReason /*why*/) { me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); me->SetLootRecipient(nullptr); + EngagementOver(); + if (HasEscortState(STATE_ESCORT_ESCORTING)) { AddEscortState(STATE_ESCORT_RETURNING); diff --git a/src/server/game/Combat/CombatManager.cpp b/src/server/game/Combat/CombatManager.cpp index 92f3edbab2..efd0766e22 100644 --- a/src/server/game/Combat/CombatManager.cpp +++ b/src/server/game/Combat/CombatManager.cpp @@ -292,13 +292,8 @@ void CombatManager::EndAllPvPCombat() /*static*/ void CombatManager::NotifyAICombat(Unit* me, Unit* other) { - if (!me->IsAIEnabled) - return; - me->GetAI()->JustEnteredCombat(other); - - if (Creature* cMe = me->ToCreature()) - if (!cMe->CanHaveThreatList()) - cMe->AI()->JustEngagedWith(other); + if (UnitAI* ai = me->GetAI()) + ai->JustEnteredCombat(other); } void CombatManager::PutReference(ObjectGuid const& guid, CombatReference* ref) @@ -335,14 +330,14 @@ bool CombatManager::UpdateOwnerCombatState() const { _owner->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IN_COMBAT); _owner->AtEnterCombat(); - if (!_owner->CanHaveThreatList() && !_owner->IsEngaged()) + if (_owner->GetTypeId() == TYPEID_UNIT) _owner->AtEngage(GetAnyTarget()); } else { _owner->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IN_COMBAT); _owner->AtExitCombat(); - if (_owner->IsEngaged() && !(_owner->ToCreature() && _owner->ToCreature()->IsAIEnabled)) + if (_owner->GetTypeId() == TYPEID_UNIT) _owner->AtDisengage(); } diff --git a/src/server/game/Combat/ThreatManager.cpp b/src/server/game/Combat/ThreatManager.cpp index 90c721f0c2..112adba0f5 100644 --- a/src/server/game/Combat/ThreatManager.cpp +++ b/src/server/game/Combat/ThreatManager.cpp @@ -15,12 +15,13 @@ * with this program. If not, see . */ +#include "ThreatManager.h" #include "Creature.h" #include "CreatureAI.h" #include "CreatureGroups.h" #include "MotionMaster.h" #include "Player.h" -#include "ThreatManager.h" +#include "TemporarySummon.h" #include "Unit.h" #include "UnitAI.h" #include "UnitDefines.h" @@ -73,6 +74,7 @@ void ThreatReference::UpdateOffline() { _online = ShouldBeSuppressed() ? ONLINE_STATE_SUPPRESSED : ONLINE_STATE_ONLINE; HeapNotifyIncreased(); + _mgr.RegisterForAIUpdate(this); } } @@ -159,9 +161,11 @@ void ThreatReference::UnregisterAndFree() if (cWho->IsPet() || cWho->IsTotem() || cWho->IsTrigger()) return false; - // summons cannot have a threat list, unless they are controlled by a creature - if (cWho->HasUnitTypeMask(UNIT_MASK_MINION | UNIT_MASK_GUARDIAN) && !cWho->GetOwnerGUID().IsCreature()) - return false; + // summons cannot have a threat list if they were summoned by a player + if (cWho->HasUnitTypeMask(UNIT_MASK_MINION | UNIT_MASK_GUARDIAN)) + if (TempSummon const* tWho = cWho->ToTempSummon()) + if (tWho->GetSummonerGUID().IsPlayer()) + return false; return true; } @@ -201,10 +205,11 @@ Unit* ThreatManager::GetCurrentVictim() { if (!_currentVictimRef || _currentVictimRef->ShouldBeOffline()) UpdateVictim(); - return const_cast(this)->GetCurrentVictim(); + ASSERT(!_currentVictimRef || _currentVictimRef->IsAvailable()); + return _currentVictimRef ? _currentVictimRef->GetVictim() : nullptr; } -Unit* ThreatManager::GetCurrentVictim() const +Unit* ThreatManager::GetLastVictim() const { if (_currentVictimRef && !_currentVictimRef->ShouldBeOffline()) return _currentVictimRef->GetVictim(); @@ -246,7 +251,7 @@ float ThreatManager::GetThreat(Unit const* who, bool includeOffline) const return (includeOffline || it->second->IsAvailable()) ? it->second->GetThreat() : 0.0f; } -std::vector ThreatManager::GetModifiableThreatList() const +std::vector ThreatManager::GetModifiableThreatList() { std::vector list; list.reserve(_myThreatListEntries.size()); @@ -386,16 +391,14 @@ void ThreatManager::AddThreat(Unit* target, float amount, SpellInfo const* spell PutThreatListRef(target->GetGUID(), ref); target->GetThreatManager().PutThreatenedByMeRef(_owner->GetGUID(), ref); - // afterwards, we evaluate whether this is an online reference (it might not be an acceptable target, but we need to add it to our threat list before we check!) ref->UpdateOffline(); - if (ref->IsOnline()) // ...and if the ref is online it also gets the threat it should have + if (ref->IsOnline()) // we only add the threat if the ref is currently available ref->AddThreat(amount); - if (!_owner->IsEngaged()) - { - _owner->AtEngage(target); + if (!_currentVictimRef) UpdateVictim(); - } + else + ProcessAIUpdates(); } void ThreatManager::ScaleThreat(Unit* target, float factor) @@ -480,14 +483,6 @@ void ThreatManager::ClearAllThreat() } } -void ThreatManager::NotifyDisengaged() -{ - // note: i don't really like having this here - // (maybe engage flag should be in creature ai? it's inherently an AI property...) - if (_owner->IsEngaged()) - _owner->AtDisengage(); -} - void ThreatManager::FixateTarget(Unit* target) { if (target) @@ -522,6 +517,7 @@ void ThreatManager::UpdateVictim() _needClientUpdate = false; } + ProcessAIUpdates(); } ThreatReference const* ThreatManager::ReselectVictim() @@ -530,7 +526,7 @@ ThreatReference const* ThreatManager::ReselectVictim() return nullptr; for (auto const& pair : _myThreatListEntries) - pair.second->UpdateOffline(); + pair.second->UpdateOffline(); // AI notifies are processed in ::UpdateVictim caller // fixated target is always preferred if (_fixateRef && _fixateRef->IsAvailable()) @@ -579,6 +575,16 @@ ThreatReference const* ThreatManager::ReselectVictim() return nullptr; } +void ThreatManager::ProcessAIUpdates() +{ + CreatureAI* ai = ASSERT_NOTNULL(_owner->ToCreature())->AI(); + std::vector v(std::move(_needsAIUpdate)); // _needClientUpdate is now empty in case this triggers a recursive call + if (!ai) + return; + for (ThreatReference const* ref : v) + ai->JustStartedThreateningMe(ref->GetVictim()); +} + // returns true if a is LOWER on the threat list than b /*static*/ bool ThreatManager::CompareReferencesLT(ThreatReference const* a, ThreatReference const* b, float aWeight) { diff --git a/src/server/game/Combat/ThreatManager.h b/src/server/game/Combat/ThreatManager.h index 6e4f20c55a..8e722707f9 100644 --- a/src/server/game/Combat/ThreatManager.h +++ b/src/server/game/Combat/ThreatManager.h @@ -103,7 +103,7 @@ class TC_GAME_API ThreatManager bool CanHaveThreatList() const { return _ownerCanHaveThreatList; } // returns the current victim - this can be nullptr if owner's threat list is empty, or has only offline targets Unit* GetCurrentVictim(); - Unit* GetCurrentVictim() const; + Unit* GetLastVictim() const; // returns an arbitrary non-offline victim from owner's threat list if one exists, nullptr otherwise Unit* GetAnyTarget() const; @@ -121,10 +121,10 @@ class TC_GAME_API ThreatManager Trinity::IteratorPair GetUnsortedThreatList() const { return { _myThreatListEntries.begin(), _myThreatListEntries.end() }; } // slightly slower than GetUnsorted, but, well...sorted - only use it if you need the sorted property, of course // this iterator pair will invalidate on any modification (even indirect) of the threat list; spell casts and similar can all induce this! - // note: current tank is NOT guaranteed to be the first entry in this list - check GetCurrentVictim separately if you want that! + // note: current tank is NOT guaranteed to be the first entry in this list - check GetLastVictim separately if you want that! Trinity::IteratorPair GetSortedThreatList() const { return { _sortedThreatList.ordered_begin(), _sortedThreatList.ordered_end() }; } // slowest of the three threat list getters (by far), but lets you modify the threat references - this is also sorted - std::vector GetModifiableThreatList() const; + std::vector GetModifiableThreatList(); // does any unit have a threat list entry with victim == this.owner? bool IsThreateningAnyone(bool includeOffline = false) const; @@ -154,9 +154,6 @@ class TC_GAME_API ThreatManager void ClearThreat(ThreatReference* ref); // Removes all targets from the threat list (will cause evade in UpdateVictim if called) void ClearAllThreat(); - // THIS SHOULD ONLY BE CALLED FROM A CREATURE'S AI (typically in EnterEvadeMode) - // notify the unit that the AI has disengaged - void NotifyDisengaged(); // Fixate on the passed target; this target will always be selected until the fixate is cleared // (if the target is not in the threat list, does nothing) @@ -204,6 +201,12 @@ class TC_GAME_API ThreatManager threat_list_heap _sortedThreatList; std::unordered_map _myThreatListEntries; + // AI notifies are delayed to ensure we are in a consistent state before we call out to arbitrary logic + // threat references might register themselves here when ::UpdateOffline() is called - MAKE SURE THIS IS PROCESSED JUST BEFORE YOU EXIT THREATMANAGER LOGIC + void ProcessAIUpdates(); + void RegisterForAIUpdate(ThreatReference const* ref) { _needsAIUpdate.push_back(ref); } + std::vector _needsAIUpdate; + // picks a new victim - called from ::Update periodically void UpdateVictim(); ThreatReference const* ReselectVictim(); @@ -278,7 +281,7 @@ class TC_GAME_API ThreatReference _owner(reinterpret_cast(mgr->_owner)), _mgr(*mgr), _victim(victim), _baseAmount(0.0f), _tempModifier(0), _taunted(TAUNT_STATE_NONE) { - _online = ShouldBeSuppressed() ? ONLINE_STATE_SUPPRESSED : ONLINE_STATE_ONLINE; + _online = ONLINE_STATE_OFFLINE; } void UnregisterAndFree(); diff --git a/src/server/game/Entities/Creature/Creature.cpp b/src/server/game/Entities/Creature/Creature.cpp index 05b59268de..652cb47033 100644 --- a/src/server/game/Entities/Creature/Creature.cpp +++ b/src/server/game/Entities/Creature/Creature.cpp @@ -3201,6 +3201,13 @@ bool Creature::CanGiveExperience() const && !(GetCreatureTemplate()->flags_extra & CREATURE_FLAG_EXTRA_NO_XP_AT_KILL); } +bool Creature::IsEngaged() const +{ + if (CreatureAI const* ai = AI()) + return ai->IsEngaged(); + return false; +} + void Creature::AtEngage(Unit* target) { Unit::AtEngage(target); diff --git a/src/server/game/Entities/Creature/Creature.h b/src/server/game/Entities/Creature/Creature.h index cf5ea1dd16..b511229b25 100644 --- a/src/server/game/Entities/Creature/Creature.h +++ b/src/server/game/Entities/Creature/Creature.h @@ -358,6 +358,7 @@ class TC_GAME_API Creature : public Unit, public GridObject, public Ma void MakeInterruptable(bool apply); + bool IsEngaged() const override; void AtEngage(Unit* target) override; void AtDisengage() override; diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 44b6747242..d9b63020a9 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -1232,10 +1232,10 @@ class TC_GAME_API Unit : public WorldObject /// ====================== THREAT & COMBAT ==================== bool CanHaveThreatList() const { return m_threatManager.CanHaveThreatList(); } - // This value can be different from IsInCombat: + // This value can be different from IsInCombat, for example: // - when a projectile spell is midair against a creature (combat on launch - threat+aggro on impact) // - when the creature has no targets left, but the AI has not yet ceased engaged logic - bool IsEngaged() const { return m_isEngaged; } + virtual bool IsEngaged() const { return IsInCombat(); } bool IsEngagedBy(Unit const* who) const { return CanHaveThreatList() ? IsThreatenedBy(who) : IsInCombatWith(who); } void EngageWithTarget(Unit* who); // Adds target to threat list if applicable, otherwise just sets combat state // Combat handling @@ -2002,11 +2002,12 @@ class TC_GAME_API Unit : public WorldObject void ProcessPositionDataChanged(PositionFullTerrainStatus const& data) override; virtual void ProcessTerrainStatusUpdate(ZLiquidStatus status, Optional const& liquidData); + // notifiers virtual void AtEnterCombat() { } virtual void AtExitCombat(); - virtual void AtEngage(Unit* /*target*/) { m_isEngaged = true; } - virtual void AtDisengage() { m_isEngaged = false; } + virtual void AtEngage(Unit* /*target*/) {} + virtual void AtDisengage() {} void InterruptMovementBasedAuras(); private: diff --git a/src/server/scripts/Commands/cs_debug.cpp b/src/server/scripts/Commands/cs_debug.cpp index 8f15f2561e..19f747ddb1 100644 --- a/src/server/scripts/Commands/cs_debug.cpp +++ b/src/server/scripts/Commands/cs_debug.cpp @@ -859,7 +859,7 @@ public: ThreatManager& mgr = target->GetThreatManager(); if (!target->IsAlive()) { - handler->PSendSysMessage("%s (GUID %u) is not alive.", target->GetName().c_str(), target->GetGUID().GetCounter()); + handler->PSendSysMessage("%s (GUID %u) is not alive.%s", target->GetName().c_str(), target->GetGUID().GetCounter(), target->IsEngaged() ? " (It is, however, engaged. Huh?)" : ""); return true; } diff --git a/src/server/scripts/EasternKingdoms/BlackrockMountain/BlackrockDepths/instance_blackrock_depths.cpp b/src/server/scripts/EasternKingdoms/BlackrockMountain/BlackrockDepths/instance_blackrock_depths.cpp index 83cf8c82fb..045458ce85 100644 --- a/src/server/scripts/EasternKingdoms/BlackrockMountain/BlackrockDepths/instance_blackrock_depths.cpp +++ b/src/server/scripts/EasternKingdoms/BlackrockMountain/BlackrockDepths/instance_blackrock_depths.cpp @@ -372,16 +372,9 @@ public: if (Creature* boss = instance->GetCreature(TombBossGUIDs[i])) { if (!boss->IsAlive()) - {//do not call EnterEvadeMode(), it will create infinit loops boss->Respawn(); - boss->RemoveAllAuras(); - boss->CombatStop(true); - boss->GetThreatManager().NotifyDisengaged(); - boss->LoadCreaturesAddon(); - boss->GetMotionMaster()->MoveTargetedHome(); - boss->SetLootRecipient(nullptr); - } - boss->SetFaction(FACTION_FRIENDLY); + else + boss->SetFaction(FACTION_FRIENDLY); } } GhostKillCount = 0; diff --git a/src/server/scripts/EasternKingdoms/Karazhan/bosses_opera.cpp b/src/server/scripts/EasternKingdoms/Karazhan/bosses_opera.cpp index 30e3bb7aae..bd7b5c7117 100644 --- a/src/server/scripts/EasternKingdoms/Karazhan/bosses_opera.cpp +++ b/src/server/scripts/EasternKingdoms/Karazhan/bosses_opera.cpp @@ -1253,7 +1253,6 @@ public: Julianne->GetMotionMaster()->Clear(); Julianne->setDeathState(JUST_DIED); Julianne->CombatStop(true); - Julianne->GetThreatManager().NotifyDisengaged(); Julianne->SetUInt32Value(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE); } return; @@ -1528,7 +1527,6 @@ void boss_julianne::boss_julianneAI::DamageTaken(Unit* /*done_by*/, uint32 &dama Romulo->GetMotionMaster()->Clear(); Romulo->setDeathState(JUST_DIED); Romulo->CombatStop(true); - Romulo->GetThreatManager().NotifyDisengaged(); Romulo->SetUInt32Value(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE); } diff --git a/src/server/scripts/EasternKingdoms/ScarletEnclave/the_scarlet_enclave_chapter_5.cpp b/src/server/scripts/EasternKingdoms/ScarletEnclave/the_scarlet_enclave_chapter_5.cpp index aecf3d32c4..ae29208b64 100644 --- a/src/server/scripts/EasternKingdoms/ScarletEnclave/the_scarlet_enclave_chapter_5.cpp +++ b/src/server/scripts/EasternKingdoms/ScarletEnclave/the_scarlet_enclave_chapter_5.cpp @@ -1388,11 +1388,12 @@ public: if (me->HasAura(SPELL_THE_MIGHT_OF_MOGRAINE)) me->RemoveAurasDueToSpell(SPELL_THE_MIGHT_OF_MOGRAINE); me->RemoveAllAuras(); - me->GetThreatManager().ClearAllThreat(); me->CombatStop(true); me->InterruptNonMeleeSpells(false); me->SetWalk(false); + EngagementOver(); + for (uint8 i = 0; i < ENCOUNTER_DEFENDER_NUMBER; ++i) DespawnNPC(uiDefenderGUID[i]); for (uint8 i = 0; i < ENCOUNTER_EARTHSHATTER_NUMBER; ++i) @@ -1408,8 +1409,8 @@ public: if (Creature* temp = ObjectAccessor::GetCreature(*me, uiKorfaxGUID)) { + temp->AI()->EnterEvadeMode(); temp->RemoveAllAuras(); - temp->GetThreatManager().ClearAllThreat(); temp->CombatStop(true); temp->AttackStop(); temp->SetFaction(me->GetFaction()); @@ -1420,7 +1421,6 @@ public: if (Creature* temp = ObjectAccessor::GetCreature(*me, uiMaxwellGUID)) { temp->RemoveAllAuras(); - temp->GetThreatManager().ClearAllThreat(); temp->CombatStop(true); temp->AttackStop(); temp->SetFaction(me->GetFaction()); @@ -1430,8 +1430,8 @@ public: if (Creature* temp = ObjectAccessor::GetCreature(*me, uiEligorGUID)) { + temp->AI()->EnterEvadeMode(); temp->RemoveAllAuras(); - temp->GetThreatManager().ClearAllThreat(); temp->CombatStop(true); temp->AttackStop(); temp->SetFaction(me->GetFaction()); @@ -1442,8 +1442,8 @@ public: if (Creature* temp = ObjectAccessor::GetCreature(*me, uiKoltiraGUID)) { + temp->AI()->EnterEvadeMode(); temp->RemoveAllAuras(); - temp->GetThreatManager().ClearAllThreat(); temp->CombatStop(true); temp->AttackStop(); temp->SetFaction(me->GetFaction()); @@ -1457,8 +1457,8 @@ public: if (Creature* temp = ObjectAccessor::GetCreature(*me, uiThassarianGUID)) { + temp->AI()->EnterEvadeMode(); temp->RemoveAllAuras(); - temp->GetThreatManager().ClearAllThreat(); temp->CombatStop(true); temp->AttackStop(); temp->SetFaction(me->GetFaction()); diff --git a/src/server/scripts/Kalimdor/CavernsOfTime/BattleForMountHyjal/hyjalAI.cpp b/src/server/scripts/Kalimdor/CavernsOfTime/BattleForMountHyjal/hyjalAI.cpp index 004069b7b4..35e9b2d274 100644 --- a/src/server/scripts/Kalimdor/CavernsOfTime/BattleForMountHyjal/hyjalAI.cpp +++ b/src/server/scripts/Kalimdor/CavernsOfTime/BattleForMountHyjal/hyjalAI.cpp @@ -423,9 +423,10 @@ void hyjalAI::EnterEvadeMode(EvadeReason /*why*/) if (me->GetEntry() != JAINA) me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); me->LoadCreaturesAddon(); + EngagementOver(); + if (me->IsAlive()) me->GetMotionMaster()->MoveTargetedHome(); diff --git a/src/server/scripts/Kalimdor/zone_desolace.cpp b/src/server/scripts/Kalimdor/zone_desolace.cpp index 785d7fd6c2..032184e111 100644 --- a/src/server/scripts/Kalimdor/zone_desolace.cpp +++ b/src/server/scripts/Kalimdor/zone_desolace.cpp @@ -85,8 +85,10 @@ public: me->UpdateEntry(NPC_TAMED_KODO); me->CombatStop(); - me->GetThreatManager().ClearAllThreat(); me->SetSpeedRate(MOVE_RUN, 0.6f); + + EngagementOver(); + me->FollowTarget(caster); me->setActive(true); } diff --git a/src/server/scripts/Kalimdor/zone_the_barrens.cpp b/src/server/scripts/Kalimdor/zone_the_barrens.cpp index bce656a1ee..c484040f92 100644 --- a/src/server/scripts/Kalimdor/zone_the_barrens.cpp +++ b/src/server/scripts/Kalimdor/zone_the_barrens.cpp @@ -220,9 +220,11 @@ public: { me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); me->StopMoving(); + + EngagementOver(); + me->GetMotionMaster()->MoveIdle(); me->SetFaction(FACTION_FRIENDLY_F); diff --git a/src/server/scripts/Northrend/IcecrownCitadel/boss_icecrown_gunship_battle.cpp b/src/server/scripts/Northrend/IcecrownCitadel/boss_icecrown_gunship_battle.cpp index d0f98c38e9..aef223ccf6 100644 --- a/src/server/scripts/Northrend/IcecrownCitadel/boss_icecrown_gunship_battle.cpp +++ b/src/server/scripts/Northrend/IcecrownCitadel/boss_icecrown_gunship_battle.cpp @@ -579,7 +579,7 @@ struct gunship_npc_AI : public ScriptedAI return; me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); me->GetMotionMaster()->MoveTargetedHome(); } @@ -724,7 +724,6 @@ class npc_gunship : public CreatureScript Creature* stalker = *itr; stalker->RemoveAllAuras(); stalker->CombatStop(true); - stalker->GetThreatManager().NotifyDisengaged(); } uint32 explosionSpell = isVictory ? SPELL_EXPLOSION_VICTORY : SPELL_EXPLOSION_WIPE; @@ -883,7 +882,7 @@ class npc_high_overlord_saurfang_igb : public CreatureScript return; me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); me->GetMotionMaster()->MoveTargetedHome(); Reset(); @@ -1147,7 +1146,7 @@ class npc_muradin_bronzebeard_igb : public CreatureScript return; me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); me->GetMotionMaster()->MoveTargetedHome(); Reset(); diff --git a/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp b/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp index fd5d510b2f..4268abcfa0 100644 --- a/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp +++ b/src/server/scripts/Northrend/IcecrownCitadel/boss_the_lich_king.cpp @@ -1767,7 +1767,7 @@ class npc_terenas_menethil : public CreatureScript return; me->CombatStop(false); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); } void DamageTaken(Unit* /*attacker*/, uint32& damage) override diff --git a/src/server/scripts/Northrend/Naxxramas/boss_patchwerk.cpp b/src/server/scripts/Northrend/Naxxramas/boss_patchwerk.cpp index 66203f440e..31ddcda930 100644 --- a/src/server/scripts/Northrend/Naxxramas/boss_patchwerk.cpp +++ b/src/server/scripts/Northrend/Naxxramas/boss_patchwerk.cpp @@ -117,7 +117,7 @@ public: ThreatReference* secondThreat = nullptr; ThreatReference* thirdThreat = nullptr; - ThreatManager const& mgr = me->GetThreatManager(); + ThreatManager& mgr = me->GetThreatManager(); Unit* currentVictim = mgr.GetCurrentVictim(); auto list = mgr.GetModifiableThreatList(); auto it = list.begin(), end = list.end(); diff --git a/src/server/scripts/Northrend/Nexus/Oculus/boss_urom.cpp b/src/server/scripts/Northrend/Nexus/Oculus/boss_urom.cpp index 1ea959d370..9f176efa24 100644 --- a/src/server/scripts/Northrend/Nexus/Oculus/boss_urom.cpp +++ b/src/server/scripts/Northrend/Nexus/Oculus/boss_urom.cpp @@ -290,7 +290,7 @@ class boss_urom : public CreatureScript { me->RemoveAllAuras(); me->CombatStop(false); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); } void SpellHit(Unit* /*caster*/, SpellInfo const* spellInfo) override diff --git a/src/server/scripts/Northrend/Ulduar/Ulduar/boss_freya.cpp b/src/server/scripts/Northrend/Ulduar/Ulduar/boss_freya.cpp index 4ef5a170b3..bac96a03ec 100644 --- a/src/server/scripts/Northrend/Ulduar/Ulduar/boss_freya.cpp +++ b/src/server/scripts/Northrend/Ulduar/Ulduar/boss_freya.cpp @@ -631,7 +631,7 @@ class boss_freya : public CreatureScript Elder->RemoveAllAuras(); Elder->AttackStop(); Elder->CombatStop(true); - Elder->GetThreatManager().NotifyDisengaged(); + EngagementOver(); Elder->AI()->DoAction(ACTION_ELDER_FREYA_KILLED); } } diff --git a/src/server/scripts/Northrend/zone_borean_tundra.cpp b/src/server/scripts/Northrend/zone_borean_tundra.cpp index 5efd5a7b9e..1d4c41e05d 100644 --- a/src/server/scripts/Northrend/zone_borean_tundra.cpp +++ b/src/server/scripts/Northrend/zone_borean_tundra.cpp @@ -720,7 +720,6 @@ public: Creature* owner = GetOwner()->ToCreature(); owner->RemoveAllAurasExceptType(SPELL_AURA_DUMMY); owner->CombatStop(true); - owner->GetThreatManager().NotifyDisengaged(); owner->GetMotionMaster()->Clear(false); owner->GetMotionMaster()->MoveFollow(GetCaster(), 4.0f, 0.0f); owner->CastSpell(owner, SPELL_SUBDUED, true); diff --git a/src/server/scripts/Outland/CoilfangReservoir/SerpentShrine/boss_leotheras_the_blind.cpp b/src/server/scripts/Outland/CoilfangReservoir/SerpentShrine/boss_leotheras_the_blind.cpp index 4daaef7af1..e3938c2b21 100644 --- a/src/server/scripts/Outland/CoilfangReservoir/SerpentShrine/boss_leotheras_the_blind.cpp +++ b/src/server/scripts/Outland/CoilfangReservoir/SerpentShrine/boss_leotheras_the_blind.cpp @@ -525,7 +525,7 @@ public: { ThreatManager const& mgr = me->GetThreatManager(); std::list TargetList; - Unit* currentVictim = mgr.GetCurrentVictim(); + Unit* currentVictim = mgr.GetLastVictim(); for (ThreatReference const* ref : mgr.GetSortedThreatList()) { if (Player* tempTarget = ref->GetVictim()->ToPlayer()) diff --git a/src/server/scripts/Outland/zone_hellfire_peninsula.cpp b/src/server/scripts/Outland/zone_hellfire_peninsula.cpp index 5e62d752d6..d13e736c61 100644 --- a/src/server/scripts/Outland/zone_hellfire_peninsula.cpp +++ b/src/server/scripts/Outland/zone_hellfire_peninsula.cpp @@ -101,7 +101,7 @@ public: me->SetFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_QUESTGIVER); me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); Talk(SAY_FREE); return; } @@ -981,7 +981,7 @@ public: me->RestoreFaction(); me->RemoveAllAuras(); me->CombatStop(true); - me->GetThreatManager().NotifyDisengaged(); + EngagementOver(); me->SetFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_QUESTGIVER); me->SetImmuneToPC(true); Talk(SAY_DEFEATED);