-
Notifications
You must be signed in to change notification settings - Fork 293
/
Group.cpp
1356 lines (1134 loc) · 47.5 KB
/
Group.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Common.h"
#include "Opcodes.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "Player.h"
#include "ObjectMgr.h"
#include "ObjectGuid.h"
#include "Group.h"
#include "Formulas.h"
#include "ObjectAccessor.h"
#include "BattleGround/BattleGround.h"
#include "MapManager.h"
#include "MapPersistentStateMgr.h"
//===================================================
//============== Group ==============================
//===================================================
Group::Group() : m_Id(0), m_groupType(GROUPTYPE_NORMAL),
m_dungeonDifficulty(REGULAR_DIFFICULTY), m_raidDifficulty(REGULAR_DIFFICULTY),
m_bgGroup(nullptr), m_lootMethod(FREE_FOR_ALL), m_lootThreshold(ITEM_QUALITY_UNCOMMON),
m_subGroupsCounts(nullptr)
{
}
Group::~Group()
{
if (m_bgGroup)
{
DEBUG_LOG("Group::~Group: battleground group being deleted.");
if (m_bgGroup->GetBgRaid(ALLIANCE) == this)
m_bgGroup->SetBgRaid(ALLIANCE, nullptr);
else if (m_bgGroup->GetBgRaid(HORDE) == this)
m_bgGroup->SetBgRaid(HORDE, nullptr);
else
sLog.outError("Group::~Group: battleground group is not linked to the correct battleground.");
}
// it is undefined whether objectmgr (which stores the groups) or instancesavemgr
// will be unloaded first so we must be prepared for both cases
// this may unload some dungeon persistent state
for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
for (BoundInstancesMap::iterator itr2 = m_boundInstances[i].begin(); itr2 != m_boundInstances[i].end(); ++itr2)
itr2->second.state->RemoveGroup(this);
// Sub group counters clean up
delete[] m_subGroupsCounts;
}
bool Group::Create(ObjectGuid guid, const char* name)
{
m_leaderGuid = guid;
m_leaderName = name;
m_groupType = isBGGroup() ? GROUPTYPE_BGRAID : GROUPTYPE_NORMAL;
if (m_groupType & GROUPTYPE_RAID)
_initRaidSubGroupsCounter();
m_lootMethod = GROUP_LOOT;
m_lootThreshold = ITEM_QUALITY_UNCOMMON;
m_masterLooterGuid = guid;
m_currentLooterGuid = guid; // used for round robin looter
m_dungeonDifficulty = DUNGEON_DIFFICULTY_NORMAL;
m_raidDifficulty = RAID_DIFFICULTY_10MAN_NORMAL;
if (!isBGGroup())
{
m_Id = sObjectMgr.GenerateGroupLowGuid();
Player* leader = sObjectMgr.GetPlayer(guid);
if (leader)
{
m_dungeonDifficulty = leader->GetDungeonDifficulty();
m_raidDifficulty = leader->GetRaidDifficulty();
}
Player::ConvertInstancesToGroup(leader, this, guid);
// store group in database
CharacterDatabase.BeginTransaction();
CharacterDatabase.PExecute("DELETE FROM groups WHERE groupId ='%u'", m_Id);
CharacterDatabase.PExecute("DELETE FROM group_member WHERE groupId ='%u'", m_Id);
CharacterDatabase.PExecute("INSERT INTO groups (groupId,leaderGuid,mainTank,mainAssistant,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,groupType,difficulty,raiddifficulty) "
"VALUES ('%u','%u','%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u','%u')",
m_Id, m_leaderGuid.GetCounter(), m_mainTankGuid.GetCounter(), m_mainAssistantGuid.GetCounter(), uint32(m_lootMethod),
m_masterLooterGuid.GetCounter(), uint32(m_lootThreshold),
m_targetIcons[0].GetRawValue(), m_targetIcons[1].GetRawValue(),
m_targetIcons[2].GetRawValue(), m_targetIcons[3].GetRawValue(),
m_targetIcons[4].GetRawValue(), m_targetIcons[5].GetRawValue(),
m_targetIcons[6].GetRawValue(), m_targetIcons[7].GetRawValue(),
uint8(m_groupType), uint32(m_dungeonDifficulty), uint32(m_raidDifficulty));
}
if (!AddMember(guid, name))
return false;
if (!isBGGroup())
CharacterDatabase.CommitTransaction();
return true;
}
bool Group::LoadGroupFromDB(Field* fields)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// result = CharacterDatabase.Query("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, groupType, difficulty, raiddifficulty, leaderGuid, groupId FROM groups");
m_Id = fields[17].GetUInt32();
m_leaderGuid = ObjectGuid(HIGHGUID_PLAYER, fields[16].GetUInt32());
// group leader not exist
if (!sObjectMgr.GetPlayerNameByGUID(m_leaderGuid, m_leaderName))
return false;
m_groupType = GroupType(fields[13].GetUInt8());
if (m_groupType & GROUPTYPE_RAID)
_initRaidSubGroupsCounter();
uint32 diff = fields[14].GetUInt8();
if (diff >= MAX_DUNGEON_DIFFICULTY)
diff = DUNGEON_DIFFICULTY_NORMAL;
m_dungeonDifficulty = Difficulty(diff);
uint32 r_diff = fields[15].GetUInt8();
if (r_diff >= MAX_RAID_DIFFICULTY)
r_diff = RAID_DIFFICULTY_10MAN_NORMAL;
m_raidDifficulty = Difficulty(r_diff);
m_mainTankGuid = ObjectGuid(HIGHGUID_PLAYER, fields[0].GetUInt32());
m_mainAssistantGuid = ObjectGuid(HIGHGUID_PLAYER, fields[1].GetUInt32());
m_lootMethod = LootMethod(fields[2].GetUInt8());
m_masterLooterGuid = ObjectGuid(HIGHGUID_PLAYER, fields[3].GetUInt32());
m_lootThreshold = ItemQualities(fields[4].GetUInt16());
for (int i = 0; i < TARGET_ICON_COUNT; ++i)
m_targetIcons[i] = ObjectGuid(fields[5 + i].GetUInt64());
m_currentLooterGuid = m_masterLooterGuid;
return true;
}
bool Group::LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant)
{
MemberSlot member;
member.guid = ObjectGuid(HIGHGUID_PLAYER, guidLow);
// skip nonexistent member
if (!sObjectMgr.GetPlayerNameByGUID(member.guid, member.name))
return false;
member.group = subgroup;
member.assistant = assistant;
m_memberSlots.push_back(member);
SubGroupCounterIncrease(subgroup);
return true;
}
void Group::ConvertToRaid()
{
m_groupType = GroupType(m_groupType | GROUPTYPE_RAID);
_initRaidSubGroupsCounter();
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE groups SET groupType = %u WHERE groupId='%u'", uint8(m_groupType), m_Id);
SendUpdate();
// update quest related GO states (quest activity dependent from raid membership)
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
if (Player* player = sObjectMgr.GetPlayer(citr->guid))
player->UpdateForQuestWorldObjects();
}
bool Group::AddInvite(Player* player)
{
if (!player || player->GetGroupInvite())
return false;
Group* group = player->GetGroup();
if (group && group->isBGGroup())
group = player->GetOriginalGroup();
if (group)
return false;
RemoveInvite(player);
m_invitees.insert(player);
player->SetGroupInvite(this);
return true;
}
bool Group::AddLeaderInvite(Player* player)
{
if (!AddInvite(player))
return false;
m_leaderGuid = player->GetObjectGuid();
m_leaderName = player->GetName();
return true;
}
uint32 Group::RemoveInvite(Player* player)
{
m_invitees.erase(player);
player->SetGroupInvite(nullptr);
return GetMembersCount();
}
void Group::RemoveAllInvites()
{
for (InvitesList::iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
(*itr)->SetGroupInvite(nullptr);
m_invitees.clear();
}
Player* Group::GetInvited(ObjectGuid guid) const
{
for (InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
if ((*itr)->GetObjectGuid() == guid)
return (*itr);
return nullptr;
}
Player* Group::GetInvited(const std::string& name) const
{
for (InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
{
if ((*itr)->GetName() == name)
return (*itr);
}
return nullptr;
}
bool Group::AddMember(ObjectGuid guid, const char* name)
{
if (!_addMember(guid, name))
return false;
SendUpdate();
if (Player* player = sObjectMgr.GetPlayer(guid))
{
if (!IsLeader(player->GetObjectGuid()) && !isBGGroup())
{
// reset the new member's instances, unless he is currently in one of them
// including raid/heroic instances that they are not permanently bound to!
player->ResetInstances(INSTANCE_RESET_GROUP_JOIN, false);
player->ResetInstances(INSTANCE_RESET_GROUP_JOIN, true);
if (player->getLevel() >= LEVELREQUIREMENT_HEROIC)
{
if (player->GetDungeonDifficulty() != GetDungeonDifficulty())
{
player->SetDungeonDifficulty(GetDungeonDifficulty());
player->SendDungeonDifficulty(true);
}
if (player->GetRaidDifficulty() != GetRaidDifficulty())
{
player->SetRaidDifficulty(GetRaidDifficulty());
player->SendRaidDifficulty(true);
}
}
}
player->SetGroupUpdateFlag(GROUP_UPDATE_FULL);
UpdatePlayerOutOfRange(player);
// quest related GO state dependent from raid membership
if (isRaidGroup())
player->UpdateForQuestWorldObjects();
}
return true;
}
uint32 Group::RemoveMember(ObjectGuid guid, uint8 method)
{
// remove member and change leader (if need) only if strong more 2 members _before_ member remove
if (GetMembersCount() > uint32(isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group
{
bool leaderChanged = _removeMember(guid);
if (Player* player = sObjectMgr.GetPlayer(guid))
{
// quest related GO state dependent from raid membership
if (isRaidGroup())
player->UpdateForQuestWorldObjects();
WorldPacket data;
if (method == 1)
{
data.Initialize(SMSG_GROUP_UNINVITE, 0);
player->GetSession()->SendPacket(&data);
}
// we already removed player from group and in player->GetGroup() is his original group!
if (Group* group = player->GetGroup())
{
group->SendUpdate();
}
else
{
data.Initialize(SMSG_GROUP_LIST, 1 + 1 + 1 + 1 + 8 + 4 + 4 + 8);
data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
data << uint64(0) << uint32(0) << uint32(0) << uint64(0);
player->GetSession()->SendPacket(&data);
}
_homebindIfInstance(player);
}
if (leaderChanged)
{
WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size() + 1));
data << m_memberSlots.front().name;
BroadcastPacket(&data, true);
}
SendUpdate();
}
// if group before remove <= 2 disband it
else
Disband(true);
return m_memberSlots.size();
}
void Group::ChangeLeader(ObjectGuid guid)
{
member_citerator slot = _getMemberCSlot(guid);
if (slot == m_memberSlots.end())
return;
_setLeader(guid);
WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size() + 1);
data << slot->name;
BroadcastPacket(&data, true);
SendUpdate();
}
void Group::Disband(bool hideDestroy)
{
Player* player;
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
player = sObjectMgr.GetPlayer(citr->guid);
if (!player)
continue;
// we cannot call _removeMember because it would invalidate member iterator
// if we are removing player from battleground raid
if (isBGGroup())
player->RemoveFromBattleGroundRaid();
else
{
// we can remove player who is in battleground from his original group
if (player->GetOriginalGroup() == this)
player->SetOriginalGroup(nullptr);
else
player->SetGroup(nullptr);
}
// quest related GO state dependent from raid membership
if (isRaidGroup())
player->UpdateForQuestWorldObjects();
if (!player->GetSession())
continue;
WorldPacket data;
if (!hideDestroy)
{
data.Initialize(SMSG_GROUP_DESTROYED, 0);
player->GetSession()->SendPacket(&data);
}
// we already removed player from group and in player->GetGroup() is his original group, send update
if (Group* group = player->GetGroup())
{
group->SendUpdate();
}
else
{
data.Initialize(SMSG_GROUP_LIST, 1 + 1 + 1 + 1 + 8 + 4 + 4 + 8);
data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
data << uint64(0) << uint32(0) << uint32(0) << uint64(0);
player->GetSession()->SendPacket(&data);
}
_homebindIfInstance(player);
}
m_memberSlots.clear();
RemoveAllInvites();
if (!isBGGroup())
{
CharacterDatabase.BeginTransaction();
CharacterDatabase.PExecute("DELETE FROM groups WHERE groupId='%u'", m_Id);
CharacterDatabase.PExecute("DELETE FROM group_member WHERE groupId='%u'", m_Id);
CharacterDatabase.CommitTransaction();
ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, nullptr);
ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, nullptr);
}
m_leaderGuid.Clear();
m_leaderName.clear();
}
void Group::SetTargetIcon(uint8 id, ObjectGuid whoGuid, ObjectGuid targetGuid)
{
if (id >= TARGET_ICON_COUNT)
return;
// clean other icons
if (targetGuid)
for (int i = 0; i < TARGET_ICON_COUNT; ++i)
if (m_targetIcons[i] == targetGuid)
SetTargetIcon(i, ObjectGuid(), ObjectGuid());
m_targetIcons[id] = targetGuid;
WorldPacket data(MSG_RAID_TARGET_UPDATE, (1 + 8 + 1 + 8));
data << uint8(0); // set targets
data << whoGuid;
data << uint8(id);
data << targetGuid;
BroadcastPacket(&data, true);
}
static void GetDataForXPAtKill_helper(Player* player, Unit const* victim, uint32& sum_level, Player*& member_with_max_level, Player*& not_gray_member_with_max_level)
{
sum_level += player->getLevel();
if (!member_with_max_level || member_with_max_level->getLevel() < player->getLevel())
member_with_max_level = player;
uint32 gray_level = MaNGOS::XP::GetGrayLevel(player->getLevel());
if (victim->getLevel() > gray_level && (!not_gray_member_with_max_level
|| not_gray_member_with_max_level->getLevel() < player->getLevel()))
not_gray_member_with_max_level = player;
}
void Group::GetDataForXPAtKill(Unit const* victim, uint32& count, uint32& sum_level, Player*& member_with_max_level, Player*& not_gray_member_with_max_level, Player* additional)
{
for (GroupReference* itr = GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* member = itr->getSource();
if (!member || !member->isAlive()) // only for alive
continue;
// will proccesed later
if (member == additional)
continue;
if (!member->IsAtGroupRewardDistance(victim)) // at req. distance
continue;
++count;
GetDataForXPAtKill_helper(member, victim, sum_level, member_with_max_level, not_gray_member_with_max_level);
}
if (additional)
{
if (additional->IsAtGroupRewardDistance(victim)) // at req. distance
{
++count;
GetDataForXPAtKill_helper(additional, victim, sum_level, member_with_max_level, not_gray_member_with_max_level);
}
}
}
void Group::SendTargetIconList(WorldSession* session)
{
if (!session)
return;
WorldPacket data(MSG_RAID_TARGET_UPDATE, (1 + TARGET_ICON_COUNT * 9));
data << uint8(1); // list targets
for (int i = 0; i < TARGET_ICON_COUNT; ++i)
{
if (!m_targetIcons[i])
continue;
data << uint8(i);
data << m_targetIcons[i];
}
session->SendPacket(&data);
}
void Group::SendUpdate()
{
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
Player* player = sObjectMgr.GetPlayer(citr->guid);
if (!player || !player->GetSession() || player->GetGroup() != this)
continue;
// guess size
WorldPacket data(SMSG_GROUP_LIST, (1 + 1 + 1 + 1 + 8 + 4 + GetMembersCount() * 20));
data << uint8(m_groupType); // group type (flags in 3.3)
data << uint8(citr->group); // groupid
data << uint8(GetFlags(*citr)); // group flags
data << uint8(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
if (m_groupType & GROUPTYPE_LFD)
{
data << uint8(0);
data << uint32(0);
}
data << GetObjectGuid(); // group guid
data << uint32(0); // 3.3, this value increments every time SMSG_GROUP_LIST is sent
data << uint32(GetMembersCount() - 1);
for (member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
{
if (citr->guid == citr2->guid)
continue;
Player* member = sObjectMgr.GetPlayer(citr2->guid);
uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
data << citr2->name;
data << citr2->guid;
data << uint8(onlineState); // online-state
data << uint8(citr2->group); // groupid
data << uint8(GetFlags(*citr2)); // group flags
data << uint8(0); // 3.3, role?
}
ObjectGuid masterLootGuid = (m_lootMethod == MASTER_LOOT) ? m_masterLooterGuid : ObjectGuid();
data << m_leaderGuid; // leader guid
if (GetMembersCount() - 1)
{
data << uint8(m_lootMethod); // loot method
data << masterLootGuid; // master loot guid
data << uint8(m_lootThreshold); // loot threshold
data << uint8(m_dungeonDifficulty); // Dungeon Difficulty
data << uint8(m_raidDifficulty); // Raid Difficulty
data << uint8(0); // 3.3, dynamic difficulty?
}
player->GetSession()->SendPacket(&data);
}
}
void Group::UpdatePlayerOutOfRange(Player* pPlayer)
{
if (!pPlayer || !pPlayer->IsInWorld())
return;
if (pPlayer->GetGroupUpdateFlag() == GROUP_UPDATE_FLAG_NONE)
return;
WorldPacket data;
pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
for (GroupReference* itr = GetFirstMember(); itr != nullptr; itr = itr->next())
if (Player* player = itr->getSource())
if (player != pPlayer && !player->HaveAtClient(pPlayer))
player->GetSession()->SendPacket(&data);
}
void Group::BroadcastPacket(WorldPacket* packet, bool ignorePlayersInBGRaid, int group, ObjectGuid ignore)
{
for (GroupReference* itr = GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* pl = itr->getSource();
if (!pl || (ignore && pl->GetObjectGuid() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this))
continue;
if (pl->GetSession() && (group == -1 || itr->getSubGroup() == group))
pl->GetSession()->SendPacket(packet);
}
}
void Group::BroadcastReadyCheck(WorldPacket* packet)
{
for (GroupReference* itr = GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* pl = itr->getSource();
if (pl && pl->GetSession())
if (IsLeader(pl->GetObjectGuid()) || IsAssistant(pl->GetObjectGuid()))
pl->GetSession()->SendPacket(packet);
}
}
void Group::OfflineReadyCheck()
{
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
Player* pl = sObjectMgr.GetPlayer(citr->guid);
if (!pl || !pl->GetSession())
{
WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
data << citr->guid;
data << uint8(0);
BroadcastReadyCheck(&data);
}
}
}
bool Group::_addMember(ObjectGuid guid, const char* name, bool isAssistant)
{
// get first not-full group
uint8 groupid = 0;
if (m_subGroupsCounts)
{
bool groupFound = false;
for (; groupid < MAX_RAID_SUBGROUPS; ++groupid)
{
if (m_subGroupsCounts[groupid] < MAX_GROUP_SIZE)
{
groupFound = true;
break;
}
}
// We are raid group and no one slot is free
if (!groupFound)
return false;
}
return _addMember(guid, name, isAssistant, groupid);
}
bool Group::_addMember(ObjectGuid guid, const char* name, bool isAssistant, uint8 group)
{
if (IsFull())
return false;
if (!guid)
return false;
Player* player = sObjectMgr.GetPlayer(guid, false);
uint32 lastMap = 0;
if (player && player->IsInWorld())
lastMap = player->GetMapId();
else if (player && player->IsBeingTeleported())
lastMap = player->GetTeleportDest().mapid;
MemberSlot member;
member.guid = guid;
member.name = name;
member.group = group;
member.assistant = isAssistant;
member.lastMap = lastMap;
m_memberSlots.push_back(member);
SubGroupCounterIncrease(group);
if (player)
{
player->SetGroupInvite(nullptr);
// if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid()
if (player->GetGroup() && isBGGroup())
player->SetBattleGroundRaid(this, group);
// if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup()
else if (player->GetGroup())
player->SetOriginalGroup(this, group);
// if player is not in group, then call set group
else
player->SetGroup(this, group);
if (player->IsInWorld())
{
// if the same group invites the player back, cancel the homebind timer
if (InstanceGroupBind* bind = GetBoundInstance(player->GetMapId(), player))
if (bind->state->GetInstanceId() == player->GetInstanceId())
player->m_InstanceValid = true;
}
}
if (!isRaidGroup()) // reset targetIcons for non-raid-groups
{
for (int i = 0; i < TARGET_ICON_COUNT; ++i)
m_targetIcons[i].Clear();
}
if (!isBGGroup())
{
// insert into group table
CharacterDatabase.PExecute("INSERT INTO group_member(groupId,memberGuid,assistant,subgroup) VALUES('%u','%u','%u','%u')",
m_Id, member.guid.GetCounter(), ((member.assistant == 1) ? 1 : 0), member.group);
}
return true;
}
bool Group::_removeMember(ObjectGuid guid)
{
Player* player = sObjectMgr.GetPlayer(guid);
if (player)
{
// if we are removing player from battleground raid
if (isBGGroup())
player->RemoveFromBattleGroundRaid();
else
{
// we can remove player who is in battleground from his original group
if (player->GetOriginalGroup() == this)
player->SetOriginalGroup(nullptr);
else
player->SetGroup(nullptr);
}
}
member_witerator slot = _getMemberWSlot(guid);
if (slot != m_memberSlots.end())
{
SubGroupCounterDecrease(slot->group);
m_memberSlots.erase(slot);
}
if (!isBGGroup())
CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", guid.GetCounter());
if (m_leaderGuid == guid) // leader was removed
{
if (GetMembersCount() > 0)
_setLeader(m_memberSlots.front().guid);
return true;
}
return false;
}
void Group::_setLeader(ObjectGuid guid)
{
member_citerator slot = _getMemberCSlot(guid);
if (slot == m_memberSlots.end())
return;
if (!isBGGroup())
{
uint32 slot_lowguid = slot->guid.GetCounter();
uint32 leader_lowguid = m_leaderGuid.GetCounter();
// TODO: set a time limit to have this function run rarely cause it can be slow
CharacterDatabase.BeginTransaction();
// update the group's bound instances when changing leaders
// remove all permanent binds from the group
// in the DB also remove solo binds that will be replaced with permbinds
// from the new leader
CharacterDatabase.PExecute(
"DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
"instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
")", leader_lowguid, slot_lowguid);
Player* player = sObjectMgr.GetPlayer(slot->guid);
if (player)
{
for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
{
for (BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
{
if (itr->second.perm)
{
itr->second.state->RemoveGroup(this);
m_boundInstances[i].erase(itr++);
}
else
++itr;
}
}
}
// update the group's solo binds to the new leader
CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'",
slot_lowguid, leader_lowguid);
// copy the permanent binds from the new leader to the group
// overwriting the solo binds with permanent ones if necessary
// in the DB those have been deleted already
Player::ConvertInstancesToGroup(player, this, slot->guid);
// update the group leader
CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE groupId='%u'", slot_lowguid, m_Id);
CharacterDatabase.CommitTransaction();
}
m_leaderGuid = slot->guid;
m_leaderName = slot->name;
}
bool Group::_setMembersGroup(ObjectGuid guid, uint8 group)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return false;
slot->group = group;
SubGroupCounterIncrease(group);
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, guid.GetCounter());
return true;
}
bool Group::_setAssistantFlag(ObjectGuid guid, const bool& state)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return false;
slot->assistant = state;
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state == true) ? 1 : 0, guid.GetCounter());
return true;
}
bool Group::_setMainTank(ObjectGuid guid)
{
if (m_mainTankGuid == guid)
return false;
if (guid)
{
member_citerator slot = _getMemberCSlot(guid);
if (slot == m_memberSlots.end())
return false;
if (m_mainAssistantGuid == guid)
_setMainAssistant(ObjectGuid());
}
m_mainTankGuid = guid;
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE groupId='%u'", m_mainTankGuid.GetCounter(), m_Id);
return true;
}
bool Group::_setMainAssistant(ObjectGuid guid)
{
if (m_mainAssistantGuid == guid)
return false;
if (guid)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return false;
if (m_mainTankGuid == guid)
_setMainTank(ObjectGuid());
}
m_mainAssistantGuid = guid;
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE groupId='%u'",
m_mainAssistantGuid.GetCounter(), m_Id);
return true;
}
bool Group::SameSubGroup(Player const* member1, Player const* member2) const
{
if (!member1 || !member2)
return false;
if (member1->GetGroup() != this || member2->GetGroup() != this)
return false;
else
return member1->GetSubGroup() == member2->GetSubGroup();
}
// allows setting subgroup for offline members
void Group::ChangeMembersGroup(ObjectGuid guid, uint8 group)
{
if (!isRaidGroup())
return;
Player* player = sObjectMgr.GetPlayer(guid);
if (!player)
{
uint8 prevSubGroup = GetMemberGroup(guid);
if (prevSubGroup == group)
return;
if (_setMembersGroup(guid, group))
{
SubGroupCounterDecrease(prevSubGroup);
SendUpdate();
}
}
else
// This methods handles itself groupcounter decrease
ChangeMembersGroup(player, group);
}
// only for online members
void Group::ChangeMembersGroup(Player* player, uint8 group)
{
if (!player || !isRaidGroup())
return;
uint8 prevSubGroup = player->GetSubGroup();
if (prevSubGroup == group)
return;
if (_setMembersGroup(player->GetObjectGuid(), group))
{
if (player->GetGroup() == this)
player->GetGroupRef().setSubGroup(group);
// if player is in BG raid, it is possible that he is also in normal raid - and that normal raid is stored in m_originalGroup reference
else
{
prevSubGroup = player->GetOriginalSubGroup();
player->GetOriginalGroupRef().setSubGroup(group);
}
SubGroupCounterDecrease(prevSubGroup);
SendUpdate();
}
}
uint32 Group::GetMaxSkillValueForGroup(SkillType skill)
{
uint32 maxvalue = 0;
for (GroupReference* itr = GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* member = itr->getSource();
if (!member)
continue;
uint32 value = member->GetSkillValue(skill);
if (maxvalue < value)
maxvalue = value;
}
return maxvalue;
}
GroupJoinBattlegroundResult Group::CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 /*MaxPlayerCount*/, bool isRated, uint32 arenaSlot)
{
BattlemasterListEntry const* bgEntry = sBattlemasterListStore.LookupEntry(bgOrTemplate->GetTypeID());
if (!bgEntry)
return ERR_GROUP_JOIN_BATTLEGROUND_FAIL; // shouldn't happen
// check for min / max count
uint32 memberscount = GetMembersCount();
// only check for MinPlayerCount since MinPlayerCount == MaxPlayerCount for arenas...
if (bgOrTemplate->isArena() && memberscount != MinPlayerCount)
return ERR_ARENA_TEAM_PARTY_SIZE;
if (memberscount > bgEntry->maxGroupSize) // no MinPlayerCount for battlegrounds
return ERR_BATTLEGROUND_NONE; // ERR_GROUP_JOIN_BATTLEGROUND_TOO_MANY handled on client side
// get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
Player* reference = GetFirstMember()->getSource();
// no reference found, can't join this way
if (!reference)
return ERR_BATTLEGROUND_JOIN_FAILED;
PvPDifficultyEntry const* bracketEntry = GetBattlegroundBracketByLevel(bgOrTemplate->GetMapId(), reference->getLevel());
if (!bracketEntry)
return ERR_BATTLEGROUND_JOIN_FAILED;
uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
Team team = reference->GetTeam();
// check every member of the group to be able to join