From 6161b1a75105448731062fdae2544d598f58a450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Thu, 8 Feb 2018 15:15:56 +0100 Subject: [PATCH] Sdk/World: Use a Bitset to store free entity id --- ChangeLog.md | 1 + SDK/include/NDK/World.hpp | 2 +- SDK/include/NDK/World.inl | 2 +- SDK/src/NDK/World.cpp | 15 +++++++++------ 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 89ad63f04..4b5545f15 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -95,6 +95,7 @@ Nazara Development Kit: - Add ConstraintComponent2D class - Fix CollisionComponent3D initialization (teleportation to their real coordinates) which could sometimes mess up the physics scene. - ⚠️ Renamed World::Update() to World::Refresh() for more clarity and to differentiate it from World::Update(elapsedTime) +- World entity ids are now reused from lowest to highest (they were previously reused in reverse order of death) # 0.4: diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 694457412..1ee35979b 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -96,9 +96,9 @@ namespace Ndk std::vector m_entities; std::vector m_entityBlocks; std::vector> m_waitingEntities; - std::vector m_freeIdList; EntityList m_aliveEntities; Nz::Bitset m_dirtyEntities; + Nz::Bitset m_freeEntityIds; Nz::Bitset m_killedEntities; bool m_orderedSystemsUpdated; }; diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index 4ddc2638f..87aee67d3 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -288,7 +288,7 @@ namespace Ndk m_aliveEntities = std::move(world.m_aliveEntities); m_dirtyEntities = std::move(world.m_dirtyEntities); m_entityBlocks = std::move(world.m_entityBlocks); - m_freeIdList = std::move(world.m_freeIdList); + m_freeEntityIds = std::move(world.m_freeEntityIds); m_killedEntities = std::move(world.m_killedEntities); m_orderedSystems = std::move(world.m_orderedSystems); m_orderedSystemsUpdated = world.m_orderedSystemsUpdated; diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index dd99fee54..beb6a7d3b 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -61,11 +61,14 @@ namespace Ndk { EntityId id; EntityBlock* entBlock; - if (!m_freeIdList.empty()) + + std::size_t freeEntityId = m_freeEntityIds.FindFirst(); + if (freeEntityId != m_freeEntityIds.npos) { // We get an identifier - id = m_freeIdList.back(); - m_freeIdList.pop_back(); + m_freeEntityIds.Reset(freeEntityId); //< Remove id from free entity id + + id = static_cast(freeEntityId); entBlock = &m_entities[id]; entBlock->handle.Reset(&entBlock->entity); //< Reset handle (as it was reset when entity got destroyed) @@ -81,7 +84,7 @@ namespace Ndk { NazaraAssert(m_waitingEntities.empty(), "There should be no waiting entities if space is available in main container"); - m_entities.push_back(Entity(this, id)); //< We can't use emplace_back due to the scope + m_entities.emplace_back(Entity(this, id)); //< We can't make our vector create the entity due to the scope entBlock = &m_entities.back(); } else @@ -124,11 +127,11 @@ namespace Ndk m_entityBlocks.clear(); m_entities.clear(); - m_freeIdList.clear(); m_waitingEntities.clear(); m_aliveEntities.Clear(); m_dirtyEntities.Clear(); + m_freeEntityIds.Clear(); m_killedEntities.Clear(); } @@ -207,7 +210,7 @@ namespace Ndk entity->Destroy(); // Send back the identifier of the entity to the free queue - m_freeIdList.push_back(entity->GetId()); + m_freeEntityIds.UnboundedSet(entity->GetId()); } m_killedEntities.Reset();