[8849] Fix a typo in STATUS_LOGGEDIN_OR_RECENTLY_LOGGEDOUT.
[getmangos.git] / src / game / Group.cpp
blob18e77bf243eb5f154ce0620a01cfc71baa35bb38
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 "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, 24);
369 data << uint64(0) << uint64(0) << uint64(0);
370 player->GetSession()->SendPacket(&data);
373 _homebindIfInstance(player);
376 if(leaderChanged)
378 WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1));
379 data << m_memberSlots.front().name;
380 BroadcastPacket(&data, true);
383 SendUpdate();
385 // if group before remove <= 2 disband it
386 else
387 Disband(true);
389 return m_memberSlots.size();
392 void Group::ChangeLeader(const uint64 &guid)
394 member_citerator slot = _getMemberCSlot(guid);
396 if(slot == m_memberSlots.end())
397 return;
399 _setLeader(guid);
401 WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1);
402 data << slot->name;
403 BroadcastPacket(&data, true);
404 SendUpdate();
407 void Group::Disband(bool hideDestroy)
409 Player *player;
411 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
413 player = sObjectMgr.GetPlayer(citr->guid);
414 if(!player)
415 continue;
417 //we cannot call _removeMember because it would invalidate member iterator
418 //if we are removing player from battleground raid
419 if( isBGGroup() )
420 player->RemoveFromBattleGroundRaid();
421 else
423 //we can remove player who is in battleground from his original group
424 if( player->GetOriginalGroup() == this )
425 player->SetOriginalGroup(NULL);
426 else
427 player->SetGroup(NULL);
430 // quest related GO state dependent from raid membership
431 if(isRaidGroup())
432 player->UpdateForQuestWorldObjects();
434 if(!player->GetSession())
435 continue;
437 WorldPacket data;
438 if(!hideDestroy)
440 data.Initialize(SMSG_GROUP_DESTROYED, 0);
441 player->GetSession()->SendPacket(&data);
444 //we already removed player from group and in player->GetGroup() is his original group, send update
445 if( Group* group = player->GetGroup() )
447 group->SendUpdate();
449 else
451 data.Initialize(SMSG_GROUP_LIST, 24);
452 data << uint64(0) << uint64(0) << uint64(0);
453 player->GetSession()->SendPacket(&data);
456 _homebindIfInstance(player);
458 RollId.clear();
459 m_memberSlots.clear();
461 RemoveAllInvites();
463 if(!isBGGroup())
465 CharacterDatabase.BeginTransaction();
466 CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
467 CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
468 CharacterDatabase.CommitTransaction();
469 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, NULL);
470 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, NULL);
473 m_leaderGuid = 0;
474 m_leaderName = "";
477 /*********************************************************/
478 /*** LOOT SYSTEM ***/
479 /*********************************************************/
481 void Group::SendLootStartRoll(uint32 CountDown, const Roll &r)
483 WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4+4));
484 data << uint64(r.itemGUID); // guid of rolled item
485 data << uint32(r.totalPlayersRolling); // maybe the number of players rolling for it???
486 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
487 data << uint32(r.itemRandomSuffix); // randomSuffix
488 data << uint32(r.itemRandomPropId); // item random property ID
489 data << uint32(r.itemCount); // items in stack
490 data << uint32(CountDown); // the countdown time to choose "need" or "greed"
492 for (Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
494 Player *p = sObjectMgr.GetPlayer(itr->first);
495 if(!p || !p->GetSession())
496 continue;
498 if(itr->second != NOT_VALID)
499 p->GetSession()->SendPacket( &data );
503 void Group::SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
505 WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1+1));
506 data << uint64(SourceGuid); // guid of the item rolled
507 data << uint32(0); // unknown, maybe amount of players
508 data << uint64(TargetGuid);
509 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
510 data << uint32(r.itemRandomSuffix); // randomSuffix
511 data << uint32(r.itemRandomPropId); // Item random property ID
512 data << uint8(RollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
513 data << uint8(RollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
514 data << uint8(0); // 2.4.0
516 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
518 Player *p = sObjectMgr.GetPlayer(itr->first);
519 if(!p || !p->GetSession())
520 continue;
522 if(itr->second != NOT_VALID)
523 p->GetSession()->SendPacket( &data );
527 void Group::SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
529 WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
530 data << uint64(SourceGuid); // guid of the item rolled
531 data << uint32(0); // unknown, maybe amount of players
532 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
533 data << uint32(r.itemRandomSuffix); // randomSuffix
534 data << uint32(r.itemRandomPropId); // Item random property
535 data << uint64(TargetGuid); // guid of the player who won.
536 data << uint8(RollNumber); // rollnumber realted to SMSG_LOOT_ROLL
537 data << uint8(RollType); // Rolltype related to SMSG_LOOT_ROLL
539 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
541 Player *p = sObjectMgr.GetPlayer(itr->first);
542 if(!p || !p->GetSession())
543 continue;
545 if(itr->second != NOT_VALID)
546 p->GetSession()->SendPacket( &data );
550 void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r)
552 WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
553 data << uint64(r.itemGUID); // Guid of the item rolled
554 data << uint32(NumberOfPlayers); // The number of players rolling for it???
555 data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for
556 data << uint32(r.itemRandomPropId); // Item random property ID
557 data << uint32(r.itemRandomSuffix); // Item random suffix ID
559 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
561 Player *p = sObjectMgr.GetPlayer(itr->first);
562 if(!p || !p->GetSession())
563 continue;
565 if(itr->second != NOT_VALID)
566 p->GetSession()->SendPacket( &data );
570 void Group::GroupLoot(const uint64& playerGUID, Loot *loot, Creature *creature)
572 std::vector<LootItem>::iterator i;
573 ItemPrototype const *item;
574 uint8 itemSlot = 0;
575 Player *player = sObjectMgr.GetPlayer(playerGUID);
576 Group *group = player->GetGroup();
578 for (i = loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
580 item = ObjectMgr::GetItemPrototype(i->itemid);
581 if (!item)
583 //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
584 continue;
587 //roll for over-threshold item if it's one-player loot
588 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
590 uint64 newitemGUID = MAKE_NEW_GUID(sObjectMgr.GenerateLowGuid(HIGHGUID_ITEM), 0, HIGHGUID_ITEM);
591 Roll* r = new Roll(newitemGUID, *i);
593 //a vector is filled with only near party members
594 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
596 Player *member = itr->getSource();
597 if(!member || !member->GetSession())
598 continue;
599 if ( i->AllowedForPlayer(member) )
601 if (member->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
603 r->playerVote[member->GetGUID()] = NOT_EMITED_YET;
604 ++r->totalPlayersRolling;
609 r->setLoot(loot);
610 r->itemSlot = itemSlot;
612 group->SendLootStartRoll(60000, *r);
614 loot->items[itemSlot].is_blocked = true;
615 creature->m_groupLootTimer = 60000;
616 creature->lootingGroupLeaderGUID = GetLeaderGUID();
618 RollId.push_back(r);
620 else
621 i->is_underthreshold = 1;
625 void Group::NeedBeforeGreed(const uint64& playerGUID, Loot *loot, Creature *creature)
627 ItemPrototype const *item;
628 Player *player = sObjectMgr.GetPlayer(playerGUID);
629 Group *group = player->GetGroup();
631 uint8 itemSlot = 0;
632 for(std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
634 item = ObjectMgr::GetItemPrototype(i->itemid);
636 //only roll for one-player items, not for ones everyone can get
637 if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
639 uint64 newitemGUID = MAKE_NEW_GUID(sObjectMgr.GenerateLowGuid(HIGHGUID_ITEM), 0, HIGHGUID_ITEM);
640 Roll* r = new Roll(newitemGUID, *i);
642 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
644 Player *playerToRoll = itr->getSource();
645 if(!playerToRoll || !playerToRoll->GetSession())
646 continue;
648 if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll) )
650 if (playerToRoll->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
652 r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET;
653 ++r->totalPlayersRolling;
658 if (r->totalPlayersRolling > 0)
660 r->setLoot(loot);
661 r->itemSlot = itemSlot;
663 group->SendLootStartRoll(60000, *r);
665 loot->items[itemSlot].is_blocked = true;
667 RollId.push_back(r);
669 else
671 delete r;
674 else
675 i->is_underthreshold = 1;
679 void Group::MasterLoot(const uint64& playerGUID, Loot* /*loot*/, Creature *creature)
681 Player *player = sObjectMgr.GetPlayer(playerGUID);
682 if(!player)
683 return;
685 sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330) player = [%s].", player->GetName());
687 uint32 real_count = 0;
689 WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
690 data << (uint8)GetMembersCount();
692 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
694 Player *looter = itr->getSource();
695 if (!looter->IsInWorld())
696 continue;
698 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
700 data << looter->GetGUID();
701 ++real_count;
705 data.put<uint8>(0, real_count);
707 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
709 Player *looter = itr->getSource();
710 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
711 looter->GetSession()->SendPacket(&data);
715 void Group::CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choise)
717 Rolls::iterator rollI = GetRoll(Guid);
718 if (rollI == RollId.end())
719 return;
720 Roll* roll = *rollI;
722 Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID);
723 // this condition means that player joins to the party after roll begins
724 if (itr == roll->playerVote.end())
725 return;
727 if (roll->getLoot())
728 if (roll->getLoot()->items.empty())
729 return;
731 switch (Choise)
733 case ROLL_PASS: // Player choose pass
735 SendLootRoll(0, playerGUID, 0, ROLL_PASS, *roll);
736 ++roll->totalPass;
737 itr->second = PASS;
739 break;
740 case ROLL_NEED: // player choose Need
742 SendLootRoll(0, playerGUID, 0, ROLL_NEED, *roll);
743 ++roll->totalNeed;
744 itr->second = NEED;
746 break;
747 case ROLL_GREED: // player choose Greed
749 SendLootRoll(0, playerGUID, 128, ROLL_GREED, *roll);
750 ++roll->totalGreed;
751 itr->second = GREED;
753 break;
755 if (roll->totalPass + roll->totalGreed + roll->totalNeed >= roll->totalPlayersRolling)
757 CountTheRoll(rollI, NumberOfPlayers);
761 //called when roll timer expires
762 void Group::EndRoll()
764 Rolls::iterator itr;
765 while(!RollId.empty())
767 //need more testing here, if rolls disappear
768 itr = RollId.begin();
769 CountTheRoll(itr, GetMembersCount()); //i don't have to edit player votes, who didn't vote ... he will pass
773 void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers)
775 Roll* roll = *rollI;
776 if(!roll->isValid()) // is loot already deleted ?
778 RollId.erase(rollI);
779 delete roll;
780 return;
782 //end of the roll
783 if (roll->totalNeed > 0)
785 if(!roll->playerVote.empty())
787 uint8 maxresul = 0;
788 uint64 maxguid = (*roll->playerVote.begin()).first;
789 Player *player;
791 for( Roll::PlayerVote::const_iterator itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
793 if (itr->second != NEED)
794 continue;
796 uint8 randomN = urand(1, 99);
797 SendLootRoll(0, itr->first, randomN, ROLL_NEED, *roll);
798 if (maxresul < randomN)
800 maxguid = itr->first;
801 maxresul = randomN;
804 SendLootRollWon(0, maxguid, maxresul, ROLL_NEED, *roll);
805 player = sObjectMgr.GetPlayer(maxguid);
807 if(player && player->GetSession())
809 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul);
811 ItemPosCountVec dest;
812 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
813 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
814 if ( msg == EQUIP_ERR_OK )
816 item->is_looted = true;
817 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
818 --roll->getLoot()->unlootedCount;
819 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
821 else
823 item->is_blocked = false;
824 player->SendEquipError( msg, NULL, NULL );
829 else if (roll->totalGreed > 0)
831 if(!roll->playerVote.empty())
833 uint8 maxresul = 0;
834 uint64 maxguid = (*roll->playerVote.begin()).first;
835 Player *player;
837 Roll::PlayerVote::iterator itr;
838 for (itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
840 if (itr->second != GREED)
841 continue;
843 uint8 randomN = urand(1, 99);
844 SendLootRoll(0, itr->first, randomN, ROLL_GREED, *roll);
845 if (maxresul < randomN)
847 maxguid = itr->first;
848 maxresul = randomN;
851 SendLootRollWon(0, maxguid, maxresul, ROLL_GREED, *roll);
852 player = sObjectMgr.GetPlayer(maxguid);
854 if(player && player->GetSession())
856 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul);
858 ItemPosCountVec dest;
859 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
860 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
861 if ( msg == EQUIP_ERR_OK )
863 item->is_looted = true;
864 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
865 --roll->getLoot()->unlootedCount;
866 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
868 else
870 item->is_blocked = false;
871 player->SendEquipError( msg, NULL, NULL );
876 else
878 SendLootAllPassed(NumberOfPlayers, *roll);
879 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
880 if(item) item->is_blocked = false;
882 RollId.erase(rollI);
883 delete roll;
886 void Group::SetTargetIcon(uint8 id, uint64 guid)
888 if(id >= TARGETICONCOUNT)
889 return;
891 // clean other icons
892 if( guid != 0 )
893 for(int i = 0; i < TARGETICONCOUNT; ++i)
894 if( m_targetIcons[i] == guid )
895 SetTargetIcon(i, 0);
897 m_targetIcons[id] = guid;
899 WorldPacket data(MSG_RAID_TARGET_UPDATE, (2+8));
900 data << (uint8)0;
901 data << id;
902 data << guid;
903 BroadcastPacket(&data, true);
906 void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level)
908 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
910 Player* member = itr->getSource();
911 if(!member || !member->isAlive()) // only for alive
912 continue;
914 if(!member->IsAtGroupRewardDistance(victim)) // at req. distance
915 continue;
917 ++count;
918 sum_level += member->getLevel();
919 if(!member_with_max_level || member_with_max_level->getLevel() < member->getLevel())
920 member_with_max_level = member;
922 uint32 gray_level = MaNGOS::XP::GetGrayLevel(member->getLevel());
923 if( victim->getLevel() > gray_level && (!not_gray_member_with_max_level
924 || not_gray_member_with_max_level->getLevel() < member->getLevel()))
925 not_gray_member_with_max_level = member;
929 void Group::SendTargetIconList(WorldSession *session)
931 if(!session)
932 return;
934 WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
935 data << (uint8)1;
937 for(int i = 0; i < TARGETICONCOUNT; ++i)
939 if(m_targetIcons[i] == 0)
940 continue;
942 data << (uint8)i;
943 data << m_targetIcons[i];
946 session->SendPacket(&data);
949 void Group::SendUpdate()
951 Player *player;
953 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
955 player = sObjectMgr.GetPlayer(citr->guid);
956 if(!player || !player->GetSession() || player->GetGroup() != this )
957 continue;
958 // guess size
959 WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+8+4+GetMembersCount()*20));
960 data << (uint8)m_groupType; // group type
961 data << (uint8)(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
962 data << (uint8)(citr->group); // groupid
963 data << (uint8)(citr->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
964 data << uint64(0x50000000FFFFFFFELL); // related to voice chat?
965 data << uint32(GetMembersCount()-1);
966 for(member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
968 if(citr->guid == citr2->guid)
969 continue;
970 Player* member = sObjectMgr.GetPlayer(citr2->guid);
971 uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
972 onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
974 data << citr2->name;
975 data << (uint64)citr2->guid;
976 // online-state
977 data << (uint8)(onlineState);
978 data << (uint8)(citr2->group); // groupid
979 data << (uint8)(citr2->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
982 data << uint64(m_leaderGuid); // leader guid
983 if(GetMembersCount()-1)
985 data << (uint8)m_lootMethod; // loot method
986 data << (uint64)m_looterGuid; // looter guid
987 data << (uint8)m_lootThreshold; // loot threshold
988 data << (uint8)m_dungeonDifficulty; // Dungeon Difficulty
989 data << (uint8)m_raidDifficulty; // Raid Difficulty
991 player->GetSession()->SendPacket( &data );
995 void Group::UpdatePlayerOutOfRange(Player* pPlayer)
997 if(!pPlayer || !pPlayer->IsInWorld())
998 return;
1000 Player *player;
1001 WorldPacket data;
1002 pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
1004 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1006 player = itr->getSource();
1007 if (player && player != pPlayer && !pPlayer->isVisibleFor(player,player->GetViewPoint()))
1008 player->GetSession()->SendPacket(&data);
1012 void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore)
1014 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1016 Player *pl = itr->getSource();
1017 if(!pl || (ignore != 0 && pl->GetGUID() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this) )
1018 continue;
1020 if (pl->GetSession() && (group == -1 || itr->getSubGroup() == group))
1021 pl->GetSession()->SendPacket(packet);
1025 void Group::BroadcastReadyCheck(WorldPacket *packet)
1027 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1029 Player *pl = itr->getSource();
1030 if(pl && pl->GetSession())
1031 if(IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
1032 pl->GetSession()->SendPacket(packet);
1036 void Group::OfflineReadyCheck()
1038 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
1040 Player *pl = sObjectMgr.GetPlayer(citr->guid);
1041 if (!pl || !pl->GetSession())
1043 WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
1044 data << citr->guid;
1045 data << (uint8)0;
1046 BroadcastReadyCheck(&data);
1051 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant)
1053 // get first not-full group
1054 uint8 groupid = 0;
1055 if (m_subGroupsCounts)
1057 bool groupFound = false;
1058 for (; groupid < MAXRAIDSIZE / MAXGROUPSIZE; ++groupid)
1060 if (m_subGroupsCounts[groupid] < MAXGROUPSIZE)
1062 groupFound = true;
1063 break;
1066 // We are raid group and no one slot is free
1067 if (!groupFound)
1068 return false;
1071 return _addMember(guid, name, isAssistant, groupid);
1074 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group)
1076 if(IsFull())
1077 return false;
1079 if(!guid)
1080 return false;
1082 Player *player = sObjectMgr.GetPlayer(guid);
1084 MemberSlot member;
1085 member.guid = guid;
1086 member.name = name;
1087 member.group = group;
1088 member.assistant = isAssistant;
1089 m_memberSlots.push_back(member);
1091 SubGroupCounterIncrease(group);
1093 if(player)
1095 player->SetGroupInvite(NULL);
1096 //if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid()
1097 if( player->GetGroup() && isBGGroup() )
1098 player->SetBattleGroundRaid(this, group);
1099 //if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup()
1100 else if ( player->GetGroup() )
1101 player->SetOriginalGroup(this, group);
1102 //if player is not in group, then call set group
1103 else
1104 player->SetGroup(this, group);
1105 // if the same group invites the player back, cancel the homebind timer
1106 InstanceGroupBind *bind = GetBoundInstance(player);
1107 if(bind && bind->save->GetInstanceId() == player->GetInstanceId())
1108 player->m_InstanceValid = true;
1111 if(!isRaidGroup()) // reset targetIcons for non-raid-groups
1113 for(int i = 0; i < TARGETICONCOUNT; ++i)
1114 m_targetIcons[i] = 0;
1117 if(!isBGGroup())
1119 // insert into group table
1120 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);
1123 return true;
1126 bool Group::_removeMember(const uint64 &guid)
1128 Player *player = sObjectMgr.GetPlayer(guid);
1129 if (player)
1131 //if we are removing player from battleground raid
1132 if( isBGGroup() )
1133 player->RemoveFromBattleGroundRaid();
1134 else
1136 //we can remove player who is in battleground from his original group
1137 if( player->GetOriginalGroup() == this )
1138 player->SetOriginalGroup(NULL);
1139 else
1140 player->SetGroup(NULL);
1144 _removeRolls(guid);
1146 member_witerator slot = _getMemberWSlot(guid);
1147 if (slot != m_memberSlots.end())
1149 SubGroupCounterDecrease(slot->group);
1151 m_memberSlots.erase(slot);
1154 if(!isBGGroup())
1155 CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", GUID_LOPART(guid));
1157 if(m_leaderGuid == guid) // leader was removed
1159 if(GetMembersCount() > 0)
1160 _setLeader(m_memberSlots.front().guid);
1161 return true;
1164 return false;
1167 void Group::_setLeader(const uint64 &guid)
1169 member_citerator slot = _getMemberCSlot(guid);
1170 if(slot==m_memberSlots.end())
1171 return;
1173 if(!isBGGroup())
1175 // TODO: set a time limit to have this function run rarely cause it can be slow
1176 CharacterDatabase.BeginTransaction();
1178 // update the group's bound instances when changing leaders
1180 // remove all permanent binds from the group
1181 // in the DB also remove solo binds that will be replaced with permbinds
1182 // from the new leader
1183 CharacterDatabase.PExecute(
1184 "DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
1185 "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
1186 ")", GUID_LOPART(m_leaderGuid), GUID_LOPART(slot->guid)
1189 Player *player = sObjectMgr.GetPlayer(slot->guid);
1190 if(player)
1192 for(uint8 i = 0; i < MAX_DIFFICULTY; ++i)
1194 for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
1196 if(itr->second.perm)
1198 itr->second.save->RemoveGroup(this);
1199 m_boundInstances[i].erase(itr++);
1201 else
1202 ++itr;
1207 // update the group's solo binds to the new leader
1208 CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1210 // copy the permanent binds from the new leader to the group
1211 // overwriting the solo binds with permanent ones if necessary
1212 // in the DB those have been deleted already
1213 Player::ConvertInstancesToGroup(player, this, slot->guid);
1215 // update the group leader
1216 CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1217 CharacterDatabase.PExecute("UPDATE group_member SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1218 CharacterDatabase.CommitTransaction();
1221 m_leaderGuid = slot->guid;
1222 m_leaderName = slot->name;
1225 void Group::_removeRolls(const uint64 &guid)
1227 for (Rolls::iterator it = RollId.begin(); it < RollId.end(); ++it)
1229 Roll* roll = *it;
1230 Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
1231 if(itr2 == roll->playerVote.end())
1232 continue;
1234 if (itr2->second == GREED)
1235 --roll->totalGreed;
1236 if (itr2->second == NEED)
1237 --roll->totalNeed;
1238 if (itr2->second == PASS)
1239 --roll->totalPass;
1240 if (itr2->second != NOT_VALID)
1241 --roll->totalPlayersRolling;
1243 roll->playerVote.erase(itr2);
1245 CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, MAX_ROLL_TYPE);
1249 bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group)
1251 member_witerator slot = _getMemberWSlot(guid);
1252 if(slot == m_memberSlots.end())
1253 return false;
1255 slot->group = group;
1257 SubGroupCounterIncrease(group);
1259 if(!isBGGroup())
1260 CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid));
1262 return true;
1265 bool Group::_setAssistantFlag(const uint64 &guid, const bool &state)
1267 member_witerator slot = _getMemberWSlot(guid);
1268 if(slot == m_memberSlots.end())
1269 return false;
1271 slot->assistant = state;
1272 if(!isBGGroup())
1273 CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state==true)?1:0, GUID_LOPART(guid));
1274 return true;
1277 bool Group::_setMainTank(const uint64 &guid)
1279 member_citerator slot = _getMemberCSlot(guid);
1280 if(slot == m_memberSlots.end())
1281 return false;
1283 if(m_mainAssistant == guid)
1284 _setMainAssistant(0);
1285 m_mainTank = guid;
1286 if(!isBGGroup())
1287 CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainTank), GUID_LOPART(m_leaderGuid));
1288 return true;
1291 bool Group::_setMainAssistant(const uint64 &guid)
1293 member_witerator slot = _getMemberWSlot(guid);
1294 if(slot == m_memberSlots.end())
1295 return false;
1297 if(m_mainTank == guid)
1298 _setMainTank(0);
1299 m_mainAssistant = guid;
1300 if(!isBGGroup())
1301 CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainAssistant), GUID_LOPART(m_leaderGuid));
1302 return true;
1305 bool Group::SameSubGroup(Player const* member1, Player const* member2) const
1307 if(!member1 || !member2)
1308 return false;
1309 if (member1->GetGroup() != this || member2->GetGroup() != this)
1310 return false;
1311 else
1312 return member1->GetSubGroup() == member2->GetSubGroup();
1315 // allows setting subgroup for offline members
1316 void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group)
1318 if(!isRaidGroup())
1319 return;
1320 Player *player = sObjectMgr.GetPlayer(guid);
1322 if (!player)
1324 uint8 prevSubGroup;
1325 prevSubGroup = GetMemberGroup(guid);
1327 SubGroupCounterDecrease(prevSubGroup);
1329 if(_setMembersGroup(guid, group))
1330 SendUpdate();
1332 else
1333 // This methods handles itself groupcounter decrease
1334 ChangeMembersGroup(player, group);
1337 // only for online members
1338 void Group::ChangeMembersGroup(Player *player, const uint8 &group)
1340 if(!player || !isRaidGroup())
1341 return;
1342 if(_setMembersGroup(player->GetGUID(), group))
1344 uint8 prevSubGroup = player->GetSubGroup();
1345 if( player->GetGroup() == this )
1346 player->GetGroupRef().setSubGroup(group);
1347 //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
1348 else
1350 prevSubGroup = player->GetOriginalSubGroup();
1351 player->GetOriginalGroupRef().setSubGroup(group);
1353 SubGroupCounterDecrease(prevSubGroup);
1355 SendUpdate();
1359 void Group::UpdateLooterGuid( Creature* creature, bool ifneed )
1361 switch (GetLootMethod())
1363 case MASTER_LOOT:
1364 case FREE_FOR_ALL:
1365 return;
1366 default:
1367 // round robin style looting applies for all low
1368 // quality items in each loot method except free for all and master loot
1369 break;
1372 member_citerator guid_itr = _getMemberCSlot(GetLooterGuid());
1373 if(guid_itr != m_memberSlots.end())
1375 if(ifneed)
1377 // not update if only update if need and ok
1378 Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
1379 if(looter && looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1380 return;
1382 ++guid_itr;
1385 // search next after current
1386 if(guid_itr != m_memberSlots.end())
1388 for(member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
1390 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1392 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1394 bool refresh = pl->GetLootGUID() == creature->GetGUID();
1396 //if(refresh) // update loot for new looter
1397 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1398 SetLooterGuid(pl->GetGUID());
1399 SendUpdate();
1400 if(refresh) // update loot for new looter
1401 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1402 return;
1408 // search from start
1409 for(member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
1411 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1413 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE), false))
1415 bool refresh = pl->GetLootGUID()==creature->GetGUID();
1417 //if(refresh) // update loot for new looter
1418 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1419 SetLooterGuid(pl->GetGUID());
1420 SendUpdate();
1421 if(refresh) // update loot for new looter
1422 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1423 return;
1428 SetLooterGuid(0);
1429 SendUpdate();
1432 uint32 Group::CanJoinBattleGroundQueue(BattleGroundTypeId bgTypeId, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot)
1434 // check for min / max count
1435 uint32 memberscount = GetMembersCount();
1436 if(memberscount < MinPlayerCount)
1437 return BG_JOIN_ERR_GROUP_NOT_ENOUGH;
1438 if(memberscount > MaxPlayerCount)
1439 return BG_JOIN_ERR_GROUP_TOO_MANY;
1441 // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
1442 Player * reference = GetFirstMember()->getSource();
1443 // no reference found, can't join this way
1444 if(!reference)
1445 return BG_JOIN_ERR_OFFLINE_MEMBER;
1447 BGQueueIdBasedOnLevel queue_id = reference->GetBattleGroundQueueIdFromLevel();
1448 uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
1449 uint32 team = reference->GetTeam();
1451 // check every member of the group to be able to join
1452 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1454 Player *member = itr->getSource();
1455 // offline member? don't let join
1456 if(!member)
1457 return BG_JOIN_ERR_OFFLINE_MEMBER;
1458 // don't allow cross-faction join as group
1459 if(member->GetTeam() != team)
1460 return BG_JOIN_ERR_MIXED_FACTION;
1461 // not in the same battleground level braket, don't let join
1462 if(member->GetBattleGroundQueueIdFromLevel() != queue_id)
1463 return BG_JOIN_ERR_MIXED_LEVELS;
1464 // don't let join rated matches if the arena team id doesn't match
1465 if(isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
1466 return BG_JOIN_ERR_MIXED_ARENATEAM;
1467 // don't let join if someone from the group is already in that bg queue
1468 if(member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId))
1469 return BG_JOIN_ERR_GROUP_MEMBER_ALREADY_IN_QUEUE;
1470 // check for deserter debuff in case not arena queue
1471 if(bgTypeId != BATTLEGROUND_AA && !member->CanJoinToBattleground())
1472 return BG_JOIN_ERR_GROUP_DESERTER;
1473 // check if member can join any more battleground queues
1474 if(!member->HasFreeBattleGroundQueueId())
1475 return BG_JOIN_ERR_ALL_QUEUES_USED;
1477 return BG_JOIN_ERR_OK;
1480 //===================================================
1481 //============== Roll ===============================
1482 //===================================================
1484 void Roll::targetObjectBuildLink()
1486 // called from link()
1487 getTarget()->addLootValidatorRef(this);
1490 void Group::SetDungeonDifficulty(Difficulty difficulty)
1492 m_dungeonDifficulty = difficulty;
1493 if(!isBGGroup())
1494 CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE leaderGuid ='%u'", m_dungeonDifficulty, GUID_LOPART(m_leaderGuid));
1496 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1498 Player *player = itr->getSource();
1499 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1500 continue;
1501 player->SetDungeonDifficulty(difficulty);
1502 player->SendDungeonDifficulty(true);
1506 void Group::SetRaidDifficulty(Difficulty difficulty)
1508 m_raidDifficulty = difficulty;
1509 if(!isBGGroup())
1510 CharacterDatabase.PExecute("UPDATE groups SET raiddifficulty = %u WHERE leaderGuid ='%u'", m_raidDifficulty, GUID_LOPART(m_leaderGuid));
1512 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1514 Player *player = itr->getSource();
1515 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1516 continue;
1517 player->SetRaidDifficulty(difficulty);
1518 player->SendRaidDifficulty(true);
1522 bool Group::InCombatToInstance(uint32 instanceId)
1524 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1526 Player *pPlayer = itr->getSource();
1527 if(pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId)
1528 return true;
1530 return false;
1533 void Group::ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo)
1535 if(isBGGroup())
1536 return;
1538 // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
1540 // we assume that when the difficulty changes, all instances that can be reset will be
1541 Difficulty diff = GetDifficulty(isRaid);
1543 for(BoundInstancesMap::iterator itr = m_boundInstances[diff].begin(); itr != m_boundInstances[diff].end();)
1545 InstanceSave *p = itr->second.save;
1546 const MapEntry *entry = sMapStore.LookupEntry(itr->first);
1547 if (!entry || entry->IsRaid() != isRaid || (!p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND))
1549 ++itr;
1550 continue;
1553 if(method == INSTANCE_RESET_ALL)
1555 // the "reset all instances" method can only reset normal maps
1556 if (entry->map_type == MAP_RAID || diff == DUNGEON_DIFFICULTY_HEROIC)
1558 ++itr;
1559 continue;
1563 bool isEmpty = true;
1564 // if the map is loaded, reset it
1565 Map *map = sMapMgr.FindMap(p->GetMapId(), p->GetInstanceId());
1566 if(map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset()))
1567 isEmpty = ((InstanceMap*)map)->Reset(method);
1569 if(SendMsgTo)
1571 if(isEmpty)
1572 SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
1573 else
1574 SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
1577 if(isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
1579 // do not reset the instance, just unbind if others are permanently bound to it
1580 if(p->CanReset())
1581 p->DeleteFromDB();
1582 else
1583 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
1584 // i don't know for sure if hash_map iterators
1585 m_boundInstances[diff].erase(itr);
1586 itr = m_boundInstances[diff].begin();
1587 // this unloads the instance save unless online players are bound to it
1588 // (eg. permanent binds or GM solo binds)
1589 p->RemoveGroup(this);
1591 else
1592 ++itr;
1596 InstanceGroupBind* Group::GetBoundInstance(Player* player)
1598 uint32 mapid = player->GetMapId();
1599 MapEntry const* mapEntry = sMapStore.LookupEntry(mapid);
1600 if(!mapEntry)
1601 return NULL;
1603 Difficulty difficulty = player->GetDifficulty(mapEntry->IsRaid());
1605 // some instances only have one difficulty
1606 MapDifficulty const* mapDiff = GetMapDifficultyData(mapid,difficulty);
1607 if(!mapDiff)
1608 difficulty = DUNGEON_DIFFICULTY_NORMAL;
1610 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1611 if(itr != m_boundInstances[difficulty].end())
1612 return &itr->second;
1613 else
1614 return NULL;
1617 InstanceGroupBind* Group::GetBoundInstance(Map* aMap)
1619 // Currently spawn numbering not different from map difficulty
1620 Difficulty difficulty = GetDifficulty(aMap->IsRaid());
1622 // some instances only have one difficulty
1623 MapDifficulty const* mapDiff = GetMapDifficultyData(aMap->GetId(),difficulty);
1624 if(!mapDiff)
1625 return NULL;
1627 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(aMap->GetId());
1628 if(itr != m_boundInstances[difficulty].end())
1629 return &itr->second;
1630 else
1631 return NULL;
1634 InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
1636 if(save && !isBGGroup())
1638 InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
1639 if(bind.save)
1641 // when a boss is killed or when copying the players's binds to the group
1642 if(permanent != bind.perm || save != bind.save)
1643 if(!load)
1644 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());
1646 else if(!load)
1647 CharacterDatabase.PExecute("INSERT INTO group_instance (leaderGuid, instance, permanent) VALUES ('%u', '%u', '%u')", GUID_LOPART(GetLeaderGUID()), save->GetInstanceId(), permanent);
1649 if(bind.save != save)
1651 if(bind.save)
1652 bind.save->RemoveGroup(this);
1653 save->AddGroup(this);
1656 bind.save = save;
1657 bind.perm = permanent;
1658 if(!load)
1659 sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetLeaderGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
1660 return &bind;
1662 else
1663 return NULL;
1666 void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
1668 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1669 if(itr != m_boundInstances[difficulty].end())
1671 if(!unload)
1672 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE leaderGuid = '%u' AND instance = '%u'", GUID_LOPART(GetLeaderGUID()), itr->second.save->GetInstanceId());
1673 itr->second.save->RemoveGroup(this); // save can become invalid
1674 m_boundInstances[difficulty].erase(itr);
1678 void Group::_homebindIfInstance(Player *player)
1680 if(player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
1682 // leaving the group in an instance, the homebind timer is started
1683 // unless the player is permanently saved to the instance
1684 InstanceSave *save = sInstanceSaveMgr.GetInstanceSave(player->GetInstanceId());
1685 InstancePlayerBind *playerBind = save ? player->GetBoundInstance(save->GetMapId(), save->GetDifficulty()) : NULL;
1686 if(!playerBind || !playerBind->perm)
1687 player->m_InstanceValid = false;