From 9df43cc3d03cfa3e7db620ce31ddb663ae04655f Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Wed, 16 Feb 2022 08:44:22 +0100
Subject: [PATCH] Revert "Revert "improve astar cleanup performance. thanks
 @Andrettin""

This reverts commit 740701089f492c74cfdedda8a9f43a5e46ef6bfc.
---
 src/pathfinder/astar.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/pathfinder/astar.cpp b/src/pathfinder/astar.cpp
index 7932fdcea..996bbca12 100644
--- a/src/pathfinder/astar.cpp
+++ b/src/pathfinder/astar.cpp
@@ -263,6 +263,7 @@ void InitAStar(int mapWidth, int mapHeight)
 	memset(AStarMatrix, 0, AStarMatrixSize);
 
 	Threshold = AStarMapWidth * AStarMapHeight / MAX_CLOSE_SET_RATIO;
+	CloseSetSize = Threshold;
 	CloseSet = new int[Threshold];
 
 	OpenSetMaxSize = AStarMapWidth * AStarMapHeight / MAX_OPEN_SET_RATIO;
@@ -307,16 +308,19 @@ static void AStarPrepare()
 /**
 **  Clean up A*
 */
+static void CostMoveToCacheCleanUp();
 static void AStarCleanUp()
 {
 	ProfileBegin("AStarCleanUp");
 
 	if (CloseSetSize >= Threshold) {
 		AStarPrepare();
+		CostMoveToCacheCleanUp();
 	} else {
 		for (int i = 0; i < CloseSetSize; ++i) {
 			AStarMatrix[CloseSet[i]].CostFromStart = 0;
 			AStarMatrix[CloseSet[i]].InGoal = 0;
+			CostMoveToCache[CloseSet[i]] = CacheNotSet;
 		}
 	}
 	ProfileEnd("AStarCleanUp");
@@ -936,7 +940,6 @@ int AStarFindPath(const Vec2i &startPos, const Vec2i &goalPos, int gw, int gh,
 
 	//  Initialize
 	AStarCleanUp();
-	CostMoveToCacheCleanUp();
 
 	OpenSetSize = 0;
 	CloseSetSize = 0;