[7918] Improve portability in work with uint64 string format specifiers and in code...
[getmangos.git] / src / game / Group.h
blob478b7c5cbad246a77558a1380c7c7236b86c502f
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
27 #include <map>
28 #include <vector>
30 #define MAXGROUPSIZE 5
31 #define MAXRAIDSIZE 40
32 #define TARGETICONCOUNT 8
34 enum RollVote
36 PASS = 0,
37 NEED = 1,
38 GREED = 2,
39 NOT_EMITED_YET = 3,
40 NOT_VALID = 4
43 enum GroupMemberOnlineStatus
45 MEMBER_STATUS_OFFLINE = 0x0000,
46 MEMBER_STATUS_ONLINE = 0x0001,
47 MEMBER_STATUS_PVP = 0x0002,
48 MEMBER_STATUS_UNK0 = 0x0004, // dead? (health=0)
49 MEMBER_STATUS_UNK1 = 0x0008, // ghost? (health=1)
50 MEMBER_STATUS_UNK2 = 0x0010, // never seen
51 MEMBER_STATUS_UNK3 = 0x0020, // never seen
52 MEMBER_STATUS_UNK4 = 0x0040, // appears with dead and ghost flags
53 MEMBER_STATUS_UNK5 = 0x0080, // never seen
56 enum GroupType
58 GROUPTYPE_NORMAL = 0,
59 GROUPTYPE_RAID = 1
62 class BattleGround;
64 enum GroupUpdateFlags
66 GROUP_UPDATE_FLAG_NONE = 0x00000000, // nothing
67 GROUP_UPDATE_FLAG_STATUS = 0x00000001, // uint16, flags
68 GROUP_UPDATE_FLAG_CUR_HP = 0x00000002, // uint32
69 GROUP_UPDATE_FLAG_MAX_HP = 0x00000004, // uint32
70 GROUP_UPDATE_FLAG_POWER_TYPE = 0x00000008, // uint8
71 GROUP_UPDATE_FLAG_CUR_POWER = 0x00000010, // uint16
72 GROUP_UPDATE_FLAG_MAX_POWER = 0x00000020, // uint16
73 GROUP_UPDATE_FLAG_LEVEL = 0x00000040, // uint16
74 GROUP_UPDATE_FLAG_ZONE = 0x00000080, // uint16
75 GROUP_UPDATE_FLAG_POSITION = 0x00000100, // uint16, uint16
76 GROUP_UPDATE_FLAG_AURAS = 0x00000200, // uint64 mask, for each bit set uint32 spellid + uint8 unk
77 GROUP_UPDATE_FLAG_PET_GUID = 0x00000400, // uint64 pet guid
78 GROUP_UPDATE_FLAG_PET_NAME = 0x00000800, // pet name, NULL terminated string
79 GROUP_UPDATE_FLAG_PET_MODEL_ID = 0x00001000, // uint16, model id
80 GROUP_UPDATE_FLAG_PET_CUR_HP = 0x00002000, // uint32 pet cur health
81 GROUP_UPDATE_FLAG_PET_MAX_HP = 0x00004000, // uint32 pet max health
82 GROUP_UPDATE_FLAG_PET_POWER_TYPE = 0x00008000, // uint8 pet power type
83 GROUP_UPDATE_FLAG_PET_CUR_POWER = 0x00010000, // uint16 pet cur power
84 GROUP_UPDATE_FLAG_PET_MAX_POWER = 0x00020000, // uint16 pet max power
85 GROUP_UPDATE_FLAG_PET_AURAS = 0x00040000, // uint64 mask, for each bit set uint32 spellid + uint8 unk, pet auras...
86 GROUP_UPDATE_FLAG_VEHICLE_SEAT = 0x00080000, // uint32 vehicle_seat_id (index from VehicleSeat.dbc)
87 GROUP_UPDATE_PET = 0x0007FC00, // all pet flags
88 GROUP_UPDATE_FULL = 0x0007FFFF, // all known flags
91 #define GROUP_UPDATE_FLAGS_COUNT 20
92 // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19
93 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};
95 class InstanceSave;
97 class Roll : public LootValidatorRef
99 public:
100 Roll(uint64 _guid, LootItem const& li)
101 : itemGUID(_guid), itemid(li.itemid), itemRandomPropId(li.randomPropertyId), itemRandomSuffix(li.randomSuffix),
102 totalPlayersRolling(0), totalNeed(0), totalGreed(0), totalPass(0), itemSlot(0) {}
103 ~Roll() { }
104 void setLoot(Loot *pLoot) { link(pLoot, this); }
105 Loot *getLoot() { return getTarget(); }
106 void targetObjectBuildLink();
108 uint64 itemGUID;
109 uint32 itemid;
110 int32 itemRandomPropId;
111 uint32 itemRandomSuffix;
112 typedef std::map<uint64, RollVote> PlayerVote;
113 PlayerVote playerVote; //vote position correspond with player position (in group)
114 uint8 totalPlayersRolling;
115 uint8 totalNeed;
116 uint8 totalGreed;
117 uint8 totalPass;
118 uint8 itemSlot;
121 struct InstanceGroupBind
123 InstanceSave *save;
124 bool perm;
125 /* permanent InstanceGroupBinds exist iff the leader has a permanent
126 PlayerInstanceBind for the same instance. */
127 InstanceGroupBind() : save(NULL), perm(false) {}
130 /** request member stats checken **/
131 /** todo: uninvite people that not accepted invite **/
132 class MANGOS_DLL_SPEC Group
134 public:
135 struct MemberSlot
137 uint64 guid;
138 std::string name;
139 uint8 group;
140 bool assistant;
142 typedef std::list<MemberSlot> MemberSlotList;
143 typedef MemberSlotList::const_iterator member_citerator;
145 typedef UNORDERED_MAP< uint32 /*mapId*/, InstanceGroupBind> BoundInstancesMap;
146 protected:
147 typedef MemberSlotList::iterator member_witerator;
148 typedef std::set<Player*> InvitesList;
150 typedef std::vector<Roll*> Rolls;
152 public:
153 Group();
154 ~Group();
156 // group manipulation methods
157 bool Create(const uint64 &guid, const char * name);
158 bool LoadGroupFromDB(const uint64 &leaderGuid, QueryResult *result = NULL, bool loadMembers = true);
159 bool LoadMemberFromDB(uint32 guidLow, uint8 subgroup, bool assistant);
160 bool AddInvite(Player *player);
161 uint32 RemoveInvite(Player *player);
162 void RemoveAllInvites();
163 bool AddLeaderInvite(Player *player);
164 bool AddMember(const uint64 &guid, const char* name);
165 // method: 0=just remove, 1=kick
166 uint32 RemoveMember(const uint64 &guid, const uint8 &method);
167 void ChangeLeader(const uint64 &guid);
168 void SetLootMethod(LootMethod method) { m_lootMethod = method; }
169 void SetLooterGuid(const uint64 &guid) { m_looterGuid = guid; }
170 void UpdateLooterGuid( Creature* creature, bool ifneed = false );
171 void SetLootThreshold(ItemQualities threshold) { m_lootThreshold = threshold; }
172 void Disband(bool hideDestroy=false);
174 // properties accessories
175 bool IsFull() const { return (m_groupType==GROUPTYPE_NORMAL) ? (m_memberSlots.size()>=MAXGROUPSIZE) : (m_memberSlots.size()>=MAXRAIDSIZE); }
176 bool isRaidGroup() const { return m_groupType==GROUPTYPE_RAID; }
177 bool isBGGroup() const { return m_bgGroup != NULL; }
178 bool IsCreated() const { return GetMembersCount() > 0; }
179 const uint64& GetLeaderGUID() const { return m_leaderGuid; }
180 const char * GetLeaderName() const { return m_leaderName.c_str(); }
181 LootMethod GetLootMethod() const { return m_lootMethod; }
182 const uint64& GetLooterGuid() const { return m_looterGuid; }
183 ItemQualities GetLootThreshold() const { return m_lootThreshold; }
185 // member manipulation methods
186 bool IsMember(const uint64& guid) const { return _getMemberCSlot(guid) != m_memberSlots.end(); }
187 bool IsLeader(const uint64& guid) const { return (GetLeaderGUID() == guid); }
188 uint64 GetMemberGUID(const std::string& name)
190 for(member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
192 if(itr->name == name)
194 return itr->guid;
197 return 0;
199 bool IsAssistant(uint64 guid) const
201 member_citerator mslot = _getMemberCSlot(guid);
202 if(mslot==m_memberSlots.end())
203 return false;
205 return mslot->assistant;
207 Player* GetInvited(const uint64& guid) const;
208 Player* GetInvited(const std::string& name) const;
210 bool SameSubGroup(uint64 guid1,const uint64& guid2) const
212 member_citerator mslot2 = _getMemberCSlot(guid2);
213 if(mslot2==m_memberSlots.end())
214 return false;
216 return SameSubGroup(guid1,&*mslot2);
219 bool SameSubGroup(uint64 guid1, MemberSlot const* slot2) const
221 member_citerator mslot1 = _getMemberCSlot(guid1);
222 if(mslot1==m_memberSlots.end() || !slot2)
223 return false;
225 return (mslot1->group==slot2->group);
228 bool HasFreeSlotSubGroup(uint8 subgroup) const
230 return (m_subGroupsCounts && m_subGroupsCounts[subgroup] < MAXGROUPSIZE);
233 bool SameSubGroup(Player const* member1, Player const* member2) const;
235 MemberSlotList const& GetMemberSlots() const { return m_memberSlots; }
236 GroupReference* GetFirstMember() { return m_memberMgr.getFirst(); }
237 uint32 GetMembersCount() const { return m_memberSlots.size(); }
238 void GetDataForXPAtKill(Unit const* victim, uint32& count,uint32& sum_level, Player* & member_with_max_level, Player* & not_gray_member_with_max_level);
239 uint8 GetMemberGroup(uint64 guid) const
241 member_citerator mslot = _getMemberCSlot(guid);
242 if(mslot==m_memberSlots.end())
243 return (MAXRAIDSIZE/MAXGROUPSIZE+1);
245 return mslot->group;
248 // some additional raid methods
249 void ConvertToRaid();
251 void SetBattlegroundGroup(BattleGround *bg) { m_bgGroup = bg; }
252 uint32 CanJoinBattleGroundQueue(BattleGroundTypeId bgTypeId, BattleGroundQueueTypeId bgQueueTypeId, uint32 MinPlayerCount, uint32 MaxPlayerCount, bool isRated, uint32 arenaSlot);
254 void ChangeMembersGroup(const uint64 &guid, const uint8 &group);
255 void ChangeMembersGroup(Player *player, const uint8 &group);
257 void SetAssistant(uint64 guid, const bool &state)
259 if(!isRaidGroup())
260 return;
261 if(_setAssistantFlag(guid, state))
262 SendUpdate();
264 void SetMainTank(uint64 guid)
266 if(!isRaidGroup())
267 return;
269 if(_setMainTank(guid))
270 SendUpdate();
272 void SetMainAssistant(uint64 guid)
274 if(!isRaidGroup())
275 return;
277 if(_setMainAssistant(guid))
278 SendUpdate();
281 void SetTargetIcon(uint8 id, uint64 guid);
282 void SetDifficulty(uint8 difficulty);
283 uint8 GetDifficulty() { return m_difficulty; }
284 uint16 InInstance();
285 bool InCombatToInstance(uint32 instanceId);
286 void ResetInstances(uint8 method, Player* SendMsgTo);
288 // -no description-
289 //void SendInit(WorldSession *session);
290 void SendTargetIconList(WorldSession *session);
291 void SendUpdate();
292 void UpdatePlayerOutOfRange(Player* pPlayer);
293 // ignore: GUID of player that will be ignored
294 void BroadcastPacket(WorldPacket *packet, bool ignorePlayersInBGRaid, int group=-1, uint64 ignore=0);
295 void BroadcastReadyCheck(WorldPacket *packet);
296 void OfflineReadyCheck();
298 /*********************************************************/
299 /*** LOOT SYSTEM ***/
300 /*********************************************************/
302 void SendLootStartRoll(uint32 CountDown, const Roll &r);
303 void SendLootRoll(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r);
304 void SendLootRollWon(const uint64& SourceGuid, const uint64& TargetGuid, uint8 RollNumber, uint8 RollType, const Roll &r);
305 void SendLootAllPassed(uint32 NumberOfPlayers, const Roll &r);
306 void GroupLoot(const uint64& playerGUID, Loot *loot, Creature *creature);
307 void NeedBeforeGreed(const uint64& playerGUID, Loot *loot, Creature *creature);
308 void MasterLoot(const uint64& playerGUID, Loot *loot, Creature *creature);
309 Rolls::iterator GetRoll(uint64 Guid)
311 Rolls::iterator iter;
312 for (iter=RollId.begin(); iter != RollId.end(); ++iter)
314 if ((*iter)->itemGUID == Guid && (*iter)->isValid())
316 return iter;
319 return RollId.end();
321 void CountTheRoll(Rolls::iterator roll, uint32 NumberOfPlayers);
322 void CountRollVote(const uint64& playerGUID, const uint64& Guid, uint32 NumberOfPlayers, 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(uint32 mapid, uint8 difficulty);
331 BoundInstancesMap& GetBoundInstances(uint8 difficulty) { return m_boundInstances[difficulty]; }
333 protected:
334 bool _addMember(const uint64 &guid, const char* name, bool isAssistant=false);
335 bool _addMember(const uint64 &guid, const char* name, bool isAssistant, uint8 group);
336 bool _removeMember(const uint64 &guid); // returns true if leader has changed
337 void _setLeader(const uint64 &guid);
339 void _removeRolls(const uint64 &guid);
341 bool _setMembersGroup(const uint64 &guid, const uint8 &group);
342 bool _setAssistantFlag(const uint64 &guid, const bool &state);
343 bool _setMainTank(const uint64 &guid);
344 bool _setMainAssistant(const uint64 &guid);
346 void _homebindIfInstance(Player *player);
348 void _initRaidSubGroupsCounter()
350 // Sub group counters initialization
351 if (!m_subGroupsCounts)
352 m_subGroupsCounts = new uint8[MAXRAIDSIZE / MAXGROUPSIZE];
354 memset((void*)m_subGroupsCounts, 0, (MAXRAIDSIZE / MAXGROUPSIZE)*sizeof(uint8));
356 for (member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
357 ++m_subGroupsCounts[itr->group];
360 member_citerator _getMemberCSlot(uint64 Guid) const
362 for(member_citerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
364 if (itr->guid == Guid)
365 return itr;
367 return m_memberSlots.end();
370 member_witerator _getMemberWSlot(uint64 Guid)
372 for(member_witerator itr = m_memberSlots.begin(); itr != m_memberSlots.end(); ++itr)
374 if (itr->guid == Guid)
375 return itr;
377 return m_memberSlots.end();
380 void SubGroupCounterIncrease(uint8 subgroup)
382 if (m_subGroupsCounts)
383 ++m_subGroupsCounts[subgroup];
386 void SubGroupCounterDecrease(uint8 subgroup)
388 if (m_subGroupsCounts)
389 --m_subGroupsCounts[subgroup];
392 MemberSlotList m_memberSlots;
393 GroupRefManager m_memberMgr;
394 InvitesList m_invitees;
395 uint64 m_leaderGuid;
396 std::string m_leaderName;
397 uint64 m_mainTank;
398 uint64 m_mainAssistant;
399 GroupType m_groupType;
400 uint8 m_difficulty;
401 BattleGround* m_bgGroup;
402 uint64 m_targetIcons[TARGETICONCOUNT];
403 LootMethod m_lootMethod;
404 ItemQualities m_lootThreshold;
405 uint64 m_looterGuid;
406 Rolls RollId;
407 BoundInstancesMap m_boundInstances[TOTAL_DIFFICULTIES];
408 uint8* m_subGroupsCounts;
410 #endif