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 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() { if (instance is null) instance = Mallocator.instance.make!EntityManager; } 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) system.m_destroy(system.m_system_pointer); } Mallocator.instance.dispose(instance); instance = null; } /************************************************************************************************************************ *Default constructor. */ this() { //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 absen;"; uint req; uint opt; uint absen; 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 == "absen") { absen++; //ret ~= "absen++;"; has_att = true; break; } } if (!has_att)req++; //ret ~= "req++;"; } } } static if(__traits(hasMember, Sys, "AbsenComponents")) { static if(is(Sys.AbsenComponents == enum)) { absen += (Fields!(Sys.AbsenComponents)).length;//static assert(0,"Enum AbsenComponents are not implemented yet."); } else static if(__traits(compiles,allSameType!(string,typeof(Sys.AbsenComponents))) && allSameType!(string,typeof(Sys.AbsenComponents)) && isExpressions!(Sys.AbsenComponents)) { absen += Sys.AbsenComponents.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(absen > 0)ret ~= "system.m_absen_components = Mallocator.instance.makeArray!ushort("~absen.to!string~");"; ret ~= "ushort comp;";//uint opt = 0;uint req = 0;uint absen = 0;"; opt = 0; req = 0; absen = 0; static if(__traits(hasMember, Sys, "AbsenComponents")) { static if(is(Sys.AbsenComponents == enum)) { //static assert(0,"Enum AbsenComponents are not implemented yet."); foreach(str;Fields!(Sys.AbsenComponents))ret ~= "system.m_absen_components["~(absen++).to!string~"] = components_map.get(\""~str.stringof~"\", ushort.max);"; } else static if(__traits(compiles,allSameType!(string,typeof(Sys.AbsenComponents))) && allSameType!(string,typeof(Sys.AbsenComponents)) && isExpressions!(Sys.AbsenComponents)) { foreach(str;Sys.AbsenComponents)ret ~= "system.m_absen_components["~(absen++).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 == "absen") { ret ~= "system.m_absen_components["~(absen++).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 absen = 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())[0 .. block.entities_count];"; } else if(member == "length") { ret ~= "input_data." ~ member ~ " = block.entities_count;"; } 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 ~ "]]))[0 .. block.entities_count]; else input_data." ~ member ~ " = null;"; opt++; has_att = true; break; } else if (att == "absen") { absen++; 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 ~ "]]))[0 .. block.entities_count];"; req++; } } } } return ret; } static void callUpdate(ref CallData data) { Sys* s = cast(Sys*) data.system.m_system_pointer; Sys.EntitiesData input_data; EntitiesBlock* block = data.info.first_block; while (block !is null) { EntityInfo* info = block.type_info; mixin(genFillInputData()); s.update(input_data); block = block.next_block; } } 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; 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_absen_components)qsort(system.m_absen_components.ptr, system.m_absen_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) 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) 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); } } } 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() { foreach (info; &entities_infos.byValue) { foreach (data; info.callers) { if (data.system.enabled) (cast(SytemFuncType) data.system.m_update)(data); //caller(call_data,null); } } } static void alignNum(ref ushort num, ushort alignment) { num = cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); //num += alignment - (num & (alignment - 1)); } /*static ushort alignedNum(ushort num, ushort alignment) { return cast(ushort)((num + alignment - 1) & (-cast(int) alignment)); }*/ 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); } 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 addEntityCaller(ref EntityInfo entity, uint system_id) { System* system = &systems[system_id]; CallData call_data = CallData(system_id, system, &entity); if(system.m_absen_components) { foreach (id; system.m_absen_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) { uint num = cast(uint) del_ids.length; 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); } 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] = ∁ }*/ 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; foreach (ref id; ids) { if (k >= new_ids.length) { id = info.components[j++]; continue; } if (j >= info.components.length) { id = new_ids[k++]; continue; } debug if (new_ids[k] == info.components[j]) assert(0, "Trying to add already existing component!"); if (new_ids[k] < info.components[j]) { id = new_ids[k++]; } else id = info.components[j++]; } EntityInfo* new_info = getEntityInfo(ids); EntitiesBlock* new_block = findBlockWithFreeSpace(new_info); //removeEntityNoID(entity, block); 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) { 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] = ∁ }*/ change_entities_list.add(1); 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[]) new_ids); static foreach (i, comp; comps) { 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) { EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info); uint id = (block.entities_count + 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) blocks_to_update.add(block); Entity* entity = cast(Entity*) start; entity.id = id_manager.getNewID(); entity.updateID(); block.added_count++; //block.entities_count++; return *entity; } private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info) { EntitiesBlock* previous_block; EntitiesBlock* block = info.first_with_free_space; 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.first_with_free_space = block; break; // new block certainly has free space } // check if there is enought space /*if (block.dataDelta() + ( block.entities_count + block.added_count + 1) * info.size > page_size)*/ if (block.entities_count + block.added_count >= block.type_info.max_entities) { previous_block = block; block = block.next_block; continue; } info.first_with_free_space = block; 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) { 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; block.entities_count--; //set "first_with_free_space" if should it be if (info.first_with_free_space.id > block.id) info.first_with_free_space = block; 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.entities_count == 0) { if (info.first_block is block) { info.first_block = block.next_block; } if (info.first_with_free_space is block) { info.first_with_free_space = block.next_block; //info.first_block; } if (block.prev_block) { block.prev_block.next_block = block.next_block; } if (block.next_block) { block.next_block.prev_block = block.prev_block; } allocator.freeBlock(block); return; } if (pos == block.entities_count) return; foreach (comp; info.components) { void* ptr = cast(void*) block + info.deltas[comp]; uint size = components[comp].size; memcpy(ptr + pos * size, ptr + block.entities_count * size, size); } entity.id = *cast(EntityID*)(data_begin + block.entities_count * EntityID.sizeof); //update pointer for moved entity ID //entity = cast(Entity*) dst; entity.updateID(); } /************************************************************************************************************************ *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() { uint index = 0; uint len = cast(uint) change_entities_list.length; while (index < len) { if (!change_entities_list[index++]) { EntityID id = *cast(EntityID*)&change_entities_list[index]; index += EntityID.sizeof; uint num = *cast(uint*)&change_entities_list[index]; index += uint.sizeof; ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num]; ids[0 .. $] = (cast(ushort*)&change_entities_list[index])[0 .. num]; index += ushort.sizeof * num; __removeComponents(id, ids); } else { EntityID id = *cast(EntityID*)&change_entities_list[index]; index += EntityID.sizeof; uint num = *cast(uint*)&change_entities_list[index]; index += uint.sizeof; ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num]; ids[0 .. $] = (cast(ushort*)&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] = &change_entities_list[index]; index += components[ids[i]].size; } __addComponents(id, ids, pointers); } } change_entities_list.clear(); } private void updateBlocks() { foreach (block; blocks_to_update) { block.entities_count += block.added_count; block.added_count = 0; } blocks_to_update.clear(); } private void removeEntities() { foreach (id; entities_to_remove) { __removeEntity(id); } entities_to_remove.clear(); } /************************************************************************************************************************ *Begin of update process. Should be called before any update is called. */ export void begin() { updateBlocks(); removeEntities(); changeEntites(); foreach (ref system; instance.systems) { if (system.m_begin) system.m_begin(system.m_system_pointer); } } /************************************************************************************************************************ *End of update process. Should be called after every update function. */ export void end() { foreach (ref system; instance.systems) { if (system.m_end) system.m_end(system.m_system_pointer); } updateBlocks(); removeEntities(); changeEntites(); //clearEvents(); } /************************************************************************************************************************ *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 { ///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; ///a hint for allocations EntitiesBlock* first_with_free_space; // a hint for allocations, should have empty space in it but doesn't have to ///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 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. */ struct CallData { ///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; } 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; 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; __gshared EntityManager instance = null; } /* static ulong defaultHashFunc(T)(auto ref T t) { ulong ret = 0; foreach(id;t) { ret = ret } }*/