From e22c31052cd746576c28c29c18f18956504ef779 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 15 Jul 2023 11:11:03 +0900 Subject: [PATCH] remove nullable RcVec3f --- src/DotRecast.Detour/DtNavMeshQuery.cs | 11 +++-- src/DotRecast.Recast.Demo/RecastDemo.cs | 29 +++++++------ .../Tools/CrowdProfilingTool.cs | 43 ++++++++----------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index ca94f5b..1dce91b 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -3157,8 +3157,10 @@ namespace DotRecast.Detour m_openList.Push(startNode); float radiusSqr = Sqr(maxRadius); - RcVec3f? bestvj = null; - RcVec3f? bestvi = null; + + var hasBestV = false; + var bestvj = RcVec3f.Zero; + var bestvi = RcVec3f.Zero; var status = DtStatus.DT_SUCCSESS; while (!m_openList.IsEmpty()) @@ -3238,6 +3240,7 @@ namespace DotRecast.Detour hitPos.x = bestTile.data.verts[vj + 0] + (bestTile.data.verts[vi + 0] - bestTile.data.verts[vj + 0]) * tseg; hitPos.y = bestTile.data.verts[vj + 1] + (bestTile.data.verts[vi + 1] - bestTile.data.verts[vj + 1]) * tseg; hitPos.z = bestTile.data.verts[vj + 2] + (bestTile.data.verts[vi + 2] - bestTile.data.verts[vj + 2]) * tseg; + hasBestV = true; bestvj = RcVec3f.Of(bestTile.data.verts, vj); bestvi = RcVec3f.Of(bestTile.data.verts, vi); } @@ -3322,9 +3325,9 @@ namespace DotRecast.Detour } // Calc hit normal. - if (bestvi != null && bestvj != null) + if (hasBestV) { - var tangent = bestvi.Value.Subtract(bestvj.Value); + var tangent = bestvi.Subtract(bestvj); hitNormal.x = tangent.z; hitNormal.y = 0; hitNormal.z = -tangent.x; diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index 7458f26..b018645 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -517,18 +517,21 @@ public class RecastDemo : IRecastDemoChannel if (_sample.IsChanged()) { - RcVec3f? bminN = null; - RcVec3f? bmaxN = null; + bool hasBound = false; + RcVec3f bminN = RcVec3f.Zero; + RcVec3f bmaxN = RcVec3f.Zero; if (_sample.GetInputGeom() != null) { bminN = _sample.GetInputGeom().GetMeshBoundsMin(); bmaxN = _sample.GetInputGeom().GetMeshBoundsMax(); + hasBound = true; } else if (_sample.GetNavMesh() != null) { RcVec3f[] bounds = NavMeshUtils.GetNavMeshBounds(_sample.GetNavMesh()); bminN = bounds[0]; bmaxN = bounds[1]; + hasBound = true; } else if (0 < _sample.GetRecastResults().Count) { @@ -536,31 +539,33 @@ public class RecastDemo : IRecastDemoChannel { if (result.GetSolidHeightfield() != null) { - if (bminN == null) + if (!hasBound) { bminN = RcVec3f.Of(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); bmaxN = RcVec3f.Of(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); } bminN = RcVec3f.Of( - Math.Min(bminN.Value.x, result.GetSolidHeightfield().bmin.x), - Math.Min(bminN.Value.y, result.GetSolidHeightfield().bmin.y), - Math.Min(bminN.Value.z, result.GetSolidHeightfield().bmin.z) + Math.Min(bminN.x, result.GetSolidHeightfield().bmin.x), + Math.Min(bminN.y, result.GetSolidHeightfield().bmin.y), + Math.Min(bminN.z, result.GetSolidHeightfield().bmin.z) ); bmaxN = RcVec3f.Of( - Math.Max(bmaxN.Value.x, result.GetSolidHeightfield().bmax.x), - Math.Max(bmaxN.Value.y, result.GetSolidHeightfield().bmax.y), - Math.Max(bmaxN.Value.z, result.GetSolidHeightfield().bmax.z) + Math.Max(bmaxN.x, result.GetSolidHeightfield().bmax.x), + Math.Max(bmaxN.y, result.GetSolidHeightfield().bmax.y), + Math.Max(bmaxN.z, result.GetSolidHeightfield().bmax.z) ); + + hasBound = true; } } } - if (bminN != null && bmaxN != null) + if (hasBound) { - RcVec3f bmin = bminN.Value; - RcVec3f bmax = bmaxN.Value; + RcVec3f bmin = bminN; + RcVec3f bmax = bmaxN; camr = (float)(Math.Sqrt( Sqr(bmax.x - bmin.x) + Sqr(bmax.y - bmin.y) + Sqr(bmax.z - bmin.z)) diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs index 3bb7fd2..e1c87f3 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs @@ -102,23 +102,24 @@ public class CrowdProfilingTool } } - RcVec3f? pos = null; + var status = DtStatus.DT_FAILURE; + var randomPt = RcVec3f.Zero; switch (type) { case CrowdAgentType.MOB: - pos = GetMobPosition(navquery, filter); + status = GetMobPosition(navquery, filter, out randomPt); break; case CrowdAgentType.VILLAGER: - pos = GetVillagerPosition(navquery, filter); + status = GetVillagerPosition(navquery, filter, out randomPt); break; case CrowdAgentType.TRAVELLER: - pos = GetVillagerPosition(navquery, filter); + status = GetVillagerPosition(navquery, filter, out randomPt); break; } - if (pos != null) + if (status.Succeeded()) { - AddAgent(pos.Value, type); + AddAgent(randomPt, type); } } } @@ -142,31 +143,21 @@ public class CrowdProfilingTool } } - private RcVec3f? GetMobPosition(DtNavMeshQuery navquery, IDtQueryFilter filter) + private DtStatus GetMobPosition(DtNavMeshQuery navquery, IDtQueryFilter filter, out RcVec3f randomPt) { - var status = navquery.FindRandomPoint(filter, rnd, out var randomRef, out var randomPt); - if (status.Succeeded()) - { - return randomPt; - } - - return null; + return navquery.FindRandomPoint(filter, rnd, out var randomRef, out randomPt); } - private RcVec3f? GetVillagerPosition(DtNavMeshQuery navquery, IDtQueryFilter filter) + private DtStatus GetVillagerPosition(DtNavMeshQuery navquery, IDtQueryFilter filter, out RcVec3f randomPt) { - if (0 < _polyPoints.Count) - { - int zone = (int)(rnd.Next() * _polyPoints.Count); - var status = navquery.FindRandomPointWithinCircle(_polyPoints[zone].refs, _polyPoints[zone].pt, zoneRadius, filter, rnd, - out var randomRef, out var randomPt); - if (status.Succeeded()) - { - return randomPt; - } - } + randomPt = RcVec3f.Zero; - return null; + if (0 >= _polyPoints.Count) + return DtStatus.DT_FAILURE; + + int zone = (int)(rnd.Next() * _polyPoints.Count); + return navquery.FindRandomPointWithinCircle(_polyPoints[zone].refs, _polyPoints[zone].pt, zoneRadius, filter, rnd, + out var randomRef, out randomPt); } private void CreateZones()