From b672ffa8ba598df341585ac02a7e5eae3dc3fe90 Mon Sep 17 00:00:00 2001 From: James R Date: Sun, 14 Feb 2021 22:55:03 -0800 Subject: [PATCH] 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. --- src/k_waypoint.c | 58 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/k_waypoint.c b/src/k_waypoint.c index 39e9efa5e..4150ff333 100644 --- a/src/k_waypoint.c +++ b/src/k_waypoint.c @@ -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) {