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
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
25 #include "ObjectMgr.h"
26 #include "ObjectDefines.h"
29 #include "ObjectAccessor.h"
30 #include "BattleGround.h"
31 #include "MapManager.h"
32 #include "InstanceSaveMgr.h"
33 #include "MapInstanced.h"
41 m_groupType
= (GroupType
)0;
43 m_lootMethod
= (LootMethod
)0;
45 m_lootThreshold
= ITEM_QUALITY_UNCOMMON
;
46 m_subGroupsCounts
= NULL
;
48 for (int i
= 0; i
< TARGETICONCOUNT
; ++i
)
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
);
62 sLog
.outError("Group::~Group: battleground group is not linked to the correct battleground.");
65 while(!RollId
.empty())
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
)
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
;
99 m_dungeonDifficulty
= DUNGEON_DIFFICULTY_NORMAL
;
100 m_raidDifficulty
= RAID_DIFFICULTY_10MAN_NORMAL
;
103 Player
*leader
= sObjectMgr
.GetPlayer(guid
);
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
))
126 CharacterDatabase
.CommitTransaction();
131 bool Group::LoadGroupFromDB(const uint64
&leaderGuid
, QueryResult
*result
, bool loadMembers
)
136 bool external
= true;
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
));
146 m_leaderGuid
= leaderGuid
;
148 // group leader not exist
149 if(!sObjectMgr
.GetPlayerNameByGUID(m_leaderGuid
, m_leaderName
))
151 if(!external
) delete result
;
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();
183 result
= CharacterDatabase
.PQuery("SELECT memberGuid, assistant, subgroup FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid
));
189 LoadMemberFromDB((*result
)[0].GetUInt32(), (*result
)[2].GetUInt8(), (*result
)[1].GetBool());
190 } while( result
->NextRow() );
193 if(GetMembersCount() < 2)
200 bool Group::LoadMemberFromDB(uint32 guidLow
, uint8 subgroup
, bool assistant
)
203 member
.guid
= MAKE_NEW_GUID(guidLow
, 0, HIGHGUID_PLAYER
);
205 // skip non-existed member
206 if(!sObjectMgr
.GetPlayerNameByGUID(member
.guid
, member
.name
))
209 member
.group
= subgroup
;
210 member
.assistant
= assistant
;
211 m_memberSlots
.push_back(member
);
213 SubGroupCounterIncrease(subgroup
);
218 void Group::ConvertToRaid()
220 m_groupType
= GROUPTYPE_RAID
;
222 _initRaidSubGroupsCounter();
225 CharacterDatabase
.PExecute("UPDATE groups SET isRaid = 1 WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid
));
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() )
238 Group
* group
= player
->GetGroup();
239 if( group
&& group
->isBGGroup() )
240 group
= player
->GetOriginalGroup();
244 RemoveInvite(player
);
246 m_invitees
.insert(player
);
248 player
->SetGroupInvite(this);
253 bool Group::AddLeaderInvite(Player
*player
)
255 if(!AddInvite(player
))
258 m_leaderGuid
= player
->GetGUID();
259 m_leaderName
= player
->GetName();
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
);
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
)
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
)
299 bool Group::AddMember(const uint64
&guid
, const char* name
)
301 if(!_addMember(guid
, name
))
305 Player
*player
= sObjectMgr
.GetPlayer(guid
);
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
334 player
->UpdateForQuestWorldObjects();
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
351 player
->UpdateForQuestWorldObjects();
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() )
368 data
.Initialize(SMSG_GROUP_LIST
, 24);
369 data
<< uint64(0) << uint64(0) << uint64(0);
370 player
->GetSession()->SendPacket(&data
);
373 _homebindIfInstance(player
);
378 WorldPacket
data(SMSG_GROUP_SET_LEADER
, (m_memberSlots
.front().name
.size()+1));
379 data
<< m_memberSlots
.front().name
;
380 BroadcastPacket(&data
, true);
385 // if group before remove <= 2 disband it
389 return m_memberSlots
.size();
392 void Group::ChangeLeader(const uint64
&guid
)
394 member_citerator slot
= _getMemberCSlot(guid
);
396 if(slot
== m_memberSlots
.end())
401 WorldPacket
data(SMSG_GROUP_SET_LEADER
, slot
->name
.size()+1);
403 BroadcastPacket(&data
, true);
407 void Group::Disband(bool hideDestroy
)
411 for(member_citerator citr
= m_memberSlots
.begin(); citr
!= m_memberSlots
.end(); ++citr
)
413 player
= sObjectMgr
.GetPlayer(citr
->guid
);
417 //we cannot call _removeMember because it would invalidate member iterator
418 //if we are removing player from battleground raid
420 player
->RemoveFromBattleGroundRaid();
423 //we can remove player who is in battleground from his original group
424 if( player
->GetOriginalGroup() == this )
425 player
->SetOriginalGroup(NULL
);
427 player
->SetGroup(NULL
);
430 // quest related GO state dependent from raid membership
432 player
->UpdateForQuestWorldObjects();
434 if(!player
->GetSession())
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() )
451 data
.Initialize(SMSG_GROUP_LIST
, 24);
452 data
<< uint64(0) << uint64(0) << uint64(0);
453 player
->GetSession()->SendPacket(&data
);
456 _homebindIfInstance(player
);
459 m_memberSlots
.clear();
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
);
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())
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())
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())
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())
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
;
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
);
583 //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
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())
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
;
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();
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();
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())
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)
661 r
->itemSlot
= itemSlot
;
663 group
->SendLootStartRoll(60000, *r
);
665 loot
->items
[itemSlot
].is_blocked
= true;
675 i
->is_underthreshold
= 1;
679 void Group::MasterLoot(const uint64
& playerGUID
, Loot
* /*loot*/, Creature
*creature
)
681 Player
*player
= sObjectMgr
.GetPlayer(playerGUID
);
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())
698 if (looter
->IsWithinDist(creature
, sWorld
.getConfig(CONFIG_GROUP_XP_DISTANCE
), false))
700 data
<< looter
->GetGUID();
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())
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())
728 if (roll
->getLoot()->items
.empty())
733 case ROLL_PASS
: // Player choose pass
735 SendLootRoll(0, playerGUID
, 0, ROLL_PASS
, *roll
);
740 case ROLL_NEED
: // player choose Need
742 SendLootRoll(0, playerGUID
, 0, ROLL_NEED
, *roll
);
747 case ROLL_GREED
: // player choose Greed
749 SendLootRoll(0, playerGUID
, 128, ROLL_GREED
, *roll
);
755 if (roll
->totalPass
+ roll
->totalGreed
+ roll
->totalNeed
>= roll
->totalPlayersRolling
)
757 CountTheRoll(rollI
, NumberOfPlayers
);
761 //called when roll timer expires
762 void Group::EndRoll()
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
)
776 if(!roll
->isValid()) // is loot already deleted ?
783 if (roll
->totalNeed
> 0)
785 if(!roll
->playerVote
.empty())
788 uint64 maxguid
= (*roll
->playerVote
.begin()).first
;
791 for( Roll::PlayerVote::const_iterator itr
= roll
->playerVote
.begin(); itr
!= roll
->playerVote
.end(); ++itr
)
793 if (itr
->second
!= NEED
)
796 uint8 randomN
= urand(1, 99);
797 SendLootRoll(0, itr
->first
, randomN
, ROLL_NEED
, *roll
);
798 if (maxresul
< randomN
)
800 maxguid
= itr
->first
;
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
);
823 item
->is_blocked
= false;
824 player
->SendEquipError( msg
, NULL
, NULL
);
829 else if (roll
->totalGreed
> 0)
831 if(!roll
->playerVote
.empty())
834 uint64 maxguid
= (*roll
->playerVote
.begin()).first
;
837 Roll::PlayerVote::iterator itr
;
838 for (itr
= roll
->playerVote
.begin(); itr
!= roll
->playerVote
.end(); ++itr
)
840 if (itr
->second
!= GREED
)
843 uint8 randomN
= urand(1, 99);
844 SendLootRoll(0, itr
->first
, randomN
, ROLL_GREED
, *roll
);
845 if (maxresul
< randomN
)
847 maxguid
= itr
->first
;
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
);
870 item
->is_blocked
= false;
871 player
->SendEquipError( msg
, NULL
, NULL
);
878 SendLootAllPassed(NumberOfPlayers
, *roll
);
879 LootItem
*item
= &(roll
->getLoot()->items
[roll
->itemSlot
]);
880 if(item
) item
->is_blocked
= false;
886 void Group::SetTargetIcon(uint8 id
, uint64 guid
)
888 if(id
>= TARGETICONCOUNT
)
893 for(int i
= 0; i
< TARGETICONCOUNT
; ++i
)
894 if( m_targetIcons
[i
] == guid
)
897 m_targetIcons
[id
] = guid
;
899 WorldPacket
data(MSG_RAID_TARGET_UPDATE
, (2+8));
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
914 if(!member
->IsAtGroupRewardDistance(victim
)) // at req. distance
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
)
934 WorldPacket
data(MSG_RAID_TARGET_UPDATE
, (1+TARGETICONCOUNT
*9));
937 for(int i
= 0; i
< TARGETICONCOUNT
; ++i
)
939 if(m_targetIcons
[i
] == 0)
943 data
<< m_targetIcons
[i
];
946 session
->SendPacket(&data
);
949 void Group::SendUpdate()
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 )
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
)
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);
975 data
<< (uint64
)citr2
->guid
;
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())
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) )
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);
1046 BroadcastReadyCheck(&data
);
1051 bool Group::_addMember(const uint64
&guid
, const char* name
, bool isAssistant
)
1053 // get first not-full group
1055 if (m_subGroupsCounts
)
1057 bool groupFound
= false;
1058 for (; groupid
< MAXRAIDSIZE
/ MAXGROUPSIZE
; ++groupid
)
1060 if (m_subGroupsCounts
[groupid
] < MAXGROUPSIZE
)
1066 // We are raid group and no one slot is free
1071 return _addMember(guid
, name
, isAssistant
, groupid
);
1074 bool Group::_addMember(const uint64
&guid
, const char* name
, bool isAssistant
, uint8 group
)
1082 Player
*player
= sObjectMgr
.GetPlayer(guid
);
1087 member
.group
= group
;
1088 member
.assistant
= isAssistant
;
1089 m_memberSlots
.push_back(member
);
1091 SubGroupCounterIncrease(group
);
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
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;
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
);
1126 bool Group::_removeMember(const uint64
&guid
)
1128 Player
*player
= sObjectMgr
.GetPlayer(guid
);
1131 //if we are removing player from battleground raid
1133 player
->RemoveFromBattleGroundRaid();
1136 //we can remove player who is in battleground from his original group
1137 if( player
->GetOriginalGroup() == this )
1138 player
->SetOriginalGroup(NULL
);
1140 player
->SetGroup(NULL
);
1146 member_witerator slot
= _getMemberWSlot(guid
);
1147 if (slot
!= m_memberSlots
.end())
1149 SubGroupCounterDecrease(slot
->group
);
1151 m_memberSlots
.erase(slot
);
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
);
1167 void Group::_setLeader(const uint64
&guid
)
1169 member_citerator slot
= _getMemberCSlot(guid
);
1170 if(slot
==m_memberSlots
.end())
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
);
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
++);
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
)
1230 Roll::PlayerVote::iterator itr2
= roll
->playerVote
.find(guid
);
1231 if(itr2
== roll
->playerVote
.end())
1234 if (itr2
->second
== GREED
)
1236 if (itr2
->second
== NEED
)
1238 if (itr2
->second
== PASS
)
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())
1255 slot
->group
= group
;
1257 SubGroupCounterIncrease(group
);
1260 CharacterDatabase
.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group
, GUID_LOPART(guid
));
1265 bool Group::_setAssistantFlag(const uint64
&guid
, const bool &state
)
1267 member_witerator slot
= _getMemberWSlot(guid
);
1268 if(slot
== m_memberSlots
.end())
1271 slot
->assistant
= state
;
1273 CharacterDatabase
.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state
==true)?1:0, GUID_LOPART(guid
));
1277 bool Group::_setMainTank(const uint64
&guid
)
1279 member_citerator slot
= _getMemberCSlot(guid
);
1280 if(slot
== m_memberSlots
.end())
1283 if(m_mainAssistant
== guid
)
1284 _setMainAssistant(0);
1287 CharacterDatabase
.PExecute("UPDATE groups SET mainTank='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainTank
), GUID_LOPART(m_leaderGuid
));
1291 bool Group::_setMainAssistant(const uint64
&guid
)
1293 member_witerator slot
= _getMemberWSlot(guid
);
1294 if(slot
== m_memberSlots
.end())
1297 if(m_mainTank
== guid
)
1299 m_mainAssistant
= guid
;
1301 CharacterDatabase
.PExecute("UPDATE groups SET mainAssistant='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainAssistant
), GUID_LOPART(m_leaderGuid
));
1305 bool Group::SameSubGroup(Player
const* member1
, Player
const* member2
) const
1307 if(!member1
|| !member2
)
1309 if (member1
->GetGroup() != this || member2
->GetGroup() != this)
1312 return member1
->GetSubGroup() == member2
->GetSubGroup();
1315 // allows setting subgroup for offline members
1316 void Group::ChangeMembersGroup(const uint64
&guid
, const uint8
&group
)
1320 Player
*player
= sObjectMgr
.GetPlayer(guid
);
1325 prevSubGroup
= GetMemberGroup(guid
);
1327 SubGroupCounterDecrease(prevSubGroup
);
1329 if(_setMembersGroup(guid
, group
))
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())
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
1350 prevSubGroup
= player
->GetOriginalSubGroup();
1351 player
->GetOriginalGroupRef().setSubGroup(group
);
1353 SubGroupCounterDecrease(prevSubGroup
);
1359 void Group::UpdateLooterGuid( Creature
* creature
, bool ifneed
)
1361 switch (GetLootMethod())
1367 // round robin style looting applies for all low
1368 // quality items in each loot method except free for all and master loot
1372 member_citerator guid_itr
= _getMemberCSlot(GetLooterGuid());
1373 if(guid_itr
!= m_memberSlots
.end())
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))
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());
1400 if(refresh
) // update loot for new looter
1401 pl
->SendLoot(creature
->GetGUID(), LOOT_CORPSE
);
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());
1421 if(refresh
) // update loot for new looter
1422 pl
->SendLoot(creature
->GetGUID(), LOOT_CORPSE
);
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
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
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
;
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
)
1501 player
->SetDungeonDifficulty(difficulty
);
1502 player
->SendDungeonDifficulty(true);
1506 void Group::SetRaidDifficulty(Difficulty difficulty
)
1508 m_raidDifficulty
= difficulty
;
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
)
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
)
1533 void Group::ResetInstances(uint8 method
, bool isRaid
, Player
* SendMsgTo
)
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
))
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
)
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
);
1572 SendMsgTo
->SendResetInstanceSuccess(p
->GetMapId());
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
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);
1596 InstanceGroupBind
* Group::GetBoundInstance(Player
* player
)
1598 uint32 mapid
= player
->GetMapId();
1599 MapEntry
const* mapEntry
= sMapStore
.LookupEntry(mapid
);
1603 Difficulty difficulty
= player
->GetDifficulty(mapEntry
->IsRaid());
1605 // some instances only have one difficulty
1606 MapDifficulty
const* mapDiff
= GetMapDifficultyData(mapid
,difficulty
);
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
;
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
);
1627 BoundInstancesMap::iterator itr
= m_boundInstances
[difficulty
].find(aMap
->GetId());
1628 if(itr
!= m_boundInstances
[difficulty
].end())
1629 return &itr
->second
;
1634 InstanceGroupBind
* Group::BindToInstance(InstanceSave
*save
, bool permanent
, bool load
)
1636 if(save
&& !isBGGroup())
1638 InstanceGroupBind
& bind
= m_boundInstances
[save
->GetDifficulty()][save
->GetMapId()];
1641 // when a boss is killed or when copying the players's binds to the group
1642 if(permanent
!= bind
.perm
|| save
!= bind
.save
)
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());
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
)
1652 bind
.save
->RemoveGroup(this);
1653 save
->AddGroup(this);
1657 bind
.perm
= permanent
;
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());
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())
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;