[8483] Implement glyph 43361.
[getmangos.git] / src / game / Group.cpp
blob5c83df614c7d7a85c62a3036e17406b64188f293
1 /*
2 * Copyright (C) 2005-2009 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 "Group.h"
27 #include "Formulas.h"
28 #include "ObjectAccessor.h"
29 #include "BattleGround.h"
30 #include "MapManager.h"
31 #include "InstanceSaveMgr.h"
32 #include "MapInstanced.h"
33 #include "Util.h"
35 Group::Group()
37 m_leaderGuid = 0;
38 m_mainTank = 0;
39 m_mainAssistant = 0;
40 m_groupType = (GroupType)0;
41 m_bgGroup = NULL;
42 m_lootMethod = (LootMethod)0;
43 m_looterGuid = 0;
44 m_lootThreshold = ITEM_QUALITY_UNCOMMON;
45 m_subGroupsCounts = NULL;
47 for (int i=0; i<TARGETICONCOUNT; ++i)
48 m_targetIcons[i] = 0;
51 Group::~Group()
53 if(m_bgGroup)
55 sLog.outDebug("Group::~Group: battleground group being deleted.");
56 if(m_bgGroup->GetBgRaid(ALLIANCE) == this) m_bgGroup->SetBgRaid(ALLIANCE, NULL);
57 else if(m_bgGroup->GetBgRaid(HORDE) == this) m_bgGroup->SetBgRaid(HORDE, NULL);
58 else sLog.outError("Group::~Group: battleground group is not linked to the correct battleground.");
60 Rolls::iterator itr;
61 while(!RollId.empty())
63 itr = RollId.begin();
64 Roll *r = *itr;
65 RollId.erase(itr);
66 delete(r);
69 // it is undefined whether objectmgr (which stores the groups) or instancesavemgr
70 // will be unloaded first so we must be prepared for both cases
71 // this may unload some instance saves
72 for(uint8 i = 0; i < TOTAL_DIFFICULTIES; ++i)
73 for(BoundInstancesMap::iterator itr2 = m_boundInstances[i].begin(); itr2 != m_boundInstances[i].end(); ++itr2)
74 itr2->second.save->RemoveGroup(this);
76 // Sub group counters clean up
77 if (m_subGroupsCounts)
78 delete[] m_subGroupsCounts;
81 bool Group::Create(const uint64 &guid, const char * name)
83 m_leaderGuid = guid;
84 m_leaderName = name;
86 m_groupType = isBGGroup() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
88 if (m_groupType == GROUPTYPE_RAID)
89 _initRaidSubGroupsCounter();
91 m_lootMethod = GROUP_LOOT;
92 m_lootThreshold = ITEM_QUALITY_UNCOMMON;
93 m_looterGuid = guid;
95 m_difficulty = DIFFICULTY_NORMAL;
96 if(!isBGGroup())
98 Player *leader = objmgr.GetPlayer(guid);
99 if(leader) m_difficulty = leader->GetDifficulty();
101 Player::ConvertInstancesToGroup(leader, this, guid);
103 // store group in database
104 CharacterDatabase.BeginTransaction();
105 CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
106 CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
107 CharacterDatabase.PExecute("INSERT INTO groups(leaderGuid,mainTank,mainAssistant,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,isRaid,difficulty) "
108 "VALUES('%u','%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u')",
109 GUID_LOPART(m_leaderGuid), GUID_LOPART(m_mainTank), GUID_LOPART(m_mainAssistant), uint32(m_lootMethod),
110 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(), m_difficulty);
113 if(!AddMember(guid, name))
114 return false;
116 if(!isBGGroup()) CharacterDatabase.CommitTransaction();
118 return true;
121 bool Group::LoadGroupFromDB(const uint64 &leaderGuid, QueryResult *result, bool loadMembers)
123 if(isBGGroup())
124 return false;
126 bool external = true;
127 if(!result)
129 external = false;
130 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
131 result = CharacterDatabase.PQuery("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, isRaid, difficulty FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
132 if(!result)
133 return false;
136 m_leaderGuid = leaderGuid;
138 // group leader not exist
139 if(!objmgr.GetPlayerNameByGUID(m_leaderGuid, m_leaderName))
141 if(!external) delete result;
142 return false;
145 m_groupType = (*result)[13].GetBool() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
147 if (m_groupType == GROUPTYPE_RAID)
148 _initRaidSubGroupsCounter();
150 m_difficulty = (*result)[14].GetUInt8();
151 m_mainTank = (*result)[0].GetUInt64();
152 m_mainAssistant = (*result)[1].GetUInt64();
153 m_lootMethod = (LootMethod)(*result)[2].GetUInt8();
154 m_looterGuid = MAKE_NEW_GUID((*result)[3].GetUInt32(), 0, HIGHGUID_PLAYER);
155 m_lootThreshold = (ItemQualities)(*result)[4].GetUInt16();
157 for(int i=0; i<TARGETICONCOUNT; ++i)
158 m_targetIcons[i] = (*result)[5+i].GetUInt64();
159 if(!external) delete result;
161 if(loadMembers)
163 result = CharacterDatabase.PQuery("SELECT memberGuid, assistant, subgroup FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
164 if(!result)
165 return false;
169 LoadMemberFromDB((*result)[0].GetUInt32(), (*result)[2].GetUInt8(), (*result)[1].GetBool());
170 } while( result->NextRow() );
171 delete result;
172 // group too small
173 if(GetMembersCount() < 2)
174 return false;
177 return true;
180 bool Group::LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant)
182 MemberSlot member;
183 member.guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
185 // skip non-existed member
186 if(!objmgr.GetPlayerNameByGUID(member.guid, member.name))
187 return false;
189 member.group = subgroup;
190 member.assistant = assistant;
191 m_memberSlots.push_back(member);
193 SubGroupCounterIncrease(subgroup);
195 return true;
198 void Group::ConvertToRaid()
200 m_groupType = GROUPTYPE_RAID;
202 _initRaidSubGroupsCounter();
204 if(!isBGGroup())
205 CharacterDatabase.PExecute("UPDATE groups SET isRaid = 1 WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
206 SendUpdate();
208 // update quest related GO states (quest activity dependent from raid membership)
209 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
210 if(Player* player = objmgr.GetPlayer(citr->guid))
211 player->UpdateForQuestWorldObjects();
214 bool Group::AddInvite(Player *player)
216 if( !player || player->GetGroupInvite() )
217 return false;
218 Group* group = player->GetGroup();
219 if( group && group->isBGGroup() )
220 group = player->GetOriginalGroup();
221 if( group )
222 return false;
224 RemoveInvite(player);
226 m_invitees.insert(player);
228 player->SetGroupInvite(this);
230 return true;
233 bool Group::AddLeaderInvite(Player *player)
235 if(!AddInvite(player))
236 return false;
238 m_leaderGuid = player->GetGUID();
239 m_leaderName = player->GetName();
240 return true;
243 uint32 Group::RemoveInvite(Player *player)
245 m_invitees.erase(player);
247 player->SetGroupInvite(NULL);
248 return GetMembersCount();
251 void Group::RemoveAllInvites()
253 for(InvitesList::iterator itr=m_invitees.begin(); itr!=m_invitees.end(); ++itr)
254 (*itr)->SetGroupInvite(NULL);
256 m_invitees.clear();
259 Player* Group::GetInvited(const uint64& guid) const
261 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
263 if((*itr)->GetGUID() == guid)
264 return (*itr);
266 return NULL;
269 Player* Group::GetInvited(const std::string& name) const
271 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
273 if((*itr)->GetName() == name)
274 return (*itr);
276 return NULL;
279 bool Group::AddMember(const uint64 &guid, const char* name)
281 if(!_addMember(guid, name))
282 return false;
283 SendUpdate();
285 Player *player = objmgr.GetPlayer(guid);
286 if(player)
288 if(!IsLeader(player->GetGUID()) && !isBGGroup())
290 // reset the new member's instances, unless he is currently in one of them
291 // including raid/heroic instances that they are not permanently bound to!
292 player->ResetInstances(INSTANCE_RESET_GROUP_JOIN);
294 if(player->getLevel() >= LEVELREQUIREMENT_HEROIC && player->GetDifficulty() != GetDifficulty() )
296 player->SetDifficulty(m_difficulty);
297 player->SendDungeonDifficulty(true);
300 player->SetGroupUpdateFlag(GROUP_UPDATE_FULL);
301 UpdatePlayerOutOfRange(player);
303 // quest related GO state dependent from raid memebership
304 if(isRaidGroup())
305 player->UpdateForQuestWorldObjects();
308 return true;
311 uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method)
313 // remove member and change leader (if need) only if strong more 2 members _before_ member remove
314 if(GetMembersCount() > (isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group
316 bool leaderChanged = _removeMember(guid);
318 if(Player *player = objmgr.GetPlayer( guid ))
320 // quest related GO state dependent from raid membership
321 if(isRaidGroup())
322 player->UpdateForQuestWorldObjects();
324 WorldPacket data;
326 if(method == 1)
328 data.Initialize( SMSG_GROUP_UNINVITE, 0 );
329 player->GetSession()->SendPacket( &data );
332 //we already removed player from group and in player->GetGroup() is his original group!
333 if( Group* group = player->GetGroup() )
335 group->SendUpdate();
337 else
339 data.Initialize(SMSG_GROUP_LIST, 24);
340 data << uint64(0) << uint64(0) << uint64(0);
341 player->GetSession()->SendPacket(&data);
344 _homebindIfInstance(player);
347 if(leaderChanged)
349 WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1));
350 data << m_memberSlots.front().name;
351 BroadcastPacket(&data, true);
354 SendUpdate();
356 // if group before remove <= 2 disband it
357 else
358 Disband(true);
360 return m_memberSlots.size();
363 void Group::ChangeLeader(const uint64 &guid)
365 member_citerator slot = _getMemberCSlot(guid);
367 if(slot==m_memberSlots.end())
368 return;
370 _setLeader(guid);
372 WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1);
373 data << slot->name;
374 BroadcastPacket(&data, true);
375 SendUpdate();
378 void Group::Disband(bool hideDestroy)
380 Player *player;
382 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
384 player = objmgr.GetPlayer(citr->guid);
385 if(!player)
386 continue;
388 //we cannot call _removeMember because it would invalidate member iterator
389 //if we are removing player from battleground raid
390 if( isBGGroup() )
391 player->RemoveFromBattleGroundRaid();
392 else
394 //we can remove player who is in battleground from his original group
395 if( player->GetOriginalGroup() == this )
396 player->SetOriginalGroup(NULL);
397 else
398 player->SetGroup(NULL);
401 // quest related GO state dependent from raid membership
402 if(isRaidGroup())
403 player->UpdateForQuestWorldObjects();
405 if(!player->GetSession())
406 continue;
408 WorldPacket data;
409 if(!hideDestroy)
411 data.Initialize(SMSG_GROUP_DESTROYED, 0);
412 player->GetSession()->SendPacket(&data);
415 //we already removed player from group and in player->GetGroup() is his original group, send update
416 if( Group* group = player->GetGroup() )
418 group->SendUpdate();
420 else
422 data.Initialize(SMSG_GROUP_LIST, 24);
423 data << uint64(0) << uint64(0) << uint64(0);
424 player->GetSession()->SendPacket(&data);
427 _homebindIfInstance(player);
429 RollId.clear();
430 m_memberSlots.clear();
432 RemoveAllInvites();
434 if(!isBGGroup())
436 CharacterDatabase.BeginTransaction();
437 CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
438 CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
439 CharacterDatabase.CommitTransaction();
440 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, NULL);
443 m_leaderGuid = 0;
444 m_leaderName = "";
447 /*********************************************************/
448 /*** LOOT SYSTEM ***/
449 /*********************************************************/
451 void Group::SendLootStartRoll(uint32 CountDown, const Roll &r)
453 WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4));
454 data << uint64(r.itemGUID); // guid of rolled item
455 data << uint32(r.totalPlayersRolling); // maybe the number of players rolling for it???
456 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
457 data << uint32(r.itemRandomSuffix); // randomSuffix
458 data << uint32(r.itemRandomPropId); // item random property ID
459 data << uint32(CountDown); // the countdown time to choose "need" or "greed"
461 for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
463 Player *p = objmgr.GetPlayer(itr->first);
464 if(!p || !p->GetSession())
465 continue;
467 if(itr->second != NOT_VALID)
468 p->GetSession()->SendPacket( &data );
472 void Group::SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
474 WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1));
475 data << uint64(SourceGuid); // guid of the item rolled
476 data << uint32(0); // unknown, maybe amount of players
477 data << uint64(TargetGuid);
478 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
479 data << uint32(r.itemRandomSuffix); // randomSuffix
480 data << uint32(r.itemRandomPropId); // Item random property ID
481 data << uint8(RollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
482 data << uint8(RollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
483 data << uint8(0); // 2.4.0
485 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
487 Player *p = objmgr.GetPlayer(itr->first);
488 if(!p || !p->GetSession())
489 continue;
491 if(itr->second != NOT_VALID)
492 p->GetSession()->SendPacket( &data );
496 void Group::SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
498 WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
499 data << uint64(SourceGuid); // guid of the item rolled
500 data << uint32(0); // unknown, maybe amount of players
501 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
502 data << uint32(r.itemRandomSuffix); // randomSuffix
503 data << uint32(r.itemRandomPropId); // Item random property
504 data << uint64(TargetGuid); // guid of the player who won.
505 data << uint8(RollNumber); // rollnumber realted to SMSG_LOOT_ROLL
506 data << uint8(RollType); // Rolltype related to SMSG_LOOT_ROLL
508 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
510 Player *p = objmgr.GetPlayer(itr->first);
511 if(!p || !p->GetSession())
512 continue;
514 if(itr->second != NOT_VALID)
515 p->GetSession()->SendPacket( &data );
519 void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r)
521 WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
522 data << uint64(r.itemGUID); // Guid of the item rolled
523 data << uint32(NumberOfPlayers); // The number of players rolling for it???
524 data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for
525 data << uint32(r.itemRandomPropId); // Item random property ID
526 data << uint32(r.itemRandomSuffix); // Item random suffix ID
528 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
530 Player *p = objmgr.GetPlayer(itr->first);
531 if(!p || !p->GetSession())
532 continue;
534 if(itr->second != NOT_VALID)
535 p->GetSession()->SendPacket( &data );
539 void Group::GroupLoot(const uint64& playerGUID, Loot *loot, Creature *creature)
541 std::vector<LootItem>::iterator i;
542 ItemPrototype const *item;
543 uint8 itemSlot = 0;
544 Player *player = objmgr.GetPlayer(playerGUID);
545 Group *group = player->GetGroup();
547 for (i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
549 item = objmgr.GetItemPrototype(i->itemid);
550 if (!item)
552 //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
553 continue;
556 //roll for over-threshold item if it's one-player loot
557 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
559 uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
560 Roll* r=new Roll(newitemGUID,*i);
562 //a vector is filled with only near party members
563 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
565 Player *member = itr->getSource();
566 if(!member || !member->GetSession())
567 continue;
568 if ( i->AllowedForPlayer(member) )
570 if (member->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
572 r->playerVote[member->GetGUID()] = NOT_EMITED_YET;
573 ++r->totalPlayersRolling;
578 r->setLoot(loot);
579 r->itemSlot = itemSlot;
581 group->SendLootStartRoll(60000, *r);
583 loot->items[itemSlot].is_blocked = true;
584 creature->m_groupLootTimer = 60000;
585 creature->lootingGroupLeaderGUID = GetLeaderGUID();
587 RollId.push_back(r);
589 else
590 i->is_underthreshold=1;
595 void Group::NeedBeforeGreed(const uint64& playerGUID, Loot *loot, Creature *creature)
597 ItemPrototype const *item;
598 Player *player = objmgr.GetPlayer(playerGUID);
599 Group *group = player->GetGroup();
601 uint8 itemSlot = 0;
602 for(std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
604 item = objmgr.GetItemPrototype(i->itemid);
606 //only roll for one-player items, not for ones everyone can get
607 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
609 uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
610 Roll* r=new Roll(newitemGUID,*i);
612 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
614 Player *playerToRoll = itr->getSource();
615 if(!playerToRoll || !playerToRoll->GetSession())
616 continue;
618 if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll) )
620 if (playerToRoll->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
622 r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET;
623 ++r->totalPlayersRolling;
628 if (r->totalPlayersRolling > 0)
630 r->setLoot(loot);
631 r->itemSlot = itemSlot;
633 group->SendLootStartRoll(60000, *r);
635 loot->items[itemSlot].is_blocked = true;
637 RollId.push_back(r);
639 else
641 delete r;
644 else
645 i->is_underthreshold=1;
649 void Group::MasterLoot(const uint64& playerGUID, Loot* /*loot*/, Creature *creature)
651 Player *player = objmgr.GetPlayer(playerGUID);
652 if(!player)
653 return;
655 sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330) player = [%s].", player->GetName());
657 uint32 real_count = 0;
659 WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
660 data << (uint8)GetMembersCount();
662 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
664 Player *looter = itr->getSource();
665 if (!looter->IsInWorld())
666 continue;
668 if (looter->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
670 data << looter->GetGUID();
671 ++real_count;
675 data.put<uint8>(0,real_count);
677 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
679 Player *looter = itr->getSource();
680 if (looter->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
681 looter->GetSession()->SendPacket(&data);
685 void Group::CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choise)
687 Rolls::iterator rollI = GetRoll(Guid);
688 if (rollI == RollId.end())
689 return;
690 Roll* roll = *rollI;
692 Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID);
693 // this condition means that player joins to the party after roll begins
694 if (itr == roll->playerVote.end())
695 return;
697 if (roll->getLoot())
698 if (roll->getLoot()->items.empty())
699 return;
701 switch (Choise)
703 case 0: //Player choose pass
705 SendLootRoll(0, playerGUID, 128, 128, *roll);
706 ++roll->totalPass;
707 itr->second = PASS;
709 break;
710 case 1: //player choose Need
712 SendLootRoll(0, playerGUID, 0, 0, *roll);
713 ++roll->totalNeed;
714 itr->second = NEED;
716 break;
717 case 2: //player choose Greed
719 SendLootRoll(0, playerGUID, 128, 2, *roll);
720 ++roll->totalGreed;
721 itr->second = GREED;
723 break;
725 if (roll->totalPass + roll->totalGreed + roll->totalNeed >= roll->totalPlayersRolling)
727 CountTheRoll(rollI, NumberOfPlayers);
731 //called when roll timer expires
732 void Group::EndRoll()
734 Rolls::iterator itr;
735 while(!RollId.empty())
737 //need more testing here, if rolls disappear
738 itr = RollId.begin();
739 CountTheRoll(itr, GetMembersCount()); //i don't have to edit player votes, who didn't vote ... he will pass
743 void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers)
745 Roll* roll = *rollI;
746 if(!roll->isValid()) // is loot already deleted ?
748 RollId.erase(rollI);
749 delete roll;
750 return;
752 //end of the roll
753 if (roll->totalNeed > 0)
755 if(!roll->playerVote.empty())
757 uint8 maxresul = 0;
758 uint64 maxguid = (*roll->playerVote.begin()).first;
759 Player *player;
761 for( Roll::PlayerVote::const_iterator itr=roll->playerVote.begin(); itr!=roll->playerVote.end(); ++itr)
763 if (itr->second != NEED)
764 continue;
766 uint8 randomN = urand(1, 99);
767 SendLootRoll(0, itr->first, randomN, 1, *roll);
768 if (maxresul < randomN)
770 maxguid = itr->first;
771 maxresul = randomN;
774 SendLootRollWon(0, maxguid, maxresul, 1, *roll);
775 player = objmgr.GetPlayer(maxguid);
777 if(player && player->GetSession())
779 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul);
781 ItemPosCountVec dest;
782 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
783 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
784 if ( msg == EQUIP_ERR_OK )
786 item->is_looted = true;
787 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
788 --roll->getLoot()->unlootedCount;
789 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
791 else
793 item->is_blocked = false;
794 player->SendEquipError( msg, NULL, NULL );
799 else if (roll->totalGreed > 0)
801 if(!roll->playerVote.empty())
803 uint8 maxresul = 0;
804 uint64 maxguid = (*roll->playerVote.begin()).first;
805 Player *player;
807 Roll::PlayerVote::iterator itr;
808 for (itr=roll->playerVote.begin(); itr!=roll->playerVote.end(); ++itr)
810 if (itr->second != GREED)
811 continue;
813 uint8 randomN = urand(1, 99);
814 SendLootRoll(0, itr->first, randomN, 2, *roll);
815 if (maxresul < randomN)
817 maxguid = itr->first;
818 maxresul = randomN;
821 SendLootRollWon(0, maxguid, maxresul, 2, *roll);
822 player = objmgr.GetPlayer(maxguid);
824 if(player && player->GetSession())
826 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul);
828 ItemPosCountVec dest;
829 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
830 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
831 if ( msg == EQUIP_ERR_OK )
833 item->is_looted = true;
834 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
835 --roll->getLoot()->unlootedCount;
836 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
838 else
840 item->is_blocked = false;
841 player->SendEquipError( msg, NULL, NULL );
846 else
848 SendLootAllPassed(NumberOfPlayers, *roll);
849 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
850 if(item) item->is_blocked = false;
852 RollId.erase(rollI);
853 delete roll;
856 void Group::SetTargetIcon(uint8 id, uint64 guid)
858 if(id >= TARGETICONCOUNT)
859 return;
861 // clean other icons
862 if( guid != 0 )
863 for(int i=0; i<TARGETICONCOUNT; ++i)
864 if( m_targetIcons[i] == guid )
865 SetTargetIcon(i, 0);
867 m_targetIcons[id] = guid;
869 WorldPacket data(MSG_RAID_TARGET_UPDATE, (2+8));
870 data << (uint8)0;
871 data << id;
872 data << guid;
873 BroadcastPacket(&data, true);
876 void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level)
878 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
880 Player* member = itr->getSource();
881 if(!member || !member->isAlive()) // only for alive
882 continue;
884 if(!member->IsAtGroupRewardDistance(victim)) // at req. distance
885 continue;
887 ++count;
888 sum_level += member->getLevel();
889 if(!member_with_max_level || member_with_max_level->getLevel() < member->getLevel())
890 member_with_max_level = member;
892 uint32 gray_level = MaNGOS::XP::GetGrayLevel(member->getLevel());
893 if( victim->getLevel() > gray_level && (!not_gray_member_with_max_level
894 || not_gray_member_with_max_level->getLevel() < member->getLevel()))
895 not_gray_member_with_max_level = member;
899 void Group::SendTargetIconList(WorldSession *session)
901 if(!session)
902 return;
904 WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
905 data << (uint8)1;
907 for(int i=0; i<TARGETICONCOUNT; ++i)
909 if(m_targetIcons[i] == 0)
910 continue;
912 data << (uint8)i;
913 data << m_targetIcons[i];
916 session->SendPacket(&data);
919 void Group::SendUpdate()
921 Player *player;
923 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
925 player = objmgr.GetPlayer(citr->guid);
926 if(!player || !player->GetSession() || player->GetGroup() != this )
927 continue;
928 // guess size
929 WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+8+4+GetMembersCount()*20));
930 data << (uint8)m_groupType; // group type
931 data << (uint8)(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
932 data << (uint8)(citr->group); // groupid
933 data << (uint8)(citr->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
934 data << uint64(0x50000000FFFFFFFELL); // related to voice chat?
935 data << uint32(GetMembersCount()-1);
936 for(member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
938 if(citr->guid == citr2->guid)
939 continue;
940 Player* member = objmgr.GetPlayer(citr2->guid);
941 uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
942 onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
944 data << citr2->name;
945 data << (uint64)citr2->guid;
946 // online-state
947 data << (uint8)(onlineState);
948 data << (uint8)(citr2->group); // groupid
949 data << (uint8)(citr2->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
952 data << uint64(m_leaderGuid); // leader guid
953 if(GetMembersCount()-1)
955 data << (uint8)m_lootMethod; // loot method
956 data << (uint64)m_looterGuid; // looter guid
957 data << (uint8)m_lootThreshold; // loot threshold
958 data << (uint8)m_difficulty; // Heroic Mod Group
960 player->GetSession()->SendPacket( &data );
964 void Group::UpdatePlayerOutOfRange(Player* pPlayer)
966 if(!pPlayer || !pPlayer->IsInWorld())
967 return;
969 Player *player;
970 WorldPacket data;
971 pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
973 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
975 player = itr->getSource();
976 if (player && player != pPlayer && !pPlayer->isVisibleFor(player,player->GetViewPoint()))
977 player->GetSession()->SendPacket(&data);
981 void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore)
983 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
985 Player *pl = itr->getSource();
986 if(!pl || (ignore != 0 && pl->GetGUID() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this) )
987 continue;
989 if (pl->GetSession() && (group==-1 || itr->getSubGroup()==group))
990 pl->GetSession()->SendPacket(packet);
994 void Group::BroadcastReadyCheck(WorldPacket *packet)
996 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
998 Player *pl = itr->getSource();
999 if(pl && pl->GetSession())
1000 if(IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
1001 pl->GetSession()->SendPacket(packet);
1005 void Group::OfflineReadyCheck()
1007 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
1009 Player *pl = objmgr.GetPlayer(citr->guid);
1010 if (!pl || !pl->GetSession())
1012 WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
1013 data << citr->guid;
1014 data << (uint8)0;
1015 BroadcastReadyCheck(&data);
1020 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant)
1022 // get first not-full group
1023 uint8 groupid = 0;
1024 if (m_subGroupsCounts)
1026 bool groupFound = false;
1027 for (; groupid < MAXRAIDSIZE/MAXGROUPSIZE; ++groupid)
1029 if (m_subGroupsCounts[groupid] < MAXGROUPSIZE)
1031 groupFound = true;
1032 break;
1035 // We are raid group and no one slot is free
1036 if (!groupFound)
1037 return false;
1040 return _addMember(guid, name, isAssistant, groupid);
1043 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group)
1045 if(IsFull())
1046 return false;
1048 if(!guid)
1049 return false;
1051 Player *player = objmgr.GetPlayer(guid);
1053 MemberSlot member;
1054 member.guid = guid;
1055 member.name = name;
1056 member.group = group;
1057 member.assistant = isAssistant;
1058 m_memberSlots.push_back(member);
1060 SubGroupCounterIncrease(group);
1062 if(player)
1064 player->SetGroupInvite(NULL);
1065 //if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid()
1066 if( player->GetGroup() && isBGGroup() )
1067 player->SetBattleGroundRaid(this, group);
1068 //if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup()
1069 else if ( player->GetGroup() )
1070 player->SetOriginalGroup(this, group);
1071 //if player is not in group, then call set group
1072 else
1073 player->SetGroup(this, group);
1074 // if the same group invites the player back, cancel the homebind timer
1075 InstanceGroupBind *bind = GetBoundInstance(player->GetMapId(), player->GetDifficulty());
1076 if(bind && bind->save->GetInstanceId() == player->GetInstanceId())
1077 player->m_InstanceValid = true;
1080 if(!isRaidGroup()) // reset targetIcons for non-raid-groups
1082 for(int i=0; i<TARGETICONCOUNT; ++i)
1083 m_targetIcons[i] = 0;
1086 if(!isBGGroup())
1088 // insert into group table
1089 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);
1092 return true;
1095 bool Group::_removeMember(const uint64 &guid)
1097 Player *player = objmgr.GetPlayer(guid);
1098 if (player)
1100 //if we are removing player from battleground raid
1101 if( isBGGroup() )
1102 player->RemoveFromBattleGroundRaid();
1103 else
1105 //we can remove player who is in battleground from his original group
1106 if( player->GetOriginalGroup() == this )
1107 player->SetOriginalGroup(NULL);
1108 else
1109 player->SetGroup(NULL);
1113 _removeRolls(guid);
1115 member_witerator slot = _getMemberWSlot(guid);
1116 if (slot != m_memberSlots.end())
1118 SubGroupCounterDecrease(slot->group);
1120 m_memberSlots.erase(slot);
1123 if(!isBGGroup())
1124 CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", GUID_LOPART(guid));
1126 if(m_leaderGuid == guid) // leader was removed
1128 if(GetMembersCount() > 0)
1129 _setLeader(m_memberSlots.front().guid);
1130 return true;
1133 return false;
1136 void Group::_setLeader(const uint64 &guid)
1138 member_citerator slot = _getMemberCSlot(guid);
1139 if(slot==m_memberSlots.end())
1140 return;
1142 if(!isBGGroup())
1144 // TODO: set a time limit to have this function run rarely cause it can be slow
1145 CharacterDatabase.BeginTransaction();
1147 // update the group's bound instances when changing leaders
1149 // remove all permanent binds from the group
1150 // in the DB also remove solo binds that will be replaced with permbinds
1151 // from the new leader
1152 CharacterDatabase.PExecute(
1153 "DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
1154 "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
1155 ")", GUID_LOPART(m_leaderGuid), GUID_LOPART(slot->guid)
1158 Player *player = objmgr.GetPlayer(slot->guid);
1159 if(player)
1161 for(uint8 i = 0; i < TOTAL_DIFFICULTIES; ++i)
1163 for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
1165 if(itr->second.perm)
1167 itr->second.save->RemoveGroup(this);
1168 m_boundInstances[i].erase(itr++);
1170 else
1171 ++itr;
1176 // update the group's solo binds to the new leader
1177 CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1179 // copy the permanent binds from the new leader to the group
1180 // overwriting the solo binds with permanent ones if necessary
1181 // in the DB those have been deleted already
1182 Player::ConvertInstancesToGroup(player, this, slot->guid);
1184 // update the group leader
1185 CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1186 CharacterDatabase.PExecute("UPDATE group_member SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1187 CharacterDatabase.CommitTransaction();
1190 m_leaderGuid = slot->guid;
1191 m_leaderName = slot->name;
1194 void Group::_removeRolls(const uint64 &guid)
1196 for (Rolls::iterator it = RollId.begin(); it < RollId.end(); ++it)
1198 Roll* roll = *it;
1199 Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
1200 if(itr2 == roll->playerVote.end())
1201 continue;
1203 if (itr2->second == GREED) --roll->totalGreed;
1204 if (itr2->second == NEED) --roll->totalNeed;
1205 if (itr2->second == PASS) --roll->totalPass;
1206 if (itr2->second != NOT_VALID) --roll->totalPlayersRolling;
1208 roll->playerVote.erase(itr2);
1210 CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, 3);
1214 bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group)
1216 member_witerator slot = _getMemberWSlot(guid);
1217 if(slot==m_memberSlots.end())
1218 return false;
1220 slot->group = group;
1222 SubGroupCounterIncrease(group);
1224 if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid));
1226 return true;
1229 bool Group::_setAssistantFlag(const uint64 &guid, const bool &state)
1231 member_witerator slot = _getMemberWSlot(guid);
1232 if(slot==m_memberSlots.end())
1233 return false;
1235 slot->assistant = state;
1236 if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state==true)?1:0, GUID_LOPART(guid));
1237 return true;
1240 bool Group::_setMainTank(const uint64 &guid)
1242 member_citerator slot = _getMemberCSlot(guid);
1243 if(slot==m_memberSlots.end())
1244 return false;
1246 if(m_mainAssistant == guid)
1247 _setMainAssistant(0);
1248 m_mainTank = guid;
1249 if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainTank), GUID_LOPART(m_leaderGuid));
1250 return true;
1253 bool Group::_setMainAssistant(const uint64 &guid)
1255 member_witerator slot = _getMemberWSlot(guid);
1256 if(slot==m_memberSlots.end())
1257 return false;
1259 if(m_mainTank == guid)
1260 _setMainTank(0);
1261 m_mainAssistant = guid;
1262 if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainAssistant), GUID_LOPART(m_leaderGuid));
1263 return true;
1266 bool Group::SameSubGroup(Player const* member1, Player const* member2) const
1268 if(!member1 || !member2) return false;
1269 if (member1->GetGroup() != this || member2->GetGroup() != this) return false;
1270 else return member1->GetSubGroup() == member2->GetSubGroup();
1273 // allows setting subgroup for offline members
1274 void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group)
1276 if(!isRaidGroup())
1277 return;
1278 Player *player = objmgr.GetPlayer(guid);
1280 if (!player)
1282 uint8 prevSubGroup;
1283 prevSubGroup = GetMemberGroup(guid);
1285 SubGroupCounterDecrease(prevSubGroup);
1287 if(_setMembersGroup(guid, group))
1288 SendUpdate();
1290 else
1291 // This methods handles itself groupcounter decrease
1292 ChangeMembersGroup(player, group);
1295 // only for online members
1296 void Group::ChangeMembersGroup(Player *player, const uint8 &group)
1298 if(!player || !isRaidGroup())
1299 return;
1300 if(_setMembersGroup(player->GetGUID(), group))
1302 uint8 prevSubGroup = player->GetSubGroup();
1303 if( player->GetGroup() == this )
1304 player->GetGroupRef().setSubGroup(group);
1305 //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
1306 else
1308 prevSubGroup = player->GetOriginalSubGroup();
1309 player->GetOriginalGroupRef().setSubGroup(group);
1311 SubGroupCounterDecrease(prevSubGroup);
1313 SendUpdate();
1317 void Group::UpdateLooterGuid( Creature* creature, bool ifneed )
1319 switch (GetLootMethod())
1321 case MASTER_LOOT:
1322 case FREE_FOR_ALL:
1323 return;
1324 default:
1325 // round robin style looting applies for all low
1326 // quality items in each loot method except free for all and master loot
1327 break;
1330 member_citerator guid_itr = _getMemberCSlot(GetLooterGuid());
1331 if(guid_itr != m_memberSlots.end())
1333 if(ifneed)
1335 // not update if only update if need and ok
1336 Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
1337 if(looter && looter->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
1338 return;
1340 ++guid_itr;
1343 // search next after current
1344 if(guid_itr != m_memberSlots.end())
1346 for(member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
1348 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1350 if (pl->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
1352 bool refresh = pl->GetLootGUID()==creature->GetGUID();
1354 //if(refresh) // update loot for new looter
1355 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1356 SetLooterGuid(pl->GetGUID());
1357 SendUpdate();
1358 if(refresh) // update loot for new looter
1359 pl->SendLoot(creature->GetGUID(),LOOT_CORPSE);
1360 return;
1366 // search from start
1367 for(member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
1369 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1371 if (pl->IsWithinDist(creature,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
1373 bool refresh = pl->GetLootGUID()==creature->GetGUID();
1375 //if(refresh) // update loot for new looter
1376 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1377 SetLooterGuid(pl->GetGUID());
1378 SendUpdate();
1379 if(refresh) // update loot for new looter
1380 pl->SendLoot(creature->GetGUID(),LOOT_CORPSE);
1381 return;
1386 SetLooterGuid(0);
1387 SendUpdate();
1390 uint32 Group::CanJoinBattleGroundQueue(BattleGroundTypeId bgTypeId, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot)
1392 // check for min / max count
1393 uint32 memberscount = GetMembersCount();
1394 if(memberscount < MinPlayerCount)
1395 return BG_JOIN_ERR_GROUP_NOT_ENOUGH;
1396 if(memberscount > MaxPlayerCount)
1397 return BG_JOIN_ERR_GROUP_TOO_MANY;
1399 // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
1400 Player * reference = GetFirstMember()->getSource();
1401 // no reference found, can't join this way
1402 if(!reference)
1403 return BG_JOIN_ERR_OFFLINE_MEMBER;
1405 BGQueueIdBasedOnLevel queue_id = reference->GetBattleGroundQueueIdFromLevel(bgTypeId);
1406 uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
1407 uint32 team = reference->GetTeam();
1409 // check every member of the group to be able to join
1410 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1412 Player *member = itr->getSource();
1413 // offline member? don't let join
1414 if(!member)
1415 return BG_JOIN_ERR_OFFLINE_MEMBER;
1416 // don't allow cross-faction join as group
1417 if(member->GetTeam() != team)
1418 return BG_JOIN_ERR_MIXED_FACTION;
1419 // not in the same battleground level braket, don't let join
1420 if(member->GetBattleGroundQueueIdFromLevel(bgTypeId) != queue_id)
1421 return BG_JOIN_ERR_MIXED_LEVELS;
1422 // don't let join rated matches if the arena team id doesn't match
1423 if(isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
1424 return BG_JOIN_ERR_MIXED_ARENATEAM;
1425 // don't let join if someone from the group is already in that bg queue
1426 if(member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId))
1427 return BG_JOIN_ERR_GROUP_MEMBER_ALREADY_IN_QUEUE;
1428 // check for deserter debuff in case not arena queue
1429 if(bgTypeId != BATTLEGROUND_AA && !member->CanJoinToBattleground())
1430 return BG_JOIN_ERR_GROUP_DESERTER;
1431 // check if member can join any more battleground queues
1432 if(!member->HasFreeBattleGroundQueueId())
1433 return BG_JOIN_ERR_ALL_QUEUES_USED;
1435 return BG_JOIN_ERR_OK;
1438 //===================================================
1439 //============== Roll ===============================
1440 //===================================================
1442 void Roll::targetObjectBuildLink()
1444 // called from link()
1445 getTarget()->addLootValidatorRef(this);
1448 void Group::SetDifficulty(uint8 difficulty)
1450 m_difficulty = difficulty;
1451 if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE leaderGuid ='%u'", m_difficulty, GUID_LOPART(m_leaderGuid));
1453 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1455 Player *player = itr->getSource();
1456 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1457 continue;
1458 player->SetDifficulty(difficulty);
1459 player->SendDungeonDifficulty(true);
1463 bool Group::InCombatToInstance(uint32 instanceId)
1465 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1467 Player *pPlayer = itr->getSource();
1468 if(pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId)
1469 return true;
1471 return false;
1474 void Group::ResetInstances(uint8 method, Player* SendMsgTo)
1476 if(isBGGroup())
1477 return;
1479 // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
1481 // we assume that when the difficulty changes, all instances that can be reset will be
1482 uint8 dif = GetDifficulty();
1484 for(BoundInstancesMap::iterator itr = m_boundInstances[dif].begin(); itr != m_boundInstances[dif].end();)
1486 InstanceSave *p = itr->second.save;
1487 const MapEntry *entry = sMapStore.LookupEntry(itr->first);
1488 if(!entry || (!p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND))
1490 ++itr;
1491 continue;
1494 if(method == INSTANCE_RESET_ALL)
1496 // the "reset all instances" method can only reset normal maps
1497 if(dif == DIFFICULTY_HEROIC || entry->map_type == MAP_RAID)
1499 ++itr;
1500 continue;
1504 bool isEmpty = true;
1505 // if the map is loaded, reset it
1506 Map *map = MapManager::Instance().FindMap(p->GetMapId(), p->GetInstanceId());
1507 if(map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset()))
1508 isEmpty = ((InstanceMap*)map)->Reset(method);
1510 if(SendMsgTo)
1512 if(isEmpty) SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
1513 else SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
1516 if(isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
1518 // do not reset the instance, just unbind if others are permanently bound to it
1519 if(p->CanReset()) p->DeleteFromDB();
1520 else CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
1521 // i don't know for sure if hash_map iterators
1522 m_boundInstances[dif].erase(itr);
1523 itr = m_boundInstances[dif].begin();
1524 // this unloads the instance save unless online players are bound to it
1525 // (eg. permanent binds or GM solo binds)
1526 p->RemoveGroup(this);
1528 else
1529 ++itr;
1533 InstanceGroupBind* Group::GetBoundInstance(uint32 mapid, uint8 difficulty)
1535 // some instances only have one difficulty
1536 const MapEntry* entry = sMapStore.LookupEntry(mapid);
1537 if(!entry || !entry->SupportsHeroicMode()) difficulty = DIFFICULTY_NORMAL;
1539 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1540 if(itr != m_boundInstances[difficulty].end())
1541 return &itr->second;
1542 else
1543 return NULL;
1546 InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
1548 if(save && !isBGGroup())
1550 InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
1551 if(bind.save)
1553 // when a boss is killed or when copying the players's binds to the group
1554 if(permanent != bind.perm || save != bind.save)
1555 if(!load) 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());
1557 else
1558 if(!load) CharacterDatabase.PExecute("INSERT INTO group_instance (leaderGuid, instance, permanent) VALUES ('%u', '%u', '%u')", GUID_LOPART(GetLeaderGUID()), save->GetInstanceId(), permanent);
1560 if(bind.save != save)
1562 if(bind.save) bind.save->RemoveGroup(this);
1563 save->AddGroup(this);
1566 bind.save = save;
1567 bind.perm = permanent;
1568 if(!load) sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetLeaderGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
1569 return &bind;
1571 else
1572 return NULL;
1575 void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
1577 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1578 if(itr != m_boundInstances[difficulty].end())
1580 if(!unload) CharacterDatabase.PExecute("DELETE FROM group_instance WHERE leaderGuid = '%u' AND instance = '%u'", GUID_LOPART(GetLeaderGUID()), itr->second.save->GetInstanceId());
1581 itr->second.save->RemoveGroup(this); // save can become invalid
1582 m_boundInstances[difficulty].erase(itr);
1586 void Group::_homebindIfInstance(Player *player)
1588 if(player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
1590 // leaving the group in an instance, the homebind timer is started
1591 // unless the player is permanently saved to the instance
1592 InstanceSave *save = sInstanceSaveManager.GetInstanceSave(player->GetInstanceId());
1593 InstancePlayerBind *playerBind = save ? player->GetBoundInstance(save->GetMapId(), save->GetDifficulty()) : NULL;
1594 if(!playerBind || !playerBind->perm)
1595 player->m_InstanceValid = false;