First Working Compile

This commit is contained in:
ExeVirus 2024-05-06 23:24:17 -04:00
parent 97e9f37bd8
commit 66866ba342
11 changed files with 39 additions and 33 deletions

View File

@ -45,7 +45,7 @@ public:
// destructed.
}
virtual void step(float dtime, const std::function<void(T *, v3f& lastPosition, bool &position_changed)> &f) = 0;
virtual void step(float dtime, const std::function<void(T *)> &f) = 0;
virtual bool registerObject(std::unique_ptr<T> obj) = 0;
virtual void removeObject(u16 id) = 0;

View File

@ -46,15 +46,13 @@ void ActiveObjectMgr::clearIf(const std::function<bool(ServerActiveObject *, u16
}
}
void ActiveObjectMgr::invalidateCachedObjectID(u16 id, v3f &last_position, bool &position_changed)
void ActiveObjectMgr::invalidateCachedObjectID(u16 id, v3f &last_position)
{
if(position_changed) {
m_spatial_map.invalidate(id, last_position);
}
m_spatial_map.invalidate(id, last_position);
}
void ActiveObjectMgr::step(
float dtime, const std::function<void(ServerActiveObject *, v3f &last_position, bool &position_changed)> &f)
float dtime, const std::function<void(ServerActiveObject *)> &f)
{
size_t count = 0;
@ -64,6 +62,7 @@ void ActiveObjectMgr::step(
count++;
f(ao_it.second.get());
}
m_spatial_map.cacheUpdate([this](u16 id){ return getActiveObject(id)->getBasePosition(); });
g_profiler->avg("ActiveObjectMgr: SAO count [#]", count);
}

View File

@ -35,10 +35,10 @@ public:
// If cb returns true, the obj will be deleted
void clearIf(const std::function<bool(ServerActiveObject *, u16)> &cb);
void step(float dtime,
const std::function<void(ServerActiveObject *, v3f &last_position, bool &position_changed)> &f) override;
const std::function<void(ServerActiveObject *)> &f) override;
bool registerObject(std::unique_ptr<ServerActiveObject> obj) override;
void removeObject(u16 id) override;
void invalidateCachedObjectID(u16 id, v3f &last_position, bool &position_changed);
void invalidateCachedObjectID(u16 id, v3f &last_position);
void getObjectsInsideRadius(const v3f &pos, float radius,
std::vector<ServerActiveObject *> &result,

View File

@ -132,9 +132,8 @@ void LuaEntitySAO::dispatchScriptDeactivate(bool removal)
m_env->getScriptIface()->luaentity_Deactivate(m_id, removal);
}
void LuaEntitySAO::step(float dtime, bool send_recommended, v3f &last_position, bool &position_changed)
void LuaEntitySAO::step(float dtime, bool send_recommended)
{
last_position = m_base_position;
if (!m_properties_sent) {
m_properties_sent = true;
std::string str = getPropertyPacket();
@ -242,7 +241,6 @@ void LuaEntitySAO::step(float dtime, bool send_recommended, v3f &last_position,
}
sendOutdatedData();
position_changed = last_position != m_base_position;
}
std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)

View File

@ -40,7 +40,7 @@ public:
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_LUAENTITY; }
ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
virtual void addedToEnvironment(u32 dtime_s);
void step(float dtime, bool send_recommended, v3f& last_position, bool &position_changed) override;
void step(float dtime, bool send_recommended) override;
std::string getClientInitializationData(u16 protocol_version);
bool isStaticAllowed() const { return m_prop.static_save; }

View File

