From 721fb369fd5920646b363a4890271f1aadebe7a9 Mon Sep 17 00:00:00 2001 From: Sally Cochenour Date: Tue, 3 Mar 2020 18:34:17 -0500 Subject: [PATCH] Move changes to K_GetBestWaypointForMobj so that K_GetClosestWaypointToMobj can stay the same --- src/k_kart.c | 2 +- src/k_waypoint.c | 85 +++++++++++++++++++++++------------------------- src/k_waypoint.h | 10 +++--- src/p_enemy.c | 5 ++- 4 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/k_kart.c b/src/k_kart.c index dd86fe201..0cb8b9d20 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -5749,7 +5749,7 @@ static waypoint_t *K_GetPlayerNextWaypoint(player_t *player) if ((player != NULL) && (player->mo != NULL) && (P_MobjWasRemoved(player->mo) == false)) { - waypoint_t *waypoint = K_GetClosestWaypointToMobj(player->mo); + waypoint_t *waypoint = K_GetBestWaypointForMobj(player->mo); boolean updaterespawn = false; bestwaypoint = waypoint; diff --git a/src/k_waypoint.c b/src/k_waypoint.c index 8d822c3d6..dfa868204 100644 --- a/src/k_waypoint.c +++ b/src/k_waypoint.c @@ -222,8 +222,46 @@ waypoint_t *K_GetClosestWaypointToMobj(mobj_t *const mobj) { checkwaypoint = &waypointheap[i]; - // TODO: Keep the old version of this function, - // make the vertical axis faking & sight checks a separate function. + checkdist = P_AproxDistance( + (mobj->x >> FRACBITS) - (checkwaypoint->mobj->x >> FRACBITS), + (mobj->y >> FRACBITS) - (checkwaypoint->mobj->y >> FRACBITS)); + checkdist = P_AproxDistance(checkdist, (mobj->z >> FRACBITS) - (checkwaypoint->mobj->z >> FRACBITS)); + + if (checkdist < closestdist) + { + closestwaypoint = checkwaypoint; + closestdist = checkdist; + } + } + } + + return closestwaypoint; +} + +/*-------------------------------------------------- + waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj) + + See header file for description. +--------------------------------------------------*/ +waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj) +{ + waypoint_t *bestwaypoint = NULL; + + if ((mobj == NULL) || P_MobjWasRemoved(mobj)) + { + CONS_Debug(DBG_GAMELOGIC, "NULL mobj in K_GetBestWaypointForMobj.\n"); + } + else + { + size_t i = 0U; + waypoint_t *checkwaypoint = NULL; + fixed_t closestdist = INT32_MAX; + fixed_t checkdist = INT32_MAX; + + for (i = 0; i < numwaypoints; i++) + { + checkwaypoint = &waypointheap[i]; + checkdist = P_AproxDistance( (mobj->x >> FRACBITS) - (checkwaypoint->mobj->x >> FRACBITS), (mobj->y >> FRACBITS) - (checkwaypoint->mobj->y >> FRACBITS)); @@ -237,49 +275,8 @@ waypoint_t *K_GetClosestWaypointToMobj(mobj_t *const mobj) continue; } - closestwaypoint = checkwaypoint; - closestdist = checkdist; - } - } - } - - return closestwaypoint; -} - -/*-------------------------------------------------- - waypoint_t *K_GetBestWaypointTouchingMobj(mobj_t *const mobj) - - See header file for description. ---------------------------------------------------*/ -waypoint_t *K_GetBestWaypointTouchingMobj(mobj_t *const mobj) -{ - waypoint_t *bestwaypoint = NULL; - - if ((mobj == NULL) || P_MobjWasRemoved(mobj)) - { - CONS_Debug(DBG_GAMELOGIC, "NULL mobj in K_GetBestWaypointTouchingMobj.\n"); - } - else - { - size_t i = 0U; - waypoint_t *checkwaypoint = NULL; - fixed_t bestdist = INT32_MAX; - fixed_t checkdist = INT32_MAX; - - for (i = 0; i < numwaypoints; i++) - { - checkwaypoint = &waypointheap[i]; - checkdist = P_AproxDistance( - (mobj->x >> FRACBITS) - (checkwaypoint->mobj->x >> FRACBITS), - (mobj->y >> FRACBITS) - (checkwaypoint->mobj->y >> FRACBITS)); - checkdist = P_AproxDistance(checkdist, (mobj->z >> FRACBITS) - (checkwaypoint->mobj->z >> FRACBITS)); - - // The mobj has to be touching this waypoint to use it. - if ((checkdist <= (checkwaypoint->mobj->radius >> FRACBITS)) - && (checkdist < bestdist)) - { bestwaypoint = checkwaypoint; - bestdist = checkdist; + closestdist = checkdist; } } } diff --git a/src/k_waypoint.h b/src/k_waypoint.h index af4ac71a5..058ff6882 100644 --- a/src/k_waypoint.h +++ b/src/k_waypoint.h @@ -152,17 +152,19 @@ waypoint_t *K_GetClosestWaypointToMobj(mobj_t *const mobj); /*-------------------------------------------------- - waypoint_t *K_GetBestWaypointTouchingMobj(mobj_t *const mobj) + waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj) - Returns the waypoint closest to the finish line that an mobj is touching + Similar to K_GetClosestWaypointToMobj, but prioritizes horizontal distance over vertical distance, and + sight checks to ensure that the waypoint and mobj are the in same area. Can potentially return NULL if + there are no visible waypoints. Input Arguments:- mobj - mobj to get the waypoint for. Return:- - The best waypoint for the mobj + The best waypoint for the mobj, or NULL if there were no matches --------------------------------------------------*/ -waypoint_t *K_GetBestWaypointTouchingMobj(mobj_t *const mobj); +waypoint_t *K_GetBestWaypointForMobj(mobj_t *const mobj); /*-------------------------------------------------- diff --git a/src/p_enemy.c b/src/p_enemy.c index 6d313f566..69058db23 100644 --- a/src/p_enemy.c +++ b/src/p_enemy.c @@ -8741,7 +8741,7 @@ void A_SPBChase(mobj_t *actor) { // Previously set nextwaypoint lastwaypoint = K_GetWaypointFromIndex((size_t)actor->cusval); - tempwaypoint = K_GetBestWaypointTouchingMobj(actor); + tempwaypoint = K_GetBestWaypointForMobj(actor); // check if the tempwaypoint corresponds to lastwaypoint's next ID at least; // This is to avoid situations where the SPB decides to suicide jump down a bridge because it found a COMPLETELY unrelated waypoint down there. @@ -8752,7 +8752,7 @@ void A_SPBChase(mobj_t *actor) bestwaypoint = K_GetWaypointFromIndex((size_t)actor->extravalue2); // keep going from the PREVIOUS wp. } else - bestwaypoint = K_GetBestWaypointTouchingMobj(actor); + bestwaypoint = K_GetBestWaypointForMobj(actor); if (bestwaypoint == NULL && lastwaypoint == NULL) { @@ -8781,7 +8781,6 @@ void A_SPBChase(mobj_t *actor) nextwaypoint = lastwaypoint; } - if (nextwaypoint != NULL) { const fixed_t xywaypointdist = P_AproxDistance(