[9192] Fixed typo in error output for lock id gameobject template data check.
[getmangos.git] / src / game / Group.cpp
blob9f88a1fb3da4ecb88d5e55cb4f3dba005e182964
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Opcodes.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "Player.h"
24 #include "World.h"
25 #include "ObjectMgr.h"
26 #include "ObjectDefines.h"
27 #include "Group.h"
28 #include "Formulas.h"
29 #include "ObjectAccessor.h"
30 #include "BattleGround.h"
31 #include "MapManager.h"
32 #include "InstanceSaveMgr.h"
33 #include "MapInstanced.h"
34 #include "Util.h"
36 Group::Group()
38 m_leaderGuid = 0;
39 m_mainTank = 0;
40 m_mainAssistant = 0;
41 m_groupType = (GroupType)0;
42 m_bgGroup = NULL;
43 m_lootMethod = (LootMethod)0;
44 m_looterGuid = 0;
45 m_lootThreshold = ITEM_QUALITY_UNCOMMON;
46 m_subGroupsCounts = NULL;
48 for (int i = 0; i < TARGETICONCOUNT; ++i)
49 m_targetIcons[i] = 0;
52 Group::~Group()
54 if(m_bgGroup)
56 sLog.outDebug("Group::~Group: battleground group being deleted.");
57 if(m_bgGroup->GetBgRaid(ALLIANCE) == this)
58 m_bgGroup->SetBgRaid(ALLIANCE, NULL);
59 else if(m_bgGroup->GetBgRaid(HORDE) == this)
60 m_bgGroup->SetBgRaid(HORDE, NULL);
61 else
62 sLog.outError("Group::~Group: battleground group is not linked to the correct battleground.");
64 Rolls::iterator itr;
65 while(!RollId.empty())
67 itr = RollId.begin();
68 Roll *r = *itr;
69 RollId.erase(itr);
70 delete(r);
73 // it is undefined whether objectmgr (which stores the groups) or instancesavemgr
74 // will be unloaded first so we must be prepared for both cases
75 // this may unload some instance saves
76 for(uint8 i = 0; i < MAX_DIFFICULTY; ++i)
77 for(BoundInstancesMap::iterator itr2 = m_boundInstances[i].begin(); itr2 != m_boundInstances[i].end(); ++itr2)
78 itr2->second.save->RemoveGroup(this);
80 // Sub group counters clean up
81 if (m_subGroupsCounts)
82 delete[] m_subGroupsCounts;
85 bool Group::Create(const uint64 &guid, const char * name)
87 m_leaderGuid = guid;
88 m_leaderName = name;
90 m_groupType = isBGGroup() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
92 if (m_groupType == GROUPTYPE_RAID)
93 _initRaidSubGroupsCounter();
95 m_lootMethod = GROUP_LOOT;
96 m_lootThreshold = ITEM_QUALITY_UNCOMMON;
97 m_looterGuid = guid;
99 m_dungeonDifficulty = DUNGEON_DIFFICULTY_NORMAL;
100 m_raidDifficulty = RAID_DIFFICULTY_10MAN_NORMAL;
101 if(!isBGGroup())
103 Player *leader = sObjectMgr.GetPlayer(guid);
104 if(leader)
106 m_dungeonDifficulty = leader->GetDungeonDifficulty();
107 m_raidDifficulty = leader->GetRaidDifficulty();
110 Player::ConvertInstancesToGroup(leader, this, guid);
112 // store group in database
113 CharacterDatabase.BeginTransaction();
114 CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
115 CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
116 CharacterDatabase.PExecute("INSERT INTO groups (leaderGuid,mainTank,mainAssistant,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,isRaid,difficulty,raiddifficulty) "
117 "VALUES ('%u','%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u','%u')",
118 GUID_LOPART(m_leaderGuid), GUID_LOPART(m_mainTank), GUID_LOPART(m_mainAssistant), uint32(m_lootMethod),
119 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], isRaidGroup(), uint32(m_dungeonDifficulty), m_raidDifficulty);
122 if(!AddMember(guid, name))
123 return false;
125 if(!isBGGroup())
126 CharacterDatabase.CommitTransaction();
128 return true;
131 bool Group::LoadGroupFromDB(const uint64 &leaderGuid, QueryResult *result, bool loadMembers)
133 if(isBGGroup())
134 return false;
136 bool external = true;
137 if(!result)
139 external = false;
140 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
141 result = CharacterDatabase.PQuery("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, isRaid, difficulty, raiddifficulty FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
142 if(!result)
143 return false;
146 m_leaderGuid = leaderGuid;
148 // group leader not exist
149 if(!sObjectMgr.GetPlayerNameByGUID(m_leaderGuid, m_leaderName))
151 if(!external) delete result;
152 return false;
155 m_groupType = (*result)[13].GetBool() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
157 if (m_groupType == GROUPTYPE_RAID)
158 _initRaidSubGroupsCounter();
160 uint32 diff = (*result)[14].GetUInt8();
161 if (diff >= MAX_DUNGEON_DIFFICULTY)
162 diff = DUNGEON_DIFFICULTY_NORMAL;
163 m_dungeonDifficulty = Difficulty(diff);
165 uint32 r_diff = (*result)[15].GetUInt8();
166 if (r_diff >= MAX_RAID_DIFFICULTY)
167 r_diff = RAID_DIFFICULTY_10MAN_NORMAL;
168 m_raidDifficulty = Difficulty(r_diff);
170 m_mainTank = (*result)[0].GetUInt64();
171 m_mainAssistant = (*result)[1].GetUInt64();
172 m_lootMethod = (LootMethod)(*result)[2].GetUInt8();
173 m_looterGuid = MAKE_NEW_GUID((*result)[3].GetUInt32(), 0, HIGHGUID_PLAYER);
174 m_lootThreshold = (ItemQualities)(*result)[4].GetUInt16();
176 for(int i = 0; i < TARGETICONCOUNT; ++i)
177 m_targetIcons[i] = (*result)[5+i].GetUInt64();
178 if(!external)
179 delete result;
181 if(loadMembers)
183 result = CharacterDatabase.PQuery("SELECT memberGuid, assistant, subgroup FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
184 if(!result)
185 return false;
189 LoadMemberFromDB((*result)[0].GetUInt32(), (*result)[2].GetUInt8(), (*result)[1].GetBool());
190 } while( result->NextRow() );
191 delete result;
192 // group too small
193 if(GetMembersCount() < 2)
194 return false;
197 return true;
200 bool Group::LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant)
202 MemberSlot member;
203 member.guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
205 // skip non-existed member
206 if(!sObjectMgr.GetPlayerNameByGUID(member.guid, member.name))
207 return false;
209 member.group = subgroup;
210 member.assistant = assistant;
211 m_memberSlots.push_back(member);
213 SubGroupCounterIncrease(subgroup);
215 return true;
218 void Group::ConvertToRaid()
220 m_groupType = GROUPTYPE_RAID;
222 _initRaidSubGroupsCounter();
224 if(!isBGGroup())
225 CharacterDatabase.PExecute("UPDATE groups SET isRaid = 1 WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
226 SendUpdate();
228 // update quest related GO states (quest activity dependent from raid membership)
229 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
230 if(Player* player = sObjectMgr.GetPlayer(citr->guid))
231 player->UpdateForQuestWorldObjects();
234 bool Group::AddInvite(Player *player)
236 if( !player || player->GetGroupInvite() )
237 return false;
238 Group* group = player->GetGroup();
239 if( group && group->isBGGroup() )
240 group = player->GetOriginalGroup();
241 if( group )
242 return false;
244 RemoveInvite(player);
246 m_invitees.insert(player);
248 player->SetGroupInvite(this);
250 return true;
253 bool Group::AddLeaderInvite(Player *player)
255 if(!AddInvite(player))
256 return false;
258 m_leaderGuid = player->GetGUID();
259 m_leaderName = player->GetName();
260 return true;
263 uint32 Group::RemoveInvite(Player *player)
265 m_invitees.erase(player);
267 player->SetGroupInvite(NULL);
268 return GetMembersCount();
271 void Group::RemoveAllInvites()
273 for(InvitesList::iterator itr=m_invitees.begin(); itr!=m_invitees.end(); ++itr)
274 (*itr)->SetGroupInvite(NULL);
276 m_invitees.clear();
279 Player* Group::GetInvited(const uint64& guid) const
281 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
283 if((*itr)->GetGUID() == guid)
284 return (*itr);
286 return NULL;
289 Player* Group::GetInvited(const std::string& name) const
291 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
293 if((*itr)->GetName() == name)
294 return (*itr);
296 return NULL;
299 bool Group::AddMember(const uint64 &guid, const char* name)
301 if(!_addMember(guid, name))
302 return false;
303 SendUpdate();
305 Player *player = sObjectMgr.GetPlayer(guid);
306 if(player)
308 if(!IsLeader(player->GetGUID()) && !isBGGroup())
310 // reset the new member's instances, unless he is currently in one of them
311 // including raid/heroic instances that they are not permanently bound to!
312 player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,false);
313 player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,true);
315 if (player->getLevel() >= LEVELREQUIREMENT_HEROIC)
317 if (player->GetDungeonDifficulty() != GetDungeonDifficulty())
319 player->SetDungeonDifficulty(GetDungeonDifficulty());
320 player->SendDungeonDifficulty(true);
322 if (player->GetRaidDifficulty() != GetRaidDifficulty())
324 player->SetRaidDifficulty(GetRaidDifficulty());
325 player->SendRaidDifficulty(true);
329 player->SetGroupUpdateFlag(GROUP_UPDATE_FULL);
330 UpdatePlayerOutOfRange(player);
332 // quest related GO state dependent from raid membership
333 if(isRaidGroup())
334 player->UpdateForQuestWorldObjects();
337 return true;
340 uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method)
342 // remove member and change leader (if need) only if strong more 2 members _before_ member remove
343 if(GetMembersCount() > (isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group
345 bool leaderChanged = _removeMember(guid);
347 if(Player *player = sObjectMgr.GetPlayer( guid ))
349 // quest related GO state dependent from raid membership
350 if(isRaidGroup())
351 player->UpdateForQuestWorldObjects();
353 WorldPacket data;
355 if(method == 1)
357 data.Initialize( SMSG_GROUP_UNINVITE, 0 );
358 player->GetSession()->SendPacket( &data );
361 //we already removed player from group and in player->GetGroup() is his original group!
362 if( Group* group = player->GetGroup() )
364 group->SendUpdate();
366 else
368 data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8);
369 data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
370 data << uint64(0) << uint32(0) << uint32(0) << uint64(0);
371 player->GetSession()->SendPacket(&data);
374 _homebindIfInstance(player);
377 if(leaderChanged)
379 WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1));
380 data << m_memberSlots.front().name;
381 BroadcastPacket(&data, true);
384 SendUpdate();
386 // if group before remove <= 2 disband it
387 else
388 Disband(true);
390 return m_memberSlots.size();
393 void Group::ChangeLeader(const uint64 &guid)
395 member_citerator slot = _getMemberCSlot(guid);
397 if(slot == m_memberSlots.end())
398 return;
400 _setLeader(guid);
402 WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1);
403 data << slot->name;
404 BroadcastPacket(&data, true);
405 SendUpdate();
408 void Group::Disband(bool hideDestroy)
410 Player *player;
412 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
414 player = sObjectMgr.GetPlayer(citr->guid);
415 if(!player)
416 continue;
418 //we cannot call _removeMember because it would invalidate member iterator
419 //if we are removing player from battleground raid
420 if( isBGGroup() )
421 player->RemoveFromBattleGroundRaid();
422 else
424 //we can remove player who is in battleground from his original group
425 if( player->GetOriginalGroup() == this )
426 player->SetOriginalGroup(NULL);
427 else
428 player->SetGroup(NULL);
431 // quest related GO state dependent from raid membership
432 if(isRaidGroup())
433 player->UpdateForQuestWorldObjects();
435 if(!player->GetSession())
436 continue;
438 WorldPacket data;
439 if(!hideDestroy)
441 data.Initialize(SMSG_GROUP_DESTROYED, 0);
442 player->GetSession()->SendPacket(&data);
445 //we already removed player from group and in player->GetGroup() is his original group, send update
446 if( Group* group = player->GetGroup() )
448 group->SendUpdate();
450 else
452 data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8);
453 data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
454 data << uint64(0) << uint32(0) << uint32(0) << uint64(0);
455 player->GetSession()->SendPacket(&data);
458 _homebindIfInstance(player);
460 RollId.clear();
461 m_memberSlots.clear();
463 RemoveAllInvites();
465 if(!isBGGroup())
467 CharacterDatabase.BeginTransaction();
468 CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
469 CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
470 CharacterDatabase.CommitTransaction();
471 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, NULL);
472 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, NULL);
475 m_leaderGuid = 0;
476 m_leaderName = "";
479 /*********************************************************/
480 /*** LOOT SYSTEM ***/
481 /*********************************************************/
483 void Group::SendLootStartRoll(uint32 CountDown, const Roll &r)
485 WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4+4+1));
486 data << uint64(r.itemGUID); // guid of rolled item
487 data << uint32(r.totalPlayersRolling); // maybe the number of players rolling for it???
488 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
489 data << uint32(r.itemRandomSuffix); // randomSuffix
490 data << uint32(r.itemRandomPropId); // item random property ID
491 data << uint32(r.itemCount); // items in stack
492 data << uint32(CountDown); // the countdown time to choose "need" or "greed"
493 data << uint8(ALL_ROLL_TYPE_MASK); // roll type mask
495 for (Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
497 Player *p = sObjectMgr.GetPlayer(itr->first);
498 if(!p || !p->GetSession())
499 continue;
501 if(itr->second != NOT_VALID)
502 p->GetSession()->SendPacket( &data );
506 void Group::SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
508 WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1+1));
509 data << uint64(SourceGuid); // guid of the item rolled
510 data << uint32(0); // unknown, maybe amount of players
511 data << uint64(TargetGuid);
512 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
513 data << uint32(r.itemRandomSuffix); // randomSuffix
514 data << uint32(r.itemRandomPropId); // Item random property ID
515 data << uint8(RollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
516 data << uint8(RollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
517 data << uint8(0); // 2.4.0
519 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
521 Player *p = sObjectMgr.GetPlayer(itr->first);
522 if(!p || !p->GetSession())
523 continue;
525 if(itr->second != NOT_VALID)
526 p->GetSession()->SendPacket( &data );
530 void Group::SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
532 WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
533 data << uint64(SourceGuid); // guid of the item rolled
534 data << uint32(0); // unknown, maybe amount of players
535 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
536 data << uint32(r.itemRandomSuffix); // randomSuffix
537 data << uint32(r.itemRandomPropId); // Item random property
538 data << uint64(TargetGuid); // guid of the player who won.
539 data << uint8(RollNumber); // rollnumber realted to SMSG_LOOT_ROLL
540 data << uint8(RollType); // Rolltype related to SMSG_LOOT_ROLL
542 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
544 Player *p = sObjectMgr.GetPlayer(itr->first);
545 if(!p || !p->GetSession())
546 continue;
548 if(itr->second != NOT_VALID)
549 p->GetSession()->SendPacket( &data );
553 void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r)
555 WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
556 data << uint64(r.itemGUID); // Guid of the item rolled
557 data << uint32(NumberOfPlayers); // The number of players rolling for it???
558 data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for
559 data << uint32(r.itemRandomPropId); // Item random property ID
560 data << uint32(r.itemRandomSuffix); // Item random suffix ID
562 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
564 Player *p = sObjectMgr.GetPlayer(itr->first);
565 if(!p || !p->GetSession())
566 continue;
568 if(itr->second != NOT_VALID)
569 p->GetSession()->SendPacket( &data );
573 void Group::GroupLoot(const uint64& playerGUID, Loot *loot, Creature *creature)
575 std::vector<LootItem>::iterator i;
576 ItemPrototype const *item;
577 uint8 itemSlot = 0;
578 Player *player = sObjectMgr.GetPlayer(playerGUID);
579 Group *group = player->GetGroup();
581 for (i = loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
583 item = ObjectMgr::GetItemPrototype(i->itemid);
584 if (!item)
586 //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
587 continue;
590 //roll for over-threshold item if it's one-player loot
591 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
593 uint64 newitemGUID = MAKE_NEW_GUID(sObjectMgr.GenerateLowGuid(HIGHGUID_ITEM), 0, HIGHGUID_ITEM);
594 Roll* r = new Roll(newitemGUID, *i);
596 //a vector is filled with only near party members
597 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
599 Player *member = itr->getSource();
600 if(!member || !member->GetSession())
601 continue;
602 if ( i->AllowedForPlayer(member) )
604 if (member->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
606 r->playerVote[member->GetGUID()] = NOT_EMITED_YET;
607 ++r->totalPlayersRolling;
612 r->setLoot(loot);
613 r->itemSlot = itemSlot;
615 group->SendLootStartRoll(60000, *r);
617 loot->items[itemSlot].is_blocked = true;
618 creature->m_groupLootTimer = 60000;
619 creature->lootingGroupLeaderGUID = GetLeaderGUID();
621 RollId.push_back(r);
623 else
624 i->is_underthreshold = 1;
628 void Group::NeedBeforeGreed(const uint64& playerGUID, Loot *loot, Creature *creature)
630 ItemPrototype const *item;
631 Player *player = sObjectMgr.GetPlayer(playerGUID);
632 Group *group = player->GetGroup();
634 uint8 itemSlot = 0;
635 for(std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
637 item = ObjectMgr::GetItemPrototype(i->itemid);
639 //only roll for one-player items, not for ones everyone can get
640 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
642 uint64 newitemGUID = MAKE_NEW_GUID(sObjectMgr.GenerateLowGuid(HIGHGUID_ITEM), 0, HIGHGUID_ITEM);
643 Roll* r = new Roll(newitemGUID, *i);
645 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
647 Player *playerToRoll = itr->getSource();
648 if(!playerToRoll || !playerToRoll->GetSession())
649 continue;
651 if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll) )
653 if (playerToRoll->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
655 r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET;
656 ++r->totalPlayersRolling;
661 if (r->totalPlayersRolling > 0)
663 r->setLoot(loot);
664 r->itemSlot = itemSlot;
666 group->SendLootStartRoll(60000, *r);
668 loot->items[itemSlot].is_blocked = true;
670 RollId.push_back(r);
672 else
674 delete r;
677 else
678 i->is_underthreshold = 1;
682 void Group::MasterLoot(const uint64& playerGUID, Loot* /*loot*/, Creature *creature)
684 Player *player = sObjectMgr.GetPlayer(playerGUID);
685 if(!player)
686 return;
688 sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330) player = [%s].", player->GetName());
690 uint32 real_count = 0;
692 WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
693 data << (uint8)GetMembersCount();
695 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
697 Player *looter = itr->getSource();
698 if (!looter->IsInWorld())
699 continue;
701 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
703 data << looter->GetGUID();
704 ++real_count;
708 data.put<uint8>(0, real_count);
710 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
712 Player *looter = itr->getSource();
713 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
714 looter->GetSession()->SendPacket(&data);
718 void Group::CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choise)
720 Rolls::iterator rollI = GetRoll(Guid);
721 if (rollI == RollId.end())
722 return;
723 Roll* roll = *rollI;
725 Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID);
726 // this condition means that player joins to the party after roll begins
727 if (itr == roll->playerVote.end())
728 return;
730 if (roll->getLoot())
731 if (roll->getLoot()->items.empty())
732 return;
734 switch (Choise)
736 case ROLL_PASS: // Player choose pass
738 SendLootRoll(0, playerGUID, 0, ROLL_PASS, *roll);
739 ++roll->totalPass;
740 itr->second = PASS;
742 break;
743 case ROLL_NEED: // player choose Need
745 SendLootRoll(0, playerGUID, 0, ROLL_NEED, *roll);
746 ++roll->totalNeed;
747 itr->second = NEED;
749 break;
750 case ROLL_GREED: // player choose Greed
752 SendLootRoll(0, playerGUID, 128, ROLL_GREED, *roll);
753 ++roll->totalGreed;
754 itr->second = GREED;
756 break;
757 case ROLL_DISENCHANT: // player choose Disenchant
759 SendLootRoll(0, playerGUID, 128, ROLL_DISENCHANT, *roll);
760 ++roll->totalGreed;
761 itr->second = DISENCHANT;
763 break;
765 if (roll->totalPass + roll->totalNeed + roll->totalGreed >= roll->totalPlayersRolling)
767 CountTheRoll(rollI, NumberOfPlayers);
771 //called when roll timer expires
772 void Group::EndRoll()
774 Rolls::iterator itr;
775 while(!RollId.empty())
777 //need more testing here, if rolls disappear
778 itr = RollId.begin();
779 CountTheRoll(itr, GetMembersCount()); //i don't have to edit player votes, who didn't vote ... he will pass
783 void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers)
785 Roll* roll = *rollI;
786 if(!roll->isValid()) // is loot already deleted ?
788 RollId.erase(rollI);
789 delete roll;
790 return;
792 //end of the roll
793 if (roll->totalNeed > 0)
795 if(!roll->playerVote.empty())
797 uint8 maxresul = 0;
798 uint64 maxguid = (*roll->playerVote.begin()).first;
799 Player *player;
801 for( Roll::PlayerVote::const_iterator itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
803 if (itr->second != NEED)
804 continue;
806 uint8 randomN = urand(1, 99);
807 SendLootRoll(0, itr->first, randomN, ROLL_NEED, *roll);
808 if (maxresul < randomN)
810 maxguid = itr->first;
811 maxresul = randomN;
814 SendLootRollWon(0, maxguid, maxresul, ROLL_NEED, *roll);
815 player = sObjectMgr.GetPlayer(maxguid);
817 if(player && player->GetSession())
819 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul);
821 ItemPosCountVec dest;
822 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
823 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
824 if ( msg == EQUIP_ERR_OK )
826 item->is_looted = true;
827 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
828 --roll->getLoot()->unlootedCount;
829 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
831 else
833 item->is_blocked = false;
834 player->SendEquipError( msg, NULL, NULL );
839 else if (roll->totalGreed > 0)
841 if(!roll->playerVote.empty())
843 uint8 maxresul = 0;
844 uint64 maxguid = (*roll->playerVote.begin()).first;
845 Player *player;
846 RollVote rollvote;
848 Roll::PlayerVote::iterator itr;
849 for (itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
851 if (itr->second != GREED && itr->second != DISENCHANT)
852 continue;
854 uint8 randomN = urand(1, 99);
855 SendLootRoll(0, itr->first, randomN, itr->second, *roll);
856 if (maxresul < randomN)
858 maxguid = itr->first;
859 maxresul = randomN;
860 rollvote = itr->second;
863 SendLootRollWon(0, maxguid, maxresul, rollvote, *roll);
864 player = sObjectMgr.GetPlayer(maxguid);
866 if(player && player->GetSession())
868 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul);
870 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
872 if(rollvote == GREED)
874 ItemPosCountVec dest;
875 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
876 if ( msg == EQUIP_ERR_OK )
878 item->is_looted = true;
879 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
880 --roll->getLoot()->unlootedCount;
881 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
883 else
885 item->is_blocked = false;
886 player->SendEquipError( msg, NULL, NULL );
889 else if(rollvote == DISENCHANT)
891 item->is_looted = true;
892 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
893 --roll->getLoot()->unlootedCount;
895 ItemPrototype const *pProto = ObjectMgr::GetItemPrototype(roll->itemid);
896 player->AutoStoreLoot(pProto->DisenchantID, LootTemplates_Disenchant, true);
901 else
903 SendLootAllPassed(NumberOfPlayers, *roll);
904 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
905 if(item) item->is_blocked = false;
907 RollId.erase(rollI);
908 delete roll;
911 void Group::SetTargetIcon(uint8 id, uint64 guid)
913 if(id >= TARGETICONCOUNT)
914 return;
916 // clean other icons
917 if( guid != 0 )
918 for(int i = 0; i < TARGETICONCOUNT; ++i)
919 if( m_targetIcons[i] == guid )
920 SetTargetIcon(i, 0);
922 m_targetIcons[id] = guid;
924 WorldPacket data(MSG_RAID_TARGET_UPDATE, (2+8));
925 data << uint8(0);
926 data << uint8(id);
927 data << uint64(guid);
928 BroadcastPacket(&data, true);
931 void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level)
933 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
935 Player* member = itr->getSource();
936 if(!member || !member->isAlive()) // only for alive
937 continue;
939 if(!member->IsAtGroupRewardDistance(victim)) // at req. distance
940 continue;
942 ++count;
943 sum_level += member->getLevel();
944 if(!member_with_max_level || member_with_max_level->getLevel() < member->getLevel())
945 member_with_max_level = member;
947 uint32 gray_level = MaNGOS::XP::GetGrayLevel(member->getLevel());
948 if( victim->getLevel() > gray_level && (!not_gray_member_with_max_level
949 || not_gray_member_with_max_level->getLevel() < member->getLevel()))
950 not_gray_member_with_max_level = member;
954 void Group::SendTargetIconList(WorldSession *session)
956 if(!session)
957 return;
959 WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
960 data << (uint8)1;
962 for(int i = 0; i < TARGETICONCOUNT; ++i)
964 if(m_targetIcons[i] == 0)
965 continue;
967 data << uint8(i);
968 data << uint64(m_targetIcons[i]);
971 session->SendPacket(&data);
974 void Group::SendUpdate()
976 Player *player;
978 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
980 player = sObjectMgr.GetPlayer(citr->guid);
981 if(!player || !player->GetSession() || player->GetGroup() != this )
982 continue;
983 // guess size
984 WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+8+4+GetMembersCount()*20));
985 data << uint8(m_groupType); // group type (flags in 3.3)
986 data << uint8(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
987 data << uint8(citr->group); // groupid
988 data << uint8(citr->assistant ? 0x01 : 0x00); // 0x2 main assist, 0x4 main tank
989 if(m_groupType & GROUPTYPE_LFD)
991 data << uint8(0);
992 data << uint32(0);
994 data << uint64(0x50000000FFFFFFFELL); // related to voice chat?
995 data << uint32(0); // 3.3, this value increments every time SMSG_GROUP_LIST is sent
996 data << uint32(GetMembersCount()-1);
997 for(member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
999 if(citr->guid == citr2->guid)
1000 continue;
1001 Player* member = sObjectMgr.GetPlayer(citr2->guid);
1002 uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
1003 onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
1005 data << citr2->name;
1006 data << uint64(citr2->guid);
1007 // online-state
1008 data << uint8(onlineState);
1009 data << uint8(citr2->group); // groupid
1010 data << uint8(citr2->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
1011 data << uint8(0); // 3.3, role?
1014 data << uint64(m_leaderGuid); // leader guid
1015 if(GetMembersCount()-1)
1017 data << uint8(m_lootMethod); // loot method
1018 data << uint64(m_looterGuid); // looter guid
1019 data << uint8(m_lootThreshold); // loot threshold
1020 data << uint8(m_dungeonDifficulty); // Dungeon Difficulty
1021 data << uint8(m_raidDifficulty); // Raid Difficulty
1022 data << uint8(0); // 3.3, dynamic difficulty?
1024 player->GetSession()->SendPacket( &data );
1028 void Group::UpdatePlayerOutOfRange(Player* pPlayer)
1030 if(!pPlayer || !pPlayer->IsInWorld())
1031 return;
1033 Player *player;
1034 WorldPacket data;
1035 pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
1037 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1039 player = itr->getSource();
1040 if (player && player != pPlayer && !pPlayer->isVisibleFor(player,player->GetViewPoint()))
1041 player->GetSession()->SendPacket(&data);
1045 void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore)
1047 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1049 Player *pl = itr->getSource();
1050 if(!pl || (ignore != 0 && pl->GetGUID() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this) )
1051 continue;
1053 if (pl->GetSession() && (group == -1 || itr->getSubGroup() == group))
1054 pl->GetSession()->SendPacket(packet);
1058 void Group::BroadcastReadyCheck(WorldPacket *packet)
1060 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1062 Player *pl = itr->getSource();
1063 if(pl && pl->GetSession())
1064 if(IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
1065 pl->GetSession()->SendPacket(packet);
1069 void Group::OfflineReadyCheck()
1071 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
1073 Player *pl = sObjectMgr.GetPlayer(citr->guid);
1074 if (!pl || !pl->GetSession())
1076 WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
1077 data << citr->guid;
1078 data << (uint8)0;
1079 BroadcastReadyCheck(&data);
1084 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant)
1086 // get first not-full group
1087 uint8 groupid = 0;
1088 if (m_subGroupsCounts)
1090 bool groupFound = false;
1091 for (; groupid < MAXRAIDSIZE / MAXGROUPSIZE; ++groupid)
1093 if (m_subGroupsCounts[groupid] < MAXGROUPSIZE)
1095 groupFound = true;
1096 break;
1099 // We are raid group and no one slot is free
1100 if (!groupFound)
1101 return false;
1104 return _addMember(guid, name, isAssistant, groupid);
1107 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group)
1109 if(IsFull())
1110 return false;
1112 if(!guid)
1113 return false;
1115 Player *player = sObjectMgr.GetPlayer(guid);
1117 MemberSlot member;
1118 member.guid = guid;
1119 member.name = name;
1120 member.group = group;
1121 member.assistant = isAssistant;
1122 m_memberSlots.push_back(member);
1124 SubGroupCounterIncrease(group);
1126 if(player)
1128 player->SetGroupInvite(NULL);
1129 //if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid()
1130 if( player->GetGroup() && isBGGroup() )
1131 player->SetBattleGroundRaid(this, group);
1132 //if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup()
1133 else if ( player->GetGroup() )
1134 player->SetOriginalGroup(this, group);
1135 //if player is not in group, then call set group
1136 else
1137 player->SetGroup(this, group);
1138 // if the same group invites the player back, cancel the homebind timer
1139 InstanceGroupBind *bind = GetBoundInstance(player);
1140 if(bind && bind->save->GetInstanceId() == player->GetInstanceId())
1141 player->m_InstanceValid = true;
1144 if(!isRaidGroup()) // reset targetIcons for non-raid-groups
1146 for(int i = 0; i < TARGETICONCOUNT; ++i)
1147 m_targetIcons[i] = 0;
1150 if(!isBGGroup())
1152 // insert into group table
1153 CharacterDatabase.PExecute("INSERT INTO group_member(leaderGuid,memberGuid,assistant,subgroup) VALUES('%u','%u','%u','%u')", GUID_LOPART(m_leaderGuid), GUID_LOPART(member.guid), ((member.assistant==1)?1:0), member.group);
1156 return true;
1159 bool Group::_removeMember(const uint64 &guid)
1161 Player *player = sObjectMgr.GetPlayer(guid);
1162 if (player)
1164 //if we are removing player from battleground raid
1165 if( isBGGroup() )
1166 player->RemoveFromBattleGroundRaid();
1167 else
1169 //we can remove player who is in battleground from his original group
1170 if( player->GetOriginalGroup() == this )
1171 player->SetOriginalGroup(NULL);
1172 else
1173 player->SetGroup(NULL);
1177 _removeRolls(guid);
1179 member_witerator slot = _getMemberWSlot(guid);
1180 if (slot != m_memberSlots.end())
1182 SubGroupCounterDecrease(slot->group);
1184 m_memberSlots.erase(slot);
1187 if(!isBGGroup())
1188 CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", GUID_LOPART(guid));
1190 if(m_leaderGuid == guid) // leader was removed
1192 if(GetMembersCount() > 0)
1193 _setLeader(m_memberSlots.front().guid);
1194 return true;
1197 return false;
1200 void Group::_setLeader(const uint64 &guid)
1202 member_citerator slot = _getMemberCSlot(guid);
1203 if(slot==m_memberSlots.end())
1204 return;
1206 if(!isBGGroup())
1208 // TODO: set a time limit to have this function run rarely cause it can be slow
1209 CharacterDatabase.BeginTransaction();
1211 // update the group's bound instances when changing leaders
1213 // remove all permanent binds from the group
1214 // in the DB also remove solo binds that will be replaced with permbinds
1215 // from the new leader
1216 CharacterDatabase.PExecute(
1217 "DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
1218 "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
1219 ")", GUID_LOPART(m_leaderGuid), GUID_LOPART(slot->guid)
1222 Player *player = sObjectMgr.GetPlayer(slot->guid);
1223 if(player)
1225 for(uint8 i = 0; i < MAX_DIFFICULTY; ++i)
1227 for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
1229 if(itr->second.perm)
1231 itr->second.save->RemoveGroup(this);
1232 m_boundInstances[i].erase(itr++);
1234 else
1235 ++itr;
1240 // update the group's solo binds to the new leader
1241 CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1243 // copy the permanent binds from the new leader to the group
1244 // overwriting the solo binds with permanent ones if necessary
1245 // in the DB those have been deleted already
1246 Player::ConvertInstancesToGroup(player, this, slot->guid);
1248 // update the group leader
1249 CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1250 CharacterDatabase.PExecute("UPDATE group_member SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1251 CharacterDatabase.CommitTransaction();
1254 m_leaderGuid = slot->guid;
1255 m_leaderName = slot->name;
1258 void Group::_removeRolls(const uint64 &guid)
1260 for (Rolls::iterator it = RollId.begin(); it < RollId.end(); ++it)
1262 Roll* roll = *it;
1263 Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
1264 if(itr2 == roll->playerVote.end())
1265 continue;
1267 if (itr2->second == GREED || itr2->second == DISENCHANT)
1268 --roll->totalGreed;
1269 if (itr2->second == NEED)
1270 --roll->totalNeed;
1271 if (itr2->second == PASS)
1272 --roll->totalPass;
1273 if (itr2->second != NOT_VALID)
1274 --roll->totalPlayersRolling;
1276 roll->playerVote.erase(itr2);
1278 CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, MAX_ROLL_TYPE);
1282 bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group)
1284 member_witerator slot = _getMemberWSlot(guid);
1285 if(slot == m_memberSlots.end())
1286 return false;
1288 slot->group = group;
1290 SubGroupCounterIncrease(group);
1292 if(!isBGGroup())
1293 CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid));
1295 return true;
1298 bool Group::_setAssistantFlag(const uint64 &guid, const bool &state)
1300 member_witerator slot = _getMemberWSlot(guid);
1301 if(slot == m_memberSlots.end())
1302 return false;
1304 slot->assistant = state;
1305 if(!isBGGroup())
1306 CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state==true)?1:0, GUID_LOPART(guid));
1307 return true;
1310 bool Group::_setMainTank(const uint64 &guid)
1312 member_citerator slot = _getMemberCSlot(guid);
1313 if(slot == m_memberSlots.end())
1314 return false;
1316 if(m_mainAssistant == guid)
1317 _setMainAssistant(0);
1318 m_mainTank = guid;
1319 if(!isBGGroup())
1320 CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainTank), GUID_LOPART(m_leaderGuid));
1321 return true;
1324 bool Group::_setMainAssistant(const uint64 &guid)
1326 member_witerator slot = _getMemberWSlot(guid);
1327 if(slot == m_memberSlots.end())
1328 return false;
1330 if(m_mainTank == guid)
1331 _setMainTank(0);
1332 m_mainAssistant = guid;
1333 if(!isBGGroup())
1334 CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainAssistant), GUID_LOPART(m_leaderGuid));
1335 return true;
1338 bool Group::SameSubGroup(Player const* member1, Player const* member2) const
1340 if(!member1 || !member2)
1341 return false;
1342 if (member1->GetGroup() != this || member2->GetGroup() != this)
1343 return false;
1344 else
1345 return member1->GetSubGroup() == member2->GetSubGroup();
1348 // allows setting subgroup for offline members
1349 void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group)
1351 if(!isRaidGroup())
1352 return;
1353 Player *player = sObjectMgr.GetPlayer(guid);
1355 if (!player)
1357 uint8 prevSubGroup;
1358 prevSubGroup = GetMemberGroup(guid);
1360 SubGroupCounterDecrease(prevSubGroup);
1362 if(_setMembersGroup(guid, group))
1363 SendUpdate();
1365 else
1366 // This methods handles itself groupcounter decrease
1367 ChangeMembersGroup(player, group);
1370 // only for online members
1371 void Group::ChangeMembersGroup(Player *player, const uint8 &group)
1373 if(!player || !isRaidGroup())
1374 return;
1375 if(_setMembersGroup(player->GetGUID(), group))
1377 uint8 prevSubGroup = player->GetSubGroup();
1378 if( player->GetGroup() == this )
1379 player->GetGroupRef().setSubGroup(group);
1380 //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
1381 else
1383 prevSubGroup = player->GetOriginalSubGroup();
1384 player->GetOriginalGroupRef().setSubGroup(group);
1386 SubGroupCounterDecrease(prevSubGroup);
1388 SendUpdate();
1392 void Group::UpdateLooterGuid( Creature* creature, bool ifneed )
1394 switch (GetLootMethod())
1396 case MASTER_LOOT:
1397 case FREE_FOR_ALL:
1398 return;
1399 default:
1400 // round robin style looting applies for all low
1401 // quality items in each loot method except free for all and master loot
1402 break;
1405 member_citerator guid_itr = _getMemberCSlot(GetLooterGuid());
1406 if(guid_itr != m_memberSlots.end())
1408 if(ifneed)
1410 // not update if only update if need and ok
1411 Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
1412 if(looter && looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1413 return;
1415 ++guid_itr;
1418 // search next after current
1419 if(guid_itr != m_memberSlots.end())
1421 for(member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
1423 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1425 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1427 bool refresh = pl->GetLootGUID() == creature->GetGUID();
1429 //if(refresh) // update loot for new looter
1430 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1431 SetLooterGuid(pl->GetGUID());
1432 SendUpdate();
1433 if(refresh) // update loot for new looter
1434 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1435 return;
1441 // search from start
1442 for(member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
1444 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1446 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1448 bool refresh = pl->GetLootGUID()==creature->GetGUID();
1450 //if(refresh) // update loot for new looter
1451 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1452 SetLooterGuid(pl->GetGUID());
1453 SendUpdate();
1454 if(refresh) // update loot for new looter
1455 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1456 return;
1461 SetLooterGuid(0);
1462 SendUpdate();
1465 uint32 Group::CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot)
1467 // check for min / max count
1468 uint32 memberscount = GetMembersCount();
1469 if(memberscount < MinPlayerCount)
1470 return BG_JOIN_ERR_GROUP_NOT_ENOUGH;
1471 if(memberscount > MaxPlayerCount)
1472 return BG_JOIN_ERR_GROUP_TOO_MANY;
1474 // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
1475 Player * reference = GetFirstMember()->getSource();
1476 // no reference found, can't join this way
1477 if(!reference)
1478 return BG_JOIN_ERR_OFFLINE_MEMBER;
1480 PvPDifficultyEntry const* bracketEntry = GetBattlegroundBracketByLevel(bgOrTemplate->GetMapId(),reference->getLevel());
1481 if(!bracketEntry)
1482 return BG_JOIN_ERR_OFFLINE_MEMBER;
1484 uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
1485 uint32 team = reference->GetTeam();
1487 // check every member of the group to be able to join
1488 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1490 Player *member = itr->getSource();
1491 // offline member? don't let join
1492 if(!member)
1493 return BG_JOIN_ERR_OFFLINE_MEMBER;
1494 // don't allow cross-faction join as group
1495 if(member->GetTeam() != team)
1496 return BG_JOIN_ERR_MIXED_FACTION;
1497 // not in the same battleground level braket, don't let join
1498 PvPDifficultyEntry const* memberBracketEntry = GetBattlegroundBracketByLevel(bracketEntry->mapId,member->getLevel());
1499 if(memberBracketEntry != bracketEntry)
1500 return BG_JOIN_ERR_MIXED_LEVELS;
1501 // don't let join rated matches if the arena team id doesn't match
1502 if(isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
1503 return BG_JOIN_ERR_MIXED_ARENATEAM;
1504 // don't let join if someone from the group is already in that bg queue
1505 if(member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId))
1506 return BG_JOIN_ERR_GROUP_MEMBER_ALREADY_IN_QUEUE;
1507 // check for deserter debuff in case not arena queue
1508 if(bgOrTemplate->GetTypeID() != BATTLEGROUND_AA && !member->CanJoinToBattleground())
1509 return BG_JOIN_ERR_GROUP_DESERTER;
1510 // check if member can join any more battleground queues
1511 if(!member->HasFreeBattleGroundQueueId())
1512 return BG_JOIN_ERR_ALL_QUEUES_USED;
1514 return BG_JOIN_ERR_OK;
1517 //===================================================
1518 //============== Roll ===============================
1519 //===================================================
1521 void Roll::targetObjectBuildLink()
1523 // called from link()
1524 getTarget()->addLootValidatorRef(this);
1527 void Group::SetDungeonDifficulty(Difficulty difficulty)
1529 m_dungeonDifficulty = difficulty;
1530 if(!isBGGroup())
1531 CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE leaderGuid ='%u'", m_dungeonDifficulty, GUID_LOPART(m_leaderGuid));
1533 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1535 Player *player = itr->getSource();
1536 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1537 continue;
1538 player->SetDungeonDifficulty(difficulty);
1539 player->SendDungeonDifficulty(true);
1543 void Group::SetRaidDifficulty(Difficulty difficulty)
1545 m_raidDifficulty = difficulty;
1546 if(!isBGGroup())
1547 CharacterDatabase.PExecute("UPDATE groups SET raiddifficulty = %u WHERE leaderGuid ='%u'", m_raidDifficulty, GUID_LOPART(m_leaderGuid));
1549 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1551 Player *player = itr->getSource();
1552 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1553 continue;
1554 player->SetRaidDifficulty(difficulty);
1555 player->SendRaidDifficulty(true);
1559 bool Group::InCombatToInstance(uint32 instanceId)
1561 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1563 Player *pPlayer = itr->getSource();
1564 if(pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId)
1565 return true;
1567 return false;
1570 void Group::ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo)
1572 if(isBGGroup())
1573 return;
1575 // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
1577 // we assume that when the difficulty changes, all instances that can be reset will be
1578 Difficulty diff = GetDifficulty(isRaid);
1580 for(BoundInstancesMap::iterator itr = m_boundInstances[diff].begin(); itr != m_boundInstances[diff].end();)
1582 InstanceSave *p = itr->second.save;
1583 const MapEntry *entry = sMapStore.LookupEntry(itr->first);
1584 if (!entry || entry->IsRaid() != isRaid || (!p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND))
1586 ++itr;
1587 continue;
1590 if(method == INSTANCE_RESET_ALL)
1592 // the "reset all instances" method can only reset normal maps
1593 if (entry->map_type == MAP_RAID || diff == DUNGEON_DIFFICULTY_HEROIC)
1595 ++itr;
1596 continue;
1600 bool isEmpty = true;
1601 // if the map is loaded, reset it
1602 Map *map = sMapMgr.FindMap(p->GetMapId(), p->GetInstanceId());
1603 if(map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset()))
1604 isEmpty = ((InstanceMap*)map)->Reset(method);
1606 if(SendMsgTo)
1608 if(isEmpty)
1609 SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
1610 else
1611 SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
1614 if(isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
1616 // do not reset the instance, just unbind if others are permanently bound to it
1617 if(p->CanReset())
1618 p->DeleteFromDB();
1619 else
1620 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
1621 // i don't know for sure if hash_map iterators
1622 m_boundInstances[diff].erase(itr);
1623 itr = m_boundInstances[diff].begin();
1624 // this unloads the instance save unless online players are bound to it
1625 // (eg. permanent binds or GM solo binds)
1626 p->RemoveGroup(this);
1628 else
1629 ++itr;
1633 InstanceGroupBind* Group::GetBoundInstance(Player* player)
1635 uint32 mapid = player->GetMapId();
1636 MapEntry const* mapEntry = sMapStore.LookupEntry(mapid);
1637 if(!mapEntry)
1638 return NULL;
1640 Difficulty difficulty = player->GetDifficulty(mapEntry->IsRaid());
1642 // some instances only have one difficulty
1643 MapDifficulty const* mapDiff = GetMapDifficultyData(mapid,difficulty);
1644 if(!mapDiff)
1645 difficulty = DUNGEON_DIFFICULTY_NORMAL;
1647 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1648 if(itr != m_boundInstances[difficulty].end())
1649 return &itr->second;
1650 else
1651 return NULL;
1654 InstanceGroupBind* Group::GetBoundInstance(Map* aMap)
1656 // Currently spawn numbering not different from map difficulty
1657 Difficulty difficulty = GetDifficulty(aMap->IsRaid());
1659 // some instances only have one difficulty
1660 MapDifficulty const* mapDiff = GetMapDifficultyData(aMap->GetId(),difficulty);
1661 if(!mapDiff)
1662 return NULL;
1664 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(aMap->GetId());
1665 if(itr != m_boundInstances[difficulty].end())
1666 return &itr->second;
1667 else
1668 return NULL;
1671 InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
1673 if(save && !isBGGroup())
1675 InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
1676 if(bind.save)
1678 // when a boss is killed or when copying the players's binds to the group
1679 if(permanent != bind.perm || save != bind.save)
1680 if(!load)
1681 CharacterDatabase.PExecute("UPDATE group_instance SET instance = '%u', permanent = '%u' WHERE leaderGuid = '%u' AND instance = '%u'", save->GetInstanceId(), permanent, GUID_LOPART(GetLeaderGUID()), bind.save->GetInstanceId());
1683 else if(!load)
1684 CharacterDatabase.PExecute("INSERT INTO group_instance (leaderGuid, instance, permanent) VALUES ('%u', '%u', '%u')", GUID_LOPART(GetLeaderGUID()), save->GetInstanceId(), permanent);
1686 if(bind.save != save)
1688 if(bind.save)
1689 bind.save->RemoveGroup(this);
1690 save->AddGroup(this);
1693 bind.save = save;
1694 bind.perm = permanent;
1695 if(!load)
1696 sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetLeaderGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
1697 return &bind;
1699 else
1700 return NULL;
1703 void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
1705 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1706 if(itr != m_boundInstances[difficulty].end())
1708 if(!unload)
1709 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE leaderGuid = '%u' AND instance = '%u'", GUID_LOPART(GetLeaderGUID()), itr->second.save->GetInstanceId());
1710 itr->second.save->RemoveGroup(this); // save can become invalid
1711 m_boundInstances[difficulty].erase(itr);
1715 void Group::_homebindIfInstance(Player *player)
1717 if(player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
1719 // leaving the group in an instance, the homebind timer is started
1720 // unless the player is permanently saved to the instance
1721 InstanceSave *save = sInstanceSaveMgr.GetInstanceSave(player->GetInstanceId());
1722 InstancePlayerBind *playerBind = save ? player->GetBoundInstance(save->GetMapId(), save->GetDifficulty()) : NULL;
1723 if(!playerBind || !playerBind->perm)
1724 player->m_InstanceValid = false;