2#include "gaia/config/config.h"
7#include "gaia/cnt/sparse_storage.h"
8#include "gaia/core/hashing_policy.h"
9#include "gaia/core/utility.h"
10#include "gaia/ecs/id_fwd.h"
14#define GAIA_ID(type) GAIA_ID_##type
16 using Identifier = uint64_t;
17 inline constexpr Identifier IdentifierBad = (Identifier)-1;
18 inline constexpr Identifier EntityCompMask = IdentifierBad << 1;
19 inline constexpr IdentifierId IdentifierIdBad = (IdentifierId)-1;
26 static constexpr uint32_t IdMask = IdentifierIdBad;
27 static constexpr uint32_t MaxAlignment_Bits = 10;
28 static constexpr uint32_t MaxAlignment = (1U << MaxAlignment_Bits) - 1;
29 static constexpr uint32_t MaxComponentSize_Bits = 12;
30 static constexpr uint32_t MaxComponentSizeInBytes = (1 << MaxComponentSize_Bits) - 1;
37 IdentifierData
soa : meta::StructToTupleMaxTypes_Bits;
39 IdentifierData
size : MaxComponentSize_Bits;
41 IdentifierData
alig : MaxAlignment_Bits;
45 static_assert(
sizeof(
InternalData) ==
sizeof(Identifier));
54 Component(uint32_t
id, uint32_t soa, uint32_t size, uint32_t alig) noexcept {
62 GAIA_NODISCARD
constexpr auto id() const noexcept {
63 return (uint32_t)data.id;
66 GAIA_NODISCARD
constexpr auto soa() const noexcept {
67 return (uint32_t)data.soa;
70 GAIA_NODISCARD
constexpr auto size() const noexcept {
71 return (uint32_t)data.size;
74 GAIA_NODISCARD
constexpr auto alig() const noexcept {
75 return (uint32_t)data.alig;
78 GAIA_NODISCARD
constexpr auto value() const noexcept {
82 GAIA_NODISCARD
constexpr bool operator==(Component other)
const noexcept {
83 return value() == other.value();
86 GAIA_NODISCARD
constexpr bool operator!=(Component other)
const noexcept {
87 return value() != other.value();
90 GAIA_NODISCARD
constexpr bool operator<(Component other)
const noexcept {
91 return id() < other.id();
99 enum EntityKind : uint8_t {
106 inline constexpr const char* EntityKindString[] = {
"Gen",
"Uni"};
112 template <
typename T>
114 static_assert(core::is_raw_v<T>);
116 std::is_trivial_v<T> ||
120 "Non-trivial Uni component must implement operator==");
123 static constexpr EntityKind
Kind = EntityKind::EK_Uni;
134 template <
typename,
typename =
void>
136 template <
typename T>
139 template <
typename T>
142 static constexpr EntityKind
Kind = EntityKind::EK_Gen;
152 template <
typename T>
155 static constexpr EntityKind
Kind = T::Kind;
158 using Type =
typename T::TType;
160 using TypeFull = std::conditional_t<Kind == EntityKind::EK_Gen, Type, uni<Type>>;
165 template <
typename,
typename =
void>
167 template <
typename T>
168 struct is_gen_component<T, std::void_t<decltype(T::Kind)>>: std::bool_constant<T::Kind == EntityKind::EK_Gen> {};
170 template <
typename T,
typename =
void>
174 template <
typename T>
180 template <
typename T>
181 using component_type_t =
typename detail::component_type<T>::type;
183 template <
typename T>
184 inline constexpr EntityKind entity_kind_v = component_type_t<T>::Kind;
201 template <
typename Rel,
typename Tgt>
203 using rel_comp_type = component_type_t<Rel>;
204 using tgt_comp_type = component_type_t<Tgt>;
207 using rel =
typename rel_comp_type::TypeFull;
208 using tgt =
typename tgt_comp_type::TypeFull;
209 using rel_type =
typename rel_comp_type::Type;
210 using tgt_type =
typename tgt_comp_type::Type;
211 using rel_original =
typename rel_comp_type::TypeOriginal;
212 using tgt_original =
typename tgt_comp_type::TypeOriginal;
213 using type = std::conditional_t<!std::is_empty_v<rel_type> || std::is_empty_v<tgt_type>, rel, tgt>;
216 template <
typename T>
218 static constexpr bool value = std::is_base_of<detail::pair_base, core::raw_t<T>>::value;
226 static constexpr uint32_t IdMask = IdentifierIdBad;
251 static_assert(
sizeof(
InternalData) ==
sizeof(Identifier));
258 constexpr Entity() noexcept: val(IdentifierBad) {};
263 template <
typename T,
typename = std::enable_if_t<std::is_same_v<T, Identifier>>>
264 constexpr Entity(T value)
noexcept: val(value) {}
267 Entity(EntityId
id, IdentifierData gen)
noexcept {
273 Entity(EntityId
id, IdentifierData gen,
bool isEntity,
bool isPair, EntityKind kind)
noexcept {
282 GAIA_NODISCARD
constexpr auto id() const noexcept {
283 return (uint32_t)data.id;
286 GAIA_NODISCARD
constexpr auto gen() const noexcept {
287 return (uint32_t)data.gen;
290 GAIA_NODISCARD
constexpr bool entity() const noexcept {
291 return data.ent != 0;
294 GAIA_NODISCARD
constexpr bool pair() const noexcept {
295 return data.pair != 0;
298 GAIA_NODISCARD
constexpr bool comp() const noexcept {
299 return (data.pair | data.ent) == 0;
302 GAIA_NODISCARD
constexpr auto kind() const noexcept {
303 return (EntityKind)data.kind;
306 GAIA_NODISCARD
constexpr auto value() const noexcept {
310 GAIA_NODISCARD
constexpr bool operator==(Entity other)
const noexcept {
311 return value() == other.value();
314 GAIA_NODISCARD
constexpr bool operator!=(Entity other)
const noexcept {
315 return value() != other.value();
318 GAIA_NODISCARD
constexpr bool operator<(Entity other)
const noexcept {
319 return value() < other.value();
321 GAIA_NODISCARD
constexpr bool operator<=(Entity other)
const noexcept {
322 return value() <= other.value();
325 GAIA_NODISCARD
constexpr bool operator>(Entity other)
const noexcept {
326 return value() > other.value();
328 GAIA_NODISCARD
constexpr bool operator>=(Entity other)
const noexcept {
329 return value() >= other.value();
333 inline static const Entity EntityBad = Entity(IdentifierBad);
346 return {core::calculate_hash64(entity.value())};
350 static constexpr bool IsDirectHashKey =
true;
365 size_t hash()
const {
366 return (
size_t)m_hash.hash;
370 if GAIA_LIKELY (m_hash != other.m_hash)
373 return m_entity == other.m_entity;
377 return !operator==(other);
402 operator Entity()
const noexcept {
404 m_first.id(), m_second.id(),
407 (
bool)m_first.kind(),
414 Entity first()
const noexcept {
418 Entity second()
const noexcept {
422 bool operator==(
const pair& other)
const {
423 return m_first == other.m_first && m_second == other.m_second;
425 bool operator!=(
const pair& other)
const {
426 return !operator==(other);
437 template <
typename T,
typename U =
void>
439 using type =
typename component_type<T>::type;
442 template <
typename T>
444 using storage_type =
typename T::type;
445 using type =
typename component_type<storage_type>::type;
449 template <
typename T>
450 using actual_type_t =
typename detail::actual_type<T>::type;
491 inline Entity Core =
Entity(0, 0,
false,
false, EntityKind::EK_Gen);
495 inline Entity OnDelete =
Entity(3, 0,
false,
false, EntityKind::EK_Gen);
496 inline Entity OnDeleteTarget =
Entity(4, 0,
false,
false, EntityKind::EK_Gen);
497 inline Entity Remove =
Entity(5, 0,
false,
false, EntityKind::EK_Gen);
498 inline Entity Delete =
Entity(6, 0,
false,
false, EntityKind::EK_Gen);
499 inline Entity Error =
Entity(7, 0,
false,
false, EntityKind::EK_Gen);
501 inline Entity Requires =
Entity(8, 0,
false,
false, EntityKind::EK_Gen);
502 inline Entity CantCombine =
Entity(9, 0,
false,
false, EntityKind::EK_Gen);
503 inline Entity Exclusive =
Entity(10, 0,
false,
false, EntityKind::EK_Gen);
505 inline Entity Acyclic =
Entity(11, 0,
false,
false, EntityKind::EK_Gen);
506 inline Entity Traversable =
Entity(12, 0,
false,
false, EntityKind::EK_Gen);
508 inline Entity All =
Entity(13, 0,
false,
false, EntityKind::EK_Gen);
511 inline Entity ChildOf =
Entity(14, 0,
false,
false, EntityKind::EK_Gen);
513 inline Entity Is =
Entity(15, 0,
false,
false, EntityKind::EK_Gen);
515 inline Entity System =
Entity(16, 0,
false,
false, EntityKind::EK_Gen);
516 inline Entity DependsOn =
Entity(17, 0,
false,
false, EntityKind::EK_Gen);
518 inline Entity Var0 =
Entity(18, 0,
false,
false, EntityKind::EK_Gen);
519 inline Entity Var1 =
Entity(19, 0,
false,
false, EntityKind::EK_Gen);
520 inline Entity Var2 =
Entity(20, 0,
false,
false, EntityKind::EK_Gen);
521 inline Entity Var3 =
Entity(21, 0,
false,
false, EntityKind::EK_Gen);
522 inline Entity Var4 =
Entity(22, 0,
false,
false, EntityKind::EK_Gen);
523 inline Entity Var5 =
Entity(23, 0,
false,
false, EntityKind::EK_Gen);
524 inline Entity Var6 =
Entity(24, 0,
false,
false, EntityKind::EK_Gen);
525 inline Entity Var7 =
Entity(25, 0,
false,
false, EntityKind::EK_Gen);
528 inline Entity GAIA_ID(LastCoreComponent) = Var7;
534 GAIA_NODISCARD
inline bool is_wildcard(EntityId entityId) {
535 return entityId == All.id();
538 GAIA_NODISCARD
inline bool is_wildcard(Entity entity) {
539 return entity.pair() && (is_wildcard(entity.id()) || is_wildcard(entity.gen()));
542 GAIA_NODISCARD
inline bool is_wildcard(Pair pair) {
543 return pair.first() == All || pair.second() == All;
546 GAIA_NODISCARD
inline bool is_variable(EntityId entityId) {
547 return entityId <= Var7.id() && entityId >= Var0.id();
550 GAIA_NODISCARD
inline bool is_variable(Entity entity) {
551 return entity.id() <= Var7.id() && entity.id() >= Var0.id();
554 GAIA_NODISCARD
inline bool is_variable(Pair pair) {
555 return is_variable(pair.first()) || is_variable(pair.second());
563 static sparse_id get(
const ecs::Entity& item)
noexcept {
Wrapper for two Entities forming a relationship pair.
Definition id.h:395
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:202
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition sparse_storage.h:31
IdentifierData soa
Component is SoA.
Definition id.h:37
IdentifierData alig
Component alignment.
Definition id.h:41
IdentifierData size
Component size.
Definition id.h:39
uint32_t id
Index in the entity array.
Definition id.h:35
IdentifierData unused
Unused part.
Definition id.h:43
Component used to describe the entity name.
Definition id.h:384
Hashmap lookup structure used for Entity.
Definition id.h:336
EntityId id
Index in the entity array.
Definition id.h:230
IdentifierData tmp
0-real entity, 1-temporary entity
Definition id.h:247
IdentifierData kind
0-EntityKind::CT_Gen, 1-EntityKind::CT_Uni
Definition id.h:245
IdentifierData gen
Generation index. Incremented every time an entity is deleted.
Definition id.h:239
IdentifierData pair
0-ordinary, 1-pair
Definition id.h:243
IdentifierData ent
0-component, 1-entity
Definition id.h:241
Entity(EntityId id, IdentifierData gen) noexcept
Special constructor for cnt::ilist.
Definition id.h:267
constexpr Entity(T value) noexcept
We need the entity to be braces-constructible and at the same type prevent it from getting constructe...
Definition id.h:264
static constexpr EntityKind Kind
Component kind.
Definition id.h:123
T TTypeOriginal
Original template type.
Definition id.h:130
T TType
Raw type with no additional sugar.
Definition id.h:126