diff options
author | ShadowNinja <shadowninja@minetest.net> | 2013-11-30 12:11:07 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2013-11-30 13:05:13 -0500 |
commit | 4594ba652293e776ccba2184c16502a346f4147a (patch) | |
tree | 19713fac8de559ba86cd9d08d9c07cb1c7241e37 /src/script/lua_api/l_env.cpp | |
parent | 06baf05c641355ead97e9428c4455af9e8b11cef (diff) | |
download | minetest-4594ba652293e776ccba2184c16502a346f4147a.tar.gz minetest-4594ba652293e776ccba2184c16502a346f4147a.tar.bz2 minetest-4594ba652293e776ccba2184c16502a346f4147a.zip |
Optimize table creation
Diffstat (limited to 'src/script/lua_api/l_env.cpp')
-rw-r--r-- | src/script/lua_api/l_env.cpp | 45 |
1 files changed, 14 insertions, 31 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index a33882bdf..4a8150396 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -448,28 +448,20 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L) // minetest.get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { - // Get the table insert function - lua_getglobal(L, "table"); - lua_getfield(L, -1, "insert"); - int table_insert = lua_gettop(L); - GET_ENV_PTR; // Do it v3f pos = checkFloatPos(L, 1); float radius = luaL_checknumber(L, 2) * BS; std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); - lua_newtable(L); - int table = lua_gettop(L); - for(std::set<u16>::const_iterator - i = ids.begin(); i != ids.end(); i++){ - ServerActiveObject *obj = env->getActiveObject(*i); + ScriptApiBase *script = getScriptApiBase(L); + lua_createtable(L, ids.size(), 0); + std::set<u16>::const_iterator iter = ids.begin(); + for(u32 i = 0; iter != ids.end(); iter++) { + ServerActiveObject *obj = env->getActiveObject(*iter); // Insert object reference into table - lua_pushvalue(L, table_insert); - lua_pushvalue(L, table); - getScriptApiBase(L)->objectrefGetOrCreate(obj); - if(lua_pcall(L, 2, 0, 0)) - script_error(L); + script->objectrefGetOrCreate(obj); + lua_rawseti(L, -2, ++i); } return 1; } @@ -579,25 +571,16 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L) ndef->getIds(lua_tostring(L, 3), filter); } - // Get the table insert function - lua_getglobal(L, "table"); - lua_getfield(L, -1, "insert"); - int table_insert = lua_gettop(L); - lua_newtable(L); - int table = lua_gettop(L); - for(s16 x=minp.X; x<=maxp.X; x++) - for(s16 y=minp.Y; y<=maxp.Y; y++) - for(s16 z=minp.Z; z<=maxp.Z; z++) - { - v3s16 p(x,y,z); + u64 i = 0; + for(s16 x = minp.X; x <= maxp.X; x++) + for(s16 y = minp.Y; y <= maxp.Y; y++) + for(s16 z = minp.Z; z <= maxp.Z; z++) { + v3s16 p(x, y, z); content_t c = env->getMap().getNodeNoEx(p).getContent(); - if(filter.count(c) != 0){ - lua_pushvalue(L, table_insert); - lua_pushvalue(L, table); + if(filter.count(c) != 0) { push_v3s16(L, p); - if(lua_pcall(L, 2, 0, 0)) - script_error(L); + lua_rawseti(L, -2, ++i); } } return 1; |