mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-27 17:28:41 +00:00
Add spatial index for objects (#14631)
This commit is contained in:
parent
bed36139db
commit
a3648b0b16
17 changed files with 982 additions and 116 deletions
|
@ -26,7 +26,7 @@ void ActiveObjectMgr::clearIf(const std::function<bool(ServerActiveObject *, u16
|
|||
continue;
|
||||
if (cb(it.second.get(), it.first)) {
|
||||
// Remove reference from m_active_objects
|
||||
m_active_objects.remove(it.first);
|
||||
removeObject(it.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,16 +68,17 @@ bool ActiveObjectMgr::registerObject(std::unique_ptr<ServerActiveObject> obj)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (objectpos_over_limit(obj->getBasePosition())) {
|
||||
v3f p = obj->getBasePosition();
|
||||
const v3f pos = obj->getBasePosition();
|
||||
if (objectpos_over_limit(pos)) {
|
||||
warningstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
|
||||
<< "object position (" << p.X << "," << p.Y << "," << p.Z
|
||||
<< "object position (" << pos.X << "," << pos.Y << "," << pos.Z
|
||||
<< ") outside maximum range" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto obj_id = obj->getId();
|
||||
m_active_objects.put(obj_id, std::move(obj));
|
||||
m_spatial_index.insert(pos.toArray(), obj_id);
|
||||
|
||||
auto new_size = m_active_objects.size();
|
||||
verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
|
||||
|
@ -100,6 +101,8 @@ void ActiveObjectMgr::removeObject(u16 id)
|
|||
if (!ok) {
|
||||
infostream << "Server::ActiveObjectMgr::removeObject(): "
|
||||
<< "id=" << id << " not found" << std::endl;
|
||||
} else {
|
||||
m_spatial_index.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,43 +116,47 @@ void ActiveObjectMgr::invalidateActiveObjectObserverCaches()
|
|||
}
|
||||
}
|
||||
|
||||
void ActiveObjectMgr::getObjectsInsideRadius(const v3f &pos, float radius,
|
||||
void ActiveObjectMgr::updateObjectPos(u16 id, v3f pos)
|
||||
{
|
||||
// HACK defensively only update if we already know the object,
|
||||
// otherwise we're still waiting to be inserted into the index
|
||||
// (or have already been removed).
|
||||
if (m_active_objects.get(id))
|
||||
m_spatial_index.update(pos.toArray(), id);
|
||||
}
|
||||
|
||||
void ActiveObjectMgr::getObjectsInsideRadius(v3f pos, float radius,
|
||||
std::vector<ServerActiveObject *> &result,
|
||||
std::function<bool(ServerActiveObject *obj)> include_obj_cb)
|
||||
{
|
||||
float r2 = radius * radius;
|
||||
for (auto &activeObject : m_active_objects.iter()) {
|
||||
ServerActiveObject *obj = activeObject.second.get();
|
||||
if (!obj)
|
||||
continue;
|
||||
const v3f &objectpos = obj->getBasePosition();
|
||||
if (objectpos.getDistanceFromSQ(pos) > r2)
|
||||
continue;
|
||||
float r_squared = radius * radius;
|
||||
m_spatial_index.rangeQuery((pos - v3f(radius)).toArray(), (pos + v3f(radius)).toArray(), [&](auto objPos, u16 id) {
|
||||
if (v3f(objPos).getDistanceFromSQ(pos) > r_squared)
|
||||
return;
|
||||
|
||||
auto obj = m_active_objects.get(id).get();
|
||||
if (!obj)
|
||||
return;
|
||||
if (!include_obj_cb || include_obj_cb(obj))
|
||||
result.push_back(obj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ActiveObjectMgr::getObjectsInArea(const aabb3f &box,
|
||||
std::vector<ServerActiveObject *> &result,
|
||||
std::function<bool(ServerActiveObject *obj)> include_obj_cb)
|
||||
{
|
||||
for (auto &activeObject : m_active_objects.iter()) {
|
||||
ServerActiveObject *obj = activeObject.second.get();
|
||||
m_spatial_index.rangeQuery(box.MinEdge.toArray(), box.MaxEdge.toArray(), [&](auto _, u16 id) {
|
||||
auto obj = m_active_objects.get(id).get();
|
||||
if (!obj)
|
||||
continue;
|
||||
const v3f &objectpos = obj->getBasePosition();
|
||||
if (!box.isPointInside(objectpos))
|
||||
continue;
|
||||
|
||||
return;
|
||||
if (!include_obj_cb || include_obj_cb(obj))
|
||||
result.push_back(obj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ActiveObjectMgr::getAddedActiveObjectsAroundPos(
|
||||
const v3f &player_pos, const std::string &player_name,
|
||||
v3f player_pos, const std::string &player_name,
|
||||
f32 radius, f32 player_radius,
|
||||
const std::set<u16> ¤t_objects,
|
||||
std::vector<u16> &added_objects)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <vector>
|
||||
#include "../activeobjectmgr.h"
|
||||
#include "serveractiveobject.h"
|
||||
#include "util/k_d_tree.h"
|
||||
|
||||
namespace server
|
||||
{
|
||||
|
@ -25,16 +26,21 @@ public:
|
|||
|
||||
void invalidateActiveObjectObserverCaches();
|
||||
|
||||
void getObjectsInsideRadius(const v3f &pos, float radius,
|
||||
void updateObjectPos(u16 id, v3f pos);
|
||||
|
||||
void getObjectsInsideRadius(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(
|
||||
const v3f &player_pos, const std::string &player_name,
|
||||
v3f player_pos, const std::string &player_name,
|
||||
f32 radius, f32 player_radius,
|
||||
const std::set<u16> ¤t_objects,
|
||||
std::vector<u16> &added_objects);
|
||||
|
||||
private:
|
||||
k_d_tree::DynamicKdTrees<3, f32, u16> m_spatial_index;
|
||||
};
|
||||
} // namespace server
|
||||
|
|
|
@ -147,7 +147,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|||
// Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
|
||||
// If the object gets detached this comes into effect automatically from the last known origin
|
||||
if (auto *parent = getParent()) {
|
||||
m_base_position = parent->getBasePosition();
|
||||
setBasePosition(parent->getBasePosition());
|
||||
m_velocity = v3f(0,0,0);
|
||||
m_acceleration = v3f(0,0,0);
|
||||
} else {
|
||||
|
@ -155,7 +155,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|||
aabb3f box = m_prop.collisionbox;
|
||||
box.MinEdge *= BS;
|
||||
box.MaxEdge *= BS;
|
||||
v3f p_pos = m_base_position;
|
||||
v3f p_pos = getBasePosition();
|
||||
v3f p_velocity = m_velocity;
|
||||
v3f p_acceleration = m_acceleration;
|
||||
moveresult = collisionMoveSimple(m_env, m_env->getGameDef(),
|
||||
|
@ -165,11 +165,11 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|||
moveresult_p = &moveresult;
|
||||
|
||||
// Apply results
|
||||
m_base_position = p_pos;
|
||||
setBasePosition(p_pos);
|
||||
m_velocity = p_velocity;
|
||||
m_acceleration = p_acceleration;
|
||||
} else {
|
||||
m_base_position += (m_velocity + m_acceleration * 0.5f * dtime) * dtime;
|
||||
addPos((m_velocity + m_acceleration * 0.5f * dtime) * dtime);
|
||||
m_velocity += dtime * m_acceleration;
|
||||
}
|
||||
|
||||
|
@ -212,7 +212,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|||
} else if(m_last_sent_position_timer > 0.2){
|
||||
minchange = 0.05*BS;
|
||||
}
|
||||
float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
|
||||
float move_d = getBasePosition().getDistanceFrom(m_last_sent_position);
|
||||
move_d += m_last_sent_move_precision;
|
||||
float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
|
||||
if (move_d > minchange || vel_d > minchange ||
|
||||
|
@ -236,7 +236,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
|
|||
os << serializeString16(m_init_name); // name
|
||||
writeU8(os, 0); // is_player
|
||||
writeU16(os, getId()); //id
|
||||
writeV3F32(os, m_base_position);
|
||||
writeV3F32(os, getBasePosition());
|
||||
writeV3F32(os, m_rotation);
|
||||
writeU16(os, m_hp);
|
||||
|
||||
|
@ -365,7 +365,7 @@ void LuaEntitySAO::setPos(const v3f &pos)
|
|||
{
|
||||
if(isAttached())
|
||||
return;
|
||||
m_base_position = pos;
|
||||
setBasePosition(pos);
|
||||
sendPosition(false, true);
|
||||
}
|
||||
|
||||
|
@ -373,7 +373,7 @@ void LuaEntitySAO::moveTo(v3f pos, bool continuous)
|
|||
{
|
||||
if(isAttached())
|
||||
return;
|
||||
m_base_position = pos;
|
||||
setBasePosition(pos);
|
||||
if(!continuous)
|
||||
sendPosition(true, true);
|
||||
}
|
||||
|
@ -387,7 +387,7 @@ std::string LuaEntitySAO::getDescription()
|
|||
{
|
||||
std::ostringstream oss;
|
||||
oss << "LuaEntitySAO \"" << m_init_name << "\" ";
|
||||
auto pos = floatToInt(m_base_position, BS);
|
||||
auto pos = floatToInt(getBasePosition(), BS);
|
||||
oss << "at " << pos;
|
||||
return oss.str();
|
||||
}
|
||||
|
@ -503,10 +503,10 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
|
|||
// Send attachment updates instantly to the client prior updating position
|
||||
sendOutdatedData();
|
||||
|
||||
m_last_sent_move_precision = m_base_position.getDistanceFrom(
|
||||
m_last_sent_move_precision = getBasePosition().getDistanceFrom(
|
||||
m_last_sent_position);
|
||||
m_last_sent_position_timer = 0;
|
||||
m_last_sent_position = m_base_position;
|
||||
m_last_sent_position = getBasePosition();
|
||||
m_last_sent_velocity = m_velocity;
|
||||
//m_last_sent_acceleration = m_acceleration;
|
||||
m_last_sent_rotation = m_rotation;
|
||||
|
@ -514,7 +514,7 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
|
|||
float update_interval = m_env->getSendRecommendedInterval();
|
||||
|
||||
std::string str = generateUpdatePositionCommand(
|
||||
m_base_position,
|
||||
getBasePosition(),
|
||||
m_velocity,
|
||||
m_acceleration,
|
||||
m_rotation,
|
||||
|
@ -534,8 +534,8 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
|
|||
toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
|
||||
toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
|
||||
|
||||
toset->MinEdge += m_base_position;
|
||||
toset->MaxEdge += m_base_position;
|
||||
toset->MinEdge += getBasePosition();
|
||||
toset->MaxEdge += getBasePosition();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -70,11 +70,10 @@ std::string PlayerSAO::getDescription()
|
|||
void PlayerSAO::addedToEnvironment(u32 dtime_s)
|
||||
{
|
||||
ServerActiveObject::addedToEnvironment(dtime_s);
|
||||
ServerActiveObject::setBasePosition(m_base_position);
|
||||
m_player->setPlayerSAO(this);
|
||||
m_player->setPeerId(m_peer_id_initial);
|
||||
m_peer_id_initial = PEER_ID_INEXISTENT; // don't try to use it again.
|
||||
m_last_good_position = m_base_position;
|
||||
m_last_good_position = getBasePosition();
|
||||
}
|
||||
|
||||
// Called before removing from environment
|
||||
|
@ -100,7 +99,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
|
|||
os << serializeString16(m_player->getName()); // name
|
||||
writeU8(os, 1); // is_player
|
||||
writeS16(os, getId()); // id
|
||||
writeV3F32(os, m_base_position);
|
||||
writeV3F32(os, getBasePosition());
|
||||
writeV3F32(os, m_rotation);
|
||||
writeU16(os, getHP());
|
||||
|
||||
|
@ -184,7 +183,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
|
|||
// Sequence of damage points, starting 0.1 above feet and progressing
|
||||
// upwards in 1 node intervals, stopping below top damage point.
|
||||
for (float dam_height = 0.1f; dam_height < dam_top; dam_height++) {
|
||||
v3s16 p = floatToInt(m_base_position +
|
||||
v3s16 p = floatToInt(getBasePosition() +
|
||||
v3f(0.0f, dam_height * BS, 0.0f), BS);
|
||||
MapNode n = m_env->getMap().getNode(p);
|
||||
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
|
||||
|
@ -196,7 +195,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
|
|||
}
|
||||
|
||||
// Top damage point
|
||||
v3s16 ptop = floatToInt(m_base_position +
|
||||
v3s16 ptop = floatToInt(getBasePosition() +
|
||||
v3f(0.0f, dam_top * BS, 0.0f), BS);
|
||||
MapNode ntop = m_env->getMap().getNode(ptop);
|
||||
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(ntop);
|
||||
|
@ -273,7 +272,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
|
|||
if (isAttached())
|
||||
pos = m_last_good_position;
|
||||
else
|
||||
pos = m_base_position;
|
||||
pos = getBasePosition();
|
||||
|
||||
std::string str = generateUpdatePositionCommand(
|
||||
pos,
|
||||
|
@ -332,7 +331,7 @@ std::string PlayerSAO::generateUpdatePhysicsOverrideCommand() const
|
|||
|
||||
void PlayerSAO::setBasePosition(v3f position)
|
||||
{
|
||||
if (m_player && position != m_base_position)
|
||||
if (m_player && position != getBasePosition())
|
||||
m_player->setDirty(true);
|
||||
|
||||
// This needs to be ran for attachments too
|
||||
|
@ -636,7 +635,7 @@ bool PlayerSAO::checkMovementCheat()
|
|||
if (m_is_singleplayer ||
|
||||
isAttached() ||
|
||||
!(anticheat_flags & AC_MOVEMENT)) {
|
||||
m_last_good_position = m_base_position;
|
||||
m_last_good_position = getBasePosition();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -701,7 +700,7 @@ bool PlayerSAO::checkMovementCheat()
|
|||
if (player_max_jump < 0.0001f)
|
||||
player_max_jump = 0.0001f;
|
||||
|
||||
v3f diff = (m_base_position - m_last_good_position);
|
||||
v3f diff = (getBasePosition() - m_last_good_position);
|
||||
float d_vert = diff.Y;
|
||||
diff.Y = 0;
|
||||
float d_horiz = diff.getLength();
|
||||
|
@ -722,7 +721,7 @@ bool PlayerSAO::checkMovementCheat()
|
|||
required_time /= anticheat_movement_tolerance;
|
||||
|
||||
if (m_move_pool.grab(required_time)) {
|
||||
m_last_good_position = m_base_position;
|
||||
m_last_good_position = getBasePosition();
|
||||
} else {
|
||||
const float LAG_POOL_MIN = 5.0;
|
||||
float lag_pool_max = m_env->getMaxLagEstimate() * 2.0;
|
||||
|
@ -744,8 +743,8 @@ bool PlayerSAO::getCollisionBox(aabb3f *toset) const
|
|||
toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
|
||||
toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
|
||||
|
||||
toset->MinEdge += m_base_position;
|
||||
toset->MaxEdge += m_base_position;
|
||||
toset->MinEdge += getBasePosition();
|
||||
toset->MaxEdge += getBasePosition();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -170,7 +170,7 @@ public:
|
|||
|
||||
void finalize(RemotePlayer *player, const std::set<std::string> &privs);
|
||||
|
||||
v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
|
||||
v3f getEyePosition() const { return getBasePosition() + getEyeOffset(); }
|
||||
v3f getEyeOffset() const;
|
||||
float getZoomFOV() const;
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "inventory.h"
|
||||
#include "inventorymanager.h"
|
||||
#include "constants.h" // BS
|
||||
#include "serverenvironment.h"
|
||||
|
||||
ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
|
||||
ActiveObject(0),
|
||||
|
@ -14,6 +15,17 @@ ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
|
|||
{
|
||||
}
|
||||
|
||||
void ServerActiveObject::setBasePosition(v3f pos)
|
||||
{
|
||||
bool changed = m_base_position != pos;
|
||||
m_base_position = pos;
|
||||
if (changed && getEnv()) {
|
||||
// getEnv() should never be null if the object is in an environment.
|
||||
// It may however be null e.g. in tests or database migrations.
|
||||
getEnv()->updateObjectPos(getId(), pos);
|
||||
}
|
||||
}
|
||||
|
||||
float ServerActiveObject::getMinimumSavedMovement()
|
||||
{
|
||||
return 2.0*BS;
|
||||
|
|
|
@ -63,7 +63,7 @@ public:
|
|||
Some simple getters/setters
|
||||
*/
|
||||
v3f getBasePosition() const { return m_base_position; }
|
||||
void setBasePosition(v3f pos){ m_base_position = pos; }
|
||||
void setBasePosition(v3f pos);
|
||||
ServerEnvironment* getEnv(){ return m_env; }
|
||||
|
||||
/*
|
||||
|
@ -245,7 +245,6 @@ protected:
|
|||
virtual void onMarkedForRemoval() {}
|
||||
|
||||
ServerEnvironment *m_env;
|
||||
v3f m_base_position;
|
||||
std::unordered_set<u32> m_attached_particle_spawners;
|
||||
|
||||
/*
|
||||
|
@ -273,4 +272,7 @@ protected:
|
|||
Queue of messages to be sent to the client
|
||||
*/
|
||||
std::queue<ActiveObjectMessage> m_messages_out;
|
||||
|
||||
private:
|
||||
v3f m_base_position; // setBasePosition updates index and MUST be called
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue