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

@ -18,7 +18,7 @@ struct IDManager
pragma(inline, false) EntityID getNewID() nothrow @nogc
{
int current = m_stack_top.atomicOp!"-="(1) + 1;
if(current < 0)
if (current < 0)
{
uint add_id = m_last_id.atomicOp!"+="(1) - 1;
@ -29,7 +29,7 @@ struct IDManager
if (block_id >= m_blocks_count)
{
add_mutex.lock();
if(block_id >= m_blocks_count)
if (block_id >= m_blocks_count)
{
m_blocks[m_blocks_count].alloc();
m_blocks_count++;
@ -112,9 +112,11 @@ struct IDManager
*/
export bool isExist(EntityID id) nothrow @nogc
{
if(id.id >= m_ids_array.length)return false;
if (id.id >= m_ids_array.length)
return false;
Data* data = &m_ids_array[id.id];
if(data.entity is null)return false;
if (data.entity is null)
return false;
return data.counter == id.counter;
}
@ -126,7 +128,8 @@ struct IDManager
m_ids_array = Mallocator.makeArray!Data(65536);
m_free_stack = Mallocator.makeArray!uint(65536);
m_blocks = Mallocator.makeArray!Block(64);
foreach(ref block;m_blocks)block = Block();
foreach (ref block; m_blocks)
block = Block();
m_blocks_count = 1;
m_blocks[0].alloc();
@ -142,20 +145,23 @@ struct IDManager
*/
void deinitialize() @trusted @nogc nothrow
{
if(m_ids_array)Mallocator.dispose(m_ids_array);
if(m_free_stack)Mallocator.dispose(m_free_stack);
if(m_blocks)
if (m_ids_array)
Mallocator.dispose(m_ids_array);
if (m_free_stack)
Mallocator.dispose(m_free_stack);
if (m_blocks)
{
foreach(ref block;m_blocks)
foreach (ref block; m_blocks)
{
if(block.data)block.free();
if (block.data)
block.free();
}
Mallocator.dispose(m_blocks);
}
if(add_mutex)
if (add_mutex)
{
add_mutex.destroy();
Mallocator.dispose(add_mutex);//cast(void*)add_mutex); //workaround for compiler bug
Mallocator.dispose(add_mutex); //cast(void*)add_mutex); //workaround for compiler bug
add_mutex = null;
}
}
@ -165,27 +171,31 @@ struct IDManager
*/
void optimize() nothrow @nogc
{
if(m_stack_top < -1)m_stack_top = -1;
if(m_last_id > m_ids_array.length)
if (m_stack_top < -1)
m_stack_top = -1;
if (m_last_id > m_ids_array.length)
{
uint begin = cast(uint)m_ids_array.length;
uint begin = cast(uint) m_ids_array.length;
Data[] new_array = Mallocator.makeArray!Data(begin + (m_blocks_count << 16));
memcpy(new_array.ptr, m_ids_array.ptr, m_ids_array.length * Data.sizeof);
Mallocator.dispose(m_ids_array);
m_ids_array = new_array;
uint[] new_stack = Mallocator.makeArray!uint(m_ids_array.length);
memcpy(new_stack.ptr,m_free_stack.ptr,m_free_stack.length * uint.sizeof);
memcpy(new_stack.ptr, m_free_stack.ptr, m_free_stack.length * uint.sizeof);
Mallocator.dispose(m_free_stack);
m_free_stack = new_stack;
foreach(block;m_blocks[0..m_blocks_count-1])
foreach (block; m_blocks[0 .. m_blocks_count - 1])
{
memcpy(cast(void*)m_ids_array.ptr + begin * Data.sizeof, block.data.ptr, 65536 * Data.sizeof);
memcpy(cast(void*) m_ids_array.ptr + begin * Data.sizeof,
block.data.ptr, 65536 * Data.sizeof);
begin += 65536;
}
memcpy(cast(void*)m_ids_array.ptr + begin * Data.sizeof, m_blocks[m_blocks_count-1].data.ptr, (m_last_id - begin) * Data.sizeof);
foreach(ref block;m_blocks[1..m_blocks_count])block.free();
memcpy(cast(void*) m_ids_array.ptr + begin * Data.sizeof,
m_blocks[m_blocks_count - 1].data.ptr, (m_last_id - begin) * Data.sizeof);
foreach (ref block; m_blocks[1 .. m_blocks_count])
block.free();
m_blocks_count = 1;
}
}
@ -217,12 +227,12 @@ struct IDManager
private:
Mutex* add_mutex;
Data[] m_ids_array = null;
uint m_blocks_count = 0;
Block[] m_blocks;
uint[] m_free_stack = null;
align(64) shared uint m_last_id = 0;
align(64) shared int m_stack_top = -1;
}