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 "CombatPackets.h"
21#include "CreatureAI.h"
22#include "CreatureGroups.h"
23#include "MapUtils.h"
24#include "MotionMaster.h"
25#include "ObjectAccessor.h"
26#include "Player.h"
27#include "SpellAuraEffects.h"
28#include "SpellMgr.h"
29#include "TemporarySummon.h"
30#include <boost/heap/fibonacci_heap.hpp>
31
33
34class ThreatManager::Heap : public boost::heap::fibonacci_heap<ThreatReference const*, boost::heap::compare<CompareThreatLessThan>>
35{
36};
37
39{
40 if (amount == 0.0f)
41 return;
42 _baseAmount = std::max<float>(_baseAmount + amount, 0.0f);
43 if (amount > 0.0f)
45 else
48}
49
51{
52 if (factor == 1.0f)
53 return;
54 _baseAmount *= factor;
55 if (factor > 1.0f)
57 else
60}
61
63{
64 bool const shouldBeOffline = ShouldBeOffline();
65 if (shouldBeOffline == IsOffline())
66 return;
67
68 if (shouldBeOffline)
69 {
73 }
74 else
75 {
79 }
80}
81
82/*static*/ bool ThreatReference::FlagsAllowFighting(Unit const* a, Unit const* b)
83{
84 if (a->GetTypeId() == TYPEID_UNIT && a->ToCreature()->IsTrigger())
85 return false;
87 {
89 return false;
90 }
91 else
92 {
94 return false;
95 }
96 return true;
97}
98
100{
102 return true;
104 return true;
106 return true;
107 return false;
108}
109
111{
112 if (IsTaunting()) // a taunting victim can never be suppressed
113 return false;
115 return true;
117 return true;
119 return true;
120 return false;
121}
122
124{
125 // Check for SPELL_AURA_MOD_DETAUNT (applied from owner to victim)
126 if (state < TAUNT_STATE_TAUNT && _victim->HasAuraTypeWithCaster(SPELL_AURA_MOD_DETAUNT, _owner->GetGUID()))
127 state = TAUNT_STATE_DETAUNT;
128
129 if (state == _taunted)
130 return;
131
132 std::swap(state, _taunted);
133
134 if (_taunted < state)
136 else
138
139 _mgr._needClientUpdate = true;
140}
141
146
153
155{
156public:
157 explicit ThreatReferenceImpl(ThreatManager* mgr, Unit* victim) : ThreatReference(mgr, victim) { }
158
159 ThreatManager::Heap::handle_type _handle;
160};
161
163{
164 _mgr._sortedThreatList->increase(static_cast<ThreatReferenceImpl*>(this)->_handle);
165}
166
168{
169 _mgr._sortedThreatList->decrease(static_cast<ThreatReferenceImpl*>(this)->_handle);
170}
171
172/*static*/ bool ThreatManager::CanHaveThreatList(Unit const* who)
173{
174 Creature const* cWho = who->ToCreature();
175 // only creatures can have threat list
176 if (!cWho)
177 return false;
178
179 // pets, totems and triggers cannot have threat list
180 if (cWho->IsPet() || cWho->IsTotem() || cWho->IsTrigger())
181 return false;
182
183 // summons cannot have a threat list if they were summoned by a player
185 if (TempSummon const* tWho = cWho->ToTempSummon())
186 if (tWho->GetSummonerGUID().IsPlayer())
187 return false;
188
189 // accessories are fully treated as components of the parent and cannot have threat
191 return false;
192
193 return true;
194}
195
196ThreatManager::ThreatManager(Unit* owner) : _owner(owner), _ownerCanHaveThreatList(false), _needClientUpdate(false), _needThreatClearUpdate(false), _updateTimer(THREAT_UPDATE_INTERVAL),
197 _sortedThreatList(std::make_unique<Heap>()), _currentVictimRef(nullptr), _fixateRef(nullptr)
198{
199 for (int8 i = 0; i < MAX_SPELL_SCHOOL; ++i)
200 _singleSchoolModifiers[i] = 1.0f;
201}
202
204{
205 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());
206 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());
207 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());
208}
209
214
216{
217 if (!CanHaveThreatList())
218 return;
219
220 if (_updateTimer <= tdiff)
221 {
223 {
226 }
227
228 if (!IsThreatListEmpty(true))
229 UpdateVictim();
230
232 }
233 else
234 _updateTimer -= tdiff;
235}
236
241
249
251{
254 return nullptr;
255}
256
258{
259 for (ThreatReference const* ref : *_sortedThreatList)
260 if (!ref->IsOffline())
261 return ref->GetVictim();
262 return nullptr;
263}
264
265bool ThreatManager::IsThreatListEmpty(bool includeOffline) const
266{
267 if (includeOffline)
268 return _sortedThreatList->empty();
269 for (ThreatReference const* ref : *_sortedThreatList)
270 if (ref->IsAvailable())
271 return false;
272 return true;
273}
274
275bool ThreatManager::IsThreatenedBy(ObjectGuid const& who, bool includeOffline) const
276{
277 auto it = _myThreatListEntries.find(who);
278 if (it == _myThreatListEntries.end())
279 return false;
280 return (includeOffline || it->second->IsAvailable());
281}
282bool ThreatManager::IsThreatenedBy(Unit const* who, bool includeOffline) const { return IsThreatenedBy(who->GetGUID(), includeOffline); }
283
284float ThreatManager::GetThreat(Unit const* who, bool includeOffline) const
285{
286 auto it = _myThreatListEntries.find(who->GetGUID());
287 if (it == _myThreatListEntries.end())
288 return 0.0f;
289 return (includeOffline || it->second->IsAvailable()) ? it->second->GetThreat() : 0.0f;
290}
291
293{
294 return _sortedThreatList->size();
295}
296
298{
299 auto itr = _myThreatListEntries.begin();
300 auto end = _myThreatListEntries.end();
301 std::function<ThreatReference const* ()> generator = [itr, end]() mutable -> ThreatReference const*
302 {
303 if (itr == end)
304 return nullptr;
305
306 return (itr++)->second;
307 };
308 return { ThreatListIterator{ std::move(generator) }, nullptr };
309}
310
312{
313 auto itr = _sortedThreatList->ordered_begin();
314 auto end = _sortedThreatList->ordered_end();
315 std::function<ThreatReference const* ()> generator = [itr, end]() mutable -> ThreatReference const*
316 {
317 if (itr == end)
318 return nullptr;
319
320 return *(itr++);
321 };
322 return { ThreatListIterator{ std::move(generator) }, nullptr };
323}
324
325std::vector<ThreatReference*> ThreatManager::GetModifiableThreatList()
326{
327 std::vector<ThreatReference*> list;
328 list.reserve(_myThreatListEntries.size());
329 for (auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end(); it != end; ++it)
330 list.push_back(const_cast<ThreatReference*>(*it));
331 return list;
332}
333
334bool ThreatManager::IsThreateningAnyone(bool includeOffline) const
335{
336 if (includeOffline)
337 return !_threatenedByMe.empty();
338 for (auto const& pair : _threatenedByMe)
339 if (pair.second->IsAvailable())
340 return true;
341 return false;
342}
343
344bool ThreatManager::IsThreateningTo(ObjectGuid const& who, bool includeOffline) const
345{
346 auto it = _threatenedByMe.find(who);
347 if (it == _threatenedByMe.end())
348 return false;
349 return (includeOffline || it->second->IsAvailable());
350}
351bool ThreatManager::IsThreateningTo(Unit const* who, bool includeOffline) const { return IsThreateningTo(who->GetGUID(), includeOffline); }
352
354{
355 for (auto const& pair : _threatenedByMe)
356 {
357 bool const shouldBeSuppressed = pair.second->ShouldBeSuppressed();
358 if (pair.second->IsOnline() && shouldBeSuppressed)
359 {
360 pair.second->_online = ThreatReference::ONLINE_STATE_SUPPRESSED;
361 pair.second->HeapNotifyDecreased();
362 }
363 else if (canExpire && pair.second->IsSuppressed() && !shouldBeSuppressed)
364 {
365 pair.second->_online = ThreatReference::ONLINE_STATE_ONLINE;
366 pair.second->HeapNotifyIncreased();
367 }
368 }
369}
370
371void ThreatManager::AddThreat(Unit* target, float amount, SpellInfo const* spell, bool ignoreModifiers, bool ignoreRedirects)
372{
373 // step 1: we can shortcut if the spell has one of the NO_THREAT attrs set - nothing will happen
374 if (spell)
375 {
377 return;
379 return;
380 }
381
382 // if we cannot actually have a threat list, we instead just set combat state and avoid creating threat refs altogether
383 if (!CanHaveThreatList())
384 {
385 CombatManager& combatMgr = _owner->GetCombatManager();
386 if (!combatMgr.SetInCombatWith(target))
387 return;
388 // traverse redirects and put them in combat, too
389 for (auto const& pair : target->GetThreatManager()._redirectInfo)
390 if (!combatMgr.IsInCombatWith(pair.first))
391 if (Unit* redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first))
392 combatMgr.SetInCombatWith(redirTarget);
393 return;
394 }
395
396 // apply threat modifiers to the amount
397 if (!ignoreModifiers)
398 amount = CalculateModifiedThreat(amount, target, spell);
399
400 // if we're increasing threat, send some/all of it to redirection targets instead if applicable
401 if (!ignoreRedirects && amount > 0.0f)
402 {
403 auto const& redirInfo = target->GetThreatManager()._redirectInfo;
404 if (!redirInfo.empty())
405 {
406 float const origAmount = amount;
407 // intentional iteration by index - there's a nested AddThreat call further down that might cause AI calls which might modify redirect info through spells
408 for (size_t i = 0; i < redirInfo.size(); ++i)
409 {
410 auto const pair = redirInfo[i]; // (victim,pct)
411 Unit* redirTarget = nullptr;
412 auto it = _myThreatListEntries.find(pair.first); // try to look it up in our threat list first (faster)
413 if (it != _myThreatListEntries.end())
414 redirTarget = it->second->_victim;
415 else
416 redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first);
417
418 if (redirTarget)
419 {
420 float amountRedirected = CalculatePct(origAmount, pair.second);
421 AddThreat(redirTarget, amountRedirected, spell, true, true);
422 amount -= amountRedirected;
423 }
424 }
425 }
426 }
427
428 // ensure we're in combat (threat implies combat!)
429 if (!_owner->GetCombatManager().SetInCombatWith(target)) // if this returns false, we're not actually in combat, and thus cannot have threat!
430 return; // typical causes: bad scripts trying to add threat to GMs, dead targets etc
431
432 // ok, now we actually apply threat
433 // check if we already have an entry - if we do, just increase threat for that entry and we're done
434 auto it = _myThreatListEntries.find(target->GetGUID());
435 if (it != _myThreatListEntries.end())
436 {
437 ThreatReference* const ref = it->second;
438 // SUPPRESSED threat states don't go back to ONLINE until threat is caused by them (retail behavior)
440 if (!ref->ShouldBeSuppressed())
441 {
443 ref->HeapNotifyIncreased();
444 }
445
446 if (ref->IsOnline())
447 ref->AddThreat(amount);
448 return;
449 }
450
451 // ok, we're now in combat - create the threat list reference and push it to the respective managers
452 ThreatReference* ref = new ThreatReferenceImpl(this, target);
453 PutThreatListRef(target->GetGUID(), ref);
455
456 ref->UpdateOffline();
457 if (ref->IsOnline()) // we only add the threat if the ref is currently available
458 ref->AddThreat(amount);
459
461 UpdateVictim();
462 else
464}
465
466void ThreatManager::ScaleThreat(Unit* target, float factor)
467{
468 auto it = _myThreatListEntries.find(target->GetGUID());
469 if (it != _myThreatListEntries.end())
470 it->second->ScaleThreat(std::max<float>(factor,0.0f));
471}
472
474{
475 if (_sortedThreatList->empty())
476 return;
477
478 auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end();
479 ThreatReference const* highest = *it;
480 if (!highest->IsAvailable())
481 return;
482
483 if (highest->IsTaunting() && ((++it) != end)) // might need to skip this - max threat could be the preceding element (there is only one taunt element)
484 {
485 ThreatReference const* a = *it;
486 if (a->IsAvailable() && a->GetThreat() > highest->GetThreat())
487 highest = a;
488 }
489
490 AddThreat(target, highest->GetThreat() - GetThreat(target, true), nullptr, true, true);
491}
492
494{
496
497 uint32 tauntPriority = 0; // lowest is highest
498 std::unordered_map<ObjectGuid, uint32> tauntStates;
499 // Only the last taunt effect applied by something still on our threat list is considered
500 for (AuraEffect const* tauntEffect : tauntEffects)
501 tauntStates[tauntEffect->GetCasterGUID()] = ++tauntPriority;
502
503 for (auto const& pair : _myThreatListEntries)
504 {
505 auto it = tauntStates.find(pair.first);
506 if (it != tauntStates.end())
507 pair.second->UpdateTauntState(ThreatReference::TauntState(ThreatReference::TAUNT_STATE_TAUNT + tauntStates.size() - it->second));
508 else
509 pair.second->UpdateTauntState();
510 }
511
512 // taunt aura update also re-evaluates all suppressed states (retail behavior)
513 EvaluateSuppressed(true);
514}
515
517{
518 for (auto const& pair : _myThreatListEntries)
519 pair.second->ScaleThreat(0.0f);
520}
521
523{
524 auto it = _myThreatListEntries.find(target->GetGUID());
525 if (it != _myThreatListEntries.end())
526 ClearThreat(it->second);
527}
528
536
538{
539 if (!_myThreatListEntries.empty())
540 {
542 do
543 _myThreatListEntries.begin()->second->UnregisterAndFree();
544 while (!_myThreatListEntries.empty());
545 }
546}
547
549{
550 if (target)
551 {
552 auto it = _myThreatListEntries.find(target->GetGUID());
553 if (it != _myThreatListEntries.end())
554 {
555 _fixateRef = it->second;
556 return;
557 }
558 }
559 _fixateRef = nullptr;
560}
561
563{
564 if (_fixateRef)
565 return _fixateRef->GetVictim();
566 else
567 return nullptr;
568}
569
571{
572 ThreatReference const* const newVictim = ReselectVictim();
573 bool const newHighest = newVictim && (newVictim != _currentVictimRef);
574
575 _currentVictimRef = newVictim;
576 if (newHighest || _needClientUpdate)
577 {
578 SendThreatListToClients(newHighest);
579 _needClientUpdate = false;
580 }
581
583}
584
586{
587 if (_sortedThreatList->empty())
588 return nullptr;
589
590 for (auto const& pair : _myThreatListEntries)
591 pair.second->UpdateOffline(); // AI notifies are processed in ::UpdateVictim caller
592
593 // fixated target is always preferred
595 return _fixateRef;
596
597 ThreatReference const* oldVictimRef = _currentVictimRef;
598 if (oldVictimRef && oldVictimRef->IsOffline())
599 oldVictimRef = nullptr;
600 // in 99% of cases - we won't need to actually look at anything beyond the first element
601 ThreatReference const* highest = _sortedThreatList->top();
602 // if the highest reference is offline, the entire list is offline, and we indicate this
603 if (!highest->IsAvailable())
604 return nullptr;
605 // if we have no old victim, or old victim is still highest, then highest is our target and we're done
606 if (!oldVictimRef || highest == oldVictimRef)
607 return highest;
608 // 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
609 if (!ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.1f))
610 return oldVictimRef;
611 // if highest threat breaks 130%, it's our new target regardless of range (and we're done)
612 if (ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.3f))
613 return highest;
614 // 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
615 if (_owner->IsWithinMeleeRange(highest->_victim))
616 return highest;
617 // 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
618 // 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)
619 auto it = _sortedThreatList->ordered_begin(), end = _sortedThreatList->ordered_end();
620 while (it != end)
621 {
622 ThreatReference const* next = *it;
623 // if we've found current victim, we're done (nothing above is higher, and nothing below can be higher)
624 if (next == oldVictimRef)
625 return next;
626 // if next isn't above 110% threat, then nothing below it can be either - we're done, old victim stays
627 if (!ThreatManager::CompareReferencesLT(oldVictimRef, next, 1.1f))
628 return oldVictimRef;
629 // if next is melee, he's above 110% and our new victim
631 return next;
632 // otherwise the next highest target may still be a melee above 110% and we need to look further
633 ++it;
634 }
635 // we should have found the old victim at some point in the loop above, so execution should never get to this point
636 ABORT_MSG("Current victim not found in sorted threat list even though it has a reference - manager desync!");
637 return nullptr;
638}
639
641{
643 std::vector<ObjectGuid> v(std::move(_needsAIUpdate)); // _needsAIUpdate is now empty in case this triggers a recursive call
644 if (!ai)
645 return;
646 for (ObjectGuid const& guid : v)
648 ai->JustStartedThreateningMe(ref->GetVictim());
649}
650
652{
653 _needsAIUpdate.push_back(guid);
654}
655
656// returns true if a is LOWER on the threat list than b
657/*static*/ bool ThreatManager::CompareReferencesLT(ThreatReference const* a, ThreatReference const* b, float aWeight)
658{
659 if (a->_online != b->_online) // online state precedence (ONLINE > SUPPRESSED > OFFLINE)
660 return a->_online < b->_online;
661 if (a->_taunted != b->_taunted) // taunt state precedence (TAUNT > NONE > DETAUNT)
662 return a->_taunted < b->_taunted;
663 return (a->GetThreat()*aWeight < b->GetThreat());
664}
665
666/*static*/ float ThreatManager::CalculateModifiedThreat(float threat, Unit const* victim, SpellInfo const* spell)
667{
668 // modifiers by spell
669 if (spell)
670 {
671 if (SpellThreatEntry const* threatEntry = sSpellMgr->GetSpellThreatEntry(spell->Id))
672 if (threatEntry->pctMod != 1.0f) // flat/AP modifiers handled in Spell::HandleThreatSpells
673 threat *= threatEntry->pctMod;
674
675 if (Player* modOwner = victim->GetSpellModOwner())
676 modOwner->ApplySpellMod(spell, SpellModOp::Hate, threat);
677 }
678
679 // modifiers by effect school
680 ThreatManager const& victimMgr = victim->GetThreatManager();
681 SpellSchoolMask const mask = spell ? spell->GetSchoolMask() : SPELL_SCHOOL_MASK_NORMAL;
682 switch (mask)
683 {
685 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NORMAL];
686 break;
688 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_HOLY];
689 break;
691 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FIRE];
692 break;
694 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NATURE];
695 break;
697 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FROST];
698 break;
700 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_SHADOW];
701 break;
703 threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_ARCANE];
704 break;
705 default:
706 {
707 auto it = victimMgr._multiSchoolModifiers.find(mask);
708 if (it != victimMgr._multiSchoolModifiers.end())
709 {
710 threat *= it->second;
711 break;
712 }
714 victimMgr._multiSchoolModifiers[mask] = mod;
715 threat *= mod;
716 break;
717 }
718 }
719 return threat;
720}
721
722void ThreatManager::ForwardThreatForAssistingMe(Unit* assistant, float baseAmount, SpellInfo const* spell, bool ignoreModifiers)
723{
724 if (spell && (spell->HasAttribute(SPELL_ATTR1_NO_THREAT) || spell->HasAttribute(SPELL_ATTR4_NO_HELPFUL_THREAT))) // shortcut, none of the calls would do anything
725 return;
726 if (_threatenedByMe.empty())
727 return;
728
729 std::vector<Creature*> canBeThreatened, cannotBeThreatened;
730 for (auto const& pair : _threatenedByMe)
731 {
732 Creature* owner = pair.second->GetOwner();
734 canBeThreatened.push_back(owner);
735 else
736 cannotBeThreatened.push_back(owner);
737 }
738
739 if (!canBeThreatened.empty()) // targets under CC cannot gain assist threat - split evenly among the rest
740 {
741 float const perTarget = baseAmount / canBeThreatened.size();
742 for (Creature* threatened : canBeThreatened)
743 threatened->GetThreatManager().AddThreat(assistant, perTarget, spell, ignoreModifiers);
744 }
745
746 for (Creature* threatened : cannotBeThreatened)
747 threatened->GetThreatManager().AddThreat(assistant, 0.0f, spell, true);
748}
749
750void ThreatManager::RemoveMeFromThreatLists(bool (*unitFilter)(Unit const* otherUnit))
751{
752 std::vector<ThreatReference*> threatReferencesToRemove;
753 threatReferencesToRemove.reserve(_threatenedByMe.size());
754 for (auto const& [guid, ref] : _threatenedByMe)
755 if (!unitFilter || unitFilter(ref->GetOwner()))
756 threatReferencesToRemove.push_back(ref);
757
758 for (ThreatReference* ref : threatReferencesToRemove)
759 ref->_mgr.ClearThreat(_owner);
760}
761
763{
764 SpellEffectValue mod = 0;
766 mod += eff->GetAmount();
767
768 if (_threatenedByMe.empty())
769 return;
770
771 auto it = _threatenedByMe.begin();
772 bool const isIncrease = (it->second->_tempModifier < mod);
773 do
774 {
775 it->second->_tempModifier = mod;
776 if (isIncrease)
777 it->second->HeapNotifyIncreased();
778 else
779 it->second->HeapNotifyDecreased();
780 } while ((++it) != _threatenedByMe.end());
781}
782
789
790void ThreatManager::RegisterRedirectThreat(uint32 spellId, ObjectGuid const& victim, float pct)
791{
792 _redirectRegistry[spellId][victim] = pct;
794}
795
797{
798 auto it = _redirectRegistry.find(spellId);
799 if (it == _redirectRegistry.end())
800 return;
801 _redirectRegistry.erase(it);
803}
804
806{
807 auto it = _redirectRegistry.find(spellId);
808 if (it == _redirectRegistry.end())
809 return;
810 auto& victimMap = it->second;
811 auto it2 = victimMap.find(victim);
812 if (it2 == victimMap.end())
813 return;
814 victimMap.erase(it2);
816}
817
819{
820 if (Creature const* owner = _owner->ToCreature(); owner && owner->IsThreatFeedbackDisabled())
821 return;
822
824 threatClear.UnitGUID = _owner->GetGUID();
825 _owner->SendMessageToSet(threatClear.Write(), false);
826}
827
829{
830 if (Creature const* owner = _owner->ToCreature(); owner && owner->IsThreatFeedbackDisabled())
831 return;
832
834 threatRemove.UnitGUID = _owner->GetGUID();
835 threatRemove.AboutGUID = victim->GetGUID();
836 _owner->SendMessageToSet(threatRemove.Write(), false);
837}
838
839void ThreatManager::SendThreatListToClients(bool newHighest) const
840{
841 if (Creature const* owner = _owner->ToCreature(); owner && owner->IsThreatFeedbackDisabled())
842 return;
843
844 auto fillSharedPacketDataAndSend = [&](auto& packet)
845 {
846 packet.UnitGUID = _owner->GetGUID();
847 packet.ThreatList.reserve(_sortedThreatList->size());
848 for (ThreatReference const* ref : *_sortedThreatList)
849 {
850 if (!ref->IsAvailable())
851 continue;
852
854 threatInfo.UnitGUID = ref->GetVictim()->GetGUID();
855 threatInfo.Threat = int64(ref->GetThreat() * 100);
856 packet.ThreatList.push_back(threatInfo);
857 }
858 _owner->SendMessageToSet(packet.Write(), false);
859 };
860
861 if (newHighest)
862 {
864 highestThreatUpdate.HighestThreatGUID = _currentVictimRef->GetVictim()->GetGUID();
865 fillSharedPacketDataAndSend(highestThreatUpdate);
866 }
867 else
868 {
870 fillSharedPacketDataAndSend(threatUpdate);
871 }
872}
873
875{
876 _needClientUpdate = true;
877 auto& inMap = _myThreatListEntries[guid];
878 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());
879 inMap = ref;
880 static_cast<ThreatReferenceImpl*>(ref)->_handle = _sortedThreatList->push(ref);
881}
882
884{
885 auto it = _myThreatListEntries.find(guid);
886 if (it == _myThreatListEntries.end())
887 return;
888 ThreatReference* ref = it->second;
889 _myThreatListEntries.erase(it);
890 _sortedThreatList->erase(static_cast<ThreatReferenceImpl*>(ref)->_handle);
891
892 if (_fixateRef == ref)
893 _fixateRef = nullptr;
894 if (_currentVictimRef == ref)
895 _currentVictimRef = nullptr;
896}
897
899{
900 auto& inMap = _threatenedByMe[guid];
901 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());
902 inMap = ref;
903}
904
906{
907 auto it = _threatenedByMe.find(guid);
908 if (it != _threatenedByMe.end())
909 _threatenedByMe.erase(it);
910}
911
913{
914 _redirectInfo.clear();
915 float totalPct = 0;
916 for (auto const& pair : _redirectRegistry) // (spellid, victim -> pct)
917 for (auto const& victimPair : pair.second) // (victim,pct)
918 {
919 float thisPct = std::min(100.0f - totalPct, victimPair.second);
920 if (thisPct > 0)
921 {
922 _redirectInfo.push_back({ victimPair.first, thisPct });
923 totalPct += thisPct;
924 ASSERT(totalPct <= 100);
925 if (totalPct == 100)
926 return;
927 }
928 }
929}
uint8_t uint8
Definition Define.h:156
int64_t int64
Definition Define.h:149
int8_t int8
Definition Define.h:152
uint32_t uint32
Definition Define.h:154
#define ABORT_MSG
Definition Errors.h:88
#define ASSERT_NOTNULL(pointer)
Definition Errors.h:82
#define ASSERT
Definition Errors.h:80
@ TYPEID_UNIT
Definition ObjectGuid.h:43
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_ATTR2_NO_INITIAL_THREAT
@ SPELL_ATTR1_NO_THREAT
@ SPELL_SCHOOL_SHADOW
@ SPELL_SCHOOL_NORMAL
@ SPELL_SCHOOL_NATURE
@ SPELL_SCHOOL_FROST
@ SPELL_SCHOOL_ARCANE
@ SPELL_SCHOOL_FIRE
@ SPELL_SCHOOL_HOLY
@ MAX_SPELL_SCHOOL
@ SPELL_ATTR4_NO_HELPFUL_THREAT
@ 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
double SpellEffectValue
This is a double instead of float to be able to store full range of int32.
#define sSpellMgr
Definition SpellMgr.h:812
@ UNIT_FLAG_IMMUNE_TO_NPC
@ UNIT_FLAG_IMMUNE_TO_PC
@ UNIT_FLAG_PLAYER_CONTROLLED
@ UNIT_MASK_ACCESSORY
Definition Unit.h:365
@ UNIT_MASK_GUARDIAN
Definition Unit.h:358
@ UNIT_MASK_MINION
Definition Unit.h:357
@ UNIT_STATE_CONTROLLED
Definition Unit.h:301
T CalculatePct(T base, U pct)
Definition Util.h:72
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
TypeID GetTypeId() const
Definition BaseEntity.h:166
bool SetInCombatWith(Unit *who, bool addSecondUnitSuppressed=false)
bool IsInCombatWith(ObjectGuid const &who) const
virtual void JustStartedThreateningMe(Unit *who)
Definition CreatureAI.h:98
bool IsTrigger() const
Definition Creature.h:127
bool _IsTargetAcceptable(Unit const *target) const
SpellSchoolMask GetMeleeDamageSchoolMask(WeaponAttackType=BASE_ATTACK) const override
Definition Creature.h:230
bool CanCreatureAttack(Unit const *victim, bool force=true) const
std::string ToString() const
Creature * ToCreature()
Definition Object.h:121
uint32 const Id
Definition SpellInfo.h:328
SpellSchoolMask GetSchoolMask() const
bool HasAttribute(SpellAttr0 attribute) const
Definition SpellInfo.h:456
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 RegisterRedirectThreat(uint32 spellId, ObjectGuid const &victim, float pct)
== REDIRECT SYSTEM ==
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 ==
bool IsThreatenedBy(ObjectGuid const &who, bool includeOffline=false) const
void PutThreatListRef(ObjectGuid const &guid, ThreatReference *ref)
== MY THREAT LIST ==
bool IsThreateningTo(ObjectGuid const &who, bool includeOffline=false) const
void RemoveMeFromThreatLists(bool(*unitFilter)(Unit const *otherUnit))
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
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)
std::unordered_map< uint32, std::unordered_map< ObjectGuid, float > > _redirectRegistry
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
Trinity::IteratorPair< ThreatListIterator, std::nullptr_t > GetSortedThreatList() const
std::vector< ThreatReference * > GetModifiableThreatList()
std::unordered_map< std::underlying_type< SpellSchoolMask >::type, float > _multiSchoolModifiers
Unit * GetFixateTarget() const
friend class ThreatReferenceImpl
void UpdateMyTempModifiers()
void PurgeThreatenedByMeRef(ObjectGuid const &guid)
float GetThreat(Unit const *who, bool includeOffline=false) const
void MatchUnitThreatToHighestThreat(Unit *target)
std::vector< std::pair< ObjectGuid, float > > _redirectInfo
bool _needThreatClearUpdate
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:635
bool IsWithinMeleeRange(Unit const *obj) const
Definition Unit.h:710
AuraEffectList const & GetAuraEffectsByType(AuraType type) const
Definition Unit.h:1342
ThreatManager & GetThreatManager()
Definition Unit.h:1078
std::forward_list< AuraEffect * > AuraEffectList
Definition Unit.h:652
bool IsPet() const
Definition Unit.h:751
bool HasUnitFlag(UnitFlags flags) const
Definition Unit.h:845
TempSummon * ToTempSummon()
Definition Unit.h:1828
bool HasAuraType(AuraType auraType) const
Definition Unit.cpp:4814
Unit * GetVictim() const
Definition Unit.h:726
bool IsImmunedToDamage(SpellSchoolMask meleeSchoolMask) const
Definition Unit.cpp:7855
float GetTotalAuraMultiplierByMiscMask(AuraType auraType, uint32 misc_mask) const
Definition Unit.cpp:5099
bool HasUnitState(const uint32 f) const
Definition Unit.h:743
uint32 HasUnitTypeMask(uint32 mask) const
Definition Unit.h:747
bool HasBreakableByDamageAuraType(AuraType type, uint32 excludeAura=0) const
Definition Unit.cpp:769
CombatManager & GetCombatManager()
Definition Unit.h:1038
bool IsTotem() const
Definition Unit.h:753
virtual bool IsEngaged() const
Definition Unit.h:1034
virtual void SendMessageToSet(WorldPacket const *data, bool self) const
Definition Object.cpp:1094
Unit * GetOwner() const
Definition Object.cpp:1598
bool CanSeeOrDetect(WorldObject const *obj, CanSeeOrDetectExtraArgs const &args={ }) const
Definition Object.cpp:857
Player * GetSpellModOwner() const
Definition Object.cpp:1641
WorldPacket const * Write() override
WorldPacket const * Write() override
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:37
STL namespace.