1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Custom data structure for active objects to get performance *and* safety (#13880)

This commit is contained in:
sfan5 2024-01-17 20:04:56 +01:00 committed by GitHub
parent 08ee6d8d4b
commit 0383c44f0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 434 additions and 78 deletions

View file

@ -37,17 +37,14 @@ ActiveObjectMgr::~ActiveObjectMgr()
void ActiveObjectMgr::step(
float dtime, const std::function<void(ClientActiveObject *)> &f)
{
g_profiler->avg("ActiveObjectMgr: CAO count [#]", m_active_objects.size());
// Same as in server activeobjectmgr.
std::vector<u16> ids = getAllIds();
for (u16 id : ids) {
auto it = m_active_objects.find(id);
if (it == m_active_objects.end())
continue; // obj was removed
f(it->second.get());
size_t count = 0;
for (auto &ao_it : m_active_objects.iter()) {
if (!ao_it.second)
continue;
count++;
f(ao_it.second.get());
}
g_profiler->avg("ActiveObjectMgr: CAO count [#]", count);
}
bool ActiveObjectMgr::registerObject(std::unique_ptr<ClientActiveObject> obj)
@ -71,7 +68,7 @@ bool ActiveObjectMgr::registerObject(std::unique_ptr<ClientActiveObject> obj)
}
infostream << "Client::ActiveObjectMgr::registerObject(): "
<< "added (id=" << obj->getId() << ")" << std::endl;
m_active_objects[obj->getId()] = std::move(obj);
m_active_objects.put(obj->getId(), std::move(obj));
return true;
}
@ -79,16 +76,14 @@ void ActiveObjectMgr::removeObject(u16 id)
{
verbosestream << "Client::ActiveObjectMgr::removeObject(): "
<< "id=" << id << std::endl;
auto it = m_active_objects.find(id);
if (it == m_active_objects.end()) {
std::unique_ptr<ClientActiveObject> obj = m_active_objects.take(id);
if (!obj) {
infostream << "Client::ActiveObjectMgr::removeObject(): "
<< "id=" << id << " not found" << std::endl;
return;
}
std::unique_ptr<ClientActiveObject> obj = std::move(it->second);
m_active_objects.erase(it);
obj->removeFromScene(true);
}
@ -96,8 +91,10 @@ 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) {
for (auto &ao_it : m_active_objects.iter()) {
ClientActiveObject *obj = ao_it.second.get();
if (!obj)
continue;
f32 d2 = (obj->getPosition() - origin).getLengthSQ();
@ -114,8 +111,10 @@ std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObje
f32 max_d = shootline.getLength();
v3f dir = shootline.getVector().normalize();
for (auto &ao_it : m_active_objects) {
for (auto &ao_it : m_active_objects.iter()) {
ClientActiveObject *obj = ao_it.second.get();
if (!obj)
continue;
aabb3f selection_box;
if (!obj->getSelectionBox(&selection_box))