1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-03-29 16:06:44 +00:00

Return empty path when navmesh is not found for agent

This commit is contained in:
elsid 2019-02-16 15:32:47 +03:00
parent 1d3668cd22
commit 16675fd254
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
5 changed files with 23 additions and 10 deletions

View file

@ -65,9 +65,10 @@ namespace
} }
}; };
TEST_F(DetourNavigatorNavigatorTest, find_path_for_empty_should_throw_exception) TEST_F(DetourNavigatorNavigatorTest, find_path_for_empty_should_return_empty)
{ {
EXPECT_THROW(mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut), InvalidArgument); mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut);
EXPECT_EQ(mPath, std::deque<osg::Vec3f>());
} }
TEST_F(DetourNavigatorNavigatorTest, find_path_for_existing_agent_with_no_navmesh_should_throw_exception) TEST_F(DetourNavigatorNavigatorTest, find_path_for_existing_agent_with_no_navmesh_should_throw_exception)
@ -76,11 +77,12 @@ namespace
EXPECT_THROW(mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut), NavigatorException); EXPECT_THROW(mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut), NavigatorException);
} }
TEST_F(DetourNavigatorNavigatorTest, find_path_for_removed_agent_should_throw_exception) TEST_F(DetourNavigatorNavigatorTest, find_path_for_removed_agent_should_return_empty)
{ {
mNavigator->addAgent(mAgentHalfExtents); mNavigator->addAgent(mAgentHalfExtents);
mNavigator->removeAgent(mAgentHalfExtents); mNavigator->removeAgent(mAgentHalfExtents);
EXPECT_THROW(mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut), InvalidArgument); mNavigator->findPath(mAgentHalfExtents, mStart, mEnd, Flag_walk, mOut);
EXPECT_EQ(mPath, std::deque<osg::Vec3f>());
} }
TEST_F(DetourNavigatorNavigatorTest, add_agent_should_count_each_agent) TEST_F(DetourNavigatorNavigatorTest, add_agent_should_count_each_agent)

View file

@ -171,6 +171,8 @@ namespace DetourNavigator
"out is not an OutputIterator" "out is not an OutputIterator"
); );
const auto navMesh = getNavMesh(agentHalfExtents); const auto navMesh = getNavMesh(agentHalfExtents);
if (!navMesh)
return out;
const auto settings = getSettings(); const auto settings = getSettings();
return findSmoothPath(navMesh.lock()->getValue(), toNavMeshCoordinates(settings, agentHalfExtents), return findSmoothPath(navMesh.lock()->getValue(), toNavMeshCoordinates(settings, agentHalfExtents),
toNavMeshCoordinates(settings, start), toNavMeshCoordinates(settings, end), includeFlags, toNavMeshCoordinates(settings, start), toNavMeshCoordinates(settings, end), includeFlags,

View file

@ -133,7 +133,13 @@ namespace DetourNavigator
else else
lastPlayerTile->second = playerTile; lastPlayerTile->second = playerTile;
std::map<TilePosition, ChangeType> tilesToPost; std::map<TilePosition, ChangeType> tilesToPost;
const auto& cached = getCached(agentHalfExtents); const auto cached = getCached(agentHalfExtents);
if (!cached)
{
std::ostringstream stream;
stream << "Agent with half extents is not found: " << agentHalfExtents;
throw InvalidArgument(stream.str());
}
const auto changedTiles = mChangedTiles.find(agentHalfExtents); const auto changedTiles = mChangedTiles.find(agentHalfExtents);
{ {
const auto locked = cached.lock(); const auto locked = cached.lock();
@ -218,13 +224,11 @@ namespace DetourNavigator
} }
} }
const SharedNavMeshCacheItem& NavMeshManager::getCached(const osg::Vec3f& agentHalfExtents) const SharedNavMeshCacheItem NavMeshManager::getCached(const osg::Vec3f& agentHalfExtents) const
{ {
const auto cached = mCache.find(agentHalfExtents); const auto cached = mCache.find(agentHalfExtents);
if (cached != mCache.end()) if (cached != mCache.end())
return cached->second; return cached->second;
std::ostringstream stream; return SharedNavMeshCacheItem();
stream << "Agent with half extents is not found: " << agentHalfExtents;
throw InvalidArgument(stream.str());
} }
} }

View file

@ -67,7 +67,7 @@ namespace DetourNavigator
void addChangedTile(const TilePosition& tilePosition, const ChangeType changeType); void addChangedTile(const TilePosition& tilePosition, const ChangeType changeType);
const SharedNavMeshCacheItem& getCached(const osg::Vec3f& agentHalfExtents) const; SharedNavMeshCacheItem getCached(const osg::Vec3f& agentHalfExtents) const;
}; };
} }

View file

@ -106,6 +106,11 @@ namespace Misc
return Locked<const T>(*mMutex, *mValue); return Locked<const T>(*mMutex, *mValue);
} }
operator bool() const
{
return static_cast<bool>(mValue);
}
private: private:
std::shared_ptr<std::mutex> mMutex; std::shared_ptr<std::mutex> mMutex;
std::shared_ptr<T> mValue; std::shared_ptr<T> mValue;