Skip to content

Commit

Permalink
Optimize getting active objects a bit. #8674
Browse files Browse the repository at this point in the history
  • Loading branch information
lhofhansl committed Jul 16, 2019
1 parent 4122969 commit 9fe3246
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/client/activeobjectmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ void ActiveObjectMgr::removeObject(u16 id)
void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
std::vector<DistanceSortedActiveObject> &dest)
{
f32 max_d2 = max_d * max_d;
for (auto &ao_it : m_active_objects) {
ClientActiveObject *obj = ao_it.second;

f32 d = (obj->getPosition() - origin).getLength();
f32 d2 = (obj->getPosition() - origin).getLengthSQ();

if (d > max_d)
if (d2 > max_d2)
continue;

dest.emplace_back(obj, d);
dest.emplace_back(obj, d2);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/client/clientobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class ClientActiveObject : public ActiveObject
static std::unordered_map<u16, Factory> m_types;
};

struct DistanceSortedActiveObject
class DistanceSortedActiveObject
{
public:
ClientActiveObject *obj;
f32 d;

DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
{
Expand All @@ -105,4 +105,7 @@ struct DistanceSortedActiveObject
{
return d < other.d;
}

private:
f32 d;
};
3 changes: 2 additions & 1 deletion src/server/activeobjectmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ void ActiveObjectMgr::removeObject(u16 id)
void ActiveObjectMgr::getObjectsInsideRadius(
const v3f &pos, float radius, std::vector<u16> &result)
{
float r2 = radius * radius;
for (auto &activeObject : m_active_objects) {
ServerActiveObject *obj = activeObject.second;
u16 id = activeObject.first;
const v3f &objectpos = obj->getBasePosition();
if (objectpos.getDistanceFrom(pos) > radius)
if (objectpos.getDistanceFromSQ(pos) > r2)
continue;
result.push_back(id);
}
Expand Down

0 comments on commit 9fe3246

Please sign in to comment.