mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Optimize entity-entity collision (#6587)
* Add IrrLicht type aliases * Add hash for IrrLicht vector * Add object map
This commit is contained in:
parent
2481ea27ce
commit
528908a4c3
13 changed files with 613 additions and 80 deletions
|
@ -996,14 +996,7 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n)
|
|||
void ServerEnvironment::getObjectsInsideRadius(std::vector<u16> &objects, v3f pos,
|
||||
float radius)
|
||||
{
|
||||
for (auto &activeObject : m_active_objects) {
|
||||
ServerActiveObject* obj = activeObject.second;
|
||||
u16 id = activeObject.first;
|
||||
v3f objectpos = obj->getBasePosition();
|
||||
if (objectpos.getDistanceFrom(pos) > radius)
|
||||
continue;
|
||||
objects.push_back(id);
|
||||
}
|
||||
objects = m_active_objects.getObjectsInsideRadius(pos, radius);
|
||||
}
|
||||
|
||||
void ServerEnvironment::clearObjects(ClearObjectsMode mode)
|
||||
|
@ -1011,9 +1004,9 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
|
|||
infostream << "ServerEnvironment::clearObjects(): "
|
||||
<< "Removing all active objects" << std::endl;
|
||||
std::vector<u16> objects_to_remove;
|
||||
for (auto &it : m_active_objects) {
|
||||
for (auto &it : m_active_objects.getObjects()) {
|
||||
u16 id = it.first;
|
||||
ServerActiveObject* obj = it.second;
|
||||
ServerActiveObject* obj = it.second.object;
|
||||
if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER)
|
||||
continue;
|
||||
|
||||
|
@ -1040,7 +1033,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
|
|||
|
||||
// Remove references from m_active_objects
|
||||
for (u16 i : objects_to_remove) {
|
||||
m_active_objects.erase(i);
|
||||
m_active_objects.removeObject(i);
|
||||
}
|
||||
|
||||
// Get list of loaded blocks
|
||||
|
@ -1338,8 +1331,8 @@ void ServerEnvironment::step(float dtime)
|
|||
send_recommended = true;
|
||||
}
|
||||
|
||||
for (auto &ao_it : m_active_objects) {
|
||||
ServerActiveObject* obj = ao_it.second;
|
||||
for (auto &ao_it : m_active_objects.getObjects()) {
|
||||
ServerActiveObject* obj = ao_it.second.object;
|
||||
if (obj->isGone())
|
||||
continue;
|
||||
|
||||
|
@ -1425,40 +1418,7 @@ void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object)
|
|||
|
||||
ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
|
||||
{
|
||||
ServerActiveObjectMap::const_iterator n = m_active_objects.find(id);
|
||||
return (n != m_active_objects.end() ? n->second : NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if id is a free active object id
|
||||
* @param id
|
||||
* @return true if slot is free
|
||||
*/
|
||||
bool ServerEnvironment::isFreeServerActiveObjectId(u16 id) const
|
||||
{
|
||||
if (id == 0)
|
||||
return false;
|
||||
|
||||
return m_active_objects.find(id) == m_active_objects.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the first free ActiveObject ID
|
||||
* @return free activeobject ID or 0 if none was found
|
||||
*/
|
||||
u16 ServerEnvironment::getFreeServerActiveObjectId()
|
||||
{
|
||||
// try to reuse id's as late as possible
|
||||
static u16 last_used_id = 0;
|
||||
u16 startid = last_used_id;
|
||||
for (;;) {
|
||||
last_used_id++;
|
||||
if (isFreeServerActiveObjectId(last_used_id))
|
||||
return last_used_id;
|
||||
|
||||
if (last_used_id == startid)
|
||||
return 0;
|
||||
}
|
||||
return m_active_objects.getObject(id);
|
||||
}
|
||||
|
||||
u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
|
||||
|
@ -1469,6 +1429,12 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object)
|
|||
return id;
|
||||
}
|
||||
|
||||
void ServerEnvironment::updateActiveObject(ServerActiveObject *object)
|
||||
{
|
||||
assert(object);
|
||||
m_active_objects.updateObject(object);
|
||||
}
|
||||
|
||||
/*
|
||||
Finds out what new objects have been added to
|
||||
inside a radius around a position
|
||||
|
@ -1490,11 +1456,11 @@ void ServerEnvironment::getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
|
|||
- discard objects that are found in current_objects.
|
||||
- add remaining objects to added_objects
|
||||
*/
|
||||
for (auto &ao_it : m_active_objects) {
|
||||
for (auto &ao_it : m_active_objects.getObjects()) {
|
||||
u16 id = ao_it.first;
|
||||
|
||||
// Get object
|
||||
ServerActiveObject *object = ao_it.second;
|
||||
ServerActiveObject *object = ao_it.second.object;
|
||||
if (object == NULL)
|
||||
continue;
|
||||
|
||||
|
@ -1578,16 +1544,14 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock(
|
|||
|
||||
for (auto &so_it : block->m_static_objects.m_active) {
|
||||
// Get the ServerActiveObject counterpart to this StaticObject
|
||||
ServerActiveObjectMap::const_iterator ao_it = m_active_objects.find(so_it.first);
|
||||
if (ao_it == m_active_objects.end()) {
|
||||
ServerActiveObject *sao = m_active_objects.getObject(so_it.first);
|
||||
if (!sao) {
|
||||
// If this ever happens, there must be some kind of nasty bug.
|
||||
errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): "
|
||||
"Object from MapBlock::m_static_objects::m_active not found "
|
||||
"in m_active_objects";
|
||||
continue;
|
||||
}
|
||||
|
||||
ServerActiveObject *sao = ao_it->second;
|
||||
sao->m_static_exists = static_exists;
|
||||
sao->m_static_block = static_block;
|
||||
}
|
||||
|
@ -1644,7 +1608,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
|
|||
{
|
||||
assert(object); // Pre-condition
|
||||
if(object->getId() == 0){
|
||||
u16 new_id = getFreeServerActiveObjectId();
|
||||
u16 new_id = m_active_objects.getFreeId();
|
||||
if(new_id == 0)
|
||||
{
|
||||
errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
|
||||
|
@ -1660,7 +1624,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
|
|||
<<"supplied with id "<<object->getId()<<std::endl;
|
||||
}
|
||||
|
||||
if(!isFreeServerActiveObjectId(object->getId())) {
|
||||
if (!m_active_objects.isFreeId(object->getId())) {
|
||||
errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
|
||||
<<"id is not free ("<<object->getId()<<")"<<std::endl;
|
||||
if(object->environmentDeletes())
|
||||
|
@ -1681,7 +1645,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
|
|||
/*infostream<<"ServerEnvironment::addActiveObjectRaw(): "
|
||||
<<"added (id="<<object->getId()<<")"<<std::endl;*/
|
||||
|
||||
m_active_objects[object->getId()] = object;
|
||||
m_active_objects.addObject(object);
|
||||
|
||||
verbosestream<<"ServerEnvironment::addActiveObjectRaw(): "
|
||||
<<"Added id="<<object->getId()<<"; there are now "
|
||||
|
@ -1727,9 +1691,9 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
|
|||
void ServerEnvironment::removeRemovedObjects()
|
||||
{
|
||||
std::vector<u16> objects_to_remove;
|
||||
for (auto &ao_it : m_active_objects) {
|
||||
for (auto &ao_it : m_active_objects.getObjects()) {
|
||||
u16 id = ao_it.first;
|
||||
ServerActiveObject* obj = ao_it.second;
|
||||
ServerActiveObject* obj = ao_it.second.object;
|
||||
|
||||
// This shouldn't happen but check it
|
||||
if (!obj) {
|
||||
|
@ -1794,7 +1758,7 @@ void ServerEnvironment::removeRemovedObjects()
|
|||
}
|
||||
// Remove references from m_active_objects
|
||||
for (u16 i : objects_to_remove) {
|
||||
m_active_objects.erase(i);
|
||||
m_active_objects.removeObject(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1916,11 +1880,11 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
|
|||
void ServerEnvironment::deactivateFarObjects(bool _force_delete)
|
||||
{
|
||||
std::vector<u16> objects_to_remove;
|
||||
for (auto &ao_it : m_active_objects) {
|
||||
for (auto &ao_it : m_active_objects.getObjects()) {
|
||||
// force_delete might be overriden per object
|
||||
bool force_delete = _force_delete;
|
||||
|
||||
ServerActiveObject* obj = ao_it.second;
|
||||
ServerActiveObject* obj = ao_it.second.object;
|
||||
assert(obj);
|
||||
|
||||
// Do not deactivate if static data creation not allowed
|
||||
|
@ -2051,7 +2015,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
|
|||
|
||||
// Remove references from m_active_objects
|
||||
for (u16 i : objects_to_remove) {
|
||||
m_active_objects.erase(i);
|
||||
m_active_objects.removeObject(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue