diff options
| author | Rat <none@none> | 2010-06-05 23:40:08 +0200 |
|---|---|---|
| committer | Rat <none@none> | 2010-06-05 23:40:08 +0200 |
| commit | 75b80d9f5b02a643c983b2fb1ededed79fd5d133 (patch) | |
| tree | ebd1c2cc12a2715909dd04c1ed147a260c6ceb14 /src/server/game/Groups | |
| parent | 6a9357b13d7ea6bd7d77dbfc6587af9028caa401 (diff) | |
rearranged core files
--HG--
branch : trunk
Diffstat (limited to 'src/server/game/Groups')
| -rw-r--r-- | src/server/game/Groups/Group.cpp | 1867 | ||||
| -rw-r--r-- | src/server/game/Groups/Group.h | 461 | ||||
| -rw-r--r-- | src/server/game/Groups/GroupHandler.cpp | 940 | ||||
| -rw-r--r-- | src/server/game/Groups/GroupRefManager.h | 36 | ||||
| -rw-r--r-- | src/server/game/Groups/GroupReference.cpp | 42 | ||||
| -rw-r--r-- | src/server/game/Groups/GroupReference.h | 44 |
6 files changed, 3390 insertions, 0 deletions
diff --git a/src/server/game/Groups/Group.cpp b/src/server/game/Groups/Group.cpp new file mode 100644 index 00000000000..acd19e02838 --- /dev/null +++ b/src/server/game/Groups/Group.cpp @@ -0,0 +1,1867 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "Common.h" +#include "Opcodes.h" +#include "WorldPacket.h" +#include "WorldSession.h" +#include "Player.h" +#include "World.h" +#include "ObjectMgr.h" +#include "Group.h" +#include "Formulas.h" +#include "ObjectAccessor.h" +#include "BattleGround.h" +#include "BattleGroundMgr.h" +#include "MapManager.h" +#include "InstanceSaveMgr.h" +#include "MapInstanced.h" +#include "Util.h" +#include "LFGMgr.h" + +Group::Group() +{ + m_leaderGuid = 0; + m_groupType = GroupType(0); + m_bgGroup = NULL; + m_lootMethod = LootMethod(0); + m_looterGuid = 0; + m_lootThreshold = ITEM_QUALITY_UNCOMMON; + m_subGroupsCounts = NULL; + m_guid = 0; + m_counter = 0; + m_maxEnchantingLevel= 0; + + for (uint8 i = 0; i < TARGETICONCOUNT; ++i) + m_targetIcons[i] = 0; +} + +Group::~Group() +{ + if (m_bgGroup) + { + sLog.outDebug("Group::~Group: battleground group being deleted."); + if (m_bgGroup->GetBgRaid(ALLIANCE) == this) m_bgGroup->SetBgRaid(ALLIANCE, NULL); + else if (m_bgGroup->GetBgRaid(HORDE) == this) m_bgGroup->SetBgRaid(HORDE, NULL); + else sLog.outError("Group::~Group: battleground group is not linked to the correct battleground."); + } + Rolls::iterator itr; + while (!RollId.empty()) + { + itr = RollId.begin(); + Roll *r = *itr; + RollId.erase(itr); + delete(r); + } + + // it is undefined whether objectmgr (which stores the groups) or instancesavemgr + // will be unloaded first so we must be prepared for both cases + // this may unload some instance saves + for (uint8 i = 0; i < MAX_DIFFICULTY; ++i) + for (BoundInstancesMap::iterator itr2 = m_boundInstances[i].begin(); itr2 != m_boundInstances[i].end(); ++itr2) + itr2->second.save->RemoveGroup(this); + + // Sub group counters clean up + if (m_subGroupsCounts) + delete[] m_subGroupsCounts; +} + +bool Group::Create(const uint64 &guid, const char * name) +{ + uint32 lowguid = objmgr.GenerateLowGuid(HIGHGUID_GROUP); + m_guid = MAKE_NEW_GUID(lowguid, 0, HIGHGUID_GROUP); + m_leaderGuid = guid; + m_leaderName = name; + + m_groupType = isBGGroup() ? GROUPTYPE_BGRAID : GROUPTYPE_NORMAL; + + if (m_groupType & GROUPTYPE_RAID) + _initRaidSubGroupsCounter(); + + m_lootMethod = GROUP_LOOT; + m_lootThreshold = ITEM_QUALITY_UNCOMMON; + m_looterGuid = guid; + + m_dungeonDifficulty = DUNGEON_DIFFICULTY_NORMAL; + m_raidDifficulty = RAID_DIFFICULTY_10MAN_NORMAL; + if (!isBGGroup()) + { + Player *leader = objmgr.GetPlayer(guid); + if (leader) + { + m_dungeonDifficulty = leader->GetDungeonDifficulty(); + m_raidDifficulty = leader->GetRaidDifficulty(); + } + + Player::ConvertInstancesToGroup(leader, this, guid); + + // store group in database + CharacterDatabase.BeginTransaction(); + CharacterDatabase.PExecute("DELETE FROM groups WHERE guid ='%u'", lowguid); + CharacterDatabase.PExecute("DELETE FROM group_member WHERE guid ='%u'", lowguid); + CharacterDatabase.PExecute("INSERT INTO groups (guid,leaderGuid,lootMethod,looterGuid,lootThreshold,icon1,icon2,icon3,icon4,icon5,icon6,icon7,icon8,groupType,difficulty,raiddifficulty) " + "VALUES ('%u','%u','%u','%u','%u','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','" UI64FMTD "','%u','%u','%u')", + lowguid, GUID_LOPART(m_leaderGuid), uint32(m_lootMethod), + GUID_LOPART(m_looterGuid), uint32(m_lootThreshold), m_targetIcons[0], m_targetIcons[1], m_targetIcons[2], m_targetIcons[3], m_targetIcons[4], m_targetIcons[5], m_targetIcons[6], m_targetIcons[7], uint8(m_groupType), uint32(m_dungeonDifficulty), m_raidDifficulty); + if (!AddMember(guid, name)) + { + CharacterDatabase.RollbackTransaction(); + return false; + } + CharacterDatabase.CommitTransaction(); + } + else if (!AddMember(guid, name)) + return false; + + return true; +} + +bool Group::LoadGroupFromDB(const uint64 &groupGuid, QueryResult_AutoPtr result, bool loadMembers) +{ + if (isBGGroup()) + return false; + + uint32 groupLowGuid = GUID_LOPART(groupGuid); + if (!result) + { + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 + result = CharacterDatabase.PQuery("SELECT leaderGuid, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, groupType, difficulty, raiddifficulty FROM groups WHERE guid=%u", groupLowGuid); + + if (!result) + return false; + } + Field *fields = result->Fetch(); + m_guid = groupGuid; + m_leaderGuid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER); + + // group leader not exist + if (!objmgr.GetPlayerNameByGUID(fields[0].GetUInt32(), m_leaderName)) + return false; + + m_lootMethod = (LootMethod)fields[1].GetUInt8(); + m_looterGuid = MAKE_NEW_GUID(fields[2].GetUInt32(), 0, HIGHGUID_PLAYER); + m_lootThreshold = (ItemQualities)fields[3].GetUInt16(); + + for (uint8 i = 0; i < TARGETICONCOUNT; ++i) + m_targetIcons[i] = fields[4+i].GetUInt64(); + + m_groupType = GroupType(fields[12].GetUInt8()); + if (m_groupType & GROUPTYPE_RAID) + _initRaidSubGroupsCounter(); + + uint32 diff = fields[13].GetUInt8(); + if (diff >= MAX_DUNGEON_DIFFICULTY) + diff = DUNGEON_DIFFICULTY_NORMAL; + m_dungeonDifficulty = Difficulty(diff); + + uint32 r_diff = fields[14].GetUInt8(); + if (r_diff >= MAX_RAID_DIFFICULTY) + r_diff = RAID_DIFFICULTY_10MAN_NORMAL; + m_raidDifficulty = Difficulty(r_diff); + + if (loadMembers) + { + result = CharacterDatabase.PQuery("SELECT memberGuid, memberFlags, subgroup FROM group_member WHERE guid=%u", groupLowGuid); + if (!result) + return false; + + do + { + fields = result->Fetch(); + LoadMemberFromDB(fields[0].GetUInt32(), fields[1].GetUInt8(), fields[2].GetUInt8()); + } while (result->NextRow()); + + if (GetMembersCount() < 2) // group too small + return false; + } + + return true; +} + +bool Group::LoadMemberFromDB(uint32 guidLow, uint8 memberFlags, uint8 subgroup) +{ + MemberSlot member; + member.guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER); + + // skip non-existed member + if (!objmgr.GetPlayerNameByGUID(member.guid, member.name)) + { + CharacterDatabase.PQuery("DELETE FROM group_member WHERE memberGuid=%u", guidLow); + return false; + } + + member.group = subgroup; + member.flags = memberFlags; + + m_memberSlots.push_back(member); + + SubGroupCounterIncrease(subgroup); + + return true; +} + +void Group::ConvertToRaid() +{ + m_groupType = GroupType(m_groupType | GROUPTYPE_RAID); + + _initRaidSubGroupsCounter(); + + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE groups SET groupType='%u' WHERE guid='%u'", uint8(m_groupType), GUID_LOPART(m_guid)); + SendUpdate(); + + // update quest related GO states (quest activity dependent from raid membership) + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + if (Player* player = objmgr.GetPlayer(citr->guid)) + player->UpdateForQuestWorldObjects(); +} + +bool Group::AddInvite(Player *player) +{ + if (!player || player->GetGroupInvite()) + return false; + Group* group = player->GetGroup(); + if (group && group->isBGGroup()) + group = player->GetOriginalGroup(); + if (group) + return false; + + RemoveInvite(player); + + m_invitees.insert(player); + + player->SetGroupInvite(this); + + return true; +} + +bool Group::AddLeaderInvite(Player *player) +{ + if (!AddInvite(player)) + return false; + + m_leaderGuid = player->GetGUID(); + m_leaderName = player->GetName(); + return true; +} + +uint32 Group::RemoveInvite(Player *player) +{ + m_invitees.erase(player); + + player->SetGroupInvite(NULL); + return GetMembersCount(); +} + +void Group::RemoveAllInvites() +{ + for (InvitesList::iterator itr=m_invitees.begin(); itr != m_invitees.end(); ++itr) + (*itr)->SetGroupInvite(NULL); + + m_invitees.clear(); +} + +Player* Group::GetInvited(const uint64& guid) const +{ + for (InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr) + { + if ((*itr)->GetGUID() == guid) + return (*itr); + } + return NULL; +} + +Player* Group::GetInvited(const std::string& name) const +{ + for (InvitesList::const_iterator itr = m_invitees.begin(); itr != m_invitees.end(); ++itr) + { + if ((*itr)->GetName() == name) + return (*itr); + } + return NULL; +} + +bool Group::AddMember(const uint64 &guid, const char* name) +{ + if (!_addMember(guid, name)) + return false; + SendUpdate(); + + Player *player = objmgr.GetPlayer(guid); + if (player) + { + if (!IsLeader(player->GetGUID()) && !isBGGroup()) + { + // reset the new member's instances, unless he is currently in one of them + // including raid/heroic instances that they are not permanently bound to! + player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,false); + player->ResetInstances(INSTANCE_RESET_GROUP_JOIN,true); + + if (player->getLevel() >= LEVELREQUIREMENT_HEROIC) + { + if (player->GetDungeonDifficulty() != GetDungeonDifficulty()) + { + player->SetDungeonDifficulty(GetDungeonDifficulty()); + player->SendDungeonDifficulty(true); + } + if (player->GetRaidDifficulty() != GetRaidDifficulty()) + { + player->SetRaidDifficulty(GetRaidDifficulty()); + player->SendRaidDifficulty(true); + } + } + } + player->SetGroupUpdateFlag(GROUP_UPDATE_FULL); + UpdatePlayerOutOfRange(player); + + // quest related GO state dependent from raid memebership + if (isRaidGroup()) + player->UpdateForQuestWorldObjects(); + + if (m_maxEnchantingLevel < player->GetSkillValue(SKILL_ENCHANTING)) + m_maxEnchantingLevel = player->GetSkillValue(SKILL_ENCHANTING); + } + + return true; +} + +uint32 Group::RemoveMember(const uint64 &guid, const uint8 &method) +{ + BroadcastGroupUpdate(); + + if (!isBGGroup()) + sLFGMgr.Leave(NULL, this); + + // remove member and change leader (if need) only if strong more 2 members _before_ member remove + if (GetMembersCount() > (isBGGroup() ? 1 : 2)) // in BG group case allow 1 members group + { + bool leaderChanged = _removeMember(guid); + + if (Player *player = objmgr.GetPlayer(guid)) + { + // quest related GO state dependent from raid membership + if (isRaidGroup()) + player->UpdateForQuestWorldObjects(); + + WorldPacket data; + + if (method == 1) + { + data.Initialize(SMSG_GROUP_UNINVITE, 0); + player->GetSession()->SendLfgUpdateParty(LFG_UPDATETYPE_LEADER); + player->GetSession()->SendPacket(&data); + } + + //we already removed player from group and in player->GetGroup() is his original group! + if (Group* group = player->GetGroup()) + { + group->SendUpdate(); + } + else + { + data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8); + data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0); + data << uint64(m_guid) << uint32(m_counter) << uint32(0) << uint64(0); + player->GetSession()->SendPacket(&data); + } + + _homebindIfInstance(player); + } + + if (leaderChanged) + { + WorldPacket data(SMSG_GROUP_SET_LEADER, (m_memberSlots.front().name.size()+1)); + data << m_memberSlots.front().name; + BroadcastPacket(&data, true); + } + + SendUpdate(); + ResetMaxEnchantingLevel(); + } + // if group before remove <= 2 disband it + else + Disband(); + + return m_memberSlots.size(); +} + +void Group::ChangeLeader(const uint64 &guid) +{ + member_citerator slot = _getMemberCSlot(guid); + + if (slot == m_memberSlots.end()) + return; + + _setLeader(guid); + + WorldPacket data(SMSG_GROUP_SET_LEADER, slot->name.size()+1); + data << slot->name; + BroadcastPacket(&data, true); + SendUpdate(); +} + +void Group::Disband(bool hideDestroy) +{ + Player *player; + + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + { + player = objmgr.GetPlayer(citr->guid); + if (!player) + continue; + + //we cannot call _removeMember because it would invalidate member iterator + //if we are removing player from battleground raid + if (isBGGroup()) + player->RemoveFromBattleGroundRaid(); + else + { + //we can remove player who is in battleground from his original group + if (player->GetOriginalGroup() == this) + player->SetOriginalGroup(NULL); + else + player->SetGroup(NULL); + + player->GetSession()->SendLfgUpdateParty(LFG_UPDATETYPE_GROUP_DISBAND); + player->GetSession()->SendLfgUpdateParty(LFG_UPDATETYPE_LEADER); + } + + // quest related GO state dependent from raid membership + if (isRaidGroup()) + player->UpdateForQuestWorldObjects(); + + if (!player->GetSession()) + continue; + + WorldPacket data; + if (!hideDestroy) + { + data.Initialize(SMSG_GROUP_DESTROYED, 0); + player->GetSession()->SendPacket(&data); + } + + //we already removed player from group and in player->GetGroup() is his original group, send update + if (Group* group = player->GetGroup()) + { + group->SendUpdate(); + } + else + { + data.Initialize(SMSG_GROUP_LIST, 1+1+1+1+8+4+4+8); + data << uint8(0x10) << uint8(0) << uint8(0) << uint8(0); + data << uint64(m_guid) << uint32(m_counter) << uint32(0) << uint64(0); + player->GetSession()->SendPacket(&data); + } + + _homebindIfInstance(player); + } + RollId.clear(); + m_memberSlots.clear(); + + RemoveAllInvites(); + + if (!isBGGroup()) + { + uint32 lowguid = GUID_LOPART(m_guid); + CharacterDatabase.BeginTransaction(); + CharacterDatabase.PExecute("DELETE FROM groups WHERE guid=%u", lowguid); + CharacterDatabase.PExecute("DELETE FROM group_member WHERE guid=%u", lowguid); + CharacterDatabase.CommitTransaction(); + ResetInstances(INSTANCE_RESET_GROUP_DISBAND, false, NULL); + ResetInstances(INSTANCE_RESET_GROUP_DISBAND, true, NULL); + // FIXME - Safe check! Debug purposes - Will remove after a time if got no reports + QueryResult_AutoPtr result = CharacterDatabase.PQuery("SELECT COUNT(1) FROM group_instance WHERE guid=%u", lowguid); + if (result) + { + sLog.outError("Group::Disband: %u instances are not being properly deleted from group %u", (*result)[0].GetUInt8(), lowguid); + CharacterDatabase.PExecute("DELETE FROM group_instance WHERE guid=%u", lowguid); + } + } + + m_guid = 0; + m_leaderGuid = 0; + m_leaderName = ""; +} + +/*********************************************************/ +/*** LOOT SYSTEM ***/ +/*********************************************************/ + +void Group::SendLootStartRoll(uint32 CountDown, uint32 mapid, const Roll &r) +{ + WorldPacket data(SMSG_LOOT_START_ROLL, (8+4+4+4+4+4+4+1)); + data << uint64(r.itemGUID); // guid of rolled item + data << uint32(mapid); // 3.3.3 mapid + data << uint32(r.totalPlayersRolling); // maybe the number of players rolling for it??? + data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for + data << uint32(r.itemRandomSuffix); // randomSuffix + data << uint32(r.itemRandomPropId); // item random property ID + data << uint32(r.itemCount); // items in stack + data << uint32(CountDown); // the countdown time to choose "need" or "greed" + data << uint8(r.rollVoteMask); // roll type mask + + for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second == NOT_EMITED_YET) + p->GetSession()->SendPacket(&data); + } +} + +void Group::SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r) +{ + WorldPacket data(SMSG_LOOT_ROLL, (8+4+8+4+4+4+1+1+1)); + data << uint64(SourceGuid); // guid of the item rolled + data << uint32(0); // unknown, maybe amount of players + data << uint64(TargetGuid); + data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for + data << uint32(r.itemRandomSuffix); // randomSuffix + data << uint32(r.itemRandomPropId); // Item random property ID + data << uint8(RollNumber); // 0: "Need for: [item name]" > 127: "you passed on: [item name]" Roll number + data << uint8(RollType); // 0: "Need for: [item name]" 0: "You have selected need for [item name] 1: need roll 2: greed roll + data << uint8(0); // auto pass on NeedBeforeGreed loot because player cannot use the object + + for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second != NOT_VALID) + p->GetSession()->SendPacket(&data); + } +} + +void Group::SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r) +{ + WorldPacket data(SMSG_LOOT_ROLL_WON, (8+4+4+4+4+8+1+1)); + data << uint64(SourceGuid); // guid of the item rolled + data << uint32(0); // unknown, maybe amount of players + data << uint32(r.itemid); // the itemEntryId for the item that shall be rolled for + data << uint32(r.itemRandomSuffix); // randomSuffix + data << uint32(r.itemRandomPropId); // Item random property + data << uint64(TargetGuid); // guid of the player who won. + data << uint8(RollNumber); // rollnumber realted to SMSG_LOOT_ROLL + data << uint8(RollType); // Rolltype related to SMSG_LOOT_ROLL + + for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second != NOT_VALID) + p->GetSession()->SendPacket(&data); + } +} + +void Group::SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r) +{ + WorldPacket data(SMSG_LOOT_ALL_PASSED, (8+4+4+4+4)); + data << uint64(r.itemGUID); // Guid of the item rolled + data << uint32(NumberOfPlayers); // The number of players rolling for it??? + data << uint32(r.itemid); // The itemEntryId for the item that shall be rolled for + data << uint32(r.itemRandomPropId); // Item random property ID + data << uint32(r.itemRandomSuffix); // Item random suffix ID + + for (Roll::PlayerVote::const_iterator itr=r.playerVote.begin(); itr != r.playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second != NOT_VALID) + p->GetSession()->SendPacket(&data); + } +} + +// notify group members which player is the allowed looter for the given creature +void Group::SendLooter(Creature *pCreature, Player *pLooter) +{ + assert(pCreature); + + WorldPacket data(SMSG_LOOT_LIST, (8+8)); + data << uint64(pCreature->GetGUID()); + data << uint8(0); // unk1 + + if (pLooter) + data.append(pLooter->GetPackGUID()); + else + data << uint8(0); + + BroadcastPacket(&data, false); +} + +void Group::GroupLoot(Loot *loot, WorldObject* pLootedObject) +{ + std::vector<LootItem>::iterator i; + ItemPrototype const *item; + uint8 itemSlot = 0; + + for (i = loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot) + { + if (i->freeforall) + continue; + + item = objmgr.GetItemPrototype(i->itemid); + if (!item) + { + //sLog.outDebug("Group::GroupLoot: missing item prototype for item with id: %d", i->itemid); + continue; + } + + //roll for over-threshold item if it's one-player loot + if (item->Quality >= uint32(m_lootThreshold)) + { + uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM); + Roll* r = new Roll(newitemGUID,*i); + + //a vector is filled with only near party members + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *member = itr->getSource(); + if (!member || !member->GetSession()) + continue; + if (i->AllowedForPlayer(member)) + { + if (member->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + { + r->totalPlayersRolling++; + + if (member->GetPassOnGroupLoot()) + { + r->playerVote[member->GetGUID()] = PASS; + r->totalPass++; + // can't broadcast the pass now. need to wait until all rolling players are known. + } + else + { + r->playerVote[member->GetGUID()] = NOT_EMITED_YET; + } + } + } + } + + if (r->totalPlayersRolling > 0) + { + r->setLoot(loot); + r->itemSlot = itemSlot; + if (item->DisenchantID && m_maxEnchantingLevel >= item->RequiredDisenchantSkill) + r->rollVoteMask |= ROLL_FLAG_TYPE_DISENCHANT; + + loot->items[itemSlot].is_blocked = true; + + // If there is any "auto pass", broadcast the pass now. + if (r->totalPass) + { + for (Roll::PlayerVote::const_iterator itr=r->playerVote.begin(); itr != r->playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second == PASS) + SendLootRoll(newitemGUID, p->GetGUID(), 128, ROLL_PASS, *r); + } + } + + SendLootStartRoll(60000, pLootedObject->GetMapId(), *r); + + RollId.push_back(r); + + if (Creature* creature = dynamic_cast<Creature *>(pLootedObject)) + { + creature->m_groupLootTimer = 60000; + creature->lootingGroupGUID = GetGUID(); + } + else if (GameObject* go = dynamic_cast<GameObject *>(pLootedObject)) + { + go->m_groupLootTimer = 60000; + go->lootingGroupGUID = GetGUID(); + } + } + else + { + delete r; + } + } + else + i->is_underthreshold=1; + } +} + +void Group::NeedBeforeGreed(Loot *loot, WorldObject* pLootedObject) +{ + ItemPrototype const *item; + uint8 itemSlot = 0; + for (std::vector<LootItem>::iterator i=loot->items.begin(); i != loot->items.end(); ++i, ++itemSlot) + { + if (i->freeforall) + continue; + + item = objmgr.GetItemPrototype(i->itemid); + + //roll for over-threshold item if it's one-player loot + if (item->Quality >= uint32(m_lootThreshold)) + { + uint64 newitemGUID = MAKE_NEW_GUID(objmgr.GenerateLowGuid(HIGHGUID_ITEM),0,HIGHGUID_ITEM); + Roll* r=new Roll(newitemGUID,*i); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *playerToRoll = itr->getSource(); + if (!playerToRoll || !playerToRoll->GetSession()) + continue; + + if (playerToRoll->CanUseItem(item) && i->AllowedForPlayer(playerToRoll)) + { + if (playerToRoll->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + { + r->totalPlayersRolling++; + + if (playerToRoll->GetPassOnGroupLoot()) + { + r->playerVote[playerToRoll->GetGUID()] = PASS; + r->totalPass++; + // can't broadcast the pass now. need to wait until all rolling players are known. + } + else + { + r->playerVote[playerToRoll->GetGUID()] = NOT_EMITED_YET; + } + } + } + } + + if (r->totalPlayersRolling > 0) + { + r->setLoot(loot); + r->itemSlot = itemSlot; + if (item->DisenchantID && m_maxEnchantingLevel >= item->RequiredDisenchantSkill) + r->rollVoteMask |= ROLL_FLAG_TYPE_DISENCHANT; + + if (item->Flags2 & ITEM_FLAGS_EXTRA_NEED_ROLL_DISABLED) + r->rollVoteMask &= ~ROLL_FLAG_TYPE_NEED; + + loot->items[itemSlot].is_blocked = true; + + // If there is any "auto pass", broadcast the pass now. + if (r->totalPass) + { + for (Roll::PlayerVote::const_iterator itr=r->playerVote.begin(); itr != r->playerVote.end(); ++itr) + { + Player *p = objmgr.GetPlayer(itr->first); + if (!p || !p->GetSession()) + continue; + + if (itr->second == PASS) + SendLootRoll(newitemGUID, p->GetGUID(), 128, ROLL_PASS, *r); + } + } + + SendLootStartRoll(60000, pLootedObject->GetMapId(), *r); + + RollId.push_back(r); + + if (Creature* creature = dynamic_cast<Creature *>(pLootedObject)) + { + creature->m_groupLootTimer = 60000; + creature->lootingGroupGUID = GetGUID(); + } + } + else + { + delete r; + } + } + else + i->is_underthreshold=1; + } +} + +void Group::MasterLoot(Loot* /*loot*/, WorldObject* pLootedObject) +{ + sLog.outDebug("Group::MasterLoot (SMSG_LOOT_MASTER_LIST, 330)"); + + uint32 real_count = 0; + + WorldPacket data(SMSG_LOOT_MASTER_LIST, 330); + data << (uint8)GetMembersCount(); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *looter = itr->getSource(); + if (!looter->IsInWorld()) + continue; + + if (looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + { + data << uint64(looter->GetGUID()); + ++real_count; + } + } + + data.put<uint8>(0,real_count); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *looter = itr->getSource(); + if (looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + looter->GetSession()->SendPacket(&data); + } +} + +void Group::CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choice) +{ + Rolls::iterator rollI = GetRoll(Guid); + if (rollI == RollId.end()) + return; + Roll* roll = *rollI; + + Roll::PlayerVote::iterator itr = roll->playerVote.find(playerGUID); + // this condition means that player joins to the party after roll begins + if (itr == roll->playerVote.end()) + return; + + if (roll->getLoot()) + if (roll->getLoot()->items.empty()) + return; + + switch (Choice) + { + case ROLL_PASS: // Player choose pass + SendLootRoll(0, playerGUID, 128, ROLL_PASS, *roll); + ++roll->totalPass; + itr->second = PASS; + break; + case ROLL_NEED: // player choose Need + SendLootRoll(0, playerGUID, 0, 0, *roll); + ++roll->totalNeed; + itr->second = NEED; + break; + case ROLL_GREED: // player choose Greed + SendLootRoll(0, playerGUID, 128, ROLL_GREED, *roll); + ++roll->totalGreed; + itr->second = GREED; + break; + case ROLL_DISENCHANT: // player choose Disenchant + SendLootRoll(0, playerGUID, 128, ROLL_DISENCHANT, *roll); + ++roll->totalGreed; + itr->second = DISENCHANT; + break; + } + + if (roll->totalPass + roll->totalNeed + roll->totalGreed >= roll->totalPlayersRolling) + { + CountTheRoll(rollI, NumberOfPlayers); + } +} + +//called when roll timer expires +void Group::EndRoll(Loot *pLoot) +{ + for (Rolls::iterator itr = RollId.begin(); itr != RollId.end();) + { + if ((*itr)->getLoot() == pLoot) { + CountTheRoll(itr, GetMembersCount()); //i don't have to edit player votes, who didn't vote ... he will pass + itr = RollId.begin(); + } + else + itr++; + } +} + +void Group::CountTheRoll(Rolls::iterator rollI, uint32 NumberOfPlayers) +{ + Roll* roll = *rollI; + if (!roll->isValid()) // is loot already deleted ? + { + RollId.erase(rollI); + delete roll; + return; + } + //end of the roll + if (roll->totalNeed > 0) + { + if (!roll->playerVote.empty()) + { + uint8 maxresul = 0; + uint64 maxguid = (*roll->playerVote.begin()).first; + Player *player; + + for (Roll::PlayerVote::const_iterator itr=roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr) + { + if (itr->second != NEED) + continue; + + uint8 randomN = urand(1, 100); + SendLootRoll(0, itr->first, randomN, ROLL_NEED, *roll); + if (maxresul < randomN) + { + maxguid = itr->first; + maxresul = randomN; + } + } + SendLootRollWon(0, maxguid, maxresul, ROLL_NEED, *roll); + player = objmgr.GetPlayer(maxguid); + + if (player && player->GetSession()) + { + player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED_ON_LOOT, roll->itemid, maxresul); + + ItemPosCountVec dest; + LootItem *item = &(roll->getLoot()->items[roll->itemSlot]); + uint8 msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count); + if (msg == EQUIP_ERR_OK) + { + item->is_looted = true; + roll->getLoot()->NotifyItemRemoved(roll->itemSlot); + roll->getLoot()->unlootedCount--; + player->StoreNewItem(dest, roll->itemid, true, item->randomPropertyId); + } + else + { + item->is_blocked = false; + player->SendEquipError(msg, NULL, NULL); + } + } + } + } + else if (roll->totalGreed > 0) + { + if (!roll->playerVote.empty()) + { + uint8 maxresul = 0; + uint64 maxguid = (*roll->playerVote.begin()).first; + Player *player; + RollVote rollvote = NOT_VALID; + + Roll::PlayerVote::iterator itr; + for (itr = roll->playerVote.begin(); itr != roll->playerVote.end(); ++itr) + { + if (itr->second != GREED && itr->second != DISENCHANT) + continue; + + uint8 randomN = urand(1, 100); + SendLootRoll(0, itr->first, randomN, itr->second, *roll); + if (maxresul < randomN) + { + maxguid = itr->first; + maxresul = randomN; + rollvote = itr->second; + } + } + SendLootRollWon(0, maxguid, maxresul, rollvote, *roll); + player = objmgr.GetPlayer(maxguid); + + if (player && player->GetSession()) + { + player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED_ON_LOOT, roll->itemid, maxresul); + + LootItem *item = &(roll->getLoot()->items[roll->itemSlot]); + + if (rollvote == GREED) + { + ItemPosCountVec dest; + uint8 msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, roll->itemid, item->count); + if (msg == EQUIP_ERR_OK) + { + item->is_looted = true; + roll->getLoot()->NotifyItemRemoved(roll->itemSlot); + roll->getLoot()->unlootedCount--; + player->StoreNewItem(dest, roll->itemid, true, item->randomPropertyId); + } + else + { + item->is_blocked = false; + player->SendEquipError(msg, NULL, NULL); + } + } + else if (rollvote == DISENCHANT) + { + item->is_looted = true; + roll->getLoot()->NotifyItemRemoved(roll->itemSlot); + roll->getLoot()->unlootedCount--; + ItemPrototype const *pProto = ObjectMgr::GetItemPrototype(roll->itemid); + player->AutoStoreLoot(pProto->DisenchantID, LootTemplates_Disenchant, true); + } + } + } + } + else + { + SendLootAllPassed(NumberOfPlayers, *roll); + + // remove is_blocked so that the item is lootable by all players + LootItem *item = &(roll->getLoot()->items[roll->itemSlot]); + if (item) + item->is_blocked = false; + } + RollId.erase(rollI); + delete roll; +} + +void Group::SetTargetIcon(uint8 id, uint64 whoGuid, uint64 targetGuid) +{ + if (id >= TARGETICONCOUNT) + return; + + // clean other icons + if (targetGuid != 0) + for (int i=0; i<TARGETICONCOUNT; ++i) + if (m_targetIcons[i] == targetGuid) + SetTargetIcon(i, 0, 0); + + m_targetIcons[id] = targetGuid; + + WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+8+1+8)); + data << uint8(0); // set targets + data << uint64(whoGuid); + data << uint8(id); + data << uint64(targetGuid); + BroadcastPacket(&data, true); +} + +void Group::GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level) +{ + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player* member = itr->getSource(); + if (!member || !member->isAlive()) // only for alive + continue; + + if (!member->IsAtGroupRewardDistance(victim)) // at req. distance + continue; + + ++count; + sum_level += member->getLevel(); + if (!member_with_max_level || member_with_max_level->getLevel() < member->getLevel()) + member_with_max_level = member; + + uint32 gray_level = Trinity::XP::GetGrayLevel(member->getLevel()); + if (victim->getLevel() > gray_level && (!not_gray_member_with_max_level + || not_gray_member_with_max_level->getLevel() < member->getLevel())) + not_gray_member_with_max_level = member; + } +} + +void Group::SendTargetIconList(WorldSession *session) +{ + if (!session) + return; + + WorldPacket data(MSG_RAID_TARGET_UPDATE, (1+TARGETICONCOUNT*9)); + data << uint8(1); // list targets + + for (int i=0; i<TARGETICONCOUNT; ++i) + { + if (m_targetIcons[i] == 0) + continue; + + data << uint8(i); + data << uint64(m_targetIcons[i]); + } + + session->SendPacket(&data); +} + +void Group::SendUpdate() +{ + Player *player; + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + { + player = objmgr.GetPlayer(citr->guid); + if (!player || !player->GetSession() || player->GetGroup() != this) + continue; + + WorldPacket data(SMSG_GROUP_LIST, (1+1+1+1+1+4+8+4+4+(GetMembersCount()-1)*(13+8+1+1+1+1)+8+1+8+1+1+1+1)); + data << uint8(m_groupType); // group type (flags in 3.3) + data << uint8(citr->group); + data << uint8(citr->flags); + data << uint8(isBGGroup() ? 1 : 0); // 2.0.x, isBattleGroundGroup? + if (m_groupType & GROUPTYPE_LFD) + { + data << uint8(0); + data << uint32(0); + } + data << uint64(m_guid); + data << uint32(m_counter++); // 3.3, value increases every time this packet gets sent + data << uint32(GetMembersCount()-1); + for (member_citerator citr2 = m_memberSlots.begin(); citr2 != m_memberSlots.end(); ++citr2) + { + if (citr->guid == citr2->guid) + continue; + Player* member = objmgr.GetPlayer(citr2->guid); + + uint8 onlineState = (member) ? MEMBER_STATUS_ONLINE : MEMBER_STATUS_OFFLINE; + onlineState = onlineState | ((isBGGroup()) ? MEMBER_STATUS_PVP : 0); + + data << citr2->name; + data << uint64(citr2->guid); // guid + data << uint8(onlineState); // online-state + data << uint8(citr2->group); // groupid + data << uint8(citr2->flags); // See enum GroupMemberFlags + data << uint8(0); // 3.3 + } + data << uint64(m_leaderGuid); // leader guid + if (GetMembersCount()-1) + { + data << uint8(m_lootMethod); // loot method + data << uint64(m_looterGuid); // looter guid + data << uint8(m_lootThreshold); // loot threshold + data << uint8(m_dungeonDifficulty); // Dungeon Difficulty + data << uint8(m_raidDifficulty); // Raid Difficulty + data << uint8(0); // 3.3 + } + player->GetSession()->SendPacket(&data); + } +} + +void Group::UpdatePlayerOutOfRange(Player* pPlayer) +{ + if (!pPlayer || !pPlayer->IsInWorld()) + return; + + Player *player; + WorldPacket data; + pPlayer->GetSession()->BuildPartyMemberStatsChangedPacket(pPlayer, &data); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + player = itr->getSource(); + if (player && player != pPlayer && !pPlayer->isVisibleFor(player)) + player->GetSession()->SendPacket(&data); + } +} + +void Group::BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group, uint64 ignore) +{ + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *pl = itr->getSource(); + if (!pl || (ignore != 0 && pl->GetGUID() == ignore) || (ignorePlayersInBGRaid && pl->GetGroup() != this)) + continue; + + if (pl->GetSession() && (group == -1 || itr->getSubGroup() == group)) + pl->GetSession()->SendPacket(packet); + } +} + +void Group::BroadcastReadyCheck(WorldPacket *packet) +{ + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *pl = itr->getSource(); + if (pl && pl->GetSession()) + if (IsLeader(pl->GetGUID()) || IsAssistant(pl->GetGUID())) + pl->GetSession()->SendPacket(packet); + } +} + +void Group::OfflineReadyCheck() +{ + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + { + Player *pl = objmgr.GetPlayer(citr->guid); + if (!pl || !pl->GetSession()) + { + WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9); + data << uint64(citr->guid); + data << uint8(0); + BroadcastReadyCheck(&data); + } + } +} + +bool Group::_addMember(const uint64 &guid, const char* name) +{ + // get first not-full group + uint8 groupid = 0; + if (m_subGroupsCounts) + { + bool groupFound = false; + for (; groupid < MAXRAIDSIZE/MAXGROUPSIZE; ++groupid) + { + if (m_subGroupsCounts[groupid] < MAXGROUPSIZE) + { + groupFound = true; + break; + } + } + // We are raid group and no one slot is free + if (!groupFound) + return false; + } + + return _addMember(guid, name, groupid); +} + +bool Group::_addMember(const uint64 &guid, const char* name, uint8 group) +{ + if (IsFull()) + return false; + + if (!guid) + return false; + + Player *player = objmgr.GetPlayer(guid); + + MemberSlot member; + member.guid = guid; + member.name = name; + member.group = group; + member.flags = 0; + m_memberSlots.push_back(member); + + SubGroupCounterIncrease(group); + + if (player) + { + player->SetGroupInvite(NULL); + //if player is in group and he is being added to BG raid group, then call SetBattleGroundRaid() + if (player->GetGroup() && isBGGroup()) + player->SetBattleGroundRaid(this, group); + //if player is in bg raid and we are adding him to normal group, then call SetOriginalGroup() + else if (player->GetGroup()) + player->SetOriginalGroup(this, group); + //if player is not in group, then call set group + else + player->SetGroup(this, group); + // if the same group invites the player back, cancel the homebind timer + InstanceGroupBind *bind = GetBoundInstance(player); + if (bind && bind->save->GetInstanceId() == player->GetInstanceId()) + player->m_InstanceValid = true; + } + + if (!isRaidGroup()) // reset targetIcons for non-raid-groups + { + for (uint8 i = 0; i < TARGETICONCOUNT; ++i) + m_targetIcons[i] = 0; + } + + if (!isBGGroup()) + { + // insert into group table + CharacterDatabase.PExecute("INSERT INTO group_member(guid,memberGuid,memberFlags,subgroup) VALUES(%u,%u,%u,%u)", GUID_LOPART(m_guid), GUID_LOPART(member.guid), member.flags, member.group); + } + + return true; +} + +bool Group::_removeMember(const uint64 &guid) +{ + Player *player = objmgr.GetPlayer(guid); + if (player) + { + //if we are removing player from battleground raid + if (isBGGroup()) + player->RemoveFromBattleGroundRaid(); + else + { + //we can remove player who is in battleground from his original group + if (player->GetOriginalGroup() == this) + player->SetOriginalGroup(NULL); + else + player->SetGroup(NULL); + player->GetSession()->SendLfgUpdateParty(LFG_UPDATETYPE_LEADER); + } + } + + _removeRolls(guid); + + member_witerator slot = _getMemberWSlot(guid); + if (slot != m_memberSlots.end()) + { + SubGroupCounterDecrease(slot->group); + + m_memberSlots.erase(slot); + } + + if (!isBGGroup()) + CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid=%u", GUID_LOPART(guid)); + + if (m_leaderGuid == guid) // leader was removed + { + if (GetMembersCount() > 0) + _setLeader(m_memberSlots.front().guid); + return true; + } + + return false; +} + +void Group::_setLeader(const uint64 &guid) +{ + member_witerator slot = _getMemberWSlot(guid); + if (slot == m_memberSlots.end()) + return; + + if (!isBGGroup()) + { + // TODO: set a time limit to have this function run rarely cause it can be slow + CharacterDatabase.BeginTransaction(); + + // update the group's bound instances when changing leaders + + // remove all permanent binds from the group + // in the DB also remove solo binds that will be replaced with permbinds + // from the new leader + CharacterDatabase.PExecute( + "DELETE FROM group_instance WHERE guid=%u AND (permanent = 1 OR " + "instance IN (SELECT instance FROM character_instance WHERE guid = '%u')" + ")", GUID_LOPART(m_guid), GUID_LOPART(slot->guid) + ); + + Player *player = objmgr.GetPlayer(slot->guid); + if (player) + { + for (uint8 i = 0; i < MAX_DIFFICULTY; ++i) + { + for (BoundInstancesMap::iterator itr = m_boundInstances[i].begin(); itr != m_boundInstances[i].end();) + { + if (itr->second.perm) + { + itr->second.save->RemoveGroup(this); + m_boundInstances[i].erase(itr++); + } + else + ++itr; + } + } + } + + // copy the permanent binds from the new leader to the group + // overwriting the solo binds with permanent ones if necessary + // in the DB those have been deleted already + Player::ConvertInstancesToGroup(player, this, slot->guid); + + // update the group leader + CharacterDatabase.PExecute("UPDATE groups SET leaderGuid='%u' WHERE guid='%u'", GUID_LOPART(slot->guid), GUID_LOPART(m_guid)); + CharacterDatabase.CommitTransaction(); + } + + m_leaderGuid = slot->guid; + m_leaderName = slot->name; + ToggleGroupMemberFlag(slot, MEMBER_FLAG_ASSISTANT, false); +} + +void Group::_removeRolls(const uint64 &guid) +{ + for (Rolls::iterator it = RollId.begin(); it != RollId.end(); ++it) + { + Roll* roll = *it; + Roll::PlayerVote::iterator itr2 = roll->playerVote.find(guid); + if (itr2 == roll->playerVote.end()) + continue; + + if (itr2->second == GREED || itr2->second == DISENCHANT) --roll->totalGreed; + if (itr2->second == NEED) --roll->totalNeed; + if (itr2->second == PASS) --roll->totalPass; + if (itr2->second != NOT_VALID) --roll->totalPlayersRolling; + + roll->playerVote.erase(itr2); + + CountRollVote(guid, roll->itemGUID, GetMembersCount()-1, MAX_ROLL_TYPE); + } +} + +bool Group::_setMembersGroup(const uint64 &guid, const uint8 &group) +{ + member_witerator slot = _getMemberWSlot(guid); + if (slot == m_memberSlots.end()) + return false; + + slot->group = group; + + SubGroupCounterIncrease(group); + + if (!isBGGroup()) CharacterDatabase.PExecute("UPDATE group_member SET subgroup='%u' WHERE memberGuid='%u'", group, GUID_LOPART(guid)); + + return true; +} + +bool Group::_setAssistantFlag(const uint64 &guid, const bool &apply) +{ + member_witerator slot = _getMemberWSlot(guid); + if (slot == m_memberSlots.end()) + return false; + + ToggleGroupMemberFlag(slot, MEMBER_FLAG_ASSISTANT, apply); + + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid)); + return true; +} + +bool Group::_setMainTank(const uint64 &guid, const bool &apply) +{ + member_witerator slot = _getMemberWSlot(guid); // First check member slots to see if the target exists + if (slot == m_memberSlots.end()) + return false; + + RemoveUniqueGroupMemberFlag(MEMBER_FLAG_MAINTANK); // Remove main tank flag from current if any. + ToggleGroupMemberFlag(slot, MEMBER_FLAG_MAINTANK, apply); // And apply main tank flag on new main tank. + + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid)); + return true; +} + +bool Group::_setMainAssistant(const uint64 &guid, const bool &apply) +{ + member_witerator slot = _getMemberWSlot(guid); + if (slot == m_memberSlots.end()) + return false; + + RemoveUniqueGroupMemberFlag(MEMBER_FLAG_MAINASSIST); // Remove main assist flag from current if any. + ToggleGroupMemberFlag(slot, MEMBER_FLAG_MAINASSIST, apply); // Apply main assist flag on new main assist. + + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE group_member SET memberFlags='%u' WHERE memberGuid='%u'", slot->flags, GUID_LOPART(guid)); + return true; +} + +bool Group::SameSubGroup(Player const* member1, Player const* member2) const +{ + if (!member1 || !member2) return false; + if (member1->GetGroup() != this || member2->GetGroup() != this) return false; + else return member1->GetSubGroup() == member2->GetSubGroup(); +} + +// allows setting subgroup for offline members +void Group::ChangeMembersGroup(const uint64 &guid, const uint8 &group) +{ + if (!isRaidGroup()) + return; + Player *player = objmgr.GetPlayer(guid); + + if (!player) + { + uint8 prevSubGroup; + prevSubGroup = GetMemberGroup(guid); + + SubGroupCounterDecrease(prevSubGroup); + + if (_setMembersGroup(guid, group)) + SendUpdate(); + } + else + // This methods handles itself groupcounter decrease + ChangeMembersGroup(player, group); +} + +// only for online members +void Group::ChangeMembersGroup(Player *player, const uint8 &group) +{ + if (!player || !isRaidGroup()) + return; + if (_setMembersGroup(player->GetGUID(), group)) + { + uint8 prevSubGroup = player->GetSubGroup(); + if (player->GetGroup() == this) + player->GetGroupRef().setSubGroup(group); + //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 + else + { + prevSubGroup = player->GetOriginalSubGroup(); + player->GetOriginalGroupRef().setSubGroup(group); + } + SubGroupCounterDecrease(prevSubGroup); + + SendUpdate(); + } +} + +// Retrieve the next Round-Roubin player for the group +// +// No update done if loot method is Master or FFA. +// +// If the RR player is not yet set for the group, the first group member becomes the round-robin player. +// If the RR player is set, the next player in group becomes the round-robin player. +// +// If ifneed is true, +// the current RR player is checked to be near the looted object. +// if yes, no update done. +// if not, he looses his turn. +void Group::UpdateLooterGuid(WorldObject* pLootedObject, bool ifneed) +{ + switch (GetLootMethod()) + { + case MASTER_LOOT: + case FREE_FOR_ALL: + return; + default: + // round robin style looting applies for all low + // quality items in each loot method except free for all and master loot + break; + } + + uint64 oldLooterGUID = GetLooterGuid(); + member_citerator guid_itr = _getMemberCSlot(oldLooterGUID); + if (guid_itr != m_memberSlots.end()) + { + if (ifneed) + { + // not update if only update if need and ok + Player* looter = ObjectAccessor::FindPlayer(guid_itr->guid); + if (looter && looter->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + return; + } + ++guid_itr; + } + + // search next after current + Player *pNewLooter = NULL; + for (member_citerator itr = guid_itr; itr != m_memberSlots.end(); ++itr) + { + if (Player* pl = ObjectAccessor::FindPlayer(itr->guid)) + if (pl->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + { + pNewLooter = pl; + break; + } + } + + if (!pNewLooter) + { + // search from start + for (member_citerator itr = m_memberSlots.begin(); itr != guid_itr; ++itr) + { + if (Player* pl = ObjectAccessor::FindPlayer(itr->guid)) + if (pl->IsWithinDistInMap(pLootedObject,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false)) + { + pNewLooter = pl; + break; + } + } + } + + if (pNewLooter) + { + if (oldLooterGUID != pNewLooter->GetGUID()) + { + SetLooterGuid(pNewLooter->GetGUID()); + SendUpdate(); + } + } + else + { + SetLooterGuid(0); + SendUpdate(); + } +} + +GroupJoinBattlegroundResult Group::CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 /*MaxPlayerCount*/, bool isRated, uint32 arenaSlot) +{ + BattlemasterListEntry const* bgEntry = sBattlemasterListStore.LookupEntry(bgOrTemplate->GetTypeID()); + if (!bgEntry) + return ERR_GROUP_JOIN_BATTLEGROUND_FAIL; // shouldn't happen + + // check for min / max count + uint32 memberscount = GetMembersCount(); + + // only check for MinPlayerCount since MinPlayerCount == MaxPlayerCount for arenas... + if (bgOrTemplate->isArena() && memberscount != MinPlayerCount) + return ERR_ARENA_TEAM_PARTY_SIZE; + + if (memberscount > bgEntry->maxGroupSize) // no MinPlayerCount for battlegrounds + return ERR_BATTLEGROUND_NONE; // ERR_GROUP_JOIN_BATTLEGROUND_TOO_MANY handled on client side + + // get a player as reference, to compare other players' stats to (arena team id, queue id based on level, etc.) + Player * reference = GetFirstMember()->getSource(); + // no reference found, can't join this way + if (!reference) + return ERR_BATTLEGROUND_JOIN_FAILED; + + PvPDifficultyEntry const* bracketEntry = GetBattlegroundBracketByLevel(bgOrTemplate->GetMapId(),reference->getLevel()); + if (!bracketEntry) + return ERR_BATTLEGROUND_JOIN_FAILED; + + uint32 arenaTeamId = reference->GetArenaTeamId(arenaSlot); + uint32 team = reference->GetTeam(); + + BattleGroundQueueTypeId bgQueueTypeIdRandom = BattleGroundMgr::BGQueueTypeId(BATTLEGROUND_RB, 0); + + // check every member of the group to be able to join + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *member = itr->getSource(); + // offline member? don't let join + if (!member) + return ERR_BATTLEGROUND_JOIN_FAILED; + // don't allow cross-faction join as group + if (member->GetTeam() != team) + return ERR_BATTLEGROUND_JOIN_TIMED_OUT; + // not in the same battleground level braket, don't let join + PvPDifficultyEntry const* memberBracketEntry = GetBattlegroundBracketByLevel(bracketEntry->mapId,member->getLevel()); + if (memberBracketEntry != bracketEntry) + return ERR_BATTLEGROUND_JOIN_RANGE_INDEX; + // don't let join rated matches if the arena team id doesn't match + if (isRated && member->GetArenaTeamId(arenaSlot) != arenaTeamId) + return ERR_BATTLEGROUND_JOIN_FAILED; + // don't let join if someone from the group is already in that bg queue + if (member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeId)) + return ERR_BATTLEGROUND_JOIN_FAILED; // not blizz-like + // don't let join if someone from the group is in bg queue random + if (member->InBattleGroundQueueForBattleGroundQueueType(bgQueueTypeIdRandom)) + return ERR_IN_RANDOM_BG; + // don't let join to bg queue random if someone from the group is already in bg queue + if (bgOrTemplate->GetTypeID() == BATTLEGROUND_RB && member->InBattleGroundQueue()) + return ERR_IN_NON_RANDOM_BG; + // check for deserter debuff in case not arena queue + if (bgOrTemplate->GetTypeID() != BATTLEGROUND_AA && !member->CanJoinToBattleground()) + return ERR_GROUP_JOIN_BATTLEGROUND_DESERTERS; + // check if member can join any more battleground queues + if (!member->HasFreeBattleGroundQueueId()) + return ERR_BATTLEGROUND_TOO_MANY_QUEUES; // not blizz-like + } + return GroupJoinBattlegroundResult(bgOrTemplate->GetTypeID()); +} + +//=================================================== +//============== Roll =============================== +//=================================================== + +void Roll::targetObjectBuildLink() +{ + // called from link() + getTarget()->addLootValidatorRef(this); +} + +void Group::SetDungeonDifficulty(Difficulty difficulty) +{ + m_dungeonDifficulty = difficulty; + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE groups SET difficulty = %u WHERE guid ='%u'", m_dungeonDifficulty, GUID_LOPART(m_guid)); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *player = itr->getSource(); + if (!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC) + continue; + player->SetDungeonDifficulty(difficulty); + player->SendDungeonDifficulty(true); + } +} + +void Group::SetRaidDifficulty(Difficulty difficulty) +{ + m_raidDifficulty = difficulty; + if (!isBGGroup()) + CharacterDatabase.PExecute("UPDATE groups SET raiddifficulty = %u WHERE guid ='%u'", m_raidDifficulty, GUID_LOPART(m_guid)); + + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *player = itr->getSource(); + if (!player->GetSession() || player->getLevel() < LEVELREQUIREMENT_HEROIC) + continue; + player->SetRaidDifficulty(difficulty); + player->SendRaidDifficulty(true); + } +} + +bool Group::InCombatToInstance(uint32 instanceId) +{ + for (GroupReference *itr = GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player *pPlayer = itr->getSource(); + if (pPlayer && pPlayer->getAttackers().size() && pPlayer->GetInstanceId() == instanceId && (pPlayer->GetMap()->IsRaidOrHeroicDungeon())) + for (std::set<Unit*>::const_iterator i = pPlayer->getAttackers().begin(); i != pPlayer->getAttackers().end(); ++i) + if ((*i) && (*i)->GetTypeId() == TYPEID_UNIT && (*i)->ToCreature()->GetCreatureInfo()->flags_extra & CREATURE_FLAG_EXTRA_INSTANCE_BIND) + return true; + } + return false; +} + +void Group::ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo) +{ + if (isBGGroup()) + return; + + // method can be INSTANCE_RESET_ALL, INSTANCE_RESET_CHANGE_DIFFICULTY, INSTANCE_RESET_GROUP_DISBAND + + // we assume that when the difficulty changes, all instances that can be reset will be + Difficulty diff = GetDifficulty(isRaid); + + for (BoundInstancesMap::iterator itr = m_boundInstances[diff].begin(); itr != m_boundInstances[diff].end();) + { + InstanceSave *p = itr->second.save; + const MapEntry *entry = sMapStore.LookupEntry(itr->first); + if (!entry || entry->IsRaid() != isRaid || !p->CanReset() && method != INSTANCE_RESET_GROUP_DISBAND) + { + ++itr; + continue; + } + + if (method == INSTANCE_RESET_ALL) + { + // the "reset all instances" method can only reset normal maps + if (entry->map_type == MAP_RAID || diff == DUNGEON_DIFFICULTY_HEROIC) + { + ++itr; + continue; + } + } + + bool isEmpty = true; + // if the map is loaded, reset it + Map *map = MapManager::Instance().FindMap(p->GetMapId(), p->GetInstanceId()); + if (map && map->IsDungeon() && !(method == INSTANCE_RESET_GROUP_DISBAND && !p->CanReset())) + { + if (p->CanReset()) + isEmpty = ((InstanceMap*)map)->Reset(method); + else + isEmpty = !map->HavePlayers(); + } + + if (SendMsgTo) + { + if (isEmpty) SendMsgTo->SendResetInstanceSuccess(p->GetMapId()); + else SendMsgTo->SendResetInstanceFailed(0, p->GetMapId()); + } + + if (isEmpty || method == INSTANCE_RESET_GROUP_DISBAND || method == INSTANCE_RESET_CHANGE_DIFFICULTY) + { + // do not reset the instance, just unbind if others are permanently bound to it + if (p->CanReset()) p->DeleteFromDB(); + else CharacterDatabase.PExecute("DELETE FROM group_instance WHERE instance = '%u'", p->GetInstanceId()); + // i don't know for sure if hash_map iterators + m_boundInstances[diff].erase(itr); + itr = m_boundInstances[diff].begin(); + // this unloads the instance save unless online players are bound to it + // (eg. permanent binds or GM solo binds) + p->RemoveGroup(this); + } + else + ++itr; + } +} + +InstanceGroupBind* Group::GetBoundInstance(Player* player) +{ + uint32 mapid = player->GetMapId(); + MapEntry const* mapEntry = sMapStore.LookupEntry(mapid); + if (!mapEntry) + return NULL; + + Difficulty difficulty = player->GetDifficulty(mapEntry->IsRaid()); + + // some instances only have one difficulty + MapDifficulty const* mapDiff = GetMapDifficultyData(mapid,difficulty); + if (!mapDiff) + difficulty = DUNGEON_DIFFICULTY_NORMAL; + + BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid); + if (itr != m_boundInstances[difficulty].end()) + return &itr->second; + else + return NULL; +} + +InstanceGroupBind* Group::GetBoundInstance(Map* aMap) +{ + // Currently spawn numbering not different from map difficulty + Difficulty difficulty = GetDifficulty(aMap->IsRaid()); + + // some instances only have one difficulty + MapDifficulty const* mapDiff = GetMapDifficultyData(aMap->GetId(),difficulty); + if (!mapDiff) + return NULL; + + BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(aMap->GetId()); + if (itr != m_boundInstances[difficulty].end()) + return &itr->second; + else + return NULL; +} + + +InstanceGroupBind* Group::BindToInstance(InstanceSave *save, bool permanent, bool load) +{ + if (!save || isBGGroup()) + return NULL; + + InstanceGroupBind& bind = m_boundInstances[save->GetDifficulty()][save->GetMapId()]; + if (!load && (!bind.save || permanent != bind.perm || save != bind.save)) + CharacterDatabase.PExecute("REPLACE INTO group_instance (guid, instance, permanent) VALUES (%u, %u, %u)", GUID_LOPART(GetGUID()), save->GetInstanceId(), permanent); + + if (bind.save != save) + { + if (bind.save) + bind.save->RemoveGroup(this); + save->AddGroup(this); + } + + bind.save = save; + bind.perm = permanent; + if (!load) + sLog.outDebug("Group::BindToInstance: %d is now bound to map %d, instance %d, difficulty %d", GUID_LOPART(GetGUID()), save->GetMapId(), save->GetInstanceId(), save->GetDifficulty()); + return &bind; +} + +void Group::UnbindInstance(uint32 mapid, uint8 difficulty, bool unload) +{ + BoundInstancesMap::iterator itr = m_boundInstances[difficulty].find(mapid); + if (itr != m_boundInstances[difficulty].end()) + { + if (!unload) + CharacterDatabase.PExecute("DELETE FROM group_instance WHERE guid=%u AND instance=%u", GUID_LOPART(GetGUID()), itr->second.save->GetInstanceId()); + itr->second.save->RemoveGroup(this); // save can become invalid + m_boundInstances[difficulty].erase(itr); + } +} + +void Group::_homebindIfInstance(Player *player) +{ + if (player && !player->isGameMaster() && sMapStore.LookupEntry(player->GetMapId())->IsDungeon()) + player->m_InstanceValid = false; +} + +void Group::BroadcastGroupUpdate(void) +{ + // FG: HACK: force flags update on group leave - for values update hack + // -- not very efficient but safe + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + { + + Player *pp = objmgr.GetPlayer(citr->guid); + if (pp && pp->IsInWorld()) + { + pp->ForceValuesUpdateAtIndex(UNIT_FIELD_BYTES_2); + pp->ForceValuesUpdateAtIndex(UNIT_FIELD_FACTIONTEMPLATE); + DEBUG_LOG("-- Forced group value update for '%s'", pp->GetName()); + } + } +} + +void Group::ResetMaxEnchantingLevel() +{ + m_maxEnchantingLevel = 0; + Player *pMember = NULL; + for (member_citerator citr = m_memberSlots.begin(); citr != m_memberSlots.end(); ++citr) + { + if (pMember = objmgr.GetPlayer(citr->guid)) + if (m_maxEnchantingLevel < pMember->GetSkillValue(SKILL_ENCHANTING)) + m_maxEnchantingLevel = pMember->GetSkillValue(SKILL_ENCHANTING); + } +} diff --git a/src/server/game/Groups/Group.h b/src/server/game/Groups/Group.h new file mode 100644 index 00000000000..808de417b79 --- /dev/null +++ b/src/server/game/Groups/Group.h @@ -0,0 +1,461 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef TRINITYCORE_GROUP_H +#define TRINITYCORE_GROUP_H + +#include "GroupReference.h" +#include "GroupRefManager.h" +#include "BattleGround.h" +#include "LootMgr.h" +#include "DBCEnums.h" +#include "Unit.h" + +#include <map> +#include <vector> + +#define MAXGROUPSIZE 5 +#define MAXRAIDSIZE 40 +#define TARGETICONCOUNT 8 + +enum RollVote +{ + PASS = 0, + NEED = 1, + GREED = 2, + DISENCHANT = 3, + NOT_EMITED_YET = 4, + NOT_VALID = 5 +}; + +enum GroupMemberOnlineStatus +{ + MEMBER_STATUS_OFFLINE = 0x0000, + MEMBER_STATUS_ONLINE = 0x0001, + MEMBER_STATUS_PVP = 0x0002, + MEMBER_STATUS_UNK0 = 0x0004, // dead? (health=0) + MEMBER_STATUS_UNK1 = 0x0008, // ghost? (health=1) + MEMBER_STATUS_UNK2 = 0x0010, // never seen + MEMBER_STATUS_UNK3 = 0x0020, // never seen + MEMBER_STATUS_UNK4 = 0x0040, // appears with dead and ghost flags + MEMBER_STATUS_UNK5 = 0x0080, // never seen +}; + +enum GroupMemberFlags +{ + MEMBER_FLAG_ASSISTANT = 0x01, + MEMBER_FLAG_MAINTANK = 0x02, + MEMBER_FLAG_MAINASSIST = 0x04, +}; + +enum GroupType +{ + GROUPTYPE_NORMAL = 0x00, + GROUPTYPE_BG = 0x01, + GROUPTYPE_RAID = 0x02, + GROUPTYPE_BGRAID = GROUPTYPE_BG | GROUPTYPE_RAID, // mask + // 0x04? + GROUPTYPE_LFD = 0x08, + // 0x10, leave/change group?, I saw this flag when leaving group and after leaving BG while in group +}; + +class BattleGround; + +enum GroupUpdateFlags +{ + GROUP_UPDATE_FLAG_NONE = 0x00000000, // nothing + GROUP_UPDATE_FLAG_STATUS = 0x00000001, // uint16, flags + GROUP_UPDATE_FLAG_CUR_HP = 0x00000002, // uint32 + GROUP_UPDATE_FLAG_MAX_HP = 0x00000004, // uint32 + GROUP_UPDATE_FLAG_POWER_TYPE = 0x00000008, // uint8 + GROUP_UPDATE_FLAG_CUR_POWER = 0x00000010, // uint16 + GROUP_UPDATE_FLAG_MAX_POWER = 0x00000020, // uint16 + GROUP_UPDATE_FLAG_LEVEL = 0x00000040, // uint16 + GROUP_UPDATE_FLAG_ZONE = 0x00000080, // uint16 + GROUP_UPDATE_FLAG_POSITION = 0x00000100, // uint16, uint16 + GROUP_UPDATE_FLAG_AURAS = 0x00000200, // uint64 mask, for each bit set uint32 spellid + uint8 unk + GROUP_UPDATE_FLAG_PET_GUID = 0x00000400, // uint64 pet guid + GROUP_UPDATE_FLAG_PET_NAME = 0x00000800, // pet name, NULL terminated string + GROUP_UPDATE_FLAG_PET_MODEL_ID = 0x00001000, // uint16, model id + GROUP_UPDATE_FLAG_PET_CUR_HP = 0x00002000, // uint32 pet cur health + GROUP_UPDATE_FLAG_PET_MAX_HP = 0x00004000, // uint32 pet max health + GROUP_UPDATE_FLAG_PET_POWER_TYPE = 0x00008000, // uint8 pet power type + GROUP_UPDATE_FLAG_PET_CUR_POWER = 0x00010000, // uint16 pet cur power + GROUP_UPDATE_FLAG_PET_MAX_POWER = 0x00020000, // uint16 pet max power + GROUP_UPDATE_FLAG_PET_AURAS = 0x00040000, // uint64 mask, for each bit set uint32 spellid + uint8 unk, pet auras... + GROUP_UPDATE_FLAG_VEHICLE_SEAT = 0x00080000, // uint32 vehicle_seat_id (index from VehicleSeat.dbc) + GROUP_UPDATE_PET = 0x0007FC00, // all pet flags + GROUP_UPDATE_FULL = 0x0007FFFF, // all known flags +}; + +#define GROUP_UPDATE_FLAGS_COUNT 20 + // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19 +static const uint8 GroupUpdateLength[GROUP_UPDATE_FLAGS_COUNT] = { 0, 2, 2, 2, 1, 2, 2, 2, 2, 4, 8, 8, 1, 2, 2, 2, 1, 2, 2, 8}; + +class InstanceSave; + +class Roll : public LootValidatorRef +{ + public: + Roll(uint64 _guid, LootItem const& li) + : itemGUID(_guid), itemid(li.itemid), itemRandomPropId(li.randomPropertyId), itemRandomSuffix(li.randomSuffix), itemCount(li.count), + totalPlayersRolling(0), totalNeed(0), totalGreed(0), totalPass(0), itemSlot(0), rollVoteMask(ROLL_ALL_TYPE_NO_DISENCHANT) {} + ~Roll() { } + void setLoot(Loot *pLoot) { link(pLoot, this); } + Loot *getLoot() { return getTarget(); } + void targetObjectBuildLink(); + + uint64 itemGUID; + uint32 itemid; + int32 itemRandomPropId; + uint32 itemRandomSuffix; + uint8 itemCount; + typedef std::map<uint64, RollVote> PlayerVote; + PlayerVote playerVote; //vote position correspond with player position (in group) + uint8 totalPlayersRolling; + uint8 totalNeed; + uint8 totalGreed; + uint8 totalPass; + uint8 itemSlot; + uint8 rollVoteMask; +}; + +struct InstanceGroupBind +{ + InstanceSave *save; + bool perm; + /* permanent InstanceGroupBinds exist iff the leader has a permanent + PlayerInstanceBind for the same instance. */ + InstanceGroupBind() : save(NULL), perm(false) {} +}; + +/** request member stats checken **/ +/** todo: uninvite people that not accepted invite **/ +class Group +{ + public: + struct MemberSlot + { + uint64 guid; + std::string name; + uint8 group; + uint8 flags; + }; + typedef std::list<MemberSlot> MemberSlotList; + typedef MemberSlotList::const_iterator member_citerator; + + typedef UNORDERED_MAP< uint32 /*mapId*/, InstanceGroupBind> BoundInstancesMap; + protected: + typedef MemberSlotList::iterator member_witerator; + typedef std::set<Player*> InvitesList; + + typedef std::vector<Roll*> Rolls; + + public: + Group(); + ~Group(); + + // group manipulation methods + bool Create(const uint64 &guid, const char * name); + bool LoadGroupFromDB(const uint64 &guid, QueryResult_AutoPtr result = QueryResult_AutoPtr(NULL), bool loadMembers = true); + bool LoadMemberFromDB(uint32 guidLow, uint8 memberFlags, uint8 subgroup); + bool AddInvite(Player *player); + uint32 RemoveInvite(Player *player); + void RemoveAllInvites(); + bool AddLeaderInvite(Player *player); + bool AddMember(const uint64 &guid, const char* name); + // method: 0=just remove, 1=kick + uint32 RemoveMember(const uint64 &guid, const uint8 &method); + void ChangeLeader(const uint64 &guid); + void SetLootMethod(LootMethod method) { m_lootMethod = method; } + void SetLooterGuid(const uint64 &guid) { m_looterGuid = guid; } + void UpdateLooterGuid(WorldObject* pLootedObject, bool ifneed = false); + void SetLootThreshold(ItemQualities threshold) { m_lootThreshold = threshold; } + void Disband(bool hideDestroy=false); + + // properties accessories + bool IsFull() const { return (m_groupType == GROUPTYPE_NORMAL) ? (m_memberSlots.size() >= MAXGROUPSIZE) : (m_memberSlots.size() >= MAXRAIDSIZE); } + bool isRaidGroup() const { return m_groupType & GROUPTYPE_RAID; } + bool isBGGroup() const { return m_bgGroup != NULL; } + bool IsCreated() const { return GetMembersCount() > 0; } + const uint64& GetLeaderGUID() const { return m_leaderGuid; } + const uint64& GetGUID() const { return m_guid; } + const char * GetLeaderName() const { return m_leaderName.c_str(); } + LootMethod GetLootMethod() const { return m_lootMethod; } + const uint64& GetLooterGuid() const { return m_looterGuid; } + ItemQualities GetLootThreshold() const { return m_lootThreshold; } + + // member manipulation methods + bool IsMember(const uint64& guid) const { return _getMemberCSlot(guid) != m_memberSlots.end(); } + bool IsLeader(const uint64& guid) const { return (GetLeaderGUID() == guid); } + uint64 GetMemberGUID(const std::string& name) + { + for (member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr) + { + if (itr->name == name) + { + return itr->guid; + } + } + return 0; + } + bool IsAssistant(uint64 guid) const + { + member_citerator mslot = _getMemberCSlot(guid); + if (mslot == m_memberSlots.end()) + return false; + + return mslot->flags & MEMBER_FLAG_ASSISTANT; + } + Player* GetInvited(const uint64& guid) const; + Player* GetInvited(const std::string& name) const; + + bool SameSubGroup(uint64 guid1,const uint64& guid2) const + { + member_citerator mslot2 = _getMemberCSlot(guid2); + if (mslot2 == m_memberSlots.end()) + return false; + + return SameSubGroup(guid1,&*mslot2); + } + + bool SameSubGroup(uint64 guid1, MemberSlot const* slot2) const + { + member_citerator mslot1 = _getMemberCSlot(guid1); + if (mslot1 == m_memberSlots.end() || !slot2) + return false; + + return (mslot1->group == slot2->group); + } + + bool HasFreeSlotSubGroup(uint8 subgroup) const + { + return (m_subGroupsCounts && m_subGroupsCounts[subgroup] < MAXGROUPSIZE); + } + + bool SameSubGroup(Player const* member1, Player const* member2) const; + + MemberSlotList const& GetMemberSlots() const { return m_memberSlots; } + GroupReference* GetFirstMember() { return m_memberMgr.getFirst(); } + uint32 GetMembersCount() const { return m_memberSlots.size(); } + void GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level); + uint8 GetMemberGroup(uint64 guid) const + { + member_citerator mslot = _getMemberCSlot(guid); + if (mslot == m_memberSlots.end()) + return (MAXRAIDSIZE/MAXGROUPSIZE+1); + + return mslot->group; + } + + // some additional raid methods + void ConvertToRaid(); + + void SetBattlegroundGroup(BattleGround *bg) { m_bgGroup = bg; } + GroupJoinBattlegroundResult CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot); + + void ChangeMembersGroup(const uint64 &guid, const uint8 &group); + void ChangeMembersGroup(Player *player, const uint8 &group); + + void SetAssistant(uint64 guid, const bool &apply) + { + if (!isRaidGroup()) + return; + if (_setAssistantFlag(guid, apply)) + SendUpdate(); + } + void SetMainTank(uint64 guid, const bool &apply) + { + if (!isRaidGroup()) + return; + + if (_setMainTank(guid, apply)) + SendUpdate(); + } + void SetMainAssistant(uint64 guid, const bool &apply) + { + if (!isRaidGroup()) + return; + + if (_setMainAssistant(guid, apply)) + SendUpdate(); + } + + void SetTargetIcon(uint8 id, uint64 whoGuid, uint64 targetGuid); + + Difficulty GetDifficulty(bool isRaid) const { return isRaid ? m_raidDifficulty : m_dungeonDifficulty; } + Difficulty GetDungeonDifficulty() const { return m_dungeonDifficulty; } + Difficulty GetRaidDifficulty() const { return m_raidDifficulty; } + void SetDungeonDifficulty(Difficulty difficulty); + void SetRaidDifficulty(Difficulty difficulty); + uint16 InInstance(); + bool InCombatToInstance(uint32 instanceId); + void ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo); + + // -no description- + //void SendInit(WorldSession *session); + void SendTargetIconList(WorldSession *session); + void SendUpdate(); + void UpdatePlayerOutOfRange(Player* pPlayer); + // ignore: GUID of player that will be ignored + void BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group=-1, uint64 ignore=0); + void BroadcastReadyCheck(WorldPacket *packet); + void OfflineReadyCheck(); + + /*********************************************************/ + /*** LOOT SYSTEM ***/ + /*********************************************************/ + + void SendLootStartRoll(uint32 CountDown, uint32 mapid, const Roll &r); + void SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r); + void SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r); + void SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r); + void SendLooter(Creature *pCreature, Player *pLooter); + void GroupLoot(Loot *loot, WorldObject* pLootedObject); + void NeedBeforeGreed(Loot *loot, WorldObject* pLootedObject); + void MasterLoot(Loot *loot, WorldObject* pLootedObject); + Rolls::iterator GetRoll(uint64 Guid) + { + Rolls::iterator iter; + for (iter=RollId.begin(); iter != RollId.end(); ++iter) + { + if ((*iter)->itemGUID == Guid && (*iter)->isValid()) + { + return iter; + } + } + return RollId.end(); + } + void CountTheRoll(Rolls::iterator roll, uint32 NumberOfPlayers); + void CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, uint8 Choise); + void EndRoll(Loot *loot); + + // related to disenchant rolls + void ResetMaxEnchantingLevel(); + + void LinkMember(GroupReference *pRef) { m_memberMgr.insertFirst(pRef); } + void DelinkMember(GroupReference* /*pRef*/) { } + + InstanceGroupBind* BindToInstance(InstanceSave *save, bool permanent, bool load = false); + void UnbindInstance(uint32 mapid, uint8 difficulty, bool unload = false); + InstanceGroupBind* GetBoundInstance(Player* player); + InstanceGroupBind* GetBoundInstance(Map* aMap); + BoundInstancesMap& GetBoundInstances(Difficulty difficulty) { return m_boundInstances[difficulty]; } + + // FG: evil hacks + void BroadcastGroupUpdate(void); + + protected: + bool _addMember(const uint64 &guid, const char* name); + bool _addMember(const uint64 &guid, const char* name, uint8 group); + bool _removeMember(const uint64 &guid); // returns true if leader has changed + void _setLeader(const uint64 &guid); + + void _removeRolls(const uint64 &guid); + + bool _setMembersGroup(const uint64 &guid, const uint8 &group); + bool _setAssistantFlag(const uint64 &guid, const bool &apply); + bool _setMainTank(const uint64 &guid, const bool &apply); + bool _setMainAssistant(const uint64 &guid, const bool &apply); + + void _homebindIfInstance(Player *player); + + void _initRaidSubGroupsCounter() + { + // Sub group counters initialization + if (!m_subGroupsCounts) + m_subGroupsCounts = new uint8[MAXRAIDSIZE / MAXGROUPSIZE]; + + memset((void*)m_subGroupsCounts, 0, (MAXRAIDSIZE / MAXGROUPSIZE)*sizeof(uint8)); + + for (member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr) + ++m_subGroupsCounts[itr->group]; + } + + member_citerator _getMemberCSlot(uint64 Guid) const + { + for (member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr) + { + if (itr->guid == Guid) + return itr; + } + return m_memberSlots.end(); + } + + member_witerator _getMemberWSlot(uint64 Guid) + { + for (member_witerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr) + { + if (itr->guid == Guid) + return itr; + } + return m_memberSlots.end(); + } + + void SubGroupCounterIncrease(uint8 subgroup) + { + if (m_subGroupsCounts) + ++m_subGroupsCounts[subgroup]; + } + + void SubGroupCounterDecrease(uint8 subgroup) + { + if (m_subGroupsCounts) + --m_subGroupsCounts[subgroup]; + } + + void RemoveUniqueGroupMemberFlag(GroupMemberFlags flag) + { + for (member_witerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr) + { + if (itr->flags & flag) + itr->flags &= ~flag; + } + } + + void ToggleGroupMemberFlag(member_witerator slot, uint8 flag, bool apply) + { + if (apply) + slot->flags |= flag; + else + slot->flags &= ~flag; + } + + MemberSlotList m_memberSlots; + GroupRefManager m_memberMgr; + InvitesList m_invitees; + uint64 m_leaderGuid; + std::string m_leaderName; + GroupType m_groupType; + Difficulty m_dungeonDifficulty; + Difficulty m_raidDifficulty; + BattleGround* m_bgGroup; + uint64 m_targetIcons[TARGETICONCOUNT]; + LootMethod m_lootMethod; + ItemQualities m_lootThreshold; + uint64 m_looterGuid; + Rolls RollId; + BoundInstancesMap m_boundInstances[MAX_DIFFICULTY]; + uint8* m_subGroupsCounts; + uint64 m_guid; + uint32 m_counter; // used only in SMSG_GROUP_LIST + uint32 m_maxEnchantingLevel; +}; +#endif diff --git a/src/server/game/Groups/GroupHandler.cpp b/src/server/game/Groups/GroupHandler.cpp new file mode 100644 index 00000000000..f973bc24722 --- /dev/null +++ b/src/server/game/Groups/GroupHandler.cpp @@ -0,0 +1,940 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "Common.h" +#include "Database/DatabaseEnv.h" +#include "Opcodes.h" +#include "Log.h" +#include "WorldPacket.h" +#include "WorldSession.h" +#include "World.h" +#include "ObjectMgr.h" +#include "Player.h" +#include "Group.h" +#include "SocialMgr.h" +#include "Util.h" +#include "SpellAuras.h" +#include "Vehicle.h" +#include "LFG.h" + +class Aura; + +/* differeces from off: + -you can uninvite yourself - is is useful + -you can accept invitation even if leader went offline +*/ +/* todo: + -group_destroyed msg is sent but not shown + -reduce xp gaining when in raid group + -quest sharing has to be corrected + -FIX sending PartyMemberStats +*/ + +void WorldSession::SendPartyResult(PartyOperation operation, const std::string& member, PartyResult res) +{ + WorldPacket data(SMSG_PARTY_COMMAND_RESULT, (4+member.size()+1+4+4)); + data << (uint32)operation; + data << member; + data << (uint32)res; + data << uint32(0); // LFD cooldown related (used with ERR_PARTY_LFG_BOOT_COOLDOWN_S and ERR_PARTY_LFG_BOOT_NOT_ELIGIBLE_S) + + SendPacket(&data); +} + +void WorldSession::HandleGroupInviteOpcode(WorldPacket & recv_data) +{ + std::string membername; + recv_data >> membername; + + // attempt add selected player + + // cheating + if (!normalizePlayerName(membername)) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_BAD_PLAYER_NAME_S); + return; + } + + Player *player = objmgr.GetPlayer(membername.c_str()); + + // no player + if (!player) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_BAD_PLAYER_NAME_S); + return; + } + + // restrict invite to GMs + if (!sWorld.getConfig(CONFIG_ALLOW_GM_GROUP) && !GetPlayer()->isGameMaster() && player->isGameMaster()) + return; + + // can't group with + if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GROUP) && GetPlayer()->GetTeam() != player->GetTeam()) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_PLAYER_WRONG_FACTION); + return; + } + if (GetPlayer()->GetInstanceId() != 0 && player->GetInstanceId() != 0 && GetPlayer()->GetInstanceId() != player->GetInstanceId() && GetPlayer()->GetMapId() == player->GetMapId()) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_TARGET_NOT_IN_INSTANCE_S); + return; + } + // just ignore us + if (player->GetInstanceId() != 0 && player->GetDungeonDifficulty() != GetPlayer()->GetDungeonDifficulty()) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_IGNORING_YOU_S); + return; + } + + if (player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow())) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_IGNORING_YOU_S); + return; + } + + Group *group = GetPlayer()->GetGroup(); + if (group && group->isBGGroup()) + group = GetPlayer()->GetOriginalGroup(); + + Group *group2 = player->GetGroup(); + if (group2 && group2->isBGGroup()) + group2 = player->GetOriginalGroup(); + // player already in another group or invited + if (group2 || player->GetGroupInvite()) + { + SendPartyResult(PARTY_OP_INVITE, membername, ERR_ALREADY_IN_GROUP_S); + return; + } + + if (group) + { + // not have permissions for invite + if (group->isRaidGroup() && !group->IsLeader(GetPlayer()->GetGUID()) && !group->IsAssistant(GetPlayer()->GetGUID())) + { + SendPartyResult(PARTY_OP_INVITE, "", ERR_NOT_LEADER); + return; + } + // not have place + if (group->IsFull()) + { + SendPartyResult(PARTY_OP_INVITE, "", ERR_GROUP_FULL); + return; + } + } + + // ok, but group not exist, start a new group + // but don't create and save the group to the DB until + // at least one person joins + if (!group) + { + group = new Group; + // new group: if can't add then delete + if (!group->AddLeaderInvite(GetPlayer())) + { + delete group; + return; + } + if (!group->AddInvite(player)) + { + delete group; + return; + } + } + else + { + // already existed group: if can't add then just leave + if (!group->AddInvite(player)) + { + return; + } + } + + // ok, we do it + WorldPacket data(SMSG_GROUP_INVITE, 10); // guess size + data << uint8(1); // invited/already in group flag + data << GetPlayer()->GetName(); // max len 48 + data << uint32(0); // unk + data << uint8(0); // count + //for (int i = 0; i < count; ++i) + // data << uint32(0); + data << uint32(0); // unk + player->GetSession()->SendPacket(&data); + + SendLfgUpdatePlayer(LFG_UPDATETYPE_REMOVED_FROM_QUEUE); + SendLfgUpdateParty(LFG_UPDATETYPE_REMOVED_FROM_QUEUE); + SendPartyResult(PARTY_OP_INVITE, membername, ERR_PARTY_RESULT_OK); +} + +void WorldSession::HandleGroupAcceptOpcode(WorldPacket & /*recv_data*/) +{ + Group *group = GetPlayer()->GetGroupInvite(); + if (!group) return; + + if (group->GetLeaderGUID() == GetPlayer()->GetGUID()) + { + sLog.outError("HandleGroupAcceptOpcode: player %s(%d) tried to accept an invite to his own group", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow()); + return; + } + + // remove in from ivites in any case + group->RemoveInvite(GetPlayer()); + + /** error handling **/ + /********************/ + + // not have place + if (group->IsFull()) + { + SendPartyResult(PARTY_OP_INVITE, "", ERR_GROUP_FULL); + return; + } + + Player* leader = objmgr.GetPlayer(group->GetLeaderGUID()); + + // forming a new group, create it + if (!group->IsCreated()) + { + if (leader) + group->RemoveInvite(leader); + group->Create(group->GetLeaderGUID(), group->GetLeaderName()); + objmgr.AddGroup(group); + } + + // everything's fine, do it, PLAYER'S GROUP IS SET IN ADDMEMBER!!! + if (!group->AddMember(GetPlayer()->GetGUID(), GetPlayer()->GetName())) + return; + + SendLfgUpdatePlayer(LFG_UPDATETYPE_REMOVED_FROM_QUEUE); + for (GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next()) + if (Player *plrg = itr->getSource()) + { + plrg->GetSession()->SendLfgUpdatePlayer(LFG_UPDATETYPE_CLEAR_LOCK_LIST); + plrg->GetSession()->SendLfgUpdateParty(LFG_UPDATETYPE_CLEAR_LOCK_LIST); + } + + group->BroadcastGroupUpdate(); +} + +void WorldSession::HandleGroupDeclineOpcode(WorldPacket & /*recv_data*/) +{ + Group *group = GetPlayer()->GetGroupInvite(); + if (!group) return; + + // remember leader if online + Player *leader = objmgr.GetPlayer(group->GetLeaderGUID()); + + // uninvite, group can be deleted + GetPlayer()->UninviteFromGroup(); + + if (!leader || !leader->GetSession()) + return; + + // report + WorldPacket data(SMSG_GROUP_DECLINE, 10); // guess size + data << GetPlayer()->GetName(); + leader->GetSession()->SendPacket(&data); +} + +void WorldSession::HandleGroupUninviteGuidOpcode(WorldPacket & recv_data) +{ + uint64 guid; + recv_data >> guid; + recv_data.read_skip<std::string>(); // reason + + //can't uninvite yourself + if (guid == GetPlayer()->GetGUID()) + { + sLog.outError("WorldSession::HandleGroupUninviteGuidOpcode: leader %s(%d) tried to uninvite himself from the group.", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow()); + return; + } + + PartyResult res = GetPlayer()->CanUninviteFromGroup(); + if (res != ERR_PARTY_RESULT_OK) + { + SendPartyResult(PARTY_OP_UNINVITE, "", res); + return; + } + + Group* grp = GetPlayer()->GetGroup(); + if (!grp) + return; + + if (grp->IsMember(guid)) + { + Player::RemoveFromGroup(grp,guid); + return; + } + + if (Player* plr = grp->GetInvited(guid)) + { + plr->UninviteFromGroup(); + return; + } + + SendPartyResult(PARTY_OP_UNINVITE, "", ERR_TARGET_NOT_IN_GROUP_S); +} + +void WorldSession::HandleGroupUninviteOpcode(WorldPacket & recv_data) +{ + std::string membername; + recv_data >> membername; + + // player not found + if (!normalizePlayerName(membername)) + return; + + // can't uninvite yourself + if (GetPlayer()->GetName() == membername) + { + sLog.outError("WorldSession::HandleGroupUninviteOpcode: leader %s(%d) tried to uninvite himself from the group.", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow()); + return; + } + + PartyResult res = GetPlayer()->CanUninviteFromGroup(); + if (res != ERR_PARTY_RESULT_OK) + { + SendPartyResult(PARTY_OP_UNINVITE, "", res); + return; + } + + Group* grp = GetPlayer()->GetGroup(); + if (!grp) + return; + + if (uint64 guid = grp->GetMemberGUID(membername)) + { + Player::RemoveFromGroup(grp,guid); + return; + } + + if (Player* plr = grp->GetInvited(membername)) + { + plr->UninviteFromGroup(); + return; + } + + SendPartyResult(PARTY_OP_UNINVITE, membername, ERR_TARGET_NOT_IN_GROUP_S); +} + +void WorldSession::HandleGroupSetLeaderOpcode(WorldPacket & recv_data) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + uint64 guid; + recv_data >> guid; + + Player *player = objmgr.GetPlayer(guid); + + /** error handling **/ + if (!player || !group->IsLeader(GetPlayer()->GetGUID()) || player->GetGroup() != group) + return; + /********************/ + + // everything's fine, do it + group->ChangeLeader(guid); +} + +void WorldSession::HandleGroupDisbandOpcode(WorldPacket & /*recv_data*/) +{ + if (!GetPlayer()->GetGroup()) + return; + + if (_player->InBattleGround()) + { + SendPartyResult(PARTY_OP_INVITE, "", ERR_INVITE_RESTRICTED); + return; + } + + /** error handling **/ + /********************/ + + // everything's fine, do it + SendPartyResult(PARTY_OP_LEAVE, GetPlayer()->GetName(), ERR_PARTY_RESULT_OK); + + GetPlayer()->RemoveFromGroup(); +} + +void WorldSession::HandleLootMethodOpcode(WorldPacket & recv_data) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + uint32 lootMethod; + uint64 lootMaster; + uint32 lootThreshold; + recv_data >> lootMethod >> lootMaster >> lootThreshold; + + /** error handling **/ + if (!group->IsLeader(GetPlayer()->GetGUID())) + return; + /********************/ + + // everything's fine, do it + group->SetLootMethod((LootMethod)lootMethod); + group->SetLooterGuid(lootMaster); + group->SetLootThreshold((ItemQualities)lootThreshold); + group->SendUpdate(); +} + +void WorldSession::HandleLootRoll(WorldPacket &recv_data) +{ + if (!GetPlayer()->GetGroup()) + return; + + uint64 Guid; + uint32 NumberOfPlayers; + uint8 rollType; + recv_data >> Guid; //guid of the item rolled + recv_data >> NumberOfPlayers; + recv_data >> rollType; //0: pass, 1: need, 2: greed + + //sLog.outDebug("WORLD RECIEVE CMSG_LOOT_ROLL, From:%u, Numberofplayers:%u, Choise:%u", (uint32)Guid, NumberOfPlayers, Choise); + + Group* group = GetPlayer()->GetGroup(); + if (!group) + return; + + // everything's fine, do it + group->CountRollVote(GetPlayer()->GetGUID(), Guid, NumberOfPlayers, rollType); + + switch (rollType) + { + case ROLL_NEED: + GetPlayer()->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_NEED, 1); + break; + case ROLL_GREED: + GetPlayer()->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_ROLL_GREED, 1); + break; + } +} + +void WorldSession::HandleMinimapPingOpcode(WorldPacket& recv_data) +{ + if (!GetPlayer()->GetGroup()) + return; + + float x, y; + recv_data >> x; + recv_data >> y; + + //sLog.outDebug("Received opcode MSG_MINIMAP_PING X: %f, Y: %f", x, y); + + /** error handling **/ + /********************/ + + // everything's fine, do it + WorldPacket data(MSG_MINIMAP_PING, (8+4+4)); + data << uint64(GetPlayer()->GetGUID()); + data << float(x); + data << float(y); + GetPlayer()->GetGroup()->BroadcastPacket(&data, true, -1, GetPlayer()->GetGUID()); +} + +void WorldSession::HandleRandomRollOpcode(WorldPacket& recv_data) +{ + uint32 minimum, maximum, roll; + recv_data >> minimum; + recv_data >> maximum; + + /** error handling **/ + if (minimum > maximum || maximum > 10000) // < 32768 for urand call + return; + /********************/ + + // everything's fine, do it + roll = urand(minimum, maximum); + + //sLog.outDebug("ROLL: MIN: %u, MAX: %u, ROLL: %u", minimum, maximum, roll); + + WorldPacket data(MSG_RANDOM_ROLL, 4+4+4+8); + data << uint32(minimum); + data << uint32(maximum); + data << uint32(roll); + data << uint64(GetPlayer()->GetGUID()); + if (GetPlayer()->GetGroup()) + GetPlayer()->GetGroup()->BroadcastPacket(&data, false); + else + SendPacket(&data); +} + +void WorldSession::HandleRaidTargetUpdateOpcode(WorldPacket & recv_data) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + uint8 x; + recv_data >> x; + + /** error handling **/ + /********************/ + + // everything's fine, do it + if (x == 0xFF) // target icon request + { + group->SendTargetIconList(this); + } + else // target icon update + { + if (!group->IsLeader(GetPlayer()->GetGUID()) && !group->IsAssistant(GetPlayer()->GetGUID())) + return; + + uint64 guid; + recv_data >> guid; + group->SetTargetIcon(x, _player->GetGUID(), guid); + } +} + +void WorldSession::HandleGroupRaidConvertOpcode(WorldPacket & /*recv_data*/) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + if (_player->InBattleGround()) + return; + + /** error handling **/ + if (!group->IsLeader(GetPlayer()->GetGUID()) || group->GetMembersCount() < 2) + return; + /********************/ + + // everything's fine, do it (is it 0 (PARTY_OP_INVITE) correct code) + SendPartyResult(PARTY_OP_INVITE, "", ERR_PARTY_RESULT_OK); + group->ConvertToRaid(); +} + +void WorldSession::HandleGroupChangeSubGroupOpcode(WorldPacket & recv_data) +{ + // we will get correct pointer for group here, so we don't have to check if group is BG raid + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + std::string name; + uint8 groupNr; + recv_data >> name; + + recv_data >> groupNr; + + /** error handling **/ + uint64 senderGuid = GetPlayer()->GetGUID(); + if (!group->IsLeader(senderGuid) && !group->IsAssistant(senderGuid)) + return; + + if (!group->HasFreeSlotSubGroup(groupNr)) + return; + /********************/ + + Player *movedPlayer=objmgr.GetPlayer(name.c_str()); + if (!movedPlayer) + return; + + //Do not allow leader to change group of player in combat + if (movedPlayer->isInCombat()) + return; + + // everything's fine, do it + group->ChangeMembersGroup(movedPlayer, groupNr); +} + +void WorldSession::HandleGroupAssistantLeaderOpcode(WorldPacket & recv_data) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + uint64 guid; + uint8 flag; + recv_data >> guid; + recv_data >> flag; + + /** error handling **/ + if (!group->IsLeader(GetPlayer()->GetGUID())) + return; + /********************/ + + // everything's fine, do it + group->SetAssistant(guid, (flag != 0)); +} + +void WorldSession::HandlePartyAssignmentOpcode(WorldPacket & recv_data) +{ + sLog.outDebug("MSG_PARTY_ASSIGNMENT"); + + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + uint8 flag, apply; + uint64 guid; + recv_data >> flag >> apply; + recv_data >> guid; + + /** error handling **/ + uint64 senderGuid = GetPlayer()->GetGUID(); + if (!group->IsLeader(senderGuid) && group->IsAssistant(senderGuid)) + return; + /********************/ + + // everything's fine, do it + if (flag == 0) + group->SetMainTank(guid, apply); + + else if (flag == 1) + group->SetMainAssistant(guid, apply); +} + +void WorldSession::HandleRaidReadyCheckOpcode(WorldPacket & recv_data) +{ + Group *group = GetPlayer()->GetGroup(); + if (!group) + return; + + if (recv_data.empty()) // request + { + /** error handling **/ + if (!group->IsLeader(GetPlayer()->GetGUID()) && !group->IsAssistant(GetPlayer()->GetGUID())) + return; + /********************/ + + // everything's fine, do it + WorldPacket data(MSG_RAID_READY_CHECK, 8); + data << GetPlayer()->GetGUID(); + group->BroadcastPacket(&data, false, -1); + + group->OfflineReadyCheck(); + } + else // answer + { + uint8 state; + recv_data >> state; + + // everything's fine, do it + WorldPacket data(MSG_RAID_READY_CHECK_CONFIRM, 9); + data << uint64(GetPlayer()->GetGUID()); + data << uint8(state); + group->BroadcastReadyCheck(&data); + } +} + +void WorldSession::HandleRaidReadyCheckFinishedOpcode(WorldPacket & /*recv_data*/) +{ + //Group* group = GetPlayer()->GetGroup(); + //if (!group) + // return; + + //if (!group->IsLeader(GetPlayer()->GetGUID()) && !group->IsAssistant(GetPlayer()->GetGUID())) + // return; + + // Is any reaction need? +} + +void WorldSession::BuildPartyMemberStatsChangedPacket(Player *player, WorldPacket *data) +{ + uint32 mask = player->GetGroupUpdateFlag(); + + if (mask == GROUP_UPDATE_FLAG_NONE) + return; + + if (mask & GROUP_UPDATE_FLAG_POWER_TYPE) // if update power type, update current/max power also + mask |= (GROUP_UPDATE_FLAG_CUR_POWER | GROUP_UPDATE_FLAG_MAX_POWER); + + if (mask & GROUP_UPDATE_FLAG_PET_POWER_TYPE) // same for pets + mask |= (GROUP_UPDATE_FLAG_PET_CUR_POWER | GROUP_UPDATE_FLAG_PET_MAX_POWER); + + uint32 byteCount = 0; + for (int i = 1; i < GROUP_UPDATE_FLAGS_COUNT; ++i) + if (mask & (1 << i)) + byteCount += GroupUpdateLength[i]; + + data->Initialize(SMSG_PARTY_MEMBER_STATS, 8 + 4 + byteCount); + data->append(player->GetPackGUID()); + *data << (uint32) mask; + + if (mask & GROUP_UPDATE_FLAG_STATUS) + { + if (player) + { + if (player->IsPvP()) + *data << (uint16) (MEMBER_STATUS_ONLINE | MEMBER_STATUS_PVP); + else + *data << (uint16) MEMBER_STATUS_ONLINE; + } + else + *data << (uint16) MEMBER_STATUS_OFFLINE; + } + + if (mask & GROUP_UPDATE_FLAG_CUR_HP) + *data << (uint32) player->GetHealth(); + + if (mask & GROUP_UPDATE_FLAG_MAX_HP) + *data << (uint32) player->GetMaxHealth(); + + Powers powerType = player->getPowerType(); + if (mask & GROUP_UPDATE_FLAG_POWER_TYPE) + *data << (uint8) powerType; + + if (mask & GROUP_UPDATE_FLAG_CUR_POWER) + *data << (uint16) player->GetPower(powerType); + + if (mask & GROUP_UPDATE_FLAG_MAX_POWER) + *data << (uint16) player->GetMaxPower(powerType); + + if (mask & GROUP_UPDATE_FLAG_LEVEL) + *data << (uint16) player->getLevel(); + + if (mask & GROUP_UPDATE_FLAG_ZONE) + *data << (uint16) player->GetZoneId(); + + if (mask & GROUP_UPDATE_FLAG_POSITION) + *data << (uint16) player->GetPositionX() << (uint16) player->GetPositionY(); + + if (mask & GROUP_UPDATE_FLAG_AURAS) + { + const uint64& auramask = player->GetAuraUpdateMaskForRaid(); + *data << uint64(auramask); + for (uint32 i = 0; i < MAX_AURAS; ++i) + { + if (auramask & (uint64(1) << i)) + { + AuraApplication const * aurApp = player->GetVisibleAura(i); + *data << uint32(aurApp ? aurApp->GetBase()->GetId() : 0); + *data << uint8(1); + } + } + } + + Pet *pet = player->GetPet(); + if (mask & GROUP_UPDATE_FLAG_PET_GUID) + { + if (pet) + *data << (uint64) pet->GetGUID(); + else + *data << (uint64) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_NAME) + { + if (pet) + *data << pet->GetName(); + else + *data << (uint8) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_MODEL_ID) + { + if (pet) + *data << (uint16) pet->GetDisplayId(); + else + *data << (uint16) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_CUR_HP) + { + if (pet) + *data << (uint32) pet->GetHealth(); + else + *data << (uint32) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_MAX_HP) + { + if (pet) + *data << (uint32) pet->GetMaxHealth(); + else + *data << (uint32) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_POWER_TYPE) + { + if (pet) + *data << (uint8) pet->getPowerType(); + else + *data << (uint8) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_CUR_POWER) + { + if (pet) + *data << (uint16) pet->GetPower(pet->getPowerType()); + else + *data << (uint16) 0; + } + + if (mask & GROUP_UPDATE_FLAG_PET_MAX_POWER) + { + if (pet) + *data << (uint16) pet->GetMaxPower(pet->getPowerType()); + else + *data << (uint16) 0; + } + + if (mask & GROUP_UPDATE_FLAG_VEHICLE_SEAT) + { + if (player->GetVehicle()){ + Vehicle* vv=player->GetVehicle(); + *data << (uint32) vv->GetVehicleInfo()->m_seatID[player->m_movementInfo.t_seat]; + } + } + + if (mask & GROUP_UPDATE_FLAG_PET_AURAS) + { + if (pet) + { + const uint64& auramask = pet->GetAuraUpdateMaskForRaid(); + *data << uint64(auramask); + for (uint32 i = 0; i < MAX_AURAS; ++i) + { + if (auramask & (uint64(1) << i)) + { + AuraApplication const * aurApp = player->GetVisibleAura(i); + *data << uint32(aurApp ? aurApp->GetBase()->GetId() : 0); + *data << uint8(1); + } + } + } + else + *data << (uint64) 0; + } +} + +/*this procedure handles clients CMSG_REQUEST_PARTY_MEMBER_STATS request*/ +void WorldSession::HandleRequestPartyMemberStatsOpcode(WorldPacket &recv_data) +{ + sLog.outDebug("WORLD: Received CMSG_REQUEST_PARTY_MEMBER_STATS"); + uint64 Guid; + recv_data >> Guid; + + Player *player = objmgr.GetPlayer(Guid); + if (!player) + { + WorldPacket data(SMSG_PARTY_MEMBER_STATS_FULL, 3+4+2); + data << uint8(0); // only for SMSG_PARTY_MEMBER_STATS_FULL, probably arena/bg related + data.appendPackGUID(Guid); + data << (uint32) GROUP_UPDATE_FLAG_STATUS; + data << (uint16) MEMBER_STATUS_OFFLINE; + SendPacket(&data); + return; + } + + Pet *pet = player->GetPet(); + + WorldPacket data(SMSG_PARTY_MEMBER_STATS_FULL, 4+2+2+2+1+2*6+8+1+8); + data << uint8(0); // only for SMSG_PARTY_MEMBER_STATS_FULL, probably arena/bg related + data.append(player->GetPackGUID()); + + uint32 mask1 = 0x00040BFF; // common mask, real flags used 0x000040BFF + if (pet) + mask1 = 0x7FFFFFFF; // for hunters and other classes with pets + + Powers powerType = player->getPowerType(); + data << (uint32) mask1; // group update mask + data << (uint16) MEMBER_STATUS_ONLINE; // member's online status + data << (uint32) player->GetHealth(); // GROUP_UPDATE_FLAG_CUR_HP + data << (uint32) player->GetMaxHealth(); // GROUP_UPDATE_FLAG_MAX_HP + data << (uint8) powerType; // GROUP_UPDATE_FLAG_POWER_TYPE + data << (uint16) player->GetPower(powerType); // GROUP_UPDATE_FLAG_CUR_POWER + data << (uint16) player->GetMaxPower(powerType); // GROUP_UPDATE_FLAG_MAX_POWER + data << (uint16) player->getLevel(); // GROUP_UPDATE_FLAG_LEVEL + data << (uint16) player->GetZoneId(); // GROUP_UPDATE_FLAG_ZONE + data << (uint16) player->GetPositionX(); // GROUP_UPDATE_FLAG_POSITION + data << (uint16) player->GetPositionY(); // GROUP_UPDATE_FLAG_POSITION + + uint64 auramask = 0; + size_t maskPos = data.wpos(); + data << (uint64) auramask; // placeholder + for (uint8 i = 0; i < MAX_AURAS; ++i) + { + if (AuraApplication * aurApp = player->GetVisibleAura(i)) + { + auramask |= (uint64(1) << i); + data << (uint32) aurApp->GetBase()->GetId(); + data << (uint8) 1; + } + } + data.put<uint64>(maskPos,auramask); // GROUP_UPDATE_FLAG_AURAS + + if (pet) + { + Powers petpowertype = pet->getPowerType(); + data << (uint64) pet->GetGUID(); // GROUP_UPDATE_FLAG_PET_GUID + data << pet->GetName(); // GROUP_UPDATE_FLAG_PET_NAME + data << (uint16) pet->GetDisplayId(); // GROUP_UPDATE_FLAG_PET_MODEL_ID + data << (uint32) pet->GetHealth(); // GROUP_UPDATE_FLAG_PET_CUR_HP + data << (uint32) pet->GetMaxHealth(); // GROUP_UPDATE_FLAG_PET_MAX_HP + data << (uint8) petpowertype; // GROUP_UPDATE_FLAG_PET_POWER_TYPE + data << (uint16) pet->GetPower(petpowertype); // GROUP_UPDATE_FLAG_PET_CUR_POWER + data << (uint16) pet->GetMaxPower(petpowertype); // GROUP_UPDATE_FLAG_PET_MAX_POWER + + uint64 petauramask = 0; + size_t petMaskPos = data.wpos(); + data << (uint64) petauramask; // placeholder + for (uint8 i = 0; i < MAX_AURAS; ++i) + { + if (AuraApplication * auraApp = pet->GetVisibleAura(i)) + { + petauramask |= (uint64(1) << i); + data << (uint32) auraApp->GetBase()->GetId(); + data << (uint8) 1; + } + } + data.put<uint64>(petMaskPos,petauramask); // GROUP_UPDATE_FLAG_PET_AURAS + } + else + { + data << (uint8) 0; // GROUP_UPDATE_FLAG_PET_NAME + data << (uint64) 0; // GROUP_UPDATE_FLAG_PET_AURAS + } + + SendPacket(&data); +} + +/*!*/void WorldSession::HandleRequestRaidInfoOpcode(WorldPacket & /*recv_data*/) +{ + // every time the player checks the character screen + _player->SendRaidInfo(); +} + +/*void WorldSession::HandleGroupCancelOpcode(WorldPacket & recv_data) +{ + sLog.outDebug("WORLD: got CMSG_GROUP_CANCEL."); +}*/ + +void WorldSession::HandleOptOutOfLootOpcode(WorldPacket & recv_data) +{ + sLog.outDebug("WORLD: Received CMSG_OPT_OUT_OF_LOOT"); + + uint32 passOnLoot; + recv_data >> passOnLoot; // 1 always pass, 0 do not pass + + // ignore if player not loaded + if (!GetPlayer()) // needed because STATUS_AUTHED + { + if (passOnLoot != 0) + sLog.outError("CMSG_OPT_OUT_OF_LOOT value<>0 for not-loaded character!"); + return; + } + + GetPlayer()->SetPassOnGroupLoot(passOnLoot); +} diff --git a/src/server/game/Groups/GroupRefManager.h b/src/server/game/Groups/GroupRefManager.h new file mode 100644 index 00000000000..4fdeba2dd8c --- /dev/null +++ b/src/server/game/Groups/GroupRefManager.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _GROUPREFMANAGER +#define _GROUPREFMANAGER + +#include "Utilities/LinkedReference/RefManager.h" + +class Group; +class Player; +class GroupReference; + +class GroupRefManager : public RefManager<Group, Player> +{ + public: + GroupReference* getFirst() { return ((GroupReference*) RefManager<Group, Player>::getFirst()); } +}; +#endif + diff --git a/src/server/game/Groups/GroupReference.cpp b/src/server/game/Groups/GroupReference.cpp new file mode 100644 index 00000000000..d9c972f2cec --- /dev/null +++ b/src/server/game/Groups/GroupReference.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "Player.h" +#include "Group.h" +#include "GroupReference.h" + +void GroupReference::targetObjectBuildLink() +{ + // called from link() + getTarget()->LinkMember(this); +} + +void GroupReference::targetObjectDestroyLink() +{ + // called from unlink() + getTarget()->DelinkMember(this); +} + +void GroupReference::sourceObjectDestroyLink() +{ + // called from invalidate() + getTarget()->DelinkMember(this); +} + diff --git a/src/server/game/Groups/GroupReference.h b/src/server/game/Groups/GroupReference.h new file mode 100644 index 00000000000..76f54c5230f --- /dev/null +++ b/src/server/game/Groups/GroupReference.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> + * + * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _GROUPREFERENCE_H +#define _GROUPREFERENCE_H + +#include "Utilities/LinkedReference/Reference.h" + +class Group; +class Player; + +class GroupReference : public Reference<Group, Player> +{ + protected: + uint8 iSubGroup; + void targetObjectBuildLink(); + void targetObjectDestroyLink(); + void sourceObjectDestroyLink(); + public: + GroupReference() : Reference<Group, Player>(), iSubGroup(0) {} + ~GroupReference() { unlink(); } + GroupReference *next() { return (GroupReference*)Reference<Group, Player>::next(); } + uint8 getSubGroup() const { return iSubGroup; } + void setSubGroup(uint8 pSubGroup) { iSubGroup = pSubGroup; } +}; +#endif + |
