From 34e04f010eba105a532722549a5d038cc6aa04dc Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 11 Jun 2023 01:15:44 +0900 Subject: [PATCH] remove Result> --- src/DotRecast.Detour/DtNavMesh.cs | 23 ++++-- src/DotRecast.Detour/DtNavMeshQuery.cs | 80 +++++++------------ src/DotRecast.Detour/LegacyNavMeshQuery.cs | 13 +-- src/DotRecast.Detour/PathUtils.cs | 8 +- .../Draw/RecastDebugDraw.cs | 10 +-- .../Tools/TestNavmeshTool.cs | 6 +- test/DotRecast.Detour.Test/RandomPointTest.cs | 31 +++---- 7 files changed, 72 insertions(+), 99 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index 4c8761b..080bea1 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -211,30 +211,41 @@ namespace DotRecast.Detour ty = (int)Math.Floor((pos.z - m_orig.z) / m_tileHeight); } - public Result> GetTileAndPolyByRef(long refs) + /// Gets the tile and polygon for the specified polygon reference. + /// @param[in] ref The reference for the a polygon. + /// @param[out] tile The tile containing the polygon. + /// @param[out] poly The polygon. + /// @return The status flags for the operation. + public DtStatus GetTileAndPolyByRef(long refs, out DtMeshTile tile, out DtPoly poly) { + tile = null; + poly = null; + if (refs == 0) { - return Results.InvalidParam>("ref = 0"); + return DtStatus.DT_FAILURE; } DecodePolyId(refs, out var salt, out var it, out var ip); if (it >= m_maxTiles) { - return Results.InvalidParam>("tile > m_maxTiles"); + return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } if (m_tiles[it].salt != salt || m_tiles[it].data.header == null) { - return Results.InvalidParam>("Invalid salt or header"); + return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } if (ip >= m_tiles[it].data.header.polyCount) { - return Results.InvalidParam>("poly > polyCount"); + return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } - return Results.Success(Tuple.Create(m_tiles[it], m_tiles[it].data.polys[ip])); + tile = m_tiles[it]; + poly = m_tiles[it].data.polys[ip]; + + return DtStatus.DT_SUCCSESS; } /// @par diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 478268e..e2774a2 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -466,14 +466,12 @@ namespace DotRecast.Detour /// @returns The status flags for the query. public Result ClosestPointOnPolyBoundary(long refs, RcVec3f pos) { - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(refs); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Failed()) { - return Results.Of(tileAndPoly.status, tileAndPoly.message); + return Results.Of(status, ""); } - DtMeshTile tile = tileAndPoly.result.Item1; - DtPoly poly = tileAndPoly.result.Item2; if (tile == null) { return Results.InvalidParam("Invalid tile"); @@ -533,15 +531,12 @@ namespace DotRecast.Detour /// @returns The status flags for the query. public Result GetPolyHeight(long refs, RcVec3f pos) { - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(refs); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Failed()) { - return Results.Of(tileAndPoly.status, tileAndPoly.message); + return Results.Of(status, ""); } - DtMeshTile tile = tileAndPoly.result.Item1; - DtPoly poly = tileAndPoly.result.Item2; - if (!RcVec3f.IsFinite2D(pos)) { return Results.InvalidParam(); @@ -1136,16 +1131,14 @@ namespace DotRecast.Detour // The API input has been cheked already, skip checking internal // data. long bestRef = bestNode.id; - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(bestRef); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(bestRef, out var bestTile, out var bestPoly); + if (status.Failed()) { m_query.status = DtStatus.DT_FAILURE; // The polygon has disappeared during the sliced query, fail. return Results.Of(m_query.status, iter); } - DtMeshTile bestTile = tileAndPoly.result.Item1; - DtPoly bestPoly = tileAndPoly.result.Item2; // Get parent and grand parent poly and tile. long parentRef = 0, grandpaRef = 0; DtMeshTile parentTile = null; @@ -1164,8 +1157,8 @@ namespace DotRecast.Detour if (parentRef != 0) { bool invalidParent = false; - tileAndPoly = m_nav.GetTileAndPolyByRef(parentRef); - invalidParent = tileAndPoly.Failed(); + status = m_nav.GetTileAndPolyByRef(parentRef, out parentTile, out parentPoly); + invalidParent = status.Failed(); if (invalidParent || (grandpaRef != 0 && !m_nav.IsValidPolyRef(grandpaRef))) { // The polygon has disappeared during the sliced query, @@ -1173,9 +1166,6 @@ namespace DotRecast.Detour m_query.status = DtStatus.DT_FAILURE; return Results.Of(m_query.status, iter); } - - parentTile = tileAndPoly.result.Item1; - parentPoly = tileAndPoly.result.Item2; } // decide whether to test raycast to previous nodes @@ -1464,25 +1454,19 @@ namespace DotRecast.Detour { // Calculate portal long from = path[i]; - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(from); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(from, out var fromTile, out var fromPoly); + if (status.Failed()) { return DtStatus.DT_FAILURE; } - DtMeshTile fromTile = tileAndPoly.result.Item1; - DtPoly fromPoly = tileAndPoly.result.Item2; - long to = path[i + 1]; - tileAndPoly = m_nav.GetTileAndPolyByRef(to); - if (tileAndPoly.Failed()) + status = m_nav.GetTileAndPolyByRef(to, out var toTile, out var toPoly); + if (status.Failed()) { return DtStatus.DT_FAILURE; } - DtMeshTile toTile = tileAndPoly.result.Item1; - DtPoly toPoly = tileAndPoly.result.Item2; - Result portals = GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, 0, 0); if (portals.Failed()) { @@ -1969,26 +1953,20 @@ namespace DotRecast.Detour protected Result GetPortalPoints(long from, long to) { - Result> tileAndPolyResult = m_nav.GetTileAndPolyByRef(from); - if (tileAndPolyResult.Failed()) + var status = m_nav.GetTileAndPolyByRef(from, out var fromTile, out var fromPoly); + if (status.Failed()) { - return Results.Of(tileAndPolyResult.status, tileAndPolyResult.message); + return Results.Of(status, ""); } - Tuple tileAndPoly = tileAndPolyResult.result; - DtMeshTile fromTile = tileAndPoly.Item1; - DtPoly fromPoly = tileAndPoly.Item2; int fromType = fromPoly.GetPolyType(); - tileAndPolyResult = m_nav.GetTileAndPolyByRef(to); - if (tileAndPolyResult.Failed()) + status = m_nav.GetTileAndPolyByRef(to, out var toTile, out var toPoly); + if (status.Failed()) { - return Results.Of(tileAndPolyResult.status, tileAndPolyResult.message); + return Results.Of(status, ""); } - tileAndPoly = tileAndPolyResult.result; - DtMeshTile toTile = tileAndPoly.Item1; - DtPoly toPoly = tileAndPoly.Item2; int toType = toPoly.GetPolyType(); return GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, fromType, toType); @@ -3000,10 +2978,10 @@ namespace DotRecast.Detour /// @returns The status flags for the query. public Result GetPolyWallSegments(long refs, bool storePortals, IDtQueryFilter filter) { - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(refs); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Failed()) { - return Results.Of(tileAndPoly.status, tileAndPoly.message); + return Results.Of(status, ""); } if (null == filter) @@ -3011,9 +2989,6 @@ namespace DotRecast.Detour return Results.InvalidParam(); } - DtMeshTile tile = tileAndPoly.result.Item1; - DtPoly poly = tileAndPoly.result.Item2; - List segmentRefs = new List(); List segmentVerts = new List(); List ints = new List(16); @@ -3338,15 +3313,14 @@ namespace DotRecast.Detour /// @param[in] filter The filter to apply. public bool IsValidPolyRef(long refs, IDtQueryFilter filter) { - Result> tileAndPolyResult = m_nav.GetTileAndPolyByRef(refs); - if (tileAndPolyResult.Failed()) + var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Failed()) { return false; } - - Tuple tileAndPoly = tileAndPolyResult.result; + // If cannot pass filter, assume flags has changed and boundary is invalid. - if (!filter.PassFilter(refs, tileAndPoly.Item1, tileAndPoly.Item2)) + if (!filter.PassFilter(refs, tile, poly)) { return false; } diff --git a/src/DotRecast.Detour/LegacyNavMeshQuery.cs b/src/DotRecast.Detour/LegacyNavMeshQuery.cs index 153ce85..65efa18 100644 --- a/src/DotRecast.Detour/LegacyNavMeshQuery.cs +++ b/src/DotRecast.Detour/LegacyNavMeshQuery.cs @@ -264,16 +264,14 @@ namespace DotRecast.Detour // The API input has been cheked already, skip checking internal // data. long bestRef = bestNode.id; - Result> tileAndPoly = m_nav.GetTileAndPolyByRef(bestRef); - if (tileAndPoly.Failed()) + var status = m_nav.GetTileAndPolyByRef(bestRef, out var bestTile, out var bestPoly); + if (status.Failed()) { m_query.status = DtStatus.DT_FAILURE; // The polygon has disappeared during the sliced query, fail. return Results.Of(m_query.status, iter); } - DtMeshTile bestTile = tileAndPoly.result.Item1; - DtPoly bestPoly = tileAndPoly.result.Item2; // Get parent and grand parent poly and tile. long parentRef = 0, grandpaRef = 0; DtMeshTile parentTile = null; @@ -292,8 +290,8 @@ namespace DotRecast.Detour if (parentRef != 0) { bool invalidParent = false; - tileAndPoly = m_nav.GetTileAndPolyByRef(parentRef); - invalidParent = tileAndPoly.Failed(); + status = m_nav.GetTileAndPolyByRef(parentRef, out parentTile, out parentPoly); + invalidParent = status.Failed(); if (invalidParent || (grandpaRef != 0 && !m_nav.IsValidPolyRef(grandpaRef))) { // The polygon has disappeared during the sliced query, @@ -301,9 +299,6 @@ namespace DotRecast.Detour m_query.status = DtStatus.DT_FAILURE; return Results.Of(m_query.status, iter); } - - parentTile = tileAndPoly.result.Item1; - parentPoly = tileAndPoly.result.Item2; } // decide whether to test raycast to previous nodes diff --git a/src/DotRecast.Detour/PathUtils.cs b/src/DotRecast.Detour/PathUtils.cs index 34e4d74..2229a08 100644 --- a/src/DotRecast.Detour/PathUtils.cs +++ b/src/DotRecast.Detour/PathUtils.cs @@ -153,14 +153,12 @@ namespace DotRecast.Detour // Get connected polygons List neis = new List(); - Result> tileAndPoly = navQuery.GetAttachedNavMesh().GetTileAndPolyByRef(path[0]); - if (tileAndPoly.Failed()) + var status = navQuery.GetAttachedNavMesh().GetTileAndPolyByRef(path[0], out var tile, out var poly); + if (status.Failed()) { return path; } - DtMeshTile tile = tileAndPoly.result.Item1; - DtPoly poly = tileAndPoly.result.Item2; for (int k = tile.polyLinks[poly.index]; k != DtNavMesh.DT_NULL_LINK; k = tile.links[k].next) { @@ -198,4 +196,4 @@ namespace DotRecast.Detour return path; } } -} +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs index 154457c..17a8e54 100644 --- a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs @@ -1281,16 +1281,12 @@ public class RecastDebugDraw : DebugDraw return; } - Result> tileAndPolyResult = mesh.GetTileAndPolyByRef(refs); - if (tileAndPolyResult.Failed()) + var status = mesh.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Failed()) { return; } - Tuple tileAndPoly = tileAndPolyResult.result; - DtMeshTile tile = tileAndPoly.Item1; - DtPoly poly = tileAndPoly.Item2; - DepthMask(false); int c = DuTransCol(col, 64); @@ -1406,4 +1402,4 @@ public class RecastDebugDraw : DebugDraw End(); } -} +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs index c0e0d08..4a66888 100644 --- a/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/TestNavmeshTool.cs @@ -951,11 +951,9 @@ public class TestNavmeshTool : ITool { RcVec3f center = RcVec3f.Zero; - Result> tileAndPoly = navMesh.GetTileAndPolyByRef(refs); - if (tileAndPoly.Succeeded()) + var status = navMesh.GetTileAndPolyByRef(refs, out var tile, out var poly); + if (status.Succeeded()) { - DtMeshTile tile = tileAndPoly.result.Item1; - DtPoly poly = tileAndPoly.result.Item2; for (int i = 0; i < poly.vertCount; ++i) { int v = poly.verts[i] * 3; diff --git a/test/DotRecast.Detour.Test/RandomPointTest.cs b/test/DotRecast.Detour.Test/RandomPointTest.cs index 8cf546a..3b276ad 100644 --- a/test/DotRecast.Detour.Test/RandomPointTest.cs +++ b/test/DotRecast.Detour.Test/RandomPointTest.cs @@ -37,16 +37,17 @@ public class RandomPointTest : AbstractDetourTest { var status = query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); Assert.That(status.Succeeded(), Is.True); - Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(randomRef).result; + + status = navmesh.GetTileAndPolyByRef(randomRef, out var tile, out var poly); float[] bmin = new float[2]; float[] bmax = new float[2]; - for (int j = 0; j < tileAndPoly.Item2.vertCount; j++) + for (int j = 0; j < poly.vertCount; j++) { - int v = tileAndPoly.Item2.verts[j] * 3; - bmin[0] = j == 0 ? tileAndPoly.Item1.data.verts[v] : Math.Min(bmin[0], tileAndPoly.Item1.data.verts[v]); - bmax[0] = j == 0 ? tileAndPoly.Item1.data.verts[v] : Math.Max(bmax[0], tileAndPoly.Item1.data.verts[v]); - bmin[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Min(bmin[1], tileAndPoly.Item1.data.verts[v + 2]); - bmax[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Max(bmax[1], tileAndPoly.Item1.data.verts[v + 2]); + int v = poly.verts[j] * 3; + bmin[0] = j == 0 ? tile.data.verts[v] : Math.Min(bmin[0], tile.data.verts[v]); + bmax[0] = j == 0 ? tile.data.verts[v] : Math.Max(bmax[0], tile.data.verts[v]); + bmin[1] = j == 0 ? tile.data.verts[v + 2] : Math.Min(bmin[1], tile.data.verts[v + 2]); + bmax[1] = j == 0 ? tile.data.verts[v + 2] : Math.Max(bmax[1], tile.data.verts[v + 2]); } Assert.That(randomPt.x >= bmin[0], Is.True); @@ -69,18 +70,18 @@ public class RandomPointTest : AbstractDetourTest randomRef = nextRandomRef; randomPt = nextRandomPt; - - Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(randomRef).result; + + status = navmesh.GetTileAndPolyByRef(randomRef, out var tile, out var poly); float[] bmin = new float[2]; float[] bmax = new float[2]; - for (int j = 0; j < tileAndPoly.Item2.vertCount; j++) + for (int j = 0; j < poly.vertCount; j++) { - int v = tileAndPoly.Item2.verts[j] * 3; - bmin[0] = j == 0 ? tileAndPoly.Item1.data.verts[v] : Math.Min(bmin[0], tileAndPoly.Item1.data.verts[v]); - bmax[0] = j == 0 ? tileAndPoly.Item1.data.verts[v] : Math.Max(bmax[0], tileAndPoly.Item1.data.verts[v]); - bmin[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Min(bmin[1], tileAndPoly.Item1.data.verts[v + 2]); - bmax[1] = j == 0 ? tileAndPoly.Item1.data.verts[v + 2] : Math.Max(bmax[1], tileAndPoly.Item1.data.verts[v + 2]); + int v = poly.verts[j] * 3; + bmin[0] = j == 0 ? tile.data.verts[v] : Math.Min(bmin[0], tile.data.verts[v]); + bmax[0] = j == 0 ? tile.data.verts[v] : Math.Max(bmax[0], tile.data.verts[v]); + bmin[1] = j == 0 ? tile.data.verts[v + 2] : Math.Min(bmin[1], tile.data.verts[v + 2]); + bmax[1] = j == 0 ? tile.data.verts[v + 2] : Math.Max(bmax[1], tile.data.verts[v + 2]); } Assert.That(randomPt.x >= bmin[0], Is.True);