1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 18:19:55 +00:00
openmw-tes3mp/components/detournavigator/recastmeshmanager.cpp
elsid 3e67f5ffa5
Detect and ignore updates for oscillating objects
To avoid triggering NavMesh update when RecastMesh change should not change
NavMesh.

Based on the following assumption:
Given a set of transformations and a bounding shape for all these
tranformations, a new object transformation that does not change this
bounding shape also should not change navmesh if for all of this object
transformations resulting navmesh tiles are equivalent

The idea is to report back to RecastMeshManager all changes of NavMesh if there
are any assiciated with RecastMesh version. So we know the last time when
RecastMesh change resulted into the NavMesh change. When later report shows
that there was no NavMesh change for a new RecastMesh version we can assume
that any object transformation within the same bounding box should not change
NavMesh.
2021-04-18 23:00:13 +02:00

111 lines
3.9 KiB
C++

#include "recastmeshmanager.hpp"
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
namespace DetourNavigator
{
RecastMeshManager::RecastMeshManager(const Settings& settings, const TileBounds& bounds, std::size_t generation)
: mGeneration(generation)
, mMeshBuilder(settings, bounds)
{
}
bool RecastMeshManager::addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform,
const AreaType areaType)
{
const auto iterator = mObjectsOrder.emplace(mObjectsOrder.end(),
OscillatingRecastMeshObject(RecastMeshObject(shape, transform, areaType), mRevision + 1));
if (!mObjects.emplace(id, iterator).second)
{
mObjectsOrder.erase(iterator);
return false;
}
++mRevision;
return true;
}
bool RecastMeshManager::updateObject(const ObjectId id, const btTransform& transform, const AreaType areaType)
{
const auto object = mObjects.find(id);
if (object == mObjects.end())
return false;
const std::size_t lastChangeRevision = mLastNavMeshReportedChange.has_value()
? mLastNavMeshReportedChange->mRevision : mRevision;
if (!object->second->update(transform, areaType, lastChangeRevision))
return false;
++mRevision;
return true;
}
std::optional<RemovedRecastMeshObject> RecastMeshManager::removeObject(const ObjectId id)
{
const auto object = mObjects.find(id);
if (object == mObjects.end())
return std::nullopt;
const RemovedRecastMeshObject result {object->second->getImpl().getShape(), object->second->getImpl().getTransform()};
mObjectsOrder.erase(object->second);
mObjects.erase(object);
++mRevision;
return result;
}
bool RecastMeshManager::addWater(const osg::Vec2i& cellPosition, const int cellSize,
const btTransform& transform)
{
const auto iterator = mWaterOrder.emplace(mWaterOrder.end(), Water {cellSize, transform});
if (!mWater.emplace(cellPosition, iterator).second)
{
mWaterOrder.erase(iterator);
return false;
}
++mRevision;
return true;
}
std::optional<RecastMeshManager::Water> RecastMeshManager::removeWater(const osg::Vec2i& cellPosition)
{
const auto water = mWater.find(cellPosition);
if (water == mWater.end())
return std::nullopt;
++mRevision;
const auto result = *water->second;
mWaterOrder.erase(water->second);
mWater.erase(water);
return result;
}
std::shared_ptr<RecastMesh> RecastMeshManager::getMesh()
{
rebuild();
return mMeshBuilder.create(mGeneration, mRevision);
}
bool RecastMeshManager::isEmpty() const
{
return mObjects.empty();
}
void RecastMeshManager::reportNavMeshChange(Version recastMeshVersion, Version navMeshVersion)
{
if (recastMeshVersion.mGeneration != mGeneration)
return;
if (mLastNavMeshReport.has_value() && navMeshVersion < mLastNavMeshReport->mNavMeshVersion)
return;
mLastNavMeshReport = {recastMeshVersion.mRevision, navMeshVersion};
if (!mLastNavMeshReportedChange.has_value()
|| mLastNavMeshReportedChange->mNavMeshVersion < mLastNavMeshReport->mNavMeshVersion)
mLastNavMeshReportedChange = mLastNavMeshReport;
}
void RecastMeshManager::rebuild()
{
mMeshBuilder.reset();
for (const auto& v : mWaterOrder)
mMeshBuilder.addWater(v.mCellSize, v.mTransform);
for (const auto& object : mObjectsOrder)
{
const RecastMeshObject& v = object.getImpl();
mMeshBuilder.addObject(v.getShape(), v.getTransform(), v.getAreaType());
}
}
}