OregonCore  revision fb2a440-git
Your Favourite TBC server
ObjectGuid.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_OBJECT_GUID_H
19 #define OREGON_OBJECT_GUID_H
20 
21 #include "Common.h"
22 #include "ByteBuffer.h"
23 
24 enum TypeID
25 {
34 };
35 
36 #define MAX_TYPE_ID 8
37 
39 {
40  TYPEMASK_OBJECT = 0x0001,
41  TYPEMASK_ITEM = 0x0002,
42  TYPEMASK_CONTAINER = 0x0006, // TYPEMASK_ITEM | 0x0004
43  TYPEMASK_UNIT = 0x0008, // players also have it
47  TYPEMASK_CORPSE = 0x0080,
49 };
50 
52 {
53  HIGHGUID_ITEM = 0x4000, // blizz 4000
54  HIGHGUID_CONTAINER = 0x4000, // blizz 4000
55  HIGHGUID_PLAYER = 0x0000, // blizz 0000
56  HIGHGUID_GAMEOBJECT = 0xF110, // blizz F110
57  HIGHGUID_TRANSPORT = 0xF120, // blizz F120 (for GAMEOBJECT_TYPE_TRANSPORT)
58  HIGHGUID_UNIT = 0xF130, // blizz F130
59  HIGHGUID_PET = 0xF140, // blizz F140
60  HIGHGUID_DYNAMICOBJECT = 0xF100, // blizz F100
61  HIGHGUID_CORPSE = 0xF101, // blizz F100
62  HIGHGUID_MO_TRANSPORT = 0x1FC0, // blizz 1FC0 (for GAMEOBJECT_TYPE_MO_TRANSPORT)
63 };
64 
65 //*** Must be replaced by ObjectGuid use ***
66 #define IS_CREATURE_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_UNIT)
67 #define IS_PET_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_PET)
68 #define IS_CREATURE_OR_PET_GUID(Guid)(IS_CREATURE_GUID(Guid) || IS_PET_GUID(Guid))
69 #define IS_PLAYER_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_PLAYER && Guid != 0)
70 #define IS_UNIT_GUID(Guid) (IS_CREATURE_OR_PET_GUID(Guid) || IS_PLAYER_GUID(Guid))
71 // special case for empty guid need check
72 #define IS_ITEM_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_ITEM)
73 #define IS_GAMEOBJECT_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_GAMEOBJECT)
74 #define IS_CORPSE_GUID(Guid) (GUID_HIPART(Guid) == HIGHGUID_CORPSE)
75 #define IS_MO_TRANSPORT(Guid) (GUID_HIPART(Guid) == HIGHGUID_MO_TRANSPORT)
76 
77 // l - OBJECT_FIELD_GUID
78 // e - OBJECT_FIELD_ENTRY for GO (except GAMEOBJECT_TYPE_MO_TRANSPORT) and creatures or UNIT_FIELD_PETNUMBER for pets
79 // h - OBJECT_FIELD_GUID + 1
80 #define MAKE_NEW_GUID(l, e, h) uint64(uint64(l) | (uint64(e) << 24) | (uint64(h) << 48))
81 
82 #define GUID_HIPART(x) (uint32)((uint64(x) >> 48) & 0x0000FFFF)
83 
84 // We have different low and middle part size for different guid types
85 #define _GUID_ENPART_2(x) 0
86 #define _GUID_ENPART_3(x) (uint32)((uint64(x) >> 24) & UI64LIT(0x0000000000FFFFFF))
87 #define _GUID_LOPART_2(x) (uint32)(uint64(x) & UI64LIT(0x00000000FFFFFFFF))
88 #define _GUID_LOPART_3(x) (uint32)(uint64(x) & UI64LIT(0x0000000000FFFFFF))
89 
90 inline bool IsGuidHaveEnPart(uint64 const& guid)
91 {
92  switch (GUID_HIPART(guid))
93  {
94  case HIGHGUID_ITEM:
95  case HIGHGUID_PLAYER:
97  case HIGHGUID_CORPSE:
99  return false;
100  case HIGHGUID_GAMEOBJECT:
101  case HIGHGUID_TRANSPORT:
102  case HIGHGUID_UNIT:
103  case HIGHGUID_PET:
104  default:
105  return true;
106  }
107 }
108 
109 #define GUID_ENPART(x) (IsGuidHaveEnPart(x) ? _GUID_ENPART_3(x) : _GUID_ENPART_2(x))
110 #define GUID_LOPART(x) (IsGuidHaveEnPart(x) ? _GUID_LOPART_3(x) : _GUID_LOPART_2(x))
111 
112 //*** Must be replaced by ObjectGuid use END ***
113 class ObjectGuid;
114 class PackedGuid;
115 
117 {
118  explicit PackedGuidReader(ObjectGuid& guid) : m_guidPtr(&guid) {}
120 };
121 
123 {
124  public: // constructors
125  ObjectGuid() : m_guid(0) {}
126  ObjectGuid(uint64 const& guid) : m_guid(guid) {} // NOTE: must be explicit in future for more strict control type conversions
127  ObjectGuid(HighGuid hi, uint32 entry, uint32 counter) : m_guid(uint64(counter) | (uint64(entry) << 24) | (uint64(hi) << 48)) {}
128 
129  public: // modifiers
130  operator uint64() const { return m_guid; }
131 
133  {
134  return PackedGuidReader(*this);
135  }
136 
137  void Set(uint64 const& guid)
138  {
139  m_guid = guid;
140  }
141  void Clear()
142  {
143  m_guid = 0;
144  }
145 
146  // Possible removed in future for more strict control type conversions
147  void operator= (uint64 const& guid)
148  {
149  m_guid = guid;
150  }
151 
152  PackedGuid WriteAsPacked() const;
153  public: // accessors
154  uint64 const& GetRawValue() const
155  {
156  return m_guid;
157  }
159  {
160  return HighGuid((m_guid >> 48) & 0x0000FFFF);
161  }
162  uint32 GetEntry() const
163  {
164  return HasEntry() ? uint32((m_guid >> 24) & UI64LIT(0x0000000000FFFFFF)) : 0;
165  }
167  {
168  return HasEntry()
169  ? uint32(m_guid & UI64LIT(0x0000000000FFFFFF))
170  : uint32(m_guid & UI64LIT(0x00000000FFFFFFFF));
171  }
172 
173  bool IsEmpty() const
174  {
175  return m_guid == 0;
176  }
177  bool IsCreature() const
178  {
179  return GetHigh() == HIGHGUID_UNIT;
180  }
181  bool IsPet() const
182  {
183  return GetHigh() == HIGHGUID_PET;
184  }
185  bool IsCreatureOrPet() const
186  {
187  return IsCreature() || IsPet();
188  }
189  bool IsPlayer() const
190  {
191  return !IsEmpty() && GetHigh() == HIGHGUID_PLAYER;
192  }
193  bool IsUnit() const
194  {
195  return IsCreatureOrPet() || IsPlayer();
196  }
197  bool IsItem() const
198  {
199  return GetHigh() == HIGHGUID_ITEM;
200  }
201  bool IsGameobject() const
202  {
203  return GetHigh() == HIGHGUID_GAMEOBJECT;
204  }
205  bool IsDynamicObject() const
206  {
207  return GetHigh() == HIGHGUID_DYNAMICOBJECT;
208  }
209  bool IsCorpse() const
210  {
211  return GetHigh() == HIGHGUID_CORPSE;
212  }
213  bool IsTransport() const
214  {
215  return GetHigh() == HIGHGUID_TRANSPORT;
216  }
217  bool IsMOTransport() const
218  {
219  return GetHigh() == HIGHGUID_MO_TRANSPORT;
220  }
221 
223  {
224  switch (GetHigh())
225  {
226  case HIGHGUID_ITEM:
227  return TYPEID_ITEM;
228  //case HIGHGUID_CONTAINER: return TYPEID_CONTAINER; HIGHGUID_CONTAINER==HIGHGUID_ITEM currently
229  case HIGHGUID_UNIT:
230  return TYPEID_UNIT;
231  case HIGHGUID_PET:
232  return TYPEID_UNIT;
233  case HIGHGUID_PLAYER:
234  return TYPEID_PLAYER;
235  case HIGHGUID_GAMEOBJECT:
236  return TYPEID_GAMEOBJECT;
238  return TYPEID_DYNAMICOBJECT;
239  case HIGHGUID_CORPSE:
240  return TYPEID_CORPSE;
242  return TYPEID_GAMEOBJECT;
243  // unknown
244  default:
245  return TYPEID_OBJECT;
246  }
247  }
248  bool operator!() const { return IsEmpty(); }
249  bool operator== (ObjectGuid const& guid) const { return GetRawValue() == guid.GetRawValue(); }
250  bool operator!= (ObjectGuid const& guid) const { return GetRawValue() != guid.GetRawValue(); }
251  bool operator< (ObjectGuid const& guid) const { return GetRawValue() < guid.GetRawValue(); }
252 
253  public: // accessors - for debug
254  char const* GetTypeName() const;
255  std::string GetString() const;
256  private: // internal functions
257  bool HasEntry() const
258  {
259  switch (GetHigh())
260  {
261  case HIGHGUID_ITEM:
262  case HIGHGUID_PLAYER:
264  case HIGHGUID_CORPSE:
266  return false;
267  case HIGHGUID_GAMEOBJECT:
268  case HIGHGUID_TRANSPORT:
269  case HIGHGUID_UNIT:
270  case HIGHGUID_PET:
271  default:
272  return true;
273  }
274  }
275 
276  private: // fields
278 };
279 
280 // Some Shared defines
281 typedef std::list<ObjectGuid> GuidList;
282 
284 {
285  friend ByteBuffer& operator<< (ByteBuffer& buf, PackedGuid const& guid);
286 
287  public: // constructors
288  explicit PackedGuid()
289  {
290  m_packedGuid.appendPackGUID(0);
291  }
292  explicit PackedGuid(uint64 const& guid)
293  {
294  m_packedGuid.appendPackGUID(guid);
295  }
296  explicit PackedGuid(ObjectGuid const& guid)
297  {
298  m_packedGuid.appendPackGUID(guid.GetRawValue());
299  }
300 
301  public: // modifiers
302  void Set(uint64 const& guid)
303  {
304  m_packedGuid.wpos(0);
305  m_packedGuid.appendPackGUID(guid);
306  }
307  void Set(ObjectGuid const& guid)
308  {
309  m_packedGuid.wpos(0);
310  m_packedGuid.appendPackGUID(guid.GetRawValue());
311  }
312 
313  public: // accessors
314  size_t size() const
315  {
316  return m_packedGuid.size();
317  }
318 
319  private: // fields
321 };
322 
323 ByteBuffer& operator<< (ByteBuffer& buf, ObjectGuid const& guid);
325 
326 ByteBuffer& operator<< (ByteBuffer& buf, PackedGuid const& guid);
328 
330 {
331  return PackedGuid(*this);
332 }
333 
334 #endif
335 
bool IsGameobject() const
Definition: ObjectGuid.h:201
uint32 GetCounter() const
Definition: ObjectGuid.h:166
PackedGuid(uint64 const &guid)
Definition: ObjectGuid.h:292
bool IsUnit() const
Definition: ObjectGuid.h:193
TypeID GetTypeId()
Definition: ObjectGuid.h:222
bool IsPlayer() const
Definition: ObjectGuid.h:189
ObjectGuid * m_guidPtr
Definition: ObjectGuid.h:119
bool IsTransport() const
Definition: ObjectGuid.h:213
ObjectGuid(uint64 const &guid)
Definition: ObjectGuid.h:126
PackedGuidReader ReadAsPacked()
Definition: ObjectGuid.h:132
ObjectGuid(HighGuid hi, uint32 entry, uint32 counter)
Definition: ObjectGuid.h:127
bool IsGuidHaveEnPart(uint64 const &guid)
Definition: ObjectGuid.h:90
#define GUID_HIPART(x)
Definition: ObjectGuid.h:82
std::list< ObjectGuid > GuidList
Definition: ObjectGuid.h:281
bool IsMOTransport() const
Definition: ObjectGuid.h:217
bool IsItem() const
Definition: ObjectGuid.h:197
bool IsDynamicObject() const
Definition: ObjectGuid.h:205
uint64 const & GetRawValue() const
Definition: ObjectGuid.h:154
void Set(uint64 const &guid)
Definition: ObjectGuid.h:137
HighGuid
Definition: ObjectGuid.h:51
#define UI64LIT(N)
Definition: Common.h:150
PackedGuid(ObjectGuid const &guid)
Definition: ObjectGuid.h:296
void Set(ObjectGuid const &guid)
Definition: ObjectGuid.h:307
uint64 m_guid
Definition: ObjectGuid.h:277
bool operator!() const
Definition: ObjectGuid.h:248
size_t size() const
Definition: ObjectGuid.h:314
TypeMask
Definition: ObjectGuid.h:38
uint32 GetEntry() const
Definition: ObjectGuid.h:162
TypeID
Definition: ObjectGuid.h:24
void Clear()
Definition: ObjectGuid.h:141
ACE_UINT64 uint64
Definition: Define.h:70
ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
Definition: ObjectGuid.cpp:58
void Set(uint64 const &guid)
Definition: ObjectGuid.h:302
bool HasEntry() const
Definition: ObjectGuid.h:257
PackedGuid WriteAsPacked() const
Definition: ObjectGuid.h:329
ByteBuffer m_packedGuid
Definition: ObjectGuid.h:320
HighGuid GetHigh() const
Definition: ObjectGuid.h:158
bool IsCreatureOrPet() const
Definition: ObjectGuid.h:185
ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
Definition: ObjectGuid.cpp:64
ACE_UINT32 uint32
Definition: Define.h:71
bool IsEmpty() const
Definition: ObjectGuid.h:173
bool IsCreature() const
Definition: ObjectGuid.h:177
bool IsPet() const
Definition: ObjectGuid.h:181
PackedGuidReader(ObjectGuid &guid)
Definition: ObjectGuid.h:118
bool IsCorpse() const
Definition: ObjectGuid.h:209