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;
21 enum class DataStorageType : uint32_t {
35 static constexpr uint32_t IdMask = IdentifierIdBad;
36 static constexpr uint32_t MaxComponentSize_Bits = 13;
37 static constexpr uint32_t MaxComponentSizeInBytes = (1 << MaxComponentSize_Bits) - 1;
38 static constexpr uint32_t MaxAlignment_Bits = MaxComponentSize_Bits;
39 static constexpr uint32_t MaxAlignment = MaxComponentSizeInBytes;
46 IdentifierData
size : MaxComponentSize_Bits;
48 IdentifierData
alig : MaxAlignment_Bits;
52 IdentifierData
soa : meta::StructToTupleMaxTypes_Bits;
56 static_assert(
sizeof(
InternalData) ==
sizeof(Identifier));
65 Component(uint32_t
id, uint32_t soa, uint32_t size, uint32_t alig, DataStorageType storage) noexcept {
70 data.storage = (IdentifierData)storage;
74 GAIA_NODISCARD
constexpr auto id() const noexcept {
75 return (uint32_t)data.id;
78 GAIA_NODISCARD
constexpr auto soa() const noexcept {
79 return (uint32_t)data.soa;
82 GAIA_NODISCARD
constexpr auto size() const noexcept {
83 return (uint32_t)data.size;
86 GAIA_NODISCARD
constexpr auto alig() const noexcept {
87 return (uint32_t)data.alig;
90 GAIA_NODISCARD
constexpr DataStorageType storage_type() const noexcept {
91 return (DataStorageType)data.storage;
94 GAIA_NODISCARD
constexpr auto value() const noexcept {
98 GAIA_NODISCARD
constexpr bool operator==(Component other)
const noexcept {
99 return value() == other.value();
102 GAIA_NODISCARD
constexpr bool operator!=(Component other)
const noexcept {
103 return value() != other.value();
106 GAIA_NODISCARD
constexpr bool operator<(Component other)
const noexcept {
107 return id() < other.id();
115 enum EntityKind : uint8_t {
122 inline constexpr const char* EntityKindString[] = {
"Gen",
"Uni"};
128 template <
typename T>
130 static_assert(core::is_raw_v<T>);
132 std::is_trivial_v<T> ||
136 "Non-trivial Uni component must implement operator==");
139 static constexpr EntityKind
Kind = EntityKind::EK_Uni;
150 template <
typename,
typename =
void>
152 template <
typename T>
155 template <
typename T>
158 static constexpr EntityKind
Kind = EntityKind::EK_Gen;
168 template <
typename T>
171 static constexpr EntityKind
Kind = T::Kind;
174 using Type =
typename T::TType;
176 using TypeFull = std::conditional_t<Kind == EntityKind::EK_Gen, Type, uni<Type>>;
181 template <
typename,
typename =
void>
183 template <
typename T>
184 struct is_gen_component<T, std::void_t<decltype(T::Kind)>>: std::bool_constant<T::Kind == EntityKind::EK_Gen> {};
186 template <
typename T,
typename =
void>
190 template <
typename T>
196 template <
typename T>
197 using component_type_t =
typename detail::component_type<T>::type;
199 template <
typename T>
200 inline constexpr EntityKind entity_kind_v = component_type_t<T>::Kind;
217 template <
typename Rel,
typename Tgt>
219 using rel_comp_type = component_type_t<Rel>;
220 using tgt_comp_type = component_type_t<Tgt>;
223 using rel =
typename rel_comp_type::TypeFull;
224 using tgt =
typename tgt_comp_type::TypeFull;
225 using rel_type =
typename rel_comp_type::Type;
226 using tgt_type =
typename tgt_comp_type::Type;
227 using rel_original =
typename rel_comp_type::TypeOriginal;
228 using tgt_original =
typename tgt_comp_type::TypeOriginal;
229 using type = std::conditional_t<!std::is_empty_v<rel_type> || std::is_empty_v<tgt_type>, rel, tgt>;
232 template <
typename T>
234 static constexpr bool value = std::is_base_of<detail::pair_base, core::raw_t<T>>::value;
242 static constexpr uint32_t IdMask = IdentifierIdBad;
267 static_assert(
sizeof(
InternalData) ==
sizeof(Identifier));
274 constexpr Entity() noexcept: val(IdentifierBad) {};
279 template <
typename T,
typename = std::enable_if_t<std::is_same_v<T, Identifier>>>
280 constexpr Entity(T value)
noexcept: val(value) {}
283 Entity(EntityId
id, IdentifierData gen)
noexcept {
289 Entity(EntityId
id, IdentifierData gen,
bool isEntity,
bool isPair, EntityKind kind)
noexcept {
298 GAIA_NODISCARD
constexpr auto id() const noexcept {
299 return (uint32_t)data.id;
302 GAIA_NODISCARD
constexpr auto gen() const noexcept {
303 return (uint32_t)data.gen;
306 GAIA_NODISCARD
constexpr bool entity() const noexcept {
307 return data.ent != 0;
310 GAIA_NODISCARD
constexpr bool pair() const noexcept {
311 return data.pair != 0;
314 GAIA_NODISCARD
constexpr bool comp() const noexcept {
315 return (data.pair | data.ent) == 0;
318 GAIA_NODISCARD
constexpr auto kind() const noexcept {
319 return (EntityKind)data.kind;
322 GAIA_NODISCARD
constexpr auto value() const noexcept {
326 GAIA_NODISCARD
constexpr bool operator==(Entity other)
const noexcept {
327 return value() == other.value();
330 GAIA_NODISCARD
constexpr bool operator!=(Entity other)
const noexcept {
331 return value() != other.value();
334 GAIA_NODISCARD
constexpr bool operator<(Entity other)
const noexcept {
335 return value() < other.value();
337 GAIA_NODISCARD
constexpr bool operator<=(Entity other)
const noexcept {
338 return value() <= other.value();
341 GAIA_NODISCARD
constexpr bool operator>(Entity other)
const noexcept {
342 return value() > other.value();
344 GAIA_NODISCARD
constexpr bool operator>=(Entity other)
const noexcept {
345 return value() >= other.value();
348 template <
typename Serializer>
349 void save(Serializer& s)
const;
351 template <
typename Serializer>
352 void load(Serializer& s);
355 inline static const Entity EntityBad = Entity(IdentifierBad);
359 uint32_t savedLastCoreComponentId = 0;
360 uint32_t currLastCoreComponentId = 0;
373 EntityLoadRemapGuard(uint32_t savedLastCoreComponentId, uint32_t currLastCoreComponentId)
noexcept:
374 prev(g_entityLoadRemapState) {
375 g_entityLoadRemapState.savedLastCoreComponentId = savedLastCoreComponentId;
376 g_entityLoadRemapState.currLastCoreComponentId = currLastCoreComponentId;
377 g_entityLoadRemapState.active =
true;
381 g_entityLoadRemapState = prev;
390 GAIA_NODISCARD
inline uint32_t remap_loaded_entity_id(
391 uint32_t
id, uint32_t savedLastCoreComponentId, uint32_t currLastCoreComponentId)
noexcept {
392 if (
id == Entity::IdMask ||
393 id <= savedLastCoreComponentId ||
394 currLastCoreComponentId <= savedLastCoreComponentId
398 return id + (currLastCoreComponentId - savedLastCoreComponentId);
401 GAIA_NODISCARD
inline Entity
402 remap_loaded_entity(
Entity entity, uint32_t savedLastCoreComponentId, uint32_t currLastCoreComponentId)
noexcept {
403 if (entity == EntityBad)
406 if (!entity.pair()) {
408 (EntityId)remap_loaded_entity_id(entity.id(), savedLastCoreComponentId, currLastCoreComponentId),
409 entity.gen(), entity.entity(),
false, entity.kind());
413 (EntityId)remap_loaded_entity_id(entity.id(), savedLastCoreComponentId, currLastCoreComponentId),
414 (IdentifierData)remap_loaded_entity_id(entity.gen(), savedLastCoreComponentId, currLastCoreComponentId),
415 entity.entity(),
true, entity.kind());
418 GAIA_NODISCARD
inline Entity remap_loaded_entity(Entity entity)
noexcept {
419 const auto& state = g_entityLoadRemapState;
423 return remap_loaded_entity(entity, state.savedLastCoreComponentId, state.currLastCoreComponentId);
427 template <
typename Serializer>
428 inline void Entity::save(Serializer& s)
const {
432 template <
typename Serializer>
433 inline void Entity::load(Serializer& s) {
435 *
this = detail::remap_loaded_entity(*
this);
449 return {core::calculate_hash64(entity.value())};
453 static constexpr bool IsDirectHashKey =
true;
468 size_t hash()
const {
469 return (
size_t)m_hash.hash;
473 if GAIA_LIKELY (m_hash != other.m_hash)
476 return m_entity == other.m_entity;
480 return !operator==(other);
491 uint32_t alias_len{};
507 operator Entity()
const noexcept {
509 m_first.id(), m_second.id(),
512 (
bool)m_first.kind(),
519 Entity first()
const noexcept {
523 Entity second()
const noexcept {
527 bool operator==(
const pair& other)
const {
528 return m_first == other.m_first && m_second == other.m_second;
530 bool operator!=(
const pair& other)
const {
531 return !operator==(other);
542 template <
typename T,
typename U =
void>
544 using type =
typename component_type<T>::type;
547 template <
typename T>
549 using storage_type =
typename T::type;
550 using type =
typename component_type<storage_type>::type;
554 template <
typename T>
555 using actual_type_t =
typename detail::actual_type<T>::type;
586 struct GAIA_API System_;
605 inline Entity Core =
Entity(0, 0,
false,
false, EntityKind::EK_Gen);
609 inline Entity OnDelete =
Entity(3, 0,
false,
false, EntityKind::EK_Gen);
610 inline Entity OnDeleteTarget =
Entity(4, 0,
false,
false, EntityKind::EK_Gen);
611 inline Entity Remove =
Entity(5, 0,
false,
false, EntityKind::EK_Gen);
612 inline Entity Delete =
Entity(6, 0,
false,
false, EntityKind::EK_Gen);
613 inline Entity Error =
Entity(7, 0,
false,
false, EntityKind::EK_Gen);
615 inline Entity Requires =
Entity(8, 0,
false,
false, EntityKind::EK_Gen);
616 inline Entity CantCombine =
Entity(9, 0,
false,
false, EntityKind::EK_Gen);
617 inline Entity Exclusive =
Entity(10, 0,
false,
false, EntityKind::EK_Gen);
619 inline Entity DontFragment =
Entity(11, 0,
false,
false, EntityKind::EK_Gen);
620 inline Entity Sparse =
Entity(12, 0,
false,
false, EntityKind::EK_Gen);
622 inline Entity Acyclic =
Entity(13, 0,
false,
false, EntityKind::EK_Gen);
623 inline Entity Traversable =
Entity(14, 0,
false,
false, EntityKind::EK_Gen);
625 inline Entity All =
Entity(15, 0,
false,
false, EntityKind::EK_Gen);
628 inline Entity ChildOf =
Entity(16, 0,
false,
false, EntityKind::EK_Gen);
630 inline Entity Parent =
Entity(17, 0,
false,
false, EntityKind::EK_Gen);
632 inline Entity Is =
Entity(18, 0,
false,
false, EntityKind::EK_Gen);
634 inline Entity Prefab =
Entity(19, 0,
false,
false, EntityKind::EK_Gen);
636 inline Entity OnInstantiate =
Entity(20, 0,
false,
false, EntityKind::EK_Gen);
637 inline Entity Override =
Entity(21, 0,
false,
false, EntityKind::EK_Gen);
638 inline Entity Inherit =
Entity(22, 0,
false,
false, EntityKind::EK_Gen);
639 inline Entity DontInherit =
Entity(23, 0,
false,
false, EntityKind::EK_Gen);
641 inline Entity System =
Entity(24, 0,
false,
false, EntityKind::EK_Gen);
642 inline Entity DependsOn =
Entity(25, 0,
false,
false, EntityKind::EK_Gen);
644 inline Entity Observer =
Entity(26, 0,
false,
false, EntityKind::EK_Gen);
646 inline Entity Var0 =
Entity(27, 0,
false,
false, EntityKind::EK_Gen);
647 inline Entity Var1 =
Entity(28, 0,
false,
false, EntityKind::EK_Gen);
648 inline Entity Var2 =
Entity(29, 0,
false,
false, EntityKind::EK_Gen);
649 inline Entity Var3 =
Entity(30, 0,
false,
false, EntityKind::EK_Gen);
650 inline Entity Var4 =
Entity(31, 0,
false,
false, EntityKind::EK_Gen);
651 inline Entity Var5 =
Entity(32, 0,
false,
false, EntityKind::EK_Gen);
652 inline Entity Var6 =
Entity(33, 0,
false,
false, EntityKind::EK_Gen);
653 inline Entity Var7 =
Entity(34, 0,
false,
false, EntityKind::EK_Gen);
654 inline static constexpr uint32_t MaxVarCnt = 8;
666 inline Entity GAIA_ID(LastCoreComponent) = Var7;
672 GAIA_NODISCARD
inline bool is_wildcard(EntityId entityId) {
673 return entityId == All.id();
676 GAIA_NODISCARD
inline bool is_wildcard(Entity entity) {
677 return entity.pair() && (is_wildcard(entity.id()) || is_wildcard(entity.gen()));
680 GAIA_NODISCARD
inline bool is_wildcard(Pair pair) {
681 return pair.first() == All || pair.second() == All;
684 GAIA_NODISCARD
inline bool is_variable(EntityId entityId) {
685 return entityId <= Var7.id() && entityId >= Var0.id();
688 GAIA_NODISCARD
inline bool is_variable(Entity entity) {
689 return entity.id() <= Var7.id() && entity.id() >= Var0.id();
692 GAIA_NODISCARD
inline bool is_variable(Pair pair) {
693 return is_variable(pair.first()) || is_variable(pair.second());
701 static sparse_id get(
const ecs::Entity& item)
noexcept {
Wrapper for two Entities forming a relationship pair.
Definition id.h:500
Wrapper for two types forming a relationship pair. Depending on what types are used to form a pair it...
Definition id.h:218
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:52
IdentifierData alig
Component alignment.
Definition id.h:48
IdentifierData size
Component size.
Definition id.h:46
uint32_t id
Index in the entity array.
Definition id.h:44
IdentifierData storage
Component storage kind. 0 = table, 1 = sparse.
Definition id.h:50
IdentifierData unused
Unused part.
Definition id.h:54
Component used to describe the entity name.
Definition id.h:487
Hashmap lookup structure used for Entity.
Definition id.h:439
EntityId id
Index in the entity array.
Definition id.h:246
IdentifierData tmp
0-real entity, 1-temporary entity
Definition id.h:263
IdentifierData kind
0-EntityKind::CT_Gen, 1-EntityKind::CT_Uni
Definition id.h:261
IdentifierData gen
Generation index. Incremented every time an entity is deleted.
Definition id.h:255
IdentifierData pair
0-ordinary, 1-pair
Definition id.h:259
IdentifierData ent
0-component, 1-entity
Definition id.h:257
Entity(EntityId id, IdentifierData gen) noexcept
Special constructor for cnt::ilist.
Definition id.h:283
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:280
Definition observer.h:238
static constexpr EntityKind Kind
Component kind.
Definition id.h:139
T TTypeOriginal
Original template type.
Definition id.h:146
T TType
Raw type with no additional sugar.
Definition id.h:142