From 9fe32461540316d9ef06b2f06c02684c36c5fa94 Mon Sep 17 00:00:00 2001 From: Lars Hofhansl Date: Tue, 16 Jul 2019 15:55:17 -0700 Subject: Optimize getting active objects a bit. #8674 --- src/client/activeobjectmgr.cpp | 7 ++++--- src/client/clientobject.h | 7 +++++-- src/server/activeobjectmgr.cpp | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/client/activeobjectmgr.cpp b/src/client/activeobjectmgr.cpp index 4ed98d79b..9fd8490f2 100644 --- a/src/client/activeobjectmgr.cpp +++ b/src/client/activeobjectmgr.cpp @@ -91,15 +91,16 @@ void ActiveObjectMgr::removeObject(u16 id) void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d, std::vector &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); } } diff --git a/src/client/clientobject.h b/src/client/clientobject.h index 9377d1e67..5e34177e4 100644 --- a/src/client/clientobject.h +++ b/src/client/clientobject.h @@ -90,10 +90,10 @@ private: static std::unordered_map m_types; }; -struct DistanceSortedActiveObject +class DistanceSortedActiveObject { +public: ClientActiveObject *obj; - f32 d; DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d) { @@ -105,4 +105,7 @@ struct DistanceSortedActiveObject { return d < other.d; } + +private: + f32 d; }; diff --git a/src/server/activeobjectmgr.cpp b/src/server/activeobjectmgr.cpp index 56febd76e..01d9d5ae8 100644 --- a/src/server/activeobjectmgr.cpp +++ b/src/server/activeobjectmgr.cpp @@ -115,11 +115,12 @@ void ActiveObjectMgr::removeObject(u16 id) void ActiveObjectMgr::getObjectsInsideRadius( const v3f &pos, float radius, std::vector &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); } -- cgit v1.2.3