From 89e8560cd288aaf949252783ee025f9ad0cc515b Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 24 Jun 2022 20:18:27 +0200 Subject: [PATCH] make max iterations for astar configurable --- src/include/pathfinder.h | 2 ++ src/pathfinder/astar.cpp | 11 +++++------ src/pathfinder/script_pathfinder.cpp | 9 +++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/include/pathfinder.h b/src/include/pathfinder.h index 1bdcb86e9..16843c177 100644 --- a/src/include/pathfinder.h +++ b/src/include/pathfinder.h @@ -199,6 +199,8 @@ extern int AStarMovingUnitCrossingCost; extern bool AStarKnowUnseenTerrain; /// Cost of using a square we haven't seen before. extern int AStarUnknownTerrainCost; +/// Maximum number of iterations of A* before giving up. +extern int AStarMaxSearchIterations; // // Convert heading into direction. diff --git a/src/pathfinder/astar.cpp b/src/pathfinder/astar.cpp index 2cfc3fc6f..90a71b77c 100644 --- a/src/pathfinder/astar.cpp +++ b/src/pathfinder/astar.cpp @@ -96,6 +96,7 @@ static int AStarMatrixSize; /// see pathfinder.h int AStarFixedUnitCrossingCost;// = MaxMapWidth * MaxMapHeight; int AStarMovingUnitCrossingCost = 5; +int AStarMaxSearchIterations = INT_MAX; bool AStarKnowUnseenTerrain = false; int AStarUnknownTerrainCost = 2; /// Used to temporary make enemy units unpassable (needs for correct path lenght calculating for automatic targeting alorithm) @@ -935,6 +936,8 @@ int AStarFindPath(const Vec2i &startPos, const Vec2i &goalPos, int gw, int gh, } Vec2i endPos; + int counter = AStarMaxSearchIterations; + // Begin search while (1) { // Find the best node of from the open set @@ -952,17 +955,13 @@ int AStarFindPath(const Vec2i &startPos, const Vec2i &goalPos, int gw, int gh, break; } -#if 0 // If we have looked too long, then exit. if (!counter--) { - // FIXME: Select a "good" point from the open set. - // Nearest point to goal. + // TODO: Select a "good" point from the open set. AstarDebugPrint("way too long\n"); - ret = PF_FAILED; ProfileEnd("AStarFindPath"); - return ret; + return PF_UNREACHABLE; } -#endif // Generate successors of this node. diff --git a/src/pathfinder/script_pathfinder.cpp b/src/pathfinder/script_pathfinder.cpp index 316a32191..6e19e5c35 100644 --- a/src/pathfinder/script_pathfinder.cpp +++ b/src/pathfinder/script_pathfinder.cpp @@ -93,6 +93,15 @@ static int CclAStar(lua_State *l) } else { AStarUnknownTerrainCost = i; } + } else if (!strcmp(value, "max-search-iterations")) { + ++j; + i = LuaToNumber(l, j + 1); + if (i <= 0) { + PrintFunction(); + fprintf(stdout, "Max A* search iterations must be strictly > 0\n"); + } else { + AStarMaxSearchIterations = i; + } } else { LuaError(l, "Unsupported tag: %s" _C_ value); }