Mostly bugfix update + empty components support and remove EntityID from Event structure

-empty components now take no memory, so flag components is now far better
-added test for critical bug
-fixed critical bug with adding/removing entities form inside events
-fixed small bug with TestRunner
-improve basic tests
-fixed betterC compilation on DMD
-remove EntityID form Event structure
-added "return" attribute to some functions
-moved some code from Tempalte side to actual implementation
-fixed bug with EntityTemplate copying
-commented out some possibliy unused code
-use code formatter
This commit is contained in:
Mergul 2020-05-27 17:03:44 +02:00
parent 15cd57dbcb
commit 6929f5a748
16 changed files with 988 additions and 544 deletions

View file

@ -126,7 +126,7 @@ export struct EntityManager
//if(info.components)Mallocator.dispose(info.components);
Mallocator.dispose(info);
}
}
foreach (UpdatePass* pass; passes)
{
@ -402,7 +402,7 @@ export struct EntityManager
Sys* data_system = cast(Sys*) data.system_pointer;
Type* event = cast(Type*) data.event;
data_system.handleEvent(gEM.getEntity(event.entity_id), *event);
data_system.handleEvent(data.entity, *event);
}
void setEventCallers(Sys)(ref System system)
@ -474,7 +474,7 @@ export struct EntityManager
{
//continue;
}
else
else
{
string name;
static if (isArray!MemberType)
@ -1018,8 +1018,7 @@ export struct EntityManager
static if (hasMember!(Sys.EntitiesData, "job_id"))
{
input_data.job_id = cast(typeof(input_data.job_id)) data
.job_id;
input_data.job_id = cast(typeof(input_data.job_id)) data.job_id;
}
//s.onUpdate(input_data);
@ -1052,8 +1051,7 @@ export struct EntityManager
static if (hasMember!(Sys.EntitiesData, "job_id"))
{
input_data.job_id = cast(typeof(input_data.job_id)) data
.job_id;
input_data.job_id = cast(typeof(input_data.job_id)) data.job_id;
}
(cast(typeof(&__traits(getOverloads, s,
@ -1146,7 +1144,8 @@ export struct EntityManager
foreach (iii, comp_info; components_info.readonlyDeps)
{
ushort comp = external_dependencies_map.get(cast(const (char)[]) comp_info.type, ushort.max);
ushort comp = external_dependencies_map.get(cast(const(char)[]) comp_info.type,
ushort.max);
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system \"" ~ Sys.stringof
@ -1156,7 +1155,7 @@ export struct EntityManager
~ "\" due to non existing dependency \"" ~ comp_info.type ~ "\".");
system.m_readonly_dependencies[iii] = comp;
}
foreach (iii, comp_info; components_info.writableDeps)
{
ushort comp = external_dependencies_map.get(cast(char[]) comp_info.type, ushort.max);
@ -1174,7 +1173,9 @@ export struct EntityManager
if (sys_id < systems.length)
{
systems[sys_id].disable();
if(systems[sys_id].m_destroy)(cast(void function(void*)) systems[sys_id].m_destroy)(systems[sys_id].m_system_pointer);
if (systems[sys_id].m_destroy)
(cast(void function(void*)) systems[sys_id].m_destroy)(
systems[sys_id].m_system_pointer);
if (system.m_create)
(cast(void function(void*)) system.m_create)(system.m_system_pointer);
@ -1236,7 +1237,7 @@ export struct EntityManager
export void registerDependency(const(char)[] name)
{
return external_dependencies_map.add(name, cast(ushort)external_dependencies_map.length);
return external_dependencies_map.add(name, cast(ushort) external_dependencies_map.length);
}
/************************************************************************************************************************
@ -1274,7 +1275,10 @@ export struct EntityManager
info.create_callback = &callCreate;
}
info.size = Comp.sizeof;
static if (Comp.sizeof == 1 && Fields!(Comp).length == 0)
info.size = 0;
else
info.size = Comp.sizeof;
info.alignment = Comp.alignof; //8;
info.init_data = Mallocator.makeArray!ubyte(Comp.sizeof);
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
@ -1340,7 +1344,9 @@ export struct EntityManager
"Can't call function with system which hasn't EntitesData structure.");
static assert(__traits(hasMember, Sys, "onUpdate"),
"Can't call function with system which hasn't onUpdate function callback.");
static assert(is(SetFunctionAttributes!(T,functionLinkage!(s.onUpdate), functionAttributes!(s.onUpdate)) == typeof(&s.onUpdate)), "Function must match system update function.");
static assert(is(SetFunctionAttributes!(T, functionLinkage!(s.onUpdate),
functionAttributes!(s.onUpdate)) == typeof(&s.onUpdate)),
"Function must match system update function.");
static assert(__traits(hasMember, Sys, "system_id"), "Sys must be system type.");
System* system = getSystem(Sys.system_id);
@ -1612,6 +1618,8 @@ export struct EntityManager
//fill components with default data
foreach (comp; info.components)
{
if (components[comp].size == 0)
continue;
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
components[comp].init_data.ptr, components[comp].size);
}
@ -1621,6 +1629,8 @@ export struct EntityManager
ushort index = block.entityIndex(entity);
foreach (comp; info.components)
{
if (components[comp].size == 0)
continue;
memcpy(cast(void*) temp.entity_data.ptr + info.tmpl_deltas[comp],
cast(void*) block + info.deltas[comp] + components[comp].size * index,
components[comp].size);
@ -1669,6 +1679,8 @@ export struct EntityManager
//fill components with default data
foreach (comp; info.components)
{
if (components[comp].size == 0)
continue;
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
components[comp].init_data.ptr, components[comp].size);
}
@ -1736,14 +1748,19 @@ export struct EntityManager
//fill components with default data and copy from base template
foreach (comp; info.components)
{
if (comp < base_tmpl.info.deltas.length && base_tmpl.info.deltas[comp] != ushort.max) //copy data from base component
{
if (comp < base_tmpl.info.tmpl_deltas.length
&& base_tmpl.info.tmpl_deltas[comp] != ushort.max) //copy data from base component
{
if (components[comp].size == 0)
continue;
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
base_tmpl.entity_data.ptr + base_tmpl.info.tmpl_deltas[comp],
components[comp].size);
}
else //fill with default data
{
if (components[comp].size == 0)
continue;
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
components[comp].init_data.ptr, components[comp].size);
}
@ -1833,8 +1850,8 @@ export struct EntityManager
}
info.comp_add_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length);
info.comp_rem_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length,
info);
//info.comp_rem_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length);
info.comp_rem_info = Mallocator.makeArray!(EntityInfo*)(info.deltas.length);
foreach (comp; info.components)
{
@ -1894,11 +1911,11 @@ export struct EntityManager
}
add_len++;
//move elements after new listener
if(add_len < tmp_add.length)
for (int k = add_len; k > j; k--)
{
tmp_add[k] = tmp_add[k - 1];
}
if (add_len < tmp_add.length)
for (int k = add_len; k > j; k--)
{
tmp_add[k] = tmp_add[k - 1];
}
//assign listener
tmp_add[j] = cast(ushort) i;
}
@ -1914,11 +1931,11 @@ export struct EntityManager
}
rem_len++;
//move elements after new listener
if(rem_len < tmp_add.length)
for (int k = rem_len; k > j; k--)
{
tmp_rem[k] = tmp_rem[k - 1];
}
if (rem_len < tmp_add.length)
for (int k = rem_len; k > j; k--)
{
tmp_rem[k] = tmp_rem[k - 1];
}
//assign listener
tmp_rem[j] = cast(ushort) i;
}
@ -1934,11 +1951,11 @@ export struct EntityManager
}
ch_len++;
//move elements after new listener
if(ch_len < tmp_add.length)
for (int k = ch_len; k > j; k--)
{
tmp_ch[k] = tmp_ch[k - 1];
}
if (ch_len < tmp_add.length)
for (int k = ch_len; k > j; k--)
{
tmp_ch[k] = tmp_ch[k - 1];
}
//assign listener
tmp_ch[j] = cast(ushort) i;
}
@ -2160,6 +2177,8 @@ export struct EntityManager
foreach (comp; new_info.components)
{
uint comp_size = components[comp].size;
if (comp_size == 0)
continue;
memcpy(cast(void*) new_block + new_info.deltas[comp] + new_block.entities_count * comp_size,
cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size);
}
@ -2296,24 +2315,29 @@ export struct EntityManager
foreach (id; new_info.components) //ids[0 .. len])
{
void* dst = cast(void*) new_block + new_info.deltas[id] + (
new_block.entities_count) * components[id].size;
uint size = components[id].size;
void* dst = void;
if (size != 0)
dst = cast(void*) new_block + new_info.deltas[id] + (new_block.entities_count)
* size;
if (k >= new_ids.length)
{
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
if (size != 0)
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
else if (j >= info.components.length || id == new_ids[k])
{
memcpy(dst, data_pointers[k], size);
if (size != 0)
memcpy(dst, data_pointers[k], size);
k++;
}
else
{
assert(id != new_ids[0]);
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
if (size != 0)
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
}
@ -2376,28 +2400,30 @@ export struct EntityManager
//__addComponents(entity_id, new_ids, pointers);
ComponentRef[num] _comps;
static foreach(i, comp; comps)
static foreach (i, comp; comps)
{
_comps[i] = comp.ref_;
}
addComponents(entity_id, _comps);
}
export void addComponents(const EntityID entity_id, ComponentRef[] comps) nothrow @nogc
{
uint num = cast(uint)comps.length;
uint num = cast(uint) comps.length;
ThreadData* data = &threads[threadID];
data.changeEntitiesList.add(cast(ubyte) 1u);
data.changeEntitiesList.add((cast(ubyte*)&entity_id)[0 .. EntityID.sizeof]);
data.changeEntitiesList.add((cast(ubyte*)&num)[0 .. uint.sizeof]);
foreach(ref_; comps)
foreach (ref_; comps)
{
data.changeEntitiesList.add((cast(ubyte*)&ref_.component_id)[0 .. ushort.sizeof]);
}
foreach(ref_; comps)
foreach (ref_; comps)
{
data.changeEntitiesList.add((cast(ubyte*)ref_.ptr)[0 .. components[ref_.component_id].size]);
if (components[ref_.component_id].size != 0)
data.changeEntitiesList.add(
(cast(ubyte*) ref_.ptr)[0 .. components[ref_.component_id].size]);
}
/*data.changeEntitiesList.add(cast(ubyte[]) new_ids);
static foreach (i, comp; comps)
@ -2449,14 +2475,15 @@ export struct EntityManager
foreach (i, comp; info.components)
{
memcpy(cast(void*) new_block + info.deltas[comp] + components[comp].size * new_id,
cast(void*) block + info.deltas[comp] + components[comp].size * index,
components[comp].size);
ushort size = components[comp].size;
if (size != 0)
memcpy(cast(void*) new_block + info.deltas[comp] + size * new_id,
cast(void*) block + info.deltas[comp] + size * index, size);
if (components[comp].create_callback)
{
components[comp].create_callback(cast(
void*) block + info.deltas[comp] + new_id * components[comp].size);
components[comp].create_callback(
cast(void*) block + info.deltas[comp] + new_id * size);
}
}
@ -2552,20 +2579,21 @@ export struct EntityManager
foreach (comp; info.components)
{
uint size = components[comp].size;
memcpy(cast(void*) block + info.deltas[comp] + size * id,
tmpl.entity_data.ptr + info.tmpl_deltas[comp], size);
if (size != 0)
memcpy(cast(void*) block + info.deltas[comp] + size * id,
tmpl.entity_data.ptr + info.tmpl_deltas[comp], size);
}
foreach(comp; replacement)
foreach (comp; replacement)
{
if(comp.component_id < info.deltas.length)
if (comp.component_id < info.deltas.length)
{
ushort delta = info.deltas[comp.component_id];
if(delta != ushort.max)
if (delta != ushort.max)
{
uint size = components[comp.component_id].size;
memcpy(cast(void*) block + delta + size * id,
comp.ptr, size);
if (size != 0)
memcpy(cast(void*) block + delta + size * id, comp.ptr, size);
}
}
}
@ -2678,8 +2706,10 @@ export struct EntityManager
{
//get entity and block meta data pointers
Entity* entity = id_manager.getEntityPointer(id);
if (entity is null)
return; //return if entity doesn't exist
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
@ -2700,6 +2730,9 @@ export struct EntityManager
{
EntityInfo* info = block.type_info;
if (info.last_block.added_count)
updateBlock(info.last_block);
info.last_block.entities_count--;
uint pos = block.entityIndex(entity);
@ -2720,9 +2753,11 @@ export struct EntityManager
{
foreach (comp; info.components)
{
uint size = components[comp].size;
if (size == 0)
continue;
void* src = cast(void*) info.last_block + info.deltas[comp];
void* dst = cast(void*) block + info.deltas[comp];
uint size = components[comp].size;
memcpy(dst + pos * size, src + info.last_block.entities_count * size, size);
}
@ -2768,7 +2803,8 @@ export struct EntityManager
{
uint index = 0;
uint len = cast(uint) thread.changeEntitiesListPrev.length;
if(len)has_work = true;
if (len)
has_work = true;
void*[32] pointers; // = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
while (index < len)
{
@ -2798,7 +2834,7 @@ export struct EntityManager
pointers[i] = &thread.changeEntitiesListPrev[index];
index += components[ids[i]].size;
}
__addComponents(id, ids, pointers[0 .. num]);
}
}
@ -2885,6 +2921,23 @@ export struct EntityManager
(cast(void function(ref ListenerCallData) nothrow @nogc) system.m_change_entity)(data);
}
private void updateBlock(EntitiesBlock* block) @nogc nothrow
{
EntityInfo* info = block.type_info;
ushort entities_count = block.entities_count;
block.entities_count += block.added_count;
if (block.entities_count > block.type_info.max_entities)
{
block.entities_count = block.type_info.max_entities;
}
block.added_count.atomicStore(cast(ushort) 0);
if (info.add_listeners)
{
callAddEntityListeners(info, block, entities_count, block.entities_count);
}
}
private bool updateBlocks()
{
bool has_work = false;
@ -2892,22 +2945,11 @@ export struct EntityManager
foreach (ref ThreadData thread; threads)
{
//thread.swapToUpdate();
if(thread.blockToUpdatePrev.length)has_work = true;
if (thread.blockToUpdatePrev.length)
has_work = true;
foreach (block; thread.blockToUpdatePrev)
{
EntityInfo* info = block.type_info;
ushort entities_count = block.entities_count;
block.entities_count += block.added_count;
if (block.entities_count > block.type_info.max_entities)
{
block.entities_count = block.type_info.max_entities;
}
block.added_count.atomicStore(cast(ushort) 0);
if (info.add_listeners)
{
callAddEntityListeners(info, block, entities_count, block.entities_count);
}
updateBlock(block);
}
thread.blockToUpdatePrev.clear();
}
@ -2920,7 +2962,8 @@ export struct EntityManager
//foreach (ref ThreadData thread; threads)thread.swapToRemove();
foreach (ref ThreadData thread; threads)
{
if(thread.entitiesToRemovePrev.length)has_work = true;
if (thread.entitiesToRemovePrev.length)
has_work = true;
foreach (id; thread.entitiesToRemovePrev)
{
__removeEntity(id);
@ -2936,59 +2979,60 @@ export struct EntityManager
// bool empty = true;
//while (1)
//{
//event_manager.swapCurrent();
uint current_index;
if (event_manager.current_index == 0)
current_index = cast(uint) threads.length;
else
current_index = 0;
foreach (i, event; event_manager.events)
//event_manager.swapCurrent();
uint current_index;
if (event_manager.current_index == 0)
current_index = cast(uint) threads.length;
else
current_index = 0;
foreach (i, event; event_manager.events)
{
foreach (first_block; event.first_blocks[current_index .. current_index + threads
.length])
{
foreach (first_block; event.first_blocks[current_index
.. current_index + threads.length])
EventManager.EventBlock* block = first_block;
if (block)
has_work = true;
// {
// has_work = true;
// //empty = false;
// }
while (block)
{
EventManager.EventBlock* block = first_block;
if (block)has_work = true;
// {
// has_work = true;
// //empty = false;
// }
while (block)
EventCallData call_data;
void* event_pointer = cast(void*) block + event.data_offset;
foreach (j; 0 .. block.count)
{
EventCallData call_data;
void* event_pointer = cast(void*) block + event.data_offset;
foreach (j; 0 .. block.count)
call_data.event = event_pointer + EntityID.sizeof;
EntityID entity_id = *cast(EntityID*)(event_pointer);
Entity* entity = id_manager.getEntityPointer(entity_id);
if (entity)
{
call_data.event = event_pointer;
EntityID entity_id = *cast(EntityID*) event_pointer;
Entity* entity = id_manager.getEntityPointer(entity_id);
if (entity)
{
call_data.block = getMetaData(entity);
call_data.id = call_data.block.entityIndex(entity);
call_data.block = getMetaData(entity);
call_data.id = call_data.block.entityIndex(entity);
call_data.entity = entity;
foreach (caller; events[i].callers)
{
if (
call_data.block.type_info.systems[caller.system.m_id]
== false)
continue;
call_data.system_pointer = caller.system.m_system_pointer;
(cast(void function(
ref EventCallData) nothrow @nogc) caller.callback)(
call_data);
}
foreach (caller; events[i].callers)
{
if (call_data.block.type_info.systems[caller.system.m_id] == false
|| !caller.system.enabled || !caller.system.willExecute)
continue;
call_data.system_pointer = caller.system.m_system_pointer;
(cast(void function(ref EventCallData) nothrow @nogc) caller
.callback)(call_data);
}
if(events[i].destroy_callback)events[i].destroy_callback(event_pointer);
event_pointer += events[i].size;
}
block = block.next;
if (events[i].destroy_callback)
events[i].destroy_callback(event_pointer);
event_pointer += events[i].size + EntityID.sizeof;
}
block = block.next;
}
}
// if (empty)
// break;
// empty = true;
}
// if (empty)
// break;
// empty = true;
//}
return has_work;
}
@ -2996,7 +3040,7 @@ export struct EntityManager
private void swapData() nothrow @nogc
{
event_manager.swapCurrent();
foreach(ref ThreadData thread; threads)
foreach (ref ThreadData thread; threads)
{
thread.swapData();
}
@ -3005,13 +3049,13 @@ export struct EntityManager
export void commit()
{
bool has_work = true;
while(has_work)
{
while (has_work)
{
swapData();
has_work = false;
has_work |= updateEvents();
id_manager.optimize();
has_work |= updateBlocks();
has_work |= changeEntities();
@ -3222,14 +3266,15 @@ export struct EntityManager
}
}
const (UpdatePass)* getPass(const (char)[] name)
const(UpdatePass)* getPass(const(char)[] name)
{
ushort id = getPassID(name);
if(id == ushort.max)return null;
if (id == ushort.max)
return null;
return passes[id];
}
ushort getPassID(const (char)[] name)
ushort getPassID(const(char)[] name)
{
return passes_map.get(name, ushort.max);
}
@ -3267,6 +3312,7 @@ export struct EntityManager
EntitiesBlock* block;
void* system_pointer;
void* event;
Entity* entity;
ushort id;
}
@ -3357,9 +3403,9 @@ export struct EntityManager
return new_info;
}
EntityInfo* getNewInfoRemove(ushort id)
EntityInfo* getNewInfoRemove(ushort id) return
{
if (comp_rem_info.length <= id)
/*if (comp_rem_info.length <= id)
{
EntityInfo*[] new_infos = Mallocator.makeArray!(EntityInfo*)(
instance.components.length, &this);
@ -3371,7 +3417,7 @@ export struct EntityManager
Mallocator.dispose(comp_rem_info);
}
comp_rem_info = new_infos;
}
}*/
if (comp_rem_info[id])
return comp_rem_info[id];
@ -3386,8 +3432,9 @@ export struct EntityManager
ids[len++] = comp;
}
}
if (len == components.length)
return &this;
assert(len != components.length);
//if (len == components.length)
// return &this;
assert(len == components.length - 1);
@ -3455,23 +3502,14 @@ export struct EntityManager
*/
struct EntitiesBlock
{
///return distance (in bytes) from begin of block to data
///TODO: probably to remove. It's used by old code if I remeber correctly.
/*export uint dataDelta() nothrow @nogc pure
{
ushort dif = EntitiesBlock.sizeof;
alignNum(dif, type_info.alignment);
return dif;
}*/
///return pointer to first element in block
export void* dataBegin() nothrow @nogc pure
export void* dataBegin() nothrow @nogc pure return
{
ushort dif = EntitiesBlock.sizeof;
return cast(void*)&this + dif;
}
export ushort entityIndex(Entity* entity) nothrow @nogc pure
export ushort entityIndex(const(Entity)* entity) nothrow @nogc pure
{
static if (EntityID.sizeof == 8)
return cast(ushort)((cast(void*) entity - dataBegin()) >> 3);
@ -3593,32 +3631,32 @@ export struct EntityManager
struct ThreadData
{
ref Vector!EntityID entitesToRemove() @nogc nothrow
ref Vector!EntityID entitesToRemove() @nogc nothrow return
{
return entities_to_remove[data_index];
}
ref SimpleVector changeEntitiesList() @nogc nothrow
ref SimpleVector changeEntitiesList() @nogc nothrow return
{
return change_entities_list[data_index];
}
ref Vector!(EntitiesBlock*) blockToUpdate() @nogc nothrow
ref Vector!(EntitiesBlock*) blockToUpdate() @nogc nothrow return
{
return blocks_to_update[data_index];
}
ref Vector!EntityID entitiesToRemovePrev() @nogc nothrow
ref Vector!EntityID entitiesToRemovePrev() @nogc nothrow return
{
return entities_to_remove[1 - data_index];
}
ref SimpleVector changeEntitiesListPrev() @nogc nothrow
ref SimpleVector changeEntitiesListPrev() @nogc nothrow return
{
return change_entities_list[1 - data_index];
}
ref Vector!(EntitiesBlock*) blockToUpdatePrev() @nogc nothrow
ref Vector!(EntitiesBlock*) blockToUpdatePrev() @nogc nothrow return
{
return blocks_to_update[1 - data_index];
}