From c7c6e53d61852a6553e90f7ee684c8e7c13b69dc Mon Sep 17 00:00:00 2001 From: wrenge Date: Wed, 13 Nov 2024 11:32:38 +0300 Subject: [PATCH] Made list in DtPathCorridor.cs readonly --- src/DotRecast.Detour.Crowd/DtPathCorridor.cs | 23 +++++++++++-------- src/DotRecast.Detour/DtPathUtils.cs | 15 +++++++----- .../Tools/RcTestNavMeshTool.cs | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs index 37787ae..d3b04c6 100644 --- a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs +++ b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs @@ -33,7 +33,7 @@ namespace DotRecast.Detour.Crowd private RcVec3f m_pos; private RcVec3f m_target; - private List m_path; + private List m_path = new List(); private int m_npath; private int m_maxPath; @@ -89,7 +89,8 @@ namespace DotRecast.Detour.Crowd /// @return True if the initialization succeeded. public bool Init(int maxPath) { - m_path = new List(maxPath); + if (m_path.Capacity < maxPath) + m_path.Capacity = maxPath; m_npath = 0; m_maxPath = maxPath; return true; @@ -218,7 +219,7 @@ namespace DotRecast.Detour.Crowd { if (res.Count > 1 && t > 0.99f) { - m_npath = DtPathUtils.MergeCorridorStartShortcut(ref m_path, m_npath, m_maxPath, res, res.Count); + m_npath = DtPathUtils.MergeCorridorStartShortcut(m_path, m_npath, m_maxPath, res, res.Count); } } } @@ -250,7 +251,7 @@ namespace DotRecast.Detour.Crowd if (status.Succeeded() && res.Count > 0) { - m_npath = DtPathUtils.MergeCorridorStartShortcut(ref m_path, m_npath, m_maxPath, res, res.Count); + m_npath = DtPathUtils.MergeCorridorStartShortcut(m_path, m_npath, m_maxPath, res, res.Count); return true; } @@ -276,7 +277,7 @@ namespace DotRecast.Detour.Crowd } // Prune path - m_path = m_path.GetRange(npos, m_npath - npos); + m_path.RemoveRange(0, npos); m_npath -= npos; refs[0] = prevRef; @@ -322,7 +323,7 @@ namespace DotRecast.Detour.Crowd var status = navquery.MoveAlongSurface(m_path[0], m_pos, npos, filter, out var result, visited, out var nvisited, MAX_VISITED); if (status.Succeeded()) { - m_npath = DtPathUtils.MergeCorridorStartMoved(ref m_path, m_npath, m_maxPath, visited, nvisited); + m_npath = DtPathUtils.MergeCorridorStartMoved(m_path, m_npath, m_maxPath, visited, nvisited); // Adjust the position to stay on top of the navmesh. m_pos = result; @@ -366,7 +367,7 @@ namespace DotRecast.Detour.Crowd var status = navquery.MoveAlongSurface(m_path[^1], m_target, npos, filter, out var result, visited, out nvisited, MAX_VISITED); if (status.Succeeded()) { - m_npath = DtPathUtils.MergeCorridorEndMoved(ref m_path, m_npath, m_maxPath, visited, nvisited); + m_npath = DtPathUtils.MergeCorridorEndMoved(m_path, m_npath, m_maxPath, visited, nvisited); // TODO: should we do that? // Adjust the position to stay on top of the navmesh. @@ -394,7 +395,11 @@ namespace DotRecast.Detour.Crowd public void SetCorridor(RcVec3f target, List path) { m_target = target; - m_path = new List(path); + if(path != m_path) + { + m_path.Clear(); + m_path.AddRange(path); + } m_npath = path.Count; } @@ -444,7 +449,7 @@ namespace DotRecast.Detour.Crowd else if (n < m_npath) { // The path is partially usable. - m_path = m_path.GetRange(0, n); + m_path.RemoveRange(n, m_path.Count - n); m_npath = n; } diff --git a/src/DotRecast.Detour/DtPathUtils.cs b/src/DotRecast.Detour/DtPathUtils.cs index 24dd8a6..688f2a0 100644 --- a/src/DotRecast.Detour/DtPathUtils.cs +++ b/src/DotRecast.Detour/DtPathUtils.cs @@ -148,7 +148,7 @@ namespace DotRecast.Detour return npath; } - public static int MergeCorridorStartMoved(ref List path, int npath, int maxPath, Span visited, int nvisited) + public static int MergeCorridorStartMoved(List path, int npath, int maxPath, Span visited, int nvisited) { int furthestPath = -1; int furthestVisited = -1; @@ -191,11 +191,12 @@ namespace DotRecast.Detour result.AddRange(path.GetRange(furthestPath, npath - furthestPath)); - path = result; + path.Clear(); + path.AddRange(result); return result.Count; } - public static int MergeCorridorEndMoved(ref List path, int npath, int maxPath, Span visited, int nvisited) + public static int MergeCorridorEndMoved(List path, int npath, int maxPath, Span visited, int nvisited) { int furthestPath = -1; int furthestVisited = -1; @@ -233,11 +234,12 @@ namespace DotRecast.Detour result.Add(v); } - path = result; + path.Clear(); + path.AddRange(path); return result.Count; } - public static int MergeCorridorStartShortcut(ref List path, int npath, int maxPath, List visited, int nvisited) + public static int MergeCorridorStartShortcut(List path, int npath, int maxPath, List visited, int nvisited) { int furthestPath = -1; int furthestVisited = -1; @@ -274,7 +276,8 @@ namespace DotRecast.Detour List result = visited.GetRange(0, furthestVisited); result.AddRange(path.GetRange(furthestPath, npath - furthestPath)); - path = result; + path.Clear(); + path.AddRange(result); return result.Count; } } diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs index 4199c74..256a1df 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs @@ -98,7 +98,7 @@ namespace DotRecast.Recast.Toolset.Tools iterPos = result; - pathIterPolyCount = DtPathUtils.MergeCorridorStartMoved(ref pathIterPolys, pathIterPolyCount, MAX_POLYS, visited, nvisited); + pathIterPolyCount = DtPathUtils.MergeCorridorStartMoved(pathIterPolys, pathIterPolyCount, MAX_POLYS, visited, nvisited); pathIterPolyCount = DtPathUtils.FixupShortcuts(ref pathIterPolys, pathIterPolyCount, navQuery); var status = navQuery.GetPolyHeight(pathIterPolys[0], result, out var h);