[9921] In case player who tap creature in group leave group it must anyway rewarded...
[getmangos.git] / src / game / Group.cpp
blobb0909df4f908ead6ae35b0a2637c7d3a66fb040d
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Opcodes.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "Player.h"
24 #include "World.h"
25 #include "ObjectMgr.h"
26 #include "ObjectGuid.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"
35 #include "LootMgr.h"
37 #define LOOT_ROLL_TIMEOUT (1*MINUTE*IN_MILLISECONDS)
39 Group::Group() : m_Id(0), m_leaderGuid(0), m_mainTank(0), m_mainAssistant(0), m_groupType(GROUPTYPE_NORMAL),
40 m_dungeonDifficulty(REGULAR_DIFFICULTY), m_raidDifficulty(REGULAR_DIFFICULTY),
41 m_bgGroup(NULL), m_lootMethod(FREE_FOR_ALL), m_looterGuid(0), m_lootThreshold(ITEM_QUALITY_UNCOMMON),
42 m_subGroupsCounts(NULL)
44 for (int i = 0; i < TARGETICONCOUNT; ++i)
45 m_targetIcons[i] = 0;
48 Group::~Group()
50 if(m_bgGroup)
52 DEBUG_LOG("Group::~Group: battleground group being deleted.");
53 if(m_bgGroup->GetBgRaid(ALLIANCE) == this)
54 m_bgGroup->SetBgRaid(ALLIANCE, NULL);
55 else if(m_bgGroup->GetBgRaid(HORDE) == this)
56 m_bgGroup->SetBgRaid(HORDE, NULL);
57 else
58 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 < MAX_DIFFICULTY; ++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_BGRAID : 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_dungeonDifficulty = DUNGEON_DIFFICULTY_NORMAL;
96 m_raidDifficulty = RAID_DIFFICULTY_10MAN_NORMAL;
97 if(!isBGGroup())
99 m_Id = sObjectMgr.GenerateGroupId();
101 Player *leader = sObjectMgr.GetPlayer(guid);
102 if(leader)
104 m_dungeonDifficulty = leader->GetDungeonDifficulty();
105 m_raidDifficulty = leader->GetRaidDifficulty();
108 Player::ConvertInstancesToGroup(leader, this, guid);
110 // store group in database
111 CharacterDatabase.BeginTransaction();
112 CharacterDatabase.PExecute("DELETE FROM groups WHERE groupId ='%u'", m_Id);
113 CharacterDatabase.PExecute("DELETE FROM group_member WHERE groupId ='%u'", m_Id);
114 CharacterDatabase.PExecute("INSERT INTO groups (groupId,leaderGuid,mainTank,mainAssistant,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,isRaid,difficulty,raiddifficulty) "
115 "VALUES ('%u','%u','%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u','%u')",
116 m_Id, GUID_LOPART(m_leaderGuid), GUID_LOPART(m_mainTank), GUID_LOPART(m_mainAssistant), uint32(m_lootMethod),
117 GUID_LOPART(m_looterGuid), uint32(m_lootThreshold), m_targetIcons[0], m_targetIcons[1], m_targetIcons[2], m_targetIcons[3], m_targetIcons[4], m_targetIcons[5], m_targetIcons[6], m_targetIcons[7], uint8(m_groupType), uint32(m_dungeonDifficulty), m_raidDifficulty);
120 if(!AddMember(guid, name))
121 return false;
123 if(!isBGGroup())
124 CharacterDatabase.CommitTransaction();
126 return true;
129 bool Group::LoadGroupFromDB(Field* fields)
131 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
132 // result = CharacterDatabase.Query("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, isRaid, difficulty, raiddifficulty, leaderGuid, groupId FROM groups");
134 m_Id = fields[17].GetUInt32();
135 m_leaderGuid = MAKE_NEW_GUID(fields[16].GetUInt32(),0,HIGHGUID_PLAYER);
137 // group leader not exist
138 if(!sObjectMgr.GetPlayerNameByGUID(m_leaderGuid, m_leaderName))
139 return false;
141 m_groupType = GroupType(fields[13].GetUInt8());
143 if (m_groupType & GROUPTYPE_RAID)
144 _initRaidSubGroupsCounter();
146 uint32 diff = fields[14].GetUInt8();
147 if (diff >= MAX_DUNGEON_DIFFICULTY)
148 diff = DUNGEON_DIFFICULTY_NORMAL;
149 m_dungeonDifficulty = Difficulty(diff);
151 uint32 r_diff = fields[15].GetUInt8();
152 if (r_diff >= MAX_RAID_DIFFICULTY)
153 r_diff = RAID_DIFFICULTY_10MAN_NORMAL;
154 m_raidDifficulty = Difficulty(r_diff);
156 m_mainTank = fields[0].GetUInt64();
157 m_mainAssistant = fields[1].GetUInt64();
158 m_lootMethod = (LootMethod)fields[2].GetUInt8();
159 m_looterGuid = MAKE_NEW_GUID(fields[3].GetUInt32(), 0, HIGHGUID_PLAYER);
160 m_lootThreshold = (ItemQualities)fields[4].GetUInt16();
162 for(int i = 0; i < TARGETICONCOUNT; ++i)
163 m_targetIcons[i] = fields[5+i].GetUInt64();
165 return true;
168 bool Group::LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant)
170 MemberSlot member;
171 member.guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
173 // skip non-existed member
174 if(!sObjectMgr.GetPlayerNameByGUID(member.guid, member.name))
175 return false;
177 member.group = subgroup;
178 member.assistant = assistant;
179 m_memberSlots.push_back(member);
181 SubGroupCounterIncrease(subgroup);
183 return true;
186 void Group::ConvertToRaid()
188 m_groupType = GroupType(m_groupType | GROUPTYPE_RAID);
190 _initRaidSubGroupsCounter();
192 if(!isBGGroup())
193 CharacterDatabase.PExecute("UPDATE groups SET isRaid = 1 WHERE groupId='%u'", m_Id);
194 SendUpdate();
196 // update quest related GO states (quest activity dependent from raid membership)
197 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
198 if(Player* player = sObjectMgr.GetPlayer(citr->guid))
199 player->UpdateForQuestWorldObjects();
202 bool Group::AddInvite(Player *player)
204 if( !player || player->GetGroupInvite() )
205 return false;
206 Group* group = player->GetGroup();
207 if( group && group->isBGGroup() )
208 group = player->GetOriginalGroup();
209 if( group )
210 return false;
212 RemoveInvite(player);
214 m_invitees.insert(player);
216 player->SetGroupInvite(this);
218 return true;
221 bool Group::AddLeaderInvite(Player *player)
223 if(!AddInvite(player))
224 return false;
226 m_leaderGuid = player->GetGUID();
227 m_leaderName = player->GetName();
228 return true;
231 uint32 Group::RemoveInvite(Player *player)
233 m_invitees.erase(player);
235 player->SetGroupInvite(NULL);
236 return GetMembersCount();
239 void Group::RemoveAllInvites()
241 for(InvitesList::iterator itr = m_invitees.begin(); itr!=m_invitees.end(); ++itr)
242 (*itr)->SetGroupInvite(NULL);
244 m_invitees.clear();
247 Player* Group::GetInvited(const uint64& guid) const
249 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
251 if((*itr)->GetGUID() == guid)
252 return (*itr);
254 return NULL;
257 Player* Group::GetInvited(const std::string& name) const
259 for(InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr)
261 if((*itr)->GetName() == name)
262 return (*itr);
264 return NULL;
267 bool Group::AddMember(const uint64 &guid, const char* name)
269 if(!_addMember(guid, name))
270 return false;
272 SendUpdate();
274 Player *player = sObjectMgr.GetPlayer(guid);
275 if(player)
277 if(!IsLeader(player->GetGUID()) && !isBGGroup())
279 // reset the new member's instances, unless he is currently in one of them
280 // including raid/heroic instances that they are not permanently bound to!
281 player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,false);
282 player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,true);
284 if (player->getLevel() >= LEVELREQUIREMENT_HEROIC)
286 if (player->GetDungeonDifficulty() != GetDungeonDifficulty())
288 player->SetDungeonDifficulty(GetDungeonDifficulty());
289 player->SendDungeonDifficulty(true);
291 if (player->GetRaidDifficulty() != GetRaidDifficulty())
293 player->SetRaidDifficulty(GetRaidDifficulty());
294 player->SendRaidDifficulty(true);
298 player->SetGroupUpdateFlag(GROUP_UPDATE_FULL);
299 UpdatePlayerOutOfRange(player);
301 // quest related GO state dependent from raid membership
302 if(isRaidGroup())
303 player->UpdateForQuestWorldObjects();
306 return true;
309 uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method)
311 // remove member and change leader (if need) only if strong more 2 members _before_ member remove
312 if(GetMembersCount() > uint32(isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group
314 bool leaderChanged = _removeMember(guid);
316 if(Player *player = sObjectMgr.GetPlayer( guid ))
318 // quest related GO state dependent from raid membership
319 if(isRaidGroup())
320 player->UpdateForQuestWorldObjects();
322 WorldPacket data;
324 if(method == 1)
326 data.Initialize( SMSG_GROUP_UNINVITE, 0 );
327 player->GetSession()->SendPacket( &data );
330 //we already removed player from group and in player->GetGroup() is his original group!
331 if( Group* group = player->GetGroup() )
333 group->SendUpdate();
335 else
337 data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8);
338 data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
339 data << uint64(0) << uint32(0) << uint32(0) << uint64(0);
340 player->GetSession()->SendPacket(&data);
343 _homebindIfInstance(player);
346 if(leaderChanged)
348 WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1));
349 data << m_memberSlots.front().name;
350 BroadcastPacket(&data, true);
353 SendUpdate();
355 // if group before remove <= 2 disband it
356 else
357 Disband(true);
359 return m_memberSlots.size();
362 void Group::ChangeLeader(const uint64 &guid)
364 member_citerator slot = _getMemberCSlot(guid);
366 if(slot == m_memberSlots.end())
367 return;
369 _setLeader(guid);
371 WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1);
372 data << slot->name;
373 BroadcastPacket(&data, true);
374 SendUpdate();
377 void Group::Disband(bool hideDestroy)
379 Player *player;
381 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
383 player = sObjectMgr.GetPlayer(citr->guid);
384 if(!player)
385 continue;
387 //we cannot call _removeMember because it would invalidate member iterator
388 //if we are removing player from battleground raid
389 if( isBGGroup() )
390 player->RemoveFromBattleGroundRaid();
391 else
393 //we can remove player who is in battleground from his original group
394 if( player->GetOriginalGroup() == this )
395 player->SetOriginalGroup(NULL);
396 else
397 player->SetGroup(NULL);
400 // quest related GO state dependent from raid membership
401 if(isRaidGroup())
402 player->UpdateForQuestWorldObjects();
404 if(!player->GetSession())
405 continue;
407 WorldPacket data;
408 if(!hideDestroy)
410 data.Initialize(SMSG_GROUP_DESTROYED, 0);
411 player->GetSession()->SendPacket(&data);
414 //we already removed player from group and in player->GetGroup() is his original group, send update
415 if( Group* group = player->GetGroup() )
417 group->SendUpdate();
419 else
421 data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8);
422 data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0);
423 data << uint64(0) << uint32(0) << uint32(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 groupId='%u'", m_Id);
438 CharacterDatabase.PExecute("DELETE FROM group_member WHERE groupId='%u'", m_Id);
439 CharacterDatabase.CommitTransaction();
440 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, NULL);
441 ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, NULL);
444 m_leaderGuid = 0;
445 m_leaderName = "";
448 /*********************************************************/
449 /*** LOOT SYSTEM ***/
450 /*********************************************************/
452 void Group::SendLootStartRoll(uint32 CountDown, uint32 mapid, const Roll &r)
454 WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4+4+1));
455 data << r.lootedTargetGUID; // creature guid what we're looting
456 data << uint32(mapid); // 3.3.3 mapid
457 data << uint32(r.itemSlot); // item slot in loot
458 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
459 data << uint32(r.itemRandomSuffix); // randomSuffix
460 data << uint32(r.itemRandomPropId); // item random property ID
461 data << uint32(r.itemCount); // items in stack
462 data << uint32(CountDown); // the countdown time to choose "need" or "greed"
463 data << uint8(ALL_ROLL_VOTE_MASK); // roll type mask, allowed choises
465 for (Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
467 Player *p = sObjectMgr.GetPlayer(itr->first);
468 if(!p || !p->GetSession())
469 continue;
471 if(itr->second != ROLL_NOT_VALID)
472 p->GetSession()->SendPacket( &data );
476 void Group::SendLootRoll(ObjectGuid const& targetGuid, uint8 rollNumber, uint8 rollType, const Roll &r)
478 WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1+1));
479 data << r.lootedTargetGUID; // creature guid what we're looting
480 data << uint32(r.itemSlot); // unknown, maybe amount of players, or item slot in loot
481 data << targetGuid;
482 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
483 data << uint32(r.itemRandomSuffix); // randomSuffix
484 data << uint32(r.itemRandomPropId); // Item random property ID
485 data << uint8(rollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number
486 data << uint8(rollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
487 data << uint8(0); // auto pass on loot
489 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
491 Player *p = sObjectMgr.GetPlayer(itr->first);
492 if(!p || !p->GetSession())
493 continue;
495 if(itr->second != ROLL_NOT_VALID)
496 p->GetSession()->SendPacket( &data );
500 void Group::SendLootRollWon(ObjectGuid const& targetGuid, uint8 rollNumber, RollVote rollType, const Roll &r)
502 WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
503 data << r.lootedTargetGUID; // creature guid what we're looting
504 data << uint32(r.itemSlot); // item slot in loot
505 data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for
506 data << uint32(r.itemRandomSuffix); // randomSuffix
507 data << uint32(r.itemRandomPropId); // Item random property
508 data << targetGuid; // guid of the player who won.
509 data << uint8(rollNumber); // rollnumber related to SMSG_LOOT_ROLL
510 data << uint8(rollType); // Rolltype related to SMSG_LOOT_ROLL
512 for( Roll::PlayerVote::const_iterator itr = r.playerVote.begin(); itr != r.playerVote.end(); ++itr)
514 Player *p = sObjectMgr.GetPlayer(itr->first);
515 if(!p || !p->GetSession())
516 continue;
518 if(itr->second != ROLL_NOT_VALID)
519 p->GetSession()->SendPacket( &data );
523 void Group::SendLootAllPassed(Roll const& r)
525 WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
526 data << r.lootedTargetGUID; // creature guid what we're looting
527 data << uint32(r.itemSlot); // item slot in loot
528 data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for
529 data << uint32(r.itemRandomPropId); // Item random property ID
530 data << uint32(r.itemRandomSuffix); // Item random suffix ID
532 for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
534 Player *p = sObjectMgr.GetPlayer(itr->first);
535 if(!p || !p->GetSession())
536 continue;
538 if(itr->second != ROLL_NOT_VALID)
539 p->GetSession()->SendPacket( &data );
543 void Group::GroupLoot(Creature *creature, Loot *loot)
545 for(uint8 itemSlot = 0; itemSlot < loot->items.size(); ++itemSlot)
547 LootItem& lootItem = loot->items[itemSlot];
548 ItemPrototype const *itemProto = ObjectMgr::GetItemPrototype(lootItem.itemid);
549 if (!itemProto)
551 DEBUG_LOG("Group::GroupLoot: missing item prototype for item with id: %d", lootItem.itemid);
552 continue;
555 //roll for over-threshold item if it's one-player loot
556 if (itemProto->Quality >= uint32(m_lootThreshold) && !lootItem.freeforall)
557 StartLootRool(creature,loot,itemSlot,false);
558 else
559 lootItem.is_underthreshold = 1;
563 void Group::NeedBeforeGreed(Creature *creature, Loot *loot)
565 for(uint8 itemSlot = 0; itemSlot < loot->items.size(); ++itemSlot)
567 LootItem& lootItem = loot->items[itemSlot];
568 ItemPrototype const *itemProto = ObjectMgr::GetItemPrototype(lootItem.itemid);
569 if (!itemProto)
571 DEBUG_LOG("Group::NeedBeforeGreed: missing item prototype for item with id: %d", lootItem.itemid);
572 continue;
575 //only roll for one-player items, not for ones everyone can get
576 if (itemProto->Quality >= uint32(m_lootThreshold) && !lootItem.freeforall)
577 StartLootRool(creature, loot, itemSlot, true);
578 else
579 lootItem.is_underthreshold = 1;
583 void Group::MasterLoot(Creature *creature, Loot* /*loot*/)
585 uint32 real_count = 0;
587 WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
588 data << uint8(GetMembersCount());
590 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
592 Player *looter = itr->getSource();
593 if (!looter->IsInWorld())
594 continue;
596 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
598 data << uint64(looter->GetGUID());
599 ++real_count;
603 data.put<uint8>(0, real_count);
605 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
607 Player *looter = itr->getSource();
608 if (looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
609 looter->GetSession()->SendPacket(&data);
613 void Group::CountRollVote(ObjectGuid const& playerGUID, ObjectGuid const& lootedTarget, uint32 itemSlot, RollVote choise)
615 Rolls::iterator rollI = RollId.begin();
616 for (; rollI != RollId.end(); ++rollI)
617 if ((*rollI)->isValid() && (*rollI)->lootedTargetGUID == lootedTarget && (*rollI)->itemSlot == itemSlot)
618 break;
620 if (rollI == RollId.end())
621 return;
623 CountRollVote(playerGUID, rollI, choise);
626 bool Group::CountRollVote(ObjectGuid const& playerGUID, Rolls::iterator& rollI, RollVote choise)
628 Roll* roll = *rollI;
630 Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID.GetRawValue());
631 // this condition means that player joins to the party after roll begins
632 if (itr == roll->playerVote.end())
633 return true; // result used for need iterator ++, so avoid for end of list
635 if (roll->getLoot())
636 if (roll->getLoot()->items.empty())
637 return false;
639 switch (choise)
641 case ROLL_PASS: // Player choose pass
643 SendLootRoll(playerGUID, 128, 128, *roll);
644 ++roll->totalPass;
645 itr->second = ROLL_PASS;
646 break;
648 case ROLL_NEED: // player choose Need
650 SendLootRoll(playerGUID, 0, 0, *roll);
651 ++roll->totalNeed;
652 itr->second = ROLL_NEED;
653 break;
655 case ROLL_GREED: // player choose Greed
657 SendLootRoll(playerGUID, 128, ROLL_GREED, *roll);
658 ++roll->totalGreed;
659 itr->second = ROLL_GREED;
660 break;
662 case ROLL_DISENCHANT: // player choose Disenchant
664 SendLootRoll(playerGUID, 128, ROLL_DISENCHANT, *roll);
665 ++roll->totalGreed;
666 itr->second = ROLL_DISENCHANT;
667 break;
669 default: // Roll removed case
670 break;
673 if (roll->totalPass + roll->totalNeed + roll->totalGreed >= roll->totalPlayersRolling)
675 CountTheRoll(rollI);
676 return true;
679 return false;
682 void Group::StartLootRool(Creature* lootTarget, Loot* loot, uint8 itemSlot, bool skipIfCanNotUse)
684 if (itemSlot >= loot->items.size())
685 return;
687 LootItem const& lootItem = loot->items[itemSlot];
689 ItemPrototype const* item = ObjectMgr::GetItemPrototype(lootItem.itemid);
691 Roll* r = new Roll(lootTarget->GetGUID(), lootItem);
693 //a vector is filled with only near party members
694 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
696 Player *playerToRoll = itr->getSource();
697 if(!playerToRoll || !playerToRoll->GetSession())
698 continue;
700 if ((!skipIfCanNotUse || playerToRoll->CanUseItem(item)) && lootItem.AllowedForPlayer(playerToRoll) )
702 if (playerToRoll->IsWithinDist(lootTarget, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
704 r->playerVote[playerToRoll->GetGUID()] = ROLL_NOT_EMITED_YET;
705 ++r->totalPlayersRolling;
710 if (r->totalPlayersRolling > 0) // has looters
712 r->setLoot(loot);
713 r->itemSlot = itemSlot;
715 if (r->totalPlayersRolling == 1) // single looter
716 r->playerVote.begin()->second = ROLL_NEED;
717 else
719 SendLootStartRoll(LOOT_ROLL_TIMEOUT, lootTarget->GetMapId(), *r);
720 loot->items[itemSlot].is_blocked = true;
721 lootTarget->StartGroupLoot(this,LOOT_ROLL_TIMEOUT);
724 RollId.push_back(r);
726 else // no looters??
727 delete r;
730 // called when roll timer expires
731 void Group::EndRoll()
733 while(!RollId.empty())
735 //need more testing here, if rolls disappear
736 Rolls::iterator itr = RollId.begin();
737 CountTheRoll(itr); //i don't have to edit player votes, who didn't vote ... he will pass
741 void Group::CountTheRoll(Rolls::iterator& rollI)
743 Roll* roll = *rollI;
744 if(!roll->isValid()) // is loot already deleted ?
746 rollI = RollId.erase(rollI);
747 delete roll;
748 return;
751 //end of the roll
752 if (roll->totalNeed > 0)
754 if(!roll->playerVote.empty())
756 uint8 maxresul = 0;
757 ObjectGuid maxguid = (*roll->playerVote.begin()).first;
758 Player *player;
760 for( Roll::PlayerVote::const_iterator itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
762 if (itr->second != ROLL_NEED)
763 continue;
765 uint8 randomN = urand(1, 99);
766 SendLootRoll(itr->first, randomN, ROLL_NEED, *roll);
767 if (maxresul < randomN)
769 maxguid = itr->first;
770 maxresul = randomN;
773 SendLootRollWon(maxguid, maxresul, ROLL_NEED, *roll);
774 player = sObjectMgr.GetPlayer(maxguid);
776 if(player && player->GetSession())
778 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul);
780 ItemPosCountVec dest;
781 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
782 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
783 if ( msg == EQUIP_ERR_OK )
785 item->is_looted = true;
786 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
787 --roll->getLoot()->unlootedCount;
788 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
790 else
792 item->is_blocked = false;
793 player->SendEquipError( msg, NULL, NULL, roll->itemid );
798 else if (roll->totalGreed > 0)
800 if(!roll->playerVote.empty())
802 uint8 maxresul = 0;
803 uint64 maxguid = (*roll->playerVote.begin()).first;
804 Player *player;
805 RollVote rollvote = ROLL_PASS; //Fixed: Using uninitialized memory 'rollvote'
807 Roll::PlayerVote::iterator itr;
808 for (itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr)
810 if (itr->second != ROLL_GREED && itr->second != ROLL_DISENCHANT)
811 continue;
813 uint8 randomN = urand(1, 99);
814 SendLootRoll(itr->first, randomN, itr->second, *roll);
815 if (maxresul < randomN)
817 maxguid = itr->first;
818 maxresul = randomN;
819 rollvote = itr->second;
822 SendLootRollWon(maxguid, maxresul, rollvote, *roll);
823 player = sObjectMgr.GetPlayer(maxguid);
825 if(player && player->GetSession())
827 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul);
829 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
831 if(rollvote == ROLL_GREED)
833 ItemPosCountVec dest;
834 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
835 if ( msg == EQUIP_ERR_OK )
837 item->is_looted = true;
838 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
839 --roll->getLoot()->unlootedCount;
840 player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
842 else
844 item->is_blocked = false;
845 player->SendEquipError( msg, NULL, NULL, roll->itemid );
848 else if(rollvote == ROLL_DISENCHANT)
850 item->is_looted = true;
851 roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
852 --roll->getLoot()->unlootedCount;
854 ItemPrototype const *pProto = ObjectMgr::GetItemPrototype(roll->itemid);
855 player->AutoStoreLoot(pProto->DisenchantID, LootTemplates_Disenchant, true);
860 else
862 SendLootAllPassed(*roll);
863 LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
864 if(item) item->is_blocked = false;
866 rollI = RollId.erase(rollI);
867 delete roll;
870 void Group::SetTargetIcon(uint8 id, uint64 whoGuid, uint64 targetGuid)
872 if(id >= TARGETICONCOUNT)
873 return;
875 // clean other icons
876 if( targetGuid != 0 )
877 for(int i = 0; i < TARGETICONCOUNT; ++i)
878 if( m_targetIcons[i] == targetGuid )
879 SetTargetIcon(i, 0, 0);
881 m_targetIcons[id] = targetGuid;
883 WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+8+1+8));
884 data << uint8(0); // set targets
885 data << uint64(whoGuid);
886 data << uint8(id);
887 data << uint64(targetGuid);
888 BroadcastPacket(&data, true);
891 static void GetDataForXPAtKill_helper(Player* player, Unit const* victim, uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level)
893 sum_level += player->getLevel();
894 if(!member_with_max_level || member_with_max_level->getLevel() < player->getLevel())
895 member_with_max_level = player;
897 uint32 gray_level = MaNGOS::XP::GetGrayLevel(player->getLevel());
898 if( victim->getLevel() > gray_level && (!not_gray_member_with_max_level
899 || not_gray_member_with_max_level->getLevel() < player->getLevel()))
900 not_gray_member_with_max_level = player;
903 void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level, Player* additional)
905 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
907 Player* member = itr->getSource();
908 if (!member || !member->isAlive()) // only for alive
909 continue;
911 // will proccesed later
912 if (member = additional)
913 continue;
915 if (!member->IsAtGroupRewardDistance(victim)) // at req. distance
916 continue;
918 ++count;
919 GetDataForXPAtKill_helper(member,victim,sum_level,member_with_max_level,not_gray_member_with_max_level);
922 if (additional)
924 if (additional->IsAtGroupRewardDistance(victim)) // at req. distance
926 ++count;
927 GetDataForXPAtKill_helper(additional,victim,sum_level,member_with_max_level,not_gray_member_with_max_level);
932 void Group::SendTargetIconList(WorldSession *session)
934 if(!session)
935 return;
937 WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
938 data << uint8(1); // list targets
940 for(int i = 0; i < TARGETICONCOUNT; ++i)
942 if(m_targetIcons[i] == 0)
943 continue;
945 data << uint8(i);
946 data << uint64(m_targetIcons[i]);
949 session->SendPacket(&data);
952 void Group::SendUpdate()
954 Player *player;
956 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
958 player = sObjectMgr.GetPlayer(citr->guid);
959 if(!player || !player->GetSession() || player->GetGroup() != this )
960 continue;
961 // guess size
962 WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+8+4+GetMembersCount()*20));
963 data << uint8(m_groupType); // group type (flags in 3.3)
964 data << uint8(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup?
965 data << uint8(citr->group); // groupid
966 data << uint8(citr->assistant ? 0x01 : 0x00); // 0x2 main assist, 0x4 main tank
967 if(m_groupType & GROUPTYPE_LFD)
969 data << uint8(0);
970 data << uint32(0);
972 data << uint64(0x50000000FFFFFFFELL); // related to voice chat?
973 data << uint32(0); // 3.3, this value increments every time SMSG_GROUP_LIST is sent
974 data << uint32(GetMembersCount()-1);
975 for(member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
977 if(citr->guid == citr2->guid)
978 continue;
979 Player* member = sObjectMgr.GetPlayer(citr2->guid);
980 uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE;
981 onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0);
983 data << citr2->name;
984 data << uint64(citr2->guid);
985 // online-state
986 data << uint8(onlineState);
987 data << uint8(citr2->group); // groupid
988 data << uint8(citr2->assistant?0x01:0); // 0x2 main assist, 0x4 main tank
989 data << uint8(0); // 3.3, role?
992 data << uint64(m_leaderGuid); // leader guid
993 if(GetMembersCount()-1)
995 data << uint8(m_lootMethod); // loot method
996 data << uint64(m_looterGuid); // looter guid
997 data << uint8(m_lootThreshold); // loot threshold
998 data << uint8(m_dungeonDifficulty); // Dungeon Difficulty
999 data << uint8(m_raidDifficulty); // Raid Difficulty
1000 data << uint8(0); // 3.3, dynamic difficulty?
1002 player->GetSession()->SendPacket( &data );
1006 void Group::UpdatePlayerOutOfRange(Player* pPlayer)
1008 if(!pPlayer || !pPlayer->IsInWorld())
1009 return;
1011 Player *player;
1012 WorldPacket data;
1013 pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
1015 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1017 player = itr->getSource();
1018 if (player && player != pPlayer && !pPlayer->isVisibleFor(player,player->GetViewPoint()))
1019 player->GetSession()->SendPacket(&data);
1023 void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore)
1025 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1027 Player *pl = itr->getSource();
1028 if(!pl || (ignore != 0 && pl->GetGUID() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this) )
1029 continue;
1031 if (pl->GetSession() && (group == -1 || itr->getSubGroup() == group))
1032 pl->GetSession()->SendPacket(packet);
1036 void Group::BroadcastReadyCheck(WorldPacket *packet)
1038 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1040 Player *pl = itr->getSource();
1041 if(pl && pl->GetSession())
1042 if(IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
1043 pl->GetSession()->SendPacket(packet);
1047 void Group::OfflineReadyCheck()
1049 for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
1051 Player *pl = sObjectMgr.GetPlayer(citr->guid);
1052 if (!pl || !pl->GetSession())
1054 WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
1055 data << uint64(citr->guid);
1056 data << uint8(0);
1057 BroadcastReadyCheck(&data);
1062 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant)
1064 // get first not-full group
1065 uint8 groupid = 0;
1066 if (m_subGroupsCounts)
1068 bool groupFound = false;
1069 for (; groupid < MAXRAIDSIZE / MAXGROUPSIZE; ++groupid)
1071 if (m_subGroupsCounts[groupid] < MAXGROUPSIZE)
1073 groupFound = true;
1074 break;
1077 // We are raid group and no one slot is free
1078 if (!groupFound)
1079 return false;
1082 return _addMember(guid, name, isAssistant, groupid);
1085 bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group)
1087 if(IsFull())
1088 return false;
1090 if(!guid)
1091 return false;
1093 Player *player = sObjectMgr.GetPlayer(guid);
1095 MemberSlot member;
1096 member.guid = guid;
1097 member.name = name;
1098 member.group = group;
1099 member.assistant = isAssistant;
1100 m_memberSlots.push_back(member);
1102 SubGroupCounterIncrease(group);
1104 if(player)
1106 player->SetGroupInvite(NULL);
1107 //if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid()
1108 if( player->GetGroup() && isBGGroup() )
1109 player->SetBattleGroundRaid(this, group);
1110 //if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup()
1111 else if ( player->GetGroup() )
1112 player->SetOriginalGroup(this, group);
1113 //if player is not in group, then call set group
1114 else
1115 player->SetGroup(this, group);
1116 // if the same group invites the player back, cancel the homebind timer
1117 InstanceGroupBind *bind = GetBoundInstance(player);
1118 if(bind && bind->save->GetInstanceId() == player->GetInstanceId())
1119 player->m_InstanceValid = true;
1122 if(!isRaidGroup()) // reset targetIcons for non-raid-groups
1124 for(int i = 0; i < TARGETICONCOUNT; ++i)
1125 m_targetIcons[i] = 0;
1128 if(!isBGGroup())
1130 // insert into group table
1131 CharacterDatabase.PExecute("INSERT INTO group_member(groupId,memberGuid,assistant,subgroup) VALUES('%u','%u','%u','%u')", m_Id, GUID_LOPART(member.guid), ((member.assistant==1)?1:0), member.group);
1134 return true;
1137 bool Group::_removeMember(const uint64 &guid)
1139 Player *player = sObjectMgr.GetPlayer(guid);
1140 if (player)
1142 //if we are removing player from battleground raid
1143 if( isBGGroup() )
1144 player->RemoveFromBattleGroundRaid();
1145 else
1147 //we can remove player who is in battleground from his original group
1148 if( player->GetOriginalGroup() == this )
1149 player->SetOriginalGroup(NULL);
1150 else
1151 player->SetGroup(NULL);
1155 _removeRolls(guid);
1157 member_witerator slot = _getMemberWSlot(guid);
1158 if (slot != m_memberSlots.end())
1160 SubGroupCounterDecrease(slot->group);
1162 m_memberSlots.erase(slot);
1165 if(!isBGGroup())
1166 CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", GUID_LOPART(guid));
1168 if(m_leaderGuid == guid) // leader was removed
1170 if(GetMembersCount() > 0)
1171 _setLeader(m_memberSlots.front().guid);
1172 return true;
1175 return false;
1178 void Group::_setLeader(const uint64 &guid)
1180 member_citerator slot = _getMemberCSlot(guid);
1181 if(slot == m_memberSlots.end())
1182 return;
1184 if(!isBGGroup())
1186 // TODO: set a time limit to have this function run rarely cause it can be slow
1187 CharacterDatabase.BeginTransaction();
1189 // update the group's bound instances when changing leaders
1191 // remove all permanent binds from the group
1192 // in the DB also remove solo binds that will be replaced with permbinds
1193 // from the new leader
1194 CharacterDatabase.PExecute(
1195 "DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
1196 "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
1197 ")", GUID_LOPART(m_leaderGuid), GUID_LOPART(slot->guid)
1200 Player *player = sObjectMgr.GetPlayer(slot->guid);
1201 if(player)
1203 for(uint8 i = 0; i < MAX_DIFFICULTY; ++i)
1205 for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
1207 if(itr->second.perm)
1209 itr->second.save->RemoveGroup(this);
1210 m_boundInstances[i].erase(itr++);
1212 else
1213 ++itr;
1218 // update the group's solo binds to the new leader
1219 CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1221 // copy the permanent binds from the new leader to the group
1222 // overwriting the solo binds with permanent ones if necessary
1223 // in the DB those have been deleted already
1224 Player::ConvertInstancesToGroup(player, this, slot->guid);
1226 // update the group leader
1227 CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE groupId='%u'", GUID_LOPART(slot->guid), m_Id);
1228 CharacterDatabase.CommitTransaction();
1231 m_leaderGuid = slot->guid;
1232 m_leaderName = slot->name;
1235 void Group::_removeRolls(const uint64 &guid)
1237 for (Rolls::iterator it = RollId.begin(); it != RollId.end(); )
1239 Roll* roll = *it;
1240 Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
1241 if(itr2 == roll->playerVote.end())
1243 ++it;
1244 continue;
1247 if (itr2->second == ROLL_GREED || itr2->second == ROLL_DISENCHANT)
1248 --roll->totalGreed;
1249 if (itr2->second == ROLL_NEED)
1250 --roll->totalNeed;
1251 if (itr2->second == ROLL_PASS)
1252 --roll->totalPass;
1253 if (itr2->second != ROLL_NOT_VALID)
1254 --roll->totalPlayersRolling;
1256 roll->playerVote.erase(itr2);
1258 if (!CountRollVote(guid, it, ROLL_NOT_EMITED_YET))
1259 ++it;
1263 bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group)
1265 member_witerator slot = _getMemberWSlot(guid);
1266 if(slot == m_memberSlots.end())
1267 return false;
1269 slot->group = group;
1271 SubGroupCounterIncrease(group);
1273 if(!isBGGroup())
1274 CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid));
1276 return true;
1279 bool Group::_setAssistantFlag(const uint64 &guid, const bool &state)
1281 member_witerator slot = _getMemberWSlot(guid);
1282 if(slot == m_memberSlots.end())
1283 return false;
1285 slot->assistant = state;
1286 if(!isBGGroup())
1287 CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state==true)?1:0, GUID_LOPART(guid));
1288 return true;
1291 bool Group::_setMainTank(const uint64 &guid)
1293 member_citerator slot = _getMemberCSlot(guid);
1294 if(slot == m_memberSlots.end())
1295 return false;
1297 if(m_mainAssistant == guid)
1298 _setMainAssistant(0);
1299 m_mainTank = guid;
1300 if(!isBGGroup())
1301 CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE groupId='%u'", GUID_LOPART(m_mainTank), m_Id);
1302 return true;
1305 bool Group::_setMainAssistant(const uint64 &guid)
1307 member_witerator slot = _getMemberWSlot(guid);
1308 if(slot == m_memberSlots.end())
1309 return false;
1311 if(m_mainTank == guid)
1312 _setMainTank(0);
1313 m_mainAssistant = guid;
1314 if(!isBGGroup())
1315 CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE groupId='%u'", GUID_LOPART(m_mainAssistant), m_Id);
1316 return true;
1319 bool Group::SameSubGroup(Player const* member1, Player const* member2) const
1321 if(!member1 || !member2)
1322 return false;
1323 if (member1->GetGroup() != this || member2->GetGroup() != this)
1324 return false;
1325 else
1326 return member1->GetSubGroup() == member2->GetSubGroup();
1329 // allows setting subgroup for offline members
1330 void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group)
1332 if(!isRaidGroup())
1333 return;
1334 Player *player = sObjectMgr.GetPlayer(guid);
1336 if (!player)
1338 uint8 prevSubGroup;
1339 prevSubGroup = GetMemberGroup(guid);
1341 SubGroupCounterDecrease(prevSubGroup);
1343 if(_setMembersGroup(guid, group))
1344 SendUpdate();
1346 else
1347 // This methods handles itself groupcounter decrease
1348 ChangeMembersGroup(player, group);
1351 // only for online members
1352 void Group::ChangeMembersGroup(Player *player, const uint8 &group)
1354 if(!player || !isRaidGroup())
1355 return;
1356 if(_setMembersGroup(player->GetGUID(), group))
1358 uint8 prevSubGroup = player->GetSubGroup();
1359 if( player->GetGroup() == this )
1360 player->GetGroupRef().setSubGroup(group);
1361 //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
1362 else
1364 prevSubGroup = player->GetOriginalSubGroup();
1365 player->GetOriginalGroupRef().setSubGroup(group);
1367 SubGroupCounterDecrease(prevSubGroup);
1369 SendUpdate();
1373 void Group::UpdateLooterGuid( Creature* creature, bool ifneed )
1375 switch (GetLootMethod())
1377 case MASTER_LOOT:
1378 case FREE_FOR_ALL:
1379 return;
1380 default:
1381 // round robin style looting applies for all low
1382 // quality items in each loot method except free for all and master loot
1383 break;
1386 member_citerator guid_itr = _getMemberCSlot(GetLooterGuid());
1387 if(guid_itr != m_memberSlots.end())
1389 if(ifneed)
1391 // not update if only update if need and ok
1392 Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
1393 if(looter && looter->IsWithinDist(creature, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
1394 return;
1396 ++guid_itr;
1399 // search next after current
1400 if(guid_itr != m_memberSlots.end())
1402 for(member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
1404 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1406 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
1408 bool refresh = pl->GetLootGUID() == creature->GetGUID();
1410 //if(refresh) // update loot for new looter
1411 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1412 SetLooterGuid(pl->GetGUID());
1413 SendUpdate();
1414 if(refresh) // update loot for new looter
1415 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1416 return;
1422 // search from start
1423 for(member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
1425 if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1427 if (pl->IsWithinDist(creature, sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE), false))
1429 bool refresh = pl->GetLootGUID()==creature->GetGUID();
1431 //if(refresh) // update loot for new looter
1432 // pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1433 SetLooterGuid(pl->GetGUID());
1434 SendUpdate();
1435 if(refresh) // update loot for new looter
1436 pl->SendLoot(creature->GetGUID(), LOOT_CORPSE);
1437 return;
1442 SetLooterGuid(0);
1443 SendUpdate();
1446 GroupJoinBattlegroundResult Group::CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot)
1448 BattlemasterListEntry const* bgEntry = sBattlemasterListStore.LookupEntry(bgOrTemplate->GetTypeID());
1449 if(!bgEntry)
1450 return ERR_GROUP_JOIN_BATTLEGROUND_FAIL; // shouldn't happen
1452 // check for min / max count
1453 uint32 memberscount = GetMembersCount();
1455 // only check for MinPlayerCount since MinPlayerCount == MaxPlayerCount for arenas...
1456 if(bgOrTemplate->isArena() && memberscount != MinPlayerCount)
1457 return ERR_ARENA_TEAM_PARTY_SIZE;
1459 if(memberscount > bgEntry->maxGroupSize) // no MinPlayerCount for battlegrounds
1460 return ERR_BATTLEGROUND_NONE; // ERR_GROUP_JOIN_BATTLEGROUND_TOO_MANY handled on client side
1462 // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
1463 Player * reference = GetFirstMember()->getSource();
1464 // no reference found, can't join this way
1465 if(!reference)
1466 return ERR_BATTLEGROUND_JOIN_FAILED;
1468 PvPDifficultyEntry const* bracketEntry = GetBattlegroundBracketByLevel(bgOrTemplate->GetMapId(), reference->getLevel());
1469 if(!bracketEntry)
1470 return ERR_BATTLEGROUND_JOIN_FAILED;
1472 uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
1473 uint32 team = reference->GetTeam();
1475 // check every member of the group to be able to join
1476 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1478 Player *member = itr->getSource();
1479 // offline member? don't let join
1480 if(!member)
1481 return ERR_BATTLEGROUND_JOIN_FAILED;
1482 // don't allow cross-faction join as group
1483 if(member->GetTeam() != team)
1484 return ERR_BATTLEGROUND_JOIN_TIMED_OUT;
1485 // not in the same battleground level bracket, don't let join
1486 PvPDifficultyEntry const* memberBracketEntry = GetBattlegroundBracketByLevel(bracketEntry->mapId, member->getLevel());
1487 if(memberBracketEntry != bracketEntry)
1488 return ERR_BATTLEGROUND_JOIN_RANGE_INDEX;
1489 // don't let join rated matches if the arena team id doesn't match
1490 if(isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
1491 return ERR_BATTLEGROUND_JOIN_FAILED;
1492 // don't let join if someone from the group is already in that bg queue
1493 if(member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId))
1494 return ERR_BATTLEGROUND_JOIN_FAILED; // not blizz-like
1495 // check for deserter debuff in case not arena queue
1496 if(bgOrTemplate->GetTypeID() != BATTLEGROUND_AA && !member->CanJoinToBattleground())
1497 return ERR_GROUP_JOIN_BATTLEGROUND_DESERTERS;
1498 // check if member can join any more battleground queues
1499 if(!member->HasFreeBattleGroundQueueId())
1500 return ERR_BATTLEGROUND_TOO_MANY_QUEUES; // not blizz-like
1502 return GroupJoinBattlegroundResult(bgOrTemplate->GetTypeID());
1505 //===================================================
1506 //============== Roll ===============================
1507 //===================================================
1509 void Roll::targetObjectBuildLink()
1511 // called from link()
1512 getTarget()->addLootValidatorRef(this);
1515 void Group::SetDungeonDifficulty(Difficulty difficulty)
1517 m_dungeonDifficulty = difficulty;
1518 if(!isBGGroup())
1519 CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE groupId='%u'", m_dungeonDifficulty, m_Id);
1521 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1523 Player *player = itr->getSource();
1524 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1525 continue;
1526 player->SetDungeonDifficulty(difficulty);
1527 player->SendDungeonDifficulty(true);
1531 void Group::SetRaidDifficulty(Difficulty difficulty)
1533 m_raidDifficulty = difficulty;
1534 if(!isBGGroup())
1535 CharacterDatabase.PExecute("UPDATE groups SET raiddifficulty = %u WHERE groupId='%u'", m_raidDifficulty, m_Id);
1537 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1539 Player *player = itr->getSource();
1540 if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1541 continue;
1542 player->SetRaidDifficulty(difficulty);
1543 player->SendRaidDifficulty(true);
1547 bool Group::InCombatToInstance(uint32 instanceId)
1549 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1551 Player *pPlayer = itr->getSource();
1552 if(pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId)
1553 return true;
1555 return false;
1558 void Group::ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo)
1560 if(isBGGroup())
1561 return;
1563 // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
1565 // we assume that when the difficulty changes, all instances that can be reset will be
1566 Difficulty diff = GetDifficulty(isRaid);
1568 for(BoundInstancesMap::iterator itr = m_boundInstances[diff].begin(); itr != m_boundInstances[diff].end();)
1570 InstanceSave *p = itr->second.save;
1571 const MapEntry *entry = sMapStore.LookupEntry(itr->first);
1572 if (!entry || entry->IsRaid() != isRaid || (!p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND))
1574 ++itr;
1575 continue;
1578 if(method == INSTANCE_RESET_ALL)
1580 // the "reset all instances" method can only reset normal maps
1581 if (entry->map_type == MAP_RAID || diff == DUNGEON_DIFFICULTY_HEROIC)
1583 ++itr;
1584 continue;
1588 bool isEmpty = true;
1589 // if the map is loaded, reset it
1590 Map *map = sMapMgr.FindMap(p->GetMapId(), p->GetInstanceId());
1591 if(map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset()))
1592 isEmpty = ((InstanceMap*)map)->Reset(method);
1594 if(SendMsgTo)
1596 if(isEmpty)
1597 SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
1598 else
1599 SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
1602 if(isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
1604 // do not reset the instance, just unbind if others are permanently bound to it
1605 if(p->CanReset())
1606 p->DeleteFromDB();
1607 else
1608 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
1609 // i don't know for sure if hash_map iterators
1610 m_boundInstances[diff].erase(itr);
1611 itr = m_boundInstances[diff].begin();
1612 // this unloads the instance save unless online players are bound to it
1613 // (eg. permanent binds or GM solo binds)
1614 p->RemoveGroup(this);
1616 else
1617 ++itr;
1621 InstanceGroupBind* Group::GetBoundInstance(Player* player)
1623 uint32 mapid = player->GetMapId();
1624 MapEntry const* mapEntry = sMapStore.LookupEntry(mapid);
1625 if(!mapEntry)
1626 return NULL;
1628 Difficulty difficulty = player->GetDifficulty(mapEntry->IsRaid());
1630 // some instances only have one difficulty
1631 MapDifficulty const* mapDiff = GetMapDifficultyData(mapid,difficulty);
1632 if(!mapDiff)
1633 difficulty = DUNGEON_DIFFICULTY_NORMAL;
1635 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1636 if(itr != m_boundInstances[difficulty].end())
1637 return &itr->second;
1638 else
1639 return NULL;
1642 InstanceGroupBind* Group::GetBoundInstance(Map* aMap, Difficulty difficulty)
1644 // some instances only have one difficulty
1645 MapDifficulty const* mapDiff = GetMapDifficultyData(aMap->GetId(),difficulty);
1646 if(!mapDiff)
1647 return NULL;
1649 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(aMap->GetId());
1650 if(itr != m_boundInstances[difficulty].end())
1651 return &itr->second;
1652 else
1653 return NULL;
1656 InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
1658 if(save && !isBGGroup())
1660 InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
1661 if(bind.save)
1663 // when a boss is killed or when copying the players's binds to the group
1664 if(permanent != bind.perm || save != bind.save)
1665 if(!load)
1666 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());
1668 else if(!load)
1669 CharacterDatabase.PExecute("INSERT INTO group_instance (leaderGuid, instance, permanent) VALUES ('%u', '%u', '%u')", GUID_LOPART(GetLeaderGUID()), save->GetInstanceId(), permanent);
1671 if(bind.save != save)
1673 if(bind.save)
1674 bind.save->RemoveGroup(this);
1675 save->AddGroup(this);
1678 bind.save = save;
1679 bind.perm = permanent;
1680 if(!load)
1681 DEBUG_LOG("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetLeaderGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
1682 return &bind;
1684 else
1685 return NULL;
1688 void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
1690 BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1691 if(itr != m_boundInstances[difficulty].end())
1693 if(!unload)
1694 CharacterDatabase.PExecute("DELETE FROM group_instance WHERE leaderGuid = '%u' AND instance = '%u'", GUID_LOPART(GetLeaderGUID()), itr->second.save->GetInstanceId());
1695 itr->second.save->RemoveGroup(this); // save can become invalid
1696 m_boundInstances[difficulty].erase(itr);
1700 void Group::_homebindIfInstance(Player *player)
1702 if(player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
1704 // leaving the group in an instance, the homebind timer is started
1705 // unless the player is permanently saved to the instance
1706 InstanceSave *save = sInstanceSaveMgr.GetInstanceSave(player->GetInstanceId());
1707 InstancePlayerBind *playerBind = save ? player->GetBoundInstance(save->GetMapId(), save->GetDifficulty()) : NULL;
1708 if(!playerBind || !playerBind->perm)
1709 player->m_InstanceValid = false;
1713 static void RewardGroupAtKill_helper(Player* pGroupGuy, Unit* pVictim, uint32 count, bool PvP, float group_rate, uint32 sum_level, bool is_dungeon, Player* not_gray_member_with_max_level, Player* member_with_max_level, uint32 xp )
1715 // honor can be in PvP and !PvP (racial leader) cases (for alive)
1716 if (pGroupGuy->isAlive())
1717 pGroupGuy->RewardHonor(pVictim,count);
1719 // xp and reputation only in !PvP case
1720 if(!PvP)
1722 float rate = group_rate * float(pGroupGuy->getLevel()) / sum_level;
1724 // if is in dungeon then all receive full reputation at kill
1725 // rewarded any alive/dead/near_corpse group member
1726 pGroupGuy->RewardReputation(pVictim,is_dungeon ? 1.0f : rate);
1728 // XP updated only for alive group member
1729 if(pGroupGuy->isAlive() && not_gray_member_with_max_level &&
1730 pGroupGuy->getLevel() <= not_gray_member_with_max_level->getLevel())
1732 uint32 itr_xp = (member_with_max_level == not_gray_member_with_max_level) ? uint32(xp*rate) : uint32((xp*rate/2)+1);
1734 pGroupGuy->GiveXP(itr_xp, pVictim);
1735 if(Pet* pet = pGroupGuy->GetPet())
1736 pet->GivePetXP(itr_xp/2);
1739 // quest objectives updated only for alive group member or dead but with not released body
1740 if(pGroupGuy->isAlive()|| !pGroupGuy->HasFlag(PLAYER_FLAGS, PLAYER_FLAGS_GHOST))
1742 // normal creature (not pet/etc) can be only in !PvP case
1743 if(pVictim->GetTypeId()==TYPEID_UNIT)
1744 pGroupGuy->KilledMonster(((Creature*)pVictim)->GetCreatureInfo(), pVictim->GetObjectGuid());
1749 /** Provide rewards to group members at unit kill
1751 * @param pVictim Killed unit
1752 * @param player_tap Player who tap unit if online, it can be group member or can be not if leaved after tap but before kill target
1754 * Rewards received by group members and player_tap
1756 void Group::RewardGroupAtKill(Unit* pVictim, Player* player_tap)
1758 bool PvP = pVictim->isCharmedOwnedByPlayerOrPlayer();
1760 // prepare data for near group iteration (PvP and !PvP cases)
1761 uint32 xp = 0;
1763 uint32 count = 0;
1764 uint32 sum_level = 0;
1765 Player* member_with_max_level = NULL;
1766 Player* not_gray_member_with_max_level = NULL;
1768 GetDataForXPAtKill(pVictim,count,sum_level,member_with_max_level,not_gray_member_with_max_level,player_tap);
1770 if(member_with_max_level)
1772 /// not get Xp in PvP or no not gray players in group
1773 xp = (PvP || !not_gray_member_with_max_level) ? 0 : MaNGOS::XP::Gain(not_gray_member_with_max_level, pVictim);
1775 /// skip in check PvP case (for speed, not used)
1776 bool is_raid = PvP ? false : sMapStore.LookupEntry(pVictim->GetMapId())->IsRaid() && isRaidGroup();
1777 bool is_dungeon = PvP ? false : sMapStore.LookupEntry(pVictim->GetMapId())->IsDungeon();
1778 float group_rate = MaNGOS::XP::xp_in_group_rate(count,is_raid);
1780 for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1782 Player* pGroupGuy = itr->getSource();
1783 if(!pGroupGuy)
1784 continue;
1786 // will proccessed later
1787 if(pGroupGuy==player_tap)
1788 continue;
1790 if(!pGroupGuy->IsAtGroupRewardDistance(pVictim))
1791 continue; // member (alive or dead) or his corpse at req. distance
1793 RewardGroupAtKill_helper(pGroupGuy, pVictim, count, PvP, group_rate, sum_level, is_dungeon, not_gray_member_with_max_level, member_with_max_level, xp);
1796 if(player_tap)
1798 // member (alive or dead) or his corpse at req. distance
1799 if(player_tap->IsAtGroupRewardDistance(pVictim))
1800 RewardGroupAtKill_helper(player_tap, pVictim, count, PvP, group_rate, sum_level, is_dungeon, not_gray_member_with_max_level, member_with_max_level, xp);