Check previously considered best waypoint when pathfinding to solve overlapping waypoints

Previously could skip the waypoint that was actually closer and assume the
further is the best.
This commit is contained in:
James R 2021-02-14 22:55:03 -08:00
parent 4993d2ce39
commit b672ffa8ba

View file

@ -253,6 +253,40 @@ waypoint_t *K_GetClosestWaypointToMobj(mobj_t *const mobj)
return closestwaypoint;
}
/*--------------------------------------------------
static void K_CompareOverlappingWaypoint
( waypoint_t *const checkwaypoint,
waypoint_t **const bestwaypoint,
fixed_t *const bestfindist)
Solves touching overlapping waypoint radiuses by sorting by distance to
finish line.
--------------------------------------------------*/
static void K_CompareOverlappingWaypoint
( waypoint_t *const checkwaypoint,
waypoint_t **const bestwaypoint,
fixed_t *const bestfindist)
{
const boolean useshortcuts = false;
const boolean huntbackwards = false;
boolean pathfindsuccess = false;
path_t pathtofinish = {};
pathfindsuccess =
K_PathfindToWaypoint(checkwaypoint, finishline, &pathtofinish, useshortcuts, huntbackwards);
if (pathfindsuccess == true)
{
if ((INT32)(pathtofinish.totaldist) < *bestfindist)
{
*bestwaypoint = checkwaypoint;
*bestfindist = pathtofinish.totaldist;
}
Z_Free(pathtofinish.array);
}
}
/*--------------------------------------------------
waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj)
@ -292,30 +326,18 @@ waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj)
rad = (checkwaypoint->mobj->radius / FRACUNIT);
if (closestdist < rad && checkdist < rad && finishline != NULL)
// remember: huge radius
if (closestdist <= rad && checkdist <= rad && finishline != NULL)
{
const boolean useshortcuts = false;
const boolean huntbackwards = false;
boolean pathfindsuccess = false;
path_t pathtofinish = {};
// If the mobj is touching multiple waypoints at once,
// then solve ties by taking the one closest to the finish line.
// Prevents position from flickering wildly when taking turns.
pathfindsuccess =
K_PathfindToWaypoint(checkwaypoint, finishline, &pathtofinish, useshortcuts, huntbackwards);
// For the first couple overlapping, check the previous best too.
if (bestfindist == INT32_MAX)
K_CompareOverlappingWaypoint(bestwaypoint, &bestwaypoint, &bestfindist);
if (pathfindsuccess == true)
{
if ((INT32)(pathtofinish.totaldist) < bestfindist)
{
bestwaypoint = checkwaypoint;
bestfindist = pathtofinish.totaldist;
}
Z_Free(pathtofinish.array);
}
K_CompareOverlappingWaypoint(checkwaypoint, &bestwaypoint, &bestfindist);
}
else if (checkdist < closestdist && bestfindist == INT32_MAX)
{