From 49db37ee292742ae42a951452f28d82b08929351 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 19 May 2024 15:38:47 +0200 Subject: [PATCH] Merge setWorldspace and updateBounds --- apps/openmw/mwworld/scene.cpp | 13 ++++------ .../detournavigator/navigator.cpp | 26 ++++++------------- components/detournavigator/navigator.hpp | 15 +++-------- components/detournavigator/navigatorimpl.cpp | 9 ++----- components/detournavigator/navigatorimpl.hpp | 4 +-- components/detournavigator/navigatorstub.hpp | 7 ++--- components/detournavigator/navmeshmanager.cpp | 18 ++++++------- components/detournavigator/navmeshmanager.hpp | 4 +-- 8 files changed, 33 insertions(+), 63 deletions(-) diff --git a/apps/openmw/mwworld/scene.cpp b/apps/openmw/mwworld/scene.cpp index e3b9419035..2465f165b9 100644 --- a/apps/openmw/mwworld/scene.cpp +++ b/apps/openmw/mwworld/scene.cpp @@ -591,8 +591,8 @@ namespace MWWorld else unloadCell(cell, navigatorUpdateGuard.get()); } - mNavigator.setWorldspace(playerCellIndex.mWorldspace, navigatorUpdateGuard.get()); - mNavigator.updateBounds(pos, navigatorUpdateGuard.get()); + + mNavigator.updateBounds(playerCellIndex.mWorldspace, pos, navigatorUpdateGuard.get()); mCurrentGridCenter = osg::Vec2i(playerCellX, playerCellY); osg::Vec4i newGrid = gridCenterToBounds(mCurrentGridCenter); @@ -694,10 +694,9 @@ namespace MWWorld CellStore& cell = mWorld.getWorldModel().getExterior( ESM::ExteriorCellLocation(it->mData.mX, it->mData.mY, ESM::Cell::sDefaultWorldspaceId)); - mNavigator.setWorldspace(cell.getCell()->getWorldSpace(), navigatorUpdateGuard.get()); const osg::Vec3f position = osg::Vec3f(it->mData.mX + 0.5f, it->mData.mY + 0.5f, 0) * Constants::CellSizeInUnits; - mNavigator.updateBounds(position, navigatorUpdateGuard.get()); + mNavigator.updateBounds(ESM::Cell::sDefaultWorldspaceId, position, navigatorUpdateGuard.get()); loadCell(cell, nullptr, false, position, navigatorUpdateGuard.get()); mNavigator.update(position, navigatorUpdateGuard.get()); @@ -751,10 +750,9 @@ namespace MWWorld + std::to_string(cells.getIntSize()) + ")..."); CellStore& cell = mWorld.getWorldModel().getInterior(it->mName); - mNavigator.setWorldspace(cell.getCell()->getWorldSpace(), navigatorUpdateGuard.get()); ESM::Position position; mWorld.findInteriorPosition(it->mName, position); - mNavigator.updateBounds(position.asVec3(), navigatorUpdateGuard.get()); + mNavigator.updateBounds(cell.getCell()->getWorldSpace(), position.asVec3(), navigatorUpdateGuard.get()); loadCell(cell, nullptr, false, position.asVec3(), navigatorUpdateGuard.get()); mNavigator.update(position.asVec3(), navigatorUpdateGuard.get()); @@ -904,8 +902,7 @@ namespace MWWorld loadingListener->setProgressRange(cell.count()); - mNavigator.setWorldspace(cell.getCell()->getWorldSpace(), navigatorUpdateGuard.get()); - mNavigator.updateBounds(position.asVec3(), navigatorUpdateGuard.get()); + mNavigator.updateBounds(cell.getCell()->getWorldSpace(), position.asVec3(), navigatorUpdateGuard.get()); // Load cell. mPagedRefs.clear(); diff --git a/apps/openmw_test_suite/detournavigator/navigator.cpp b/apps/openmw_test_suite/detournavigator/navigator.cpp index 6dcade083a..c53ff2cc37 100644 --- a/apps/openmw_test_suite/detournavigator/navigator.cpp +++ b/apps/openmw_test_suite/detournavigator/navigator.cpp @@ -44,31 +44,21 @@ namespace struct DetourNavigatorNavigatorTest : Test { Settings mSettings = makeSettings(); - std::unique_ptr mNavigator; - const osg::Vec3f mPlayerPosition; - const std::string mWorldspace; + std::unique_ptr mNavigator = std::make_unique( + mSettings, std::make_unique(":memory:", std::numeric_limits::max())); + const osg::Vec3f mPlayerPosition{ 256, 256, 0 }; + const ESM::RefId mWorldspace = ESM::RefId::stringRefId("sys::default"); const AgentBounds mAgentBounds{ CollisionShapeType::Aabb, { 29, 29, 66 } }; - osg::Vec3f mStart; - osg::Vec3f mEnd; + osg::Vec3f mStart{ 52, 460, 1 }; + osg::Vec3f mEnd{ 460, 52, 1 }; std::deque mPath; - std::back_insert_iterator> mOut; + std::back_insert_iterator> mOut{ mPath }; AreaCosts mAreaCosts; Loading::Listener mListener; const osg::Vec2i mCellPosition{ 0, 0 }; const float mEndTolerance = 0; const btTransform mTransform{ btMatrix3x3::getIdentity(), btVector3(256, 256, 0) }; const ObjectTransform mObjectTransform{ ESM::Position{ { 256, 256, 0 }, { 0, 0, 0 } }, 0.0f }; - - DetourNavigatorNavigatorTest() - : mPlayerPosition(256, 256, 0) - , mWorldspace("sys::default") - , mStart(52, 460, 1) - , mEnd(460, 52, 1) - , mOut(mPath) - { - mNavigator.reset(new NavigatorImpl( - mSettings, std::make_unique(":memory:", std::numeric_limits::max()))); - } }; constexpr std::array defaultHeightfieldData{ { @@ -878,7 +868,7 @@ namespace .mScale = 1.0f, }; - mNavigator->updateBounds(mPlayerPosition, nullptr); + mNavigator->updateBounds(mWorldspace, mPlayerPosition, nullptr); ASSERT_TRUE(mNavigator->addAgent(mAgentBounds)); mNavigator->addObject(ObjectId(&bigBox.shape()), ObjectShapes(bigBox.instance(), objectTransform), btTransform::getIdentity(), nullptr); diff --git a/components/detournavigator/navigator.hpp b/components/detournavigator/navigator.hpp index 7bab0af4d1..609944b245 100644 --- a/components/detournavigator/navigator.hpp +++ b/components/detournavigator/navigator.hpp @@ -12,6 +12,7 @@ #include "updateguard.hpp" #include "waitconditiontype.hpp" +#include #include namespace ESM @@ -87,17 +88,9 @@ namespace DetourNavigator */ virtual void removeAgent(const AgentBounds& agentBounds) = 0; - /** - * @brief setWorldspace should be called before adding object from new worldspace - * @param worldspace - */ - virtual void setWorldspace(ESM::RefId worldspace, const UpdateGuard* guard) = 0; - - /** - * @brief updateBounds should be called before adding object from loading cell - * @param playerPosition corresponds to the bounds center - */ - virtual void updateBounds(const osg::Vec3f& playerPosition, const UpdateGuard* guard) = 0; + // Updates bounds for recast mesh and navmesh tiles, removes tiles outside the range. + virtual void updateBounds(ESM::RefId worldspace, const osg::Vec3f& playerPosition, const UpdateGuard* guard) + = 0; /** * @brief addObject is used to add complex object with allowed to walk and avoided to walk shapes diff --git a/components/detournavigator/navigatorimpl.cpp b/components/detournavigator/navigatorimpl.cpp index 443a98a678..b14dde20fc 100644 --- a/components/detournavigator/navigatorimpl.cpp +++ b/components/detournavigator/navigatorimpl.cpp @@ -33,14 +33,9 @@ namespace DetourNavigator --it->second; } - void NavigatorImpl::setWorldspace(ESM::RefId worldspace, const UpdateGuard* guard) + void NavigatorImpl::updateBounds(ESM::RefId worldspace, const osg::Vec3f& playerPosition, const UpdateGuard* guard) { - mNavMeshManager.setWorldspace(worldspace, guard); - } - - void NavigatorImpl::updateBounds(const osg::Vec3f& playerPosition, const UpdateGuard* guard) - { - mNavMeshManager.updateBounds(playerPosition, guard); + mNavMeshManager.updateBounds(worldspace, playerPosition, guard); } void NavigatorImpl::addObject( diff --git a/components/detournavigator/navigatorimpl.hpp b/components/detournavigator/navigatorimpl.hpp index e9f7276172..759ddf8d14 100644 --- a/components/detournavigator/navigatorimpl.hpp +++ b/components/detournavigator/navigatorimpl.hpp @@ -27,9 +27,7 @@ namespace DetourNavigator void removeAgent(const AgentBounds& agentBounds) override; - void setWorldspace(ESM::RefId worldspace, const UpdateGuard* guard) override; - - void updateBounds(const osg::Vec3f& playerPosition, const UpdateGuard* guard) override; + void updateBounds(ESM::RefId worldspace, const osg::Vec3f& playerPosition, const UpdateGuard* guard) override; void addObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard) override; diff --git a/components/detournavigator/navigatorstub.hpp b/components/detournavigator/navigatorstub.hpp index bc14e2274c..7638faac27 100644 --- a/components/detournavigator/navigatorstub.hpp +++ b/components/detournavigator/navigatorstub.hpp @@ -24,7 +24,10 @@ namespace DetourNavigator void removeAgent(const AgentBounds& /*agentBounds*/) override {} - void setWorldspace(ESM::RefId /*worldspace*/, const UpdateGuard* /*guard*/) override {} + void updateBounds( + ESM::RefId /*worldspace*/, const osg::Vec3f& /*playerPosition*/, const UpdateGuard* /*guard*/) override + { + } void addObject(const ObjectId /*id*/, const ObjectShapes& /*shapes*/, const btTransform& /*transform*/, const UpdateGuard* /*guard*/) override @@ -68,8 +71,6 @@ namespace DetourNavigator void update(const osg::Vec3f& /*playerPosition*/, const UpdateGuard* /*guard*/) override {} - void updateBounds(const osg::Vec3f& /*playerPosition*/, const UpdateGuard* /*guard*/) override {} - void wait(WaitConditionType /*waitConditionType*/, Loading::Listener* /*listener*/) override {} SharedNavMeshCacheItem getNavMesh(const AgentBounds& /*agentBounds*/) const override diff --git a/components/detournavigator/navmeshmanager.cpp b/components/detournavigator/navmeshmanager.cpp index 7a2fd74714..502bda6f66 100644 --- a/components/detournavigator/navmeshmanager.cpp +++ b/components/detournavigator/navmeshmanager.cpp @@ -58,18 +58,16 @@ namespace DetourNavigator { } - void NavMeshManager::setWorldspace(ESM::RefId worldspace, const UpdateGuard* guard) + void NavMeshManager::updateBounds(ESM::RefId worldspace, const osg::Vec3f& playerPosition, const UpdateGuard* guard) { - if (worldspace == mWorldspace) - return; - mRecastMeshManager.setWorldspace(worldspace, guard); - for (auto& [agent, cache] : mCache) - cache = std::make_shared(++mGenerationCounter, mSettings); - mWorldspace = worldspace; - } + if (worldspace != mWorldspace) + { + mRecastMeshManager.setWorldspace(worldspace, guard); + for (auto& [agent, cache] : mCache) + cache = std::make_shared(++mGenerationCounter, mSettings); + mWorldspace = worldspace; + } - void NavMeshManager::updateBounds(const osg::Vec3f& playerPosition, const UpdateGuard* guard) - { const TilePosition playerTile = toNavMeshTilePosition(mSettings.mRecast, playerPosition); const TilesPositionsRange range = makeRange(playerTile, mSettings.mMaxTilesNumber); mRecastMeshManager.setRange(range, guard); diff --git a/components/detournavigator/navmeshmanager.hpp b/components/detournavigator/navmeshmanager.hpp index 6c25cc599f..89ced89f02 100644 --- a/components/detournavigator/navmeshmanager.hpp +++ b/components/detournavigator/navmeshmanager.hpp @@ -24,9 +24,7 @@ namespace DetourNavigator ScopedUpdateGuard makeUpdateGuard() { return mRecastMeshManager.makeUpdateGuard(); } - void setWorldspace(ESM::RefId worldspace, const UpdateGuard* guard); - - void updateBounds(const osg::Vec3f& playerPosition, const UpdateGuard* guard); + void updateBounds(ESM::RefId worldspace, const osg::Vec3f& playerPosition, const UpdateGuard* guard); bool addObject(const ObjectId id, const CollisionShape& shape, const btTransform& transform, const AreaType areaType, const UpdateGuard* guard);