TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ObjectGuid.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef ObjectGuid_h__
20 #define ObjectGuid_h__
21 
22 #include "Common.h"
23 #include "ByteBuffer.h"
24 #include <type_traits>
25 #include <functional>
26 
27 enum TypeID
28 {
40 };
41 
42 #define NUM_CLIENT_OBJECT_TYPES 11
43 
45 {
46  TYPEMASK_OBJECT = 0x0001,
47  TYPEMASK_ITEM = 0x0002,
49  TYPEMASK_UNIT = 0x0008,
50  TYPEMASK_PLAYER = 0x0010,
53  TYPEMASK_CORPSE = 0x0080,
58 };
59 
60 enum class HighGuid
61 {
62  Null = 0,
63  Uniq = 1,
64  Player = 2,
65  Item = 3,
66  WorldTransaction = 4,
67  StaticDoor = 5, //NYI
68  Transport = 6,
69  Conversation = 7,
70  Creature = 8,
71  Vehicle = 9,
72  Pet = 10,
73  GameObject = 11,
74  DynamicObject = 12,
75  AreaTrigger = 13,
76  Corpse = 14,
77  LootObject = 15,
78  SceneObject = 16,
79  Scenario = 17,
80  AIGroup = 18,
81  DynamicDoor = 19,
82  ClientActor = 20, //NYI
83  Vignette = 21,
84  CallForHelp = 22,
85  AIResource = 23,
86  AILock = 24,
87  AILockTicket = 25,
88  ChatChannel = 26,
89  Party = 27,
90  Guild = 28,
91  WowAccount = 29,
92  BNetAccount = 30,
93  GMTask = 31,
94  MobileSession = 32, //NYI
95  RaidGroup = 33,
96  Spell = 34,
97  Mail = 35,
98  WebObj = 36, //NYI
99  LFGObject = 37, //NYI
100  LFGList = 38, //NYI
101  UserRouter = 39,
102  PVPQueueGroup = 40,
103  UserClient = 41,
104  PetBattle = 42, //NYI
105  UniqUserClient = 43,
106  BattlePet = 44,
107  CommerceObj = 45,
108  ClientSession = 46,
109 
110  Count,
111 };
112 
113 template<HighGuid high>
115 {
116  static bool const Global = false;
117  static bool const RealmSpecific = false;
118  static bool const MapSpecific = false;
119 };
120 
121 #define GUID_TRAIT_GLOBAL(highguid) \
122  template<> struct ObjectGuidTraits<highguid> \
123  { \
124  static bool const Global = true; \
125  static bool const RealmSpecific = false; \
126  static bool const MapSpecific = false; \
127  };
128 
129 #define GUID_TRAIT_REALM_SPECIFIC(highguid) \
130  template<> struct ObjectGuidTraits<highguid> \
131  { \
132  static bool const Global = false; \
133  static bool const RealmSpecific = true; \
134  static bool const MapSpecific = false; \
135  };
136 
137 #define GUID_TRAIT_MAP_SPECIFIC(highguid) \
138  template<> struct ObjectGuidTraits<highguid> \
139  { \
140  static bool const Global = false; \
141  static bool const RealmSpecific = false; \
142  static bool const MapSpecific = true; \
143  };
144 
147 GUID_TRAIT_GLOBAL(HighGuid::WowAccount)
148 GUID_TRAIT_GLOBAL(HighGuid::BNetAccount)
150 GUID_TRAIT_GLOBAL(HighGuid::RaidGroup)
153 GUID_TRAIT_GLOBAL(HighGuid::UserRouter)
154 GUID_TRAIT_GLOBAL(HighGuid::PVPQueueGroup)
155 GUID_TRAIT_GLOBAL(HighGuid::UserClient)
156 GUID_TRAIT_GLOBAL(HighGuid::UniqUserClient)
157 GUID_TRAIT_GLOBAL(HighGuid::BattlePet)
158 GUID_TRAIT_GLOBAL(HighGuid::CommerceObj)
159 GUID_TRAIT_GLOBAL(HighGuid::ClientSession)
161 GUID_TRAIT_REALM_SPECIFIC(HighGuid::Item) // This is not exactly correct, there are 2 more unknown parts in highguid: (high >> 10 & 0xFF), (high >> 18 & 0xFFFFFF)
164 GUID_TRAIT_MAP_SPECIFIC(HighGuid::WorldTransaction)
165 GUID_TRAIT_MAP_SPECIFIC(HighGuid::Conversation)
174 GUID_TRAIT_MAP_SPECIFIC(HighGuid::SceneObject)
177 GUID_TRAIT_MAP_SPECIFIC(HighGuid::DynamicDoor)
179 GUID_TRAIT_MAP_SPECIFIC(HighGuid::CallForHelp)
182 GUID_TRAIT_MAP_SPECIFIC(HighGuid::AILockTicket)
183 
184 class ObjectGuid;
185 class PackedGuid;
186 
187 #pragma pack(push, 1)
188 
189 class TC_GAME_API ObjectGuid
190 {
191  friend TC_GAME_API std::ostream& operator<<(std::ostream& stream, ObjectGuid const& guid);
192  friend TC_GAME_API ByteBuffer& operator<<(ByteBuffer& buf, ObjectGuid const& guid);
193  friend TC_GAME_API ByteBuffer& operator>>(ByteBuffer& buf, ObjectGuid& guid);
194 
195  public:
196  static ObjectGuid const Empty;
197  static ObjectGuid const TradeItem;
198 
199  typedef uint64 LowType;
200 
201  template<HighGuid type>
202  static typename std::enable_if<ObjectGuidTraits<type>::Global, ObjectGuid>::type Create(LowType counter) { return Global(type, counter); }
203 
204  template<HighGuid type>
205  static typename std::enable_if<ObjectGuidTraits<type>::RealmSpecific, ObjectGuid>::type Create(LowType counter) { return RealmSpecific(type, counter); }
206 
207  template<HighGuid type>
208  static typename std::enable_if<ObjectGuidTraits<type>::MapSpecific, ObjectGuid>::type Create(uint16 mapId, uint32 entry, LowType counter) { return MapSpecific(type, 0, mapId, 0, entry, counter); }
209 
210  ObjectGuid() : _low(0), _high(0) { }
211 
212  std::vector<uint8> GetRawValue() const;
213  void SetRawValue(std::vector<uint8> const& guid);
214  void SetRawValue(uint64 high, uint64 low) { _high = high; _low = low; }
215  void Clear() { _high = 0; _low = 0; }
216 
217  HighGuid GetHigh() const { return HighGuid((_high >> 58) & 0x3F); }
218  uint32 GetRealmId() const { return uint32((_high >> 42) & 0x1FFF); }
219  uint32 GetMapId() const { return uint32((_high >> 29) & 0x1FFF); }
220  uint32 GetEntry() const { return uint32((_high >> 6) & 0x7FFFFF); }
221  LowType GetCounter() const { return _low & UI64LIT(0x000000FFFFFFFFFF); }
222 
223  static LowType GetMaxCounter(HighGuid /*high*/)
224  {
225  return UI64LIT(0xFFFFFFFFFF);
226  }
227 
228  LowType GetMaxCounter() const { return GetMaxCounter(GetHigh()); }
229 
231  {
232  ASSERT(index < sizeof(uint64) * 2);
233  return ((uint8*)&_low)[index];
234  }
235 
236  uint8 const& operator[](uint32 index) const
237  {
238  ASSERT(index < sizeof(uint64) * 2);
239  return ((uint8 const*)&_low)[index];
240  }
241 
242  bool IsEmpty() const { return _low == 0 && _high == 0; }
243  bool IsCreature() const { return GetHigh() == HighGuid::Creature; }
244  bool IsPet() const { return GetHigh() == HighGuid::Pet; }
245  bool IsVehicle() const { return GetHigh() == HighGuid::Vehicle; }
246  bool IsCreatureOrPet() const { return IsCreature() || IsPet(); }
247  bool IsCreatureOrVehicle() const { return IsCreature() || IsVehicle(); }
248  bool IsAnyTypeCreature() const { return IsCreature() || IsPet() || IsVehicle(); }
249  bool IsPlayer() const { return !IsEmpty() && GetHigh() == HighGuid::Player; }
250  bool IsUnit() const { return IsAnyTypeCreature() || IsPlayer(); }
251  bool IsItem() const { return GetHigh() == HighGuid::Item; }
252  bool IsGameObject() const { return GetHigh() == HighGuid::GameObject; }
253  bool IsDynamicObject() const { return GetHigh() == HighGuid::DynamicObject; }
254  bool IsCorpse() const { return GetHigh() == HighGuid::Corpse; }
255  bool IsAreaTrigger() const { return GetHigh() == HighGuid::AreaTrigger; }
256  bool IsMOTransport() const { return GetHigh() == HighGuid::Transport; }
257  bool IsAnyTypeGameObject() const { return IsGameObject() || IsMOTransport(); }
258  bool IsParty() const { return GetHigh() == HighGuid::Party; }
259  bool IsGuild() const { return GetHigh() == HighGuid::Guild; }
260  bool IsSceneObject() const { return GetHigh() == HighGuid::SceneObject; }
261  bool IsConversation() const { return GetHigh() == HighGuid::Conversation; }
262 
263  static TypeID GetTypeId(HighGuid high)
264  {
265  switch (high)
266  {
267  case HighGuid::Item: return TYPEID_ITEM;
268  case HighGuid::Creature:
269  case HighGuid::Pet:
270  case HighGuid::Vehicle: return TYPEID_UNIT;
271  case HighGuid::Player: return TYPEID_PLAYER;
275  case HighGuid::Corpse: return TYPEID_CORPSE;
279  default: return TYPEID_OBJECT;
280  }
281  }
282 
283  TypeID GetTypeId() const { return GetTypeId(GetHigh()); }
284 
285  bool operator!() const { return IsEmpty(); }
286  bool operator== (ObjectGuid const& guid) const { return _low == guid._low && _high == guid._high; }
287  bool operator!= (ObjectGuid const& guid) const { return !(*this == guid); }
288  bool operator< (ObjectGuid const& guid) const
289  {
290  if (_high < guid._high)
291  return true;
292  else if (_high > guid._high)
293  return false;
294 
295  return _low < guid._low;
296  }
297 
298  static char const* GetTypeName(HighGuid high);
299  char const* GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; }
300  std::string ToString() const;
301 
302  private:
303  static bool HasEntry(HighGuid high)
304  {
305  switch (high)
306  {
308  case HighGuid::Creature:
309  case HighGuid::Pet:
310  case HighGuid::Vehicle:
311  default:
312  return true;
313  }
314  }
315 
316  bool HasEntry() const { return HasEntry(GetHigh()); }
317 
318  static ObjectGuid Global(HighGuid type, LowType counter);
319  static ObjectGuid RealmSpecific(HighGuid type, LowType counter);
320  static ObjectGuid MapSpecific(HighGuid type, uint8 subType, uint16 mapId, uint32 serverId, uint32 entry, LowType counter);
321 
322  ObjectGuid(uint64 high, uint64 low) : _low(low), _high(high) { }
323  explicit ObjectGuid(uint32 const&) = delete; // no implementation, used to catch wrong type assignment
324 
327 };
328 
329 #pragma pack(pop)
330 
331 // Some Shared defines
332 typedef std::set<ObjectGuid> GuidSet;
333 typedef std::list<ObjectGuid> GuidList;
334 typedef std::deque<ObjectGuid> GuidDeque;
335 typedef std::vector<ObjectGuid> GuidVector;
336 typedef std::unordered_set<ObjectGuid> GuidUnorderedSet;
337 
338 // maximum buffer size for packed guid is 18 bytes
339 #define PACKED_GUID_MIN_BUFFER_SIZE 18
340 
341 class TC_GAME_API PackedGuid
342 {
343  friend TC_GAME_API ByteBuffer& operator<<(ByteBuffer& buf, PackedGuid const& guid);
344 
345  public:
346  explicit PackedGuid() : _packedGuid(PACKED_GUID_MIN_BUFFER_SIZE) { _packedGuid << uint16(0); }
347  explicit PackedGuid(ObjectGuid const& guid) : _packedGuid(PACKED_GUID_MIN_BUFFER_SIZE) { Set(guid); }
348 
349  void Set(ObjectGuid const& guid);
350 
351  size_t size() const { return _packedGuid.size(); }
352 
353  private:
355 };
356 
358 {
359 public:
360  ObjectGuidGeneratorBase(ObjectGuid::LowType start = UI64LIT(1)) : _nextGuid(start) { }
361 
362  virtual void Set(uint64 val) { _nextGuid = val; }
363  virtual ObjectGuid::LowType Generate() = 0;
364  ObjectGuid::LowType GetNextAfterMaxUsed() const { return _nextGuid; }
365 
366 protected:
367  static void HandleCounterOverflow(HighGuid high);
369 };
370 
371 template<HighGuid high>
373 {
374 public:
376 
378  {
379  if (_nextGuid >= ObjectGuid::GetMaxCounter(high) - 1)
380  HandleCounterOverflow(high);
381  return _nextGuid++;
382  }
383 };
384 
385 TC_GAME_API ByteBuffer& operator<<(ByteBuffer& buf, ObjectGuid const& guid);
386 TC_GAME_API ByteBuffer& operator>>(ByteBuffer& buf, ObjectGuid& guid);
387 
388 TC_GAME_API ByteBuffer& operator<<(ByteBuffer& buf, PackedGuid const& guid);
389 
390 TC_GAME_API std::ostream& operator<<(std::ostream& stream, ObjectGuid const& guid);
391 
392 namespace std
393 {
394  template<>
395  struct hash<ObjectGuid>
396  {
397  public:
398  size_t operator()(ObjectGuid const& key) const
399  {
400  return boost::hash_range(reinterpret_cast<uint64 const*>(&key), reinterpret_cast<uint64 const*>(&key) + 2);
401  }
402  };
403 }
404 
405 #endif // ObjectGuid_h__
bool IsCreatureOrVehicle() const
Definition: ObjectGuid.h:247
bool IsUnit() const
Definition: ObjectGuid.h:250
bool IsGameObject() const
Definition: ObjectGuid.h:252
Definition: Corpse.h:49
bool IsPlayer() const
Definition: ObjectGuid.h:249
Definition: ObjectGuid.h:47
Definition: Vehicle.h:32
virtual void Set(uint64 val)
Definition: ObjectGuid.h:362
Definition: ObjectGuid.h:50
static LowType GetMaxCounter(HighGuid)
Definition: ObjectGuid.h:223
static ObjectGuid const Empty
Definition: ObjectGuid.h:196
Definition: ByteBuffer.h:70
TypeID GetTypeId() const
Definition: ObjectGuid.h:283
Definition: ObjectGuid.h:31
Definition: ObjectGuid.h:36
PackedGuid()
Definition: ObjectGuid.h:346
#define hash
Definition: private_namespace.h:186
ByteBuffer _packedGuid
Definition: ObjectGuid.h:354
uint64 _low
Definition: ObjectGuid.h:325
TC_GAME_API ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
Definition: ObjectGuid.cpp:167
Definition: ObjectGuid.h:372
Definition: ObjectGuid.h:46
static bool const MapSpecific
Definition: ObjectGuid.h:118
uint8 & operator[](uint32 index)
Definition: ObjectGuid.h:230
#define UI64LIT(N)
Definition: Define.h:138
Definition: ObjectGuid.h:32
STL namespace.
char const * GetTypeName() const
Definition: ObjectGuid.h:299
static std::enable_if< ObjectGuidTraits< type >::RealmSpecific, ObjectGuid >::type Create(LowType counter)
Definition: ObjectGuid.h:205
uint64 _nextGuid
Definition: ObjectGuid.h:368
Definition: ObjectGuid.h:357
Definition: Transport.h:28
Definition: ObjectGuid.h:56
Definition: Creature.h:467
std::deque< ObjectGuid > GuidDeque
Definition: ObjectGuid.h:334
ObjectGuid::LowType Generate() override
Definition: ObjectGuid.h:377
std::vector< ObjectGuid > GuidVector
Definition: ObjectGuid.h:335
bool operator<(const String &left, const String &right)
Definition: String.cpp:785
ObjectGuid::LowType GetNextAfterMaxUsed() const
Definition: ObjectGuid.h:364
uint64 LowType
Definition: ObjectGuid.h:199
static void HandleCounterOverflow(HighGuid high)
Definition: ObjectGuid.cpp:193
bool IsSceneObject() const
Definition: ObjectGuid.h:260
uint16_t uint16
Definition: g3dmath.h:166
std::list< ObjectGuid > GuidList
Definition: ObjectGuid.h:333
static bool const RealmSpecific
Definition: ObjectGuid.h:117
bool IsAnyTypeGameObject() const
Definition: ObjectGuid.h:257
bool IsMOTransport() const
Definition: ObjectGuid.h:256
Definition: ObjectGuid.h:49
bool IsItem() const
Definition: ObjectGuid.h:251
Definition: Mail.h:166
Definition: DynamicObject.h:35
Definition: ObjectGuid.h:114
bool IsDynamicObject() const
Definition: ObjectGuid.h:253
Definition: ObjectGuid.h:51
uint32 GetMapId() const
Definition: ObjectGuid.h:219
string ToString(int i)
Definition: strutil.h:491
Definition: ObjectGuid.h:37
#define GUID_TRAIT_GLOBAL(highguid)
Definition: ObjectGuid.h:121
HighGuid
Definition: ObjectGuid.h:60
bool IsVehicle() const
Definition: ObjectGuid.h:245
static std::enable_if< ObjectGuidTraits< type >::Global, ObjectGuid >::type Create(LowType counter)
Definition: ObjectGuid.h:202
Definition: ObjectGuid.h:39
static std::enable_if< ObjectGuidTraits< type >::MapSpecific, ObjectGuid >::type Create(uint16 mapId, uint32 entry, LowType counter)
Definition: ObjectGuid.h:208
PackedGuid(ObjectGuid const &guid)
Definition: ObjectGuid.h:347
Definition: Item.h:259
ObjectGuid(uint64 high, uint64 low)
Definition: ObjectGuid.h:322
bool operator!=(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:166
Definition: ObjectGuid.h:34
#define GUID_TRAIT_MAP_SPECIFIC(highguid)
Definition: ObjectGuid.h:137
uint32 GetRealmId() const
Definition: ObjectGuid.h:218
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
uint32_t uint32
Definition: Define.h:150
bool operator!() const
Definition: ObjectGuid.h:285
uint64_t uint64
Definition: Define.h:149
size_t size() const
Definition: ObjectGuid.h:351
uint16_t uint16
Definition: Define.h:151
Definition: ObjectGuid.h:57
LowType GetMaxCounter() const
Definition: ObjectGuid.h:228
Definition: GameObject.h:880
TypeMask
Definition: ObjectGuid.h:44
void SetRawValue(uint64 high, uint64 low)
Definition: ObjectGuid.h:214
TC_GAME_API ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
Definition: ObjectGuid.cpp:146
#define GUID_TRAIT_REALM_SPECIFIC(highguid)
Definition: ObjectGuid.h:129
static bool const Global
Definition: ObjectGuid.h:116
Definition: ObjectGuid.h:29
ObjectGuidGeneratorBase(ObjectGuid::LowType start=UI64LIT(1))
Definition: ObjectGuid.h:360
uint32 GetEntry() const
Definition: ObjectGuid.h:220
bool IsAreaTrigger() const
Definition: ObjectGuid.h:255
TypeID
Definition: ObjectGuid.h:27
void Clear()
Definition: ObjectGuid.h:215
size_t operator()(ObjectGuid const &key) const
Definition: ObjectGuid.h:398
bool IsConversation() const
Definition: ObjectGuid.h:261
#define PACKED_GUID_MIN_BUFFER_SIZE
Definition: ObjectGuid.h:339
Definition: ObjectGuid.h:38
Definition: ObjectGuid.h:33
std::set< ObjectGuid > GuidSet
Definition: ObjectGuid.h:332
static ObjectGuid const TradeItem
Definition: ObjectGuid.h:197
#define TC_GAME_API
Definition: Define.h:134
bool HasEntry() const
Definition: ObjectGuid.h:316
Definition: ObjectGuid.h:54
HighGuid GetHigh() const
Definition: ObjectGuid.h:217
bool IsCreatureOrPet() const
Definition: ObjectGuid.h:246
bool IsAnyTypeCreature() const
Definition: ObjectGuid.h:248
static TypeID GetTypeId(HighGuid high)
Definition: ObjectGuid.h:263
Definition: ObjectGuid.h:48
uint8_t uint8
Definition: Define.h:152
#define ASSERT
Definition: Errors.h:55
static bool HasEntry(HighGuid high)
Definition: ObjectGuid.h:303
uint8 const & operator[](uint32 index) const
Definition: ObjectGuid.h:236
std::unordered_set< ObjectGuid > GuidUnorderedSet
Definition: ObjectGuid.h:336
Definition: ObjectGuid.h:189
uint32_t uint32
Definition: g3dmath.h:168
ObjectGuidGenerator(ObjectGuid::LowType start=UI64LIT(1))
Definition: ObjectGuid.h:375
Definition: ObjectGuid.h:35
Definition: ObjectGuid.h:55
Definition: ObjectGuid.h:341
Definition: ObjectGuid.h:53
Definition: Guild.h:320
ObjectGuid()
Definition: ObjectGuid.h:210
bool IsGuild() const
Definition: ObjectGuid.h:259
bool IsParty() const
Definition: ObjectGuid.h:258
bool IsEmpty() const
Definition: ObjectGuid.h:242
Definition: ObjectGuid.h:30
uint64 _high
Definition: ObjectGuid.h:326
Definition: Pet.h:46
Definition: AreaTrigger.h:26
bool IsCreature() const
Definition: ObjectGuid.h:243
bool IsPet() const
Definition: ObjectGuid.h:244
LowType GetCounter() const
Definition: ObjectGuid.h:221
bool IsCorpse() const
Definition: ObjectGuid.h:254
Definition: Spell.h:294
Definition: ObjectGuid.h:52