TrinityCore
Loading...
Searching...
No Matches
RaceMask.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_RACE_MASK_H
19#define TRINITYCORE_RACE_MASK_H
20
21#include "Define.h"
22#include <array>
23#include <type_traits>
24#include <cstring>
25
26// EnumUtils: DESCRIBE THIS
28{
29 RACE_NONE = 0, // SKIP
30 RACE_HUMAN = 1, // TITLE Human
31 RACE_ORC = 2, // TITLE Orc
32 RACE_DWARF = 3, // TITLE Dwarf
33 RACE_NIGHTELF = 4, // TITLE Night Elf
34 RACE_UNDEAD_PLAYER = 5, // TITLE Undead
35 RACE_TAUREN = 6, // TITLE Tauren
36 RACE_GNOME = 7, // TITLE Gnome
37 RACE_TROLL = 8, // TITLE Troll
38 RACE_GOBLIN = 9, // TITLE Goblin
39 RACE_BLOODELF = 10, // TITLE Blood Elf
40 RACE_DRAENEI = 11, // TITLE Draenei
41 //RACE_FEL_ORC = 12,
42 //RACE_NAGA = 13,
43 //RACE_BROKEN = 14,
44 //RACE_SKELETON = 15,
45 //RACE_VRYKUL = 16,
46 //RACE_TUSKARR = 17,
47 //RACE_FOREST_TROLL = 18,
48 //RACE_TAUNKA = 19,
49 //RACE_NORTHREND_SKELETON = 20,
50 //RACE_ICE_TROLL = 21,
51 RACE_WORGEN = 22, // TITLE Worgen
52 //RACE_GILNEAN = 23,
53 RACE_PANDAREN_NEUTRAL = 24, // TITLE Pandaren DESCRIPTION Pandaren (Neutral)
54 RACE_PANDAREN_ALLIANCE = 25, // TITLE Pandaren DESCRIPTION Pandaren (Alliance)
55 RACE_PANDAREN_HORDE = 26, // TITLE Pandaren DESCRIPTION Pandaren (Horde)
56 RACE_NIGHTBORNE = 27, // TITLE Nightborne
57 RACE_HIGHMOUNTAIN_TAUREN = 28, // TITLE Highmountain Tauren
58 RACE_VOID_ELF = 29, // TITLE Void Elf
59 RACE_LIGHTFORGED_DRAENEI = 30, // TITLE Lightforged Draenei
60 RACE_ZANDALARI_TROLL = 31, // TITLE Zandalari Troll
61 RACE_KUL_TIRAN = 32, // TITLE Kul Tiran
62 //RACE_THIN_HUMAN = 33,
63 RACE_DARK_IRON_DWARF = 34, // TITLE Dark Iron Dwarf DESCRIPTION Dark Iron Dwarf (RaceMask bit 11)
64 RACE_VULPERA = 35, // TITLE Vulpera DESCRIPTION Vulpera (RaceMask bit 12)
65 RACE_MAGHAR_ORC = 36, // TITLE Mag'har Orc DESCRIPTION Mag'har Orc (RaceMask bit 13)
66 RACE_MECHAGNOME = 37, // TITLE Mechagnome DESCRIPTION Mechagnome (RaceMask bit 14)
67 RACE_DRACTHYR_ALLIANCE = 52, // TITLE Dracthyr DESCRIPTION Dracthyr (Alliance) (RaceMask bit 16)
68 RACE_DRACTHYR_HORDE = 70, // TITLE Dracthyr DESCRIPTION Dracthyr (Horde) (RaceMask bit 15)
69 //RACE_COMPANION_DRAKE = 71,
70 //RACE_COMPANION_PROTO_DRAGON = 72,
71 //RACE_COMPANION_SERPENT = 73,
72 //RACE_COMPANION_WYVERN = 74,
73 //RACE_DRACTHYR_VISAGE_ALLIANCE = 75,
74 //RACE_DRACTHYR_VISAGE_HORDE = 76,
75 //RACE_COMPANION_PTERRODAX = 77,
76 //RACE_COMPANION_NETHERWING_DRAKE = 80,
77 //RACE_ROSTRUM_STORM_GRYPHON = 82,
78 //RACE_ROSTRUM_FAERIE_DRAGON = 83,
79 RACE_EARTHEN_DWARF_HORDE = 84, // TITLE Earthen DESCRIPTION Earthen (Horde) (RaceMask bit 17)
80 RACE_EARTHEN_DWARF_ALLIANCE = 85, // TITLE Earthen DESCRIPTION Earthen (Alliance) (RaceMask bit 18)
81 RACE_HARANIR_ALLIANCE = 86, // TITLE Haranir DESCRIPTION Haranir (Alliance) (RaceMask bit 20)
82 //RACE_ROSTRUM_AIRSHIP_SEASON_1 = 87,
83 //RACE_ROSTRUM_AIRSHIP_SEASON_2 = 90,
84 RACE_HARANIR_HORDE = 91, // TITLE Haranir DESCRIPTION Haranir (Horde) (RaceMask bit 19)
85 //RACE_ROSTRUM_AIRSHIP_SEASON_3 = 92,
86};
87
88namespace Trinity
89{
90template<typename T, size_t N = 1>
92{
93 static_assert(std::is_integral_v<T>, "RaceMask<T> must be integral");
94
95 std::array<T, N> RawValue;
96
97 static constexpr int32 GetRaceBit(uint32 raceId)
98 {
99 switch (raceId)
100 {
101 case RACE_HUMAN:
102 case RACE_ORC:
103 case RACE_DWARF:
104 case RACE_NIGHTELF:
106 case RACE_TAUREN:
107 case RACE_GNOME:
108 case RACE_TROLL:
109 case RACE_GOBLIN:
110 case RACE_BLOODELF:
111 case RACE_DRAENEI:
112 case RACE_WORGEN:
116 case RACE_NIGHTBORNE:
118 case RACE_VOID_ELF:
121 case RACE_KUL_TIRAN:
122 return raceId - 1;
124 return 11;
125 case RACE_VULPERA:
126 return 12;
127 case RACE_MAGHAR_ORC:
128 return 13;
129 case RACE_MECHAGNOME:
130 return 14;
132 return 16;
134 return 15;
136 return 17;
138 return 18;
140 return 19;
142 return 20;
143 default:
144 break;
145 }
146 return -1;
147 }
148
149 constexpr bool HasRace(uint32 raceId) const
150 {
151 int32 raceBit = GetRaceBit(raceId);
152 return raceBit >= 0 && uint32(raceBit) < sizeof(T) * 8 * N
153 && (RawValue[raceBit / (sizeof(T) * 8)] & (T(1) << (raceBit % (sizeof(T) * 8)))) != T(0);
154 }
155
156 static constexpr RaceMask GetMaskForRace(uint32 raceId)
157 {
158 RaceMask result = { };
159 int32 raceBit = GetRaceBit(raceId);
160 if (raceBit >= 0 && uint32(raceBit) < sizeof(T) * 8 * N)
161 result.RawValue[raceBit / (sizeof(T) * 8)] = T(1) << (raceBit % (sizeof(T) * 8));
162 return result;
163 }
164
165 constexpr bool operator==(RaceMask const&) const = default;
166
167 constexpr bool IsEmpty() const
168 {
169 for (T rawValue : RawValue)
170 if (rawValue != T(0))
171 return false;
172 return true;
173 }
174
175 constexpr RaceMask operator&(RaceMask right) const
176 {
177 RaceMask result = { };
178 for (std::size_t i = 0; i < N; ++i)
179 result.RawValue[i] = RawValue[i] & right.RawValue[i];
180 return result;
181 }
182
183 constexpr RaceMask operator|(RaceMask right) const
184 {
185 RaceMask result = { };
186 for (std::size_t i = 0; i < N; ++i)
187 result.RawValue[i] = RawValue[i] | right.RawValue[i];
188 return result;
189 }
190
191 constexpr RaceMask operator~() const
192 {
193 RaceMask result = { };
194 for (std::size_t i = 0; i < N; ++i)
195 result.RawValue[i] = ~RawValue[i];
196 return result;
197 }
198
199 template<typename OtherT, size_t OtherN>
200 constexpr explicit operator RaceMask<OtherT, OtherN>() const
201 {
202 static_assert(sizeof(OtherT) * OtherN >= sizeof(T) * N, "RaceMask truncating conversion not allowed");
203
204 RaceMask<OtherT, OtherN> result = { };
205
206 if (std::is_constant_evaluated())
207 {
208 for (std::size_t i = 0; i < N; ++i)
209 {
210 for (std::size_t j = 0; j < sizeof(T); ++j)
211 {
212 uint8 byte = (RawValue[i] >> (j * 8)) & 0xFF;
213
214 std::size_t byteIndex = j + i * sizeof(T);
215
216 std::size_t resultI = byteIndex / sizeof(OtherT);
217 std::size_t resultJ = byteIndex % sizeof(OtherT);
218
219 result.RawValue[resultI] |= static_cast<OtherT>(byte) << (resultJ * 8);
220 }
221 }
222 }
223 else
224 std::memcpy(result.RawValue.data(), RawValue.data(), sizeof(T) * N);
225
226 return result;
227 }
228};
229}
230
231template <typename T, size_t N = 1>
232inline constexpr Trinity::RaceMask<T, N> RACEMASK_ALL_v = ~Trinity::RaceMask<T, N>{};
233
234template <typename T, size_t N = 1>
267
268template <typename T, size_t N = 1>
270
271template <typename T, size_t N = 1>
288
289template <typename T, size_t N = 1>
290inline constexpr Trinity::RaceMask<T, N> RACEMASK_HORDE_v = RACEMASK_ALL_PLAYABLE_v<T, N> & ~(RACEMASK_NEUTRAL_v<T, N> | RACEMASK_ALLIANCE_v<T, N>);
291
292inline constexpr Trinity::RaceMask<uint64> RACEMASK_ALL = RACEMASK_ALL_v<uint64>;
293inline constexpr Trinity::RaceMask<uint64> RACEMASK_ALL_PLAYABLE = RACEMASK_ALL_PLAYABLE_v<uint64>;
294inline constexpr Trinity::RaceMask<uint64> RACEMASK_NEUTRAL = RACEMASK_NEUTRAL_v<uint64>;
295inline constexpr Trinity::RaceMask<uint64> RACEMASK_ALLIANCE = RACEMASK_ALLIANCE_v<uint64>;
296inline constexpr Trinity::RaceMask<uint64> RACEMASK_HORDE = RACEMASK_HORDE_v<uint64>;
297
298#endif // TRINITYCORE_RACE_MASK_H
uint8_t uint8
Definition Define.h:156
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
constexpr Trinity::RaceMask< uint64 > RACEMASK_ALL_PLAYABLE
Definition RaceMask.h:293
constexpr Trinity::RaceMask< T, N > RACEMASK_ALL_v
Definition RaceMask.h:232
constexpr Trinity::RaceMask< T, N > RACEMASK_HORDE_v
Definition RaceMask.h:290
constexpr Trinity::RaceMask< T, N > RACEMASK_NEUTRAL_v
Definition RaceMask.h:269
constexpr Trinity::RaceMask< uint64 > RACEMASK_ALLIANCE
Definition RaceMask.h:295
constexpr Trinity::RaceMask< T, N > RACEMASK_ALL_PLAYABLE_v
Definition RaceMask.h:235
constexpr Trinity::RaceMask< uint64 > RACEMASK_NEUTRAL
Definition RaceMask.h:294
constexpr Trinity::RaceMask< uint64 > RACEMASK_HORDE
Definition RaceMask.h:296
Races
Definition RaceMask.h:28
@ RACE_DARK_IRON_DWARF
Definition RaceMask.h:63
@ RACE_TROLL
Definition RaceMask.h:37
@ RACE_UNDEAD_PLAYER
Definition RaceMask.h:34
@ RACE_PANDAREN_NEUTRAL
Definition RaceMask.h:53
@ RACE_ORC
Definition RaceMask.h:31
@ RACE_LIGHTFORGED_DRAENEI
Definition RaceMask.h:59
@ RACE_NONE
Definition RaceMask.h:29
@ RACE_DRAENEI
Definition RaceMask.h:40
@ RACE_EARTHEN_DWARF_HORDE
Definition RaceMask.h:79
@ RACE_NIGHTBORNE
Definition RaceMask.h:56
@ RACE_HIGHMOUNTAIN_TAUREN
Definition RaceMask.h:57
@ RACE_HARANIR_HORDE
Definition RaceMask.h:84
@ RACE_EARTHEN_DWARF_ALLIANCE
Definition RaceMask.h:80
@ RACE_DRACTHYR_HORDE
Definition RaceMask.h:68
@ RACE_ZANDALARI_TROLL
Definition RaceMask.h:60
@ RACE_VOID_ELF
Definition RaceMask.h:58
@ RACE_NIGHTELF
Definition RaceMask.h:33
@ RACE_BLOODELF
Definition RaceMask.h:39
@ RACE_DWARF
Definition RaceMask.h:32
@ RACE_GNOME
Definition RaceMask.h:36
@ RACE_GOBLIN
Definition RaceMask.h:38
@ RACE_KUL_TIRAN
Definition RaceMask.h:61
@ RACE_HUMAN
Definition RaceMask.h:30
@ RACE_DRACTHYR_ALLIANCE
Definition RaceMask.h:67
@ RACE_WORGEN
Definition RaceMask.h:51
@ RACE_PANDAREN_ALLIANCE
Definition RaceMask.h:54
@ RACE_VULPERA
Definition RaceMask.h:64
@ RACE_MECHAGNOME
Definition RaceMask.h:66
@ RACE_HARANIR_ALLIANCE
Definition RaceMask.h:81
@ RACE_MAGHAR_ORC
Definition RaceMask.h:65
@ RACE_PANDAREN_HORDE
Definition RaceMask.h:55
@ RACE_TAUREN
Definition RaceMask.h:35
constexpr Trinity::RaceMask< T, N > RACEMASK_ALLIANCE_v
Definition RaceMask.h:272
constexpr Trinity::RaceMask< uint64 > RACEMASK_ALL
Definition RaceMask.h:292
std::array< T, N > RawValue
Definition RaceMask.h:95
constexpr bool operator==(RaceMask const &) const =default
static constexpr int32 GetRaceBit(uint32 raceId)
Definition RaceMask.h:97
constexpr RaceMask operator&(RaceMask right) const
Definition RaceMask.h:175
constexpr bool HasRace(uint32 raceId) const
Definition RaceMask.h:149
constexpr RaceMask operator~() const
Definition RaceMask.h:191
constexpr bool IsEmpty() const
Definition RaceMask.h:167
constexpr RaceMask operator|(RaceMask right) const
Definition RaceMask.h:183
static constexpr RaceMask GetMaskForRace(uint32 raceId)
Definition RaceMask.h:156