summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-05-30 17:21:38 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-05-30 17:21:38 -0400
commit2a2b5c4dbfe278e282b3d8ae0352a11da50c872a (patch)
treead0f61c521d2fe0f79a55a72e36af39161b25067 /src
parentf1d22f3fde0cb7bf201168a11774793b4efc98f7 (diff)
pke: entities: BucketContainer>pk_bkt_arr_t
Diffstat (limited to 'src')
-rw-r--r--src/ecs.cpp4
-rw-r--r--src/ecs.hpp1
-rw-r--r--src/entities.cpp102
-rw-r--r--src/entities.hpp7
-rw-r--r--src/project.cpp23
5 files changed, 65 insertions, 72 deletions
diff --git a/src/ecs.cpp b/src/ecs.cpp
index 137686b..6ff4d5c 100644
--- a/src/ecs.cpp
+++ b/src/ecs.cpp
@@ -114,6 +114,10 @@ void ECS_MarkForRemoval(Entity_Base *entity) {
pk_arr_append_t(&entitiesMarkedForRemoval, entity);
}
+pk_bkt_arr *ECS_GetEntities() {
+ return &ecs.bc.entityPtrs;
+};
+
void ECS_Tick_Early(double delta) {
// these reserves might happen 1 tick early, but that's fine
(void)delta;
diff --git a/src/ecs.hpp b/src/ecs.hpp
index 91202c4..436429c 100644
--- a/src/ecs.hpp
+++ b/src/ecs.hpp
@@ -20,6 +20,7 @@ EntityHandle ECS_CreateEntity(Entity_Base *entity, Entity_Base *parentEnt = null
Entity_Base *ECS_GetEntity(EntityHandle handle);
Entity_Base *ECS_GetEntityByUUID(pk_uuid uuid);
void ECS_MarkForRemoval(Entity_Base *entity);
+pk_bkt_arr *ECS_GetEntities();
void ECS_HandleCollision(CompInstance *lhs, CompInstance *rhs);
diff --git a/src/entities.cpp b/src/entities.cpp
index 09e9398..eba48d5 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -1,7 +1,6 @@
#include "entities.hpp"
-#include "bucketed-array.hpp"
#include "ecs.hpp"
#include "game-settings.hpp"
#include "math-helpers.hpp"
@@ -18,7 +17,10 @@
#include <filesystem>
#include <vulkan/vulkan_core.h>
-BucketContainer<EntityType> EntityType_BC{};
+
+struct EntityTypeMaster {
+ pk_bkt_arr_t<EntityType> bc{};
+} et_mstr;
struct EntToTeardown {
EntityHandle handle = EntityHandle_MAX;
@@ -28,13 +30,13 @@ struct EntToTeardown {
pk_arr_t<EntToTeardown> EntityTypesToTeardown{};
void EntityType_Init() {
- Buckets_Init(EntityType_BC);
+ new (&et_mstr.bc) pk_bkt_arr_t<EntityType>;
pk_arr_reserve(&EntityTypesToTeardown, 16);
}
EntityType *EntityType_Create(pk_uuid uuid) {
- EntityTypeHandle entTypeHandle{Buckets_NewHandle(EntityType_BC)};
- EntityType &entityType = EntityType_BC.buckets[entTypeHandle.bucketIndex][entTypeHandle.itemIndex];
+ EntityTypeHandle entTypeHandle{pk_bkt_arr_new_handle(&et_mstr.bc)};
+ EntityType &entityType = et_mstr.bc[entTypeHandle];
new (&entityType) EntityType{};
entityType.uuid = uuid;
ECS_CreateEntity(&entityType);
@@ -100,37 +102,37 @@ Entity_Base *EntityType_CreateGenericInstance(EntityType *et, Entity_Base *level
}
EntityType *EntityType_FindByTypeCode(const char *typeCode) {
- for (pk_handle_bucket_index_T b = 0; b <= EntityType_BC.pkeHandle.bucketIndex; ++b) {
- auto &bkt = EntityType_BC.buckets[b];
- long itemCount = EntityType_BC.pkeHandle.bucketIndex == b ? EntityType_BC.pkeHandle.itemIndex : EntityType_BC.limits.itemIndex;
- for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
- auto &entityType = bkt[i];
- if (entityType.handle == EntityHandle_MAX) continue;
- if (strcmp(typeCode, entityType.entityTypeCode.val) == 0) {
- return &entityType;
- }
+ auto et_find_cb = [](void *user_data, const void *user_obj_data, const void *arr_obj_data) {
+ (void)user_data;
+ const char *typeCode = reinterpret_cast<const char *>(user_obj_data);
+ const EntityType &entityType = *reinterpret_cast<const EntityType *>(arr_obj_data);
+ if (entityType.handle == EntityHandle_MAX) {
+ return false;
}
+ return strcmp(typeCode, entityType.entityTypeCode.val) == 0;
+ };
+ EntityTypeHandle handle { pk_bkt_arr_find_first_handle(&et_mstr.bc, et_find_cb, NULL, typeCode) };
+ if (handle == EntityTypeHandle_MAX) {
+ return nullptr;
}
- return nullptr;
+ return &et_mstr.bc[handle];
}
EntityType *EntityType_FindByEntityHandle_Inner(EntityHandle handle) {
- if (handle == EntityHandle_MAX) return nullptr;
- // 2025-05-29 JCB these are wrong
- if (handle.b> EntityType_BC.limits.bucketIndex) return nullptr;
- if (handle.i> EntityType_BC.limits.itemIndex) return nullptr;
- if (handle.b== EntityType_BC.pkeHandle.bucketIndex && handle.i>= EntityType_BC.pkeHandle.itemIndex) return nullptr;
- for (pk_handle_bucket_index_T b = 0; b <= EntityType_BC.pkeHandle.bucketIndex; ++b) {
- auto &bkt = EntityType_BC.buckets[b];
- long itemCount = EntityType_BC.pkeHandle.bucketIndex == b ? EntityType_BC.pkeHandle.itemIndex : EntityType_BC.limits.itemIndex;
- for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
- auto &entityType = bkt[i];
- if (entityType.handle == handle) {
- return &entityType;
- }
- }
+ if (pk_bkt_arr_handle_validate(ECS_GetEntities(), handle) != PK_BKT_ARR_HANDLE_VALIDATION_VALID) {
+ return nullptr;
}
- return nullptr;
+ auto et_find_cb = [](void *user_data, const void *user_obj_data, const void *arr_obj_data) {
+ (void)user_data;
+ const EntityHandle &handle = *reinterpret_cast<const EntityHandle *>(user_obj_data);
+ const EntityType &entityType = *reinterpret_cast<const EntityType *>(arr_obj_data);
+ return entityType.handle == handle;
+ };
+ EntityTypeHandle found_handle { pk_bkt_arr_find_first_handle(&et_mstr.bc, et_find_cb, NULL, &handle) };
+ if (found_handle == EntityTypeHandle_MAX) {
+ return nullptr;
+ }
+ return &et_mstr.bc[found_handle];
}
EntityType *EntityType_FindByEntityHandle(EntityHandle handle) {
Entity_Base *base_entity;
@@ -1398,35 +1400,23 @@ void EntityType_RolloverInstances(EntityType &et, CompGrBinds &grBinds) {
vkFreeMemory(vkDevice, oldMemory, vkAllocator);
}
-pk_handle_bucket_index_T EntityType_GetBucketCount() {
- return EntityType_BC.pkeHandle.bucketIndex + 1;
-}
-
-EntityType *EntityType_GetEntityTypes(pk_handle_bucket_index_T bucketIndex, pk_handle_item_index_T &itemCount) {
- assert(bucketIndex <= EntityType_BC.pkeHandle.bucketIndex);
- if (bucketIndex == EntityType_BC.pkeHandle.bucketIndex) {
- itemCount = EntityType_BC.pkeHandle.itemIndex;
- } else {
- itemCount = EntityType_BC.limits.itemIndex;
- }
- return EntityType_BC.buckets[bucketIndex];
+pk_bkt_arr *EntityType_GetEntityTypes() {
+ return &et_mstr.bc;
}
void EntityType_Teardown() {
- for (pk_handle_bucket_index_T b = 0; b <= EntityType_BC.pkeHandle.bucketIndex; ++b) {
- auto &bkt = EntityType_BC.buckets[b];
- long itemCount = EntityType_BC.pkeHandle.bucketIndex == b ? EntityType_BC.pkeHandle.itemIndex : EntityType_BC.limits.itemIndex;
- for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
- auto &et = bkt[i];
- if (et.modelAssetKey[0] == '\0') continue;
- CompGrBinds *grBindsArr[EntityTypeDetails_MAX] = {nullptr};
- for (long k = 0; k < et.detailsCount; ++k) {
- const EntityTypeDetails &etd = et.details[k];
- grBindsArr[k] = etd.grBinds;
- }
- EntityType_Unload(et, grBindsArr);
+ auto et_iter_cb = [](void *user_data, void *arr_obj_data) {
+ (void)user_data;
+ EntityType &et = *reinterpret_cast<EntityType *>(arr_obj_data);
+ if (et.modelAssetKey[0] == '\0') return;
+ CompGrBinds *grBindsArr[EntityTypeDetails_MAX] = {nullptr};
+ for (long k = 0; k < et.detailsCount; ++k) {
+ const EntityTypeDetails &etd = et.details[k];
+ grBindsArr[k] = etd.grBinds;
}
- }
- Buckets_Destroy(EntityType_BC);
+ EntityType_Unload(et, grBindsArr);
+ };
+ pk_bkt_arr_iterate(&et_mstr.bc, et_iter_cb, NULL);
+ pk_bkt_arr_teardown(&et_mstr.bc);
pk_arr_reset(&EntityTypesToTeardown);
}
diff --git a/src/entities.hpp b/src/entities.hpp
index 8cecb63..7ce6757 100644
--- a/src/entities.hpp
+++ b/src/entities.hpp
@@ -37,8 +37,8 @@ struct EntityType : public Entity_Base {
// PkeCallback serializeInstanceCallback; // TODO
// PkeCallback deserializeInstanceCallback; // TODO
};
-struct EntityTypeHandle : pk_handle {};
-constexpr EntityTypeHandle EntityTypeHandle_MAX = EntityTypeHandle{ pk_handle_MAX_constexpr };
+struct EntityTypeHandle : pk_bkt_arr_handle {};
+constexpr EntityTypeHandle EntityTypeHandle_MAX = EntityTypeHandle{ pk_bkt_arr_handle_MAX_constexpr };
void EntityType_Init();
EntityType *EntityType_FindByTypeCode(const char *typeCode);
@@ -49,8 +49,7 @@ void EntityType_Load(EntityType &et);
void EntityType_Tick(double delta);
void EntityType_Tick_Late(double delta);
void EntityType_RolloverInstances(EntityType &et, CompGrBinds &grBinds);
-pk_handle_bucket_index_T EntityType_GetBucketCount();
-EntityType *EntityType_GetEntityTypes(pk_handle_bucket_index_T bucketIndex, pk_handle_item_index_T &itemCount);
+pk_bkt_arr *EntityType_GetEntityTypes();
void EntityType_Teardown();
#endif /* PKE_ENTITIES_HPP */
diff --git a/src/project.cpp b/src/project.cpp
index dcb84ae..cfb54f2 100644
--- a/src/project.cpp
+++ b/src/project.cpp
@@ -381,7 +381,10 @@ void PkeProject_Save(const char *filePath) {
*/
using AssetLoopFn = pk_tmpln_1<void, Asset *, void *>;
+ using ETLoopFn = pk_tmpln_1<void, EntityType *, void *>;
AssetLoopFn asset_loop_cb{};
+ ETLoopFn et_loop_cb{};
+
asset_loop_cb.func = [&stream](Asset *arr_obj_data) {
if (PK_HAS_FLAG(arr_obj_data->flags, PKE_ASSET_FLAGS_MEM_STATIC)) return;
// TODO 2025-05-30 JCB
@@ -400,19 +403,15 @@ void PkeProject_Save(const char *filePath) {
};
pk_bkt_arr_iterate(AM_GetAssets(), &AssetLoopFn::invoke, &asset_loop_cb);
- const auto entBucketCount = EntityType_GetBucketCount();
- for (pk_handle_bucket_index_T b = 0; b < entBucketCount; ++b) {
- pk_handle_item_index_T itemCount = 0;
- auto *entities = EntityType_GetEntityTypes(b, itemCount);
- for (pk_handle_item_index_T i = 0; i < itemCount; ++i) {
- if (entities[i].modelAssetKey[0] == '\0') {
- continue;
- }
- stream << PKE_PROJ_FILE_OBJ_ENTITY_TYPE << std::endl;
- Proj_SerializeEntityType(stream, entities[i]);
- stream << PKE_PROJ_FILE_OBJ_END << std::endl;
+ et_loop_cb.func = [&stream](EntityType *arr_obj_data) {
+ if (arr_obj_data->modelAssetKey[0] == '\0') {
+ return;
}
- }
+ stream << PKE_PROJ_FILE_OBJ_ENTITY_TYPE << std::endl;
+ Proj_SerializeEntityType(stream, *arr_obj_data);
+ stream << PKE_PROJ_FILE_OBJ_END << std::endl;
+ };
+ pk_bkt_arr_iterate(EntityType_GetEntityTypes(), &ETLoopFn::invoke, &et_loop_cb);
FontTypeIndex font_count;
FontType *fonts = FontType_GetFonts(font_count);