diff --git a/components/detournavigator/tilecachedrecastmeshmanager.cpp b/components/detournavigator/tilecachedrecastmeshmanager.cpp index e01eb1521e..046eb7750d 100644 --- a/components/detournavigator/tilecachedrecastmeshmanager.cpp +++ b/components/detournavigator/tilecachedrecastmeshmanager.cpp @@ -149,15 +149,15 @@ namespace DetourNavigator else { const btVector3 shift = Misc::Convert::toBullet(getWaterShift3d(cellPosition, cellSize, level)); + const auto worldspaceTiles = mWorldspaceTiles.lock(); getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings), [&] (const TilePosition& tilePosition) { - const auto locked = mWorldspaceTiles.lock(); - auto tile = locked->mTiles.find(tilePosition); - if (tile == locked->mTiles.end()) + auto tile = worldspaceTiles->mTiles.find(tilePosition); + if (tile == worldspaceTiles->mTiles.end()) { const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); - tile = locked->mTiles.emplace_hint(tile, tilePosition, + tile = worldspaceTiles->mTiles.emplace_hint(tile, tilePosition, std::make_shared(tileBounds, mTilesGeneration)); } if (tile->second->addWater(cellPosition, cellSize, level)) @@ -180,20 +180,22 @@ namespace DetourNavigator if (object == mWaterTilesPositions.end()) return std::nullopt; std::optional result; - for (const auto& tilePosition : object->second) { - const auto locked = mWorldspaceTiles.lock(); - const auto tile = locked->mTiles.find(tilePosition); - if (tile == locked->mTiles.end()) - continue; - const auto tileResult = tile->second->removeWater(cellPosition); - if (tile->second->isEmpty()) + const auto worldspaceTiles = mWorldspaceTiles.lock(); + for (const auto& tilePosition : object->second) { - locked->mTiles.erase(tile); - ++mTilesGeneration; + const auto tile = worldspaceTiles->mTiles.find(tilePosition); + if (tile == worldspaceTiles->mTiles.end()) + continue; + const auto tileResult = tile->second->removeWater(cellPosition); + if (tile->second->isEmpty()) + { + worldspaceTiles->mTiles.erase(tile); + ++mTilesGeneration; + } + if (tileResult && !result) + result = tileResult; } - if (tileResult && !result) - result = tileResult; } mWaterTilesPositions.erase(object); if (result) @@ -214,23 +216,25 @@ namespace DetourNavigator bool result = false; - getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings), - [&] (const TilePosition& tilePosition) - { - const auto locked = mWorldspaceTiles.lock(); - auto tile = locked->mTiles.find(tilePosition); - if (tile == locked->mTiles.end()) - { - const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); - tile = locked->mTiles.emplace_hint(tile, tilePosition, - std::make_shared(tileBounds, mTilesGeneration)); - } - if (tile->second->addHeightfield(cellPosition, cellSize, shape)) + { + const auto worldspaceTiles = mWorldspaceTiles.lock(); + getTilesPositions(makeTilesPositionsRange(cellSize, shift, mSettings), + [&] (const TilePosition& tilePosition) { - tilesPositions.push_back(tilePosition); - result = true; - } - }); + auto tile = worldspaceTiles->mTiles.find(tilePosition); + if (tile == worldspaceTiles->mTiles.end()) + { + const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); + tile = worldspaceTiles->mTiles.emplace_hint(tile, tilePosition, + std::make_shared(tileBounds, mTilesGeneration)); + } + if (tile->second->addHeightfield(cellPosition, cellSize, shape)) + { + tilesPositions.push_back(tilePosition); + result = true; + } + }); + } if (result) ++mRevision; @@ -244,20 +248,22 @@ namespace DetourNavigator if (object == mHeightfieldTilesPositions.end()) return std::nullopt; std::optional result; - for (const auto& tilePosition : object->second) { - const auto locked = mWorldspaceTiles.lock(); - const auto tile = locked->mTiles.find(tilePosition); - if (tile == locked->mTiles.end()) - continue; - const auto tileResult = tile->second->removeHeightfield(cellPosition); - if (tile->second->isEmpty()) + const auto worldspaceTiles = mWorldspaceTiles.lock(); + for (const auto& tilePosition : object->second) { - locked->mTiles.erase(tile); - ++mTilesGeneration; + const auto tile = worldspaceTiles->mTiles.find(tilePosition); + if (tile == worldspaceTiles->mTiles.end()) + continue; + const auto tileResult = tile->second->removeHeightfield(cellPosition); + if (tile->second->isEmpty()) + { + worldspaceTiles->mTiles.erase(tile); + ++mTilesGeneration; + } + if (tileResult && !result) + result = tileResult; } - if (tileResult && !result) - result = tileResult; } mHeightfieldTilesPositions.erase(object); if (result) diff --git a/components/detournavigator/tilecachedrecastmeshmanager.hpp b/components/detournavigator/tilecachedrecastmeshmanager.hpp index 270db9e8ae..f6ef24ceed 100644 --- a/components/detournavigator/tilecachedrecastmeshmanager.hpp +++ b/components/detournavigator/tilecachedrecastmeshmanager.hpp @@ -165,7 +165,7 @@ namespace DetourNavigator bool addTile(const ObjectId id, const CollisionShape& shape, const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles); - bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType, + static bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles); std::optional removeTile(const ObjectId id, const TilePosition& tilePosition,