|
@@ -37,6 +37,7 @@ namespace script_world
|
|
|
{
|
|
{
|
|
|
ScriptInstance create(ScriptWorld& sw, UnitId unit, const ScriptDesc& desc)
|
|
ScriptInstance create(ScriptWorld& sw, UnitId unit, const ScriptDesc& desc)
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
CE_ASSERT(!hash_map::has(sw._map, unit), "Unit already has script component");
|
|
CE_ASSERT(!hash_map::has(sw._map, unit), "Unit already has script component");
|
|
|
|
|
|
|
|
u32 script_i = hash_map::get(sw._cache
|
|
u32 script_i = hash_map::get(sw._cache
|
|
@@ -62,6 +63,7 @@ namespace script_world
|
|
|
LuaStack stack = sw._lua_environment->execute(lr);
|
|
LuaStack stack = sw._lua_environment->execute(lr);
|
|
|
stack.push_value(0);
|
|
stack.push_value(0);
|
|
|
sd.module_ref = luaL_ref(stack.L, LUA_REGISTRYINDEX);
|
|
sd.module_ref = luaL_ref(stack.L, LUA_REGISTRYINDEX);
|
|
|
|
|
+ stack.pop(1);
|
|
|
|
|
|
|
|
array::push_back(sw._script, sd);
|
|
array::push_back(sw._script, sd);
|
|
|
hash_map::set(sw._cache, desc.script_resource, script_i);
|
|
hash_map::set(sw._cache, desc.script_resource, script_i);
|
|
@@ -76,6 +78,7 @@ namespace script_world
|
|
|
hash_map::set(sw._map, unit, instance_i);
|
|
hash_map::set(sw._map, unit, instance_i);
|
|
|
|
|
|
|
|
LuaStack stack(sw._lua_environment->L);
|
|
LuaStack stack(sw._lua_environment->L);
|
|
|
|
|
+ stack.push_function(LuaEnvironment::error);
|
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sd.module_ref);
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sd.module_ref);
|
|
|
lua_getfield(stack.L, -1, "spawned");
|
|
lua_getfield(stack.L, -1, "spawned");
|
|
|
stack.push_world(sw._world);
|
|
stack.push_world(sw._world);
|
|
@@ -83,7 +86,8 @@ namespace script_world
|
|
|
stack.push_key_begin(1);
|
|
stack.push_key_begin(1);
|
|
|
stack.push_unit(unit);
|
|
stack.push_unit(unit);
|
|
|
stack.push_key_end();
|
|
stack.push_key_end();
|
|
|
- stack.call(0);
|
|
|
|
|
|
|
+ lua_pcall(stack.L, 2, 0, -5);
|
|
|
|
|
+ stack.pop(2);
|
|
|
|
|
|
|
|
return script_world_internal::make_instance(instance_i);
|
|
return script_world_internal::make_instance(instance_i);
|
|
|
}
|
|
}
|
|
@@ -98,6 +102,7 @@ namespace script_world
|
|
|
const u32 script_i = sw._data[unit_i].script_i;
|
|
const u32 script_i = sw._data[unit_i].script_i;
|
|
|
|
|
|
|
|
LuaStack stack(sw._lua_environment->L);
|
|
LuaStack stack(sw._lua_environment->L);
|
|
|
|
|
+ stack.push_function(LuaEnvironment::error);
|
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sw._script[script_i].module_ref);
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sw._script[script_i].module_ref);
|
|
|
lua_getfield(stack.L, -1, "unspawned");
|
|
lua_getfield(stack.L, -1, "unspawned");
|
|
|
stack.push_world(sw._world);
|
|
stack.push_world(sw._world);
|
|
@@ -105,7 +110,8 @@ namespace script_world
|
|
|
stack.push_key_begin(1);
|
|
stack.push_key_begin(1);
|
|
|
stack.push_unit(unit);
|
|
stack.push_unit(unit);
|
|
|
stack.push_key_end();
|
|
stack.push_key_end();
|
|
|
- stack.call(0);
|
|
|
|
|
|
|
+ lua_pcall(stack.L, 2, 0, -5);
|
|
|
|
|
+ stack.pop(2);
|
|
|
|
|
|
|
|
sw._data[unit_i] = sw._data[last_i];
|
|
sw._data[unit_i] = sw._data[last_i];
|
|
|
hash_map::set(sw._map, last_u, unit_i);
|
|
hash_map::set(sw._map, last_u, unit_i);
|
|
@@ -119,15 +125,20 @@ namespace script_world
|
|
|
|
|
|
|
|
void update(ScriptWorld& sw, f32 dt)
|
|
void update(ScriptWorld& sw, f32 dt)
|
|
|
{
|
|
{
|
|
|
|
|
+ LuaStack stack(sw._lua_environment->L);
|
|
|
|
|
+ stack.push_function(LuaEnvironment::error);
|
|
|
|
|
+
|
|
|
for (u32 i = 0; i < array::size(sw._script); ++i)
|
|
for (u32 i = 0; i < array::size(sw._script); ++i)
|
|
|
{
|
|
{
|
|
|
- LuaStack stack(sw._lua_environment->L);
|
|
|
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sw._script[i].module_ref);
|
|
lua_rawgeti(stack.L, LUA_REGISTRYINDEX, sw._script[i].module_ref);
|
|
|
lua_getfield(stack.L, -1, "update");
|
|
lua_getfield(stack.L, -1, "update");
|
|
|
stack.push_world(sw._world);
|
|
stack.push_world(sw._world);
|
|
|
stack.push_float(dt);
|
|
stack.push_float(dt);
|
|
|
- stack.call(0);
|
|
|
|
|
|
|
+ lua_pcall(stack.L, 2, 0, -5);
|
|
|
|
|
+ stack.pop(1);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ stack.pop(1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // namespace script_world
|
|
} // namespace script_world
|