[9584] Continue fix loot roll packets and related code.
[getmangos.git] / src / game / Group.h
blob504358b25e3b2eadf16a1716dbbd3a2cf7d7f655
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOSSERVER_GROUP_H
20 #define MANGOSSERVER_GROUP_H
22 #include "GroupReference.h"
23 #include "GroupRefManager.h"
24 #include "BattleGround.h"
25 #include "LootMgr.h"
26 #include "DBCEnums.h"
28 #include <map>
29 #include <vector>
31 #define MAXGROUPSIZE 5
32 #define MAXRAIDSIZE 40
33 #define TARGETICONCOUNT 8
35 enum RollVote
37 PASS = 0,
38 NEED = 1,
39 GREED = 2,
40 DISENCHANT = 3,
41 NOT_EMITED_YET = 4,
42 NOT_VALID = 5
45 enum GroupMemberOnlineStatus
47 MEMBER_STATUS_OFFLINE = 0x0000,
48 MEMBER_STATUS_ONLINE = 0x0001,
49 MEMBER_STATUS_PVP = 0x0002,
50 MEMBER_STATUS_UNK0 = 0x0004, // dead? (health=0)
51 MEMBER_STATUS_UNK1 = 0x0008, // ghost? (health=1)
52 MEMBER_STATUS_UNK2 = 0x0010, // never seen
53 MEMBER_STATUS_UNK3 = 0x0020, // never seen
54 MEMBER_STATUS_UNK4 = 0x0040, // appears with dead and ghost flags
55 MEMBER_STATUS_UNK5 = 0x0080, // never seen
58 enum GroupType // group type flags?
60 GROUPTYPE_NORMAL = 0x00,
61 GROUPTYPE_BG = 0x01,
62 GROUPTYPE_RAID = 0x02,
63 GROUPTYPE_BGRAID = GROUPTYPE_BG | GROUPTYPE_RAID, // mask
64 // 0x04?
65 GROUPTYPE_LFD = 0x08,
66 // 0x10, leave/change group?, I saw this flag when leaving group and after leaving BG while in group
69 class BattleGround;
71 enum GroupUpdateFlags
73 GROUP_UPDATE_FLAG_NONE = 0x00000000, // nothing
74 GROUP_UPDATE_FLAG_STATUS = 0x00000001, // uint16, flags
75 GROUP_UPDATE_FLAG_CUR_HP = 0x00000002, // uint32
76 GROUP_UPDATE_FLAG_MAX_HP = 0x00000004, // uint32
77 GROUP_UPDATE_FLAG_POWER_TYPE = 0x00000008, // uint8
78 GROUP_UPDATE_FLAG_CUR_POWER = 0x00000010, // uint16
79 GROUP_UPDATE_FLAG_MAX_POWER = 0x00000020, // uint16
80 GROUP_UPDATE_FLAG_LEVEL = 0x00000040, // uint16
81 GROUP_UPDATE_FLAG_ZONE = 0x00000080, // uint16
82 GROUP_UPDATE_FLAG_POSITION = 0x00000100, // uint16, uint16
83 GROUP_UPDATE_FLAG_AURAS = 0x00000200, // uint64 mask, for each bit set uint32 spellid + uint8 unk
84 GROUP_UPDATE_FLAG_PET_GUID = 0x00000400, // uint64 pet guid
85 GROUP_UPDATE_FLAG_PET_NAME = 0x00000800, // pet name, NULL terminated string
86 GROUP_UPDATE_FLAG_PET_MODEL_ID = 0x00001000, // uint16, model id
87 GROUP_UPDATE_FLAG_PET_CUR_HP = 0x00002000, // uint32 pet cur health
88 GROUP_UPDATE_FLAG_PET_MAX_HP = 0x00004000, // uint32 pet max health
89 GROUP_UPDATE_FLAG_PET_POWER_TYPE = 0x00008000, // uint8 pet power type
90 GROUP_UPDATE_FLAG_PET_CUR_POWER = 0x00010000, // uint16 pet cur power
91 GROUP_UPDATE_FLAG_PET_MAX_POWER = 0x00020000, // uint16 pet max power
92 GROUP_UPDATE_FLAG_PET_AURAS = 0x00040000, // uint64 mask, for each bit set uint32 spellid + uint8 unk, pet auras...
93 GROUP_UPDATE_FLAG_VEHICLE_SEAT = 0x00080000, // uint32 vehicle_seat_id (index from VehicleSeat.dbc)
94 GROUP_UPDATE_PET = 0x0007FC00, // all pet flags
95 GROUP_UPDATE_FULL = 0x0007FFFF, // all known flags
98 #define GROUP_UPDATE_FLAGS_COUNT 20
99 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19
100 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};
102 class InstanceSave;
104 class Roll : public LootValidatorRef
106 public:
107 Roll(ObjectGuid _lootedTragetGuid, LootItem const& li)
108 : lootedTargetGUID(_lootedTragetGuid), itemid(li.itemid), itemRandomPropId(li.randomPropertyId), itemRandomSuffix(li.randomSuffix),
109 itemCount(li.count), totalPlayersRolling(0), totalNeed(0), totalGreed(0), totalPass(0), itemSlot(0) {}
110 ~Roll() { }
111 void setLoot(Loot *pLoot) { link(pLoot, this); }
112 Loot *getLoot() { return getTarget(); }
113 void targetObjectBuildLink();
115 ObjectGuid lootedTargetGUID;
116 uint32 itemid;
117 int32 itemRandomPropId;
118 uint32 itemRandomSuffix;
119 uint8 itemCount;
120 typedef std::map<uint64, RollVote> PlayerVote;
121 PlayerVote playerVote; //vote position correspond with player position (in group)
122 uint8 totalPlayersRolling;
123 uint8 totalNeed;
124 uint8 totalGreed;
125 uint8 totalPass;
126 uint8 itemSlot;
129 struct InstanceGroupBind
131 InstanceSave *save;
132 bool perm;
133 /* permanent InstanceGroupBinds exist iff the leader has a permanent
134 PlayerInstanceBind for the same instance. */
135 InstanceGroupBind() : save(NULL), perm(false) {}
138 /** request member stats checken **/
139 /** todo: uninvite people that not accepted invite **/
140 class MANGOS_DLL_SPEC Group
142 public:
143 struct MemberSlot
145 uint64 guid;
146 std::string name;
147 uint8 group;
148 bool assistant;
150 typedef std::list<MemberSlot> MemberSlotList;
151 typedef MemberSlotList::const_iterator member_citerator;
153 typedef UNORDERED_MAP< uint32 /*mapId*/, InstanceGroupBind> BoundInstancesMap;
154 protected:
155 typedef MemberSlotList::iterator member_witerator;
156 typedef std::set<Player*> InvitesList;
158 typedef std::vector<Roll*> Rolls;
160 public:
161 Group();
162 ~Group();
164 // group manipulation methods
165 bool Create(const uint64 &guid, const char * name);
166 bool LoadGroupFromDB(Field *fields);
167 bool LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant);
168 bool AddInvite(Player *player);
169 uint32 RemoveInvite(Player *player);
170 void RemoveAllInvites();
171 bool AddLeaderInvite(Player *player);
172 bool AddMember(const uint64 &guid, const char* name);
173 // method: 0=just remove, 1=kick
174 uint32 RemoveMember(const uint64 &guid, const uint8 &method);
175 void ChangeLeader(const uint64 &guid);
176 void SetLootMethod(LootMethod method) { m_lootMethod = method; }
177 void SetLooterGuid(const uint64 &guid) { m_looterGuid = guid; }
178 void UpdateLooterGuid( Creature* creature, bool ifneed = false );
179 void SetLootThreshold(ItemQualities threshold) { m_lootThreshold = threshold; }
180 void Disband(bool hideDestroy=false);
182 // properties accessories
183 uint32 GetId() const { return m_Id; }
184 bool IsFull() const { return (m_groupType==GROUPTYPE_NORMAL) ? (m_memberSlots.size()>=MAXGROUPSIZE) : (m_memberSlots.size()>=MAXRAIDSIZE); }
185 bool isRaidGroup() const { return m_groupType==GROUPTYPE_RAID; }
186 bool isBGGroup() const { return m_bgGroup != NULL; }
187 bool IsCreated() const { return GetMembersCount() > 0; }
188 const uint64& GetLeaderGUID() const { return m_leaderGuid; }
189 const char * GetLeaderName() const { return m_leaderName.c_str(); }
190 LootMethod GetLootMethod() const { return m_lootMethod; }
191 const uint64& GetLooterGuid() const { return m_looterGuid; }
192 ItemQualities GetLootThreshold() const { return m_lootThreshold; }
194 // member manipulation methods
195 bool IsMember(const uint64& guid) const { return _getMemberCSlot(guid) != m_memberSlots.end(); }
196 bool IsLeader(const uint64& guid) const { return (GetLeaderGUID() == guid); }
197 uint64 GetMemberGUID(const std::string& name)
199 for(member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
201 if(itr->name == name)
203 return itr->guid;
206 return 0;
208 bool IsAssistant(uint64 guid) const
210 member_citerator mslot = _getMemberCSlot(guid);
211 if(mslot==m_memberSlots.end())
212 return false;
214 return mslot->assistant;
216 Player* GetInvited(const uint64& guid) const;
217 Player* GetInvited(const std::string& name) const;
219 bool SameSubGroup(uint64 guid1,const uint64& guid2) const
221 member_citerator mslot2 = _getMemberCSlot(guid2);
222 if(mslot2==m_memberSlots.end())
223 return false;
225 return SameSubGroup(guid1,&*mslot2);
228 bool SameSubGroup(uint64 guid1, MemberSlot const* slot2) const
230 member_citerator mslot1 = _getMemberCSlot(guid1);
231 if(mslot1==m_memberSlots.end() || !slot2)
232 return false;
234 return (mslot1->group==slot2->group);
237 bool HasFreeSlotSubGroup(uint8 subgroup) const
239 return (m_subGroupsCounts && m_subGroupsCounts[subgroup] < MAXGROUPSIZE);
242 bool SameSubGroup(Player const* member1, Player const* member2) const;
244 MemberSlotList const& GetMemberSlots() const { return m_memberSlots; }
245 GroupReference* GetFirstMember() { return m_memberMgr.getFirst(); }
246 uint32 GetMembersCount() const { return m_memberSlots.size(); }
247 void GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level);
248 uint8 GetMemberGroup(uint64 guid) const
250 member_citerator mslot = _getMemberCSlot(guid);
251 if(mslot==m_memberSlots.end())
252 return (MAXRAIDSIZE/MAXGROUPSIZE+1);
254 return mslot->group;
257 // some additional raid methods
258 void ConvertToRaid();
260 void SetBattlegroundGroup(BattleGround *bg) { m_bgGroup = bg; }
261 uint32 CanJoinBattleGroundQueue(BattleGround const* bgOrTemplate, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot);
263 void ChangeMembersGroup(const uint64 &guid, const uint8 &group);
264 void ChangeMembersGroup(Player *player, const uint8 &group);
266 void SetAssistant(uint64 guid, const bool &state)
268 if(!isRaidGroup())
269 return;
270 if(_setAssistantFlag(guid, state))
271 SendUpdate();
273 void SetMainTank(uint64 guid)
275 if(!isRaidGroup())
276 return;
278 if(_setMainTank(guid))
279 SendUpdate();
281 void SetMainAssistant(uint64 guid)
283 if(!isRaidGroup())
284 return;
286 if(_setMainAssistant(guid))
287 SendUpdate();
290 void SetTargetIcon(uint8 id, uint64 whoGuid, uint64 targetGuid);
292 Difficulty GetDifficulty(bool isRaid) const { return isRaid ? m_raidDifficulty : m_dungeonDifficulty; }
293 Difficulty GetDungeonDifficulty() const { return m_dungeonDifficulty; }
294 Difficulty GetRaidDifficulty() const { return m_raidDifficulty; }
295 void SetDungeonDifficulty(Difficulty difficulty);
296 void SetRaidDifficulty(Difficulty difficulty);
297 uint16 InInstance();
298 bool InCombatToInstance(uint32 instanceId);
299 void ResetInstances(uint8 method, bool isRaid, Player* SendMsgTo);
301 // -no description-
302 //void SendInit(WorldSession *session);
303 void SendTargetIconList(WorldSession *session);
304 void SendUpdate();
305 void UpdatePlayerOutOfRange(Player* pPlayer);
306 // ignore: GUID of player that will be ignored
307 void BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group=-1, uint64 ignore=0);
308 void BroadcastReadyCheck(WorldPacket *packet);
309 void OfflineReadyCheck();
311 /*********************************************************/
312 /*** LOOT SYSTEM ***/
313 /*********************************************************/
315 void SendLootStartRoll(uint32 CountDown, const Roll &r);
316 void SendLootRoll(ObjectGuid const& targetGuid, uint8 rollNumber, uint8 rollType, const Roll &r);
317 void SendLootRollWon(ObjectGuid const& targetGuid, uint8 rollNumber, uint8 rollType, const Roll &r);
318 void SendLootAllPassed(const Roll &r);
319 void GroupLoot(ObjectGuid const& playerGUID, Loot *loot, Creature *creature);
320 void NeedBeforeGreed(ObjectGuid const& playerGUID, Loot *loot, Creature *creature);
321 void MasterLoot(ObjectGuid const& playerGUID, Loot *loot, Creature *creature);
322 void CountRollVote(ObjectGuid const& playerGUID, ObjectGuid const& lootedTarget, uint32 itemSlot, uint8 choise);
323 void EndRoll();
325 void LinkMember(GroupReference *pRef) { m_memberMgr.insertFirst(pRef); }
326 void DelinkMember(GroupReference* /*pRef*/ ) { }
328 InstanceGroupBind* BindToInstance(InstanceSave *save, bool permanent, bool load = false);
329 void UnbindInstance(uint32 mapid, uint8 difficulty, bool unload = false);
330 InstanceGroupBind* GetBoundInstance(Player* player);
331 InstanceGroupBind* GetBoundInstance(Map* aMap);
332 BoundInstancesMap& GetBoundInstances(Difficulty difficulty) { return m_boundInstances[difficulty]; }
334 protected:
335 bool _addMember(const uint64 &guid, const char* name, bool isAssistant=false);
336 bool _addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group);
337 bool _removeMember(const uint64 &guid); // returns true if leader has changed
338 void _setLeader(const uint64 &guid);
340 void _removeRolls(const uint64 &guid);
342 bool _setMembersGroup(const uint64 &guid, const uint8 &group);
343 bool _setAssistantFlag(const uint64 &guid, const bool &state);
344 bool _setMainTank(const uint64 &guid);
345 bool _setMainAssistant(const uint64 &guid);
347 void _homebindIfInstance(Player *player);
349 void _initRaidSubGroupsCounter()
351 // Sub group counters initialization
352 if (!m_subGroupsCounts)
353 m_subGroupsCounts = new uint8[MAXRAIDSIZE / MAXGROUPSIZE];
355 memset((void*)m_subGroupsCounts, 0, (MAXRAIDSIZE / MAXGROUPSIZE)*sizeof(uint8));
357 for (member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
358 ++m_subGroupsCounts[itr->group];
361 member_citerator _getMemberCSlot(uint64 Guid) const
363 for(member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
365 if (itr->guid == Guid)
366 return itr;
368 return m_memberSlots.end();
371 member_witerator _getMemberWSlot(uint64 Guid)
373 for(member_witerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
375 if (itr->guid == Guid)
376 return itr;
378 return m_memberSlots.end();
381 void SubGroupCounterIncrease(uint8 subgroup)
383 if (m_subGroupsCounts)
384 ++m_subGroupsCounts[subgroup];
387 void SubGroupCounterDecrease(uint8 subgroup)
389 if (m_subGroupsCounts)
390 --m_subGroupsCounts[subgroup];
393 void CountTheRoll(Rolls::iterator& roll); // iterator update to next, in CountRollVote if true
394 bool CountRollVote(ObjectGuid const& playerGUID, Rolls::iterator& roll, uint8 choise);
396 uint32 m_Id; // 0 for not created or BG groups
397 MemberSlotList m_memberSlots;
398 GroupRefManager m_memberMgr;
399 InvitesList m_invitees;
400 uint64 m_leaderGuid;
401 std::string m_leaderName;
402 uint64 m_mainTank;
403 uint64 m_mainAssistant;
404 GroupType m_groupType;
405 Difficulty m_dungeonDifficulty;
406 Difficulty m_raidDifficulty;
407 BattleGround* m_bgGroup;
408 uint64 m_targetIcons[TARGETICONCOUNT];
409 LootMethod m_lootMethod;
410 ItemQualities m_lootThreshold;
411 uint64 m_looterGuid;
412 Rolls RollId;
413 BoundInstancesMap m_boundInstances[MAX_DIFFICULTY];
414 uint8* m_subGroupsCounts;
416 #endif