1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-19 13:11:32 +00:00

Refactor DetourNavigator to pass prng along, use world prng for AiWander

This commit is contained in:
ζeh Matt 2022-05-17 18:04:11 +03:00
parent b2fab5f5ad
commit 18f16eac4c
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0
6 changed files with 13 additions and 8 deletions

View file

@ -359,7 +359,10 @@ namespace MWMechanics
{ {
// findRandomPointAroundCircle uses wanderDistance as limit for random and not as exact distance // findRandomPointAroundCircle uses wanderDistance as limit for random and not as exact distance
if (const auto destination = DetourNavigator::findRandomPointAroundCircle(*navigator, halfExtents, if (const auto destination = DetourNavigator::findRandomPointAroundCircle(*navigator, halfExtents,
mInitialActorPosition, wanderDistance, navigatorFlags)) mInitialActorPosition, wanderDistance, navigatorFlags, []() {
auto& prng = MWBase::Environment::get().getWorld()->getPrng();
return Misc::Rng::rollProbability(prng);
}))
mDestination = *destination; mDestination = *destination;
else else
mDestination = getRandomPointAround(mInitialActorPosition, wanderRadius); mDestination = getRandomPointAround(mInitialActorPosition, wanderRadius);

View file

@ -819,7 +819,8 @@ namespace
Misc::Rng::init(42); Misc::Rng::init(42);
const auto result = findRandomPointAroundCircle(*mNavigator, mAgentHalfExtents, mStart, 100.0, Flag_walk); const auto result = findRandomPointAroundCircle(*mNavigator, mAgentHalfExtents, mStart, 100.0, Flag_walk,
[]() { return Misc::Rng::rollClosedProbability(); });
ASSERT_THAT(result, Optional(Vec3fEq(70.35845947265625, 335.592041015625, -2.6667339801788330078125))) ASSERT_THAT(result, Optional(Vec3fEq(70.35845947265625, 335.592041015625, -2.6667339801788330078125)))
<< (result ? *result : osg::Vec3f()); << (result ? *result : osg::Vec3f());

View file

@ -10,7 +10,7 @@
namespace DetourNavigator namespace DetourNavigator
{ {
std::optional<osg::Vec3f> findRandomPointAroundCircle(const dtNavMesh& navMesh, const osg::Vec3f& halfExtents, std::optional<osg::Vec3f> findRandomPointAroundCircle(const dtNavMesh& navMesh, const osg::Vec3f& halfExtents,
const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, const DetourSettings& settings) const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, const DetourSettings& settings, float(*prng)())
{ {
dtNavMeshQuery navMeshQuery; dtNavMeshQuery navMeshQuery;
if (!initNavMeshQuery(navMeshQuery, navMesh, settings.mMaxNavMeshQueryNodes)) if (!initNavMeshQuery(navMeshQuery, navMesh, settings.mMaxNavMeshQueryNodes))
@ -25,8 +25,9 @@ namespace DetourNavigator
dtPolyRef resultRef = 0; dtPolyRef resultRef = 0;
osg::Vec3f resultPosition; osg::Vec3f resultPosition;
navMeshQuery.findRandomPointAroundCircle(startRef, start.ptr(), maxRadius, &queryFilter, navMeshQuery.findRandomPointAroundCircle(startRef, start.ptr(), maxRadius, &queryFilter,
[]() { return Misc::Rng::rollProbability(); }, &resultRef, resultPosition.ptr()); prng, &resultRef, resultPosition.ptr());
if (resultRef == 0) if (resultRef == 0)
return std::optional<osg::Vec3f>(); return std::optional<osg::Vec3f>();

View file

@ -13,7 +13,7 @@ namespace DetourNavigator
struct DetourSettings; struct DetourSettings;
std::optional<osg::Vec3f> findRandomPointAroundCircle(const dtNavMesh& navMesh, const osg::Vec3f& halfExtents, std::optional<osg::Vec3f> findRandomPointAroundCircle(const dtNavMesh& navMesh, const osg::Vec3f& halfExtents,
const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, const DetourSettings& settings); const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, const DetourSettings& settings, float(*prng)());
} }
#endif #endif

View file

@ -6,7 +6,7 @@
namespace DetourNavigator namespace DetourNavigator
{ {
std::optional<osg::Vec3f> findRandomPointAroundCircle(const Navigator& navigator, const osg::Vec3f& agentHalfExtents, std::optional<osg::Vec3f> findRandomPointAroundCircle(const Navigator& navigator, const osg::Vec3f& agentHalfExtents,
const osg::Vec3f& start, const float maxRadius, const Flags includeFlags) const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, float(*prng)())
{ {
const auto navMesh = navigator.getNavMesh(agentHalfExtents); const auto navMesh = navigator.getNavMesh(agentHalfExtents);
if (!navMesh) if (!navMesh)
@ -14,7 +14,7 @@ namespace DetourNavigator
const auto& settings = navigator.getSettings(); const auto& settings = navigator.getSettings();
const auto result = DetourNavigator::findRandomPointAroundCircle(navMesh->lockConst()->getImpl(), const auto result = DetourNavigator::findRandomPointAroundCircle(navMesh->lockConst()->getImpl(),
toNavMeshCoordinates(settings.mRecast, agentHalfExtents), toNavMeshCoordinates(settings.mRecast, start), toNavMeshCoordinates(settings.mRecast, agentHalfExtents), toNavMeshCoordinates(settings.mRecast, start),
toNavMeshCoordinates(settings.mRecast, maxRadius), includeFlags, settings.mDetour); toNavMeshCoordinates(settings.mRecast, maxRadius), includeFlags, settings.mDetour, prng);
if (!result) if (!result)
return std::nullopt; return std::nullopt;
return std::optional<osg::Vec3f>(fromNavMeshCoordinates(settings.mRecast, *result)); return std::optional<osg::Vec3f>(fromNavMeshCoordinates(settings.mRecast, *result));

View file

@ -51,7 +51,7 @@ namespace DetourNavigator
* @return not empty optional with position if point is found and empty optional if point is not found. * @return not empty optional with position if point is found and empty optional if point is not found.
*/ */
std::optional<osg::Vec3f> findRandomPointAroundCircle(const Navigator& navigator, const osg::Vec3f& agentHalfExtents, std::optional<osg::Vec3f> findRandomPointAroundCircle(const Navigator& navigator, const osg::Vec3f& agentHalfExtents,
const osg::Vec3f& start, const float maxRadius, const Flags includeFlags); const osg::Vec3f& start, const float maxRadius, const Flags includeFlags, float(*prng)());
/** /**
* @brief raycast finds farest navmesh point from start on a line from start to end that has path from start. * @brief raycast finds farest navmesh point from start on a line from start to end that has path from start.