TrinityCore
Loading...
Searching...
No Matches
ThreatManager.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "ThreatManager.h"
19#include "Creature.h"
20#include "CreatureAI.h"
21#include "CreatureGroups.h"
22#include "MapUtils.h"
23#include "MotionMaster.h"
24#include "Player.h"
25#include "TemporarySummon.h"
26#include "Unit.h"
27#include "UnitAI.h"
28#include "UnitDefines.h"
29#include "SpellAuraEffects.h"
30#include "SpellInfo.h"
31#include "SpellMgr.h"
32#include "ObjectAccessor.h"
33#include "WorldPacket.h"
34#include <algorithm>
35#include <boost/heap/fibonacci_heap.hpp>
36
38
39class ThreatManager::Heap : public boost::heap::fibonacci_heap<ThreatReference const*, boost::heap::compare<CompareThreatLessThan>>
40{
41};
42
44{
45 if (amount == 0.0f)
46 return;
47 _baseAmount = std::max<float>(_baseAmount + amount, 0.0f);
48 if (amount > 0.0f)
50 else
53}
54
56{
57 if (factor == 1.0f)
58 return;
59 _baseAmount *= factor;
60 if (factor > 1.0f)
62 else
65}
66
68{
69 bool const shouldBeOffline = ShouldBeOffline();
70 if (shouldBeOffline == IsOffline())
71 return;
72
73 if (shouldBeOffline)
74 {
78 }
79 else
80 {
84 }
85}
86
87/*static*/ bool ThreatReference::FlagsAllowFighting(Unit const* a, Unit const* b)
88{
89 if (a->GetTypeId() == TYPEID_UNIT && a->ToCreature()->IsTrigger())
90 return false;
92 {
94 return false;
95 }
96 else
97 {
99 return false;
100 }
101 return true;
102}
103
105{
107 return true;
109 return true;
111 return true;
112 return false;
113}
114
116{
117 if (IsTaunting()) // a taunting victim can never be suppressed
118 return false;
120 return true;
122 return true;
124 return true;
125 return false;
126}
127
129{
130 // Check for SPELL_AURA_MOD_DETAUNT (applied from owner to victim)
131 if (state < TAUNT_STATE_TAUNT && _victim->HasAuraTypeWithCaster(SPELL_AURA_MOD_DETAUNT, _owner->GetGUID()))
132 state = TAUNT_STATE_DETAUNT;
133
134 if (state == _taunted)
135 return;
136
137 std::swap(state, _taunted);
138
139 if (_taunted < state)
141 else
143
144 _mgr._needClientUpdate = true;
145}
146
151
158
160{
161public:
162 explicit ThreatReferenceImpl(ThreatManager* mgr, Unit* victim) : ThreatReference(mgr, victim) { }
163
164 ThreatManager::Heap::handle_type _handle;
165};
166
168{
169 _mgr._sortedThreatList->increase(static_cast<ThreatReferenceImpl*>(this)->_handle);
170}
171
173{
174 _mgr._sortedThreatList->decrease(static_cast<ThreatReferenceImpl*>(this)->_handle);
175}
176
177/*static*/ bool ThreatManager::CanHaveThreatList(Unit const* who)
178{
179 Creature const* cWho = who->ToCreature();
180 // only creatures can have threat list
181 if (!cWho)
182 return false;
183
184 // pets, totems and triggers cannot have threat list
185 if (cWho->IsPet() || cWho->IsTotem() || cWho->IsTrigger())
186 return false;
187
188 // summons cannot have a threat list if they were summoned by a player
190 if (TempSummon const* tWho = cWho->ToTempSummon())
191 if (tWho->GetSummonerGUID().IsPlayer())
192 return false;
193
194 return true;
195}
196
197ThreatManager::ThreatManager(Unit* owner) : _owner(owner), _ownerCanHaveThreatList(false), _needClientUpdate(false), _updateTimer(THREAT_UPDATE_INTERVAL),
198 _sortedThreatList(std::make_unique<Heap>()), _currentVictimRef(nullptr), _fixateRef(nullptr)
199{
200 for (int8 i = 0; i < MAX_SPELL_SCHOOL; ++i)
201 _singleSchoolModifiers[i] = 1.0f;
202}
203
205{
206 ASSERT(_myThreatListEntries.empty(), "ThreatManager::~ThreatManager - %s: we still have %zu things threatening us, one of them is %s.", _owner->GetGUID().ToString().c_str(), _myThreatListEntries.size(), _myThreatListEntries.begin()->first.ToString().c_str());
207 ASSERT(_sortedThreatList->empty(), "ThreatManager::~ThreatManager - %s: we still have %zu things threatening us, one of them is %s.", _owner->GetGUID().ToString().c_str(), _sortedThreatList->size(), (*_sortedThreatList->begin())->GetVictim()->GetGUID().ToString().c_str());
208 ASSERT(_threatenedByMe.empty(), "ThreatManager::~ThreatManager - %s: we are still threatening %zu things, one of them is %s.", _owner->GetGUID().ToString().c_str(), _threatenedByMe.size(), _threatenedByMe.begin()->first.ToString().c_str());
209}
210
215
217{
218 if (!CanHaveThreatList() || IsThreatListEmpty(true))
219 return;
220 if (_updateTimer <= tdiff)
221 {
222 UpdateVictim();
224 }
225 else
226 _updateTimer -= tdiff;
227}
228
236
238{
241 return nullptr;
242}
243
245{
246 for (ThreatReference const* ref : *_sortedThreatList)
247 if (!ref->IsOffline())
248 return ref->GetVictim();
249 return nullptr;
250}
251
252bool ThreatManager::IsThreatListEmpty(bool includeOffline) const
253{
254 if (includeOffline)
255 return _sortedThreatList->empty();
256 for (ThreatReference const* ref : *_sortedThreatList)
257 if (ref->IsAvailable())
258 return false;
259 return true;
260}
261
262bool ThreatManager::IsThreatenedBy(ObjectGuid const& who, bool includeOffline) const
263{
264 auto it = _myThreatListEntries.find(who);
265 if (it == _myThreatListEntries.end())
266 return false;
267 return (includeOffline || it->second->IsAvailable());
268}
269bool ThreatManager::IsThreatenedBy(Unit const* who, bool includeOffline) const { return IsThreatenedBy(who->GetGUID(), includeOffline); }
270
271float ThreatManager::GetThreat(Unit const* who, bool includeOffline) const
272{
273 auto it = _myThreatListEntries.find(who->GetGUID());
274 if (it == _myThreatListEntries.end())
275 return 0.0f;
276 return (includeOffline || it->second->IsAvailable()) ? it->second->GetThreat() : 0.0f;
277}
278
280{
281 return _sortedThreatList->size();
282}
283
284uint32 ThreatManager::GetThreatListPlayerCount(bool includeOffline/* = false*/) const
285{
286 if (includeOffline)
287 return uint32(_sortedThreatList->size());
288 uint32 returnValue = 0;
289 for (ThreatReference const* ref : *_sortedThreatList)
290 if (ref->IsAvailable() && ref->GetOwner()->GetTypeId() == TYPEID_PLAYER)
291 ++returnValue;
292 return returnValue;
293}
294
296{
297 auto itr = _myThreatListEntries.begin();
298 auto end = _myThreatListEntries.end();
299 std::function<ThreatReference const* ()> generator = [itr, end]() mutable -> ThreatReference const*
300 {
301 if (itr == end)
302 return nullptr;
303
304 return (itr++)->second;
305 };
306 return { ThreatListIterator{ std::move(generator) }, nullptr };
307}
308
310{
311 auto itr = _sortedThreatList->ordered_begin();
312 auto end = _sortedThreatList->ordered_end();
313 std::function<ThreatReference const* ()> generator = [itr, end]() mutable -> ThreatReference const*
314 {
315 if (itr == end)
316 return nullptr;
317
318 return *(itr++);
319 };
320 return { ThreatListIterator{ std::move(generator) }, nullptr };
321}
322
323std::vector<ThreatReference*> ThreatManager::GetModifiableThreatList()
324{
325 std::vector<ThreatReference*> list;
326 list.reserve(_myThreatListEntries.size());
327 for (auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end(); it != end; ++it)
328 list.push_back(const_cast<ThreatReference*>(*it));
329 return list;
330}
331
332bool ThreatManager::IsThreateningAnyone(bool includeOffline) const
333{
334 if (includeOffline)
335 return !_threatenedByMe.empty();
336 for (auto const& pair : _threatenedByMe)
337 if (pair.second->IsAvailable())
338 return true;
339 return false;
340}
341
342bool ThreatManager::IsThreateningTo(ObjectGuid const& who, bool includeOffline) const
343{
344 auto it = _threatenedByMe.find(who);
345 if (it == _threatenedByMe.end())
346 return false;
347 return (includeOffline || it->second->IsAvailable());
348}
349bool ThreatManager::IsThreateningTo(Unit const* who, bool includeOffline) const { return IsThreateningTo(who->GetGUID(), includeOffline); }
350
352{
353 for (auto const& pair : _threatenedByMe)
354 {
355 bool const shouldBeSuppressed = pair.second->ShouldBeSuppressed();
356 if (pair.second->IsOnline() && shouldBeSuppressed)
357 {
358 pair.second->_online = ThreatReference::ONLINE_STATE_SUPPRESSED;
359 pair.second->HeapNotifyDecreased();
360 }
361 else if (canExpire && pair.second->IsSuppressed() && !shouldBeSuppressed)
362 {
363 pair.second->_online = ThreatReference::ONLINE_STATE_ONLINE;
364 pair.second->HeapNotifyIncreased();
365 }
366 }
367}
368
369void ThreatManager::AddThreat(Unit* target, float amount, SpellInfo const* spell, bool ignoreModifiers, bool ignoreRedirects)
370{
371 // step 1: we can shortcut if the spell has one of the NO_THREAT attrs set - nothing will happen
372 if (spell)
373 {
375 return;
377 return;
378 }
379
380 // while riding a vehicle, all threat goes to the vehicle, not the pilot
381 if (Unit* vehicle = target->GetVehicleBase())
382 {
383 AddThreat(vehicle, amount, spell, ignoreModifiers, ignoreRedirects);
384 if (target->HasUnitTypeMask(UNIT_MASK_ACCESSORY)) // accessories are fully treated as components of the parent and cannot have threat
385 return;
386 amount = 0.0f;
387 }
388
389 // If victim is personal spawn, redirect all aggro to summoner
390 if (target->IsPrivateObject() && (!GetOwner()->IsPrivateObject() || !GetOwner()->CheckPrivateObjectOwnerVisibility(target)))
391 {
392 if (Unit* privateObjectOwner = ObjectAccessor::GetUnit(*GetOwner(), target->GetPrivateObjectOwner()))
393 {
394 AddThreat(privateObjectOwner, amount, spell, ignoreModifiers, ignoreRedirects);
395 amount = 0.0f;
396 }
397 }
398
399 // if we cannot actually have a threat list, we instead just set combat state and avoid creating threat refs altogether
400 if (!CanHaveThreatList())
401 {
402 CombatManager& combatMgr = _owner->GetCombatManager();
403 if (!combatMgr.SetInCombatWith(target))
404 return;
405 // traverse redirects and put them in combat, too
406 for (auto const& pair : target->GetThreatManager()._redirectInfo)
407 if (!combatMgr.IsInCombatWith(pair.first))
408 if (Unit* redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first))
409 combatMgr.SetInCombatWith(redirTarget);
410 return;
411 }
412
413 // apply threat modifiers to the amount
414 if (!ignoreModifiers)
415 amount = CalculateModifiedThreat(amount, target, spell);
416
417 // if we're increasing threat, send some/all of it to redirection targets instead if applicable
418 if (!ignoreRedirects && amount > 0.0f)
419 {
420 auto const& redirInfo = target->GetThreatManager()._redirectInfo;
421 if (!redirInfo.empty())
422 {
423 float const origAmount = amount;
424 // intentional iteration by index - there's a nested AddThreat call further down that might cause AI calls which might modify redirect info through spells
425 for (size_t i = 0; i < redirInfo.size(); ++i)
426 {
427 auto const pair = redirInfo[i]; // (victim,pct)
428 Unit* redirTarget = nullptr;
429 auto it = _myThreatListEntries.find(pair.first); // try to look it up in our threat list first (faster)
430 if (it != _myThreatListEntries.end())
431 redirTarget = it->second->_victim;
432 else
433 redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first);
434
435 if (redirTarget)
436 {
437 float amountRedirected = CalculatePct(origAmount, pair.second);
438 AddThreat(redirTarget, amountRedirected, spell, true, true);
439 amount -= amountRedirected;
440 }
441 }
442 }
443 }
444
445 // ensure we're in combat (threat implies combat!)
446 if (!_owner->GetCombatManager().SetInCombatWith(target)) // if this returns false, we're not actually in combat, and thus cannot have threat!
447 return; // typical causes: bad scripts trying to add threat to GMs, dead targets etc
448
449 // ok, now we actually apply threat
450 // check if we already have an entry - if we do, just increase threat for that entry and we're done
451 auto it = _myThreatListEntries.find(target->GetGUID());
452 if (it != _myThreatListEntries.end())
453 {
454 ThreatReference* const ref = it->second;
455 // SUPPRESSED threat states don't go back to ONLINE until threat is caused by them (retail behavior)
457 if (!ref->ShouldBeSuppressed())
458 {
460 ref->HeapNotifyIncreased();
461 }
462
463 if (ref->IsOnline())
464 ref->AddThreat(amount);
465 return;
466 }
467
468 // ok, we're now in combat - create the threat list reference and push it to the respective managers
469 ThreatReference* ref = new ThreatReferenceImpl(this, target);
470 PutThreatListRef(target->GetGUID(), ref);
472
473 ref->UpdateOffline();
474 if (ref->IsOnline()) // we only add the threat if the ref is currently available
475 ref->AddThreat(amount);
476
478 UpdateVictim();
479 else
481}
482
483void ThreatManager::ScaleThreat(Unit* target, float factor)
484{
485 auto it = _myThreatListEntries.find(target->GetGUID());
486 if (it != _myThreatListEntries.end())
487 it->second->ScaleThreat(std::max<float>(factor,0.0f));
488}
489
491{
492 if (_sortedThreatList->empty())
493 return;
494
495 auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end();
496 ThreatReference const* highest = *it;
497 if (!highest->IsAvailable())
498 return;
499
500 if (highest->IsTaunting() && ((++it) != end)) // might need to skip this - max threat could be the preceding element (there is only one taunt element)
501 {
502 ThreatReference const* a = *it;
503 if (a->IsAvailable() && a->GetThreat() > highest->GetThreat())
504 highest = a;
505 }
506
507 AddThreat(target, highest->GetThreat() - GetThreat(target, true), nullptr, true, true);
508}
509
511{
512 std::list<AuraEffect*> const& tauntEffects = _owner->GetAuraEffectsByType(SPELL_AURA_MOD_TAUNT);
513
515 std::unordered_map<ObjectGuid, ThreatReference::TauntState> tauntStates;
516 // Only the last taunt effect applied by something still on our threat list is considered
517 for (auto it = tauntEffects.begin(), end = tauntEffects.end(); it != end; ++it)
518 tauntStates[(*it)->GetCasterGUID()] = ThreatReference::TauntState(state++);
519
520 for (auto const& pair : _myThreatListEntries)
521 {
522 auto it = tauntStates.find(pair.first);
523 if (it != tauntStates.end())
524 pair.second->UpdateTauntState(it->second);
525 else
526 pair.second->UpdateTauntState();
527 }
528
529 // taunt aura update also re-evaluates all suppressed states (retail behavior)
530 EvaluateSuppressed(true);
531}
532
534{
535 for (auto const& pair : _myThreatListEntries)
536 pair.second->ScaleThreat(0.0f);
537}
538
540{
541 auto it = _myThreatListEntries.find(target->GetGUID());
542 if (it != _myThreatListEntries.end())
543 ClearThreat(it->second);
544}
545
553
555{
556 if (!_myThreatListEntries.empty())
557 {
559 do
560 _myThreatListEntries.begin()->second->UnregisterAndFree();
561 while (!_myThreatListEntries.empty());
562 }
563}
564
566{
567 if (target)
568 {
569 auto it = _myThreatListEntries.find(target->GetGUID());
570 if (it != _myThreatListEntries.end())
571 {
572 _fixateRef = it->second;
573 return;
574 }
575 }
576 _fixateRef = nullptr;
577}
578
580{
581 if (_fixateRef)
582 return _fixateRef->GetVictim();
583 else
584 return nullptr;
585}
586
588{
589 ThreatReference const* const newVictim = ReselectVictim();
590 bool const newHighest = newVictim && (newVictim != _currentVictimRef);
591
592 _currentVictimRef = newVictim;
593 if (newHighest || _needClientUpdate)
594 {
595 SendThreatListToClients(newHighest);
596 _needClientUpdate = false;
597 }
598
600}
601
603{
604 if (_sortedThreatList->empty())
605 return nullptr;
606
607 for (auto const& pair : _myThreatListEntries)
608 pair.second->UpdateOffline(); // AI notifies are processed in ::UpdateVictim caller
609
610 // fixated target is always preferred
612 return _fixateRef;
613
614 ThreatReference const* oldVictimRef = _currentVictimRef;
615 if (oldVictimRef && oldVictimRef->IsOffline())
616 oldVictimRef = nullptr;
617 // in 99% of cases - we won't need to actually look at anything beyond the first element
618 ThreatReference const* highest = _sortedThreatList->top();
619 // if the highest reference is offline, the entire list is offline, and we indicate this
620 if (!highest->IsAvailable())
621 return nullptr;
622 // if we have no old victim, or old victim is still highest, then highest is our target and we're done
623 if (!oldVictimRef || highest == oldVictimRef)
624 return highest;
625 // if highest threat doesn't break 110% of old victim, nothing below it is going to do so either; new victim = old victim and done
626 if (!ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.1f))
627 return oldVictimRef;
628 // if highest threat breaks 130%, it's our new target regardless of range (and we're done)
629 if (ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.3f))
630 return highest;
631 // if it doesn't break 130%, we need to check if it's melee - if yes, it breaks 110% (we checked earlier) and is our new target
632 if (_owner->IsWithinMeleeRange(highest->_victim))
633 return highest;
634 // If we get here, highest threat is ranged, but below 130% of current - there might be a melee that breaks 110% below us somewhere, so now we need to actually look at the next highest element
635 // luckily, this is a heap, so getting the next highest element is O(log n), and we're just gonna do that repeatedly until we've seen enough targets (or find a target)
636 auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end();
637 while (it != end)
638 {
639 ThreatReference const* next = *it;
640 // if we've found current victim, we're done (nothing above is higher, and nothing below can be higher)
641 if (next == oldVictimRef)
642 return next;
643 // if next isn't above 110% threat, then nothing below it can be either - we're done, old victim stays
644 if (!ThreatManager::CompareReferencesLT(oldVictimRef, next, 1.1f))
645 return oldVictimRef;
646 // if next is melee, he's above 110% and our new victim
648 return next;
649 // otherwise the next highest target may still be a melee above 110% and we need to look further
650 ++it;
651 }
652 // we should have found the old victim at some point in the loop above, so execution should never get to this point
653 ABORT_MSG("Current victim not found in sorted threat list even though it has a reference - manager desync!");
654 return nullptr;
655}
656
658{
660 std::vector<ObjectGuid> v(std::move(_needsAIUpdate)); // _needsAIUpdate is now empty in case this triggers a recursive call
661 if (!ai)
662 return;
663 for (ObjectGuid const& guid : v)
665 ai->JustStartedThreateningMe(ref->GetVictim());
666}
667
668// returns true if a is LOWER on the threat list than b
669/*static*/ bool ThreatManager::CompareReferencesLT(ThreatReference const* a, ThreatReference const* b, float aWeight)
670{
671 if (a->_online != b->_online) // online state precedence (ONLINE > SUPPRESSED > OFFLINE)
672 return a->_online < b->_online;
673 if (a->_taunted != b->_taunted) // taunt state precedence (TAUNT > NONE > DETAUNT)
674 return a->_taunted < b->_taunted;
675 return (a->GetThreat()*aWeight < b->GetThreat());
676}
677
678/*static*/ float ThreatManager::CalculateModifiedThreat(float threat, Unit const* victim, SpellInfo const* spell)
679{
680 // modifiers by spell
681 if (spell)
682 {
683 if (SpellThreatEntry const* threatEntry = sSpellMgr->GetSpellThreatEntry(spell->Id))
684 if (threatEntry->pctMod != 1.0f) // flat/AP modifiers handled in Spell::HandleThreatSpells
685 threat *= threatEntry->pctMod;
686
687 if (Player* modOwner = victim->GetSpellModOwner())
688 modOwner->ApplySpellMod(spell->Id, SPELLMOD_THREAT, threat);
689 }
690
691 // modifiers by effect school
692 ThreatManager const& victimMgr = victim->GetThreatManager();
693 SpellSchoolMask const mask = spell ? spell->GetSchoolMask() : SPELL_SCHOOL_MASK_NORMAL;
694 switch (mask)
695 {
697 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NORMAL];
698 break;
700 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_HOLY];
701 break;
703 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FIRE];
704 break;
706 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NATURE];
707 break;
709 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FROST];
710 break;
712 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_SHADOW];
713 break;
715 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_ARCANE];
716 break;
717 default:
718 {
719 auto it = victimMgr._multiSchoolModifiers.find(mask);
720 if (it != victimMgr._multiSchoolModifiers.end())
721 {
722 threat *= it->second;
723 break;
724 }
726 victimMgr._multiSchoolModifiers[mask] = mod;
727 threat *= mod;
728 break;
729 }
730 }
731 return threat;
732}
733
734void ThreatManager::ForwardThreatForAssistingMe(Unit* assistant, float baseAmount, SpellInfo const* spell, bool ignoreModifiers)
735{
736 if (spell && spell->HasAttribute(SPELL_ATTR1_NO_THREAT)) // shortcut, none of the calls would do anything
737 return;
738 if (_threatenedByMe.empty())
739 return;
740
741 std::vector<Creature*> canBeThreatened, cannotBeThreatened;
742 for (auto const& pair : _threatenedByMe)
743 {
744 Creature* owner = pair.second->GetOwner();
746 canBeThreatened.push_back(owner);
747 else
748 cannotBeThreatened.push_back(owner);
749 }
750
751 if (!canBeThreatened.empty()) // targets under CC cannot gain assist threat - split evenly among the rest
752 {
753 float const perTarget = baseAmount / canBeThreatened.size();
754 for (Creature* threatened : canBeThreatened)
755 threatened->GetThreatManager().AddThreat(assistant, perTarget, spell, ignoreModifiers);
756 }
757
758 for (Creature* threatened : cannotBeThreatened)
759 threatened->GetThreatManager().AddThreat(assistant, 0.0f, spell, true);
760}
761
763{
764 while (!_threatenedByMe.empty())
765 {
766 auto& ref = _threatenedByMe.begin()->second;
767 ref->_mgr.ClearThreat(_owner);
768 }
769}
770
772{
773 int32 mod = 0;
775 mod += eff->GetAmount();
776
777 if (_threatenedByMe.empty())
778 return;
779
780 auto it = _threatenedByMe.begin();
781 bool const isIncrease = (it->second->_tempModifier < mod);
782 do
783 {
784 it->second->_tempModifier = mod;
785 if (isIncrease)
786 it->second->HeapNotifyIncreased();
787 else
788 it->second->HeapNotifyDecreased();
789 } while ((++it) != _threatenedByMe.end());
790}
791
798
800{
801 _redirectRegistry[spellId][victim] = pct;
803}
804
806{
807 auto it = _redirectRegistry.find(spellId);
808 if (it == _redirectRegistry.end())
809 return;
810 _redirectRegistry.erase(it);
812}
813
815{
816 auto it = _redirectRegistry.find(spellId);
817 if (it == _redirectRegistry.end())
818 return;
819 auto& victimMap = it->second;
820 auto it2 = victimMap.find(victim);
821 if (it2 == victimMap.end())
822 return;
823 victimMap.erase(it2);
825}
826
828{
830 data << _owner->GetPackGUID();
831 _owner->SendMessageToSet(&data, false);
832}
833
835{
837 data << _owner->GetPackGUID();
838 data << victim->GetPackGUID();
839 _owner->SendMessageToSet(&data, false);
840}
841
842void ThreatManager::SendThreatListToClients(bool newHighest) const
843{
844 WorldPacket data(newHighest ? SMSG_HIGHEST_THREAT_UPDATE : SMSG_THREAT_UPDATE, (_sortedThreatList->size() + 2) * 8); // guess
845 data << _owner->GetPackGUID();
846 if (newHighest)
848 size_t countPos = data.wpos();
849 data << uint32(0); // placeholder
850 uint32 count = 0;
851 for (ThreatReference const* ref : *_sortedThreatList)
852 {
853 if (!ref->IsAvailable())
854 continue;
855 data << ref->GetVictim()->GetPackGUID();
856 data << uint32(ref->GetThreat() * 100);
857 ++count;
858 }
859 data.put<uint32>(countPos, count);
860 _owner->SendMessageToSet(&data, false);
861}
862
864{
865 _needClientUpdate = true;
866 auto& inMap = _myThreatListEntries[guid];
867 ASSERT(!inMap, "Duplicate threat reference at %p being inserted on %s for %s - memory leak!", ref, _owner->GetGUID().ToString().c_str(), guid.ToString().c_str());
868 inMap = ref;
869 static_cast<ThreatReferenceImpl*>(ref)->_handle = _sortedThreatList->push(ref);
870}
871
873{
874 auto it = _myThreatListEntries.find(guid);
875 if (it == _myThreatListEntries.end())
876 return;
877 ThreatReference* ref = it->second;
878 _myThreatListEntries.erase(it);
879 _sortedThreatList->erase(static_cast<ThreatReferenceImpl*>(ref)->_handle);
880
881 if (_fixateRef == ref)
882 _fixateRef = nullptr;
883 if (_currentVictimRef == ref)
884 _currentVictimRef = nullptr;
885}
886
888{
889 auto& inMap = _threatenedByMe[guid];
890 ASSERT(!inMap, "Duplicate threatened-by-me reference at %p being inserted on %s for %s - memory leak!", ref, _owner->GetGUID().ToString().c_str(), guid.ToString().c_str());
891 inMap = ref;
892}
893
895{
896 auto it = _threatenedByMe.find(guid);
897 if (it != _threatenedByMe.end())
898 _threatenedByMe.erase(it);
899}
900
902{
903 _redirectInfo.clear();
904 uint32 totalPct = 0;
905 for (auto const& pair : _redirectRegistry) // (spellid, victim -> pct)
906 for (auto const& victimPair : pair.second) // (victim,pct)
907 {
908 uint32 thisPct = std::min<uint32>(100 - totalPct, victimPair.second);
909 if (thisPct > 0)
910 {
911 _redirectInfo.push_back({ victimPair.first, thisPct });
912 totalPct += thisPct;
913 ASSERT(totalPct <= 100);
914 if (totalPct == 100)
915 return;
916 }
917 }
918}
uint8_t uint8
Definition Define.h:135
int8_t int8
Definition Define.h:131
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
#define ABORT_MSG
Definition Errors.h:75
#define ASSERT_NOTNULL(pointer)
Definition Errors.h:84
#define ASSERT
Definition Errors.h:68
@ TYPEID_UNIT
Definition ObjectGuid.h:38
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
@ SPELL_SCHOOL_SHADOW
@ SPELL_SCHOOL_NORMAL
@ SPELL_SCHOOL_NATURE
@ SPELL_SCHOOL_FROST
@ SPELL_SCHOOL_ARCANE
@ SPELL_SCHOOL_FIRE
@ SPELL_SCHOOL_HOLY
@ MAX_SPELL_SCHOOL
SpellSchoolMask
@ SPELL_SCHOOL_MASK_NORMAL
@ SPELL_SCHOOL_MASK_SHADOW
@ SPELL_SCHOOL_MASK_ARCANE
@ SPELL_SCHOOL_MASK_NATURE
@ SPELL_SCHOOL_MASK_HOLY
@ SPELL_SCHOOL_MASK_FIRE
@ SPELL_SCHOOL_MASK_FROST
@ SPELL_ATTR1_NO_THREAT
@ SPELL_ATTR3_NO_INITIAL_AGGRO
@ SPELL_AURA_MOD_TOTAL_THREAT
@ SPELL_AURA_MOD_THREAT
@ SPELL_AURA_MOD_DETAUNT
@ SPELL_AURA_MOD_TAUNT
@ SPELL_AURA_MOD_CONFUSE
@ SPELL_AURA_MOD_STUN
@ SPELLMOD_THREAT
#define sSpellMgr
Definition SpellMgr.h:738
@ UNIT_FLAG_IMMUNE_TO_NPC
@ UNIT_FLAG_IMMUNE_TO_PC
@ UNIT_FLAG_PLAYER_CONTROLLED
@ UNIT_MASK_ACCESSORY
Definition Unit.h:373
@ UNIT_MASK_GUARDIAN
Definition Unit.h:366
@ UNIT_MASK_MINION
Definition Unit.h:365
@ UNIT_STATE_CONTROLLED
Definition Unit.h:260
T CalculatePct(T base, U pct)
Definition Util.h:71
size_t wpos() const
Definition ByteBuffer.h:321
void put(std::size_t pos, T value)
Definition ByteBuffer.h:137
bool SetInCombatWith(Unit *who, bool addSecondUnitSuppressed=false)
bool IsInCombatWith(ObjectGuid const &who) const
virtual void JustStartedThreateningMe(Unit *who)
Definition CreatureAI.h:124
bool IsTrigger() const
Definition Creature.h:98
bool _IsTargetAcceptable(Unit const *target) const
bool CanCreatureAttack(Unit const *victim, bool force=true) const
SpellSchoolMask GetMeleeDamageSchoolMask(WeaponAttackType=BASE_ATTACK, uint8=0) const override
Definition Creature.h:158
std::string ToString() const
static Creature * ToCreature(Object *o)
Definition Object.h:186
PackedGuid const & GetPackGUID() const
Definition Object.h:80
TypeID GetTypeId() const
Definition Object.h:93
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
uint32 Id
Definition SpellInfo.h:289
SpellSchoolMask GetSchoolMask() const
bool HasAttribute(SpellAttr0 attribute) const
Definition SpellInfo.h:375
std::unordered_map< ObjectGuid, ThreatReference * > _threatenedByMe
bool CanHaveThreatList() const
ThreatManager(Unit *owner)
static float CalculateModifiedThreat(float threat, Unit const *victim, SpellInfo const *spell)
void SendClearAllThreatToClients() const
void EvaluateSuppressed(bool canExpire=false)
void UnregisterRedirectThreat(uint32 spellId)
void ForwardThreatForAssistingMe(Unit *assistant, float baseAmount, SpellInfo const *spell=nullptr, bool ignoreModifiers=false)
== AFFECT OTHERS' THREAT LISTS ==
std::vector< std::pair< ObjectGuid, uint32 > > _redirectInfo
bool IsThreatenedBy(ObjectGuid const &who, bool includeOffline=false) const
void RemoveMeFromThreatLists()
void PutThreatListRef(ObjectGuid const &guid, ThreatReference *ref)
== MY THREAT LIST ==
bool IsThreateningTo(ObjectGuid const &who, bool includeOffline=false) const
Unit * GetCurrentVictim()
static const uint32 THREAT_UPDATE_INTERVAL
void PutThreatenedByMeRef(ObjectGuid const &guid, ThreatReference *ref)
== OTHERS' THREAT LISTS ==
void RegisterForAIUpdate(ObjectGuid const &guid)
void SendThreatListToClients(bool newHighest) const
void UpdateRedirectInfo()
std::array< float, MAX_SPELL_SCHOOL > _singleSchoolModifiers
void UpdateMySpellSchoolModifiers()
static bool CompareReferencesLT(ThreatReference const *a, ThreatReference const *b, float aWeight)
Trinity::IteratorPair< ThreatListIterator, std::nullptr_t > GetUnsortedThreatList() const
std::unordered_map< uint32, std::unordered_map< ObjectGuid, uint32 > > _redirectRegistry
static const CompareThreatLessThan CompareThreat
std::unique_ptr< Heap > _sortedThreatList
void ScaleThreat(Unit *target, float factor)
void ClearThreat(Unit *target)
ThreatReference const * _currentVictimRef
Unit * GetAnyTarget() const
Unit * GetLastVictim() const
std::unordered_map< ObjectGuid, ThreatReference * > _myThreatListEntries
bool _ownerCanHaveThreatList
void FixateTarget(Unit *target)
bool IsThreatListEmpty(bool includeOffline=false) const
std::vector< ObjectGuid > _needsAIUpdate
void Update(uint32 tdiff)
void PurgeThreatListRef(ObjectGuid const &guid)
Unit *const _owner
void AddThreat(Unit *target, float amount, SpellInfo const *spell=nullptr, bool ignoreModifiers=false, bool ignoreRedirects=false)
== AFFECT MY THREAT LIST ==
bool IsThreateningAnyone(bool includeOffline=false) const
ThreatReference const * _fixateRef
ThreatReference const * ReselectVictim()
void SendRemoveToClients(Unit const *victim) const
uint32 GetThreatListPlayerCount(bool includeOffline=false) const
Trinity::IteratorPair< ThreatListIterator, std::nullptr_t > GetSortedThreatList() const
std::vector< ThreatReference * > GetModifiableThreatList()
void RegisterRedirectThreat(uint32 spellId, ObjectGuid const &victim, uint32 pct)
== REDIRECT SYSTEM ==
std::unordered_map< std::underlying_type< SpellSchoolMask >::type, float > _multiSchoolModifiers
Unit * GetFixateTarget() const
Unit * GetOwner() const
friend class ThreatReferenceImpl
void UpdateMyTempModifiers()
void PurgeThreatenedByMeRef(ObjectGuid const &guid)
float GetThreat(Unit const *who, bool includeOffline=false) const
void MatchUnitThreatToHighestThreat(Unit *target)
size_t GetThreatListSize() const
ThreatManager::Heap::handle_type _handle
ThreatReferenceImpl(ThreatManager *mgr, Unit *victim)
static bool FlagsAllowFighting(Unit const *a, Unit const *b)
void ScaleThreat(float factor)
bool IsTaunting() const
bool ShouldBeSuppressed() const
ThreatManager & _mgr
OnlineState GetOnlineState() const
Creature *const _owner
bool IsOnline() const
bool ShouldBeOffline() const
Unit *const _victim
TauntState _taunted
bool IsOffline() const
float GetThreat() const
OnlineState _online
void UpdateTauntState(TauntState state=TAUNT_STATE_NONE)
Unit * GetVictim() const
void AddThreat(float amount)
bool IsAvailable() const
Utility class to enable range for loop syntax for multimap.equal_range uses.
constexpr iterator begin() const
Definition Unit.h:769
bool IsWithinMeleeRange(Unit const *obj) const
Definition Unit.h:844
AuraEffectList const & GetAuraEffectsByType(AuraType type) const
Definition Unit.h:1384
ThreatManager & GetThreatManager()
Definition Unit.h:1155
Unit * GetVehicleBase() const
Definition Unit.cpp:11826
bool IsPet() const
Definition Unit.h:884
bool HasUnitFlag(UnitFlags flags) const
Definition Unit.h:953
TempSummon * ToTempSummon()
Definition Unit.h:1794
bool HasAuraType(AuraType auraType) const
Definition Unit.cpp:4542
Unit * GetVictim() const
Definition Unit.h:859
bool IsImmunedToDamage(SpellSchoolMask meleeSchoolMask) const
Definition Unit.cpp:7639
float GetTotalAuraMultiplierByMiscMask(AuraType auraType, uint32 misc_mask) const
Definition Unit.cpp:4822
bool HasUnitState(const uint32 f) const
Definition Unit.h:876
uint32 HasUnitTypeMask(uint32 mask) const
Definition Unit.h:880
bool HasBreakableByDamageAuraType(AuraType type, uint32 excludeAura=0) const
Definition Unit.cpp:683
CombatManager & GetCombatManager()
Definition Unit.h:1130
bool IsTotem() const
Definition Unit.h:886
virtual bool IsEngaged() const
Definition Unit.h:1126
virtual void SendMessageToSet(WorldPacket const *data, bool self) const
Definition Object.cpp:1783
ObjectGuid GetPrivateObjectOwner() const
Definition Object.h:595
Unit * GetOwner() const
Definition Object.cpp:2180
bool IsPrivateObject() const
Definition Object.h:594
bool CanSeeOrDetect(WorldObject const *obj, bool implicitDetect=false, bool distanceCheck=false, bool checkAlert=false) const
Definition Object.cpp:1547
Player * GetSpellModOwner() const
Definition Object.cpp:2223
@ SMSG_THREAT_UPDATE
Definition Opcodes.h:1184
@ SMSG_HIGHEST_THREAT_UPDATE
Definition Opcodes.h:1183
@ SMSG_THREAT_CLEAR
Definition Opcodes.h:1186
@ SMSG_THREAT_REMOVE
Definition Opcodes.h:1185
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:29
STL namespace.