-formatted with dfmt
-cleaned up code
This commit is contained in:
parent
5dd24b6462
commit
6e2c00f608
1 changed files with 79 additions and 116 deletions
|
|
@ -42,7 +42,7 @@ class EntityManager
|
|||
foreach (ref system; instance.systems)
|
||||
{
|
||||
if (system.m_destroy)
|
||||
(cast(void function(void*))system.m_destroy)(system.m_system_pointer);
|
||||
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
|
||||
}
|
||||
|
||||
Mallocator.instance.dispose(instance);
|
||||
|
|
@ -54,7 +54,8 @@ class EntityManager
|
|||
*/
|
||||
this(uint threads_count)
|
||||
{
|
||||
if(threads_count == 0)threads_count = 0;
|
||||
if (threads_count == 0)
|
||||
threads_count = 0;
|
||||
threads = Mallocator.instance.makeArray!ThreadData(threads_count);
|
||||
//event_manager = EventManager(this);
|
||||
//event_manager.manager = this;
|
||||
|
|
@ -278,7 +279,8 @@ class EntityManager
|
|||
}
|
||||
else if (member == "length")
|
||||
{
|
||||
ret ~= "input_data." ~ member ~ " = cast(typeof(input_data.length))(entities_count - offset);";
|
||||
ret ~= "input_data." ~ member
|
||||
~ " = cast(typeof(input_data.length))(entities_count - offset);";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -350,7 +352,8 @@ class EntityManager
|
|||
{
|
||||
if (data.end)
|
||||
entities_count = data.end;
|
||||
else entities_count = block.entities_count;
|
||||
else
|
||||
entities_count = block.entities_count;
|
||||
}
|
||||
else
|
||||
entities_count = block.entities_count;
|
||||
|
|
@ -412,7 +415,7 @@ class EntityManager
|
|||
/*if (systems[sys_id].m_destroy)
|
||||
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);
|
||||
(cast(void function(void*)) system.m_create)(system.m_system_pointer);
|
||||
|
||||
systems[sys_id] = system;
|
||||
Sys.system_id = sys_id;
|
||||
|
|
@ -425,7 +428,7 @@ class EntityManager
|
|||
systems.add(system);
|
||||
|
||||
if (system.m_create)
|
||||
(cast(void function(void*))system.m_create)(system.m_system_pointer);
|
||||
(cast(void function(void*)) system.m_create)(system.m_system_pointer);
|
||||
|
||||
systems[$ - 1].enable();
|
||||
|
||||
|
|
@ -610,12 +613,13 @@ class EntityManager
|
|||
foreach (info; caller.infos)
|
||||
{
|
||||
uint blocks_count = info.blocksCount();
|
||||
if(blocks_count == 0)continue;
|
||||
if (blocks_count == 0)
|
||||
continue;
|
||||
if (blocks_count > 1)
|
||||
entities_count += (blocks_count - 1) * info.max_entities;
|
||||
entities_count += info.last_block.entities_count;
|
||||
}
|
||||
|
||||
|
||||
if (!entities_count)
|
||||
continue;
|
||||
|
||||
|
|
@ -625,7 +629,8 @@ class EntityManager
|
|||
if (entities_per_job <= 4)
|
||||
{
|
||||
jobs_count = entities_count / 4;
|
||||
if(jobs_count == 0)jobs_count = 1;
|
||||
if (jobs_count == 0)
|
||||
jobs_count = 1;
|
||||
entities_per_job = entities_count / jobs_count + 1;
|
||||
}
|
||||
|
||||
|
|
@ -634,8 +639,9 @@ class EntityManager
|
|||
|
||||
void nextJob()
|
||||
{
|
||||
CallData[] callers = m_call_data_allocator.getCallData(cast(uint)tmp_datas.length);
|
||||
callers[0..$] = tmp_datas[0..$];
|
||||
CallData[] callers = m_call_data_allocator.getCallData(
|
||||
cast(uint) tmp_datas.length);
|
||||
callers[0 .. $] = tmp_datas[0 .. $];
|
||||
tmp_datas.clear();
|
||||
sys.jobs[job_id].callers = callers;
|
||||
job_id++;
|
||||
|
|
@ -653,20 +659,21 @@ class EntityManager
|
|||
if ((blocks_count - 1) * info.max_entities + entities_count
|
||||
+ info.last_block.entities_count - first_elem >= entities_per_job)
|
||||
{
|
||||
int reamaining_entities = (entities_per_job - entities_count - (first_block.entities_count - first_elem));
|
||||
if(reamaining_entities >= 0)
|
||||
int reamaining_entities = (entities_per_job - entities_count - (
|
||||
first_block.entities_count - first_elem));
|
||||
if (reamaining_entities >= 0)
|
||||
{
|
||||
int full_blocks_count = reamaining_entities / info.max_entities;
|
||||
EntitiesBlock* block = first_block;
|
||||
foreach (i; 0 .. full_blocks_count + 1)
|
||||
block = block.next_block;
|
||||
|
||||
if (
|
||||
full_blocks_count * info.max_entities
|
||||
+ entities_count + (first_block.entities_count - first_elem) >= entities_per_job)
|
||||
if (full_blocks_count * info.max_entities + entities_count + (
|
||||
first_block.entities_count - first_elem) >= entities_per_job)
|
||||
{
|
||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
||||
cast(ushort) (full_blocks_count + 1), cast(ushort) first_elem, 0);
|
||||
cast(ushort)(full_blocks_count + 1),
|
||||
cast(ushort) first_elem, 0);
|
||||
tmp_datas.add(data);
|
||||
first_elem = 0;
|
||||
blocks_count -= full_blocks_count + 1;
|
||||
|
|
@ -674,10 +681,11 @@ class EntityManager
|
|||
}
|
||||
else
|
||||
{
|
||||
entities_count += full_blocks_count * info.max_entities + (first_block.entities_count - first_elem);// - first_elem;
|
||||
uint last_elem = entities_per_job - entities_count;// + first_elem - 1;
|
||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
||||
cast(ushort)(full_blocks_count + 2),
|
||||
entities_count += full_blocks_count * info.max_entities + (
|
||||
first_block.entities_count - first_elem); // - first_elem;
|
||||
uint last_elem = entities_per_job - entities_count; // + first_elem - 1;
|
||||
CallData data = CallData(caller.system_id, sys, info,
|
||||
first_block, cast(ushort)(full_blocks_count + 2),
|
||||
cast(ushort) first_elem, cast(ushort) last_elem);
|
||||
tmp_datas.add(data);
|
||||
first_elem = last_elem;
|
||||
|
|
@ -689,8 +697,8 @@ class EntityManager
|
|||
else
|
||||
{
|
||||
uint last_elem = entities_per_job - entities_count;
|
||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
||||
1, cast(ushort) first_elem, cast(ushort)(first_elem + last_elem));
|
||||
CallData data = CallData(caller.system_id, sys, info, first_block, 1,
|
||||
cast(ushort) first_elem, cast(ushort)(first_elem + last_elem));
|
||||
tmp_datas.add(data);
|
||||
first_elem += last_elem;
|
||||
assert(first_elem <= first_block.entities_count);
|
||||
|
|
@ -704,13 +712,13 @@ class EntityManager
|
|||
CallData data = CallData(caller.system_id, sys, info,
|
||||
first_block, cast(ushort) blocks_count, cast(ushort) first_elem);
|
||||
tmp_datas.add(data);
|
||||
entities_count += (blocks_count - 1)
|
||||
* info.max_entities + info.last_block.entities_count - first_elem;
|
||||
entities_count += (blocks_count - 1) * info.max_entities
|
||||
+ info.last_block.entities_count - first_elem;
|
||||
}
|
||||
}
|
||||
nextJob();
|
||||
|
||||
m_dispatch_jobs(sys.jobs[0..job_id]);
|
||||
m_dispatch_jobs(sys.jobs[0 .. job_id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -920,7 +928,8 @@ class EntityManager
|
|||
break;
|
||||
}
|
||||
|
||||
if(index < system_callers.length)system_callers[index].infos.add(&entity);
|
||||
if (index < system_callers.length)
|
||||
system_callers[index].infos.add(&entity);
|
||||
/*for (; index < entity.callers.length; index++)
|
||||
{
|
||||
CallData* caller = &entity.callers[index];
|
||||
|
|
@ -1130,12 +1139,13 @@ class EntityManager
|
|||
uint k = 0;
|
||||
uint len = 0;
|
||||
//foreach (ref id; ids)
|
||||
for(;len<ids.length;len++)
|
||||
for (; len < ids.length; len++)
|
||||
{
|
||||
ushort* id = &ids[len];
|
||||
if (k >= new_ids.length)
|
||||
{
|
||||
if(j >= info.components.length)break;
|
||||
if (j >= info.components.length)
|
||||
break;
|
||||
*id = info.components[j++];
|
||||
//continue;
|
||||
}
|
||||
|
|
@ -1144,7 +1154,7 @@ class EntityManager
|
|||
*id = new_ids[k++];
|
||||
//continue;
|
||||
}
|
||||
else if(new_ids[k] == info.components[j])
|
||||
else if (new_ids[k] == info.components[j])
|
||||
{
|
||||
*id = info.components[j++];
|
||||
k++;
|
||||
|
|
@ -1158,9 +1168,10 @@ class EntityManager
|
|||
else
|
||||
*id = info.components[j++];
|
||||
}
|
||||
if(len == info.components.length)return;
|
||||
if (len == info.components.length)
|
||||
return;
|
||||
|
||||
EntityInfo* new_info = getEntityInfo(ids[0..len]);
|
||||
EntityInfo* new_info = getEntityInfo(ids[0 .. len]);
|
||||
|
||||
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
||||
|
||||
|
|
@ -1176,10 +1187,10 @@ class EntityManager
|
|||
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
|
||||
else
|
||||
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
|
||||
foreach (ref id; ids[0..len])
|
||||
foreach (ref id; ids[0 .. len])
|
||||
{
|
||||
void* dst = cast(void*) new_block + new_info.deltas[id] + (
|
||||
new_block.entities_count /*+ new_block.added_count*/) * components[id].size;
|
||||
new_block.entities_count /*+ new_block.added_count*/ ) * components[id].size;
|
||||
uint size = components[id].size;
|
||||
if (k >= new_ids.length)
|
||||
{
|
||||
|
|
@ -1270,59 +1281,41 @@ class EntityManager
|
|||
{
|
||||
EntityInfo* info = tmpl.info;
|
||||
EntitiesBlock* last_block = info.last_block;
|
||||
|
||||
ushort index = 0;
|
||||
if(last_block)/*index = */
|
||||
{
|
||||
//index = last_block.added_count;
|
||||
index = last_block.added_count.atomicOp!"+="(1);
|
||||
}
|
||||
EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info,index);
|
||||
if(block != last_block)
|
||||
{
|
||||
//index = block.added_count;
|
||||
/*if(last_block)
|
||||
{
|
||||
//last_block.added_count.atomicOp!"-="(1);
|
||||
last_block.added_count = cast(ushort)(info.max_entities - last_block.entities_count);
|
||||
}*/
|
||||
index = block.added_count.atomicOp!"+="(1);
|
||||
}
|
||||
|
||||
//index--;
|
||||
//index = cast(ushort)(block.added_count - 1);
|
||||
uint id = (block.entities_count + index - 1);//block.added_count);
|
||||
//EntityInfo* info = tmpl.info;
|
||||
if (last_block)index = last_block.added_count.atomicOp!"+="(1);
|
||||
EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info, index);
|
||||
if (block != last_block)index = block.added_count.atomicOp!"+="(1);
|
||||
|
||||
uint id = (block.entities_count + index - 1); //block.added_count);
|
||||
|
||||
void* data_begin = block.dataBegin();
|
||||
void* start = data_begin + EntityID.sizeof * id;
|
||||
//memcpy(data_begin + EntityID.sizeof * id, tmpl.entity_data.ptr, EntityID.sizeof);
|
||||
|
||||
foreach (i, comp; info.components)
|
||||
{
|
||||
memcpy(cast(void*) block + info.deltas[comp] + components[comp].size * id,
|
||||
tmpl.entity_data.ptr + info.tmpl_deltas[comp], components[comp].size);
|
||||
}
|
||||
|
||||
//if (!block.added_count)
|
||||
//if(block.added_count == 1)
|
||||
if(index == 1)
|
||||
if (index == 1)
|
||||
blocks_to_update.add(block);
|
||||
|
||||
Entity* entity = cast(Entity*) start;
|
||||
entity.id = id_manager.getNewID();
|
||||
entity.updateID();
|
||||
//block.added_count++;
|
||||
//import core.atomic;
|
||||
|
||||
//block.entities_count++;
|
||||
|
||||
return *entity;
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
*Return block with free space for selected EntityInfo.
|
||||
*/
|
||||
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info, uint new_index = 1)
|
||||
{
|
||||
//EntitiesBlock* previous_block;
|
||||
EntitiesBlock* block = info.last_block;
|
||||
|
||||
if(block is null)
|
||||
if (block is null)
|
||||
{
|
||||
block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
||||
*block = EntitiesBlock(info);
|
||||
|
|
@ -1330,7 +1323,7 @@ class EntityManager
|
|||
info.first_block = block;
|
||||
info.last_block = block;
|
||||
}
|
||||
else if(block.entities_count + new_index/*block.added_count*/ > info.max_entities)
|
||||
else if (block.entities_count + new_index /*block.added_count*/ > info.max_entities)
|
||||
{
|
||||
EntitiesBlock* new_block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
||||
*new_block = EntitiesBlock(info);
|
||||
|
|
@ -1339,38 +1332,7 @@ class EntityManager
|
|||
new_block.id = cast(ushort)(block.id + 1);
|
||||
block = new_block;
|
||||
info.last_block = block;
|
||||
}//*/
|
||||
/*while (1)
|
||||
{
|
||||
if (block is null)
|
||||
{
|
||||
block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
||||
*block = EntitiesBlock(info);
|
||||
if (previous_block is null)
|
||||
{
|
||||
info.first_block = block;
|
||||
block.id = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
previous_block.next_block = block;
|
||||
block.prev_block = previous_block;
|
||||
block.id = cast(ushort)(previous_block.id + 1);
|
||||
}
|
||||
info.last_block = block;
|
||||
break; // new block certainly has free space
|
||||
}
|
||||
// check if there is enought space
|
||||
if (block.entities_count + block.added_count >= block.type_info.max_entities)
|
||||
{
|
||||
previous_block = block;
|
||||
block = block.next_block;
|
||||
continue;
|
||||
}
|
||||
|
||||
break; // block exists and bounds check passed
|
||||
}//*/
|
||||
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
|
|
@ -1413,12 +1375,10 @@ class EntityManager
|
|||
|
||||
if (call_destructors)
|
||||
{
|
||||
//void* data = data_begin + pos * info.size;
|
||||
foreach (comp; info.components)
|
||||
{
|
||||
if (components[comp].destroy_callback)
|
||||
{
|
||||
//components[comp].destroy_callback(data + info.deltas[comp]);
|
||||
components[comp].destroy_callback(cast(
|
||||
void*) block + info.deltas[comp] + pos * components[comp].size);
|
||||
}
|
||||
|
|
@ -1471,7 +1431,7 @@ class EntityManager
|
|||
|
||||
private void changeEntites()
|
||||
{
|
||||
foreach(ref thread;threads)
|
||||
foreach (ref thread; threads)
|
||||
{
|
||||
uint index = 0;
|
||||
uint len = cast(uint) thread.change_entities_list.length;
|
||||
|
|
@ -1515,19 +1475,19 @@ class EntityManager
|
|||
foreach (block; blocks_to_update)
|
||||
{
|
||||
block.entities_count += block.added_count;
|
||||
if(block.entities_count > block.type_info.max_entities)
|
||||
if (block.entities_count > block.type_info.max_entities)
|
||||
{
|
||||
block.entities_count = block.type_info.max_entities;
|
||||
}
|
||||
//block.added_count = 0;
|
||||
block.added_count.atomicStore(cast(ushort)0);
|
||||
block.added_count.atomicStore(cast(ushort) 0);
|
||||
}
|
||||
blocks_to_update.clear();
|
||||
}
|
||||
|
||||
private void removeEntities()
|
||||
{
|
||||
foreach(i,ref thread;threads)
|
||||
foreach (i, ref thread; threads)
|
||||
{
|
||||
foreach (id; thread.entities_to_remove)
|
||||
{
|
||||
|
|
@ -1556,7 +1516,7 @@ class EntityManager
|
|||
foreach (ref system; instance.systems)
|
||||
{
|
||||
if (system.m_begin)
|
||||
(cast(void function(void*))system.m_begin)(system.m_system_pointer);
|
||||
(cast(void function(void*)) system.m_begin)(system.m_system_pointer);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
|
@ -1575,7 +1535,7 @@ class EntityManager
|
|||
foreach (ref system; instance.systems)
|
||||
{
|
||||
if (system.m_end)
|
||||
(cast(void function(void*))system.m_end)(system.m_system_pointer);
|
||||
(cast(void function(void*)) system.m_end)(system.m_system_pointer);
|
||||
}
|
||||
//}
|
||||
|
||||
|
|
@ -1588,8 +1548,10 @@ class EntityManager
|
|||
|
||||
private void getThreadID()
|
||||
{
|
||||
if(m_thread_id_func)thread_id = m_thread_id_func();
|
||||
else thread_id = 0;
|
||||
if (m_thread_id_func)
|
||||
thread_id = m_thread_id_func();
|
||||
else
|
||||
thread_id = 0;
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
|
|
@ -1622,9 +1584,10 @@ class EntityManager
|
|||
///Returns number of blocks
|
||||
uint blocksCount()
|
||||
{
|
||||
if(last_block)
|
||||
return last_block.id + 1;
|
||||
else return 0;
|
||||
if (last_block)
|
||||
return last_block.id + 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
///entity components
|
||||
|
|
@ -1783,11 +1746,11 @@ class EntityManager
|
|||
|
||||
void clear()
|
||||
{
|
||||
if(blocks.length > 0)
|
||||
foreach (block; blocks[0 .. id + 1])
|
||||
{
|
||||
block.allocated = 0;
|
||||
}
|
||||
if (blocks.length > 0)
|
||||
foreach (block; blocks[0 .. id + 1])
|
||||
{
|
||||
block.allocated = 0;
|
||||
}
|
||||
id = 0;
|
||||
//blocks.clear();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue