-
Notifications
You must be signed in to change notification settings - Fork 6k
/
ThreatManager.cpp
744 lines (667 loc) · 26.9 KB
/
ThreatManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* Copyright (C) 2008-2018 TrinityCore <https://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Creature.h"
#include "CreatureAI.h"
#include "MotionMaster.h"
#include "Player.h"
#include "ThreatManager.h"
#include "Unit.h"
#include "UnitAI.h"
#include "UnitDefines.h"
#include "SpellAuraEffects.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
#include "ObjectAccessor.h"
#include "WorldPacket.h"
#include <algorithm>
const CompareThreatLessThan ThreatManager::CompareThreat;
void ThreatReference::AddThreat(float amount)
{
if (amount == 0.0f)
return;
_baseAmount = std::max<float>(_baseAmount + amount, 0.0f);
if (amount > 0.0f)
HeapNotifyIncreased();
else
HeapNotifyDecreased();
}
void ThreatReference::ScaleThreat(float factor)
{
if (factor == 1.0f)
return;
_baseAmount *= factor;
if (factor > 1.0f)
HeapNotifyIncreased();
else
HeapNotifyDecreased();
}
void ThreatReference::UpdateOnlineState()
{
OnlineState onlineState = SelectOnlineState();
if (onlineState == _online)
return;
bool increase = (onlineState > _online);
_online = onlineState;
if (increase)
HeapNotifyIncreased();
else
HeapNotifyDecreased();
if (!IsAvailable())
_owner->GetThreatManager().SendRemoveToClients(_victim);
}
/*static*/ bool ThreatReference::FlagsAllowFighting(Unit const* a, Unit const* b)
{
if (a->GetTypeId() == TYPEID_UNIT && a->ToCreature()->IsTrigger())
return false;
if (a->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE))
{
if (b->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_PC))
return false;
}
else
{
if (b->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC))
return false;
}
return true;
}
ThreatReference::OnlineState ThreatReference::SelectOnlineState()
{
// first, check all offline conditions
if (!_owner->CanSeeOrDetect(_victim)) // not in map/phase, or stealth/invis
return ONLINE_STATE_OFFLINE;
if (_victim->HasUnitState(UNIT_STATE_DIED)) // feign death
return ONLINE_STATE_OFFLINE;
if (!FlagsAllowFighting(_owner, _victim) || !FlagsAllowFighting(_victim, _owner))
return ONLINE_STATE_OFFLINE;
if (_owner->IsAIEnabled && !_owner->GetAI()->CanAIAttack(_victim))
return ONLINE_STATE_OFFLINE;
// next, check suppression (immunity to chosen melee attack school)
if (_victim->IsImmunedToDamage(_owner->GetMeleeDamageSchoolMask()))
return ONLINE_STATE_SUPPRESSED;
// or any form of CC that will break on damage - disorient, polymorph, blind etc
if (_victim->HasBreakableByDamageCrowdControlAura())
return ONLINE_STATE_SUPPRESSED;
// no suppression - we're online
return ONLINE_STATE_ONLINE;
}
void ThreatReference::UpdateTauntState(bool victimIsTaunting)
{
if (victimIsTaunting)
{
_taunted = TAUNT_STATE_TAUNT;
HeapNotifyIncreased();
return;
}
// Check for SPELL_AURA_MOD_DETAUNT (applied from owner to victim)
for (AuraEffect const* eff : _victim->GetAuraEffectsByType(SPELL_AURA_MOD_DETAUNT))
if (eff->GetCasterGUID() == _owner->GetGUID())
{
_taunted = TAUNT_STATE_DETAUNT;
HeapNotifyDecreased();
return;
}
_taunted = TAUNT_STATE_NONE;
HeapNotifyChanged();
}
void ThreatReference::ClearThreat(bool sendRemove)
{
_owner->GetThreatManager().PurgeThreatListRef(_victim->GetGUID(), sendRemove);
_victim->GetThreatManager().PurgeThreatenedByMeRef(_owner->GetGUID());
delete this;
}
/*static*/ bool ThreatManager::CanHaveThreatList(Unit const* who)
{
Creature const* cWho = who->ToCreature();
// only creatures can have threat list
if (!cWho)
return false;
// pets, totems and triggers cannot have threat list
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;
return true;
}
ThreatManager::ThreatManager(Unit* owner) : _owner(owner), _ownerCanHaveThreatList(false), _ownerEngaged(false), _updateClientTimer(CLIENT_THREAT_UPDATE_INTERVAL), _currentVictimRef(nullptr)
{
for (int8 i = 0; i < MAX_SPELL_SCHOOL; ++i)
_singleSchoolModifiers[i] = 1.0f;
}
ThreatManager::~ThreatManager()
{
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());
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());
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());
}
void ThreatManager::Initialize()
{
_ownerCanHaveThreatList = ThreatManager::CanHaveThreatList(_owner);
}
void ThreatManager::Update(uint32 tdiff)
{
if (!CanHaveThreatList() || !IsEngaged())
return;
if (_updateClientTimer <= tdiff)
{
_updateClientTimer = CLIENT_THREAT_UPDATE_INTERVAL;
SendThreatListToClients();
}
else
_updateClientTimer -= tdiff;
}
Unit* ThreatManager::GetCurrentVictim() const
{
if (_currentVictimRef)
return _currentVictimRef->GetVictim();
return nullptr;
}
Unit* ThreatManager::GetAnyTarget() const
{
for (ThreatReference const* ref : _sortedThreatList)
if (!ref->IsOffline())
return ref->GetVictim();
return nullptr;
}
Unit* ThreatManager::SelectVictim()
{
if (_sortedThreatList.empty())
return nullptr;
ThreatReference const* newVictimRef = ReselectVictim();
if (newVictimRef != _currentVictimRef)
{
if (newVictimRef)
SendNewVictimToClients(newVictimRef);
_currentVictimRef = newVictimRef;
}
return newVictimRef ? newVictimRef->GetVictim() : nullptr;
}
bool ThreatManager::IsThreatListEmpty(bool includeOffline) const
{
if (includeOffline)
return _sortedThreatList.empty();
for (ThreatReference const* ref : _sortedThreatList)
if (ref->IsAvailable())
return false;
return true;
}
bool ThreatManager::IsThreatenedBy(ObjectGuid const& who, bool includeOffline) const
{
auto it = _myThreatListEntries.find(who);
if (it == _myThreatListEntries.end())
return false;
return (includeOffline || it->second->IsAvailable());
}
bool ThreatManager::IsThreatenedBy(Unit const* who, bool includeOffline) const { return IsThreatenedBy(who->GetGUID(), includeOffline); }
float ThreatManager::GetThreat(Unit const* who, bool includeOffline) const
{
auto it = _myThreatListEntries.find(who->GetGUID());
if (it == _myThreatListEntries.end())
return 0.0f;
return (includeOffline || it->second->IsAvailable()) ? it->second->GetThreat() : 0.0f;
}
std::vector<ThreatReference*> ThreatManager::GetModifiableThreatList() const
{
std::vector<ThreatReference*> list;
list.reserve(_myThreatListEntries.size());
for (auto it = _sortedThreatList.ordered_begin(), end = _sortedThreatList.ordered_end(); it != end; ++it)
list.push_back(const_cast<ThreatReference*>(*it));
return list;
}
bool ThreatManager::IsThreateningAnyone(bool includeOffline) const
{
if (includeOffline)
return !_threatenedByMe.empty();
for (auto const& pair : _threatenedByMe)
if (pair.second->IsAvailable())
return true;
return false;
}
bool ThreatManager::IsThreateningTo(ObjectGuid const& who, bool includeOffline) const
{
auto it = _threatenedByMe.find(who);
if (it == _threatenedByMe.end())
return false;
return (includeOffline || it->second->IsAvailable());
}
bool ThreatManager::IsThreateningTo(Unit const* who, bool includeOffline) const { return IsThreateningTo(who->GetGUID(), includeOffline); }
void ThreatManager::UpdateOnlineStates(bool meThreateningOthers, bool othersThreateningMe)
{
if (othersThreateningMe)
for (auto const& pair : _myThreatListEntries)
pair.second->UpdateOnlineState();
if (meThreateningOthers)
for (auto const& pair : _threatenedByMe)
pair.second->UpdateOnlineState();
}
static void SaveCreatureHomePositionIfNeed(Creature* c)
{
MovementGeneratorType const movetype = c->GetMotionMaster()->GetCurrentMovementGeneratorType();
if (movetype == WAYPOINT_MOTION_TYPE || movetype == POINT_MOTION_TYPE || (c->IsAIEnabled && c->AI()->IsEscorted()))
c->SetHomePosition(c->GetPosition());
}
void ThreatManager::AddThreat(Unit* target, float amount, SpellInfo const* spell, bool ignoreModifiers, bool ignoreRedirects)
{
// step 1: we can shortcut if the spell has one of the NO_THREAT attrs set - nothing will happen
if (spell)
{
if (spell->HasAttribute(SPELL_ATTR1_NO_THREAT))
return;
if (!_owner->IsEngaged() && spell->HasAttribute(SPELL_ATTR3_NO_INITIAL_AGGRO))
return;
}
// while riding a vehicle, all threat goes to the vehicle, not the pilot
if (Unit* vehicle = target->GetVehicleBase())
{
AddThreat(vehicle, amount, spell, ignoreModifiers, ignoreRedirects);
if (target->HasUnitTypeMask(UNIT_MASK_ACCESSORY)) // accessories are fully treated as components of the parent and cannot have threat
return;
amount = 0.0f;
}
// if we cannot actually have a threat list, we instead just set combat state and avoid creating threat refs altogether
if (!CanHaveThreatList())
{
CombatManager& combatMgr = _owner->GetCombatManager();
if (!combatMgr.SetInCombatWith(target))
return;
// traverse redirects and put them in combat, too
for (auto const& pair : target->GetThreatManager()._redirectInfo)
if (!combatMgr.IsInCombatWith(pair.first))
if (Unit* redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first))
combatMgr.SetInCombatWith(redirTarget);
return;
}
// apply threat modifiers to the amount
if (!ignoreModifiers)
amount = CalculateModifiedThreat(amount, target, spell);
// if we're increasing threat, send some/all of it to redirection targets instead if applicable
if (!ignoreRedirects && amount > 0.0f)
{
auto const& redirInfo = target->GetThreatManager()._redirectInfo;
if (!redirInfo.empty())
{
float const origAmount = amount;
// intentional iteration by index - there's a nested AddThreat call further down that might cause AI calls which might modify redirect info through spells
for (size_t i = 0; i < redirInfo.size(); ++i)
{
auto const pair = redirInfo[i]; // (victim,pct)
Unit* redirTarget = nullptr;
auto it = _myThreatListEntries.find(pair.first); // try to look it up in our threat list first (faster)
if (it != _myThreatListEntries.end())
redirTarget = it->second->_victim;
else
redirTarget = ObjectAccessor::GetUnit(*_owner, pair.first);
if (redirTarget)
{
float amountRedirected = CalculatePct(origAmount, pair.second);
AddThreat(redirTarget, amountRedirected, spell, true, true);
amount -= amountRedirected;
}
}
}
}
// ensure we're in combat (threat implies combat!)
if (!_owner->GetCombatManager().SetInCombatWith(target)) // if this returns false, we're not actually in combat, and thus cannot have threat!
return; // typical causes: bad scripts trying to add threat to GMs, dead targets etc
// ok, now we actually apply threat
// check if we already have an entry - if we do, just increase threat for that entry and we're done
auto it = _myThreatListEntries.find(target->GetGUID());
if (it != _myThreatListEntries.end())
{
it->second->AddThreat(amount);
return;
}
// ok, we're now in combat - create the threat list reference and push it to the respective managers
ThreatReference* ref = new ThreatReference(this, target, amount);
PutThreatListRef(target->GetGUID(), ref);
target->GetThreatManager().PutThreatenedByMeRef(_owner->GetGUID(), ref);
if (!_ownerEngaged)
{
_ownerEngaged = true;
Creature* cOwner = _owner->ToCreature();
ASSERT(cOwner); // if we got here the owner can have a threat list, and must be a creature!
SaveCreatureHomePositionIfNeed(cOwner);
if (cOwner->IsAIEnabled)
cOwner->AI()->JustEngagedWith(target);
}
}
void ThreatManager::ScaleThreat(Unit* target, float factor)
{
auto it = _myThreatListEntries.find(target->GetGUID());
if (it != _myThreatListEntries.end())
it->second->ScaleThreat(std::max<float>(factor,0.0f));
}
void ThreatManager::MatchUnitThreatToHighestThreat(Unit* target)
{
if (_sortedThreatList.empty())
return;
auto it = _sortedThreatList.ordered_begin(), end = _sortedThreatList.ordered_end();
ThreatReference const* highest = *it;
if (!highest->IsAvailable())
return;
if (highest->IsTaunting() && ((++it) != end)) // might need to skip this - max threat could be the preceding element (there is only one taunt element)
{
ThreatReference const* a = *it;
if (a->IsAvailable() && a->GetThreat() > highest->GetThreat())
highest = a;
}
AddThreat(target, highest->GetThreat() - GetThreat(target, true), nullptr, true, true);
}
void ThreatManager::TauntUpdate()
{
std::list<AuraEffect*> const& tauntEffects = _owner->GetAuraEffectsByType(SPELL_AURA_MOD_TAUNT);
auto threatEnd = _myThreatListEntries.end();
ThreatReference* tauntRef = nullptr;
// Only the last taunt effect applied by something still on our threat list is considered
for (auto it = tauntEffects.rbegin(), end = tauntEffects.rend(); it != end; ++it)
{
auto threatIt = _myThreatListEntries.find((*it)->GetCasterGUID());
if (threatIt == threatEnd)
continue;
if (!threatIt->second->IsAvailable())
continue;
tauntRef = threatIt->second;
break;
}
for (auto const& pair : _myThreatListEntries)
pair.second->UpdateTauntState(pair.second == tauntRef);
}
void ThreatManager::ResetAllThreat()
{
for (auto const& pair : _myThreatListEntries)
pair.second->SetThreat(0.0f);
}
void ThreatManager::ClearThreat(Unit* target)
{
auto it = _myThreatListEntries.find(target->GetGUID());
if (it != _myThreatListEntries.end())
it->second->ClearThreat();
}
void ThreatManager::ClearAllThreat()
{
_ownerEngaged = false;
if (_myThreatListEntries.empty())
return;
SendClearAllThreatToClients();
do
_myThreatListEntries.begin()->second->ClearThreat(false);
while (!_myThreatListEntries.empty());
}
ThreatReference const* ThreatManager::ReselectVictim()
{
ThreatReference const* oldVictimRef = _currentVictimRef;
if (oldVictimRef && !oldVictimRef->IsAvailable())
oldVictimRef = nullptr;
// in 99% of cases - we won't need to actually look at anything beyond the first element
ThreatReference const* highest = _sortedThreatList.top();
// if the highest reference is offline, the entire list is offline, and we indicate this
if (!highest->IsAvailable())
return nullptr;
// if we have no old victim, or old victim is still highest, then highest is our target and we're done
if (!oldVictimRef || highest == oldVictimRef)
return highest;
// 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
if (!ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.1f))
return oldVictimRef;
// if highest threat breaks 130%, it's our new target regardless of range (and we're done)
if (ThreatManager::CompareReferencesLT(oldVictimRef, highest, 1.3f))
return highest;
// 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
if (_owner->IsWithinMeleeRange(highest->_victim))
return highest;
// 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
// 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)
auto it = _sortedThreatList.ordered_begin(), end = _sortedThreatList.ordered_end();
while (it != end)
{
ThreatReference const* next = *it;
// if we've found current victim, we're done (nothing above is higher, and nothing below can be higher)
if (next == oldVictimRef)
return next;
// if next isn't above 110% threat, then nothing below it can be either - we're done, old victim stays
if (!ThreatManager::CompareReferencesLT(oldVictimRef, next, 1.1f))
return oldVictimRef;
// if next is melee, he's above 110% and our new victim
if (_owner->IsWithinMeleeRange(next->_victim))
return next;
// otherwise the next highest target may still be a melee above 110% and we need to look further
++it;
}
// we should have found the old victim at some point in the loop above, so execution should never get to this point
ASSERT(false && "Current victim not found in sorted threat list even though it has a reference - manager desync!");
return nullptr;
}
// returns true if a is LOWER on the threat list than b
/*static*/ bool ThreatManager::CompareReferencesLT(ThreatReference const* a, ThreatReference const* b, float aWeight)
{
if (a->_online != b->_online) // online state precedence (ONLINE > SUPPRESSED > OFFLINE)
return a->_online < b->_online;
if (a->_taunted != b->_taunted) // taunt state precedence (TAUNT > NONE > DETAUNT)
return a->_taunted < b->_taunted;
return (a->GetThreat()*aWeight < b->GetThreat());
}
/*static*/ float ThreatManager::CalculateModifiedThreat(float threat, Unit const* victim, SpellInfo const* spell)
{
// modifiers by spell
if (spell)
{
if (SpellThreatEntry const* threatEntry = sSpellMgr->GetSpellThreatEntry(spell->Id))
if (threatEntry->pctMod != 1.0f) // flat/AP modifiers handled in Spell::HandleThreatSpells
threat *= threatEntry->pctMod;
if (Player* modOwner = victim->GetSpellModOwner())
modOwner->ApplySpellMod(spell->Id, SPELLMOD_THREAT, threat);
}
// modifiers by effect school
ThreatManager const& victimMgr = victim->GetThreatManager();
SpellSchoolMask const mask = spell ? spell->GetSchoolMask() : SPELL_SCHOOL_MASK_NORMAL;
switch (mask)
{
case SPELL_SCHOOL_MASK_NORMAL:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NORMAL];
break;
case SPELL_SCHOOL_MASK_HOLY:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_HOLY];
break;
case SPELL_SCHOOL_MASK_FIRE:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FIRE];
break;
case SPELL_SCHOOL_MASK_NATURE:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_NATURE];
break;
case SPELL_SCHOOL_MASK_FROST:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_FROST];
break;
case SPELL_SCHOOL_MASK_SHADOW:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_SHADOW];
break;
case SPELL_SCHOOL_MASK_ARCANE:
threat *= victimMgr._singleSchoolModifiers[SPELL_SCHOOL_ARCANE];
break;
default:
{
auto it = victimMgr._multiSchoolModifiers.find(mask);
if (it != victimMgr._multiSchoolModifiers.end())
{
threat *= it->second;
break;
}
float mod = victim->GetTotalAuraMultiplierByMiscMask(SPELL_AURA_MOD_THREAT, mask);
victimMgr._multiSchoolModifiers[mask] = mod;
threat *= mod;
break;
}
}
return threat;
}
void ThreatManager::SendClearAllThreatToClients() const
{
WorldPacket data(SMSG_THREAT_CLEAR, 8);
data << _owner->GetPackGUID();
_owner->SendMessageToSet(&data, false);
}
void ThreatManager::SendThreatListToClients() const
{
WorldPacket data(SMSG_THREAT_UPDATE, (_sortedThreatList.size() + 1) * 8); // guess
data << _owner->GetPackGUID();
size_t countPos = data.wpos();
data << uint32(0); // placeholder
uint32 count = 0;
for (ThreatReference const* ref : _sortedThreatList)
{
if (!ref->IsAvailable()) // @todo check if suppressed threat should get sent for bubble/iceblock/hop etc
continue;
data << ref->GetVictim()->GetPackGUID();
data << uint32(ref->GetThreat() * 100);
++count;
}
data.put<uint32>(countPos, count);
_owner->SendMessageToSet(&data, false);
}
void ThreatManager::ForwardThreatForAssistingMe(Unit* assistant, float baseAmount, SpellInfo const* spell, bool ignoreModifiers)
{
if (spell && spell->HasAttribute(SPELL_ATTR1_NO_THREAT)) // shortcut, none of the calls would do anything
return;
if (_threatenedByMe.empty())
return;
float const perTarget = baseAmount / _threatenedByMe.size(); // Threat is divided evenly among all targets (LibThreat sourced)
for (auto const& pair : _threatenedByMe)
pair.second->GetOwner()->GetThreatManager().AddThreat(assistant, perTarget, spell, ignoreModifiers);
}
void ThreatManager::RemoveMeFromThreatLists()
{
while (!_threatenedByMe.empty())
_threatenedByMe.begin()->second->ClearThreat();
}
void ThreatManager::UpdateMyTempModifiers()
{
int32 mod = 0;
for (AuraEffect const* eff : _owner->GetAuraEffectsByType(SPELL_AURA_MOD_TOTAL_THREAT))
mod += eff->GetAmount();
for (auto const& pair : _threatenedByMe)
{
pair.second->_tempModifier = mod;
pair.second->HeapNotifyChanged();
}
}
void ThreatManager::UpdateMySpellSchoolModifiers()
{
for (uint8 i = 0; i < MAX_SPELL_SCHOOL; ++i)
_singleSchoolModifiers[i] = _owner->GetTotalAuraMultiplierByMiscMask(SPELL_AURA_MOD_THREAT, 1 << i);
_multiSchoolModifiers.clear();
}
void ThreatManager::RegisterRedirectThreat(uint32 spellId, ObjectGuid const& victim, uint32 pct)
{
_redirectRegistry[spellId][victim] = pct;
UpdateRedirectInfo();
}
void ThreatManager::UnregisterRedirectThreat(uint32 spellId)
{
auto it = _redirectRegistry.find(spellId);
if (it == _redirectRegistry.end())
return;
_redirectRegistry.erase(it);
UpdateRedirectInfo();
}
void ThreatManager::UnregisterRedirectThreat(uint32 spellId, ObjectGuid const& victim)
{
auto it = _redirectRegistry.find(spellId);
if (it == _redirectRegistry.end())
return;
auto& victimMap = it->second;
auto it2 = victimMap.find(victim);
if (it2 == victimMap.end())
return;
victimMap.erase(it2);
UpdateRedirectInfo();
}
void ThreatManager::SendRemoveToClients(Unit const* victim) const
{
WorldPacket data(SMSG_THREAT_REMOVE, 16);
data << _owner->GetPackGUID();
data << victim->GetPackGUID();
_owner->SendMessageToSet(&data, false);
}
void ThreatManager::SendNewVictimToClients(ThreatReference const* victimRef) const
{
WorldPacket data(SMSG_HIGHEST_THREAT_UPDATE, (_sortedThreatList.size() + 2) * 8);
data << _owner->GetPackGUID();
data << victimRef->_victim->GetPackGUID();
size_t countPos = data.wpos();
data << uint32(0); // placeholder
uint32 count = 0;
for (ThreatReference const* ref : _sortedThreatList)
{
if (!ref->IsAvailable())
continue;
data << ref->GetVictim()->GetPackGUID();
data << uint32(ref->GetThreat() * 100);
++count;
}
data.put<uint32>(countPos, count);
_owner->SendMessageToSet(&data, false);
}
void ThreatManager::PutThreatListRef(ObjectGuid const& guid, ThreatReference* ref)
{
auto& inMap = _myThreatListEntries[guid];
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());
inMap = ref;
ref->_handle = _sortedThreatList.push(ref);
}
void ThreatManager::PurgeThreatListRef(ObjectGuid const& guid, bool sendRemove)
{
auto it = _myThreatListEntries.find(guid);
if (it == _myThreatListEntries.end())
return;
ThreatReference* ref = it->second;
_myThreatListEntries.erase(it);
if (_currentVictimRef == ref)
_currentVictimRef = nullptr;
_sortedThreatList.erase(ref->_handle);
if (sendRemove && ref->IsAvailable())
SendRemoveToClients(ref->_victim);
}
void ThreatManager::PutThreatenedByMeRef(ObjectGuid const& guid, ThreatReference* ref)
{
auto& inMap = _threatenedByMe[guid];
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());
inMap = ref;
}
void ThreatManager::PurgeThreatenedByMeRef(ObjectGuid const& guid)
{
auto it = _threatenedByMe.find(guid);
if (it != _threatenedByMe.end())
_threatenedByMe.erase(it);
}
void ThreatManager::UpdateRedirectInfo()
{
_redirectInfo.clear();
uint32 totalPct = 0;
for (auto const& pair : _redirectRegistry) // (spellid, victim -> pct)
for (auto const& victimPair : pair.second) // (victim,pct)
{
uint32 thisPct = std::min<uint32>(100 - totalPct, victimPair.second);
if (thisPct > 0)
{
_redirectInfo.push_back({ victimPair.first, thisPct });
totalPct += thisPct;
ASSERT(totalPct <= 100);
if (totalPct == 100)
return;
}
}
}