Use std::vector instead of std::set for Environment::getObjectsInsideRadius

We are only iterating sequentially, we don't need a set here
Also use a vector reference instead of a copy
master
Loic Blot 2015-04-16 14:11:46 +02:00
parent d02300a749
commit 0c634a9719
4 changed files with 9 additions and 9 deletions

View File

@ -318,8 +318,9 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env); ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
if (s_env != 0) { if (s_env != 0) {
f32 distance = speed_f.getLength(); f32 distance = speed_f.getLength();
std::set<u16> s_objects = s_env->getObjectsInsideRadius(pos_f,distance * 1.5); std::vector<u16> s_objects;
for (std::set<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++) { s_env->getObjectsInsideRadius(s_objects, pos_f, distance * 1.5);
for (std::vector<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++) {
ServerActiveObject *current = s_env->getActiveObject(*iter); ServerActiveObject *current = s_env->getActiveObject(*iter);
if ((self == 0) || (self != current)) { if ((self == 0) || (self != current)) {
objects.push_back((ActiveObject*)current); objects.push_back((ActiveObject*)current);

View File

@ -840,9 +840,8 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
return true; return true;
} }
std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius) void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius)
{ {
std::set<u16> objects;
for(std::map<u16, ServerActiveObject*>::iterator for(std::map<u16, ServerActiveObject*>::iterator
i = m_active_objects.begin(); i = m_active_objects.begin();
i != m_active_objects.end(); ++i) i != m_active_objects.end(); ++i)
@ -852,9 +851,8 @@ std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
v3f objectpos = obj->getBasePosition(); v3f objectpos = obj->getBasePosition();
if(objectpos.getDistanceFrom(pos) > radius) if(objectpos.getDistanceFrom(pos) > radius)
continue; continue;
objects.insert(id); objects.push_back(id);
} }
return objects;
} }
void ServerEnvironment::clearAllObjects() void ServerEnvironment::clearAllObjects()

View File

@ -305,7 +305,7 @@ public:
bool swapNode(v3s16 p, const MapNode &n); bool swapNode(v3s16 p, const MapNode &n);
// Find all active objects inside a radius around a point // Find all active objects inside a radius around a point
std::set<u16> getObjectsInsideRadius(v3f pos, float radius); void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
// Clear all objects, loading and going through every MapBlock // Clear all objects, loading and going through every MapBlock
void clearAllObjects(); void clearAllObjects();

View File

@ -438,10 +438,11 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
// Do it // Do it
v3f pos = checkFloatPos(L, 1); v3f pos = checkFloatPos(L, 1);
float radius = luaL_checknumber(L, 2) * BS; float radius = luaL_checknumber(L, 2) * BS;
std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); std::vector<u16> ids;
env->getObjectsInsideRadius(ids, pos, radius);
ScriptApiBase *script = getScriptApiBase(L); ScriptApiBase *script = getScriptApiBase(L);
lua_createtable(L, ids.size(), 0); lua_createtable(L, ids.size(), 0);
std::set<u16>::const_iterator iter = ids.begin(); std::vector<u16>::const_iterator iter = ids.begin();
for(u32 i = 0; iter != ids.end(); iter++) { for(u32 i = 0; iter != ids.end(); iter++) {
ServerActiveObject *obj = env->getActiveObject(*iter); ServerActiveObject *obj = env->getActiveObject(*iter);
// Insert object reference into table // Insert object reference into table