mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Optimize get_objects_inside_radius calls (#9671)
* Optimize getObjectsInsideRadius calls our previous implementation calls the ActiveObjectMgr to return ids and then lookup those ids in the same map and test each object Instead now we call the global map to return the pointers directly and we ask filtering when building the list using lamba. This drop double looping over ranges of active objects (and then filtered one) and drop x lookups on the map regarding the first call results
This commit is contained in:
parent
62ae7adab2
commit
e8ac5a31cf
7 changed files with 55 additions and 39 deletions
|
@ -681,22 +681,22 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
|
|||
int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
|
||||
{
|
||||
GET_ENV_PTR;
|
||||
ScriptApiBase *script = getScriptApiBase(L);
|
||||
|
||||
// Do it
|
||||
v3f pos = checkFloatPos(L, 1);
|
||||
float radius = readParam<float>(L, 2) * BS;
|
||||
std::vector<u16> ids;
|
||||
env->getObjectsInsideRadius(ids, pos, radius);
|
||||
ScriptApiBase *script = getScriptApiBase(L);
|
||||
lua_createtable(L, ids.size(), 0);
|
||||
std::vector<u16>::const_iterator iter = ids.begin();
|
||||
for(u32 i = 0; iter != ids.end(); ++iter) {
|
||||
ServerActiveObject *obj = env->getActiveObject(*iter);
|
||||
if (!obj->isGone()) {
|
||||
// Insert object reference into table
|
||||
script->objectrefGetOrCreate(L, obj);
|
||||
lua_rawseti(L, -2, ++i);
|
||||
}
|
||||
std::vector<ServerActiveObject *> objs;
|
||||
|
||||
auto include_obj_cb = [](ServerActiveObject *obj){ return !obj->isGone(); };
|
||||
env->getObjectsInsideRadius(objs, pos, radius, include_obj_cb);
|
||||
|
||||
int i = 0;
|
||||
lua_createtable(L, objs.size(), 0);
|
||||
for (const auto obj : objs) {
|
||||
// Insert object reference into table
|
||||
script->objectrefGetOrCreate(L, obj);
|
||||
lua_rawseti(L, -2, ++i);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue