From 99b6c3306efd7ca8f204d1e1b39311ca5b783386 Mon Sep 17 00:00:00 2001
From: Josue Zamudio <55956919+jozamudi@users.noreply.github.com>
Date: Sun, 25 Jun 2023 12:50:50 -0700
Subject: [PATCH 01/36] Removed fixupCorridor and replaced with
 dtMergeCorridorStartMoved (#637)

* removed fixupCorridor and replaced function calls with dtMergeCorridorStartMoved

* removed fixupCorridor and replaced function calls with dtMergeCorridorStartMoved in NavMeshTesterTool.cpp

Fix #634
---
 RecastDemo/Source/NavMeshTesterTool.cpp | 52 ++-----------------------
 1 file changed, 4 insertions(+), 48 deletions(-)

diff --git a/RecastDemo/Source/NavMeshTesterTool.cpp b/RecastDemo/Source/NavMeshTesterTool.cpp
index 9f81c9f..cff9def 100644
--- a/RecastDemo/Source/NavMeshTesterTool.cpp
+++ b/RecastDemo/Source/NavMeshTesterTool.cpp
@@ -36,6 +36,7 @@
 #include "DetourNavMeshBuilder.h"
 #include "DetourDebugDraw.h"
 #include "DetourCommon.h"
+#include "DetourPathCorridor.h"
 
 #ifdef WIN32
 #	define snprintf _snprintf
@@ -59,52 +60,6 @@ inline bool inRange(const float* v1, const float* v2, const float r, const float
 	return (dx*dx + dz*dz) < r*r && fabsf(dy) < h;
 }
 
-
-static int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath,
-						 const dtPolyRef* visited, const int nvisited)
-{
-	int furthestPath = -1;
-	int furthestVisited = -1;
-	
-	// Find furthest common polygon.
-	for (int i = npath-1; i >= 0; --i)
-	{
-		bool found = false;
-		for (int j = nvisited-1; j >= 0; --j)
-		{
-			if (path[i] == visited[j])
-			{
-				furthestPath = i;
-				furthestVisited = j;
-				found = true;
-			}
-		}
-		if (found)
-			break;
-	}
-
-	// If no intersection found just return current path. 
-	if (furthestPath == -1 || furthestVisited == -1)
-		return npath;
-	
-	// Concatenate paths.	
-
-	// Adjust beginning of the buffer to include the visited.
-	const int req = nvisited - furthestVisited;
-	const int orig = rcMin(furthestPath+1, npath);
-	int size = rcMax(0, npath-orig);
-	if (req+size > maxPath)
-		size = maxPath-req;
-	if (size)
-		memmove(path+req, path+orig, size*sizeof(dtPolyRef));
-	
-	// Store visited
-	for (int i = 0; i < req; ++i)
-		path[i] = visited[(nvisited-1)-i];				
-	
-	return req+size;
-}
-
 // This function checks if the path has a small U-turn, that is,
 // a polygon further in the path is adjacent to the first polygon
 // in the path. If that happens, a shortcut is taken.
@@ -563,7 +518,8 @@ void NavMeshTesterTool::handleToggle()
 	int nvisited = 0;
 	m_navQuery->moveAlongSurface(m_pathIterPolys[0], m_iterPos, moveTgt, &m_filter,
 								 result, visited, &nvisited, 16);
-	m_pathIterPolyCount = fixupCorridor(m_pathIterPolys, m_pathIterPolyCount, MAX_POLYS, visited, nvisited);
+	m_pathIterPolyCount = dtMergeCorridorStartMoved(m_pathIterPolys, m_pathIterPolyCount, 
+								 MAX_POLYS, visited, nvisited);
 	m_pathIterPolyCount = fixupShortcuts(m_pathIterPolys, m_pathIterPolyCount, m_navQuery);
 
 	float h = 0;
@@ -762,7 +718,7 @@ void NavMeshTesterTool::recalc()
 					m_navQuery->moveAlongSurface(polys[0], iterPos, moveTgt, &m_filter,
 												 result, visited, &nvisited, 16);
 
-					npolys = fixupCorridor(polys, npolys, MAX_POLYS, visited, nvisited);
+					npolys = dtMergeCorridorStartMoved(polys, npolys, MAX_POLYS, visited, nvisited);
 					npolys = fixupShortcuts(polys, npolys, m_navQuery);
 
 					float h = 0;
-- 
2.43.0

