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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
|
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://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, 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 "World.h"
#include "ObjectMgr.h"
#include "Group.h"
#include "Formulas.h"
#include "ObjectAccessor.h"
#include "BattleGround.h"
#include "MapManager.h"
#include "InstanceSaveMgr.h"
#include "MapInstanced.h"
#include "Util.h"
Group::Group()
{
m_leaderGuid = 0;
m_groupType = GroupType(0);
m_bgGroup = NULL;
m_lootMethod = LootMethod(0);
m_looterGuid = 0;
m_lootThreshold = ITEM_QUALITY_UNCOMMON;
m_subGroupsCounts = NULL;
for (uint8 i = 0; i < TARGETICONCOUNT; ++i)
m_targetIcons[i] = 0;
}
Group::~Group()
{
if (m_bgGroup)
{
sLog.outDebug("Group::~Group: battleground group being deleted.");
if (m_bgGroup->GetBgRaid(ALLIANCE) == this) m_bgGroup->SetBgRaid(ALLIANCE, NULL);
else if (m_bgGroup->GetBgRaid(HORDE) == this) m_bgGroup->SetBgRaid(HORDE, NULL);
else sLog.outError("Group::~Group: battleground group is not linked to the correct battleground.");
}
Rolls::iterator itr;
while (!RollId.empty())
{
itr = RollId.begin();
Roll *r = *itr;
RollId.erase(itr);
delete(r);
}
// 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 instance saves
for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
for (BoundInstancesMap::iterator itr2 = m_boundInstances[i].begin(); itr2 != m_boundInstances[i].end(); ++itr2)
itr2->second.save->RemoveGroup(this);
// Sub group counters clean up
if (m_subGroupsCounts)
delete[] m_subGroupsCounts;
}
bool Group::Create(const uint64 &guid, const char * name)
{
uint32 lowguid = objmgr.GenerateLowGuid(HIGHGUID_GROUP);
m_guid = MAKE_NEW_GUID(lowguid, 0, HIGHGUID_GROUP);
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_looterGuid = guid;
m_dungeonDifficulty = DUNGEON_DIFFICULTY_NORMAL;
m_raidDifficulty = RAID_DIFFICULTY_10MAN_NORMAL;
if (!isBGGroup())
{
Player *leader = objmgr.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 guid ='%u'", lowguid);
CharacterDatabase.PExecute("DELETE FROM group_member WHERE guid ='%u'", lowguid);
CharacterDatabase.PExecute("INSERT INTO groups (guid,leaderGuid,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,groupType,difficulty,raiddifficulty) "
"VALUES ('%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u','%u')",
lowguid, GUID_LOPART(m_leaderGuid), uint32(m_lootMethod),
GUID_LOPART(m_looterGuid), uint32(m_lootThreshold), m_targetIcons[0], m_targetIcons[1], m_targetIcons[2], m_targetIcons[3], m_targetIcons[4], m_targetIcons[5], m_targetIcons[6], m_targetIcons[7], uint8(m_groupType), uint32(m_dungeonDifficulty), m_raidDifficulty);
if (!AddMember(guid, name))
{
CharacterDatabase.RollbackTransaction();
return false;
}
CharacterDatabase.CommitTransaction();
}
else if (!AddMember(guid, name))
return false;
return true;
}
bool Group::LoadGroupFromDB(const uint64 &groupGuid, QueryResult_AutoPtr result, bool loadMembers)
{
if (isBGGroup())
return false;
uint32 groupLowGuid = GUID_LOPART(groupGuid);
if (!result)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
result = CharacterDatabase.PQuery("SELECT leaderGuid, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, groupType, difficulty, raiddifficulty FROM groups WHERE guid=%u", groupLowGuid);
if (!result)
return false;
}
Field *fields = result->Fetch();
m_guid = groupGuid;
m_leaderGuid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER);
// group leader not exist
if (!objmgr.GetPlayerNameByGUID(fields[0].GetUInt32(), m_leaderName))
return false;
m_lootMethod = (LootMethod)fields[1].GetUInt8();
m_looterGuid = MAKE_NEW_GUID(fields[2].GetUInt32(), 0, HIGHGUID_PLAYER);
m_lootThreshold = (ItemQualities)fields[3].GetUInt16();
for (uint8 i = 0; i < TARGETICONCOUNT; ++i)
m_targetIcons[i] = fields[4+i].GetUInt64();
m_groupType = GroupType(fields[12].GetUInt8());
if (m_groupType & GROUPTYPE_RAID)
_initRaidSubGroupsCounter();
uint32 diff = fields[13].GetUInt8();
if (diff >= MAX_DUNGEON_DIFFICULTY)
diff = DUNGEON_DIFFICULTY_NORMAL;
m_dungeonDifficulty = Difficulty(diff);
uint32 r_diff = fields[14].GetUInt8();
if (r_diff >= MAX_RAID_DIFFICULTY)
r_diff = RAID_DIFFICULTY_10MAN_NORMAL;
m_raidDifficulty = Difficulty(r_diff);
if (loadMembers)
{
result = CharacterDatabase.PQuery("SELECT memberGuid, memberFlags, subgroup FROM group_member WHERE guid=%u", groupLowGuid);
if (!result)
return false;
do
{
fields = result->Fetch();
LoadMemberFromDB(fields[0].GetUInt32(), fields[1].GetUInt8(), fields[2].GetUInt8());
} while (result->NextRow());
if (GetMembersCount() < 2) // group too small
return false;
}
return true;
}
bool Group::LoadMemberFromDB(uint32 guidLow, uint8 memberFlags, uint8 subgroup)
{
MemberSlot member;
member.guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
// skip non-existed member
if (!objmgr.GetPlayerNameByGUID(member.guid, member.name))
{
CharacterDatabase.PQuery("DELETE FROM group_member WHERE memberGuid=%u", guidLow);
return false;
}
member.group = subgroup;
member.flags = memberFlags;
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 guid='%u'", uint8(m_groupType), GUID_LOPART(m_guid));
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 = objmgr.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->GetGUID();
m_leaderName = player->GetName();
return true;
}
uint32 Group::RemoveInvite(Player *player)
{
m_invitees.erase(player);
player->SetGroupInvite(NULL);
return GetMembersCount();
}
void Group::RemoveAllInvites()
{
for (InvitesList::iterator itr=m_invitees.begin(); itr != m_invitees.end(); ++itr)
(*itr)->SetGroupInvite(NULL);
m_invitees.clear();
}
Player* Group::GetInvited(const uint64& guid) const
{
for (InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
{
if ((*itr)->GetGUID() == guid)
return (*itr);
}
return NULL;
}
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 NULL;
}
bool Group::AddMember(const uint64 &guid, const char* name)
{
if (!_addMember(guid, name))
return false;
SendUpdate();
Player *player = objmgr.GetPlayer(guid);
if (player)
{
if (!IsLeader(player->GetGUID()) && !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 memebership
if (isRaidGroup())
player->UpdateForQuestWorldObjects();
}
return true;
}
uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method)
{
BroadcastGroupUpdate();
// remove member and change leader (if need) only if strong more 2 members _before_ member remove
if (GetMembersCount() > (isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group
{
bool leaderChanged = _removeMember(guid);
if (Player *player = objmgr.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(const uint64 &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 = objmgr.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(NULL);
else
player->SetGroup(NULL);
}
// 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);
}
RollId.clear();
m_memberSlots.clear();
RemoveAllInvites();
if (!isBGGroup())
{
uint32 lowguid = GUID_LOPART(m_guid);
CharacterDatabase.BeginTransaction();
CharacterDatabase.PExecute("DELETE FROM groups WHERE guid=%u", lowguid);
CharacterDatabase.PExecute("DELETE FROM group_member WHERE guid=%u", lowguid);
CharacterDatabase.CommitTransaction();
ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, NULL);
ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, NULL);
// FIXME - Safe check! Debug purposes - Will remove after a time if got no reports
QueryResult_AutoPtr result = CharacterDatabase.PQuery("SELECT COUNT(1) FROM group_instance WHERE guid=%u", lowguid);
if (result)
{
sLog.outError("Group::Disband: %u instances are not being properly deleted from group %u", (*result)[0].GetUInt8(), lowguid);
CharacterDatabase.PExecute("DELETE FROM group_instance WHERE guid=%u", lowguid);
}
}
m_guid = 0;
m_leaderGuid = 0;
m_leaderName = "";
}
/*********************************************************/
/*** LOOT SYSTEM ***/
/*********************************************************/
void Group::SendLootStartRoll(uint32 CountDown, uint32 mapid, const Roll &r)
{
WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4+4+1));
data << uint64(r.itemGUID); // guid of rolled item
data << uint32(mapid); // 3.3.3 mapid
data << uint32(r.totalPlayersRolling); // maybe the number of players rolling for it???
data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
data << uint32(r.itemRandomSuffix); // randomSuffix
data << uint32(r.itemRandomPropId); // item random property ID
data << uint32(r.itemCount); // items in stack
data << uint32(CountDown); // the countdown time to choose "need" or "greed"
data << uint8(ALL_ROLL_TYPE_MASK); // roll type mask
for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second == NOT_EMITED_YET)
p->GetSession()->SendPacket(&data);
}
}
void Group::SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
{
WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1+1));
data << uint64(SourceGuid); // guid of the item rolled
data << uint32(0); // unknown, maybe amount of players
data << uint64(TargetGuid);
data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
data << uint32(r.itemRandomSuffix); // randomSuffix
data << uint32(r.itemRandomPropId); // Item random property ID
data << uint8(RollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
data << uint8(RollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
data << uint8(0); // auto pass on NeedBeforeGreed loot because player cannot use the object
for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second != NOT_VALID)
p->GetSession()->SendPacket(&data);
}
}
void Group::SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
{
WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
data << uint64(SourceGuid); // guid of the item rolled
data << uint32(0); // unknown, maybe amount of players
data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
data << uint32(r.itemRandomSuffix); // randomSuffix
data << uint32(r.itemRandomPropId); // Item random property
data << uint64(TargetGuid); // guid of the player who won.
data << uint8(RollNumber); // rollnumber realted to SMSG_LOOT_ROLL
data << uint8(RollType); // Rolltype related to SMSG_LOOT_ROLL
for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second != NOT_VALID)
p->GetSession()->SendPacket(&data);
}
}
void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r)
{
WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
data << uint64(r.itemGUID); // Guid of the item rolled
data << uint32(NumberOfPlayers); // The number of players rolling for it???
data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for
data << uint32(r.itemRandomPropId); // Item random property ID
data << uint32(r.itemRandomSuffix); // Item random suffix ID
for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second != NOT_VALID)
p->GetSession()->SendPacket(&data);
}
}
// notify group members which player is the allowed looter for the given creature
void Group::SendLooter(Creature *pCreature, Player *pLooter)
{
assert(pCreature);
WorldPacket data(SMSG_LOOT_LIST, (8+8));
data << uint64(pCreature->GetGUID());
data << uint8(0); // unk1
if (pLooter)
data.append(pLooter->GetPackGUID());
else
data << uint8(0);
BroadcastPacket(&data, false);
}
void Group::GroupLoot(Loot *loot, WorldObject* pLootedObject)
{
std::vector<LootItem>::iterator i;
ItemPrototype const *item;
uint8 itemSlot = 0;
for (i = loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
{
if (i->freeforall)
continue;
item = objmgr.GetItemPrototype(i->itemid);
if (!item)
{
//sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
continue;
}
//roll for over-threshold item if it's one-player loot
if (item->Quality >= uint32(m_lootThreshold))
{
uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
Roll* r = new Roll(newitemGUID,*i);
//a vector is filled with only near party members
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *member = itr->getSource();
if (!member || !member->GetSession())
continue;
if (i->AllowedForPlayer(member))
{
if (member->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
{
r->totalPlayersRolling++;
if (member->GetPassOnGroupLoot())
{
r->playerVote[member->GetGUID()] = PASS;
r->totalPass++;
// can't broadcast the pass now. need to wait until all rolling players are known.
}
else
{
r->playerVote[member->GetGUID()] = NOT_EMITED_YET;
}
}
}
}
if (r->totalPlayersRolling > 0)
{
r->setLoot(loot);
r->itemSlot = itemSlot;
loot->items[itemSlot].is_blocked = true;
// If there is any "auto pass", broadcast the pass now.
if (r->totalPass)
{
for (Roll::PlayerVote::const_iterator itr=r->playerVote.begin(); itr != r->playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second == PASS)
SendLootRoll(newitemGUID, p->GetGUID(), 128, ROLL_PASS, *r);
}
}
SendLootStartRoll(60000, pLootedObject->GetMapId(), *r);
RollId.push_back(r);
if (Creature* creature = dynamic_cast<Creature *>(pLootedObject))
{
creature->m_groupLootTimer = 60000;
creature->lootingGroupGUID = GetGUID();
}
else if (GameObject* go = dynamic_cast<GameObject *>(pLootedObject))
{
go->m_groupLootTimer = 60000;
go->lootingGroupGUID = GetGUID();
}
}
else
{
delete r;
}
}
else
i->is_underthreshold=1;
}
}
void Group::NeedBeforeGreed(Loot *loot, WorldObject* pLootedObject)
{
ItemPrototype const *item;
uint8 itemSlot = 0;
for (std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
{
if (i->freeforall)
continue;
item = objmgr.GetItemPrototype(i->itemid);
//roll for over-threshold item if it's one-player loot
if (item->Quality >= uint32(m_lootThreshold))
{
uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
Roll* r=new Roll(newitemGUID,*i);
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *playerToRoll = itr->getSource();
if (!playerToRoll || !playerToRoll->GetSession())
continue;
if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll))
{
if (playerToRoll->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
{
r->totalPlayersRolling++;
if (playerToRoll->GetPassOnGroupLoot())
{
r->playerVote[playerToRoll->GetGUID()] = PASS;
r->totalPass++;
// can't broadcast the pass now. need to wait until all rolling players are known.
}
else
{
r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET;
}
}
}
}
if (r->totalPlayersRolling > 0)
{
r->setLoot(loot);
r->itemSlot = itemSlot;
loot->items[itemSlot].is_blocked = true;
// If there is any "auto pass", broadcast the pass now.
if (r->totalPass)
{
for (Roll::PlayerVote::const_iterator itr=r->playerVote.begin(); itr != r->playerVote.end(); ++itr)
{
Player *p = objmgr.GetPlayer(itr->first);
if (!p || !p->GetSession())
continue;
if (itr->second == PASS)
SendLootRoll(newitemGUID, p->GetGUID(), 128, ROLL_PASS, *r);
}
}
SendLootStartRoll(60000, pLootedObject->GetMapId(), *r);
RollId.push_back(r);
if (Creature* creature = dynamic_cast<Creature *>(pLootedObject))
{
creature->m_groupLootTimer = 60000;
creature->lootingGroupGUID = GetGUID();
}
}
else
{
delete r;
}
}
else
i->is_underthreshold=1;
}
}
void Group::MasterLoot(Loot* /*loot*/, WorldObject* pLootedObject)
{
sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330)");
uint32 real_count = 0;
WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
data << (uint8)GetMembersCount();
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *looter = itr->getSource();
if (!looter->IsInWorld())
continue;
if (looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
{
data << uint64(looter->GetGUID());
++real_count;
}
}
data.put<uint8>(0,real_count);
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *looter = itr->getSource();
if (looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
looter->GetSession()->SendPacket(&data);
}
}
void Group::CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choice)
{
Rolls::iterator rollI = GetRoll(Guid);
if (rollI == RollId.end())
return;
Roll* roll = *rollI;
Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID);
// this condition means that player joins to the party after roll begins
if (itr == roll->playerVote.end())
return;
if (roll->getLoot())
if (roll->getLoot()->items.empty())
return;
switch (Choice)
{
case ROLL_PASS: // Player choose pass
SendLootRoll(0, playerGUID, 128, ROLL_PASS, *roll);
++roll->totalPass;
itr->second = PASS;
break;
case ROLL_NEED: // player choose Need
SendLootRoll(0, playerGUID, 0, 0, *roll);
++roll->totalNeed;
itr->second = NEED;
break;
case ROLL_GREED: // player choose Greed
SendLootRoll(0, playerGUID, 128, ROLL_GREED, *roll);
++roll->totalGreed;
itr->second = GREED;
break;
case ROLL_DISENCHANT: // player choose Disenchant
SendLootRoll(0, playerGUID, 128, ROLL_DISENCHANT, *roll);
++roll->totalGreed;
itr->second = DISENCHANT;
break;
}
if (roll->totalPass + roll->totalNeed + roll->totalGreed >= roll->totalPlayersRolling)
{
CountTheRoll(rollI, NumberOfPlayers);
}
}
//called when roll timer expires
void Group::EndRoll(Loot *pLoot)
{
for (Rolls::iterator itr = RollId.begin(); itr != RollId.end();)
{
if ((*itr)->getLoot() == pLoot) {
CountTheRoll(itr, GetMembersCount()); //i don't have to edit player votes, who didn't vote ... he will pass
itr = RollId.begin();
}
else
itr++;
}
}
void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers)
{
Roll* roll = *rollI;
if (!roll->isValid()) // is loot already deleted ?
{
RollId.erase(rollI);
delete roll;
return;
}
//end of the roll
if (roll->totalNeed > 0)
{
if (!roll->playerVote.empty())
{
uint8 maxresul = 0;
uint64 maxguid = (*roll->playerVote.begin()).first;
Player *player;
for (Roll::PlayerVote::const_iterator itr=roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
{
if (itr->second != NEED)
continue;
uint8 randomN = urand(1, 100);
SendLootRoll(0, itr->first, randomN, ROLL_NEED, *roll);
if (maxresul < randomN)
{
maxguid = itr->first;
maxresul = randomN;
}
}
SendLootRollWon(0, maxguid, maxresul, ROLL_NEED, *roll);
player = objmgr.GetPlayer(maxguid);
if (player && player->GetSession())
{
player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul);
ItemPosCountVec dest;
LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
uint8 msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count);
if (msg == EQUIP_ERR_OK)
{
item->is_looted = true;
roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
roll->getLoot()->unlootedCount--;
player->StoreNewItem(dest, roll->itemid, true, item->randomPropertyId);
}
else
{
item->is_blocked = false;
player->SendEquipError(msg, NULL, NULL);
}
}
}
}
else if (roll->totalGreed > 0)
{
if (!roll->playerVote.empty())
{
uint8 maxresul = 0;
uint64 maxguid = (*roll->playerVote.begin()).first;
Player *player;
RollVote rollvote = NOT_VALID;
Roll::PlayerVote::iterator itr;
for (itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
{
if (itr->second != GREED && itr->second != DISENCHANT)
continue;
uint8 randomN = urand(1, 100);
SendLootRoll(0, itr->first, randomN, itr->second, *roll);
if (maxresul < randomN)
{
maxguid = itr->first;
maxresul = randomN;
rollvote = itr->second;
}
}
SendLootRollWon(0, maxguid, maxresul, rollvote, *roll);
player = objmgr.GetPlayer(maxguid);
if (player && player->GetSession())
{
player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul);
LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
if (rollvote == GREED)
{
ItemPosCountVec dest;
uint8 msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count);
if (msg == EQUIP_ERR_OK)
{
item->is_looted = true;
roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
roll->getLoot()->unlootedCount--;
player->StoreNewItem(dest, roll->itemid, true, item->randomPropertyId);
}
else
{
item->is_blocked = false;
player->SendEquipError(msg, NULL, NULL);
}
}
else if (rollvote == DISENCHANT)
{
item->is_looted = true;
roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
roll->getLoot()->unlootedCount--;
ItemPrototype const *pProto = ObjectMgr::GetItemPrototype(roll->itemid);
player->AutoStoreLoot(pProto->DisenchantID, LootTemplates_Disenchant, true);
}
}
}
}
else
{
SendLootAllPassed(NumberOfPlayers, *roll);
// remove is_blocked so that the item is lootable by all players
LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
if (item)
item->is_blocked = false;
}
RollId.erase(rollI);
delete roll;
}
void Group::SetTargetIcon(uint8 id, uint64 whoGuid, uint64 targetGuid)
{
if (id >= TARGETICONCOUNT)
return;
// clean other icons
if (targetGuid != 0)
for (int i=0; i<TARGETICONCOUNT; ++i)
if (m_targetIcons[i] == targetGuid)
SetTargetIcon(i, 0, 0);
m_targetIcons[id] = targetGuid;
WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+8+1+8));
data << uint8(0); // set targets
data << uint64(whoGuid);
data << uint8(id);
data << uint64(targetGuid);
BroadcastPacket(&data, true);
}
void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level)
{
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player* member = itr->getSource();
if (!member || !member->isAlive()) // only for alive
continue;
if (!member->IsAtGroupRewardDistance(victim)) // at req. distance
continue;
++count;
sum_level += member->getLevel();
if (!member_with_max_level || member_with_max_level->getLevel() < member->getLevel())
member_with_max_level = member;
uint32 gray_level = Trinity::XP::GetGrayLevel(member->getLevel());
if (victim->getLevel() > gray_level && (!not_gray_member_with_max_level
|| not_gray_member_with_max_level->getLevel() < member->getLevel()))
not_gray_member_with_max_level = member;
}
}
void Group::SendTargetIconList(WorldSession *session)
{
if (!session)
return;
WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
data << uint8(1); // list targets
for (int i=0; i<TARGETICONCOUNT; ++i)
{
if (m_targetIcons[i] == 0)
continue;
data << uint8(i);
data << uint64(m_targetIcons[i]);
}
session->SendPacket(&data);
}
void Group::SendUpdate()
{
Player *player;
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
player = objmgr.GetPlayer(citr->guid);
if (!player || !player->GetSession() || player->GetGroup() != this)
continue;
WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+1+4+8+4+4+(GetMembersCount()-1)*(13+8+1+1+1+1)+8+1+8+1+1+1+1));
data << uint8(m_groupType); // group type (flags in 3.3)
data << uint8(citr->group);
data << uint8(citr->flags);
data << uint8(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
if (m_groupType & GROUPTYPE_LFD)
{
data << uint8(0);
data << uint32(0);
}
data << uint64(m_guid);
data << uint32(m_counter++); // 3.3, value increases every time this packet gets 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 = objmgr.GetPlayer(citr2->guid);
uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
data << citr2->name;
data << uint64(citr2->guid); // guid
data << uint8(onlineState); // online-state
data << uint8(citr2->group); // groupid
data << uint8(citr2->flags); // See enum GroupMemberFlags
data << uint8(0); // 3.3
}
data << uint64(m_leaderGuid); // leader guid
if (GetMembersCount()-1)
{
data << uint8(m_lootMethod); // loot method
data << uint64(m_looterGuid); // looter guid
data << uint8(m_lootThreshold); // loot threshold
data << uint8(m_dungeonDifficulty); // Dungeon Difficulty
data << uint8(m_raidDifficulty); // Raid Difficulty
data << uint8(0); // 3.3
}
player->GetSession()->SendPacket(&data);
}
}
void Group::UpdatePlayerOutOfRange(Player* pPlayer)
{
if (!pPlayer || !pPlayer->IsInWorld())
return;
Player *player;
WorldPacket data;
pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
player = itr->getSource();
if (player && player != pPlayer && !pPlayer->isVisibleFor(player))
player->GetSession()->SendPacket(&data);
}
}
void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore)
{
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *pl = itr->getSource();
if (!pl || (ignore != 0 && pl->GetGUID() == 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 != NULL; itr = itr->next())
{
Player *pl = itr->getSource();
if (pl && pl->GetSession())
if (IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
pl->GetSession()->SendPacket(packet);
}
}
void Group::OfflineReadyCheck()
{
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
Player *pl = objmgr.GetPlayer(citr->guid);
if (!pl || !pl->GetSession())
{
WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
data << uint64(citr->guid);
data << uint8(0);
BroadcastReadyCheck(&data);
}
}
}
bool Group::_addMember(const uint64 &guid, const char* name)
{
// get first not-full group
uint8 groupid = 0;
if (m_subGroupsCounts)
{
bool groupFound = false;
for (; groupid < MAXRAIDSIZE/MAXGROUPSIZE; ++groupid)
{
if (m_subGroupsCounts[groupid] < MAXGROUPSIZE)
{
groupFound = true;
break;
}
}
// We are raid group and no one slot is free
if (!groupFound)
return false;
}
return _addMember(guid, name, groupid);
}
bool Group::_addMember(const uint64 &guid, const char* name, uint8 group)
{
if (IsFull())
return false;
if (!guid)
return false;
Player *player = objmgr.GetPlayer(guid);
MemberSlot member;
member.guid = guid;
member.name = name;
member.group = group;
member.flags = 0;
m_memberSlots.push_back(member);
SubGroupCounterIncrease(group);
if (player)
{
player->SetGroupInvite(NULL);
//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 the same group invites the player back, cancel the homebind timer
InstanceGroupBind *bind = GetBoundInstance(player);
if (bind && bind->save->GetInstanceId() == player->GetInstanceId())
player->m_InstanceValid = true;
}
if (!isRaidGroup()) // reset targetIcons for non-raid-groups
{
for (uint8 i = 0; i < TARGETICONCOUNT; ++i)
m_targetIcons[i] = 0;
}
if (!isBGGroup())
{
// insert into group table
CharacterDatabase.PExecute("INSERT INTO group_member(guid,memberGuid,memberFlags,subgroup) VALUES(%u,%u,%u,%u)", GUID_LOPART(m_guid), GUID_LOPART(member.guid), member.flags, member.group);
}
return true;
}
bool Group::_removeMember(const uint64 &guid)
{
Player *player = objmgr.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(NULL);
else
player->SetGroup(NULL);
}
}
_removeRolls(guid);
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_LOPART(guid));
if (m_leaderGuid == guid) // leader was removed
{
if (GetMembersCount() > 0)
_setLeader(m_memberSlots.front().guid);
return true;
}
return false;
}
void Group::_setLeader(const uint64 &guid)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return;
if (!isBGGroup())
{
// 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 guid=%u AND (permanent = 1 OR "
"instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
")", GUID_LOPART(m_guid), GUID_LOPART(slot->guid)
);
Player *player = objmgr.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.save->RemoveGroup(this);
m_boundInstances[i].erase(itr++);
}
else
++itr;
}
}
}
// 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 guid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_guid));
CharacterDatabase.CommitTransaction();
}
m_leaderGuid = slot->guid;
m_leaderName = slot->name;
ToggleGroupMemberFlag(slot, MEMBER_FLAG_ASSISTANT, false);
}
void Group::_removeRolls(const uint64 &guid)
{
for (Rolls::iterator it = RollId.begin(); it != RollId.end(); ++it)
{
Roll* roll = *it;
Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
if (itr2 == roll->playerVote.end())
continue;
if (itr2->second == GREED || itr2->second == DISENCHANT) --roll->totalGreed;
if (itr2->second == NEED) --roll->totalNeed;
if (itr2->second == PASS) --roll->totalPass;
if (itr2->second != NOT_VALID) --roll->totalPlayersRolling;
roll->playerVote.erase(itr2);
CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, MAX_ROLL_TYPE);
}
}
bool Group::_setMembersGroup(const uint64 &guid, const 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_LOPART(guid));
return true;
}
bool Group::_setAssistantFlag(const uint64 &guid, const bool &apply)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return false;
ToggleGroupMemberFlag(slot, MEMBER_FLAG_ASSISTANT, apply);
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid));
return true;
}
bool Group::_setMainTank(const uint64 &guid, const bool &apply)
{
member_witerator slot = _getMemberWSlot(guid); // First check member slots to see if the target exists
if (slot == m_memberSlots.end())
return false;
RemoveUniqueGroupMemberFlag(MEMBER_FLAG_MAINTANK); // Remove main tank flag from current if any.
ToggleGroupMemberFlag(slot, MEMBER_FLAG_MAINTANK, apply); // And apply main tank flag on new main tank.
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid));
return true;
}
bool Group::_setMainAssistant(const uint64 &guid, const bool &apply)
{
member_witerator slot = _getMemberWSlot(guid);
if (slot == m_memberSlots.end())
return false;
RemoveUniqueGroupMemberFlag(MEMBER_FLAG_MAINASSIST); // Remove main assist flag from current if any.
ToggleGroupMemberFlag(slot, MEMBER_FLAG_MAINASSIST, apply); // Apply main assist flag on new main assist.
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid));
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(const uint64 &guid, const uint8 &group)
{
if (!isRaidGroup())
return;
Player *player = objmgr.GetPlayer(guid);
if (!player)
{
uint8 prevSubGroup;
prevSubGroup = GetMemberGroup(guid);
SubGroupCounterDecrease(prevSubGroup);
if (_setMembersGroup(guid, group))
SendUpdate();
}
else
// This methods handles itself groupcounter decrease
ChangeMembersGroup(player, group);
}
// only for online members
void Group::ChangeMembersGroup(Player *player, const uint8 &group)
{
if (!player || !isRaidGroup())
return;
if (_setMembersGroup(player->GetGUID(), group))
{
uint8 prevSubGroup = player->GetSubGroup();
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();
}
}
// Retrieve the next Round-Roubin player for the group
//
// No update done if loot method is Master or FFA.
//
// If the RR player is not yet set for the group, the first group member becomes the round-robin player.
// If the RR player is set, the next player in group becomes the round-robin player.
//
// If ifneed is true,
// the current RR player is checked to be near the looted object.
// if yes, no update done.
// if not, he looses his turn.
void Group::UpdateLooterGuid(WorldObject* pLootedObject, bool ifneed)
{
switch (GetLootMethod())
{
case MASTER_LOOT:
case FREE_FOR_ALL:
return;
default:
// round robin style looting applies for all low
// quality items in each loot method except free for all and master loot
break;
}
uint64 oldLooterGUID = GetLooterGuid();
member_citerator guid_itr = _getMemberCSlot(oldLooterGUID);
if (guid_itr != m_memberSlots.end())
{
if (ifneed)
{
// not update if only update if need and ok
Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
if (looter && looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
return;
}
++guid_itr;
}
// search next after current
Player *pNewLooter = NULL;
for (member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
{
if (Player* pl = ObjectAccessor::FindPlayer(itr->guid))
if (pl->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
{
pNewLooter = pl;
break;
}
}
if (!pNewLooter)
{
// search from start
for (member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
{
if (Player* pl = ObjectAccessor::FindPlayer(itr->guid))
if (pl->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
{
pNewLooter = pl;
break;
}
}
}
if (pNewLooter)
{
if (oldLooterGUID != pNewLooter->GetGUID())
{
SetLooterGuid(pNewLooter->GetGUID());
SendUpdate();
}
}
else
{
SetLooterGuid(0);
SendUpdate();
}
}
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);
uint32 team = reference->GetTeam();
// check every member of the group to be able to join
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *member = itr->getSource();
// offline member? don't let join
if (!member)
return ERR_BATTLEGROUND_JOIN_FAILED;
// don't allow cross-faction join as group
if (member->GetTeam() != team)
return ERR_BATTLEGROUND_JOIN_TIMED_OUT;
// not in the same battleground level braket, don't let join
PvPDifficultyEntry const* memberBracketEntry = GetBattlegroundBracketByLevel(bracketEntry->mapId,member->getLevel());
if (memberBracketEntry != bracketEntry)
return ERR_BATTLEGROUND_JOIN_RANGE_INDEX;
// don't let join rated matches if the arena team id doesn't match
if (isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
return ERR_BATTLEGROUND_JOIN_FAILED;
// don't let join if someone from the group is already in that bg queue
if (member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId))
return ERR_BATTLEGROUND_JOIN_FAILED; // not blizz-like
// check for deserter debuff in case not arena queue
if (bgOrTemplate->GetTypeID() != BATTLEGROUND_AA && !member->CanJoinToBattleground())
return ERR_GROUP_JOIN_BATTLEGROUND_DESERTERS;
// check if member can join any more battleground queues
if (!member->HasFreeBattleGroundQueueId())
return ERR_BATTLEGROUND_TOO_MANY_QUEUES; // not blizz-like
}
return GroupJoinBattlegroundResult(bgOrTemplate->GetTypeID());
}
//===================================================
//============== Roll ===============================
//===================================================
void Roll::targetObjectBuildLink()
{
// called from link()
getTarget()->addLootValidatorRef(this);
}
void Group::SetDungeonDifficulty(Difficulty difficulty)
{
m_dungeonDifficulty = difficulty;
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE guid ='%u'", m_dungeonDifficulty, GUID_LOPART(m_guid));
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *player = itr->getSource();
if (!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
continue;
player->SetDungeonDifficulty(difficulty);
player->SendDungeonDifficulty(true);
}
}
void Group::SetRaidDifficulty(Difficulty difficulty)
{
m_raidDifficulty = difficulty;
if (!isBGGroup())
CharacterDatabase.PExecute("UPDATE groups SET raiddifficulty = %u WHERE guid ='%u'", m_raidDifficulty, GUID_LOPART(m_guid));
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *player = itr->getSource();
if (!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
continue;
player->SetRaidDifficulty(difficulty);
player->SendRaidDifficulty(true);
}
}
bool Group::InCombatToInstance(uint32 instanceId)
{
for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
{
Player *pPlayer = itr->getSource();
if (pPlayer && pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId && (pPlayer->GetMap()->IsRaidOrHeroicDungeon()))
for (std::set<Unit*>::const_iterator i = pPlayer->getAttackers().begin(); i != pPlayer->getAttackers().end(); ++i)
if ((*i) && (*i)->GetTypeId() == TYPEID_UNIT && (*i)->ToCreature()->GetCreatureInfo()->flags_extra & CREATURE_FLAG_EXTRA_INSTANCE_BIND)
return true;
}
return false;
}
void Group::ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo)
{
if (isBGGroup())
return;
// method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
// we assume that when the difficulty changes, all instances that can be reset will be
Difficulty diff = GetDifficulty(isRaid);
for (BoundInstancesMap::iterator itr = m_boundInstances[diff].begin(); itr != m_boundInstances[diff].end();)
{
InstanceSave *p = itr->second.save;
const MapEntry *entry = sMapStore.LookupEntry(itr->first);
if (!entry || entry->IsRaid() != isRaid || !p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND)
{
++itr;
continue;
}
if (method == INSTANCE_RESET_ALL)
{
// the "reset all instances" method can only reset normal maps
if (entry->map_type == MAP_RAID || diff == DUNGEON_DIFFICULTY_HEROIC)
{
++itr;
continue;
}
}
bool isEmpty = true;
// if the map is loaded, reset it
Map *map = MapManager::Instance().FindMap(p->GetMapId(), p->GetInstanceId());
if (map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset()))
{
if (p->CanReset())
isEmpty = ((InstanceMap*)map)->Reset(method);
else
isEmpty = !map->HavePlayers();
}
if (SendMsgTo)
{
if (isEmpty) SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
else SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
}
if (isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
{
// do not reset the instance, just unbind if others are permanently bound to it
if (p->CanReset()) p->DeleteFromDB();
else CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
// i don't know for sure if hash_map iterators
m_boundInstances[diff].erase(itr);
itr = m_boundInstances[diff].begin();
// this unloads the instance save unless online players are bound to it
// (eg. permanent binds or GM solo binds)
p->RemoveGroup(this);
}
else
++itr;
}
}
InstanceGroupBind* Group::GetBoundInstance(Player* player)
{
uint32 mapid = player->GetMapId();
MapEntry const* mapEntry = sMapStore.LookupEntry(mapid);
if (!mapEntry)
return NULL;
Difficulty difficulty = player->GetDifficulty(mapEntry->IsRaid());
// some instances only have one difficulty
MapDifficulty const* mapDiff = GetMapDifficultyData(mapid,difficulty);
if (!mapDiff)
difficulty = DUNGEON_DIFFICULTY_NORMAL;
BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
if (itr != m_boundInstances[difficulty].end())
return &itr->second;
else
return NULL;
}
InstanceGroupBind* Group::GetBoundInstance(Map* aMap)
{
// Currently spawn numbering not different from map difficulty
Difficulty difficulty = GetDifficulty(aMap->IsRaid());
// some instances only have one difficulty
MapDifficulty const* mapDiff = GetMapDifficultyData(aMap->GetId(),difficulty);
if (!mapDiff)
return NULL;
BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(aMap->GetId());
if (itr != m_boundInstances[difficulty].end())
return &itr->second;
else
return NULL;
}
InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
{
if (!save || isBGGroup())
return NULL;
InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
if (!load && (!bind.save || permanent != bind.perm || save != bind.save))
CharacterDatabase.PExecute("REPLACE INTO group_instance (guid, instance, permanent) VALUES (%u, %u, %u)", GUID_LOPART(GetGUID()), save->GetInstanceId(), permanent);
if (bind.save != save)
{
if (bind.save)
bind.save->RemoveGroup(this);
save->AddGroup(this);
}
bind.save = save;
bind.perm = permanent;
if (!load)
sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
return &bind;
}
void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
{
BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
if (itr != m_boundInstances[difficulty].end())
{
if (!unload)
CharacterDatabase.PExecute("DELETE FROM group_instance WHERE guid=%u AND instance=%u", GUID_LOPART(GetGUID()), itr->second.save->GetInstanceId());
itr->second.save->RemoveGroup(this); // save can become invalid
m_boundInstances[difficulty].erase(itr);
}
}
void Group::_homebindIfInstance(Player *player)
{
if (player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
player->m_InstanceValid = false;
}
void Group::BroadcastGroupUpdate(void)
{
// FG: HACK: force flags update on group leave - for values update hack
// -- not very efficient but safe
for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
{
Player *pp = objmgr.GetPlayer(citr->guid);
if (pp && pp->IsInWorld())
{
pp->ForceValuesUpdateAtIndex(UNIT_FIELD_BYTES_2);
pp->ForceValuesUpdateAtIndex(UNIT_FIELD_FACTIONTEMPLATE);
DEBUG_LOG("-- Forced group value update for '%s'", pp->GetName());
}
}
}
|