-formatted with dfmt

-cleaned up code
This commit is contained in:
Mergul 2018-10-14 18:36:31 +02:00
parent 5dd24b6462
commit 6e2c00f608

View file

@ -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;
@ -610,7 +613,8 @@ 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;
@ -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,7 +639,8 @@ class EntityManager
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 .. $];
tmp_datas.clear();
sys.jobs[job_id].callers = callers;
@ -653,7 +659,8 @@ 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));
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;
@ -661,12 +668,12 @@ class EntityManager
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;
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),
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,8 +712,8 @@ 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();
@ -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];
@ -1135,7 +1144,8 @@ class EntityManager
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;
}
@ -1158,7 +1168,8 @@ 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]);
@ -1270,56 +1281,38 @@ 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);
ushort index = 0;
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);
//EntityInfo* info = tmpl.info;
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)
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)
@ -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);
}
@ -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;
}
/************************************************************************************************************************
@ -1624,7 +1586,8 @@ class EntityManager
{
if (last_block)
return last_block.id + 1;
else return 0;
else
return 0;
}
///entity components