From 542717394a7f18553843bcaf0ee4b976b5baf2e8 Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 1 Feb 2022 21:48:06 +0100 Subject: [PATCH] Remove objects, water and heightfields when no longer required --- .../detournavigator/navigator.cpp | 12 ++++++++++ .../tilecachedrecastmeshmanager.cpp | 4 ++-- .../tilecachedrecastmeshmanager.cpp | 22 ++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/apps/openmw_test_suite/detournavigator/navigator.cpp b/apps/openmw_test_suite/detournavigator/navigator.cpp index 09a2be58ee..50bf1fe735 100644 --- a/apps/openmw_test_suite/detournavigator/navigator.cpp +++ b/apps/openmw_test_suite/detournavigator/navigator.cpp @@ -1132,4 +1132,16 @@ namespace Vec3fEq(306, 56.66666412353515625, -2.6667339801788330078125) )) << mPath; } + + TEST_F(DetourNavigatorNavigatorTest, only_one_water_per_cell_is_allowed) + { + const int cellSize1 = 100; + const float level1 = 1; + const int cellSize2 = 200; + const float level2 = 2; + + mNavigator->addAgent(mAgentHalfExtents); + EXPECT_TRUE(mNavigator->addWater(mCellPosition, cellSize1, level1)); + EXPECT_FALSE(mNavigator->addWater(mCellPosition, cellSize2, level2)); + } } diff --git a/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp b/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp index c44ebc5155..c637d35424 100644 --- a/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp +++ b/apps/openmw_test_suite/detournavigator/tilecachedrecastmeshmanager.cpp @@ -63,7 +63,7 @@ namespace EXPECT_TRUE(manager.addObject(ObjectId(&boxShape), shape, btTransform::getIdentity(), AreaType::AreaType_ground)); } - TEST_F(DetourNavigatorTileCachedRecastMeshManagerTest, add_object_for_existing_object_should_return_false) + TEST_F(DetourNavigatorTileCachedRecastMeshManagerTest, add_object_for_existing_object_should_throw_exception) { TileCachedRecastMeshManager manager(mSettings); const btBoxShape boxShape(btVector3(20, 20, 100)); @@ -225,7 +225,7 @@ namespace const CollisionShape shape(mInstance, boxShape, mObjectTransform); manager.addObject(ObjectId(&boxShape), shape, btTransform::getIdentity(), AreaType::AreaType_ground); const auto beforeAddRevision = manager.getRevision(); - manager.addObject(ObjectId(&boxShape), shape, btTransform::getIdentity(), AreaType::AreaType_ground); + EXPECT_FALSE(manager.addObject(ObjectId(&boxShape), shape, btTransform::getIdentity(), AreaType::AreaType_ground)); EXPECT_EQ(manager.getRevision(), beforeAddRevision); } diff --git a/components/detournavigator/tilecachedrecastmeshmanager.cpp b/components/detournavigator/tilecachedrecastmeshmanager.cpp index 17ba7afc39..d1f586f85c 100644 --- a/components/detournavigator/tilecachedrecastmeshmanager.cpp +++ b/components/detournavigator/tilecachedrecastmeshmanager.cpp @@ -33,6 +33,9 @@ namespace DetourNavigator bool TileCachedRecastMeshManager::addObject(const ObjectId id, const CollisionShape& shape, const btTransform& transform, const AreaType areaType) { + const auto it = mObjectsTilesPositions.find(id); + if (it != mObjectsTilesPositions.end()) + return false; std::vector tilesPositions; { const std::lock_guard lock(mMutex); @@ -46,7 +49,7 @@ namespace DetourNavigator if (tilesPositions.empty()) return false; std::sort(tilesPositions.begin(), tilesPositions.end()); - mObjectsTilesPositions.insert_or_assign(id, std::move(tilesPositions)); + mObjectsTilesPositions.emplace_hint(it, id, std::move(tilesPositions)); ++mRevision; return true; } @@ -66,6 +69,7 @@ namespace DetourNavigator result = removed; } } + mObjectsTilesPositions.erase(object); if (result) ++mRevision; return result; @@ -73,7 +77,12 @@ namespace DetourNavigator bool TileCachedRecastMeshManager::addWater(const osg::Vec2i& cellPosition, int cellSize, float level) { - auto& tilesPositions = mWaterTilesPositions[cellPosition]; + const auto it = mWaterTilesPositions.find(cellPosition); + if (it != mWaterTilesPositions.end()) + return false; + + std::vector& tilesPositions = mWaterTilesPositions.emplace_hint( + it, cellPosition, std::vector())->second; bool result = false; @@ -138,6 +147,7 @@ namespace DetourNavigator if (tileResult && !result) result = tileResult; } + mWaterTilesPositions.erase(object); if (result) ++mRevision; return result; @@ -146,8 +156,13 @@ namespace DetourNavigator bool TileCachedRecastMeshManager::addHeightfield(const osg::Vec2i& cellPosition, int cellSize, const HeightfieldShape& shape) { + const auto it = mHeightfieldTilesPositions.find(cellPosition); + if (it != mHeightfieldTilesPositions.end()) + return false; + + std::vector& tilesPositions = mHeightfieldTilesPositions.emplace_hint( + it, cellPosition, std::vector())->second; const btVector3 shift = getHeightfieldShift(shape, cellPosition, cellSize); - auto& tilesPositions = mHeightfieldTilesPositions[cellPosition]; bool result = false; @@ -196,6 +211,7 @@ namespace DetourNavigator if (tileResult && !result) result = tileResult; } + mHeightfieldTilesPositions.erase(object); if (result) ++mRevision; return result;