bubel-ecs/source/ecs/manager.d
Mergul 5dd24b6462 -Systems, Components and Events now must have proper mixin. Mixins are located in ecs.core module. (i.e. mixin ECS.Component)
-Multithreading support:
 *multithreaded update - updateMT(), function generates jobs to execute
 *added dispatch callback function to dispatch generated jobs (setJobDispachFunc)
 *added getID callback for get thread ID by EntityManager
-added Job structure. Job has one function "execute()".
-calling partial info update (required to multithreading)
-multithreaded removeEntity, addCompoenents, removeComponents. Every thread has own data and remove/change lists.
-multithreaded addEntity (WIP)
-fixed issue with duplicating components
-simpler and faster "findBlockWithFreeSpace" function
-CallDataAllocator, allocator for CallDatas (used for Jobs)
-fixed some bugs/issues
2018-10-14 17:00:53 +02:00

1836 lines
62 KiB
D

module ecs.manager;
import std.algorithm : max;
import std.conv : to;
import std.experimental.allocator;
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
import std.traits;
import core.stdc.stdlib;
import core.stdc.string;
import core.atomic;
import ecs.entity;
import ecs.block_allocator;
import ecs.hash_map;
import ecs.id_manager;
import ecs.system;
import ecs.vector;
import ecs.events;
alias gEM = EntityManager.instance;
alias SerializeVector = ecs.vector.Vector!ubyte;
class EntityManager
{
export static void initialize(uint threads_count)
{
if (instance is null)
instance = Mallocator.instance.make!EntityManager(threads_count);
}
export static void destroy()
{
if (instance is null)
return;
foreach (ref system; instance.systems)
{
system.disable();
}
foreach (ref system; instance.systems)
{
if (system.m_destroy)
(cast(void function(void*))system.m_destroy)(system.m_system_pointer);
}
Mallocator.instance.dispose(instance);
instance = null;
}
/************************************************************************************************************************
*Default constructor.
*/
this(uint threads_count)
{
if(threads_count == 0)threads_count = 0;
threads = Mallocator.instance.makeArray!ThreadData(threads_count);
//event_manager = EventManager(this);
//event_manager.manager = this;
}
/************************************************************************************************************************
*Register new System into EntityManager. This funcion generate glue between EntityManager and System.
*Systems can be registered from external dynamic library, and can be registered after adding entities too.
*System mustn't be registered before components which system want to use, in this case functions call assertion.
*
*params:
*priority = system priority. Priority determines order of execution of systems updates.
*/
void registerSystem(Sys)(int priority)
{
alias STC = ParameterStorageClass;
System system;
static if (!(hasMember!(Sys, "system_id")) || !is(typeof(Sys.system_id) == ushort))
{
static assert(0, "System should have \"__gshared ushort system_id");
}
static if (!(hasMember!(Sys, "EntitiesData")))
{
static assert(0, "System should gave \"EntitiesData\" struct for input components");
}
static string genCompList()()
{
static foreach (member; __traits(allMembers, Sys.EntitiesData))
{
static if (isFunction!(__traits(getMember, Sys.EntitiesData, member)))
static assert(0, "EntitiesData can't have any function!");
else static if (member == "length")
{
static assert(isIntegral!(typeof(__traits(getMember, Sys.EntitiesData,
member))), "EntitiesData 'length' member must be integral type.");
static assert(typeof(__traits(getMember, Sys.EntitiesData, member))
.sizeof > 1, "EntitiesData 'length' member can't be byte or ubyte.");
}
else static if (!(isArray!(typeof(__traits(getMember,
Sys.EntitiesData, member)))))
static assert(0, "EntitiesData members should be arrays of elements!");
}
string ret; // = "ushort comp;uint req;uint opt;uint absent;";
uint req;
uint opt;
uint absent;
foreach (member; __traits(allMembers, Sys.EntitiesData))
{
if (member == "length" || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == Entity[]) || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == const(Entity)[]))
{
}
else
{
{
bool has_att = false;
foreach (att; __traits(getAttributes,
__traits(getMember, Sys.EntitiesData, member)))
{
if (att == "optional")
{
//ret ~= "opt++;";
opt++;
has_att = true;
break;
}
else if (att == "absent")
{
absent++;
//ret ~= "absen++;";
has_att = true;
break;
}
}
if (!has_att)
req++;
//ret ~= "req++;";
}
}
}
static if (__traits(hasMember, Sys, "AbsentComponents"))
{
static if (is(Sys.AbsentComponents == enum))
{
absent += (Fields!(Sys.AbsentComponents)).length; //static assert(0,"Enum AbsentComponents are not implemented yet.");
}
else static if (__traits(compiles, allSameType!(string, typeof(Sys.AbsentComponents)))
&& allSameType!(string, typeof(Sys.AbsentComponents))
&& isExpressions!(Sys.AbsentComponents))
{
absent += Sys.AbsentComponents.length;
}
}
if (req > 0)
ret ~= "system.m_components = Mallocator.instance.makeArray!ushort("
~ req.to!string ~ ");";
if (opt > 0)
ret ~= "system.m_optional_components = Mallocator.instance.makeArray!ushort("
~ opt.to!string ~ ");";
if (absent > 0)
ret ~= "system.m_absent_components = Mallocator.instance.makeArray!ushort("
~ absent.to!string ~ ");";
ret ~= "ushort comp;"; //uint opt = 0;uint req = 0;uint absent = 0;";
opt = 0;
req = 0;
absent = 0;
static if (__traits(hasMember, Sys, "AbsentComponents"))
{
static if (is(Sys.AbsentComponents == enum))
{
//static assert(0,"Enum AbsentComponents are not implemented yet.");
foreach (str; Fields!(Sys.AbsentComponents))
ret ~= "system.m_absent_components[" ~ (absent++)
.to!string ~ "] = components_map.get(\""
~ str.stringof ~ "\", ushort.max);";
}
else static if (__traits(compiles, allSameType!(string, typeof(Sys.AbsentComponents)))
&& allSameType!(string, typeof(Sys.AbsentComponents))
&& isExpressions!(Sys.AbsentComponents))
{
foreach (str; Sys.AbsentComponents)
ret ~= "system.m_absent_components[" ~ (absent++)
.to!string ~ "] = components_map.get(\"" ~ str ~ "\", ushort.max);";
}
}
foreach (member; __traits(allMembers, Sys.EntitiesData))
{
if (member == "length" || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == Entity[]) || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == const(Entity)[]))
{
}
else
{
{
ret ~= "{comp = components_map.get(Unqual!(ForeachType!(typeof(
Sys.EntitiesData." ~ member ~ ")))
.stringof, ushort.max);\n
if(comp == ushort.max)assert(0,\"Can't register system \\\""
~ Sys.stringof
~ "\\\" due to non existing component \" ~ ForeachType!(typeof(
Sys.EntitiesData."
~ member ~ "))
.stringof
~ \".\");";
bool has_att = false;
foreach (att; __traits(getAttributes,
__traits(getMember, Sys.EntitiesData, member)))
{
if (att == "optional")
{
ret ~= "system.m_optional_components[" ~ (opt++)
.to!string ~ "] = comp;}";
has_att = true;
break;
}
else if (att == "absent")
{
ret ~= "system.m_absent_components[" ~ (absent++)
.to!string ~ "] = comp;}";
has_att = true;
break;
}
}
if (!has_att)
{
ret ~= "system.m_components[" ~ (req++).to!string ~ "] = comp;}";
}
}
}
}
return ret;
}
string genParamsChecking()()
{
string ret;
foreach (func; __traits(getOverloads, Sys, "update"))
{
if ((Parameters!(func)).length == 1)
ret ~= "\"" ~ (fullyQualifiedName!(Sys.EntitiesData)) ~ "\" == \"" ~ (
fullyQualifiedName!((Parameters!(func))[0])) ~ "\" || ";
}
ret ~= "false";
return ret;
}
static if (hasMember!(Sys, "update") && (mixin(genParamsChecking())))
{
static string genFillInputData()()
{
string ret;
ushort comp;
uint req = 0;
uint opt = 0;
uint absent = 0;
foreach (member; __traits(allMembers, Sys.EntitiesData))
{
if (is(typeof(__traits(getMember, Sys.EntitiesData,
member)) == Entity[]) || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == const(Entity)[]))
{
ret ~= "input_data." ~ member
~ " = (cast(Entity*) block.dataBegin())[offset .. entities_count];";
}
else if (member == "length")
{
ret ~= "input_data." ~ member ~ " = cast(typeof(input_data.length))(entities_count - offset);";
}
else
{
{
bool has_att = false;
foreach (att; __traits(getAttributes,
__traits(getMember, Sys.EntitiesData, member)))
{
if (att == "optional")
{
ret ~= "if(data.system.m_optional_components[" ~ opt.to!string
~ "] < info.deltas.length && info.deltas[ data.system.m_optional_components["
~ opt.to!string ~ "]] != 0)input_data." ~ member
~ " = (cast(ForeachType!(typeof(Sys.EntitiesData." ~ member
~ "))*)(cast(void*) block + info.deltas[ data.system.m_optional_components["
~ opt.to!string ~ "]]))[offset .. entities_count];
else input_data."
~ member ~ " = null;";
opt++;
has_att = true;
break;
}
else if (att == "absent")
{
absent++;
has_att = true;
break;
}
}
if (!has_att)
{
ret ~= "input_data." ~ member ~ " = (cast(ForeachType!(typeof(Sys.EntitiesData." ~ member
~ "))*)(cast(void*) block + info.deltas[ data.system.m_components["
~ req.to!string ~ "]]))[offset .. entities_count];";
req++;
}
}
}
}
return ret;
}
//pragma(msg,genFillInputData);
static void callUpdate(ref CallData data)
{
Sys* s = cast(Sys*) data.system.m_system_pointer;
Sys.EntitiesData input_data;
EntityInfo* info = data.info; //block.type_info;
EntitiesBlock* block;
if (data.first_block)
block = data.first_block;
else
block = info.first_block;
uint offset = data.begin;
uint entities_count;
uint blocks;
if (data.blocks)
blocks = data.blocks;
else
blocks = uint.max;
while (block !is null && blocks > 0)
{
if (blocks == 1)
{
if (data.end)
entities_count = data.end;
else entities_count = block.entities_count;
}
else
entities_count = block.entities_count;
mixin(genFillInputData());
s.update(input_data);
block = block.next_block;
offset = 0;
blocks--;
}
}
system.m_update = &callUpdate;
}
static string catchFunc()(string member, string func)
{
string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\"))
{
static void call" ~ func
~ "(void* system_pointer)
{
Sys* s = cast(Sys*) system_pointer;
s." ~ func ~ "();
}
system."
~ member ~ " = &call" ~ func ~ ";
}";
return ret;
}
mixin(catchFunc("m_enable", "onEnable"));
mixin(catchFunc("m_disable", "onDisable"));
mixin(catchFunc("m_create", "onCreate"));
mixin(catchFunc("m_destroy", "onDestroy"));
mixin(catchFunc("m_begin", "onBegin"));
mixin(catchFunc("m_end", "onEnd"));
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
system.m_priority = priority;
(cast(Sys*) system.m_system_pointer).__ecsInitialize();
system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
mixin(genCompList());
/*if(system.m_components)qsort(system.m_components.ptr, system.m_components.length, ushort.sizeof, &compareUShorts);
if(system.m_optional_components)qsort(system.m_optional_components.ptr, system.m_optional_components.length, ushort.sizeof, &compareUShorts);
if(system.m_absent_components)qsort(system.m_absent_components.ptr, system.m_absent_components.length, ushort.sizeof, &compareUShorts);*/
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
if (sys_id < systems.length)
{
system.enable();
/*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);
systems[sys_id] = system;
Sys.system_id = sys_id;
}
else
{
system.name = Mallocator.instance.makeArray(Sys.stringof);
systems_map.add(system.name, cast(ushort) systems.length);
systems.add(system);
if (system.m_create)
(cast(void function(void*))system.m_create)(system.m_system_pointer);
systems[$ - 1].enable();
Sys.system_id = cast(ushort)(systems.length - 1);
if (system.m_update !is null)
{
foreach (info; &entities_infos.byValue)
{
addEntityCaller(*info, cast(uint) systems.length - 1);
}
}
version (UpdateBySystems)
{
bool added = false;
foreach (i, ref caller; system_callers)
{
if (systems[caller.system_id].priority > priority)
{
SystemCaller* sys_caller = Mallocator.instance.make!SystemCaller;
sys_caller.system_id = Sys.system_id;
system_callers.add(sys_caller, i);
added = true;
break;
}
}
if (!added)
{
SystemCaller* sys_caller = Mallocator.instance.make!SystemCaller;
sys_caller.system_id = Sys.system_id;
system_callers.add(sys_caller);
}
}
}
updateEntityCallers();
}
System* getSystem(ushort id)
{
return &systems[id];
}
Sys* getSystem(Sys)()
{
return cast(Sys*) systems[Sys.system_id].m_system_pointer;
}
/************************************************************************************************************************
*Register component into EntityManager.
*/
void registerComponent(Comp)()
{
ComponentInfo info;
static if (!(hasMember!(Comp, "component_id")) || !is(typeof(Comp.component_id) == ushort))
{
static assert(0, "Component should have \"__gshared ushort component_id");
}
static if (hasMember!(Comp, "onDestroy") && isFunction!(Comp.onDestroy)
&& is(ReturnType!(Comp.onDestroy) == void)
&& Parameters!(Comp.onDestroy).length == 0)
{
static void callDestroy(void* pointer)
{
(cast(Comp*) pointer).onDestroy();
}
info.destroy_callback = &callDestroy;
}
info.size = Comp.sizeof;
info.alignment = Comp.alignof; //8;
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
if (comp_id < components.length)
{
Comp.component_id = comp_id;
components[comp_id] = info;
}
else
{
components.add(info);
Comp.component_id = cast(ushort)(components.length - 1);
string name = Mallocator.instance.makeArray(Comp.stringof);
components_map.add(name, cast(ushort)(components.length - 1));
}
}
void registerEvent(Ev)()
{
EventInfo info;
static if (!(hasMember!(Ev, "event_id")) || !is(typeof(Ev.event_id) == ushort))
{
static assert(0, "Event should have \"__gshared ushort event_id");
}
static if (hasMember!(Ev, "onDestroy") && isFunction!(Ev.onDestroy)
&& is(ReturnType!(Ev.onDestroy) == void) && Parameters!(Ev.onDestroy).length == 0)
{
static void callDestroy(void* pointer)
{
(cast(Ev*) pointer).onDestroy();
}
info.destroy_callback = &callDestroy;
}
info.size = Ev.sizeof;
info.alignment = Ev.alignof;
ushort event_id = events_map.get(Ev.stringof, ushort.max);
if (event_id < events.length)
{
Ev.event_id = event_id;
}
else
{
events.add(info);
Ev.event_id = cast(ushort)(events.length - 1);
events_map.add(Ev.stringof, cast(ushort)(events.length - 1));
}
}
/************************************************************************************************************************
*Update systems. Should be called only between begin() and end().
*/
export void update()
{
version (UpdateBySystems)
{
foreach (caller; system_callers)
{
System* sys = &systems[caller.system_id];
if (sys.enabled)
{
//if (sys.m_begin)
// sys.m_begin(sys.m_system_pointer);
foreach (info; caller.infos)
{
CallData data = CallData(caller.system_id, sys, info);
data.update();
//(cast(SytemFuncType) data.system.m_update)(data);
}
//if (sys.m_end)
// sys.m_end(sys.m_system_pointer);
}
}
}
else
{
foreach (info; &entities_infos.byValue)
{
foreach (data; info.callers)
{
if (data.system.enabled)
data.update();
//(cast(SytemFuncType) data.system.m_update)(data); //caller(call_data,null);
}
}
}
}
void updateMT()
{
assert(m_dispatch_jobs,
"Can't update with multithreading without JobDispatch function. Please use setJobDispatchFunc().");
Vector!CallData tmp_datas;
tmp_datas.reserve(8);
foreach (caller; system_callers)
{
System* sys = &systems[caller.system_id];
if (sys.enabled)
{
uint entities_count = 0;
foreach (info; caller.infos)
{
uint blocks_count = info.blocksCount();
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;
uint jobs_count = cast(uint) sys.jobs.length;
uint entities_per_job = entities_count / jobs_count + 1;
if (entities_per_job <= 4)
{
jobs_count = entities_count / 4;
if(jobs_count == 0)jobs_count = 1;
entities_per_job = entities_count / jobs_count + 1;
}
uint job_id = 0;
entities_count = 0;
void nextJob()
{
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++;
}
foreach (info; caller.infos)
{
uint blocks_count = info.blocksCount();
EntitiesBlock* first_block = info.first_block;
uint first_elem = 0;
begin:
if (first_block is null || blocks_count == 0)
continue;
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 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)
{
CallData data = CallData(caller.system_id, sys, info, first_block,
cast(ushort) (full_blocks_count + 1), cast(ushort) first_elem, 0);
tmp_datas.add(data);
first_elem = 0;
blocks_count -= full_blocks_count + 1;
first_block = block;
}
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),
cast(ushort) first_elem, cast(ushort) last_elem);
tmp_datas.add(data);
first_elem = last_elem;
blocks_count -= full_blocks_count + 1;
assert(first_elem <= block.entities_count);
first_block = block;
}
}
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));
tmp_datas.add(data);
first_elem += last_elem;
assert(first_elem <= first_block.entities_count);
}
nextJob();
entities_count = 0;
goto begin;
}
else
{
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;
}
}
nextJob();
m_dispatch_jobs(sys.jobs[0..job_id]);
}
}
}
struct Job
{
CallData[] callers;
void execute()
{
EntityManager.instance.getThreadID();
foreach (ref caller; callers)
{
caller.update();
}
}
}
void setJobDispachFunc(void delegate(Job[]) func)
{
m_dispatch_jobs = func;
}
static void alignNum(ref ushort num, ushort alignment)
{
num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1));
}
extern (C) static int compareUShorts(const void* a, const void* b)
{
ushort _a = *cast(ushort*) a;
ushort _b = *cast(ushort*) b;
if (_a < _b)
return -1;
else if (_a == _b)
return 0;
else
return 1;
}
/************************************************************************************************************************
*Allocate EntityTemplate with specifed components and returns pointer to it.
*
*params:
*components_ids = array of components allocated with template
*/
export EntityTemplate* allocateTemplate(ushort[] components_ids)
{
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
.. components_ids.length];
ids[0 .. $] = components_ids[];
qsort(ids.ptr, ids.length, ushort.sizeof, &compareUShorts);
{
uint j = 1;
foreach (i; 1 .. ids.length)
{
if (ids[i] != ids[j - 1])
{
ids[j] = ids[i];
j++;
}
else
debug assert(0, "Duplicated components in template!!!");
}
ids = ids[0 .. j];
}
EntityInfo* info = getEntityInfo(ids);
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size);
temp.info = info;
//fill components with default data
foreach (comp; info.components)
{
temp.entity_data[info.tmpl_deltas[comp] .. info.tmpl_deltas[comp] + components[comp].size]
= components[comp].init_data;
}
return temp;
}
/************************************************************************************************************************
*Returns entity type info.
*
*params:
*ids = array of components
*/
export EntityInfo* getEntityInfo(ushort[] ids)
{
EntityInfo* info = entities_infos.get(ids, null);
if (info is null)
{
info = Mallocator.instance.make!EntityInfo;
info.components = Mallocator.instance.makeArray(ids);
info.deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
info.size = EntityID.sizeof;
info.alignment = EntityID.alignof;
info.tmpl_deltas = Mallocator.instance.makeArray!ushort(ids[$ - 1] + 1);
uint components_size = EntityID.sizeof;
foreach (i, id; ids)
{
info.alignment = max(info.alignment, components[id].alignment);
alignNum(info.size, components[id].alignment);
info.tmpl_deltas[id] = info.size;
info.size += components[id].size;
components_size += components[id].size;
}
alignNum(info.size, info.alignment);
/**/
uint block_memory = cast(uint)(
page_size - EntitiesBlock.sizeof - (info.size - components_size));
//uint entity_comps_size = EntityID.sizeof;
uint mem_begin = EntitiesBlock.sizeof;
/*foreach (id; ids)
{
entity_comps_size += components[id].size;
}*/
uint entites_in_block = block_memory / info.size; //entity_comps_size;
info.max_entities = cast(ushort) entites_in_block;
ushort current_delta = cast(ushort)(mem_begin + entites_in_block * EntityID.sizeof);
foreach (i, id; ids)
{
alignNum(current_delta, components[id].alignment);
info.deltas[id] = cast(ushort) current_delta;
current_delta += entites_in_block * components[id].size;
}
foreach (uint i, ref system; systems)
{
if (system.m_update is null)
continue;
addEntityCaller(*info, i);
}
version (UpdateBySystems)
{
foreach (uint i, ref system; systems)
{
if (system.m_update is null)
continue;
addSystemCaller(*info, i);
}
}
updateEntityCallers();
entities_infos.add(info.components, info);
}
return info;
}
export void updateEntityCallers()
{
foreach (entity; &entities_infos.byValue)
{
foreach (ref caller; entity.callers)
{
caller.system = &systems[caller.system_id];
}
}
}
export void addSystemCaller(ref EntityInfo entity, uint system_id)
{
System* system = &systems[system_id];
//CallData call_data = CallData(system_id, system, &entity);
if (system.m_absent_components)
{
foreach (id; system.m_absent_components)
{
foreach (id2; entity.components)
{
if (id == id2)
return;
}
}
}
foreach (id; system.m_components)
{
foreach (i2, id2; entity.components)
{
if (id2 == id)
goto is_;
}
return;
is_:
}
uint index = 0;
for (; index < system_callers.length; index++)
{
if (system_callers[index].system_id == system_id)
break;
}
if(index < system_callers.length)system_callers[index].infos.add(&entity);
/*for (; index < entity.callers.length; index++)
{
CallData* caller = &entity.callers[index];
if (caller.system.priority >= call_data.system.priority)
break;
}
entity.callers.add(call_data, index);*/
}
export void addEntityCaller(ref EntityInfo entity, uint system_id)
{
System* system = &systems[system_id];
CallData call_data = CallData(system_id, system, &entity);
if (system.m_absent_components)
{
foreach (id; system.m_absent_components)
{
foreach (id2; entity.components)
{
if (id == id2)
return;
}
}
}
foreach (id; system.m_components)
{
foreach (i2, id2; entity.components)
{
if (id2 == id)
goto is_;
}
return;
is_:
}
uint index = 0;
for (; index < entity.callers.length; index++)
{
CallData* caller = &entity.callers[index];
if (caller.system.priority >= call_data.system.priority)
break;
}
entity.callers.add(call_data, index);
}
/************************************************************************************************************************
*Returns pointer to entity.
*
*params:
*id = ID of entity
*/
export Entity* getEntity(EntityID id)
{
return cast(Entity*) id_manager.getEntityPointer(id);
}
/************************************************************************************************************************
*Remove components from entity by IDs. Components will be removed on end of frame.
*
*params:
*entity_id = ID of entity
*del_ids = array of components IDs
*/
export void removeComponents(EntityID entity_id, ushort[] del_ids)
{
ThreadData* data = &threads[thread_id];
uint num = cast(uint) del_ids.length;
data.change_entities_list.add(0);
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
data.change_entities_list.add(cast(ubyte[]) del_ids);
}
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
{
Entity* entity = id_manager.getEntityPointer(entity_id);
if (!entity)
return;
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
qsort(del_ids.ptr, del_ids.length, ushort.sizeof, &compareUShorts);
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length)))[0
.. info.components.length];
uint j = 0;
uint k = 0;
foreach (id; info.components)
{
while (k < del_ids.length && del_ids[k] < id)
k++;
if (k >= del_ids.length)
{
ids[j++] = id;
}
else if (del_ids[k] == info.components[j])
{
k++;
}
else
ids[j++] = id;
}
if (j == info.components.length)
return;
EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof;
Entity* new_entity = cast(Entity*) start;
new_entity.id = entity.id;
new_entity.updateID();
static if (EntityID.sizeof == 8)
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
foreach (comp; new_info.components)
{
uint comp_size = components[comp].size;
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);
}
new_block.entities_count++;
removeEntityNoID(entity, block);
}
/************************************************************************************************************************
*Remove coponents from entity.
*
*params:
*Compoenents = components types to remove
*entity_id = ID of entity
*/
void removeComponents(Components...)(EntityID entity_id)
{
const uint num = Components.length;
ushort[num] del_ids;
static foreach (i, comp; Components)
{
del_ids[i] = comp.component_id;
}
/*change_entities_list.add(0);
change_entities_list.add((cast(ubyte*)&entity_id)[0..8]);
change_entities_list.add((cast(ubyte*)&num)[0..4]);
change_entities_list.add(cast(ubyte[])del_ids);*/
removeComponents(entity_id, del_ids);
}
private void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
{
uint num = cast(uint) new_ids.length;
Entity* entity = id_manager.getEntityPointer(entity_id);
if (!entity)
return;
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
.. info.components.length + num];
/*ushort[num] new_ids;
static foreach (i, comp; Components)
{
new_ids[i] = comp.component_id;
}*/
/*void*[num] pointers;
static foreach (i, comp; comps)
{
pointers[i] = &comp;
}*/
foreach (int i; 0 .. num)
{
ushort min = new_ids[i];
int pos = i;
foreach (int j; i .. num)
{
if (new_ids[j] < min)
{
min = new_ids[j];
pos = j;
}
}
if (pos != i)
{
ushort id = new_ids[i];
new_ids[i] = new_ids[pos];
new_ids[pos] = id;
void* ptr = data_pointers[i];
data_pointers[i] = data_pointers[pos];
data_pointers[pos] = ptr;
}
}
uint j = 0;
uint k = 0;
uint len = 0;
//foreach (ref id; ids)
for(;len<ids.length;len++)
{
ushort* id = &ids[len];
if (k >= new_ids.length)
{
if(j >= info.components.length)break;
*id = info.components[j++];
//continue;
}
else if (j >= info.components.length)
{
*id = new_ids[k++];
//continue;
}
else if(new_ids[k] == info.components[j])
{
*id = info.components[j++];
k++;
}
/*debug if (new_ids[k] == info.components[j])
assert(0, "Trying to add already existing component!");*/
else if (new_ids[k] < info.components[j])
{
*id = new_ids[k++];
}
else
*id = info.components[j++];
}
if(len == info.components.length)return;
EntityInfo* new_info = getEntityInfo(ids[0..len]);
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof;
Entity* new_entity = cast(Entity*) start;
new_entity.id = entity.id;
new_entity.updateID();
j = 0;
k = 0;
static if (EntityID.sizeof == 8)
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])
{
void* dst = cast(void*) new_block + new_info.deltas[id] + (
new_block.entities_count /*+ new_block.added_count*/) * components[id].size;
uint size = components[id].size;
if (k >= new_ids.length)
{
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
else if (j >= info.components.length)
{
memcpy(dst, data_pointers[k], size);
k++;
}
else if (id == new_ids[k])
{
memcpy(dst, data_pointers[k], size);
k++;
}
else
{
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
}
new_block.entities_count++;
removeEntityNoID(entity, block);
}
/************************************************************************************************************************
*Add components to entity. Components will be added on end of frame.
*
*params:
*entity_id = ID of entity to remove
*tmpl = pointer entity template allocated by EntityManager.
*/
void addComponents(Components...)(const EntityID entity_id, Components comps)
{
const uint num = Components.length;
Entity* entity = id_manager.getEntityPointer(entity_id);
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
.. info.components.length + num];
ushort[num] new_ids;
static foreach (i, comp; Components)
{
new_ids[i] = comp.component_id;
}
/*void*[num] pointers;
static foreach (i, comp; comps)
{
pointers[i] = &comp;
}*/
ThreadData* data = &threads[thread_id];
data.change_entities_list.add(1);
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
data.change_entities_list.add(cast(ubyte[]) new_ids);
static foreach (i, comp; comps)
{
data.change_entities_list.add((cast(ubyte*)&comp)[0 .. comp.sizeof]);
}
//__addComponents(entity_id, new_ids, pointers);
}
/************************************************************************************************************************
*Free template memory.
*
*params:
*tmpl = pointer entity template allocated by EntityManager.
*/
export void freeTemplate(EntityTemplate* template_)
{
Mallocator.instance.dispose(template_.entity_data);
Mallocator.instance.dispose(template_);
}
/************************************************************************************************************************
*Add entity to system.
*
*params:
*tmpl = pointer entity template allocated by EntityManager.
*/
export ref Entity addEntity(EntityTemplate* tmpl)
{
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;
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;
}
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info, uint new_index = 1)
{
//EntitiesBlock* previous_block;
EntitiesBlock* block = info.last_block;
if(block is null)
{
block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
*block = EntitiesBlock(info);
block.id = 0;
info.first_block = block;
info.last_block = block;
}
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);
new_block.prev_block = block;
block.next_block = new_block;
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;
}
/************************************************************************************************************************
*Remove entity by ID. Entity will be removed on frame end.
*
*params:
*id = id of entity to remove
*/
export void removeEntity(EntityID id)
{
threads[thread_id].entities_to_remove.add(id);
}
private void __removeEntity(EntityID id)
{
//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);
id_manager.releaseID(id); //release id from manager
removeEntityNoID(entity, block, true);
}
private void removeEntityNoID(Entity* entity, EntitiesBlock* block,
bool call_destructors = false)
{
//pos is Entity number in block
void* data_begin = block.dataBegin();
EntityInfo* info = block.type_info;
info.last_block.entities_count--;
static if (EntityID.sizeof == 8)
uint pos = cast(uint)((cast(void*) entity - data_begin) >> 3);
else
uint pos = cast(uint)((cast(void*) entity - data_begin) / EntityID.sizeof());
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);
}
}
}
if (block !is info.last_block || pos != block.entities_count)
{
foreach (comp; info.components)
{
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);
}
block = info.last_block;
entity.id = *cast(EntityID*)(block.dataBegin() + block.entities_count * EntityID.sizeof);
entity.updateID();
}
block = info.last_block;
if (block.entities_count == 0)
{
info.last_block = block.prev_block;
if (info.first_block is block)
{
info.first_block = null;
}
if (block.prev_block)
{
block.prev_block.next_block = null;
block.prev_block.added_count = 0;
}
allocator.freeBlock(block);
}
}
/************************************************************************************************************************
*functions return MetaData of page.
*
*params:
*pointer = pointer to any data of entity (i.e. component data pointer)
*/
export EntitiesBlock* getMetaData(const void* pointer)
{
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
}
private void changeEntites()
{
foreach(ref thread;threads)
{
uint index = 0;
uint len = cast(uint) thread.change_entities_list.length;
while (index < len)
{
if (!thread.change_entities_list[index++])
{
EntityID id = *cast(EntityID*)&thread.change_entities_list[index];
index += EntityID.sizeof;
uint num = *cast(uint*)&thread.change_entities_list[index];
index += uint.sizeof;
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
index += ushort.sizeof * num;
__removeComponents(id, ids);
}
else
{
EntityID id = *cast(EntityID*)&thread.change_entities_list[index];
index += EntityID.sizeof;
uint num = *cast(uint*)&thread.change_entities_list[index];
index += uint.sizeof;
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
index += ushort.sizeof * num;
void*[] pointers = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
foreach (i; 0 .. num)
{
pointers[i] = &thread.change_entities_list[index];
index += components[ids[i]].size;
}
__addComponents(id, ids, pointers);
}
}
thread.change_entities_list.clear();
}
}
private void updateBlocks()
{
foreach (block; blocks_to_update)
{
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 = 0;
block.added_count.atomicStore(cast(ushort)0);
}
blocks_to_update.clear();
}
private void removeEntities()
{
foreach(i,ref thread;threads)
{
foreach (id; thread.entities_to_remove)
{
__removeEntity(id);
}
thread.entities_to_remove.clear();
}
}
/************************************************************************************************************************
*Begin of update process. Should be called before any update is called.
*/
export void begin()
{
updateBlocks();
removeEntities();
changeEntites();
m_call_data_allocator.clear();
/*version (UpdateBySystems)
{
}
else
{*/
foreach (ref system; instance.systems)
{
if (system.m_begin)
(cast(void function(void*))system.m_begin)(system.m_system_pointer);
}
//}
}
/************************************************************************************************************************
*End of update process. Should be called after every update function.
*/
export void end()
{
/*version (UpdateBySystems)
{
}
else
{*/
foreach (ref system; instance.systems)
{
if (system.m_end)
(cast(void function(void*))system.m_end)(system.m_system_pointer);
}
//}
updateBlocks();
removeEntities();
changeEntites();
//clearEvents();
}
private void getThreadID()
{
if(m_thread_id_func)thread_id = m_thread_id_func();
else thread_id = 0;
}
/************************************************************************************************************************
*Component info;
*/
struct ComponentInfo
{
///Component size
ushort size;
///Component data alignment
ushort alignment;
///Initialization data
ubyte[] init_data;
///Pointer to component destroy callback
void function(void* pointer) destroy_callback;
}
struct EventInfo
{
ushort size;
ushort alignment;
void function(void* pointer) destroy_callback;
}
/************************************************************************************************************************
*Entity type info.
*/
struct EntityInfo
{
///Returns number of blocks
uint blocksCount()
{
if(last_block)
return last_block.id + 1;
else return 0;
}
///entity components
ushort[] components;
///deltas in memory for components in EntitiesBlock
ushort[] deltas;
///deltas in memory for components in EntityTemplate
ushort[] tmpl_deltas;
///alignment of whole entity
ushort alignment; //unused in linear-layout
///size of entity (with alignment respect)
ushort size;
///max number of entities in block
ushort max_entities;
///pointer to first block/page
EntitiesBlock* first_block;
///pointer to last block
EntitiesBlock* last_block;
///array of CallData. Contain data for System calls.
Vector!(CallData) callers;
}
/************************************************************************************************************************
*Meta data of every block of entities (contained at the begining of block).
*/
struct EntitiesBlock
{
///return distance (in bytes) from begin of block to data
uint dataDelta()
{
ushort dif = EntitiesBlock.sizeof;
alignNum(dif, type_info.alignment);
return dif;
}
///return pointer to first element in block
export void* dataBegin()
{
ushort dif = EntitiesBlock.sizeof;
return cast(void*)&this + dif;
}
///pointer to Entity type info
EntityInfo* type_info = null;
///number of entities in block
ushort entities_count = 0;
///number of new entities in block
shared ushort added_count = 0;
//ushort added_count = 0;
///block id
ushort id = 0;
///maximum number of entities in block
//ushort max_entities = 0;
///pointer to next block/page
EntitiesBlock* next_block = null;
///pointer to next block/page
EntitiesBlock* prev_block = null;
//there is a loooot of data (some kB of memory, pure magic)
}
/************************************************************************************************************************
*Structure with data used to calling System calls.
*
*<i>first_block</i>, <i>begin</i>, <i>end</i>, <i>blocks</i> parameters are used
*to call partial info update
*/
struct CallData
{
void update()
{
(cast(SytemFuncType) system.m_update)(this);
}
///system ID. Used to update system pointer after system reload.
uint system_id;
///pointer to used system
System* system;
///poiner to Entity type info
EntityManager.EntityInfo* info;
///pointer to first block into process (if 0 then first block will be used)
EntitiesBlock* first_block;
///number of blocks to update (if 0 then update all)
ushort blocks;
///index of first element in first block
ushort begin;
///index of last element in last block
ushort end;
}
struct SystemCaller
{
uint system_id;
System* system;
Vector!(EntityInfo*) infos;
}
struct ThreadData
{
Vector!EntityID entities_to_remove;
Vector!ubyte change_entities_list;
}
static uint thread_id;
ThreadData[] threads;
Vector!(SystemCaller*) system_callers;
alias SytemFuncType = void function(ref EntityManager.CallData data);
//alias sendSelfEvent = instance.event_manager.sendSelfEvent;
//alias event_manager this;
///Single page size. Must be power of two.
enum page_size = 32768; //4096;
///Number of pages in block.
enum pages_in_block = 128;
IDManager id_manager;
BlockAllocator!(page_size, pages_in_block) allocator;
//EventManager event_manager;
mixin EventManagerCode;
//Vector!EntityID entities_to_remove;
Vector!(EntitiesBlock*) blocks_to_update;
//Vector!ubyte change_entities_list;
void delegate(Job[] jobs) m_dispatch_jobs;
uint delegate() m_thread_id_func;
HashMap!(ushort[], EntityInfo*) entities_infos;
HashMap!(const(char)[], ushort) systems_map;
HashMap!(string, ushort) components_map;
HashMap!(string, ushort) events_map;
Vector!System systems;
Vector!ComponentInfo components;
Vector!EventInfo events;
CallDataAllocator m_call_data_allocator;
struct CallDataAllocator
{
struct Block
{
CallData[256] data;
uint allocated = 0;
}
Vector!(Block*) blocks;
uint id;
void clear()
{
if(blocks.length > 0)
foreach (block; blocks[0 .. id + 1])
{
block.allocated = 0;
}
id = 0;
//blocks.clear();
}
CallData[] getCallData(uint num)
{
if (blocks.length == 0)
{
Block* new_block = Mallocator.instance.make!Block;
blocks.add(new_block);
}
/*else if(blocks[$-1].allocated + num >= 256)
{
blocks.add(Block());
}*/
Block* block = blocks[id];
if (block.allocated + num >= 256)
{
id++;
if (id == blocks.length)
{
Block* new_block = Mallocator.instance.make!Block;
blocks.add(new_block);
}
block = blocks[id];
}
CallData[] ret = block.data[block.allocated .. block.allocated + num];
block.allocated += num;
return ret;
}
}
__gshared EntityManager instance = null;
}
/*
static ulong defaultHashFunc(T)(auto ref T t)
{
ulong ret = 0;
foreach(id;t)
{
ret = ret
}
}*/