@ -154,9 +154,8 @@ void PlayerSAO::getStaticData(std::string * result) const
FATAL_ERROR("This function shall not be called for PlayerSAO");
}
void PlayerSAO::step(float dtime, bool send_recommended, v3f& last_position, bool &position_changed)
void PlayerSAO::step(float dtime, bool send_recommended)
{
last_position = m_last_good_position;
if (!isImmortal() && m_drowning_interval.step(dtime, 2.0f)) {
// Get nose/mouth position, approximate with eye position
v3s16 p = floatToInt(getEyePosition(), BS);
@ -308,7 +307,6 @@ void PlayerSAO::step(float dtime, bool send_recommended, v3f& last_position, boo
}
sendOutdatedData();
position_changed = last_position != m_base_position;
}
std::string PlayerSAO::generateUpdatePhysicsOverrideCommand() const

View File

@ -87,7 +87,7 @@ public:
bool shouldUnload() const override { return false; }
std::string getClientInitializationData(u16 protocol_version) override;
void getStaticData(std::string *result) const override;
void step(float dtime, bool send_recommended, v3f& last_position, bool &position_changed) override;
void step(float dtime, bool send_recommended) override;
void setBasePosition(v3f position);
void setPos(const v3f &pos) override;
void addPos(const v3f &added_pos) override;

View File

@ -107,7 +107,7 @@ public:
same time so that the data can be combined in a single
packet.
*/
virtual void step(float dtime, bool send_recommended, v3f& last_position, bool &position_changed){}
virtual void step(float dtime, bool send_recommended){}
/*
The return value of this is passed to the client-side object

View File

@ -17,8 +17,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "spatial_map.h"
namespace server
@ -35,7 +33,8 @@ void SpatialMap::invalidate(u16 id, v3f &pos)
{
// remove from cache, if present
bool found = false;
auto range = m_cached.equal_range(SpatialKey(pos));
SpatialKey key(pos);
auto range = m_cached.equal_range(key);
for (auto it = range.first; it != range.second; ++it) {
if (it->second == id) {
m_cached.erase(it);
@ -73,12 +72,11 @@ void SpatialMap::remove(u16 id, v3f pos)
}
// Only when at least 64 uncached objects or 10% uncached overall
void SpatialMap::cacheUpdate(ActiveObjectMgr& mgr)
void SpatialMap::cacheUpdate(const std::function<v3f(u16)> &getPos)
{
bool shouldUpdate = false;
if(m_uncached.size() >= 64 || (m_uncached.size() >= 64 && m_uncached.size() * 10 > m_cached.size())) {
for(u16& entry : m_uncached) {
m_cached.insert(std::pair<SpatialKey,u16>(mgr.getActiveObject(entry)->getBasePosition(), entry));
m_cached.insert(std::pair<SpatialKey,u16>(getPos(entry), entry));
}
m_uncached.clear();
}
@ -104,7 +102,9 @@ void SpatialMap::getRelevantObjectIds(const aabb3f &box, std::vector<u16> &relev
SpatialKey key(x,y,z);
if (m_cached.find(key) != m_cached.end()) {
auto range = m_cached.equal_range(key);
relevant_objs.insert(relevant_objs.end(), range.first, range.second);
for (auto &it = range.first; it != range.second; ++it) {
relevant_objs.push_back(it->second);
}
}
}
}

View File

@ -21,8 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <functional>
#include <vector>
#include "serveractiveobject.h"
#include "activeobjectmgr.h"
#include "irrlichttypes_bloated.h"
namespace server
{
@ -33,13 +32,13 @@ public:
void insert(u16 id);
// Invalidates upon position update or removal
void invalidate(u16 id, v3f& pos);
void invalidate(u16 id, v3f &pos);
// On active_object removal, remove.
void remove(u16 id, v3f pos);
// Only when at least 64 uncached objects or 10% uncached overall
void cacheUpdate(::server::ActiveObjectMgr &mgr);
void cacheUpdate(const std::function<v3f(u16)> &getPos);
// Use the same basic algorithm for both area and radius lookups
void getRelevantObjectIds(const aabb3f &box, std::vector<u16> &relevant_objs);
@ -57,9 +56,19 @@ protected:
z = _z >> 4;
}
SpatialKey(v3f _pos) : SpatialKey(_pos.X, _pos.Y, _pos.Z){}
bool operator==(const SpatialKey& other) const {
return (x == other.x && y == other.y && z == other.z);
}
} SpatialKey;
std::unordered_multimap<SpatialKey, u16> m_cached;
struct SpatialKeyHash {
auto operator()(const SpatialKey &key) const -> size_t {
return std::hash<size_t>()(*reinterpret_cast<const size_t*>(&key));
}
};
std::unordered_multimap<SpatialKey, u16, SpatialKeyHash> m_cached;
std::vector<u16> m_uncached;
};
} // namespace server

View File

@ -1587,10 +1587,12 @@ void ServerEnvironment::step(float dtime)
object_count++;
// Step object
v3f last_position;
bool position_changed;
obj->step(dtime, send_recommended, last_position, position_changed);
m_ao_manager.
v3f last_position = obj->getBasePosition();
obj->step(dtime, send_recommended);
if(last_position != obj->getBasePosition()) {
m_ao_manager.invalidateCachedObjectID(obj->getId(), last_position);
}
// Read messages from object
obj->dumpAOMessagesToQueue(m_active_object_messages);
};