From 31734246b617840274ac6076c2258f81b55ab96b Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 8 Oct 2023 14:28:56 +0900 Subject: [PATCH] refactor: DtFindPathOptions, DtRaycastOptions --- src/DotRecast.Detour/DtFindPathOption.cs | 2 +- src/DotRecast.Detour/DtFindPathOptions.cs | 8 ++++++ src/DotRecast.Detour/DtNavMeshQuery.cs | 27 +++++++------------ src/DotRecast.Detour/DtRaycastOptions.cs | 8 ++++++ src/DotRecast.Detour/DtStraightPathOptions.cs | 2 +- .../Tools/RcTestNavMeshTool.cs | 6 ++--- test/DotRecast.Detour.Test/FindPathTest.cs | 2 +- 7 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/DotRecast.Detour/DtFindPathOptions.cs create mode 100644 src/DotRecast.Detour/DtRaycastOptions.cs diff --git a/src/DotRecast.Detour/DtFindPathOption.cs b/src/DotRecast.Detour/DtFindPathOption.cs index 7c119d9..0d4b1c3 100644 --- a/src/DotRecast.Detour/DtFindPathOption.cs +++ b/src/DotRecast.Detour/DtFindPathOption.cs @@ -4,7 +4,7 @@ { public static readonly DtFindPathOption NoOption = new DtFindPathOption(DtDefaultQueryHeuristic.Default, 0, 0); - public static readonly DtFindPathOption AnyAngle = new DtFindPathOption(DtDefaultQueryHeuristic.Default, DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE, float.MaxValue); + public static readonly DtFindPathOption AnyAngle = new DtFindPathOption(DtDefaultQueryHeuristic.Default, DtFindPathOptions.DT_FINDPATH_ANY_ANGLE, float.MaxValue); public static readonly DtFindPathOption ZeroScale = new DtFindPathOption(new DtDefaultQueryHeuristic(0.0f), 0, 0); public readonly IDtQueryHeuristic heuristic; diff --git a/src/DotRecast.Detour/DtFindPathOptions.cs b/src/DotRecast.Detour/DtFindPathOptions.cs new file mode 100644 index 0000000..c26d0f5 --- /dev/null +++ b/src/DotRecast.Detour/DtFindPathOptions.cs @@ -0,0 +1,8 @@ +namespace DotRecast.Detour +{ + /// Options for dtNavMeshQuery::initSlicedFindPath and updateSlicedFindPath + public static class DtFindPathOptions + { + public const int DT_FINDPATH_ANY_ANGLE = 0x02; //< use raycasts during pathfind to "shortcut" (raycast still consider costs) + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index d1b6dae..25cbd78 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -24,20 +24,10 @@ using DotRecast.Core; namespace DotRecast.Detour { - using static DtNode; public class DtNavMeshQuery { - /** - * Use raycasts during pathfind to "shortcut" (raycast still consider costs) Options for - * NavMeshQuery::initSlicedFindPath and updateSlicedFindPath - */ - public const int DT_FINDPATH_ANY_ANGLE = 0x02; - - /** Raycast should calculate movement cost along the ray and fill RaycastHit::cost */ - public const int DT_RAYCAST_USE_COSTS = 0x01; - /// < Add a vertex at every polygon edge crossing. protected readonly DtNavMesh m_nav; @@ -766,7 +756,7 @@ namespace DotRecast.Detour float raycastLimitSqr = RcMath.Sqr(raycastLimit); // trade quality with performance? - if ((options & DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f) + if ((options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f) { // limiting to several times the character radius yields nice results. It is not sensitive // so it is enough to compute it from the first tile. @@ -838,7 +828,7 @@ namespace DotRecast.Detour // decide whether to test raycast to previous nodes bool tryLOS = false; - if ((options & DT_FINDPATH_ANY_ANGLE) != 0) + if ((options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0) { if ((parentRef != 0) && (raycastLimitSqr >= float.MaxValue || RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < raycastLimitSqr)) @@ -896,7 +886,7 @@ namespace DotRecast.Detour if (tryLOS) { var rayStatus = Raycast(parentRef, parentNode.pos, neighbourPos, filter, - DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit); + DtRaycastOptions.DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit); if (rayStatus.Succeeded()) { foundShortCut = rayHit.t >= 1.0f; @@ -1036,7 +1026,7 @@ namespace DotRecast.Detour } // trade quality with performance? - if ((options & DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f) + if ((options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f) { // limiting to several times the character radius yields nice results. It is not sensitive // so it is enough to compute it from the first tile. @@ -1156,7 +1146,7 @@ namespace DotRecast.Detour // decide whether to test raycast to previous nodes bool tryLOS = false; - if ((m_query.options & DT_FINDPATH_ANY_ANGLE) != 0) + if ((m_query.options & DtFindPathOptions.DT_FINDPATH_ANY_ANGLE) != 0) { if ((parentRef != 0) && (m_query.raycastLimitSqr >= float.MaxValue || RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < m_query.raycastLimitSqr)) @@ -1216,7 +1206,8 @@ namespace DotRecast.Detour List shortcut = null; if (tryLOS) { - status = Raycast(parentRef, parentNode.pos, neighbourPos, m_query.filter, DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit); + status = Raycast(parentRef, parentNode.pos, neighbourPos, m_query.filter, + DtRaycastOptions.DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit); if (status.Succeeded()) { foundShortCut = rayHit.t >= 1.0f; @@ -2239,7 +2230,7 @@ namespace DotRecast.Detour hit.t = float.MaxValue; // add the cost - if ((options & DT_RAYCAST_USE_COSTS) != 0) + if ((options & DtRaycastOptions.DT_RAYCAST_USE_COSTS) != 0) { hit.pathCost += filter.GetCost(curPos, endPos, prevRef, prevTile, prevPoly, curRef, tile, poly, curRef, tile, poly); @@ -2344,7 +2335,7 @@ namespace DotRecast.Detour } // add the cost - if ((options & DT_RAYCAST_USE_COSTS) != 0) + if ((options & DtRaycastOptions.DT_RAYCAST_USE_COSTS) != 0) { // compute the intersection point at the furthest end of the polygon // and correct the height (since the raycast moves in 2d) diff --git a/src/DotRecast.Detour/DtRaycastOptions.cs b/src/DotRecast.Detour/DtRaycastOptions.cs new file mode 100644 index 0000000..62bbef9 --- /dev/null +++ b/src/DotRecast.Detour/DtRaycastOptions.cs @@ -0,0 +1,8 @@ +namespace DotRecast.Detour +{ + /// Options for dtNavMeshQuery::raycast + public static class DtRaycastOptions + { + public const int DT_RAYCAST_USE_COSTS = 0x01; //< Raycast should calculate movement cost along the ray and fill RaycastHit::cost + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour/DtStraightPathOptions.cs b/src/DotRecast.Detour/DtStraightPathOptions.cs index 590ad0d..c2428f9 100644 --- a/src/DotRecast.Detour/DtStraightPathOptions.cs +++ b/src/DotRecast.Detour/DtStraightPathOptions.cs @@ -1,7 +1,7 @@ namespace DotRecast.Detour { /// Options for dtNavMeshQuery::findStraightPath. - public class DtStraightPathOptions + public static class DtStraightPathOptions { public const int DT_STRAIGHTPATH_AREA_CROSSINGS = 0x01; //< Add a vertex at every polygon edge crossing where area changes. public const int DT_STRAIGHTPATH_ALL_CROSSINGS = 0x02; //< Add a vertex at every polygon edge crossing. diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs index 0f45a74..f502041 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs @@ -37,7 +37,7 @@ namespace DotRecast.Recast.Toolset.Tools polys.Clear(); smoothPath.Clear(); - var opt = new DtFindPathOption(enableRaycast ? DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE : 0, float.MaxValue); + var opt = new DtFindPathOption(enableRaycast ? DtFindPathOptions.DT_FINDPATH_ANY_ANGLE : 0, float.MaxValue); navQuery.FindPath(startRef, endRef, startPt, endPt, filter, ref polys, opt); if (0 >= polys.Count) return DtStatus.DT_FAILURE; @@ -176,7 +176,7 @@ namespace DotRecast.Recast.Toolset.Tools polys.Clear(); straightPath.Clear(); - var opt = new DtFindPathOption(enableRaycast ? DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE : 0, float.MaxValue); + var opt = new DtFindPathOption(enableRaycast ? DtFindPathOptions.DT_FINDPATH_ANY_ANGLE : 0, float.MaxValue); navQuery.FindPath(startRef, endRef, startPt, endPt, filter, ref polys, opt); if (0 >= polys.Count) @@ -206,7 +206,7 @@ namespace DotRecast.Recast.Toolset.Tools } return navQuery.InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, - enableRaycast ? DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE : 0, + enableRaycast ? DtFindPathOptions.DT_FINDPATH_ANY_ANGLE : 0, float.MaxValue ); } diff --git a/test/DotRecast.Detour.Test/FindPathTest.cs b/test/DotRecast.Detour.Test/FindPathTest.cs index 14c4fca..839a7c4 100644 --- a/test/DotRecast.Detour.Test/FindPathTest.cs +++ b/test/DotRecast.Detour.Test/FindPathTest.cs @@ -163,7 +163,7 @@ public class FindPathTest : AbstractDetourTest long endRef = endRefs[i]; var startPos = startPoss[i]; var endPos = endPoss[i]; - query.InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE); + query.InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, DtFindPathOptions.DT_FINDPATH_ANY_ANGLE); DtStatus status = DtStatus.DT_IN_PROGRESS; while (status.InProgress()) {