make max iterations for astar configurable

This commit is contained in:
Tim Felgentreff 2022-06-24 20:18:27 +02:00
parent 8d35af98b2
commit 89e8560cd2
3 changed files with 16 additions and 6 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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);
}