/************************************************************************************************************************
*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;
alias gEM = EntityManager.instance;
alias gEntityManager = EntityManager.instance;
alias gEventManager = EntityManager.instance;
alias SerializeVector = ecs.vector.Vector!ubyte;
/************************************************************************************************************************
*Entity manager is responsible for everything.
*/
class EntityManager
{
export static void initialize(uint threads_count)
{
if (instance is null)
instance = Mallocator.instance.make!EntityManager(threads_count);
}
export static void destroy()
{
if (instance is null)
return;
foreach (ref system; instance.systems)
{
system.disable();
}
foreach (ref system; instance.systems)
{
if (system.m_destroy)
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
}
Mallocator.instance.dispose(instance);
instance = null;
}
/************************************************************************************************************************
*Begin registering process. Every register function should be called between beginRegister() and endRegister().
*/
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().
*/
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)
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);
}
}
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;
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));
}
/************************************************************************************************************************
*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");
}
//dfmt off
static if(hasMember!(Sys,"EventInput"))
{
static string genEventCompList()()
{
string ret = "Sys.EventInput input;\n";
string type;
//bool has = true;
foreach (member; __traits(allMembers, Sys.EventInput))
{
if(mixin("fullyQualifiedName!(ConstOf!(PointerTarget!(typeof(Sys.EventInput."~member~")))) == \"const(ecs.entity.Entity)\""))
{
ret ~= "input." ~ member ~ " = cast(Entity*) data.block.dataBegin() + data.id;\n";
continue;
}
bool has = false;
bool optional = false;
foreach(member2; __traits(allMembers, Sys.EntitiesData))
{
static if(mixin("isBasicType!(typeof(Sys.EntitiesData."~member2~"))")){}
else static if(mixin("fullyQualifiedName!(ConstOf!(PointerTarget!(typeof(Sys.EventInput."~member~")))) == fullyQualifiedName!(ConstOf!(ForeachType!(typeof(Sys.EntitiesData."~member2~"))))"))
{
has = true;
if(mixin("hasUDA!(Sys.EntitiesData."~member2~",\"optional\")"))optional = true;
break;
}
}
if(!has)assert(0);
if(optional)ret ~= "input." ~ member ~ " = null;\n";
else ret ~= "input." ~ member ~ " = cast(typeof(Sys.EventInput." ~ member
~ "))(cast(void*) data.block + info.deltas[typeof(Sys.EventInput."
~ member ~ ").component_id] + data.id * typeof(Sys.EventInput." ~ member
~ ").sizeof);\n";
}
return ret;
}
//pragma(msg,genEventCompList());
//pragma(msg,genEventCompList());
static string checkHandler()(string member)
{
string ret;
ret ~= "(Parameters!(Sys."~member~")).length == 2 && ";
ret ~= "((is(Parameters!(Sys."~member~")[0] == Sys.EventInput) && hasStaticMember!(Parameters!(Sys."~member~")[1],\"event_id\")) ||";
ret ~= " (is(Parameters!(Sys."~member~")[1] == Sys.EventInput) && hasStaticMember!(Parameters!(Sys."~member~")[0],\"event_id\")))";
return ret;
}
/*static struct Handler
{
ushort id;
void* callback;
}*/
static string catchEventHanlders()()
{
int event_handlers = 0;
string ret;
string event_param;
static if(__traits(hasMember, Sys, "handleEvent"))
{
foreach(func; __traits(getOverloads, Sys, "handleEvent"))
{
event_handlers++;//pragma(msg,"kupa");
}
//ret ~= "Handler[] handlers = (cast(Handler*)alloca("~event_handlers.to!string~" * (Handler).sizeof))[0 .. "~event_handlers.to!string~"];\n";
ret ~= "system.m_event_callers = Mallocator.instance.makeArray!(System.EventCaller)("~event_handlers.to!string~");";
event_handlers = 0;
foreach(j,func; __traits(getOverloads, Sys, "handleEvent"))
{
event_param = "Parameters!(__traits(getOverloads, Sys, \"handleEvent\")["~j.to!string~"])[1]";
ret ~= "static void callHandler"~event_handlers.to!string~"(ref EventCallData data)\n{\n";
ret ~= "Sys* s = cast(Sys*) data.system_pointer;
EntityInfo* info = data.block.type_info;";
ret ~= genEventCompList();
ret ~= "s.handleEvent(input, *cast("~event_param~"*)data.event);";
ret ~= "}\n";
ret ~= "system.m_event_callers["~event_handlers.to!string~"].callback = cast(void*)&callHandler"~event_handlers.to!string~";";
ret ~= "system.m_event_callers["~event_handlers.to!string~"].id = "~event_param~".event_id;";
event_handlers++;
}
/*ret ~= "ushort max_id = 0;";
ret ~= "foreach(handler;handlers)
{
if(handler.id > max_id)max_id = handler.id;
}";
ret ~= "system.m_event_callback = Mallocator.instance.makeArray!(System.EventHandler)(max_id);";
ret ~= "foreach(handler;handlers)
{
system.m_event_callback[handler.id] = handler.callback;
}";*/
}
/*static if(__traits(hasMember, Sys, "handleEvent"))//foreach(member; __traits(allMembers,Sys))
{
//static foreach()
static if (member == "handleEvent" && mixin("isFunction!(Sys."~member~")"))
{
static if(mixin(checkHandler(member)))
{
event_handlers++;
}
}
}*/
return ret;
}
//pragma(msg,catchEventHanlders());
mixin(catchEventHanlders());
}
//dfmt on
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;
uint req;
uint opt;
uint excluded;
uint read_only;
uint modified;
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;
bool is_read_only = false;
int attribs = 0;
if (is(CopyConstness!(ForeachType!(typeof(mixin("Sys.EntitiesData." ~ member))),
int) == const(int)))
{
is_read_only = true;
}
foreach (att; __traits(getAttributes,
__traits(getMember, Sys.EntitiesData, member)))
{
if (att == "optional")
{
opt++;
attribs++;
//break;
}
else if (att == "excluded")
{
excluded++;
attribs++;
//break;
}
if (att == "readonly")
{
is_read_only = true;
}
}
assert(attribs <= 1,
"EntitiesData member can't have both \"@optional\" and \"@excluded\".");
if (!attribs)
req++;
if (is_read_only)
read_only++;
else
modified++;
}
}
}
static if (__traits(hasMember, Sys, "ExcludedComponents"))
{
static if (is(Sys.ExcludedComponents == enum))
{
excluded += (Fields!(Sys.ExcludedComponents)).length; //static assert(0,"Enum ExcludedComponents are not implemented yet.");
}
else static if (__traits(compiles, allSameType!(string, typeof(Sys.ExcludedComponents)))
&& allSameType!(string, typeof(Sys.ExcludedComponents))
&& isExpressions!(Sys.ExcludedComponents))
{
excluded += Sys.ExcludedComponents.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 (excluded > 0)
ret ~= "system.m_excluded_components = Mallocator.instance.makeArray!ushort("
~ excluded.to!string ~ ");";
if (read_only > 0)
ret ~= "system.m_read_only_components = Mallocator.instance.makeArray!ushort("
~ read_only.to!string ~ ");";
if (modified > 0)
ret ~= "system.m_modified_components = Mallocator.instance.makeArray!ushort("
~ modified.to!string ~ ");";
ret ~= "ushort comp;"; //uint opt = 0;uint req = 0;uint excluded = 0;";
opt = 0;
req = 0;
excluded = 0;
read_only = 0;
modified = 0;
static if (__traits(hasMember, Sys, "ExcludedComponents"))
{
static if (is(Sys.ExcludedComponents == enum))
{
//static assert(0,"Enum ExcludedComponents are not implemented yet.");
foreach (str; Fields!(Sys.ExcludedComponents))
ret ~= "system.m_excluded_components[" ~ (excluded++)
.to!string ~ "] = components_map.get(\""
~ str.stringof ~ "\", ushort.max);";
}
else static if (__traits(compiles, allSameType!(string, typeof(Sys.ExcludedComponents)))
&& allSameType!(string, typeof(Sys.ExcludedComponents))
&& isExpressions!(Sys.ExcludedComponents))
{
foreach (str; Sys.ExcludedComponents)
ret ~= "system.m_excluded_components[" ~ (excluded++)
.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 is_read_only = false;
bool has_att = false;
if (is(CopyConstness!(ForeachType!(typeof(mixin("Sys.EntitiesData." ~ member))),
int) == const(int)))
{
is_read_only = true;
}
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 == "excluded")
{
ret ~= "system.m_excluded_components[" ~ (excluded++)
.to!string ~ "] = comp;";
has_att = true;
//break;
}
if (att == "readonly")
{
is_read_only = true;
}
}
if (!has_att)
{
ret ~= "system.m_components[" ~ (req++).to!string ~ "] = comp;";
}
if (is_read_only)
ret ~= "system.m_read_only_components[" ~ (read_only++)
.to!string ~ "] = comp;";
else
ret ~= "system.m_modified_components[" ~ (modified++)
.to!string ~ "] = comp;";
ret ~= "}";
}
}
}
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 string genFillInputData()()
{
string ret;
ushort comp;
uint req = 0;
uint opt = 0;
uint excluded = 0;
foreach (member; __traits(allMembers, Sys.EntitiesData))
{
if (is(typeof(__traits(getMember, Sys.EntitiesData,
member)) == Entity[]) || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == const(Entity)[]))
{
ret ~= "input_data." ~ member
~ " = (cast(Entity*) block.dataBegin())[offset .. entities_count];";
}
else if (member == "length")
{
ret ~= "input_data." ~ member
~ " = cast(typeof(input_data.length))(entities_count - offset);";
}
else
{
{
bool has_att = false;
foreach (att; __traits(getAttributes,
__traits(getMember, Sys.EntitiesData, member)))
{
if (att == "optional")
{
ret ~= "if(system.m_optional_components[" ~ opt.to!string
~ "] < info.deltas.length && info.deltas[ system.m_optional_components["
~ opt.to!string ~ "]] != 0)input_data." ~ member
~ " = (cast(ForeachType!(typeof(Sys.EntitiesData." ~ member
~ "))*)(cast(void*) block + info.deltas[ system.m_optional_components["
~ opt.to!string ~ "]]))[offset .. entities_count];
else input_data."
~ member ~ " = null;";
opt++;
has_att = true;
break;
}
else if (att == "excluded")
{
excluded++;
has_att = true;
break;
}
}
if (!has_att)
{
ret ~= "input_data." ~ member ~ " = (cast(ForeachType!(typeof(Sys.EntitiesData." ~ member
~ "))*)(cast(void*) block + info.deltas[ system.m_components["
~ req.to!string ~ "]]))[offset .. entities_count];";
req++;
}
}
}
}
return ret;
}
//pragma(msg,genFillInputData);
static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info, EntitiesBlock* block,
uint offset, uint entities_count, System* system)
{
mixin(genFillInputData());
}
static if (hasMember!(Sys, "update") && (mixin(genParamsChecking())))
{
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);
//mixin(genFillInputData());
s.update(input_data);
block = block.next_block;
offset = 0;
blocks--;
}
}
system.m_update = &callUpdate;
}
static string catchFunc(RetType = void)(string member, string func)
{
//dfmt off
static if(is(RetType == void))string ret_str = "";
else string ret_str = "return ";
string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\") &&
Parameters!(Sys."~func~").length == 0 &&
is(ReturnType!(Sys."~func~") == "~RetType.stringof~"))
{
static "~RetType.stringof~" call" ~ func
~ "(void* system_pointer)
{
Sys* s = cast(Sys*) system_pointer;
"~ret_str~"s." ~ func ~ "();
}
system."
~ member ~ " = &call" ~ func ~ ";
}";
return ret;
//dfmt on
}
static string catchEntityFunc(RetType = void)(string member, string func)
{
//dfmt off
static if(is(RetType == void))string ret_str = "";
else string ret_str = "return ";
string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\") &&
Parameters!(Sys."~func~").length == 1 &&
is(Parameters!(Sys."~func~")[0] == Sys.EntitiesData) &&
is(ReturnType!(Sys."~func~") == "~RetType.stringof~"))
{
static "~RetType.stringof~" call" ~ func
~ "(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);
"~ret_str~"s." ~ func ~ "(input_data);
}
system."
~ member ~ " = &call" ~ func ~ ";
}";
return ret;
//dfmt on
}
mixin(catchFunc("m_enable", "onEnable"));
mixin(catchFunc("m_disable", "onDisable"));
mixin(catchFunc("m_create", "onCreate"));
mixin(catchFunc("m_destroy", "onDestroy"));
mixin(catchFunc!(bool)("m_begin", "onBegin"));
mixin(catchFunc("m_end", "onEnd"));
mixin(catchEntityFunc("m_entity_added","onAdd"));
mixin(catchEntityFunc("m_entity_removed","onRemove"));
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
system.m_priority = priority;
(cast(Sys*) system.m_system_pointer).__ecsInitialize();
system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
mixin(genCompList());
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
if (sys_id < systems.length)
{
system.enable();
/*if (systems[sys_id].m_destroy)
systems[sys_id].m_destroy(systems[sys_id].m_system_pointer);*/
if (system.m_create)
(cast(void function(void*)) system.m_create)(system.m_system_pointer);
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
*/
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;
}
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(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);
const(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.
*/
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.
*/
void updateMT(const(char)[] pass_name) nothrow @nogc
{
ushort pass = passes_map.get(pass_name, ushort.max);
assert(pass != ushort.max);
updateMT(pass);
}
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]);
}
}
}
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);
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)
continue;
addSystemCaller(*info, cast(uint)i);
}
entities_infos.add(info.components, info);
generateListeners(info);
}
return info;
}
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;
}
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];
int add_len = 0;
int rem_len = 0;
foreach(i;0..systems.length)
{
if(info.systems[i])
{
System* system = &systems[i];
if(system.m_entity_added)tmp_add[add_len++] = cast(ushort)i;
if(system.m_entity_removed)tmp_rem[rem_len++] = 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_add.ptr,rem_len*ushort.sizeof);
}
}
export void addSystemCaller(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_:
}
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(&entity);
entity.systems[system_id] = true;
}
/************************************************************************************************************************
*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 .. 8]);
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
data.change_entities_list.add(cast(ubyte[]) del_ids);
}
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
{
Entity* entity = id_manager.getEntityPointer(entity_id);
if (!entity)
return;
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
qsort(del_ids.ptr, del_ids.length, ushort.sizeof, &compareUShorts);
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length)))[0
.. info.components.length];
uint j = 0;
uint k = 0;
foreach (id; info.components)
{
while (k < del_ids.length && del_ids[k] < id)
k++;
if (k >= del_ids.length)
{
ids[j++] = id;
}
else if (del_ids[k] == info.components[j])
{
k++;
}
else
ids[j++] = id;
}
if (j == info.components.length)
return;
EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
void* start = new_block.dataBegin() + new_block.entities_count * EntityID.sizeof;
Entity* new_entity = cast(Entity*) start;
new_entity.id = entity.id;
new_entity.updateID();
static if (EntityID.sizeof == 8)
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
foreach (comp; new_info.components)
{
uint comp_size = components[comp].size;
memcpy(cast(void*) new_block + new_info.deltas[comp] + new_block.entities_count * comp_size,
cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size);
}
new_block.entities_count++;
removeEntityNoID(entity, block);
}
/************************************************************************************************************************
*Remove coponents from entity.
*
*Params:
*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());
foreach (ref id; ids[0 .. len])
{
void* dst = cast(void*) new_block + new_info.deltas[id] + (
new_block.entities_count /*+ new_block.added_count*/ ) * components[id].size;
uint size = components[id].size;
if (k >= new_ids.length)
{
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
else if (j >= info.components.length)
{
memcpy(dst, data_pointers[k], size);
k++;
}
else if (id == new_ids[k])
{
memcpy(dst, data_pointers[k], size);
k++;
}
else
{
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
j++;
}
}
new_block.entities_count++;
removeEntityNoID(entity, block);
}
/************************************************************************************************************************
*Add components to entity. Components will be added on end of frame.
*
*Params:
*entity_id = ID of entity to remove
*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(1);
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
data.change_entities_list.add(cast(ubyte[]) new_ids);
static foreach (i, comp; comps)
{
data.change_entities_list.add((cast(ubyte*)&comp)[0 .. comp.sizeof]);
}
//__addComponents(entity_id, new_ids, pointers);
}
/************************************************************************************************************************
*Free template memory.
*
*Params:
*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;
while (index < len)
{
if (!thread.change_entities_list[index++])
{
EntityID id = *cast(EntityID*)&thread.change_entities_list[index];
index += EntityID.sizeof;
uint num = *cast(uint*)&thread.change_entities_list[index];
index += uint.sizeof;
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
index += ushort.sizeof * num;
__removeComponents(id, ids);
}
else
{
EntityID id = *cast(EntityID*)&thread.change_entities_list[index];
index += EntityID.sizeof;
uint num = *cast(uint*)&thread.change_entities_list[index];
index += uint.sizeof;
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
index += ushort.sizeof * num;
void*[] pointers = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
foreach (i; 0 .. num)
{
pointers[i] = &thread.change_entities_list[index];
index += components[ids[i]].size;
}
__addComponents(id, ids, pointers);
}
}
thread.change_entities_list.clear();
}
}
private void callAddEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
foreach(listener;info.add_listeners)
{
System* system = &systems[listener];
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data);
}
}
private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
foreach(listener;info.add_listeners)
{
System* system = &systems[listener];
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data);
}
}
private void updateBlocks()
{
foreach (ref thread; threads)
{
foreach (block; thread.blocks_to_update)
{
EntityInfo* info = block.type_info;
if(info.add_listeners)
{
callAddEntityListeners(info,block,block.entities_count,block.entities_count+block.added_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);
}
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();
}
}
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;
}
}
///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;
///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
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
{
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;
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);
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;
}
struct UpdatePass
{
~this() nothrow @nogc
{
assert(name);
if (name)
Mallocator.instance.dispose(name);
}
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!(const(char)[], ushort) systems_map;
HashMap!(const(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;
}
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;
}
}
__gshared EntityManager instance = null;
}
/*
static ulong defaultHashFunc(T)(auto ref T t)
{
ulong ret = 0;
foreach(id;t)
{
ret = ret
}
}*/