From 533afe49e2785b40ba5aa9f1cc7b619799937e45 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 8 Oct 2023 14:23:58 +0900 Subject: [PATCH] refactor: flags --- src/DotRecast.Detour.Crowd/DtCrowdAgent.cs | 4 +- src/DotRecast.Detour.Crowd/DtPathCorridor.cs | 4 +- src/DotRecast.Detour/DtNavMeshQuery.cs | 41 ++++++------------- src/DotRecast.Detour/DtPathUtils.cs | 2 +- src/DotRecast.Detour/DtStraightPathFlags.cs | 10 +++++ src/DotRecast.Detour/DtStraightPathOption.cs | 4 +- src/DotRecast.Detour/DtStraightPathOptions.cs | 9 ++++ .../Tools/CrowdSampleTool.cs | 2 +- .../Tools/TestNavmeshSampleTool.cs | 8 ++-- .../Tools/RcTestNavMeshTool.cs | 6 +-- .../PathCorridorTest.cs | 4 +- 11 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 src/DotRecast.Detour/DtStraightPathFlags.cs create mode 100644 src/DotRecast.Detour/DtStraightPathOptions.cs diff --git a/src/DotRecast.Detour.Crowd/DtCrowdAgent.cs b/src/DotRecast.Detour.Crowd/DtCrowdAgent.cs index 060003e..e8cb271 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowdAgent.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowdAgent.cs @@ -132,7 +132,7 @@ namespace DotRecast.Detour.Crowd return false; bool offMeshConnection = ((corners[corners.Count - 1].flags - & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) ? true : false; if (offMeshConnection) @@ -150,7 +150,7 @@ namespace DotRecast.Detour.Crowd if (0 == corners.Count) return range; - bool endOfPath = ((corners[corners.Count - 1].flags & DtNavMeshQuery.DT_STRAIGHTPATH_END) != 0) ? true : false; + bool endOfPath = ((corners[corners.Count - 1].flags & DtStraightPathFlags.DT_STRAIGHTPATH_END) != 0) ? true : false; if (endOfPath) return Math.Min(RcVec3f.Dist2D(npos, corners[corners.Count - 1].pos), range); diff --git a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs index 9166541..d670a18 100644 --- a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs +++ b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs @@ -123,7 +123,7 @@ namespace DotRecast.Detour.Crowd int start = 0; foreach (DtStraightPath spi in corners) { - if ((spi.flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0 + if ((spi.flags & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0 || RcVec3f.Dist2DSqr(spi.pos, m_pos) > MIN_TARGET_DIST) { break; @@ -137,7 +137,7 @@ namespace DotRecast.Detour.Crowd for (int i = start; i < corners.Count; i++) { DtStraightPath spi = corners[i]; - if ((spi.flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + if ((spi.flags & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) { end = i + 1; break; diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index d19ec32..d1b6dae 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -38,23 +38,6 @@ namespace DotRecast.Detour /** Raycast should calculate movement cost along the ray and fill RaycastHit::cost */ public const int DT_RAYCAST_USE_COSTS = 0x01; - /// Vertex flags returned by findStraightPath. - /** The vertex is the start position in the path. */ - public const int DT_STRAIGHTPATH_START = 0x01; - - /** The vertex is the end position in the path. */ - public const int DT_STRAIGHTPATH_END = 0x02; - - /** The vertex is the start of an off-mesh connection. */ - public const int DT_STRAIGHTPATH_OFFMESH_CONNECTION = 0x04; - - /// Options for findStraightPath. - 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. protected readonly DtNavMesh m_nav; @@ -1448,7 +1431,7 @@ namespace DotRecast.Detour } // If reached end of path or there is no space to append more vertices, return. - if (flags == DT_STRAIGHTPATH_END || straightPath.Count >= maxStraightPath) + if (flags == DtStraightPathFlags.DT_STRAIGHTPATH_END || straightPath.Count >= maxStraightPath) { return DtStatus.DT_SUCCSESS; } @@ -1486,7 +1469,7 @@ namespace DotRecast.Detour break; } - if ((options & DT_STRAIGHTPATH_AREA_CROSSINGS) != 0) + if ((options & DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS) != 0) { // Skip intersection if only area crossings are requested. if (fromPoly.GetArea() == toPoly.GetArea()) @@ -1562,7 +1545,7 @@ namespace DotRecast.Detour } // Add start point. - DtStatus stat = AppendVertex(closestStartPos, DT_STRAIGHTPATH_START, path[0], ref straightPath, maxStraightPath); + DtStatus stat = AppendVertex(closestStartPos, DtStraightPathFlags.DT_STRAIGHTPATH_START, path[0], ref straightPath, maxStraightPath); if (!stat.InProgress()) { return stat; @@ -1606,7 +1589,7 @@ namespace DotRecast.Detour } // Append portals along the current straight path segment. - if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) + if ((options & (DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS | DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) { // Ignore status return value as we're just about to return anyway. AppendPortals(apexIndex, i, closestEndPos, path, ref straightPath, maxStraightPath, options); @@ -1649,7 +1632,7 @@ namespace DotRecast.Detour else { // Append portals along the current straight path segment. - if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) + if ((options & (DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS | DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) { stat = AppendPortals(apexIndex, leftIndex, portalLeft, path, ref straightPath, maxStraightPath, options); if (!stat.InProgress()) @@ -1664,11 +1647,11 @@ namespace DotRecast.Detour int flags = 0; if (leftPolyRef == 0) { - flags = DT_STRAIGHTPATH_END; + flags = DtStraightPathFlags.DT_STRAIGHTPATH_END; } else if (leftPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) { - flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION; + flags = DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION; } long refs = leftPolyRef; @@ -1705,7 +1688,7 @@ namespace DotRecast.Detour else { // Append portals along the current straight path segment. - if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) + if ((options & (DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS | DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) { stat = AppendPortals(apexIndex, rightIndex, portalRight, path, ref straightPath, maxStraightPath, options); if (!stat.InProgress()) @@ -1720,11 +1703,11 @@ namespace DotRecast.Detour int flags = 0; if (rightPolyRef == 0) { - flags = DT_STRAIGHTPATH_END; + flags = DtStraightPathFlags.DT_STRAIGHTPATH_END; } else if (rightPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) { - flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION; + flags = DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION; } long refs = rightPolyRef; @@ -1750,7 +1733,7 @@ namespace DotRecast.Detour } // Append portals along the current straight path segment. - if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) + if ((options & (DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS | DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) { stat = AppendPortals(apexIndex, path.Count - 1, closestEndPos, path, ref straightPath, maxStraightPath, options); if (!stat.InProgress()) @@ -1761,7 +1744,7 @@ namespace DotRecast.Detour } // Ignore status return value as we're just about to return anyway. - AppendVertex(closestEndPos, DT_STRAIGHTPATH_END, 0, ref straightPath, maxStraightPath); + AppendVertex(closestEndPos, DtStraightPathFlags.DT_STRAIGHTPATH_END, 0, ref straightPath, maxStraightPath); return DtStatus.DT_SUCCSESS | (straightPath.Count >= maxStraightPath ? DtStatus.DT_BUFFER_TOO_SMALL : DtStatus.DT_STATUS_NOTHING); } diff --git a/src/DotRecast.Detour/DtPathUtils.cs b/src/DotRecast.Detour/DtPathUtils.cs index 5243135..b495dd5 100644 --- a/src/DotRecast.Detour/DtPathUtils.cs +++ b/src/DotRecast.Detour/DtPathUtils.cs @@ -50,7 +50,7 @@ namespace DotRecast.Detour while (ns < straightPath.Count) { // Stop at Off-Mesh link or when point is further than slop away. - if (((straightPath[ns].flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + if (((straightPath[ns].flags & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) || !InRange(straightPath[ns].pos, startPos, minTargetDist, 1000.0f)) break; ns++; diff --git a/src/DotRecast.Detour/DtStraightPathFlags.cs b/src/DotRecast.Detour/DtStraightPathFlags.cs new file mode 100644 index 0000000..a0ea3a3 --- /dev/null +++ b/src/DotRecast.Detour/DtStraightPathFlags.cs @@ -0,0 +1,10 @@ +namespace DotRecast.Detour +{ + /// Vertex flags returned by dtNavMeshQuery::findStraightPath. + public static class DtStraightPathFlags + { + public const int DT_STRAIGHTPATH_START = 0x01; //< The vertex is the start position in the path. + public const int DT_STRAIGHTPATH_END = 0x02; //< The vertex is the end position in the path. + public const int DT_STRAIGHTPATH_OFFMESH_CONNECTION = 0x04; //< The vertex is the start of an off-mesh connection. + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour/DtStraightPathOption.cs b/src/DotRecast.Detour/DtStraightPathOption.cs index 323b2d7..4e3fe8e 100644 --- a/src/DotRecast.Detour/DtStraightPathOption.cs +++ b/src/DotRecast.Detour/DtStraightPathOption.cs @@ -5,8 +5,8 @@ namespace DotRecast.Detour public class DtStraightPathOption { public static readonly DtStraightPathOption None = new DtStraightPathOption(0, "None"); - public static readonly DtStraightPathOption AreaCrossings = new DtStraightPathOption(DtNavMeshQuery.DT_STRAIGHTPATH_AREA_CROSSINGS, "Area"); - public static readonly DtStraightPathOption AllCrossings = new DtStraightPathOption(DtNavMeshQuery.DT_STRAIGHTPATH_ALL_CROSSINGS, "All"); + public static readonly DtStraightPathOption AreaCrossings = new DtStraightPathOption(DtStraightPathOptions.DT_STRAIGHTPATH_AREA_CROSSINGS, "Area"); + public static readonly DtStraightPathOption AllCrossings = new DtStraightPathOption(DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS, "All"); public static readonly RcImmutableArray Values = RcImmutableArray.Create( None, AreaCrossings, AllCrossings diff --git a/src/DotRecast.Detour/DtStraightPathOptions.cs b/src/DotRecast.Detour/DtStraightPathOptions.cs new file mode 100644 index 0000000..590ad0d --- /dev/null +++ b/src/DotRecast.Detour/DtStraightPathOptions.cs @@ -0,0 +1,9 @@ +namespace DotRecast.Detour +{ + /// Options for dtNavMeshQuery::findStraightPath. + public 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. + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdSampleTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdSampleTool.cs index 08bbf4c..64a1715 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdSampleTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdSampleTool.cs @@ -263,7 +263,7 @@ public class CrowdSampleTool : ISampleTool } if ((ag.corners[ag.corners.Count - 1].flags - & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) { RcVec3f v = ag.corners[ag.corners.Count - 1].pos; dd.Vertex(v.x, v.y, v.z, DuRGBA(192, 0, 0, 192)); diff --git a/src/DotRecast.Recast.Demo/Tools/TestNavmeshSampleTool.cs b/src/DotRecast.Recast.Demo/Tools/TestNavmeshSampleTool.cs index 826680b..405be80 100644 --- a/src/DotRecast.Recast.Demo/Tools/TestNavmeshSampleTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TestNavmeshSampleTool.cs @@ -288,7 +288,7 @@ public class TestNavmeshSampleTool : ISampleTool DtStraightPath straightPathItem = m_straightPath[i]; DtStraightPath straightPathItem2 = m_straightPath[i + 1]; int col; - if ((straightPathItem.flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + if ((straightPathItem.flags & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) { col = offMeshCol; } @@ -307,15 +307,15 @@ public class TestNavmeshSampleTool : ISampleTool { DtStraightPath straightPathItem = m_straightPath[i]; int col; - if ((straightPathItem.flags & DtNavMeshQuery.DT_STRAIGHTPATH_START) != 0) + if ((straightPathItem.flags & DtStraightPathFlags.DT_STRAIGHTPATH_START) != 0) { col = startCol; } - else if ((straightPathItem.flags & DtNavMeshQuery.DT_STRAIGHTPATH_END) != 0) + else if ((straightPathItem.flags & DtStraightPathFlags.DT_STRAIGHTPATH_END) != 0) { col = endCol; } - else if ((straightPathItem.flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) + else if ((straightPathItem.flags & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) { col = offMeshCol; } diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs index f08511c..0f45a74 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs @@ -64,10 +64,10 @@ namespace DotRecast.Recast.Toolset.Tools break; } - bool endOfPath = (steerPosFlag & DtNavMeshQuery.DT_STRAIGHTPATH_END) != 0 + bool endOfPath = (steerPosFlag & DtStraightPathFlags.DT_STRAIGHTPATH_END) != 0 ? true : false; - bool offMeshConnection = (steerPosFlag & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0 + bool offMeshConnection = (steerPosFlag & DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0 ? true : false; @@ -238,7 +238,7 @@ namespace DotRecast.Recast.Toolset.Tools } straightPath = new List(MAX_POLYS); - navQuery.FindStraightPath(startPos, epos, path, ref straightPath, MAX_POLYS, DtNavMeshQuery.DT_STRAIGHTPATH_ALL_CROSSINGS); + navQuery.FindStraightPath(startPos, epos, path, ref straightPath, MAX_POLYS, DtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS); } return DtStatus.DT_SUCCSESS; diff --git a/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs b/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs index 55ffded..cab16cf 100644 --- a/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs +++ b/test/DotRecast.Detour.Crowd.Test/PathCorridorTest.cs @@ -74,8 +74,8 @@ public class PathCorridorTest straightPath.Add(new DtStraightPath(RcVec3f.Of(10, 20, 30.00001f), 0, 0)); // too close straightPath.Add(new DtStraightPath(RcVec3f.Of(10, 20, 30.00002f), 0, 0)); // too close straightPath.Add(new DtStraightPath(RcVec3f.Of(11f, 21, 32f), 0, 0)); - straightPath.Add(new DtStraightPath(RcVec3f.Of(12f, 22, 33f), DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION, 0)); // offmesh - straightPath.Add(new DtStraightPath(RcVec3f.Of(11f, 21, 32f), DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION, 0)); // offmesh + straightPath.Add(new DtStraightPath(RcVec3f.Of(12f, 22, 33f), DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION, 0)); // offmesh + straightPath.Add(new DtStraightPath(RcVec3f.Of(11f, 21, 32f), DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION, 0)); // offmesh var mockQuery = new Mock(It.IsAny()); mockQuery.Setup(q => q.FindStraightPath(