TrinityCore
Loading...
Searching...
No Matches
ObjectGuid.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 TRINITYCORE_OBJECT_GUID_H
19#define TRINITYCORE_OBJECT_GUID_H
20
21#include "Define.h"
22#include "EnumFlag.h"
23#include "StringFormatFwd.h"
24#include <array>
25#include <functional>
26#include <list>
27#include <set>
28#include <string>
29#include <type_traits>
30#include <unordered_set>
31#include <vector>
32
44
45#define NUM_CLIENT_OBJECT_TYPES 8
46
61
62enum class HighGuid
63{
64 Item = 0x4000, // blizz 4000
65 Container = 0x4000, // blizz 4000
66 Player = 0x0000, // blizz 0000
67 GameObject = 0xF110, // blizz F110
68 Transport = 0xF120, // blizz F120 (for GAMEOBJECT_TYPE_TRANSPORT)
69 Unit = 0xF130, // blizz F130
70 Pet = 0xF140, // blizz F140
71 Vehicle = 0xF150, // blizz F550
72 DynamicObject = 0xF100, // blizz F100
73 Corpse = 0xF101, // blizz F100
74 Mo_Transport = 0x1FC0, // blizz 1FC0 (for GAMEOBJECT_TYPE_MAP_OBJ_TRANSPORT)
75 Instance = 0x1F40, // blizz 1F40
76 Group = 0x1F50,
77};
78
80{
81 None = 0x0,
82 Global = 0x1,
83 Map = 0x2
84};
85
87
93
94template<HighGuid high>
96{
98 using Format = std::integral_constant<ObjectGuidFormatType, ObjectGuidFormatType::OnlyCounter>;
99};
100
101#define MAKE_GUID_TRAIT(high, sequence, format) \
102 template<> struct ObjectGuidTraits<high> \
103 { \
104 static constexpr EnumFlag<ObjectGuidSequenceSource> SequenceSource = sequence; \
105 using Format = std::integral_constant<ObjectGuidFormatType, format>; \
106 }
107
120
121class ByteBuffer;
122class ObjectGuid;
123class PackedGuid;
124
126{
127 explicit PackedGuidReader(ObjectGuid& guid) : Guid(guid) { }
129};
130
132{
133 explicit PackedGuidWriter(ObjectGuid const& guid) : Guid(guid) { }
135};
136
138{
139 public:
140 static ObjectGuid const Empty;
141
143
144 constexpr ObjectGuid() = default;
145
147
148 uint64 GetRawValue() const { return _guid; }
149 void SetRawValue(uint64 guid) { _guid = guid; }
150 void Clear() { _guid = 0; }
151
153
154 HighGuid GetHigh() const { return HighGuid((_guid >> 48) & 0x0000FFFF); }
155 uint32 GetEntry() const { return HasEntry() ? uint32((_guid >> 24) & UI64LIT(0x0000000000FFFFFF)) : 0; }
157 {
158 return HasEntry()
159 ? LowType(_guid & UI64LIT(0x0000000000FFFFFF))
160 : LowType(_guid & UI64LIT(0x00000000FFFFFFFF));
161 }
162
164 {
165 return HasEntry(high)
166 ? LowType(0x00FFFFFF)
167 : LowType(0xFFFFFFFF);
168 }
169
170 LowType GetMaxCounter() const { return GetMaxCounter(GetHigh()); }
171
172 bool IsEmpty() const { return _guid == 0; }
173 bool IsCreature() const { return GetHigh() == HighGuid::Unit; }
174 bool IsPet() const { return GetHigh() == HighGuid::Pet; }
175 bool IsVehicle() const { return GetHigh() == HighGuid::Vehicle; }
176 bool IsCreatureOrPet() const { return IsCreature() || IsPet(); }
177 bool IsCreatureOrVehicle() const { return IsCreature() || IsVehicle(); }
178 bool IsAnyTypeCreature() const { return IsCreature() || IsPet() || IsVehicle(); }
179 bool IsPlayer() const { return !IsEmpty() && GetHigh() == HighGuid::Player; }
180 bool IsUnit() const { return IsAnyTypeCreature() || IsPlayer(); }
181 bool IsItem() const { return GetHigh() == HighGuid::Item; }
182 bool IsGameObject() const { return GetHigh() == HighGuid::GameObject; }
183 bool IsDynamicObject() const { return GetHigh() == HighGuid::DynamicObject; }
184 bool IsCorpse() const { return GetHigh() == HighGuid::Corpse; }
185 bool IsTransport() const { return GetHigh() == HighGuid::Transport; }
186 bool IsMOTransport() const { return GetHigh() == HighGuid::Mo_Transport; }
187 bool IsAnyTypeGameObject() const { return IsGameObject() || IsTransport() || IsMOTransport(); }
188 bool IsInstance() const { return GetHigh() == HighGuid::Instance; }
189 bool IsGroup() const { return GetHigh() == HighGuid::Group; }
190
192 {
193 switch (high)
194 {
195 case HighGuid::Item: return TYPEID_ITEM;
196 //case HighGuid::Container: return TYPEID_CONTAINER; HighGuid::Container == HighGuid::Item currently
197 case HighGuid::Unit: return TYPEID_UNIT;
198 case HighGuid::Pet: return TYPEID_UNIT;
199 case HighGuid::Player: return TYPEID_PLAYER;
202 case HighGuid::Corpse: return TYPEID_CORPSE;
204 case HighGuid::Vehicle: return TYPEID_UNIT;
205 // unknown
207 case HighGuid::Group:
208 default: return TYPEID_OBJECT;
209 }
210 }
211
212 TypeID GetTypeId() const { return GetTypeId(GetHigh()); }
213
214 bool operator!() const { return IsEmpty(); }
215 bool operator==(ObjectGuid const& right) const = default;
216 std::strong_ordering operator<=>(ObjectGuid const& right) const = default;
217
218 static std::string_view GetTypeName(HighGuid high);
219 std::string_view GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; }
220 std::string ToString() const;
221 std::string ToHexString() const;
222
223 template<HighGuid type, std::enable_if_t<ObjectGuidTraits<type>::Format::value == ObjectGuidFormatType::OnlyCounter, int32> = 0>
224 static constexpr ObjectGuid Create(LowType counter) { return ObjectGuid(counter ? uint64(counter) | (uint64(type) << 48) : 0); }
225
226 template<HighGuid type, std::enable_if_t<ObjectGuidTraits<type>::Format::value == ObjectGuidFormatType::CounterAndEntry, int32> = 0>
227 static constexpr ObjectGuid Create(uint32 entry, LowType counter) { return ObjectGuid(counter ? uint64(counter) | (uint64(entry) << 24) | (uint64(type) << 48) : 0); }
228
229 private:
230 static constexpr bool HasEntry(HighGuid high)
231 {
232 switch (high)
233 {
234 case HighGuid::Item:
235 case HighGuid::Player:
237 case HighGuid::Corpse:
240 case HighGuid::Group:
241 return false;
244 case HighGuid::Unit:
245 case HighGuid::Pet:
247 default:
248 return true;
249 }
250 }
251
252 bool HasEntry() const { return HasEntry(GetHigh()); }
253
254 explicit ObjectGuid(uint64 guid) : _guid(guid) { }
255
256 uint64 _guid = 0;
257};
258
259// Some Shared defines
260using GuidSet = std::set<ObjectGuid>;
261using GuidList = std::list<ObjectGuid>;
262using GuidVector = std::vector<ObjectGuid>;
263using GuidUnorderedSet = std::unordered_set<ObjectGuid>;
264
265// maximum buffer size for packed guid is 9 bytes
266#define PACKED_GUID_MIN_BUFFER_SIZE 9
267
269{
270 friend TC_GAME_API ByteBuffer& operator<<(ByteBuffer& buf, PackedGuid const& guid);
271
272 public:
273 explicit PackedGuid() : _packedSize(1), _packedGuid() { }
274 explicit PackedGuid(ObjectGuid guid) { Set(guid); }
275
276 void Set(ObjectGuid const& guid);
277
278 std::size_t size() const { return _packedSize; }
279
280 private:
282 std::array<uint8, PACKED_GUID_MIN_BUFFER_SIZE> _packedGuid;
283};
284
286{
287public:
288 explicit ObjectGuidGenerator(HighGuid high, ObjectGuid::LowType start = 1) : _high(high), _nextGuid(start) { }
290
291 void Set(ObjectGuid::LowType val) { _nextGuid = val; }
292 ObjectGuid::LowType Generate();
293 ObjectGuid::LowType GetNextAfterMaxUsed() const { return _nextGuid; }
294
295protected:
296 void HandleCounterOverflow();
297 void CheckGuidTrigger();
300};
301
304
308
309template<>
310struct std::hash<ObjectGuid>
311{
312 size_t operator()(ObjectGuid const& key) const noexcept
313 {
314 return std::hash<uint64>()(key.GetRawValue());
315 }
316};
317
318template <>
320{
321 template <typename FormatContext>
322 auto format(ObjectGuid const& guid, FormatContext& ctx) const -> decltype(ctx.out());
323};
324
325#endif // TRINITYCORE_OBJECT_GUID_H
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
int32_t int32
Definition Define.h:129
uint64_t uint64
Definition Define.h:132
#define UI64LIT(N)
Definition Define.h:118
uint32_t uint32
Definition Define.h:133
#define DEFINE_ENUM_FLAG(enumType)
Definition EnumFlag.h:26
ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
std::list< ObjectGuid > GuidList
Definition ObjectGuid.h:261
std::unordered_set< ObjectGuid > GuidUnorderedSet
Definition ObjectGuid.h:263
TypeID
Definition ObjectGuid.h:34
@ TYPEID_OBJECT
Definition ObjectGuid.h:35
@ TYPEID_DYNAMICOBJECT
Definition ObjectGuid.h:41
@ TYPEID_GAMEOBJECT
Definition ObjectGuid.h:40
@ TYPEID_UNIT
Definition ObjectGuid.h:38
@ TYPEID_CORPSE
Definition ObjectGuid.h:42
@ TYPEID_ITEM
Definition ObjectGuid.h:36
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
@ TYPEID_CONTAINER
Definition ObjectGuid.h:37
TC_GAME_API ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
std::set< ObjectGuid > GuidSet
Definition ObjectGuid.h:260
#define MAKE_GUID_TRAIT(high, sequence, format)
Definition ObjectGuid.h:101
HighGuid
Definition ObjectGuid.h:63
TypeMask
Definition ObjectGuid.h:48
@ TYPEMASK_OBJECT
Definition ObjectGuid.h:49
@ TYPEMASK_ITEM
Definition ObjectGuid.h:50
@ TYPEMASK_DYNAMICOBJECT
Definition ObjectGuid.h:55
@ TYPEMASK_SEER
Definition ObjectGuid.h:58
@ TYPEMASK_UNIT
Definition ObjectGuid.h:52
@ TYPEMASK_CONTAINER
Definition ObjectGuid.h:51
@ TYPEMASK_CORPSE
Definition ObjectGuid.h:56
@ TYPEMASK_GAMEOBJECT
Definition ObjectGuid.h:54
@ TYPEMASK_WORLDOBJECT
Definition ObjectGuid.h:59
@ TYPEMASK_PLAYER
Definition ObjectGuid.h:53
ObjectGuidSequenceSource
Definition ObjectGuid.h:80
ObjectGuidFormatType
Definition ObjectGuid.h:89
TC_GAME_API ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
std::vector< ObjectGuid > GuidVector
Definition ObjectGuid.h:262
Definition Group.h:165
Definition Item.h:62
Definition Map.h:281
ObjectGuid::LowType _nextGuid
Definition ObjectGuid.h:299
ObjectGuid::LowType GetNextAfterMaxUsed() const
Definition ObjectGuid.h:293
~ObjectGuidGenerator()=default
void Set(ObjectGuid::LowType val)
Definition ObjectGuid.h:291
ObjectGuidGenerator(HighGuid high, ObjectGuid::LowType start=1)
Definition ObjectGuid.h:288
static constexpr ObjectGuid Create(uint32 entry, LowType counter)
Definition ObjectGuid.h:227
bool IsTransport() const
Definition ObjectGuid.h:185
static LowType GetMaxCounter(HighGuid high)
Definition ObjectGuid.h:163
LowType GetCounter() const
Definition ObjectGuid.h:156
ObjectGuid(uint64 guid)
Definition ObjectGuid.h:254
static ObjectGuid const Empty
Definition ObjectGuid.h:140
static constexpr bool HasEntry(HighGuid high)
Definition ObjectGuid.h:230
bool IsAnyTypeGameObject() const
Definition ObjectGuid.h:187
bool IsCorpse() const
Definition ObjectGuid.h:184
bool IsEmpty() const
Definition ObjectGuid.h:172
bool operator==(ObjectGuid const &right) const =default
void SetRawValue(uint64 guid)
Definition ObjectGuid.h:149
static constexpr ObjectGuid Create(LowType counter)
Definition ObjectGuid.h:224
bool IsMOTransport() const
Definition ObjectGuid.h:186
bool IsItem() const
Definition ObjectGuid.h:181
uint64 GetRawValue() const
Definition ObjectGuid.h:148
bool HasEntry() const
Definition ObjectGuid.h:252
bool IsPlayer() const
Definition ObjectGuid.h:179
PackedGuidWriter WriteAsPacked() const
Definition ObjectGuid.h:152
bool IsUnit() const
Definition ObjectGuid.h:180
bool IsVehicle() const
Definition ObjectGuid.h:175
std::string_view GetTypeName() const
Definition ObjectGuid.h:219
bool IsDynamicObject() const
Definition ObjectGuid.h:183
bool IsInstance() const
Definition ObjectGuid.h:188
LowType GetMaxCounter() const
Definition ObjectGuid.h:170
bool IsGameObject() const
Definition ObjectGuid.h:182
uint32 LowType
Definition ObjectGuid.h:142
bool IsCreatureOrPet() const
Definition ObjectGuid.h:176
bool operator!() const
Definition ObjectGuid.h:214
uint32 GetEntry() const
Definition ObjectGuid.h:155
static TypeID GetTypeId(HighGuid high)
Definition ObjectGuid.h:191
bool IsAnyTypeCreature() const
Definition ObjectGuid.h:178
bool IsCreatureOrVehicle() const
Definition ObjectGuid.h:177
bool IsGroup() const
Definition ObjectGuid.h:189
TypeID GetTypeId() const
Definition ObjectGuid.h:212
HighGuid GetHigh() const
Definition ObjectGuid.h:154
bool IsCreature() const
Definition ObjectGuid.h:173
PackedGuidReader ReadAsPacked()
Definition ObjectGuid.h:146
bool IsPet() const
Definition ObjectGuid.h:174
void Clear()
Definition ObjectGuid.h:150
std::strong_ordering operator<=>(ObjectGuid const &right) const =default
constexpr ObjectGuid()=default
std::size_t size() const
Definition ObjectGuid.h:278
std::array< uint8, PACKED_GUID_MIN_BUFFER_SIZE > _packedGuid
Definition ObjectGuid.h:282
PackedGuid(ObjectGuid guid)
Definition ObjectGuid.h:274
uint8 _packedSize
Definition ObjectGuid.h:281
Definition Pet.h:40
Definition Unit.h:769
std::integral_constant< ObjectGuidFormatType, ObjectGuidFormatType::OnlyCounter > Format
Definition ObjectGuid.h:98
static constexpr EnumFlag< ObjectGuidSequenceSource > SequenceSource
Definition ObjectGuid.h:97
ObjectGuid & Guid
Definition ObjectGuid.h:128
PackedGuidReader(ObjectGuid &guid)
Definition ObjectGuid.h:127
ObjectGuid const & Guid
Definition ObjectGuid.h:134
PackedGuidWriter(ObjectGuid const &guid)
Definition ObjectGuid.h:133
auto format(ObjectGuid const &guid, FormatContext &ctx) const -> decltype(ctx.out())
size_t operator()(ObjectGuid const &key) const noexcept
Definition ObjectGuid.h:312