1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-06 13:45:34 +00:00

Increment NavMesh revision on remove or add tile

This commit is contained in:
elsid 2018-04-16 22:57:35 +03:00
parent faaf50446d
commit dd5f4498f6
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
5 changed files with 57 additions and 20 deletions

View file

@ -91,10 +91,7 @@ namespace DetourNavigator
const auto recastMesh = getRecastMesh(); const auto recastMesh = getRecastMesh();
updateNavMesh(job.mAgentHalfExtents, *recastMesh, job.mChangedTile, mSettings, updateNavMesh(job.mAgentHalfExtents, *recastMesh, job.mChangedTile, mSettings, *job.mNavMeshCacheItem);
job.mNavMeshCacheItem->mValue);
++job.mNavMeshCacheItem->mNavMeshRevision;
const auto finish = std::chrono::steady_clock::now(); const auto finish = std::chrono::steady_clock::now();

View file

@ -1,8 +1,8 @@
#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H #ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H #define OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H
#include "navmeshcacheitem.hpp"
#include "recastmesh.hpp" #include "recastmesh.hpp"
#include "sharednavmesh.hpp"
#include "tileposition.hpp" #include "tileposition.hpp"
#include <osg/Vec3f> #include <osg/Vec3f>
@ -22,16 +22,6 @@ class dtNavMesh;
namespace DetourNavigator namespace DetourNavigator
{ {
struct NavMeshCacheItem
{
SharedNavMesh mValue;
std::size_t mRecastMeshRevision;
std::atomic_size_t mNavMeshRevision;
NavMeshCacheItem(const NavMeshPtr& value, std::size_t revision)
: mValue(value), mRecastMeshRevision(revision), mNavMeshRevision(0) {}
};
class AsyncNavMeshUpdater class AsyncNavMeshUpdater
{ {
public: public:

View file

@ -214,6 +214,23 @@ namespace
return NavMeshData(navMeshData, navMeshDataSize); return NavMeshData(navMeshData, navMeshDataSize);
} }
struct AutoIncrementRevision
{
std::atomic_size_t& mNavMeshRevision;
bool mNavMeshChanged;
AutoIncrementRevision(std::atomic_size_t& navMeshRevision)
: mNavMeshRevision(navMeshRevision)
, mNavMeshChanged(false)
{}
~AutoIncrementRevision()
{
if (mNavMeshChanged)
++mNavMeshRevision;
}
};
} }
namespace DetourNavigator namespace DetourNavigator
@ -241,7 +258,7 @@ namespace DetourNavigator
} }
void updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh& recastMesh, void updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh& recastMesh,
const TilePosition& changedTile, const Settings& settings, SharedNavMesh& navMesh) const TilePosition& changedTile, const Settings& settings, NavMeshCacheItem& navMeshCacheItem)
{ {
log("update NavMesh with mutiple tiles:", log("update NavMesh with mutiple tiles:",
" agentHeight=", std::setprecision(std::numeric_limits<float>::max_exponent10), " agentHeight=", std::setprecision(std::numeric_limits<float>::max_exponent10),
@ -255,15 +272,19 @@ namespace DetourNavigator
const auto& boundsMin = recastMesh.getBoundsMin(); const auto& boundsMin = recastMesh.getBoundsMin();
const auto& boundsMax = recastMesh.getBoundsMax(); const auto& boundsMax = recastMesh.getBoundsMax();
auto& navMesh = navMeshCacheItem.mValue;
const auto& params = *navMesh.lock()->getParams(); const auto& params = *navMesh.lock()->getParams();
const osg::Vec3f origin(params.orig[0], params.orig[1], params.orig[2]); const osg::Vec3f origin(params.orig[0], params.orig[1], params.orig[2]);
const auto x = changedTile.x(); const auto x = changedTile.x();
const auto y = changedTile.y(); const auto y = changedTile.y();
AutoIncrementRevision incRev(navMeshCacheItem.mNavMeshRevision);
{ {
const auto locked = navMesh.lock(); const auto locked = navMesh.lock();
locked->removeTile(locked->getTileRefAt(x, y, 0), nullptr, nullptr); incRev.mNavMeshChanged = dtStatusSucceed(locked->removeTile(locked->getTileRefAt(x, y, 0),
nullptr, nullptr));
} }
const auto tileBounds = makeTileBounds(settings, changedTile); const auto tileBounds = makeTileBounds(settings, changedTile);
@ -274,10 +295,17 @@ namespace DetourNavigator
tileBorderMin, tileBorderMax, settings); tileBorderMin, tileBorderMax, settings);
if (!navMeshData.mValue) if (!navMeshData.mValue)
{
log("ignore add tile: NavMeshData is null");
return; return;
}
OPENMW_CHECK_DT_STATUS(navMesh.lock()->addTile(navMeshData.mValue.get(), navMeshData.mSize, const auto status = navMesh.lock()->addTile(navMeshData.mValue.get(), navMeshData.mSize,
DT_TILE_FREE_DATA, 0, 0)); DT_TILE_FREE_DATA, 0, 0);
if (dtStatusSucceed(status))
incRev.mNavMeshChanged = true;
else
log("failed to add tile with status=", WriteDtStatus {status});
navMeshData.mValue.release(); navMeshData.mValue.release();
} }
} }

View file

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_MAKENAVMESH_H #define OPENMW_COMPONENTS_DETOURNAVIGATOR_MAKENAVMESH_H
#include "settings.hpp" #include "settings.hpp"
#include "navmeshcacheitem.hpp"
#include "tileposition.hpp" #include "tileposition.hpp"
#include <osg/Vec3f> #include <osg/Vec3f>
@ -22,7 +23,7 @@ namespace DetourNavigator
NavMeshPtr makeEmptyNavMesh(const Settings& settings); NavMeshPtr makeEmptyNavMesh(const Settings& settings);
void updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh& recastMesh, void updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh& recastMesh,
const TilePosition& changedTile, const Settings& settings, SharedNavMesh& navMesh); const TilePosition& changedTile, const Settings& settings, NavMeshCacheItem& navMeshCacheItem);
} }
#endif #endif

View file

@ -0,0 +1,21 @@
#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_NAVMESHCACHEITEM_H
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_NAVMESHCACHEITEM_H
#include "sharednavmesh.hpp"
#include <atomic>
namespace DetourNavigator
{
struct NavMeshCacheItem
{
SharedNavMesh mValue;
std::size_t mRecastMeshRevision;
std::atomic_size_t mNavMeshRevision;
NavMeshCacheItem(const NavMeshPtr& value, std::size_t revision)
: mValue(value), mRecastMeshRevision(revision), mNavMeshRevision(0) {}
};
}
#endif