2022-12-13 04:36:51 +00:00
|
|
|
/*
|
|
|
|
entity.c
|
|
|
|
|
|
|
|
ECS entity management
|
|
|
|
|
|
|
|
Copyright (C) 2022 Bill Currke
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/heapsort.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
|
|
|
#define IMPLEMENT_ECS_ENTITY_Funcs
|
|
|
|
#include "QF/ecs.h"
|
|
|
|
|
2022-12-18 12:11:21 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
static void swap_inds (uint32_t *a, uint32_t *b)
|
|
|
|
{
|
|
|
|
uint32_t t = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = t;
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:36:51 +00:00
|
|
|
VISIBLE void *
|
|
|
|
Ent_AddComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[comp];
|
2022-12-18 12:11:21 +00:00
|
|
|
ecs_subpool_t *subpool = ®istry->subpools[comp];
|
|
|
|
component_t *c = ®istry->components.a[comp];
|
2022-12-13 04:36:51 +00:00
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
uint32_t ind = pool->sparse[id];
|
|
|
|
if (ind >= pool->count || pool->dense[ind] != ent) {
|
|
|
|
if (pool->count == pool->max_count) {
|
|
|
|
pool->max_count += COMP_GROW;
|
|
|
|
pool->dense = realloc (pool->dense,
|
|
|
|
pool->max_count * sizeof (uint32_t));
|
2022-12-18 12:11:21 +00:00
|
|
|
Component_ResizeArray (c, &pool->data, pool->max_count);
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
uint32_t ind = pool->count++;
|
|
|
|
pool->sparse[id] = ind;
|
|
|
|
pool->dense[ind] = ent;
|
2022-12-18 12:11:21 +00:00
|
|
|
uint32_t rind = subpool->num_ranges - subpool->available;
|
|
|
|
if (rind && c->rangeid) {
|
|
|
|
uint32_t rangeid = c->rangeid (registry, ent, comp);
|
|
|
|
uint32_t rangeind = subpool->sorted[Ent_Index (rangeid)];
|
|
|
|
while (rind-- > rangeind) {
|
2022-12-20 08:56:08 +00:00
|
|
|
if (subpool->ranges[rind] == ind) {
|
|
|
|
subpool->ranges[rind]++;
|
2022-12-18 12:11:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-20 08:56:08 +00:00
|
|
|
uint32_t end = subpool->ranges[rind]++;
|
2022-12-18 12:11:21 +00:00
|
|
|
Component_MoveElements (c, pool->data, ind, end, 1);
|
|
|
|
swap_inds (&pool->sparse[Ent_Index (pool->dense[end])],
|
|
|
|
&pool->sparse[Ent_Index (pool->dense[ind])]);
|
|
|
|
swap_inds (&pool->dense[ind], &pool->dense[end]);
|
|
|
|
ind = end;
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
return Ent_GetComponent (ent, comp, registry);
|
|
|
|
}
|
|
|
|
|
2022-12-18 12:11:21 +00:00
|
|
|
static int
|
|
|
|
range_cmp (const void *_key, const void *_range, void *_subpool)
|
|
|
|
{
|
|
|
|
const uint32_t *key = _key;
|
2022-12-20 08:56:08 +00:00
|
|
|
const uint32_t *range = _range;
|
2022-12-18 12:11:21 +00:00
|
|
|
ecs_subpool_t *subpool = _subpool;
|
|
|
|
|
2023-03-05 07:10:09 +00:00
|
|
|
// range is the first index for the next subpool range
|
2022-12-20 08:56:08 +00:00
|
|
|
if (*key >= *range) {
|
2023-01-21 16:04:23 +00:00
|
|
|
return 1;
|
2022-12-18 12:11:21 +00:00
|
|
|
}
|
|
|
|
if (range - subpool->ranges > 0) {
|
2022-12-20 08:56:08 +00:00
|
|
|
return *key >= range[-1] ? 0 : -1;
|
2022-12-18 12:11:21 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-20 08:56:08 +00:00
|
|
|
static uint32_t *
|
2022-12-18 12:11:21 +00:00
|
|
|
find_range (ecs_subpool_t *subpool, uint32_t ind)
|
|
|
|
{
|
|
|
|
return bsearch_r (&ind, subpool->ranges,
|
|
|
|
subpool->num_ranges - subpool->available,
|
2022-12-20 08:56:08 +00:00
|
|
|
sizeof (uint32_t), range_cmp, subpool);
|
2022-12-18 12:11:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 04:36:51 +00:00
|
|
|
VISIBLE void
|
|
|
|
Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
|
|
|
|
{
|
|
|
|
uint32_t id = Ent_Index (ent);
|
|
|
|
ecs_pool_t *pool = ®istry->comp_pools[comp];
|
2022-12-18 12:11:21 +00:00
|
|
|
ecs_subpool_t *subpool = ®istry->subpools[comp];
|
2022-12-13 04:36:51 +00:00
|
|
|
uint32_t ind = pool->sparse[id];
|
2022-12-18 12:11:21 +00:00
|
|
|
component_t *c = ®istry->components.a[comp];
|
2022-12-13 04:36:51 +00:00
|
|
|
if (ind < pool->count && pool->dense[ind] == ent) {
|
|
|
|
uint32_t last = pool->count - 1;
|
2023-03-05 12:29:26 +00:00
|
|
|
// invalidate the entity for this component to prevent the component
|
|
|
|
// being double-removed due to deletion of the component resulting
|
|
|
|
// in the entity being deleted (happens with hierarchies)
|
|
|
|
pool->dense[ind] = -1;
|
2022-12-20 01:59:26 +00:00
|
|
|
Component_DestroyElements (c, pool->data, ind, 1);
|
2023-03-05 07:10:09 +00:00
|
|
|
uint32_t range_count = subpool->num_ranges - subpool->available;
|
|
|
|
// if ind >= the last range, then it is outside the subpools
|
|
|
|
if (range_count && ind < subpool->ranges[range_count - 1]) {
|
2022-12-20 08:56:08 +00:00
|
|
|
uint32_t *range = find_range (subpool, ind);
|
2022-12-18 12:11:21 +00:00
|
|
|
while (range - subpool->ranges < range_count) {
|
2022-12-20 08:56:08 +00:00
|
|
|
uint32_t end = --*range;
|
2022-12-18 12:11:21 +00:00
|
|
|
range++;
|
2023-01-21 04:58:43 +00:00
|
|
|
if (ind < end) {
|
|
|
|
pool->sparse[Ent_Index (pool->dense[end])] = ind;
|
|
|
|
pool->dense[ind] = pool->dense[end];
|
|
|
|
Component_MoveElements (c, pool->data, ind, end, 1);
|
|
|
|
ind = end;
|
|
|
|
}
|
2022-12-18 12:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 04:36:51 +00:00
|
|
|
if (last > ind) {
|
|
|
|
pool->sparse[Ent_Index (pool->dense[last])] = ind;
|
|
|
|
pool->dense[ind] = pool->dense[last];
|
2022-12-18 12:11:21 +00:00
|
|
|
Component_MoveElements (c, pool->data, ind, last, 1);
|
2022-12-13 04:36:51 +00:00
|
|
|
}
|
|
|
|
pool->count--;
|
|
|
|
pool->sparse[id] = nullent;
|
|
|
|
}
|
|
|
|
}
|