1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38: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

@ -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;
}