Skip to content

Commit 0c634a9

Browse files
committedApr 16, 2015
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
1 parent d02300a commit 0c634a9

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed
 

Diff for: ‎src/collision.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
318318
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
319319
if (s_env != 0) {
320320
f32 distance = speed_f.getLength();
321-
std::set<u16> s_objects = s_env->getObjectsInsideRadius(pos_f,distance * 1.5);
322-
for (std::set<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++) {
321+
std::vector<u16> s_objects;
322+
s_env->getObjectsInsideRadius(s_objects, pos_f, distance * 1.5);
323+
for (std::vector<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++) {
323324
ServerActiveObject *current = s_env->getActiveObject(*iter);
324325
if ((self == 0) || (self != current)) {
325326
objects.push_back((ActiveObject*)current);

Diff for: ‎src/environment.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,8 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
840840
return true;
841841
}
842842

843-
std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
843+
void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius)
844844
{
845-
std::set<u16> objects;
846845
for(std::map<u16, ServerActiveObject*>::iterator
847846
i = m_active_objects.begin();
848847
i != m_active_objects.end(); ++i)
@@ -852,9 +851,8 @@ std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
852851
v3f objectpos = obj->getBasePosition();
853852
if(objectpos.getDistanceFrom(pos) > radius)
854853
continue;
855-
objects.insert(id);
854+
objects.push_back(id);
856855
}
857-
return objects;
858856
}
859857

860858
void ServerEnvironment::clearAllObjects()

Diff for: ‎src/environment.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class ServerEnvironment : public Environment
305305
bool swapNode(v3s16 p, const MapNode &n);
306306

307307
// Find all active objects inside a radius around a point
308-
std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
308+
void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
309309

310310
// Clear all objects, loading and going through every MapBlock
311311
void clearAllObjects();

Diff for: ‎src/script/lua_api/l_env.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,11 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
438438
// Do it
439439
v3f pos = checkFloatPos(L, 1);
440440
float radius = luaL_checknumber(L, 2) * BS;
441-
std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
441+
std::vector<u16> ids;
442+
env->getObjectsInsideRadius(ids, pos, radius);
442443
ScriptApiBase *script = getScriptApiBase(L);
443444
lua_createtable(L, ids.size(), 0);
444-
std::set<u16>::const_iterator iter = ids.begin();
445+
std::vector<u16>::const_iterator iter = ids.begin();
445446
for(u32 i = 0; iter != ids.end(); iter++) {
446447
ServerActiveObject *obj = env->getActiveObject(*iter);
447448
// Insert object reference into table

0 commit comments

Comments
 (0)
Please sign in to comment.