/************************************************************************************************************************ *Most important module. */ 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 core.sync.mutex; import ecs.entity; import ecs.block_allocator; import ecs.hash_map; import ecs.id_manager; import ecs.system; import ecs.vector; import ecs.events; import ecs.traits; export alias gEM = EntityManager.instance; export alias gEntityManager = EntityManager.instance; export alias gEventManager = EntityManager.instance; alias SerializeVector = ecs.vector.Vector!ubyte; /************************************************************************************************************************ *Entity manager is responsible for everything. */ export class EntityManager { export static void initialize(uint threads_count) { if (instance is null) { instance = Mallocator.instance.make!EntityManager(threads_count); with(instance) { UpdatePass* pass = Mallocator.instance.make!UpdatePass; pass.name = Mallocator.instance.makeArray("update"); passes.add(pass); passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1)); } } } export static void destroy() { if (instance is null) return; with(instance) { foreach (ref system; systems) { system.disable(); if (system.m_destroy) (cast(void function(void*)) system.m_destroy)(system.m_system_pointer); if(system.jobs)Mallocator.instance.dispose(system.jobs); if(system.m_read_only_components)Mallocator.instance.dispose(system.m_read_only_components); if(system.m_modified_components)Mallocator.instance.dispose(system.m_modified_components); if(system.m_components)Mallocator.instance.dispose(system.m_components); if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components); if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components); if(system.name)Mallocator.instance.dispose(system.name); if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers); if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer); } foreach(EntityInfo* info;&entities_infos.byValue) { //if(info.components)Mallocator.instance.dispose(info.components); Mallocator.instance.dispose(info); } foreach(UpdatePass* pass; passes) { Mallocator.instance.dispose(pass); } passes.clear(); foreach(ComponentInfo info; components) { if(info.init_data)Mallocator.instance.dispose(info.init_data); } foreach(EventInfo info; events) { if(info.callers)Mallocator.instance.dispose(info.callers); } foreach(name; &components_map.byKey) { if(name)Mallocator.instance.dispose(name); } } Mallocator.instance.dispose(instance); instance = null; } /************************************************************************************************************************ *Begin registering process. Every register function should be called between beginRegister() and endRegister(). */ export void beginRegister() nothrow @nogc { assert(!register_state, "beginRegister() can't be called twice before endRegister();"); register_state = true; foreach (pass; passes) { foreach (caller; pass.system_callers) { Mallocator.instance.dispose(caller); } pass.system_callers.clear(); } } /************************************************************************************************************************ *End registering process. Every register function should be called between beginRegister() and endRegister(). */ export void endRegister() { assert(register_state, "beginRegister() should be called before endRegister();"); register_state = false; foreach (ref info; &entities_infos.byValue) { if (info.systems) Mallocator.instance.dispose(info.systems); info.systems = Mallocator.instance.makeArray!bool(systems.length); } foreach (ref system; systems) { if (system.m_update is null) { if (system.m_add_entity || system.m_remove_entity || system.m_change_entity) { foreach (info; &entities_infos.byValue) { connectListenerToEntityInfo(*info, system.id); } } continue; } bool added = false; foreach (i, caller; passes[system.m_pass].system_callers) { if (systems[caller.system_id].priority > system.priority) { SystemCaller* sys_caller = Mallocator.instance.make!SystemCaller; sys_caller.system_id = system.id; sys_caller.job_group.caller = sys_caller; passes[system.m_pass].system_callers.add(sys_caller, i); added = true; break; } } if (!added) { SystemCaller* sys_caller = Mallocator.instance.make!SystemCaller; sys_caller.system_id = system.id; sys_caller.job_group.caller = sys_caller; passes[system.m_pass].system_callers.add(sys_caller); } foreach (info; &entities_infos.byValue) { addSystemCaller(*info, system.id); //info.systems[system.id] = true; } } event_manager.allocateData(cast(uint) threads.length); foreach (ref info; events) { Mallocator.instance.dispose(info.callers); } ushort[] event_callers = (cast(ushort*) alloca(ushort.sizeof * events.length))[0 .. events.length]; foreach (ref caller; event_callers) caller = 0; foreach (ref system; systems) { foreach (caller; system.m_event_callers) { event_callers[caller.id]++; } } foreach (i, ref info; events) { info.callers = Mallocator.instance.makeArray!(EventCaller)(event_callers[i]); } foreach (ref caller; event_callers) caller = 0; foreach (ref system; systems) { foreach (caller; system.m_event_callers) { events[caller.id].callers[event_callers[caller.id]].callback = caller.callback; events[caller.id].callers[event_callers[caller.id]].system = &system; event_callers[caller.id]++; } } foreach (info; &entities_infos.byValue) { generateListeners(info); } generateDependencies(); } /************************************************************************************************************************ *Default constructor. */ this(uint threads_count) { if (threads_count == 0) threads_count = 1; threads = Mallocator.instance.makeArray!ThreadData(threads_count); id_manager.initialize(); event_manager.initialize(this); allocator = BlockAllocator(page_size, pages_in_block); //add_mutex = Mallocator.instance.make!Mutex; entity_block_alloc_mutex = Mallocator.instance.make!Mutex; //event_manager = EventManager(this); //event_manager.manager = this; } ~this() { id_manager.deinitialize(); if(threads)Mallocator.instance.dispose(threads); if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex); allocator.freeMemory(); } /************************************************************************************************************************ *Same as "void registerSystem(Sys)(int priority, int pass = 0)" but use pass name instead of id. */ void registerSystem(Sys)(int priority, const(char)[] pass_name) { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max, "Update pass (Name " ~ pass_name ~ ") doesn't exist."); registerSystem!(Sys)(priority, pass); } /************************************************************************************************************************ *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 *pass = index of UpdatePass which sholud call system update */ void registerSystem(Sys)(int priority, ushort pass = 0) { //alias STC = ParameterStorageClass; assert(register_state, "registerSystem must be called between beginRegister() and endRegister()."); assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist."); System system; system.m_pass = pass; static if (!(hasMember!(Sys, "system_id")) || !is(typeof(Sys.system_id) == ushort)) { static assert(0, "Add \"mixin ECS.System;\" in top of system structure;"); //"System should have \"__gshared ushort system_id"); } static if (!(hasMember!(Sys, "EntitiesData"))) { static assert(0, "System should gave \"EntitiesData\" struct for input components"); } static if (hasMember!(Sys, "EventInput")) { static void callEventHandler(Type)(ref EventCallData data) { Sys.EventInput input; Sys* data_system = cast(Sys*) data.system_pointer; EntityInfo* info = data.block.type_info; alias EventFields = Fields!(Sys.EventInput); foreach (ref event_field; input.tupleof) { alias EventFieldType = Unqual!(typeof(*event_field)); enum bool is_entity = is(EventFieldType == ecs.entity.Entity); static if (is_entity) { event_field = cast(Entity*) data.block.dataBegin() + data.id; continue; } else { enum long index_in_entities_data = getIndexOfTypeInEntitiesData!(Sys.EntitiesData, EventFieldType); static assert(index_in_entities_data != -1, "Component present in EventInput has to be present in EntitiesData!"); // Type present in EventInput has to be present in EntitiesData enum bool is_optional = hasUDA!(Sys.EntitiesData.tupleof[index_in_entities_data], "optional"); static if (is_optional) { event_field = null; } else { event_field = cast(EventFieldType*)(cast(void*) data.block + info.deltas[EventFieldType.component_id] + data.id * EventFieldType.sizeof); } } } data_system.handleEvent(input, *cast(Type*) data.event); } static void setEventCallers(Sys)(ref System system) { enum event_handlers_num = __traits(getOverloads, Sys, "handleEvent").length; system.m_event_callers = Mallocator.instance.makeArray!( System.EventCaller)(event_handlers_num); foreach (j, func; __traits(getOverloads, Sys, "handleEvent")) { alias EventParamType = Parameters!(__traits(getOverloads, Sys, "handleEvent")[j])[1]; system.m_event_callers[j].callback = cast( void*)&callEventHandler!(EventParamType); system.m_event_callers[j].id = EventParamType.event_id; } } static if (__traits(hasMember, Sys, "handleEvent")) { setEventCallers!(Sys)(system); } } static struct CompInfo { string name; string type; } static struct ComponentsIndices { CompInfo[] readonly; CompInfo[] excluded; CompInfo[] optional; CompInfo[] mutable; CompInfo[] req; string entites_array; } static void allocateSystemComponents(ComponentsIndices components_info)(ref System system) { size_t req = components_info.req.length; size_t opt = components_info.optional.length; size_t excluded = components_info.excluded.length; size_t read_only = components_info.readonly.length; size_t modified = components_info.mutable.length; if (req > 0) system.m_components = Mallocator.instance.makeArray!ushort(req); if (opt > 0) system.m_optional_components = Mallocator.instance.makeArray!ushort(opt); if (excluded > 0) system.m_excluded_components = Mallocator.instance.makeArray!ushort(excluded); if (read_only > 0) system.m_read_only_components = Mallocator.instance.makeArray!ushort(read_only); if (modified > 0) system.m_modified_components = Mallocator.instance.makeArray!ushort(modified); } static ComponentsIndices getComponentsInfo() { ComponentsIndices components_info; bool checkExcludedComponentsSomething(Sys)() { return __traits(compiles, allSameType!(string, typeof(Sys.ExcludedComponents))) && allSameType!(string, typeof(Sys.ExcludedComponents)) && isExpressions!(Sys.ExcludedComponents); } foreach (member; __traits(allMembers, Sys.EntitiesData)) { alias MemberType = typeof(__traits(getMember, Sys.EntitiesData, member)); if (member == "length" || is(MemberType == Entity[]) || is(MemberType == const(Entity)[])) { if(is(MemberType == Entity[]) || is(MemberType == const(Entity)[]))components_info.entites_array = member; continue; } string name; static if (isArray!MemberType) { // Workaround. This code is never called with: not an array type, but compiler prints an error name = Unqual!(ForeachType!MemberType).stringof; } bool is_optional; //bool is_excluded; bool is_read_only; if (is(CopyConstness!(ForeachType!(MemberType), int) == const(int))) { is_read_only = true; } foreach (att; __traits(getAttributes, __traits(getMember, Sys.EntitiesData, member))) { if (att == "optional") { is_optional = true; } /*else if (att == "excluded") { is_excluded = true; }*/ if (att == "readonly") { is_read_only = true; } } if (is_read_only) { components_info.readonly ~= CompInfo(member,name); } else { components_info.mutable ~= CompInfo(member,name); } /*if (is_excluded) { components_info.excluded ~= CompInfo(member,name); }*/ if (is_optional) { components_info.optional ~= CompInfo(member,name); } if (is_read_only) { components_info.readonly ~= CompInfo(member,name); } if (/*is_excluded == false &&*/ is_optional == false) { //is Req components_info.req ~= CompInfo(member,name); } /*assert(!(is_optional && is_excluded), "EntitiesData member can't have both \"@optional\" and \"@excluded\".");*/ } static if (__traits(hasMember, Sys, "ExcludedComponents")) { static if (is(Sys.ExcludedComponents == enum)) { foreach (str; __traits(allMembers, Sys.ExcludedComponents)) { components_info.excluded ~= CompInfo(str,str); } } else static if (checkExcludedComponentsSomething!Sys) { foreach (str; Sys.ExcludedComponents) { components_info.excluded ~= CompInfo(str,str); } } } return components_info; } enum ComponentsIndices components_info = getComponentsInfo(); static void genCompList()(ref System system, ref HashMap!(char[], ushort) components_map) { foreach (member; __traits(allMembers, Sys.EntitiesData)) { alias MemberType=typeof(__traits(getMember, Sys.EntitiesData, member)); 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!(MemberType), "EntitiesData 'length' member must be integral type."); static assert(MemberType.sizeof > 1, "EntitiesData 'length' member can't be byte or ubyte."); } else static if (!(isArray!(MemberType))) static assert(0, "EntitiesData members should be arrays of elements!"); } //enum ComponentsIndices components_info = getComponentsInfo(); allocateSystemComponents!(components_info)(system); foreach (iii, comp_info; components_info.req) { ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max); assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\"."); system.m_components[iii] = comp; } foreach (iii, comp_info; components_info.excluded) { ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max); assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\"."); system.m_excluded_components[iii] = comp; } foreach (iii, comp_info; components_info.optional) { ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max); assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\"."); system.m_optional_components[iii] = comp; } foreach (iii, comp_info; components_info.readonly) { ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max); assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\"."); system.m_read_only_components[iii] = comp; } foreach (iii, comp_info; components_info.mutable) { ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max); assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\"."); system.m_modified_components[iii] = comp; } } static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info, EntitiesBlock* block, uint offset, uint entities_count, System* system) { //enum ComponentsIndices components_info = getComponentsInfo(); static if(components_info.entites_array) { __traits(getMember, input_data, components_info.entites_array) = (cast(Entity*) block.dataBegin())[offset .. entities_count]; } static if(hasMember!(Sys.EntitiesData,"length")) { input_data.length = cast(typeof(input_data.length))(entities_count - offset); } static foreach (iii, comp_info; components_info.req) { __traits(getMember, input_data, comp_info.name) = (cast(ForeachType!(typeof(__traits(getMember, Sys.EntitiesData, comp_info.name)))*)(cast(void*) block + info.deltas[ system.m_components[iii]]))[offset .. entities_count]; } static foreach (iii, comp_info; components_info.optional) { if(system.m_optional_components[iii] < info.deltas.length && info.deltas[system.m_optional_components[iii]] != 0) { __traits(getMember, input_data, comp_info.name) = (cast(ForeachType!(typeof(__traits(getMember, Sys.EntitiesData, comp_info.name)))*)(cast(void*) block + info.deltas[ system.m_optional_components[iii]]))[offset .. entities_count]; } } } bool checkOnUpdateParams()() { bool ret = false; foreach (func; __traits(getOverloads, Sys, "onUpdate")) { if ((Parameters!(func)).length == 1 && is(Parameters!(func)[0] == Sys.EntitiesData)) { ret = true; break; } } return ret; } static if (hasMember!(Sys, "onUpdate") && checkOnUpdateParams()) { 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; System* system = data.system; 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; assert(entities_count <= block.entities_count && offset <= block.entities_count); fillInputData(input_data, info, block, offset, entities_count, system); s.onUpdate(input_data); block = block.next_block; offset = 0; blocks--; } } system.m_update = &callUpdate; } static void catchFunction(string func_name, RetType = void)(void** member) { static if(hasMember!(Sys,func_name)) { foreach (func; __traits(getOverloads, Sys, func_name)) { static if ((Parameters!(func)).length == 0 && is(ReturnType!(func) == RetType)) { static RetType callFunc(void* system_pointer) { Sys* s = cast(Sys*) system_pointer; static if(is(RetTyp == void))mixin("s."~func_name~"()"); else return mixin("s."~func_name~"()"); } *member = cast(void*)&callFunc; break; } } } } static void catchEntityFunction(string func_name, RetType = void)(void** member) { static if(hasMember!(Sys,func_name)) { foreach (func; __traits(getOverloads, Sys, func_name)) { static if ((Parameters!(func)).length == 1 && is(Parameters!(func)[0] == Sys.EntitiesData) && is(ReturnType!(func) == RetType)) { static RetType callFunc(ref ListenerCallData data) { Sys* s = cast(Sys*) data.system.m_system_pointer; Sys.EntitiesData input_data; fillInputData(input_data, data.block.type_info, data.block, data.begin, data.end, data.system); static if(is(RetTyp == void))mixin("s."~func_name~"(input_data)"); else return mixin("s."~func_name~"(input_data)"); } *member = cast(void*)&callFunc; break; } } } } catchFunction!("onEnable")(&system.m_enable); catchFunction!("onDisable")(&system.m_disable); catchFunction!("onCreate")(&system.m_create); catchFunction!("onDestroy")(&system.m_destroy); catchFunction!("onBegin",bool)(&system.m_begin); catchFunction!("onEnd")(&system.m_end); catchEntityFunction!("onAddEntity")(&system.m_add_entity); catchEntityFunction!("onRemoveEntity")(&system.m_remove_entity); catchEntityFunction!("onChangeEntity")(&system.m_change_entity); 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; system.jobs = Mallocator.instance.makeArray!(Job)((cast(Sys*) system.m_system_pointer).__ecs_jobs_count); genCompList(system, components_map); ushort sys_id = systems_map.get(cast(char[])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); system.m_id = sys_id; systems[sys_id] = system; } else { system.name = Mallocator.instance.makeArray(Sys.stringof); systems_map.add(system.name, cast(ushort) systems.length); system.m_id = 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 = system.id; } /************************************************************************************************************************ *Return system ECS api by id */ export System* getSystem(ushort id) nothrow @nogc { return &systems[id]; } /************************************************************************************************************************ *Return pointer to system registered in manager */ Sys* getSystem(Sys)() nothrow @nogc { return cast(Sys*) systems[Sys.system_id].m_system_pointer; } export ushort registerPass(const(char)[] name) { UpdatePass* pass = Mallocator.instance.make!UpdatePass; pass.name = Mallocator.instance.makeArray(name); passes.add(pass); passes_map.add(name, cast(ushort)(passes.length - 1)); return cast(ushort)(passes.length - 1); } /************************************************************************************************************************ *Register component into EntityManager. */ void registerComponent(Comp)() { ComponentInfo info; static if (!(hasMember!(Comp, "component_id")) || !is(typeof(Comp.component_id) == ushort)) { static assert(0, "Add \"mixin ECS.Component;\" in top of component structure;"); //"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) nothrow @nogc { (cast(void delegate() nothrow @nogc)&(cast(Comp*) pointer).onDestroy)(); } info.destroy_callback = &callDestroy; } static if (hasMember!(Comp, "onCreate") && isFunction!(Comp.onCreate) && is(ReturnType!(Comp.onCreate) == void) && Parameters!(Comp.onCreate).length == 0) { static void callCreate(void* pointer) nothrow @nogc { (cast(void delegate() nothrow @nogc)&(cast(Comp*) pointer).onCreate)(); } info.create_callback = &callCreate; } 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(cast(char[])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); char[] 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, "Add \"mixin ECS.Event;\" in top of event structure;"); //"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)); } } /************************************************************************************************************************ *Same as "void update(int pass = 0)" but use pass name instead of id. */ export void update(const(char)[] pass_name) nothrow @nogc { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max); update(pass); } /************************************************************************************************************************ *Update systems. Should be called only between begin() and end(). */ export void update(ushort pass = 0) nothrow @nogc { assert(!register_state); assert(pass < passes.length); foreach (caller; passes[pass].system_callers) { System* sys = &systems[caller.system_id]; if (sys.enabled && sys.execute) { foreach (info; caller.infos) { CallData data = CallData(caller.system_id, sys, info); data.update(); } } } } /************************************************************************************************************************ *Same as "void updateMT(int pass = 0)" but use pass name instead of id. */ export void updateMT(const(char)[] pass_name) nothrow @nogc { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max); updateMT(pass); } export void updateMT(ushort pass = 0) nothrow @nogc { assert(!register_state); assert(pass < passes.length); 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; passes[pass].system_callers) { System* sys = &systems[caller.system_id]; if (sys.enabled && sys.execute) { uint entities_count = 0; foreach (info; caller.infos) { uint blocks_count = info.nonEmptyBlocksCount(); 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.nonEmptyBlocksCount(); 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(); caller.job_group.jobs = sys.jobs[0 .. job_id]; (cast(void delegate(JobGroup) nothrow @nogc) m_dispatch_jobs)(caller.job_group); //sys.jobs[0 .. job_id]); } } } export void setJobDispachFunc(void delegate(JobGroup) func) nothrow @nogc { m_dispatch_jobs = func; } static void alignNum(ref ushort num, ushort alignment) nothrow @nogc { 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) nothrow @nogc { 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, ushort.max); 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; } info.systems = Mallocator.instance.makeArray!bool(systems.length); foreach (i, ref system; systems) { if (system.m_update is null) { if (system.m_add_entity || system.m_remove_entity || system.m_change_entity) connectListenerToEntityInfo(*info, cast(uint) i); continue; } addSystemCaller(*info, cast(uint) i); } entities_infos.add(info.components, info); generateListeners(info); } return info; } private void generateListeners(EntityInfo* info) { if (info.add_listeners) { Mallocator.instance.dispose(info.add_listeners); info.add_listeners = null; } if (info.remove_listeners) { Mallocator.instance.dispose(info.remove_listeners); info.remove_listeners = null; } if (info.change_listeners) { Mallocator.instance.dispose(info.change_listeners); info.change_listeners = null; } //allocate local data ushort[] tmp_add = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0 .. systems.length]; ushort[] tmp_rem = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0 .. systems.length]; ushort[] tmp_ch = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0 .. systems .length]; int add_len = 0; int rem_len = 0; int ch_len = 0; //assign listeners to lists foreach (i; 0 .. systems.length) { if (info.systems[i]) { System* system = &systems[i]; //onAddEntity listener if (system.m_add_entity) { //find listener position by priority int j; for (j = 0; j < add_len; j++) { if (systems[i].priority > systems[tmp_add[j]].priority) break; } add_len++; //move elements after new listener for (int k = add_len; k > j; k--) { tmp_add[k] = tmp_add[k - 1]; } //assign listener tmp_add[j] = cast(ushort) i; } //onRemoveEntity listener if (system.m_remove_entity) { //find listener position by priority int j; for (j = 0; j < rem_len; j++) { if (systems[i].priority > systems[tmp_rem[j]].priority) break; } rem_len++; //move elements after new listener for (int k = rem_len; k > j; k--) { tmp_rem[k] = tmp_rem[k - 1]; } //assign listener tmp_rem[j] = cast(ushort) i; } //onChangeEntity listener if (system.m_change_entity) { //find listener position by priority int j; for (j = 0; j < ch_len; j++) { if (systems[i].priority > systems[tmp_ch[j]].priority) break; } ch_len++; //move elements after new listener for (int k = ch_len; k > j; k--) { tmp_ch[k] = tmp_ch[k - 1]; } //assign listener tmp_ch[j] = cast(ushort) i; } } } if (add_len) { info.add_listeners = Mallocator.instance.makeArray!ushort(add_len); memcpy(info.add_listeners.ptr, tmp_add.ptr, add_len * ushort.sizeof); } if (rem_len) { info.remove_listeners = Mallocator.instance.makeArray!ushort(rem_len); memcpy(info.remove_listeners.ptr, tmp_rem.ptr, rem_len * ushort.sizeof); } if (ch_len) { info.change_listeners = Mallocator.instance.makeArray!ushort(ch_len); memcpy(info.change_listeners.ptr, tmp_ch.ptr, ch_len * ushort.sizeof); } } export void connectListenerToEntityInfo(ref EntityInfo entity, uint system_id) nothrow @nogc { System* system = &systems[system_id]; if (system.m_excluded_components) { foreach (id; system.m_excluded_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_: } entity.systems[system_id] = true; } export void addSystemCaller(ref EntityInfo info, uint system_id) nothrow @nogc { System* system = &systems[system_id]; if (system.m_excluded_components) { foreach (id; system.m_excluded_components) { foreach (id2; info.components) { if (id == id2) return; } } } foreach (id; system.m_components) { foreach (i2, id2; info.components) { if (id2 == id) goto is_; } return; is_: } info.systems[system_id] = true; uint index = 0; for (; index < passes[system.m_pass].system_callers.length; index++) { if (passes[system.m_pass].system_callers[index].system_id == system_id) break; } if (index < passes[system.m_pass].system_callers.length) { passes[system.m_pass].system_callers[index].infos.add(&info); } } /************************************************************************************************************************ *Returns pointer to entity. * *Params: *id = ID of entity */ export Entity* getEntity(EntityID id) nothrow @nogc { 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) nothrow @nogc { 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 .. EntityID.sizeof]); data.change_entities_list.add((cast(ubyte*)&num)[0 .. uint.sizeof]); 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()); if (info.remove_listeners) { foreach (listener; info.remove_listeners) { if (!new_info.systems[listener]) { callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1); } } } 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); } if (new_info.add_listeners) { foreach (listener; new_info.add_listeners) { if (!info.systems[listener]) { callAddEntityListener(&systems[listener], new_info, new_block, new_block.entities_count, new_block.entities_count + 1); } } } if (new_info.change_listeners) { foreach (listener; new_info.change_listeners) { if (info.systems[listener]) { callChangeEntityListener(&systems[listener], new_info, new_block, new_block.entities_count, new_block.entities_count + 1, del_ids); } } } new_block.entities_count++; removeEntityNoID(entity, block); } /************************************************************************************************************************ *Remove coponents from entity. * *Params: *Components = 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; } 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; 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()); if (info.remove_listeners) { foreach (listener; info.remove_listeners) { if (!new_info.systems[listener]) { callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1); } } } 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++; } } if (new_info.add_listeners) { foreach (listener; new_info.add_listeners) { if (!info.systems[listener]) { callAddEntityListener(&systems[listener], new_info, new_block, new_block.entities_count, new_block.entities_count + 1); } } } if (new_info.change_listeners) { foreach (listener; new_info.change_listeners) { if (info.systems[listener]) { callChangeEntityListener(&systems[listener], new_info, new_block, new_block.entities_count, new_block.entities_count + 1, new_ids); } } } 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 *comps = components to add */ void addComponents(Components...)(const EntityID entity_id, Components comps) nothrow @nogc { 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] = ∁ }*/ ThreadData* data = &threads[thread_id]; data.change_entities_list.add(cast(ubyte)1u); data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. EntityID.sizeof]); data.change_entities_list.add((cast(ubyte*)&num)[0 .. uint.sizeof]); 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: *template_ = 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; ushort index = 0; EntitiesBlock* block; do { block = findBlockWithFreeSpaceMT(info); index = block.added_count.atomicOp!"+="(1); } while (block.entities_count + index > info.max_entities); uint id = (block.entities_count + index - 1); //block.added_count); void* data_begin = block.dataBegin(); void* start = data_begin + EntityID.sizeof * id; 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 (components[comp].create_callback) { components[comp].create_callback( cast(void*) block + info.deltas[comp] + id * components[comp].size); } } if (index == 1) threads[thread_id].blocks_to_update.add(block); Entity* entity = cast(Entity*) start; //add_mutex.lock_nothrow(); entity.id = id_manager.getNewID(); //add_mutex.unlock_nothrow(); entity.updateID(); return *entity; } /************************************************************************************************************************ *Return block with free space for selected EntityInfo. */ private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info) nothrow @nogc { EntitiesBlock* block = info.last_block; if (block is null) { block = cast(EntitiesBlock*) allocator.getBlock(); *block = EntitiesBlock(info); block.id = 0; info.first_block = block; info.last_block = block; } else if (block.entities_count >= info.max_entities) { EntitiesBlock* new_block = cast(EntitiesBlock*) allocator.getBlock(); *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; } return block; } /************************************************************************************************************************ *Return block with free space for selected EntityInfo. Additional this function is multithread safe. */ private EntitiesBlock* findBlockWithFreeSpaceMT(EntityInfo* info) { EntitiesBlock* block = info.last_block; if (block is null) { entity_block_alloc_mutex.lock_nothrow(); scope (exit) entity_block_alloc_mutex.unlock_nothrow(); if (info.last_block != null) return info.last_block; block = cast(EntitiesBlock*) allocator.getBlock(); *block = EntitiesBlock(info); block.id = 0; info.first_block = block; info.last_block = block; } else if (block.entities_count + block.added_count > info.max_entities) { EntitiesBlock* last_block = info.last_block; entity_block_alloc_mutex.lock_nothrow(); scope (exit) entity_block_alloc_mutex.unlock_nothrow(); if (info.last_block !is last_block) return info.last_block; EntitiesBlock* new_block = cast(EntitiesBlock*) allocator.getBlock(); *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; } 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) nothrow @nogc { //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); EntityInfo* info = block.type_info; if (info.remove_listeners) { void* data_begin = block.dataBegin(); 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()); callRemoveEntityListeners(info, block, pos, pos + 1); } id_manager.releaseID(id); //release id from manager removeEntityNoID(entity, block, true); } private void removeEntityNoID(Entity* entity, EntitiesBlock* block, bool call_destructors = false) nothrow @nogc { //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) { foreach (comp; info.components) { if (components[comp].destroy_callback) { 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) nothrow @nogc { return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1))); } private void changeEntities() { foreach (ref thread; threads) { uint index = 0; uint len = cast(uint) thread.change_entities_list.length; void*[32] pointers;// = (cast(void**) alloca(num * (void*).sizeof))[0 .. num]; 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 = (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 = (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[0..num]); } } thread.change_entities_list.clear(); } } private void callAddEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow { foreach (listener; info.add_listeners) { System* system = &systems[listener]; callAddEntityListener(system, info, block, begin, end); } } private void callAddEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow { ListenerCallData data; data.system = system; data.block = block; data.begin = begin; data.end = end; (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_add_entity)(data); } private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow { foreach (listener; info.remove_listeners) { System* system = &systems[listener]; callRemoveEntityListener(system, info, block, begin, end); } } private void callRemoveEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow { ListenerCallData data; data.system = system; data.block = block; data.begin = begin; data.end = end; (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_remove_entity)(data); } private void callChangeEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end, ushort[] ch_ids) @nogc nothrow { int i = 0; int j = 0; bool is_ = false; while (1) { if (ch_ids[i] == system.m_optional_components[j]) { is_ = true; break; } else if (ch_ids[i] > system.m_optional_components[j]) { j++; if (j >= system.m_optional_components.length) break; } else { i++; if (i >= ch_ids.length) break; } } if (!is_) return; ListenerCallData data; data.system = system; data.block = block; data.begin = begin; data.end = end; (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_change_entity)(data); } private void updateBlocks() { foreach (ref thread; threads) { foreach (block; thread.blocks_to_update) { EntityInfo* info = block.type_info; ushort entities_count = block.entities_count; 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.atomicStore(cast(ushort) 0); if (info.add_listeners) { callAddEntityListeners(info, block, entities_count, block.entities_count); } } thread.blocks_to_update.clear(); } } private void removeEntities() nothrow @nogc { foreach (i, ref thread; threads) { foreach (id; thread.entities_to_remove) { __removeEntity(id); } thread.entities_to_remove.clear(); } } private void updateEvents() nothrow @nogc { bool empty = true; while (1) { event_manager.swapCurrent(); uint current_index; if (event_manager.current_index == 0) current_index = cast(uint) threads.length; else current_index = 0; foreach (i, event; event_manager.events) { foreach (first_block; event.first_blocks[current_index .. current_index + threads.length]) { EventManager.EventBlock* block = first_block; if (block) empty = false; while (block) { EventCallData call_data; void* event_pointer = cast(void*) block + event.data_offset; call_data.event = event_pointer; foreach (j; 0 .. block.count) { EntityID entity_id = *cast(EntityID*) event_pointer; Entity* entity = id_manager.getEntityPointer(entity_id); call_data.block = getMetaData(entity); static if (EntityID.sizeof == 8) call_data.id = cast(ushort)( (cast(void*) entity - call_data.block.dataBegin()) >> 3); else call_data.id = cast(ushort)( (cast(void*) entity - call_data.block.dataBegin()) / EntityID .sizeof); foreach (caller; events[i].callers) { call_data.system_pointer = caller.system.m_system_pointer; (cast(void function(ref EventCallData) nothrow @nogc) caller .callback)(call_data); } event_pointer += events[i].size; } block = block.next; } } } if (empty) break; empty = true; } } export void commit() { updateEvents(); id_manager.optimize(); updateBlocks(); removeEntities(); changeEntities(); event_manager.clearEvents(); } /************************************************************************************************************************ *Begin of update process. Should be called before any update is called. */ export void begin() { commit(); m_call_data_allocator.clear(); foreach (ref system; systems) { if (system.enabled && system.m_begin) system.m_execute = (cast(bool function(void*)) system.m_begin)( system.m_system_pointer); } } /************************************************************************************************************************ *End of update process. Should be called after every update function. */ export void end() { foreach (ref system; systems) { if (system.enabled && system.m_end) (cast(void function(void*)) system.m_end)(system.m_system_pointer); } commit(); } private void getThreadID() nothrow @nogc { if (m_thread_id_func) thread_id = (cast(uint delegate() nothrow @nogc) m_thread_id_func)(); else thread_id = 0; } void sendEvent(Ev)(EntityID id, Ev event) nothrow @nogc { event_manager.sendEvent(id, event, thread_id); } private void generateDependencies() nothrow @nogc { foreach (pass_id, pass; passes) { foreach (caller; pass.system_callers) { caller.system = &systems[caller.system_id]; if (caller.exclusion) Mallocator.instance.dispose(caller.exclusion); if (caller.dependencies) Mallocator.instance.dispose(caller.dependencies); } uint index = 0; SystemCaller*[] exclusion; exclusion = (cast(SystemCaller**) alloca((SystemCaller*) .sizeof * pass.system_callers.length))[0 .. pass.system_callers.length]; foreach (caller; pass.system_callers) { index = 0; out_for: foreach (caller2; pass.system_callers) { if ( /*caller.system.priority != caller2.system.priority ||*/ caller is caller2) continue; foreach (cmp; caller.system.m_read_only_components) { foreach (cmp2; caller2.system.m_modified_components) { if (cmp == cmp2) { exclusion[index++] = caller2; continue out_for; } } } foreach (cmp; caller.system.m_modified_components) { foreach (cmp2; caller2.system.m_read_only_components) { if (cmp == cmp2) { exclusion[index++] = caller2; continue out_for; } } foreach (cmp2; caller2.system.m_modified_components) { if (cmp == cmp2) { exclusion[index++] = caller2; continue out_for; } } } } if (index > 0) caller.exclusion = Mallocator.instance.makeArray(exclusion[0 .. index]); else caller.exclusion = null; /*import std.stdio; write("Exclusive systems for system ", caller.system.name, ": "); foreach (ex; exclusion[0 .. index]) write(ex.system.name, " "); writeln();*/ } extern (C) static int compareSystems(const void* a, const void* b) { SystemCaller* _a = *cast(SystemCaller**) a; SystemCaller* _b = *cast(SystemCaller**) b; if (_a.system.priority < _b.system.priority) return -1; else if (_a.system.priority == _b.system.priority) { if (_a.exclusion.length < _b.exclusion.length) return -1; else if (_a.exclusion.length == _b.exclusion.length) return 0; else return 1; } else return 1; } qsort(pass.system_callers.array.ptr, pass.system_callers.length, (SystemCaller*).sizeof, &compareSystems); foreach (i, caller; pass.system_callers) caller.job_group.id = cast(uint) i; int priority = int.min; uint beg = 0; index = 0; foreach (i, caller; pass.system_callers) { index = 0; foreach (ex; caller.exclusion) { if (ex.job_group.id > caller.job_group.id) continue; exclusion[index++] = ex; } if (index > 0) { caller.dependencies = Mallocator.instance.makeArray(exclusion[0 .. index]); caller.job_group.dependencies = Mallocator.instance.makeArray!( JobGroup*)(index); foreach (j, dep; caller.dependencies) { caller.job_group.dependencies[j] = &dep.job_group; } } else caller.dependencies = null; /*import std.stdio; write("Dependencies for system ", caller.system.name, ": "); foreach (ex; caller.dependencies) write(ex.system.name, " "); writeln();*/ } } } /************************************************************************************************************************ *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) nothrow @nogc destroy_callback; ///Pointer to component create callback void function(void* pointer) nothrow @nogc create_callback; } struct EventCaller { System* system; void* callback; } struct EventCallData { EntitiesBlock* block; void* system_pointer; void* event; ushort id; } struct EventInfo { ushort size; ushort alignment; EventCaller[] callers; void function(void* pointer) destroy_callback; } /************************************************************************************************************************ *Entity type info. */ struct EntityInfo { ///Returns number of blocks uint blocksCount() nothrow @nogc { if (last_block) return last_block.id + 1; else return 0; } ///Returns number of non empty blocks uint nonEmptyBlocksCount() nothrow @nogc { EntitiesBlock* block = last_block; while (1) { if (block is null) return 0; if (block.entities_count == 0) block = block.prev_block; else return block.id + 1; } } ~this() { if(components)Mallocator.instance.dispose(components); if(deltas)Mallocator.instance.dispose(deltas); if(tmpl_deltas)Mallocator.instance.dispose(tmpl_deltas); if(systems)Mallocator.instance.dispose(systems); if(add_listeners)Mallocator.instance.dispose(add_listeners); if(remove_listeners)Mallocator.instance.dispose(remove_listeners); if(change_listeners)Mallocator.instance.dispose(change_listeners); } ///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; ///array of systems which will update this entity bool[] systems; ///systems which are listening for added entities ushort[] add_listeners; ///systems which are listening for removed entities ushort[] remove_listeners; ///systems which are listening for changed entities (changed in term of contained components) ushort[] change_listeners; ///pointer to first block/page EntitiesBlock* first_block; ///pointer to last block EntitiesBlock* last_block; } /************************************************************************************************************************ *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 export uint dataDelta() nothrow @nogc { ushort dif = EntitiesBlock.sizeof; alignNum(dif, type_info.alignment); return dif; } ///return pointer to first element in block export void* dataBegin() nothrow @nogc { 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. * *first_block, begin, end, blocks parameters are used *to call partial info update */ struct CallData { export void update() nothrow @nogc { (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 ListenerCallData { System* system; EntitiesBlock* block; uint begin; uint end; } struct Job { CallData[] callers; export void execute() nothrow @nogc { EntityManager.instance.getThreadID(); foreach (ref caller; callers) { caller.update(); } } } struct JobGroup { Job[] jobs; JobGroup*[] dependencies; uint id; SystemCaller* caller; //uint max_jobs; } struct SystemCaller { ~this() nothrow @nogc { if (dependencies) { Mallocator.instance.dispose(dependencies); } if(exclusion) { Mallocator.instance.dispose(exclusion); } if (job_group.dependencies) Mallocator.instance.dispose(job_group.dependencies); } uint system_id; System* system; Vector!(EntityInfo*) infos; SystemCaller*[] dependencies; SystemCaller*[] exclusion; JobGroup job_group; } struct ThreadData { Vector!EntityID entities_to_remove; Vector!ubyte change_entities_list; Vector!(EntitiesBlock*) blocks_to_update; } export struct UpdatePass { export ~this() nothrow @nogc { assert(name); if (name) Mallocator.instance.dispose(name); foreach(caller; system_callers) { Mallocator.instance.dispose(caller); } system_callers.clear(); } char[] name; Vector!(SystemCaller*) system_callers; } static uint thread_id; ThreadData[] threads; Vector!(UpdatePass*) passes; bool register_state = false; alias SytemFuncType = void function(ref EntityManager.CallData data) nothrow @nogc; ///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; void delegate(JobGroup jobs) m_dispatch_jobs; uint delegate() m_thread_id_func; HashMap!(ushort[], EntityInfo*) entities_infos; HashMap!(char[], ushort) systems_map; HashMap!(char[], ushort) components_map; HashMap!(const(char)[], ushort) events_map; HashMap!(const(char)[], ushort) passes_map; Vector!System systems; Vector!ComponentInfo components; Vector!EventInfo events; //Mutex add_mutex; Mutex entity_block_alloc_mutex; CallDataAllocator m_call_data_allocator; struct CallDataAllocator { struct Block { CallData[256] data; uint allocated = 0; } ~this() { foreach(block;blocks) { Mallocator.instance.dispose(block); } blocks.clear(); } Vector!(Block*) blocks; uint id; void clear() nothrow @nogc { if (blocks.length > 0) foreach (block; blocks[0 .. id + 1]) { block.allocated = 0; } id = 0; //blocks.clear(); } CallData[] getCallData(uint num) nothrow @nogc { 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; } } export __gshared EntityManager instance = null; } /* static ulong defaultHashFunc(T)(auto ref T t) { ulong ret = 0; foreach(id;t) { ret = ret } }*/