Refactor player waypoint code

- Condense K_GetPlayerNextWaypoint and
  K_UpdateDistanceFromFinishLine into slightly smaller
  functions
- Rename K_GetPlayerNextWaypoint to
  K_SetPlayerNextWaypoint
- Add K_UpdatePlayerWaypoints
This commit is contained in:
James R 2024-02-28 18:06:32 -08:00
parent 9d9dac6e96
commit 3179669ce7
2 changed files with 66 additions and 56 deletions

View file

@ -9422,26 +9422,27 @@ void K_KartPlayerAfterThink(player_t *player)
}
/*--------------------------------------------------
static waypoint_t *K_GetPlayerNextWaypoint(player_t *player)
static boolean K_SetPlayerNextWaypoint(player_t *player)
Gets the next waypoint of a player, by finding their closest waypoint, then checking which of itself and next or
Sets the next waypoint of a player, by finding their closest waypoint, then checking which of itself and next or
previous waypoints are infront of the player.
Also sets the current waypoint.
Input Arguments:-
player - The player the next waypoint is being found for
Return:-
The waypoint that is the player's next waypoint
Whether it is safe to update the respawn waypoint
--------------------------------------------------*/
static waypoint_t *K_GetPlayerNextWaypoint(player_t *player)
static boolean K_SetPlayerNextWaypoint(player_t *player)
{
waypoint_t *finishline = K_GetFinishLineWaypoint();
waypoint_t *bestwaypoint = NULL;
boolean updaterespawn = false;
if ((player != NULL) && (player->mo != NULL) && (P_MobjWasRemoved(player->mo) == false))
{
waypoint_t *waypoint = K_GetBestWaypointForMobj(player->mo, player->currentwaypoint);
boolean updaterespawn = false;
// Our current waypoint.
bestwaypoint = waypoint;
@ -9630,25 +9631,19 @@ static waypoint_t *K_GetPlayerNextWaypoint(player_t *player)
player->pflags &= ~PF_UPDATEMYRESPAWN;
}
// Respawn point should only be updated when we're going to a nextwaypoint
if ((updaterespawn) &&
(player->respawn.state == RESPAWNST_NONE) &&
(bestwaypoint != NULL) &&
(bestwaypoint != player->nextwaypoint) &&
(K_GetWaypointIsSpawnpoint(bestwaypoint)) &&
(K_GetWaypointIsEnabled(bestwaypoint) == true))
// If nextwaypoint is NULL, it means we don't want to update the waypoint until we touch another one.
// player->nextwaypoint will keep its previous value in this case.
if (bestwaypoint != NULL)
{
player->respawn.wp = bestwaypoint;
player->lastsafelap = player->laps;
player->lastsafecheatcheck = player->cheatchecknum;
player->nextwaypoint = bestwaypoint;
}
}
return bestwaypoint;
return updaterespawn;
}
/*--------------------------------------------------
void K_UpdateDistanceFromFinishLine(player_t *const player)
static void K_UpdateDistanceFromFinishLine(player_t *const player)
Updates the distance a player has to the finish line.
@ -9658,46 +9653,11 @@ static waypoint_t *K_GetPlayerNextWaypoint(player_t *player)
Return:-
None
--------------------------------------------------*/
void K_UpdateDistanceFromFinishLine(player_t *const player)
static void K_UpdateDistanceFromFinishLine(player_t *const player)
{
if (K_PodiumSequence() == true)
{
K_UpdatePodiumWaypoints(player);
return;
}
if ((player != NULL) && (player->mo != NULL))
{
waypoint_t *finishline = K_GetFinishLineWaypoint();
waypoint_t *nextwaypoint = NULL;
if (player->respawn.state == RESPAWNST_MOVE &&
player->respawn.init == true &&
player->lastsafelap < player->laps)
{
player->laps = player->lastsafelap;
player->cheatchecknum = player->lastsafecheatcheck;
}
if (player->spectator)
{
// Don't update waypoints while spectating
nextwaypoint = finishline;
}
else
{
nextwaypoint = K_GetPlayerNextWaypoint(player);
}
if (nextwaypoint != NULL)
{
// If nextwaypoint is NULL, it means we don't want to update the waypoint until we touch another one.
// player->nextwaypoint will keep its previous value in this case.
player->nextwaypoint = nextwaypoint;
}
// Update prev value (used for grief prevention code)
player->distancetofinishprev = player->distancetofinish;
// nextwaypoint is now the waypoint that is in front of us
if ((player->exiting && !(player->pflags & PF_NOCONTEST)) || player->spectator)
@ -9874,6 +9834,40 @@ void K_UpdateDistanceFromFinishLine(player_t *const player)
}
}
/*--------------------------------------------------
static void K_UpdatePlayerWaypoints(player_t *const player)
Updates the player's waypoints and finish line distance.
Input Arguments:-
player - The player to update
Return:-
None
--------------------------------------------------*/
static void K_UpdatePlayerWaypoints(player_t *const player)
{
waypoint_t *const old_nextwaypoint = player->nextwaypoint;
boolean updaterespawn = K_SetPlayerNextWaypoint(player);
// Update prev value (used for grief prevention code)
player->distancetofinishprev = player->distancetofinish;
K_UpdateDistanceFromFinishLine(player);
// Respawn point should only be updated when we're going to a nextwaypoint
if ((updaterespawn) &&
(player->respawn.state == RESPAWNST_NONE) &&
(player->nextwaypoint != old_nextwaypoint) &&
(K_GetWaypointIsSpawnpoint(player->nextwaypoint)) &&
(K_GetWaypointIsEnabled(player->nextwaypoint) == true))
{
player->respawn.wp = player->nextwaypoint;
player->lastsafelap = player->laps;
player->lastsafecheatcheck = player->cheatchecknum;
}
}
INT32 K_GetKartRingPower(const player_t *player, boolean boosted)
{
fixed_t ringPower = ((9 - player->kartspeed) + (9 - player->kartweight)) * (FRACUNIT/2);
@ -10798,10 +10792,27 @@ void K_UpdateAllPlayerPositions(void)
// First loop: Ensure all players' distance to the finish line are all accurate
for (i = 0; i < MAXPLAYERS; i++)
{
if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo))
player_t *player = &players[i];
if (!playeringame[i] || player->spectator || !player->mo || P_MobjWasRemoved(player->mo))
{
K_UpdateDistanceFromFinishLine(&players[i]);
continue;
}
if (K_PodiumSequence() == true)
{
K_UpdatePodiumWaypoints(player);
continue;
}
if (player->respawn.state == RESPAWNST_MOVE &&
player->respawn.init == true &&
player->lastsafelap < player->laps)
{
player->laps = player->lastsafelap;
player->cheatchecknum = player->lastsafecheatcheck;
}
K_UpdatePlayerWaypoints(player);
}
// Second loop: Ensure all player positions reflect everyone's distances

View file

@ -165,7 +165,6 @@ void K_RepairOrbitChain(mobj_t *orbit);
void K_CalculateBananaSlope(mobj_t *mobj, fixed_t x, fixed_t y, fixed_t z, fixed_t radius, fixed_t height, boolean flip, boolean player);
mobj_t *K_FindJawzTarget(mobj_t *actor, player_t *source, angle_t range);
INT32 K_GetKartRingPower(const player_t *player, boolean boosted);
void K_UpdateDistanceFromFinishLine(player_t *const player);
boolean K_CheckPlayersRespawnColliding(INT32 playernum, fixed_t x, fixed_t y);
INT16 K_UpdateSteeringValue(INT16 inputSteering, INT16 destSteering);
INT16 K_GetKartTurnValue(const player_t *player, INT16 turnvalue);