1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-27 17:28:41 +00:00

Allow managing object observers

-----

Co-authored-by: sfan5 <sfan5@live.de>
Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
This commit is contained in:
Lars Mueller 2023-11-12 15:28:29 +01:00 committed by sfan5
parent cc8e7a569e
commit 6874c358ea
15 changed files with 284 additions and 22 deletions

View file

@ -118,6 +118,16 @@ void ActiveObjectMgr::removeObject(u16 id)
}
}
void ActiveObjectMgr::invalidateActiveObjectObserverCaches()
{
for (auto &active_object : m_active_objects.iter()) {
ServerActiveObject *obj = active_object.second.get();
if (!obj)
continue;
obj->invalidateEffectiveObservers();
}
}
void ActiveObjectMgr::getObjectsInsideRadius(const v3f &pos, float radius,
std::vector<ServerActiveObject *> &result,
std::function<bool(ServerActiveObject *obj)> include_obj_cb)
@ -153,15 +163,18 @@ void ActiveObjectMgr::getObjectsInArea(const aabb3f &box,
}
}
void ActiveObjectMgr::getAddedActiveObjectsAroundPos(v3f player_pos, f32 radius,
f32 player_radius, const std::set<u16> &current_objects,
void ActiveObjectMgr::getAddedActiveObjectsAroundPos(
const v3f &player_pos, const std::string &player_name,
f32 radius, f32 player_radius,
const std::set<u16> &current_objects,
std::vector<u16> &added_objects)
{
/*
Go through the object list,
- discard removed/deactivated objects,
- discard objects that are too far away,
- discard objects that are found in current_objects.
- discard objects that are found in current_objects,
- discard objects that are not observed by the player.
- add remaining objects to added_objects
*/
for (auto &ao_it : m_active_objects.iter()) {
@ -183,6 +196,9 @@ void ActiveObjectMgr::getAddedActiveObjectsAroundPos(v3f player_pos, f32 radius,
} else if (distance_f > radius)
continue;
if (!object->isEffectivelyObservedBy(player_name))
continue;
// Discard if already on current_objects
auto n = current_objects.find(id);
if (n != current_objects.end())

View file

@ -38,15 +38,18 @@ public:
bool registerObject(std::unique_ptr<ServerActiveObject> obj) override;
void removeObject(u16 id) override;
void invalidateActiveObjectObserverCaches();
void getObjectsInsideRadius(const v3f &pos, float radius,
std::vector<ServerActiveObject *> &result,
std::function<bool(ServerActiveObject *obj)> include_obj_cb);
void getObjectsInArea(const aabb3f &box,
std::vector<ServerActiveObject *> &result,
std::function<bool(ServerActiveObject *obj)> include_obj_cb);
void getAddedActiveObjectsAroundPos(v3f player_pos, f32 radius,
f32 player_radius, const std::set<u16> &current_objects,
void getAddedActiveObjectsAroundPos(
const v3f &player_pos, const std::string &player_name,
f32 radius, f32 player_radius,
const std::set<u16> &current_objects,
std::vector<u16> &added_objects);
};
} // namespace server

View file

@ -18,11 +18,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "serveractiveobject.h"
#include <fstream>
#include "inventory.h"
#include "inventorymanager.h"
#include "constants.h" // BS
#include "log.h"
ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
ActiveObject(0),
@ -95,3 +93,48 @@ InventoryLocation ServerActiveObject::getInventoryLocation() const
{
return InventoryLocation();
}
void ServerActiveObject::invalidateEffectiveObservers()
{
m_effective_observers.reset();
}
using Observers = ServerActiveObject::Observers;
const Observers &ServerActiveObject::getEffectiveObservers()
{
if (m_effective_observers) // cached
return *m_effective_observers;
auto parent = getParent();
if (parent == nullptr)
return *(m_effective_observers = m_observers);
auto parent_observers = parent->getEffectiveObservers();
if (!parent_observers) // parent is unmanaged
return *(m_effective_observers = m_observers);
if (!m_observers) // we are unmanaged
return *(m_effective_observers = parent_observers);
// Set intersection between parent_observers and m_observers
// Avoid .clear() to free the allocated memory.
m_effective_observers = std::unordered_set<std::string>();
for (const auto &observer_name : *m_observers) {
if (parent_observers->count(observer_name) > 0)
(*m_effective_observers)->insert(observer_name);
}
return *m_effective_observers;
}
const Observers& ServerActiveObject::recalculateEffectiveObservers()
{
// Invalidate final observers for this object and all of its parents.
for (auto obj = this; obj != nullptr; obj = obj->getParent())
obj->invalidateEffectiveObservers();
// getEffectiveObservers will now be forced to recalculate.
return getEffectiveObservers();
}
bool ServerActiveObject::isEffectivelyObservedBy(const std::string &player_name)
{
auto effective_observers = getEffectiveObservers();
return !effective_observers || effective_observers->count(player_name) > 0;
}

View file

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cassert>
#include <unordered_set>
#include <optional>
#include "irrlichttypes_bloated.h"
#include "activeobject.h"
#include "itemgroup.h"
@ -236,7 +237,25 @@ public:
*/
v3s16 m_static_block = v3s16(1337,1337,1337);
// Names of players to whom the object is to be sent, not considering parents.
using Observers = std::optional<std::unordered_set<std::string>>;
Observers m_observers;
/// Invalidate final observer cache. This needs to be done whenever
/// the observers of this object or any of its ancestors may have changed.
void invalidateEffectiveObservers();
/// Cache `m_effective_observers` with the names of all observers,
/// also indirect observers (object attachment chain).
const Observers &getEffectiveObservers();
/// Force a recalculation of final observers (including all parents).
const Observers &recalculateEffectiveObservers();
/// Whether the object is sent to `player_name`
bool isEffectivelyObservedBy(const std::string &player_name);
protected:
// Cached intersection of m_observers of this object and all its parents.
std::optional<Observers> m_effective_observers;
virtual void onMarkedForDeactivation() {}
virtual void onMarkedForRemoval() {}