Skip to content

Commit 9fe3246

Browse files
committedJul 16, 2019
Optimize getting active objects a bit. #8674
1 parent 4122969 commit 9fe3246

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed
 

‎src/client/activeobjectmgr.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ void ActiveObjectMgr::removeObject(u16 id)
9191
void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
9292
std::vector<DistanceSortedActiveObject> &dest)
9393
{
94+
f32 max_d2 = max_d * max_d;
9495
for (auto &ao_it : m_active_objects) {
9596
ClientActiveObject *obj = ao_it.second;
9697

97-
f32 d = (obj->getPosition() - origin).getLength();
98+
f32 d2 = (obj->getPosition() - origin).getLengthSQ();
9899

99-
if (d > max_d)
100+
if (d2 > max_d2)
100101
continue;
101102

102-
dest.emplace_back(obj, d);
103+
dest.emplace_back(obj, d2);
103104
}
104105
}
105106

‎src/client/clientobject.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ class ClientActiveObject : public ActiveObject
9090
static std::unordered_map<u16, Factory> m_types;
9191
};
9292

93-
struct DistanceSortedActiveObject
93+
class DistanceSortedActiveObject
9494
{
95+
public:
9596
ClientActiveObject *obj;
96-
f32 d;
9797

9898
DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
9999
{
@@ -105,4 +105,7 @@ struct DistanceSortedActiveObject
105105
{
106106
return d < other.d;
107107
}
108+
109+
private:
110+
f32 d;
108111
};

‎src/server/activeobjectmgr.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ void ActiveObjectMgr::removeObject(u16 id)
115115
void ActiveObjectMgr::getObjectsInsideRadius(
116116
const v3f &pos, float radius, std::vector<u16> &result)
117117
{
118+
float r2 = radius * radius;
118119
for (auto &activeObject : m_active_objects) {
119120
ServerActiveObject *obj = activeObject.second;
120121
u16 id = activeObject.first;
121122
const v3f &objectpos = obj->getBasePosition();
122-
if (objectpos.getDistanceFrom(pos) > radius)
123+
if (objectpos.getDistanceFromSQ(pos) > r2)
123124
continue;
124125
result.push_back(id);
125126
}

0 commit comments

Comments
 (0)
Please sign in to comment.