root/trunk/src/game/Group.cpp @ 49

Revision 44, 49.4 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Common.h"
22#include "Opcodes.h"
23#include "WorldPacket.h"
24#include "WorldSession.h"
25#include "Player.h"
26#include "World.h"
27#include "ObjectMgr.h"
28#include "Group.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
36Group::Group()
37{
38    m_leaderGuid        = 0;
39    m_mainTank          = 0;
40    m_mainAssistant     = 0;
41    m_groupType         = (GroupType)0;
42    m_bgGroup           = NULL;
43    m_lootMethod        = (LootMethod)0;
44    m_looterGuid        = 0;
45    m_lootThreshold     = ITEM_QUALITY_UNCOMMON;
46
47    for(int i=0; i<TARGETICONCOUNT; i++)
48        m_targetIcons[i] = 0;
49}
50
51Group::~Group()
52{
53    if(m_bgGroup)
54    {
55        sLog.outDebug("Group::~Group: battleground group being deleted.");
56        if(m_bgGroup->GetBgRaid(ALLIANCE) == this) m_bgGroup->SetBgRaid(ALLIANCE, NULL);
57        else if(m_bgGroup->GetBgRaid(HORDE) == this) m_bgGroup->SetBgRaid(HORDE, NULL);
58        else sLog.outError("Group::~Group: battleground group is not linked to the correct battleground.");
59    }
60    Rolls::iterator itr;
61    while(!RollId.empty())
62    {
63        itr = RollId.begin();
64        Roll *r = *itr;
65        RollId.erase(itr);
66        delete(r);
67    }
68
69    // it is undefined whether objectmgr (which stores the groups) or instancesavemgr
70    // will be unloaded first so we must be prepared for both cases
71    // this may unload some instance saves
72    for(uint8 i = 0; i < TOTAL_DIFFICULTIES; i++)
73        for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end(); ++itr)
74            itr->second.save->RemoveGroup(this);
75}
76
77bool Group::Create(const uint64 &guid, const char * name)
78{
79    m_leaderGuid = guid;
80    m_leaderName = name;
81
82    m_groupType  = isBGGroup() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
83    m_lootMethod = GROUP_LOOT;
84    m_lootThreshold = ITEM_QUALITY_UNCOMMON;
85    m_looterGuid = guid;
86
87    m_difficulty = DIFFICULTY_NORMAL;
88    if(!isBGGroup())
89    {
90        Player *leader = objmgr.GetPlayer(guid);
91        if(leader) m_difficulty = leader->GetDifficulty();
92
93        Player::ConvertInstancesToGroup(leader, this, guid);
94
95        // store group in database
96        CharacterDatabase.BeginTransaction();
97        CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
98        CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(m_leaderGuid));
99        CharacterDatabase.PExecute("INSERT INTO groups(leaderGuid,mainTank,mainAssistant,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,isRaid,difficulty) "
100            "VALUES('%u','%u','%u','%u','%u','%u','" I64FMTD "','" I64FMTD "','" I64FMTD "','" I64FMTD "','" I64FMTD "','" I64FMTD "','" I64FMTD "','" I64FMTD "','%u','%u')",
101            GUID_LOPART(m_leaderGuid), GUID_LOPART(m_mainTank), GUID_LOPART(m_mainAssistant), uint32(m_lootMethod),
102            GUID_LOPART(m_looterGuid), uint32(m_lootThreshold), m_targetIcons[0], m_targetIcons[1], m_targetIcons[2], m_targetIcons[3], m_targetIcons[4], m_targetIcons[5], m_targetIcons[6], m_targetIcons[7], isRaidGroup(), m_difficulty);
103    }
104
105    if(!AddMember(guid, name))
106        return false;
107
108    if(!isBGGroup()) CharacterDatabase.CommitTransaction();
109
110    return true;
111}
112
113bool Group::LoadGroupFromDB(const uint64 &leaderGuid, QueryResult *result, bool loadMembers)
114{
115    if(isBGGroup())
116        return false;
117
118    bool external = true;
119    if(!result)
120    {
121        external = false;
122        //                                       0          1              2           3           4              5      6      7      8      9      10     11     12     13      14
123        result = CharacterDatabase.PQuery("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, isRaid, difficulty FROM groups WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
124        if(!result)
125            return false;
126    }
127
128    m_leaderGuid = leaderGuid;
129
130    // group leader not exist
131    if(!objmgr.GetPlayerNameByGUID(m_leaderGuid, m_leaderName))
132    {
133        if(!external) delete result;
134        return false;
135    }
136
137    m_groupType  = (*result)[13].GetBool() ? GROUPTYPE_RAID : GROUPTYPE_NORMAL;
138    m_difficulty = (*result)[14].GetUInt8();
139    m_mainTank = (*result)[0].GetUInt64();
140    m_mainAssistant = (*result)[1].GetUInt64();
141    m_lootMethod = (LootMethod)(*result)[2].GetUInt8();
142    m_looterGuid = MAKE_NEW_GUID((*result)[3].GetUInt32(), 0, HIGHGUID_PLAYER);
143    m_lootThreshold = (ItemQualities)(*result)[4].GetUInt16();
144
145    for(int i=0; i<TARGETICONCOUNT; i++)
146        m_targetIcons[i] = (*result)[5+i].GetUInt64();
147    if(!external) delete result;
148
149    if(loadMembers)
150    {
151        result = CharacterDatabase.PQuery("SELECT memberGuid, assistant, subgroup FROM group_member WHERE leaderGuid ='%u'", GUID_LOPART(leaderGuid));
152        if(!result)
153            return false;
154
155        do
156        {
157            LoadMemberFromDB((*result)[0].GetUInt32(), (*result)[2].GetUInt8(), (*result)[1].GetBool());
158        } while( result->NextRow() );
159        delete result;
160        // group too small
161        if(GetMembersCount() < 2)
162            return false;
163    }
164
165    return true;
166}
167
168bool Group::LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant)
169{
170    MemberSlot member;
171    member.guid      = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
172
173    // skip non-existed member
174    if(!objmgr.GetPlayerNameByGUID(member.guid, member.name))
175        return false;
176
177    member.group     = subgroup;
178    member.assistant = assistant;
179    m_memberSlots.push_back(member);
180    return true;
181}
182
183bool Group::AddInvite(Player *player)
184{
185    if(!player || player->GetGroupInvite() || player->GetGroup())
186        return false;
187
188    RemoveInvite(player);
189
190    m_invitees.insert(player->GetGUID());
191
192    player->SetGroupInvite(this);
193
194    return true;
195}
196
197bool Group::AddLeaderInvite(Player *player)
198{
199    if(!AddInvite(player))
200        return false;
201
202    m_leaderGuid = player->GetGUID();
203    m_leaderName = player->GetName();
204    return true;
205}
206
207uint32 Group::RemoveInvite(Player *player)
208{
209    for(InvitesList::iterator itr=m_invitees.begin(); itr!=m_invitees.end(); ++itr)
210    {
211        if((*itr) == player->GetGUID())
212        {
213            m_invitees.erase(itr);
214            break;
215        }
216    }
217
218    player->SetGroupInvite(NULL);
219    return GetMembersCount();
220}
221
222void Group::RemoveAllInvites()
223{
224    for(InvitesList::iterator itr=m_invitees.begin(); itr!=m_invitees.end(); ++itr)
225    {
226        Player *invitee = objmgr.GetPlayer(*itr);
227        if(invitee)
228            invitee->SetGroupInvite(NULL);
229    }
230    m_invitees.clear();
231}
232
233bool Group::AddMember(const uint64 &guid, const char* name)
234{
235    if(!_addMember(guid, name))
236        return false;
237    SendUpdate();
238
239    Player *player = objmgr.GetPlayer(guid);
240    if(player)
241    {
242        if(!IsLeader(player->GetGUID()) && !isBGGroup())
243        {
244            // reset the new member's instances, unless he is currently in one of them
245            // including raid/heroic instances that they are not permanently bound to!
246            player->ResetInstances(INSTANCE_RESET_GROUP_JOIN);
247
248            if(player->getLevel() >= LEVELREQUIREMENT_HEROIC && player->GetDifficulty() != GetDifficulty() )
249            {
250                player->SetDifficulty(m_difficulty);
251                player->SendDungeonDifficulty(true);
252            }
253        }
254        player->SetGroupUpdateFlag(GROUP_UPDATE_FULL);
255        UpdatePlayerOutOfRange(player);
256    }
257
258    return true;
259}
260
261uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method)
262{
263    // remove member and change leader (if need) only if strong more 2 members _before_ member remove
264    if(GetMembersCount() > (isBGGroup() ? 1 : 2))           // in BG group case allow 1 members group
265    {
266        bool leaderChanged = _removeMember(guid);
267
268        Player *player = objmgr.GetPlayer( guid );
269        if (player)
270        {
271            WorldPacket data;
272
273            if(method == 1)
274            {
275                data.Initialize( SMSG_GROUP_UNINVITE, 0 );
276                player->GetSession()->SendPacket( &data );
277            }
278
279            data.Initialize(SMSG_GROUP_LIST, 24);
280            data << uint64(0) << uint64(0) << uint64(0);
281            player->GetSession()->SendPacket(&data);
282
283            _homebindIfInstance(player);
284        }
285
286        if(leaderChanged)
287        {
288            WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1));
289            data << m_memberSlots.front().name;
290            BroadcastPacket(&data);
291        }
292
293        SendUpdate();
294    }
295    // if group before remove <= 2 disband it
296    else
297        Disband(true);
298
299    return m_memberSlots.size();
300}
301
302void Group::ChangeLeader(const uint64 &guid)
303{
304    member_citerator slot = _getMemberCSlot(guid);
305
306    if(slot==m_memberSlots.end())
307        return;
308
309    _setLeader(guid);
310
311    WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1);
312    data << slot->name;
313    BroadcastPacket(&data);
314    SendUpdate();
315}
316
317void Group::Disband(bool hideDestroy)
318{
319    Player *player;
320
321    for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
322    {
323        player = objmgr.GetPlayer(citr->guid);
324        if(!player)
325            continue;
326
327        player->SetGroup(NULL);
328
329        if(!player->GetSession())
330            continue;
331
332        WorldPacket data;
333        if(!hideDestroy)
334        {
335            data.Initialize(SMSG_GROUP_DESTROYED, 0);
336            player->GetSession()->SendPacket(&data);
337        }
338
339        data.Initialize(SMSG_GROUP_LIST, 24);
340        data << uint64(0) << uint64(0) << uint64(0);
341        player->GetSession()->SendPacket(&data);
342
343        _homebindIfInstance(player);
344    }
345    RollId.clear();
346    m_memberSlots.clear();
347
348    RemoveAllInvites();
349
350    if(!isBGGroup())
351    {
352        CharacterDatabase.BeginTransaction();
353        CharacterDatabase.PExecute("DELETE FROM groups WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
354        CharacterDatabase.PExecute("DELETE FROM group_member WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
355        CharacterDatabase.CommitTransaction();
356        ResetInstances(INSTANCE_RESET_GROUP_DISBAND, NULL);
357    }
358
359    m_leaderGuid = 0;
360    m_leaderName = "";
361}
362
363/*********************************************************/
364/***                   LOOT SYSTEM                     ***/
365/*********************************************************/
366
367void Group::SendLootStartRoll(uint32 CountDown, const Roll &r)
368{
369    WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4));
370    data << uint64(r.itemGUID);                             // guid of rolled item
371    data << uint32(r.totalPlayersRolling);                  // maybe the number of players rolling for it???
372    data << uint32(r.itemid);                               // the itemEntryId for the item that shall be rolled for
373    data << uint32(r.itemRandomSuffix);                     // randomSuffix
374    data << uint32(r.itemRandomPropId);                     // item random property ID
375    data << uint32(CountDown);                              // the countdown time to choose "need" or "greed"
376
377    for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
378    {
379        Player *p = objmgr.GetPlayer(itr->first);
380        if(!p || !p->GetSession())
381            continue;
382
383        if(itr->second != NOT_VALID)
384            p->GetSession()->SendPacket( &data );
385    }
386}
387
388void Group::SendLootRoll(uint64 SourceGuid, uint64 TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
389{
390    WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1));
391    data << uint64(SourceGuid);                             // guid of the item rolled
392    data << uint32(0);                                      // unknown, maybe amount of players
393    data << uint64(TargetGuid);
394    data << uint32(r.itemid);                               // the itemEntryId for the item that shall be rolled for
395    data << uint32(r.itemRandomSuffix);                     // randomSuffix
396    data << uint32(r.itemRandomPropId);                     // Item random property ID
397    data << uint8(RollNumber);                              // 0: "Need for: [item name]" > 127: "you passed on: [item name]"      Roll number
398    data << uint8(RollType);                                // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll
399    data << uint8(0);                                       // 2.4.0
400
401    for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
402    {
403        Player *p = objmgr.GetPlayer(itr->first);
404        if(!p || !p->GetSession())
405            continue;
406
407        if(itr->second != NOT_VALID)
408            p->GetSession()->SendPacket( &data );
409    }
410}
411
412void Group::SendLootRollWon(uint64 SourceGuid, uint64 TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r)
413{
414    WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1));
415    data << uint64(SourceGuid);                             // guid of the item rolled
416    data << uint32(0);                                      // unknown, maybe amount of players
417    data << uint32(r.itemid);                               // the itemEntryId for the item that shall be rolled for
418    data << uint32(r.itemRandomSuffix);                     // randomSuffix
419    data << uint32(r.itemRandomPropId);                     // Item random property
420    data << uint64(TargetGuid);                             // guid of the player who won.
421    data << uint8(RollNumber);                              // rollnumber realted to SMSG_LOOT_ROLL
422    data << uint8(RollType);                                // Rolltype related to SMSG_LOOT_ROLL
423
424    for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
425    {
426        Player *p = objmgr.GetPlayer(itr->first);
427        if(!p || !p->GetSession())
428            continue;
429
430        if(itr->second != NOT_VALID)
431            p->GetSession()->SendPacket( &data );
432    }
433}
434
435void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r)
436{
437    WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4));
438    data << uint64(r.itemGUID);                             // Guid of the item rolled
439    data << uint32(NumberOfPlayers);                        // The number of players rolling for it???
440    data << uint32(r.itemid);                               // The itemEntryId for the item that shall be rolled for
441    data << uint32(r.itemRandomPropId);                     // Item random property ID
442    data << uint32(r.itemRandomSuffix);                     // Item random suffix ID
443
444    for( Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr!=r.playerVote.end(); ++itr)
445    {
446        Player *p = objmgr.GetPlayer(itr->first);
447        if(!p || !p->GetSession())
448            continue;
449
450        if(itr->second != NOT_VALID)
451            p->GetSession()->SendPacket( &data );
452    }
453}
454
455void Group::GroupLoot(uint64 playerGUID, Loot *loot, Creature *creature)
456{
457    std::vector<LootItem>::iterator i;
458    ItemPrototype const *item;
459    uint8 itemSlot = 0;
460    Player *player = objmgr.GetPlayer(playerGUID);
461    Group *group = player->GetGroup();
462
463    for (i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
464    {
465        item = objmgr.GetItemPrototype(i->itemid);
466        if (!item)
467        {
468            //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid);
469            continue;
470        }
471
472        //roll for over-threshold item if it's one-player loot
473        if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
474        {
475            uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
476            Roll* r=new Roll(newitemGUID,*i);
477
478            //a vector is filled with only near party members
479            for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
480            {
481                Player *member = itr->getSource();
482                if(!member || !member->GetSession())
483                    continue;
484                if ( i->AllowedForPlayer(member) )
485                {
486                    if (member->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
487                    {
488                        r->playerVote[member->GetGUID()] = NOT_EMITED_YET;
489                        ++r->totalPlayersRolling;
490                    }
491                }
492            }
493
494            r->setLoot(loot);
495            r->itemSlot = itemSlot;
496
497            group->SendLootStartRoll(60000, *r);
498
499            loot->items[itemSlot].is_blocked = true;
500            creature->m_groupLootTimer = 60000;
501            creature->lootingGroupLeaderGUID = GetLeaderGUID();
502
503            RollId.push_back(r);
504        }
505        else
506            i->is_underthreshold=1;
507
508    }
509}
510
511void Group::NeedBeforeGreed(uint64 playerGUID, Loot *loot, Creature *creature)
512{
513    ItemPrototype const *item;
514    Player *player = objmgr.GetPlayer(playerGUID);
515    Group *group = player->GetGroup();
516
517    uint8 itemSlot = 0;
518    for(std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot)
519    {
520        item = objmgr.GetItemPrototype(i->itemid);
521
522        //only roll for one-player items, not for ones everyone can get
523        if (item->Quality >= uint32(m_lootThreshold) && !i->freeforall)
524        {
525            uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM);
526            Roll* r=new Roll(newitemGUID,*i);
527
528            for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
529            {
530                Player *playerToRoll = itr->getSource();
531                if(!playerToRoll || !playerToRoll->GetSession())
532                    continue;
533
534                if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll) )
535                {
536                    if (playerToRoll->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
537                    {
538                        r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET;
539                        ++r->totalPlayersRolling;
540                    }
541                }
542            }
543
544            if (r->totalPlayersRolling > 0)
545            {
546                r->setLoot(loot);
547                r->itemSlot = itemSlot;
548
549                group->SendLootStartRoll(60000, *r);
550
551                loot->items[itemSlot].is_blocked = true;
552
553                RollId.push_back(r);
554            }
555            else
556            {
557                delete r;
558            }
559        }
560        else
561            i->is_underthreshold=1;
562    }
563}
564
565void Group::MasterLoot(uint64 playerGUID, Loot* /*loot*/, Creature *creature)
566{
567    Player *player = objmgr.GetPlayer(playerGUID);
568    if(!player)
569        return;
570
571    sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330) player = [%s].", player->GetName());
572
573    uint32 real_count = 0;
574
575    WorldPacket data(SMSG_LOOT_MASTER_LIST, 330);
576    data << (uint8)GetMembersCount();
577
578    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
579    {
580        Player *looter = itr->getSource();
581        if (!looter->IsInWorld())
582            continue;
583
584        if (looter->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
585        {
586            data << looter->GetGUID();
587            ++real_count;
588        }
589    }
590
591    data.put<uint8>(0,real_count);
592
593    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
594    {
595        Player *looter = itr->getSource();
596        if (looter->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
597            looter->GetSession()->SendPacket(&data);
598    }
599}
600
601void Group::CountRollVote(uint64 playerGUID, uint64 Guid, uint32 NumberOfPlayers, uint8 Choise)
602{
603    Rolls::iterator rollI = GetRoll(Guid);
604    if (rollI == RollId.end())
605        return;
606    Roll* roll = *rollI;
607
608    Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID);
609    // this condition means that player joins to the party after roll begins
610    if (itr == roll->playerVote.end())
611        return;
612
613    if (roll->getLoot())
614        if (roll->getLoot()->items.empty())
615            return;
616
617    switch (Choise)
618    {
619        case 0:                                             //Player choose pass
620        {
621            SendLootRoll(0, playerGUID, 128, 128, *roll);
622            ++roll->totalPass;
623            itr->second = PASS;
624        }
625        break;
626        case 1:                                             //player choose Need
627        {
628            SendLootRoll(0, playerGUID, 0, 0, *roll);
629            ++roll->totalNeed;
630            itr->second = NEED;
631        }
632        break;
633        case 2:                                             //player choose Greed
634        {
635            SendLootRoll(0, playerGUID, 128, 2, *roll);
636            ++roll->totalGreed;
637            itr->second = GREED;
638        }
639        break;
640    }
641    if (roll->totalPass + roll->totalGreed + roll->totalNeed >= roll->totalPlayersRolling)
642    {
643        CountTheRoll(rollI, NumberOfPlayers);
644    }
645}
646
647//called when roll timer expires
648void Group::EndRoll()
649{
650    Rolls::iterator itr;
651    while(!RollId.empty())
652    {
653        //need more testing here, if rolls disappear
654        itr = RollId.begin();
655        CountTheRoll(itr, GetMembersCount());               //i don't have to edit player votes, who didn't vote ... he will pass
656    }
657}
658
659void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers)
660{
661    Roll* roll = *rollI;
662    if(!roll->isValid())                                    // is loot already deleted ?
663    {
664        RollId.erase(rollI);
665        delete roll;
666        return;
667    }
668    //end of the roll
669    if (roll->totalNeed > 0)
670    {
671        if(!roll->playerVote.empty())
672        {
673            uint8 maxresul = 0;
674            uint64 maxguid  = (*roll->playerVote.begin()).first;
675            Player *player;
676
677            for( Roll::PlayerVote::const_iterator itr=roll->playerVote.begin(); itr!=roll->playerVote.end(); ++itr)
678            {
679                if (itr->second != NEED)
680                    continue;
681
682                uint8 randomN = urand(1, 99);
683                SendLootRoll(0, itr->first, randomN, 1, *roll);
684                if (maxresul < randomN)
685                {
686                    maxguid  = itr->first;
687                    maxresul = randomN;
688                }
689            }
690            SendLootRollWon(0, maxguid, maxresul, 1, *roll);
691            player = objmgr.GetPlayer(maxguid);
692
693            if(player && player->GetSession())
694            {
695                ItemPosCountVec dest;
696                LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
697                uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
698                if ( msg == EQUIP_ERR_OK )
699                {
700                    item->is_looted = true;
701                    roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
702                    --roll->getLoot()->unlootedCount;
703                    player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
704                }
705                else
706                {
707                    item->is_blocked = false;
708                    player->SendEquipError( msg, NULL, NULL );
709                }
710            }
711        }
712    }
713    else if (roll->totalGreed > 0)
714    {
715        if(!roll->playerVote.empty())
716        {
717            uint8 maxresul = 0;
718            uint64 maxguid = (*roll->playerVote.begin()).first;
719            Player *player;
720
721            Roll::PlayerVote::iterator itr;
722            for (itr=roll->playerVote.begin(); itr!=roll->playerVote.end(); ++itr)
723            {
724                if (itr->second != GREED)
725                    continue;
726
727                uint8 randomN = urand(1, 99);
728                SendLootRoll(0, itr->first, randomN, 2, *roll);
729                if (maxresul < randomN)
730                {
731                    maxguid  = itr->first;
732                    maxresul = randomN;
733                }
734            }
735            SendLootRollWon(0, maxguid, maxresul, 2, *roll);
736            player = objmgr.GetPlayer(maxguid);
737
738            if(player && player->GetSession())
739            {
740                ItemPosCountVec dest;
741                LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
742                uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count );
743                if ( msg == EQUIP_ERR_OK )
744                {
745                    item->is_looted = true;
746                    roll->getLoot()->NotifyItemRemoved(roll->itemSlot);
747                    --roll->getLoot()->unlootedCount;
748                    player->StoreNewItem( dest, roll->itemid, true, item->randomPropertyId);
749                }
750                else
751                {
752                    item->is_blocked = false;
753                    player->SendEquipError( msg, NULL, NULL );
754                }
755            }
756        }
757    }
758    else
759    {
760        SendLootAllPassed(NumberOfPlayers, *roll);
761        LootItem *item = &(roll->getLoot()->items[roll->itemSlot]);
762        if(item) item->is_blocked = false;
763    }
764    RollId.erase(rollI);
765    delete roll;
766}
767
768void Group::SetTargetIcon(uint8 id, uint64 guid)
769{
770    if(id >= TARGETICONCOUNT)
771        return;
772
773    // clean other icons
774    if( guid != 0 )
775        for(int i=0; i<TARGETICONCOUNT; i++)
776            if( m_targetIcons[i] == guid )
777                SetTargetIcon(i, 0);
778
779    m_targetIcons[id] = guid;
780
781    WorldPacket data(MSG_RAID_TARGET_UPDATE, (2+8));
782    data << (uint8)0;
783    data << id;
784    data << guid;
785    BroadcastPacket(&data);
786}
787
788void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level)
789{
790    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
791    {
792        Player* member = itr->getSource();
793        if(!member || !member->isAlive())                   // only for alive
794            continue;
795
796        if(!member->IsAtGroupRewardDistance(victim))        // at req. distance
797            continue;
798
799        ++count;
800        sum_level += member->getLevel();
801        if(!member_with_max_level || member_with_max_level->getLevel() < member->getLevel())
802            member_with_max_level = member;
803    }
804}
805
806void Group::SendTargetIconList(WorldSession *session)
807{
808    if(!session)
809        return;
810
811    WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9));
812    data << (uint8)1;
813
814    for(int i=0; i<TARGETICONCOUNT; i++)
815    {
816        if(m_targetIcons[i] == 0)
817            continue;
818
819        data << (uint8)i;
820        data << m_targetIcons[i];
821    }
822
823    session->SendPacket(&data);
824}
825
826void Group::SendUpdate()
827{
828    Player *player;
829
830    for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
831    {
832        player = objmgr.GetPlayer(citr->guid);
833        if(!player || !player->GetSession())
834            continue;
835                                                            // guess size
836        WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+8+4+GetMembersCount()*20));
837        data << (uint8)m_groupType;                         // group type
838        data << (uint8)(isBGGroup() ? 1 : 0);               // 2.0.x, isBattleGroundGroup?
839        data << (uint8)(citr->group);                       // groupid
840        data << (uint8)(citr->assistant?0x01:0);            // 0x2 main assist, 0x4 main tank
841        data << uint64(0x50000000FFFFFFFELL);               // related to voice chat?
842        data << uint32(GetMembersCount()-1);
843        for(member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2)
844        {
845            if(citr->guid == citr2->guid)
846                continue;
847
848            data << citr2->name;
849            data << (uint64)citr2->guid;
850                                                            // online-state
851            data << (uint8)(objmgr.GetPlayer(citr2->guid) ? 1 : 0);
852            data << (uint8)(citr2->group);                  // groupid
853            data << (uint8)(citr2->assistant?0x01:0);       // 0x2 main assist, 0x4 main tank
854        }
855
856        data << uint64(m_leaderGuid);                       // leader guid
857        if(GetMembersCount()-1)
858        {
859            data << (uint8)m_lootMethod;                    // loot method
860            data << (uint64)m_looterGuid;                   // looter guid
861            data << (uint8)m_lootThreshold;                 // loot threshold
862            data << (uint8)m_difficulty;                    // Heroic Mod Group
863
864        }
865        player->GetSession()->SendPacket( &data );
866    }
867}
868
869void Group::UpdatePlayerOutOfRange(Player* pPlayer)
870{
871    if(!pPlayer)
872        return;
873
874    Player *player;
875    WorldPacket data;
876    pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data);
877
878    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
879    {
880        player = itr->getSource();
881        if (player && player != pPlayer && !pPlayer->isVisibleFor(player))
882            player->GetSession()->SendPacket(&data);
883    }
884}
885
886void Group::BroadcastPacket(WorldPacket *packet, int group, uint64 ignore)
887{
888    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
889    {
890        Player *pl = itr->getSource();
891        if(!pl || (ignore != 0 && pl->GetGUID() == ignore))
892            continue;
893
894        if (pl->GetSession() && (group==-1 || itr->getSubGroup()==group))
895            pl->GetSession()->SendPacket(packet);
896    }
897}
898
899void Group::BroadcastReadyCheck(WorldPacket *packet)
900{
901    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
902    {
903        Player *pl = itr->getSource();
904        if(pl && pl->GetSession())
905            if(IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID()))
906                pl->GetSession()->SendPacket(packet);
907    }
908}
909
910void Group::OfflineReadyCheck()
911{
912    for(member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr)
913    {
914        Player *pl = objmgr.GetPlayer(citr->guid);
915        if (!pl || !pl->GetSession())
916        {
917            WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9);
918            data << citr->guid;
919            data << (uint8)0;
920            BroadcastReadyCheck(&data);
921        }
922    }
923}
924
925bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant)
926{
927    // get first not-full group
928    uint8 groupid = 0;
929    std::vector<uint8> temp(MAXRAIDSIZE/MAXGROUPSIZE);
930    for(member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
931    {
932        if (itr->group >= temp.size()) continue;
933        ++temp[itr->group];
934        if(temp[groupid] >= MAXGROUPSIZE)
935            ++groupid;
936    }
937
938    return _addMember(guid, name, isAssistant, groupid);
939}
940
941bool Group::_addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group)
942{
943    if(IsFull())
944        return false;
945
946    if(!guid)
947        return false;
948
949    Player *player = objmgr.GetPlayer(guid);
950
951    MemberSlot member;
952    member.guid      = guid;
953    member.name      = name;
954    member.group     = group;
955    member.assistant = isAssistant;
956    m_memberSlots.push_back(member);
957
958    if(player)
959    {
960        player->SetGroupInvite(NULL);
961        player->SetGroup(this, group);
962        // if the same group invites the player back, cancel the homebind timer
963        InstanceGroupBind *bind = GetBoundInstance(player->GetMapId(), player->GetDifficulty());
964        if(bind && bind->save->GetInstanceId() == player->GetInstanceId())
965            player->m_InstanceValid = true;
966    }
967
968    if(!isRaidGroup())                                      // reset targetIcons for non-raid-groups
969    {
970        for(int i=0; i<TARGETICONCOUNT; i++)
971            m_targetIcons[i] = 0;
972    }
973
974    if(!isBGGroup())
975    {
976        // insert into group table
977        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);
978    }
979
980    return true;
981}
982
983bool Group::_removeMember(const uint64 &guid)
984{
985    Player *player = objmgr.GetPlayer(guid);
986    if (player)
987    {
988        player->SetGroup(NULL);
989    }
990
991    _removeRolls(guid);
992
993    member_witerator slot = _getMemberWSlot(guid);
994    if (slot != m_memberSlots.end())
995        m_memberSlots.erase(slot);
996
997    if(!isBGGroup())
998        CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid='%u'", GUID_LOPART(guid));
999
1000    if(m_leaderGuid == guid)                                // leader was removed
1001    {
1002        if(GetMembersCount() > 0)
1003            _setLeader(m_memberSlots.front().guid);
1004        return true;
1005    }
1006
1007    return false;
1008}
1009
1010void Group::_setLeader(const uint64 &guid)
1011{
1012    member_citerator slot = _getMemberCSlot(guid);
1013    if(slot==m_memberSlots.end())
1014        return;
1015
1016    if(!isBGGroup())
1017    {
1018        // TODO: set a time limit to have this function run rarely cause it can be slow
1019        CharacterDatabase.BeginTransaction();
1020
1021        // update the group's bound instances when changing leaders
1022
1023        // remove all permanent binds from the group
1024        // in the DB also remove solo binds that will be replaced with permbinds
1025        // from the new leader
1026        CharacterDatabase.PExecute(
1027            "DELETE FROM group_instance WHERE leaderguid='%u' AND (permanent = 1 OR "
1028            "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')"
1029            ")", GUID_LOPART(m_leaderGuid), GUID_LOPART(slot->guid)
1030        );
1031
1032        Player *player = objmgr.GetPlayer(slot->guid);
1033        if(player)
1034        {
1035            for(uint8 i = 0; i < TOTAL_DIFFICULTIES; i++)
1036            {
1037                for(BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();)
1038                {
1039                    if(itr->second.perm)
1040                    {
1041                        itr->second.save->RemoveGroup(this);
1042                        m_boundInstances[i].erase(itr++);
1043                    }
1044                    else
1045                        ++itr;
1046                }
1047            }
1048        }
1049
1050        // update the group's solo binds to the new leader
1051        CharacterDatabase.PExecute("UPDATE group_instance SET leaderGuid='%u' WHERE leaderGuid = '%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1052
1053        // copy the permanent binds from the new leader to the group
1054        // overwriting the solo binds with permanent ones if necessary
1055        // in the DB those have been deleted already
1056        Player::ConvertInstancesToGroup(player, this, slot->guid);
1057
1058        // update the group leader
1059        CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1060        CharacterDatabase.PExecute("UPDATE group_member SET leaderGuid='%u' WHERE leaderGuid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_leaderGuid));
1061        CharacterDatabase.CommitTransaction();
1062    }
1063
1064    m_leaderGuid = slot->guid;
1065    m_leaderName = slot->name;
1066}
1067
1068void Group::_removeRolls(const uint64 &guid)
1069{
1070    for (Rolls::iterator it = RollId.begin(); it < RollId.end(); it++)
1071    {
1072        Roll* roll = *it;
1073        Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid);
1074        if(itr2 == roll->playerVote.end())
1075            continue;
1076
1077        if (itr2->second == GREED) --roll->totalGreed;
1078        if (itr2->second == NEED) --roll->totalNeed;
1079        if (itr2->second == PASS) --roll->totalPass;
1080        if (itr2->second != NOT_VALID) --roll->totalPlayersRolling;
1081
1082        roll->playerVote.erase(itr2);
1083
1084        CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, 3);
1085    }
1086}
1087
1088void Group::_convertToRaid()
1089{
1090    m_groupType = GROUPTYPE_RAID;
1091
1092    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET isRaid = 1 WHERE leaderGuid='%u'", GUID_LOPART(m_leaderGuid));
1093}
1094
1095bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group)
1096{
1097    member_witerator slot = _getMemberWSlot(guid);
1098    if(slot==m_memberSlots.end())
1099        return false;
1100
1101    slot->group = group;
1102    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid));
1103    return true;
1104}
1105
1106bool Group::_setAssistantFlag(const uint64 &guid, const bool &state)
1107{
1108    member_witerator slot = _getMemberWSlot(guid);
1109    if(slot==m_memberSlots.end())
1110        return false;
1111
1112    slot->assistant = state;
1113    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE group_member SET assistant='%u' WHERE memberGuid='%u'", (state==true)?1:0, GUID_LOPART(guid));
1114    return true;
1115}
1116
1117bool Group::_setMainTank(const uint64 &guid)
1118{
1119    member_citerator slot = _getMemberCSlot(guid);
1120    if(slot==m_memberSlots.end())
1121        return false;
1122
1123    if(m_mainAssistant == guid)
1124        _setMainAssistant(0);
1125    m_mainTank = guid;
1126    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET mainTank='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainTank), GUID_LOPART(m_leaderGuid));
1127    return true;
1128}
1129
1130bool Group::_setMainAssistant(const uint64 &guid)
1131{
1132    member_witerator slot = _getMemberWSlot(guid);
1133    if(slot==m_memberSlots.end())
1134        return false;
1135
1136    if(m_mainTank == guid)
1137        _setMainTank(0);
1138    m_mainAssistant = guid;
1139    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET mainAssistant='%u' WHERE leaderGuid='%u'", GUID_LOPART(m_mainAssistant), GUID_LOPART(m_leaderGuid));
1140    return true;
1141}
1142
1143bool Group::SameSubGroup(Player const* member1, Player const* member2) const
1144{
1145    if(!member1 || !member2) return false;
1146    if (member1->GetGroup() != this || member2->GetGroup() != this) return false;
1147    else return member1->GetSubGroup() == member2->GetSubGroup();
1148}
1149
1150// allows setting subgroup for offline members
1151void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group)
1152{
1153    if(!isRaidGroup())
1154        return;
1155    Player *player = objmgr.GetPlayer(guid);
1156    if (!player)
1157    {
1158        if(_setMembersGroup(guid, group))
1159            SendUpdate();
1160    }
1161    else ChangeMembersGroup(player, group);
1162}
1163
1164// only for online members
1165void Group::ChangeMembersGroup(Player *player, const uint8 &group)
1166{
1167    if(!player || !isRaidGroup())
1168        return;
1169    if(_setMembersGroup(player->GetGUID(), group))
1170    {
1171        player->GetGroupRef().setSubGroup(group);
1172        SendUpdate();
1173    }
1174}
1175
1176void Group::UpdateLooterGuid( Creature* creature, bool ifneed )
1177{
1178    switch (GetLootMethod())
1179    {
1180        case MASTER_LOOT:
1181        case FREE_FOR_ALL:
1182            return;
1183        default:
1184            // round robin style looting applies for all low
1185            // quality items in each loot method except free for all and master loot
1186            break;
1187    }
1188
1189    member_citerator guid_itr = _getMemberCSlot(GetLooterGuid());
1190    if(guid_itr != m_memberSlots.end())
1191    {
1192        if(ifneed)
1193        {
1194            // not update if only update if need and ok
1195            Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid);
1196            if(looter && looter->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
1197                return;
1198        }
1199        ++guid_itr;
1200    }
1201
1202    // search next after current
1203    if(guid_itr != m_memberSlots.end())
1204    {
1205        for(member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr)
1206        {
1207            if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1208            {
1209                if (pl->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
1210                {
1211                    bool refresh = pl->GetLootGUID()==creature->GetGUID();
1212
1213                    //if(refresh)                             // update loot for new looter
1214                    //    pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1215                    SetLooterGuid(pl->GetGUID());
1216                    SendUpdate();
1217                    if(refresh)                             // update loot for new looter
1218                        pl->SendLoot(creature->GetGUID(),LOOT_CORPSE);
1219                    return;
1220                }
1221            }
1222        }
1223    }
1224
1225    // search from start
1226    for(member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr)
1227    {
1228        if(Player* pl = ObjectAccessor::FindPlayer(itr->guid))
1229        {
1230            if (pl->GetDistance2d(creature) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
1231            {
1232                bool refresh = pl->GetLootGUID()==creature->GetGUID();
1233
1234                //if(refresh)                               // update loot for new looter
1235                //    pl->GetSession()->DoLootRelease(pl->GetLootGUID());
1236                SetLooterGuid(pl->GetGUID());
1237                SendUpdate();
1238                if(refresh)                                 // update loot for new looter
1239                    pl->SendLoot(creature->GetGUID(),LOOT_CORPSE);
1240                return;
1241            }
1242        }
1243    }
1244
1245    SetLooterGuid(0);
1246    SendUpdate();
1247}
1248
1249uint32 Group::CanJoinBattleGroundQueue(uint32 bgTypeId, uint32 bgQueueType, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot)
1250{
1251    // check for min / max count
1252    uint32 memberscount = GetMembersCount();
1253    if(memberscount < MinPlayerCount)
1254        return BG_JOIN_ERR_GROUP_NOT_ENOUGH;
1255    if(memberscount > MaxPlayerCount)
1256        return BG_JOIN_ERR_GROUP_TOO_MANY;
1257
1258    // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.)
1259    Player * reference = GetFirstMember()->getSource();
1260    // no reference found, can't join this way
1261    if(!reference)
1262        return BG_JOIN_ERR_OFFLINE_MEMBER;
1263
1264    uint32 bgQueueId = reference->GetBattleGroundQueueIdFromLevel();
1265    uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot);
1266    uint32 team = reference->GetTeam();
1267
1268    // check every member of the group to be able to join
1269    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1270    {
1271        Player *member = itr->getSource();
1272        // offline member? don't let join
1273        if(!member)
1274            return BG_JOIN_ERR_OFFLINE_MEMBER;
1275        // don't allow cross-faction join as group
1276        if(member->GetTeam() != team)
1277            return BG_JOIN_ERR_MIXED_FACTION;
1278        // not in the same battleground level braket, don't let join
1279        if(member->GetBattleGroundQueueIdFromLevel() != bgQueueId)
1280            return BG_JOIN_ERR_MIXED_LEVELS;
1281        // don't let join rated matches if the arena team id doesn't match
1282        if(isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId)
1283            return BG_JOIN_ERR_MIXED_ARENATEAM;
1284        // don't let join if someone from the group is already in that bg queue
1285        if(member->InBattleGroundQueueForBattleGroundQueueType(bgQueueType))
1286            return BG_JOIN_ERR_GROUP_MEMBER_ALREADY_IN_QUEUE;
1287        // check for deserter debuff in case not arena queue
1288        if(bgTypeId != BATTLEGROUND_AA && !member->CanJoinToBattleground())
1289            return BG_JOIN_ERR_GROUP_DESERTER;
1290        // check if member can join any more battleground queues
1291        if(!member->HasFreeBattleGroundQueueId())
1292            return BG_JOIN_ERR_ALL_QUEUES_USED;
1293    }
1294    return BG_JOIN_ERR_OK;
1295}
1296
1297//===================================================
1298//============== Roll ===============================
1299//===================================================
1300
1301void Roll::targetObjectBuildLink()
1302{
1303    // called from link()
1304    this->getTarget()->addLootValidatorRef(this);
1305}
1306
1307void Group::SetDifficulty(uint8 difficulty)
1308{
1309    m_difficulty = difficulty;
1310    if(!isBGGroup()) CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE leaderGuid ='%u'", m_difficulty, GUID_LOPART(m_leaderGuid));
1311
1312    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1313    {
1314        Player *player = itr->getSource();
1315        if(!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC)
1316            continue;
1317        player->SetDifficulty(difficulty);
1318        player->SendDungeonDifficulty(true);
1319    }
1320}
1321
1322bool Group::InCombatToInstance(uint32 instanceId)
1323{
1324    for(GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next())
1325    {
1326        Player *pPlayer = itr->getSource();
1327        if(pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId)
1328            return true;
1329    }
1330    return false;
1331}
1332
1333void Group::ResetInstances(uint8 method, Player* SendMsgTo)
1334{
1335    if(isBGGroup())
1336        return;
1337
1338    // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND
1339
1340    // we assume that when the difficulty changes, all instances that can be reset will be
1341    uint8 dif = GetDifficulty();
1342
1343    for(BoundInstancesMap::iterator itr = m_boundInstances[dif].begin(); itr != m_boundInstances[dif].end();)
1344    {
1345        InstanceSave *p = itr->second.save;
1346        const MapEntry *entry = sMapStore.LookupEntry(itr->first);
1347        if(!entry || (!p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND))
1348        {
1349            ++itr;
1350            continue;
1351        }
1352
1353        if(method == INSTANCE_RESET_ALL)
1354        {
1355            // the "reset all instances" method can only reset normal maps
1356            if(dif == DIFFICULTY_HEROIC || entry->map_type == MAP_RAID)
1357            {
1358                ++itr;
1359                continue;
1360            }
1361        }
1362
1363        bool isEmpty = true;
1364        // if the map is loaded, reset it
1365        Map *map = MapManager::Instance().FindMap(p->GetMapId(), p->GetInstanceId());
1366        if(map && map->IsDungeon())
1367            isEmpty = ((InstanceMap*)map)->Reset(method);
1368
1369        if(SendMsgTo)
1370        {
1371            if(isEmpty) SendMsgTo->SendResetInstanceSuccess(p->GetMapId());
1372            else SendMsgTo->SendResetInstanceFailed(0, p->GetMapId());
1373        }
1374
1375        if(isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY)
1376        {
1377            // do not reset the instance, just unbind if others are permanently bound to it
1378            if(p->CanReset()) p->DeleteFromDB();
1379            else CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId());
1380            // i don't know for sure if hash_map iterators
1381            m_boundInstances[dif].erase(itr);
1382            itr = m_boundInstances[dif].begin();
1383            // this unloads the instance save unless online players are bound to it
1384            // (eg. permanent binds or GM solo binds)
1385            p->RemoveGroup(this);
1386        }
1387        else
1388            ++itr;
1389    }
1390}
1391
1392InstanceGroupBind* Group::GetBoundInstance(uint32 mapid, uint8 difficulty)
1393{
1394    // some instances only have one difficulty
1395    const MapEntry* entry = sMapStore.LookupEntry(mapid);
1396    if(!entry || !entry->SupportsHeroicMode()) difficulty = DIFFICULTY_NORMAL;
1397
1398    BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1399    if(itr != m_boundInstances[difficulty].end())
1400        return &itr->second;
1401    else
1402        return NULL;
1403}
1404
1405InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load)
1406{
1407    if(save && !isBGGroup())
1408    {
1409        InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()];
1410        if(bind.save)
1411        {
1412            // when a boss is killed or when copying the players's binds to the group
1413            if(permanent != bind.perm || save != bind.save)
1414                if(!load) CharacterDatabase.PExecute("UPDATE group_instance SET instance = '%u', permanent = '%u' WHERE leaderGuid = '%u' AND instance = '%u'", save->GetInstanceId(), permanent, GUID_LOPART(GetLeaderGUID()), bind.save->GetInstanceId());
1415        }
1416        else
1417            if(!load) CharacterDatabase.PExecute("INSERT INTO group_instance (leaderGuid, instance, permanent) VALUES ('%u', '%u', '%u')", GUID_LOPART(GetLeaderGUID()), save->GetInstanceId(), permanent);
1418
1419        if(bind.save != save)
1420        {
1421            if(bind.save) bind.save->RemoveGroup(this);
1422            save->AddGroup(this);
1423        }
1424
1425        bind.save = save;
1426        bind.perm = permanent;
1427        if(!load) sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetLeaderGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty());
1428        return &bind;
1429    }
1430    else
1431        return NULL;
1432}
1433
1434void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload)
1435{
1436    BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid);
1437    if(itr != m_boundInstances[difficulty].end())
1438    {
1439        if(!unload) CharacterDatabase.PExecute("DELETE FROM group_instance WHERE leaderGuid = '%u' AND instance = '%u'", GUID_LOPART(GetLeaderGUID()), itr->second.save->GetInstanceId());
1440        itr->second.save->RemoveGroup(this);                // save can become invalid
1441        m_boundInstances[difficulty].erase(itr);
1442    }
1443}
1444
1445void Group::_homebindIfInstance(Player *player)
1446{
1447    if(player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon())
1448    {
1449        // leaving the group in an instance, the homebind timer is started
1450        // unless the player is permanently saved to the instance
1451        InstanceSave *save = sInstanceSaveManager.GetInstanceSave(player->GetInstanceId());
1452        InstancePlayerBind *playerBind = save ? player->GetBoundInstance(save->GetMapId(), save->GetDifficulty()) : NULL;
1453        if(!playerBind || !playerBind->perm)
1454            player->m_InstanceValid = false;
1455    }
1456}
Note: See TracBrowser for help on using the browser.