From 21c267b319f308d56c8e97610bbdd0d612f34e38 Mon Sep 17 00:00:00 2001 From: Anonimus Date: Sat, 8 Mar 2025 17:36:50 -0500 Subject: [PATCH] Refactor to prevent desynchs --- src/d_player.h | 4 + src/k_kart.c | 34 +--- src/p_mobj.c | 3 + src/p_setup.c | 3 + src/p_user.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 428 insertions(+), 29 deletions(-) diff --git a/src/d_player.h b/src/d_player.h index 9924a1807..4c2cd7268 100644 --- a/src/d_player.h +++ b/src/d_player.h @@ -551,10 +551,14 @@ typedef struct player_s UINT8 splitscreenindex; +<<<<<<< HEAD <<<<<<< HEAD tic_t driftsparkGrowTimer; ======= +======= + UINT8 doiconspin; // A boolean in practice. Tells the game to continue spinning the minimap icons. +>>>>>>> 432dcd3a3 (Refactor to prevent desynchs) fixed_t spinoutrot; // When a player spins out, this value increments modulus 360. #ifdef HWRENDER >>>>>>> 052dcb263 (Add a cvar for minimap icon spinouts) diff --git a/src/k_kart.c b/src/k_kart.c index 800dc9d75..b4eb022e9 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -5397,6 +5397,7 @@ void K_KartPlayerHUDUpdate(player_t *player) player->kartstuff[k_cardanimation] = 0; } +<<<<<<< HEAD <<<<<<< HEAD static boolean K_SpeedLinesShouldBlend(player_t *player) { @@ -5478,6 +5479,8 @@ static inline void K_SpawnNormalSpeedLines(player_t *player, boolean synched) #define MAXSPINROT (360 * FRACUNIT) >>>>>>> 052dcb263 (Add a cvar for minimap icon spinouts) +======= +>>>>>>> 432dcd3a3 (Refactor to prevent desynchs) /** \brief Decreases various kart timers and powers per frame. Called in P_PlayerThink in p_user.c \param player player object passed from P_PlayerThink @@ -5600,9 +5603,7 @@ void K_KartPlayerThink(player_t *player, ticcmd_t *cmd) player->powers[pw_flashing]--; } - boolean doiconspin; - - doiconspin = false; + player->doiconspin = 0; if (player->kartstuff[k_spinouttimer]) { @@ -5616,7 +5617,7 @@ void K_KartPlayerThink(player_t *player, ticcmd_t *cmd) player->kartstuff[k_spinouttype] = 0; // Reset type } - doiconspin = true; + player->doiconspin = 1; } else { @@ -5633,31 +5634,6 @@ void K_KartPlayerThink(player_t *player, ticcmd_t *cmd) } } - if ((doiconspin) && (player->spinoutrot == 0)) - { - player->spinoutrot = SPINOUTROTSPEED; - } - - // MKWii-styled icon spinouts: do a full 360 rotation before stopping. - if ((player->spinoutrot) && (player->spinoutrot < MAXSPINROT)) - { - if ((player->spinoutrot + SPINOUTROTSPEED) >= MAXSPINROT) - { - if (doiconspin) - { - player->spinoutrot = (player->spinoutrot + SPINOUTROTSPEED) % MAXSPINROT; - } - else - { - player->spinoutrot = 0; - } - } - else - { - player->spinoutrot += SPINOUTROTSPEED; - } - } - /*if (player->kartstuff[k_thunderanim]) player->kartstuff[k_thunderanim]--;*/ diff --git a/src/p_mobj.c b/src/p_mobj.c index d22ad3bb8..007d59836 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -10728,6 +10728,9 @@ void P_SpawnPlayer(INT32 playernum) p->hitemtimer = 0; p->hitemvictim = 255; + p->doiconspin = 0; + p->spinoutrot = 0; + if (G_BattleGametype()) // SRB2kart { mobj_t *overheadarrow = P_SpawnMobj(mobj->x, mobj->y, mobj->z + P_GetPlayerHeight(p)+16*FRACUNIT, MT_PLAYERARROW); diff --git a/src/p_setup.c b/src/p_setup.c index 56ffd4766..44b1e19a3 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2116,6 +2116,7 @@ static void P_LevelInitStuff(boolean reloadinggamestate) players[i].realtime = racecountdown = exitcountdown = 0; +<<<<<<< HEAD <<<<<<< HEAD for (INT32 j = 0; j < LAP__MAX; j++) { @@ -2128,6 +2129,8 @@ static void P_LevelInitStuff(boolean reloadinggamestate) players[i].spinoutrot = 0; >>>>>>> 052dcb263 (Add a cvar for minimap icon spinouts) +======= +>>>>>>> 432dcd3a3 (Refactor to prevent desynchs) players[i].gotcontinue = false; players[i].xtralife = players[i].deadtimer = players[i].numboxes = players[i].totalring = players[i].laps = 0; diff --git a/src/p_user.c b/src/p_user.c index 890fa2e5f..a9ea372ac 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -4430,6 +4430,394 @@ DoABarrelRoll (player_t *player) player->tilt = slope; } +<<<<<<< HEAD +======= +/* set follower state with our weird hacks + the reason we do this is to avoid followers ever using actions (majormods, yikes!) + without having to touch p_mobj.c. + so we give it 1more tic and change the state when tic == 1 instead of 0 + cool beans? + cool beans. +*/ +static void P_SetFollowerState(mobj_t *f, statenum_t state) +{ + if (f == NULL || P_MobjWasRemoved(f) == true) + { + // safety net + return; + } + + // No, do NOT set the follower to S_NULL. Set it to S_INVISIBLE. + if (state == S_NULL) + { + state = S_INVISIBLE; + f->threshold = 1; // Threshold = 1 means stop doing anything related to setting states, so that we don't get out of S_INVISIBLE + } + + // extravalue2 stores the last "first state" we used. + // because states default to idlestates, if we use an animation that uses an "ongoing" state line, don't reset it! + // this prevents it from looking very dumb + if (state == (statenum_t)f->extravalue2) + { + return; + } + + // we will save the state into extravalue2. + f->extravalue2 = state; + + P_SetMobjStateNF(f, state); + if (f->state->tics > 0) + { + f->tics++; + } +} + +UINT8 K_GetEffectiveFollowerColor(UINT8 followercolor, follower_t *follower, UINT8 playercolor, skin_t *playerskin) +{ + if (followercolor == SKINCOLOR_NONE && follower != NULL) // "Default" + { + followercolor = follower->defaultcolor; + } + + if (followercolor > SKINCOLOR_NONE && followercolor < MAXSKINCOLORS) // bog standard + { + return followercolor; + } + + if (playercolor == SKINCOLOR_NONE) // get default color + { + if (playerskin == NULL) + { + // Nothing from this line down is valid if playerskin is invalid, just guess Sonic? + playerskin = &skins[0]; + } + + playercolor = playerskin->prefcolor; + } + + if (followercolor == FOLLOWERCOLOR_OPPOSITE) // "Opposite" + { + return KartColor_Opposite[playercolor]; + } + + // "Match" + return playercolor; +} + +static void K_UpdateFollowerState(mobj_t *f, statenum_t state, followerstate_t type) +{ + if (f == NULL || P_MobjWasRemoved(f) == true) + { + // safety net + return; + } + + if (f->extravalue1 != (INT32)type) + { + P_SetFollowerState(f, state); + f->extravalue1 = type; + } +} + + +// +//P_HandleFollower +// +//Handle the follower's spawning and moving along with the player. Do note that some of the stuff like the removal if a player doesn't exist anymore is handled in MT_FOLLOWER's thinker. +static void P_HandleFollower(player_t *player) +{ + follower_t fl; + angle_t an; + fixed_t zoffs; + fixed_t ourheight; + fixed_t sx, sy, sz, deltaz; + fixed_t fh = INT32_MIN, ch = INT32_MAX; + angle_t destAngle; + INT32 angleDiff; + UINT16 color; + + if (!player->followerready) + return; // we aren't ready to perform anything follower related yet. + + // How about making sure our follower exists and is added before trying to spawn it n' all? + if (player->followerskin > numfollowers-1 || player->followerskin < -1) + { + //CONS_Printf("Follower skin invlaid. Setting to -1.\n"); + player->followerskin = -1; + return; + } + + // don't do anything if we can't have a follower to begin with. + // (It gets removed under those conditions) + if (player->spectator || player->followerskin < 0 + || player->mo == NULL || P_MobjWasRemoved(player->mo)) + { + if (player->follower) + { + P_RemoveMobj(player->follower); + P_SetTarget(&player->follower, NULL); + } + return; + } + // Before we do anything, let's be sure of where we're supposed to be + fl = followers[player->followerskin]; + + an = player->mo->angle + fl.atangle; + zoffs = fl.zoffs; + + // do you like angle maths? I certainly don't... + sx = player->mo->x + player->mo->momx + FixedMul(FixedMul(player->mo->scale, fl.dist), FINECOSINE((an) >> ANGLETOFINESHIFT)); + sy = player->mo->y + player->mo->momy + FixedMul(FixedMul(player->mo->scale, fl.dist), FINESINE((an) >> ANGLETOFINESHIFT)); + + // interp info helps with stretchy fix + deltaz = (player->mo->z - player->mo->old_z); + + // for the z coordinate, don't be a doof like Steel and forget that MFE_VERTICALFLIP exists :P + sz = player->mo->z + player->mo->momz + FixedMul(player->mo->scale, zoffs * P_MobjFlip(player->mo)); + ourheight = FixedMul(fl.height, player->mo->scale); + if (player->mo->eflags & MFE_VERTICALFLIP) + { + sz += ourheight; + } + + fh = player->mo->floorz; + ch = player->mo->ceilingz - ourheight; + + switch (fl.mode) + { + case FOLLOWERMODE_GROUND: + { + if (player->mo->eflags & MFE_VERTICALFLIP) + { + sz = ch; + } + else + { + sz = fh; + } + break; + } + case FOLLOWERMODE_FLOAT: + default: + { + // finally, add a cool floating effect to the z height. + // not stolen from k_kart I swear!! + fixed_t sine = FixedMul(fl.bobamp, + FINESINE((( + FixedMul(4 * M_TAU_FIXED, fl.bobspeed) * leveltime + ) >> ANGLETOFINESHIFT) & FINEMASK)); + sz += FixedMul(player->mo->scale, sine) * P_MobjFlip(player->mo); + break; + } + } + + // Set follower colour + + color = K_GetEffectiveFollowerColor(player->followercolor, &fl, player->skincolor, &skins[player->skin]); + + if (!player->follower) // follower doesn't exist / isn't valid + { + //CONS_Printf("Spawning follower...\n"); + + // so let's spawn one! + P_SetTarget(&player->follower, P_SpawnMobj(sx, sy, sz, MT_FOLLOWER)); + if (player->follower == NULL) + return; + + K_UpdateFollowerState(player->follower, fl.idlestate, FOLLOWERSTATE_IDLE); + + P_SetTarget(&player->follower->target, player->mo); // we need that to know when we need to disappear + player->follower->angle = player->follower->old_angle = player->mo->angle; + } + else // follower exists, woo! + { + + // first of all, handle states following the same model as above: + if (player->follower->tics == 1) + P_SetFollowerState(player->follower, player->follower->state->nextstate); + + // Spawn Follower Shadow + player->follower->haveshadow = true; + player->follower->shadowscale = FRACUNIT; + + // move the follower next to us (yes, this is really basic maths but it looks pretty damn clean in practice)! + player->follower->momx = FixedDiv(sx - player->follower->x, fl.horzlag); + player->follower->momy = FixedDiv(sy - player->follower->y, fl.horzlag); + + player->follower->z += FixedDiv(deltaz, fl.vertlag); + + if (fl.mode == FOLLOWERMODE_GROUND) + { + sector_t *sec = player->follower->subsector->sector; + + fh = min(fh, P_GetFloorZ(player->follower, sec, sx, sy, NULL)); + ch = max(ch, P_GetCeilingZ(player->follower, sec, sx, sy, NULL) - ourheight); + + if (P_IsObjectOnGround(player->mo) == false) + { + // In the air, match their momentum. + player->follower->momz = player->mo->momz; + if (player->follower->z <= sz) // Don't go under the floor please + player->follower->z = sz; + } + else + { + fixed_t fg = P_GetMobjGravity(player->mo); + fixed_t fz = P_GetMobjZMovement(player->follower); + + player->follower->momz = fz; + + // Player is on the ground ... try to get the follower + // back to the ground also if it is above it. + if (player->follower->z > sz) // Don't go under the floor please + player->follower->momz += FixedDiv(fg * 6, fl.vertlag); // Scaled against the default value of vertlag + + + // Other wise match current floor + player->follower->momz = sz - player->follower->z; + + } + } + else + { + player->follower->momz = FixedDiv(sz - player->follower->z, fl.vertlag); + } + + if (player->mo->colorized) + player->follower->color = player->mo->color; + else + player->follower->color = color; + + player->follower->colorized = player->mo->colorized; + + P_SetScale(player->follower, FixedMul(fl.scale, player->mo->scale)); + K_GenericExtraFlagsNoZAdjust(player->follower, player->mo); // Not K_MatchGenericExtraFlag because the Z adjust it has only works properly if master & mo have the same Z height. + + // For comeback in battle. + player->follower->flags2 = (player->follower->flags2 & ~MF2_SHADOW)|(player->mo->flags2 & MF2_SHADOW); + + // Make the follower invisible if we no contest'd rather than removing it. No one will notice the diff seriously. + if (player->pflags & PF_TIMEOVER) // there is more to it than that to check for a full no contest but this isn't used for anything else. + player->follower->flags2 |= MF2_DONTDRAW; + + if (cv_hidefollowers.value) // hide em for people who don't want them + player->follower->flags2 |= MF2_DONTDRAW; + + // if we're moving let's make the angle the direction we're moving towards. This is to avoid drifting / reverse looking awkward. + if (FixedHypot(player->follower->momx, player->follower->momy) >= player->mo->scale) + { + destAngle = R_PointToAngle2(0, 0, player->follower->momx, player->follower->momy); + } + else + { + // Face the player's angle when standing still. + destAngle = player->mo->angle; + } + + /*// Sal: Turn the follower around when looking backwards. + if ( player->cmd.buttons & BT_LOOKBACK ) + { + destAngle += ANGLE_180; + }*/ + + // Sal: Smoothly rotate angle to the destination value. + angleDiff = AngleDeltaSigned(destAngle, player->follower->angle); + + if (angleDiff != 0) + { + player->follower->angle += FixedDiv(angleDiff, fl.anglelag); + } + + // Ground follower slope rotation + if (fl.mode == FOLLOWERMODE_GROUND) + { + K_CalculateFollowerSlope( + player->follower, + player->follower->x, player->follower->y, player->follower->z, + player->follower->radius, ourheight, + (player->mo->eflags & MFE_VERTICALFLIP), + false + ); + } + + if (player->follower->threshold) + { + // Threshold means the follower was "despanwed" with S_NULL. + return; + } + + // However with how the code is factored, this is just a special case of S_INVISBLE to avoid having to add other player variables. + + // handle follower animations. Could probably be better... + + // Squish + if (player->kartstuff[k_squishedtimer] > 0) + { + // cancel hit confirm + player->follower->movecount = 0; + player->follower->cvmem = 0; + + if (player->kartstuff[k_squishedtimer] > 1) + player->follower->spriteyscale = FRACUNIT/4; + else + player->follower->spriteyscale = FRACUNIT; + + K_UpdateFollowerState(player->follower, fl.hurtstate, FOLLOWERSTATE_HURT); + + } + else if (P_PlayerInPain(player) == true || player->mo->state == &states[S_KART_SPIN] || player->mo->health <= 0) + { + // hurt or dead + // cancel hit confirm + player->follower->movecount = 0; + player->follower->cvmem = 0; + + // spin out + player->follower->angle = player->frameangle; + + K_UpdateFollowerState(player->follower, fl.hurtstate, FOLLOWERSTATE_HURT); + + if (player->mo->health <= 0) + { + // if dead, follow the player's z momentum exactly so they both look like they die at the same speed. + player->follower->momz = player->mo->momz; + } + } + else if (player->exiting) + { + // win/ loss animations + if (K_IsPlayerLosing(player)) + { + // L + K_UpdateFollowerState(player->follower, fl.losestate, FOLLOWERSTATE_LOSE); + } + else + { + // W + K_UpdateFollowerState(player->follower, fl.winstate, FOLLOWERSTATE_WIN); + } + } + else if (player->follower->movecount) + { + K_UpdateFollowerState(player->follower, fl.hitconfirmstate, FOLLOWERSTATE_HITCONFIRM); + player->follower->movecount--; + } + else if (player->speed > 10*player->mo->scale) // animation for moving fast enough + { + K_UpdateFollowerState(player->follower, fl.followstate, FOLLOWERSTATE_FOLLOW); + } + else + { + K_UpdateFollowerState(player->follower, fl.idlestate, FOLLOWERSTATE_IDLE); + } + + } +} + +#define SPINOUTROTSPEED (24 * FRACUNIT) +#define MAXSPINROT (360 * FRACUNIT) + +>>>>>>> 432dcd3a3 (Refactor to prevent desynchs) // // P_PlayerThink // @@ -4581,6 +4969,31 @@ void P_PlayerThink(player_t *player) if (player->exiting && exitcountdown) player->exiting = 99; // SRB2kart + if ((player->doiconspin) && (player->spinoutrot == 0)) + { + player->spinoutrot = SPINOUTROTSPEED; + } + + // MKWii-styled icon spinouts: do a full 360 rotation before stopping. + if ((player->spinoutrot) && (player->spinoutrot < MAXSPINROT)) + { + if ((player->spinoutrot + SPINOUTROTSPEED) >= MAXSPINROT) + { + if (player->doiconspin) + { + player->spinoutrot = (player->spinoutrot + SPINOUTROTSPEED) % MAXSPINROT; + } + else + { + player->spinoutrot = 0; + } + } + else + { + player->spinoutrot += SPINOUTROTSPEED; + } + } + if (player->exiting == 2 || exitcountdown == 2) { if (cv_playersforexit.value) // Count to be sure everyone's exited