From 2e4b1c6ecbb76b9509de0b0cdcf955a8e80a6804 Mon Sep 17 00:00:00 2001 From: Sally Cochenour Date: Sun, 29 Mar 2020 17:58:17 -0400 Subject: [PATCH] Help reduce waypoint flickering by using closest waypoint to finish line --- src/k_waypoint.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/k_waypoint.c b/src/k_waypoint.c index f77e6d62f..91cb9a1b1 100644 --- a/src/k_waypoint.c +++ b/src/k_waypoint.c @@ -270,9 +270,12 @@ waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj) waypoint_t *checkwaypoint = NULL; fixed_t closestdist = INT32_MAX; fixed_t checkdist = INT32_MAX; + fixed_t bestfindist = INT32_MAX; for (i = 0; i < numwaypoints; i++) { + fixed_t rad; + checkwaypoint = &waypointheap[i]; checkdist = P_AproxDistance( @@ -280,7 +283,34 @@ waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj) (mobj->y / FRACUNIT) - (checkwaypoint->mobj->y / FRACUNIT)); checkdist = P_AproxDistance(checkdist, ((mobj->z / FRACUNIT) - (checkwaypoint->mobj->z / FRACUNIT)) * 4); - if (checkdist < closestdist) + rad = (checkwaypoint->mobj->radius / FRACUNIT); + + 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); + + if (pathfindsuccess == true) + { + if ((INT32)(pathtofinish.totaldist) < bestfindist) + { + bestwaypoint = checkwaypoint; + bestfindist = pathtofinish.totaldist; + } + + Z_Free(pathtofinish.array); + } + } + else if (checkdist < closestdist && bestfindist == INT32_MAX) { if (!P_CheckSight(mobj, checkwaypoint->mobj)) {