-formatted with dfmt
-cleaned up code
This commit is contained in:
parent
5dd24b6462
commit
6e2c00f608
1 changed files with 79 additions and 116 deletions
|
|
@ -54,7 +54,8 @@ class EntityManager
|
||||||
*/
|
*/
|
||||||
this(uint threads_count)
|
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);
|
threads = Mallocator.instance.makeArray!ThreadData(threads_count);
|
||||||
//event_manager = EventManager(this);
|
//event_manager = EventManager(this);
|
||||||
//event_manager.manager = this;
|
//event_manager.manager = this;
|
||||||
|
|
@ -278,7 +279,8 @@ class EntityManager
|
||||||
}
|
}
|
||||||
else if (member == "length")
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -350,7 +352,8 @@ class EntityManager
|
||||||
{
|
{
|
||||||
if (data.end)
|
if (data.end)
|
||||||
entities_count = data.end;
|
entities_count = data.end;
|
||||||
else entities_count = block.entities_count;
|
else
|
||||||
|
entities_count = block.entities_count;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
entities_count = block.entities_count;
|
entities_count = block.entities_count;
|
||||||
|
|
@ -610,7 +613,8 @@ class EntityManager
|
||||||
foreach (info; caller.infos)
|
foreach (info; caller.infos)
|
||||||
{
|
{
|
||||||
uint blocks_count = info.blocksCount();
|
uint blocks_count = info.blocksCount();
|
||||||
if(blocks_count == 0)continue;
|
if (blocks_count == 0)
|
||||||
|
continue;
|
||||||
if (blocks_count > 1)
|
if (blocks_count > 1)
|
||||||
entities_count += (blocks_count - 1) * info.max_entities;
|
entities_count += (blocks_count - 1) * info.max_entities;
|
||||||
entities_count += info.last_block.entities_count;
|
entities_count += info.last_block.entities_count;
|
||||||
|
|
@ -625,7 +629,8 @@ class EntityManager
|
||||||
if (entities_per_job <= 4)
|
if (entities_per_job <= 4)
|
||||||
{
|
{
|
||||||
jobs_count = entities_count / 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;
|
entities_per_job = entities_count / jobs_count + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -634,7 +639,8 @@ class EntityManager
|
||||||
|
|
||||||
void nextJob()
|
void nextJob()
|
||||||
{
|
{
|
||||||
CallData[] callers = m_call_data_allocator.getCallData(cast(uint)tmp_datas.length);
|
CallData[] callers = m_call_data_allocator.getCallData(
|
||||||
|
cast(uint) tmp_datas.length);
|
||||||
callers[0 .. $] = tmp_datas[0 .. $];
|
callers[0 .. $] = tmp_datas[0 .. $];
|
||||||
tmp_datas.clear();
|
tmp_datas.clear();
|
||||||
sys.jobs[job_id].callers = callers;
|
sys.jobs[job_id].callers = callers;
|
||||||
|
|
@ -653,7 +659,8 @@ class EntityManager
|
||||||
if ((blocks_count - 1) * info.max_entities + entities_count
|
if ((blocks_count - 1) * info.max_entities + entities_count
|
||||||
+ info.last_block.entities_count - first_elem >= entities_per_job)
|
+ info.last_block.entities_count - first_elem >= entities_per_job)
|
||||||
{
|
{
|
||||||
int reamaining_entities = (entities_per_job - entities_count - (first_block.entities_count - first_elem));
|
int reamaining_entities = (entities_per_job - entities_count - (
|
||||||
|
first_block.entities_count - first_elem));
|
||||||
if (reamaining_entities >= 0)
|
if (reamaining_entities >= 0)
|
||||||
{
|
{
|
||||||
int full_blocks_count = reamaining_entities / info.max_entities;
|
int full_blocks_count = reamaining_entities / info.max_entities;
|
||||||
|
|
@ -661,12 +668,12 @@ class EntityManager
|
||||||
foreach (i; 0 .. full_blocks_count + 1)
|
foreach (i; 0 .. full_blocks_count + 1)
|
||||||
block = block.next_block;
|
block = block.next_block;
|
||||||
|
|
||||||
if (
|
if (full_blocks_count * info.max_entities + entities_count + (
|
||||||
full_blocks_count * info.max_entities
|
first_block.entities_count - first_elem) >= entities_per_job)
|
||||||
+ entities_count + (first_block.entities_count - first_elem) >= entities_per_job)
|
|
||||||
{
|
{
|
||||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
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);
|
tmp_datas.add(data);
|
||||||
first_elem = 0;
|
first_elem = 0;
|
||||||
blocks_count -= full_blocks_count + 1;
|
blocks_count -= full_blocks_count + 1;
|
||||||
|
|
@ -674,10 +681,11 @@ class EntityManager
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
entities_count += full_blocks_count * info.max_entities + (first_block.entities_count - first_elem);// - first_elem;
|
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;
|
uint last_elem = entities_per_job - entities_count; // + first_elem - 1;
|
||||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
CallData data = CallData(caller.system_id, sys, info,
|
||||||
cast(ushort)(full_blocks_count + 2),
|
first_block, cast(ushort)(full_blocks_count + 2),
|
||||||
cast(ushort) first_elem, cast(ushort) last_elem);
|
cast(ushort) first_elem, cast(ushort) last_elem);
|
||||||
tmp_datas.add(data);
|
tmp_datas.add(data);
|
||||||
first_elem = last_elem;
|
first_elem = last_elem;
|
||||||
|
|
@ -689,8 +697,8 @@ class EntityManager
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
uint last_elem = entities_per_job - entities_count;
|
uint last_elem = entities_per_job - entities_count;
|
||||||
CallData data = CallData(caller.system_id, sys, info, first_block,
|
CallData data = CallData(caller.system_id, sys, info, first_block, 1,
|
||||||
1, cast(ushort) first_elem, cast(ushort)(first_elem + last_elem));
|
cast(ushort) first_elem, cast(ushort)(first_elem + last_elem));
|
||||||
tmp_datas.add(data);
|
tmp_datas.add(data);
|
||||||
first_elem += last_elem;
|
first_elem += last_elem;
|
||||||
assert(first_elem <= first_block.entities_count);
|
assert(first_elem <= first_block.entities_count);
|
||||||
|
|
@ -704,8 +712,8 @@ class EntityManager
|
||||||
CallData data = CallData(caller.system_id, sys, info,
|
CallData data = CallData(caller.system_id, sys, info,
|
||||||
first_block, cast(ushort) blocks_count, cast(ushort) first_elem);
|
first_block, cast(ushort) blocks_count, cast(ushort) first_elem);
|
||||||
tmp_datas.add(data);
|
tmp_datas.add(data);
|
||||||
entities_count += (blocks_count - 1)
|
entities_count += (blocks_count - 1) * info.max_entities
|
||||||
* info.max_entities + info.last_block.entities_count - first_elem;
|
+ info.last_block.entities_count - first_elem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nextJob();
|
nextJob();
|
||||||
|
|
@ -920,7 +928,8 @@ class EntityManager
|
||||||
break;
|
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++)
|
/*for (; index < entity.callers.length; index++)
|
||||||
{
|
{
|
||||||
CallData* caller = &entity.callers[index];
|
CallData* caller = &entity.callers[index];
|
||||||
|
|
@ -1135,7 +1144,8 @@ class EntityManager
|
||||||
ushort* id = &ids[len];
|
ushort* id = &ids[len];
|
||||||
if (k >= new_ids.length)
|
if (k >= new_ids.length)
|
||||||
{
|
{
|
||||||
if(j >= info.components.length)break;
|
if (j >= info.components.length)
|
||||||
|
break;
|
||||||
*id = info.components[j++];
|
*id = info.components[j++];
|
||||||
//continue;
|
//continue;
|
||||||
}
|
}
|
||||||
|
|
@ -1158,7 +1168,8 @@ class EntityManager
|
||||||
else
|
else
|
||||||
*id = info.components[j++];
|
*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]);
|
||||||
|
|
||||||
|
|
@ -1270,56 +1281,38 @@ class EntityManager
|
||||||
{
|
{
|
||||||
EntityInfo* info = tmpl.info;
|
EntityInfo* info = tmpl.info;
|
||||||
EntitiesBlock* last_block = info.last_block;
|
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--;
|
ushort index = 0;
|
||||||
//index = cast(ushort)(block.added_count - 1);
|
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);
|
uint id = (block.entities_count + index - 1); //block.added_count);
|
||||||
//EntityInfo* info = tmpl.info;
|
|
||||||
|
|
||||||
void* data_begin = block.dataBegin();
|
void* data_begin = block.dataBegin();
|
||||||
void* start = data_begin + EntityID.sizeof * id;
|
void* start = data_begin + EntityID.sizeof * id;
|
||||||
//memcpy(data_begin + EntityID.sizeof * id, tmpl.entity_data.ptr, EntityID.sizeof);
|
|
||||||
foreach (i, comp; info.components)
|
foreach (i, comp; info.components)
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) block + info.deltas[comp] + components[comp].size * id,
|
memcpy(cast(void*) block + info.deltas[comp] + components[comp].size * id,
|
||||||
tmpl.entity_data.ptr + info.tmpl_deltas[comp], components[comp].size);
|
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);
|
blocks_to_update.add(block);
|
||||||
|
|
||||||
Entity* entity = cast(Entity*) start;
|
Entity* entity = cast(Entity*) start;
|
||||||
entity.id = id_manager.getNewID();
|
entity.id = id_manager.getNewID();
|
||||||
entity.updateID();
|
entity.updateID();
|
||||||
//block.added_count++;
|
|
||||||
//import core.atomic;
|
|
||||||
|
|
||||||
//block.entities_count++;
|
|
||||||
return *entity;
|
return *entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/************************************************************************************************************************
|
||||||
|
*Return block with free space for selected EntityInfo.
|
||||||
|
*/
|
||||||
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info, uint new_index = 1)
|
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info, uint new_index = 1)
|
||||||
{
|
{
|
||||||
//EntitiesBlock* previous_block;
|
|
||||||
EntitiesBlock* block = info.last_block;
|
EntitiesBlock* block = info.last_block;
|
||||||
|
|
||||||
if (block is null)
|
if (block is null)
|
||||||
|
|
@ -1339,38 +1332,7 @@ class EntityManager
|
||||||
new_block.id = cast(ushort)(block.id + 1);
|
new_block.id = cast(ushort)(block.id + 1);
|
||||||
block = new_block;
|
block = new_block;
|
||||||
info.last_block = 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;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1413,12 +1375,10 @@ class EntityManager
|
||||||
|
|
||||||
if (call_destructors)
|
if (call_destructors)
|
||||||
{
|
{
|
||||||
//void* data = data_begin + pos * info.size;
|
|
||||||
foreach (comp; info.components)
|
foreach (comp; info.components)
|
||||||
{
|
{
|
||||||
if (components[comp].destroy_callback)
|
if (components[comp].destroy_callback)
|
||||||
{
|
{
|
||||||
//components[comp].destroy_callback(data + info.deltas[comp]);
|
|
||||||
components[comp].destroy_callback(cast(
|
components[comp].destroy_callback(cast(
|
||||||
void*) block + info.deltas[comp] + pos * components[comp].size);
|
void*) block + info.deltas[comp] + pos * components[comp].size);
|
||||||
}
|
}
|
||||||
|
|
@ -1588,8 +1548,10 @@ class EntityManager
|
||||||
|
|
||||||
private void getThreadID()
|
private void getThreadID()
|
||||||
{
|
{
|
||||||
if(m_thread_id_func)thread_id = m_thread_id_func();
|
if (m_thread_id_func)
|
||||||
else thread_id = 0;
|
thread_id = m_thread_id_func();
|
||||||
|
else
|
||||||
|
thread_id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************************************************************************************************
|
/************************************************************************************************************************
|
||||||
|
|
@ -1624,7 +1586,8 @@ class EntityManager
|
||||||
{
|
{
|
||||||
if (last_block)
|
if (last_block)
|
||||||
return last_block.id + 1;
|
return last_block.id + 1;
|
||||||
else return 0;
|
else
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
///entity components
|
///entity components
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue