From e2a949949c888b1babb37a9b439a81fb11b71127 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Tue, 3 Oct 2023 21:46:13 +1100 Subject: [PATCH] - Take better advantage of `InputPacket::vel`'s `FVector3` status. * Change it to a DVector3 as that's what the game is expecting. Too many narrowings against DAngle objects needed. * Utilise object methods where possible. * Do all math against the object where possible, not its members. --- source/common/utility/vectors.h | 6 +++++ source/core/d_net.cpp | 16 ++++---------- source/core/d_protocol.cpp | 18 +++++---------- source/core/gameinput.cpp | 7 +++--- source/core/packet.h | 2 +- source/games/blood/src/player.cpp | 7 +++--- source/games/duke/src/inlines.h | 9 +++----- source/games/exhumed/src/player.cpp | 2 +- source/games/sw/src/player.cpp | 34 +++++++++++++---------------- 9 files changed, 42 insertions(+), 59 deletions(-) diff --git a/source/common/utility/vectors.h b/source/common/utility/vectors.h index 352c161c0..608705ef9 100644 --- a/source/common/utility/vectors.h +++ b/source/common/utility/vectors.h @@ -1522,6 +1522,12 @@ inline TVector2 clamp(const TVector2 &vec, const TVector2 &min, const T return TVector2(clamp(vec.X, min.X, max.X), clamp(vec.Y, min.Y, max.Y)); } +template +inline TVector3 clamp(const TVector3 &vec, const TVector3 &min, const TVector3 &max) +{ + return TVector3(clamp(vec.X, min.X, max.X), clamp(vec.Y, min.Y, max.Y), clamp(vec.Z, min.Z, max.Z)); +} + template inline TAngle interpolatedvalue(const TAngle &oang, const TAngle &ang, const double interpfrac) { diff --git a/source/core/d_net.cpp b/source/core/d_net.cpp index e26e77bf2..94f0cd9d6 100644 --- a/source/core/d_net.cpp +++ b/source/core/d_net.cpp @@ -1032,9 +1032,7 @@ void NetUpdate (void) int mod = maketic - ticdup; int modp, tic; - float svel = 0; - float fvel = 0; - float uvel = 0; + DVector3 vel{}; float avel = 0; float horz = 0; float roll = 0; @@ -1042,17 +1040,13 @@ void NetUpdate (void) for (tic = 0; tic < ticdup; ++tic) { modp = (mod + tic) % LOCALCMDTICS; - fvel += localcmds[modp].ucmd.vel.X; - svel += localcmds[modp].ucmd.vel.Y; - uvel += localcmds[modp].ucmd.vel.Z; + vel += localcmds[modp].ucmd.vel; avel += localcmds[modp].ucmd.avel; horz += localcmds[modp].ucmd.horz; roll += localcmds[modp].ucmd.roll; } - svel /= ticdup; - fvel /= ticdup; - uvel /= ticdup; + vel /= ticdup; avel /= ticdup; horz /= ticdup; roll /= ticdup; @@ -1060,9 +1054,7 @@ void NetUpdate (void) for (tic = 0; tic < ticdup; ++tic) { modp = (mod + tic) % LOCALCMDTICS; - localcmds[modp].ucmd.vel.X = fvel; - localcmds[modp].ucmd.vel.Y = svel; - localcmds[modp].ucmd.vel.Z = uvel; + localcmds[modp].ucmd.vel = vel; localcmds[modp].ucmd.avel = avel; localcmds[modp].ucmd.horz = horz; localcmds[modp].ucmd.roll = roll; diff --git a/source/core/d_protocol.cpp b/source/core/d_protocol.cpp index 31de5bb7a..235051fbc 100644 --- a/source/core/d_protocol.cpp +++ b/source/core/d_protocol.cpp @@ -210,17 +210,17 @@ int PackUserCmd (const InputPacket *ucmd, const InputPacket *basis, uint8_t **st if (ucmd->vel.X != basis->vel.X) { flags |= UCMDF_FORWARDMOVE; - WriteFloat (ucmd->vel.X, stream); + WriteFloat ((float)ucmd->vel.X, stream); } if (ucmd->vel.Y != basis->vel.Y) { flags |= UCMDF_SIDEMOVE; - WriteFloat (ucmd->vel.Y, stream); + WriteFloat ((float)ucmd->vel.Y, stream); } if (ucmd->vel.Z != basis->vel.Z) { flags |= UCMDF_UPMOVE; - WriteFloat (ucmd->vel.Z, stream); + WriteFloat ((float)ucmd->vel.Z, stream); } if (ucmd->roll != basis->roll) { @@ -252,9 +252,7 @@ FSerializer &Serialize(FSerializer &arc, const char *key, InputPacket &cmd, Inpu arc("actions", cmd.actions) ("horz", cmd.horz) ("avel", cmd.avel) - ("fvel", cmd.vel.X) - ("svel", cmd.vel.Y) - ("uvel", cmd.vel.Z) + ("vel", cmd.vel) ("roll", cmd.roll) .EndObject(); } @@ -268,9 +266,7 @@ int WriteUserCmdMessage (InputPacket *ucmd, const InputPacket *basis, uint8_t ** if (ucmd->actions != 0 || ucmd->horz != 0 || ucmd->avel != 0 || - ucmd->vel.X != 0 || - ucmd->vel.Y != 0 || - ucmd->vel.Z != 0 || + !ucmd->vel.isZero() || ucmd->roll != 0) { WriteByte (DEM_USERCMD, stream); @@ -281,9 +277,7 @@ int WriteUserCmdMessage (InputPacket *ucmd, const InputPacket *basis, uint8_t ** if (ucmd->actions != basis->actions || ucmd->horz != basis->horz || ucmd->avel != basis->avel || - ucmd->vel.X != basis->vel.X || - ucmd->vel.Y != basis->vel.Y || - ucmd->vel.Z != basis->vel.Z || + ucmd->vel != basis->vel || ucmd->roll != basis->roll) { WriteByte (DEM_USERCMD, stream); diff --git a/source/core/gameinput.cpp b/source/core/gameinput.cpp index eea9d7ed3..112051aba 100644 --- a/source/core/gameinput.cpp +++ b/source/core/gameinput.cpp @@ -167,9 +167,8 @@ void GameInput::processMovement(PlayerAngles* const plrAngles, const float scale } // add collected input to game's local input accumulation packet. - inputBuffer.vel.X = clamp(inputBuffer.vel.X + thisInput.vel.X, -(float)keymove, (float)keymove); - inputBuffer.vel.Y = clamp(inputBuffer.vel.Y + thisInput.vel.Y, -(float)keymove, (float)keymove); - inputBuffer.vel.Z = clamp(inputBuffer.vel.Z + thisInput.vel.Z, -1.00f, 1.00f); + const DVector3 maxVel{ (double)keymove, (double)keymove, 1. }; + inputBuffer.vel = clamp(inputBuffer.vel + thisInput.vel, -maxVel, maxVel); inputBuffer.avel = clamp(inputBuffer.avel + thisInput.avel, -179.f, 179.f); inputBuffer.horz = clamp(inputBuffer.horz + thisInput.horz, -179.f, 179.f); @@ -203,7 +202,7 @@ void GameInput::processVehicle(PlayerAngles* const plrAngles, const float scaleA const auto kbdForwards = buttonMap.ButtonDown(gamefunc_Move_Forward) || buttonMap.ButtonDown(gamefunc_Strafe); const auto kbdBackward = buttonMap.ButtonDown(gamefunc_Move_Backward); thisInput.vel.X = kbdForwards - kbdBackward + joyAxes[JOYAXIS_Forward]; - inputBuffer.vel.X = clamp(inputBuffer.vel.X + thisInput.vel.X, -1.f, 1.f); + inputBuffer.vel.X = clamp(inputBuffer.vel.X + thisInput.vel.X, -1., 1.); // This sync bit is the brake key. if (buttonMap.ButtonDown(gamefunc_Run)) inputBuffer.actions |= SB_CROUCH; diff --git a/source/core/packet.h b/source/core/packet.h index bfabd1029..3041d73d7 100644 --- a/source/core/packet.h +++ b/source/core/packet.h @@ -70,7 +70,7 @@ enum struct InputPacket { - FVector3 vel; + DVector3 vel; float avel; float horz; float roll; diff --git a/source/games/blood/src/player.cpp b/source/games/blood/src/player.cpp index 4dd5bb5e4..4cd0d39f9 100644 --- a/source/games/blood/src/player.cpp +++ b/source/games/blood/src/player.cpp @@ -858,8 +858,7 @@ void playerStart(int nPlayer, int bNewLevel) actor->vel.Zero(); pInput->avel = 0; pInput->actions = 0; - pInput->vel.X = 0; - pInput->vel.Y = 0; + pInput->vel.Zero(); pInput->horz = 0; pPlayer->flickerEffect = 0; pPlayer->quakeEffect = 0; @@ -1525,7 +1524,7 @@ void ProcessInput(DBloodPlayer* pPlayer) // Allow it to become true behind a CVAR to offer an alternate playing experience if desired. pPlayer->isRunning = !!(pInput->actions & SB_RUN) && !cl_bloodvanillarun; - if ((pInput->actions & SB_BUTTON_MASK) || pInput->vel.X || pInput->vel.Y || pInput->avel) + if ((pInput->actions & SB_BUTTON_MASK) || !pInput->vel.XY().isZero() || pInput->avel) pPlayer->restTime = 0; else if (pPlayer->restTime >= 0) pPlayer->restTime += 4; @@ -1572,7 +1571,7 @@ void ProcessInput(DBloodPlayer* pPlayer) return; } - if ((pInput->vel.X || pInput->vel.Y) && (pPlayer->posture == 1 || actor->xspr.height < 256)) + if (!pInput->vel.XY().isZero() && (pPlayer->posture == 1 || actor->xspr.height < 256)) { const double speed = pPlayer->posture == 1? 1. : 1. - (actor->xspr.height * (1. / 256.) * (actor->xspr.height < 256)); const double fvAccel = pInput->vel.X > 0 ? pPosture->frontAccel : pPosture->backAccel; diff --git a/source/games/duke/src/inlines.h b/source/games/duke/src/inlines.h index f1615ed59..c66e6c79d 100644 --- a/source/games/duke/src/inlines.h +++ b/source/games/duke/src/inlines.h @@ -137,12 +137,12 @@ inline bool PlayerUseItem(int pl, int num) return getPlayer(pl)->cmd.ucmd.isItemUsed(num - 1); } -inline float PlayerInputSideVel(int pl) +inline double PlayerInputSideVel(int pl) { return getPlayer(pl)->cmd.ucmd.vel.Y; } -inline float PlayerInputForwardVel(int pl) +inline double PlayerInputForwardVel(int pl) { return getPlayer(pl)->cmd.ucmd.vel.X; } @@ -264,12 +264,9 @@ inline int monsterCheatCheck(DDukeActor* self) inline void processinputvel(int snum) { const auto p = getPlayer(snum); - const auto velvect = DVector2(p->cmd.ucmd.vel.X, p->cmd.ucmd.vel.Y).Rotated(p->GetActor()->spr.Angles.Yaw) + p->fric; - p->cmd.ucmd.vel.X = (float)velvect.X; - p->cmd.ucmd.vel.Y = (float)velvect.Y; + p->cmd.ucmd.vel.XY() = p->cmd.ucmd.vel.XY().Rotated(p->GetActor()->spr.Angles.Yaw) + p->fric; } - inline const ActorInfo* DDukeActor::conInfo() const { auto tn = static_cast(GetClass())->ActorInfo()->TypeNum; diff --git a/source/games/exhumed/src/player.cpp b/source/games/exhumed/src/player.cpp index e5bed6c66..80c0afc13 100644 --- a/source/games/exhumed/src/player.cpp +++ b/source/games/exhumed/src/player.cpp @@ -1102,7 +1102,7 @@ static void updatePlayerVelocity(DExhumedPlayer* const pPlayer) if (pPlayer->nHealth > 0) { const auto pInput = &pPlayer->cmd.ucmd; - const auto inputvect = DVector2(pInput->vel.X, pInput->vel.Y).Rotated(pPlayerActor->spr.Angles.Yaw) * 0.375; + const auto inputvect = pInput->vel.XY().Rotated(pPlayerActor->spr.Angles.Yaw) * 0.375; for (int i = 0; i < 4; i++) { diff --git a/source/games/sw/src/player.cpp b/source/games/sw/src/player.cpp index 9f1eb4d13..91e8094d6 100644 --- a/source/games/sw/src/player.cpp +++ b/source/games/sw/src/player.cpp @@ -1864,8 +1864,7 @@ void DoPlayerMove(DSWPlayer* pp) pp->ovect = pp->vect; pp->Angles.PrevStrafeVel = pp->Angles.StrafeVel; - pp->vect.X += pp->cmd.ucmd.vel.X * INPUT_SCALE; - pp->vect.Y += pp->cmd.ucmd.vel.Y * INPUT_SCALE; + pp->vect += pp->cmd.ucmd.vel.XY() * INPUT_SCALE; pp->Angles.StrafeVel += pp->svel * INPUT_SCALE; friction = pp->friction; @@ -2390,9 +2389,12 @@ void DoPlayerMoveVehicle(DSWPlayer* pp) if (!Prediction) { - if (abs(pp->cmd.ucmd.vel.X + pp->cmd.ucmd.vel.Y) && !abs(pp->lastcmd.ucmd.vel.X + pp->lastcmd.ucmd.vel.Y)) + const auto lastvel = pp->lastcmd.ucmd.vel.XY().Sum(); + const auto thisvel = pp->cmd.ucmd.vel.XY().Sum(); + + if (thisvel && !lastvel) PlaySOsound(pp->sop->mid_sector,SO_DRIVE_SOUND); - else if (!abs(pp->cmd.ucmd.vel.X + pp->cmd.ucmd.vel.Y) && abs(pp->lastcmd.ucmd.vel.X + pp->lastcmd.ucmd.vel.Y)) + else if (!thisvel && lastvel) PlaySOsound(pp->sop->mid_sector,SO_IDLE_SOUND); } @@ -2405,16 +2407,14 @@ void DoPlayerMoveVehicle(DSWPlayer* pp) if (sop->drive_speed) { - pp->vect.X = pp->cmd.ucmd.vel.X * sop->drive_speed * (70. / 1048576.); - pp->vect.Y = pp->cmd.ucmd.vel.Y * sop->drive_speed * (70. / 1048576.); + pp->vect = pp->cmd.ucmd.vel.XY() * sop->drive_speed * (70. / 1048576.); // does sliding/momentum pp->vect = (pp->vect + (pp->ovect * (sop->drive_slide-1)))/sop->drive_slide; } else { - pp->vect.X += pp->cmd.ucmd.vel.X * INPUT_SCALE; - pp->vect.Y += pp->cmd.ucmd.vel.Y * INPUT_SCALE; + pp->vect += pp->cmd.ucmd.vel.XY() * INPUT_SCALE; pp->vect *= TANK_FRICTION; pp->vect = (pp->vect + (pp->ovect*1))/2; @@ -3079,8 +3079,7 @@ void DoPlayerClimb(DSWPlayer* pp) if (Prediction) return; - pp->vect.X += pp->cmd.ucmd.vel.X * INPUT_SCALE; - pp->vect.Y += pp->cmd.ucmd.vel.Y * INPUT_SCALE; + pp->vect += pp->cmd.ucmd.vel.XY() * INPUT_SCALE; pp->vect *= PLAYER_CLIMB_FRICTION; if (abs(pp->vect.X) < 0.05 && abs(pp->vect.Y) < 0.05) pp->vect.X = pp->vect.Y = 0; @@ -4957,7 +4956,7 @@ void DoPlayerBeginOperate(DSWPlayer* pp) switch (sop->track) { case SO_VEHICLE: - if (pp->cmd.ucmd.vel.X || pp->cmd.ucmd.vel.Y) + if (!pp->cmd.ucmd.vel.XY().isZero()) PlaySOsound(pp->sop->mid_sector, SO_DRIVE_SOUND); else PlaySOsound(pp->sop->mid_sector, SO_IDLE_SOUND); @@ -4975,7 +4974,7 @@ void DoPlayerBeginOperate(DSWPlayer* pp) break; #if 0 case SO_SPEED_BOAT: - if (pp->input.fvel || pp->input.svel) + if (!pp->cmd.ucmd.vel.XY().isZero()) PlaySOsound(pp->sop->mid_sector, SO_DRIVE_SOUND); else PlaySOsound(pp->sop->mid_sector, SO_IDLE_SOUND); @@ -5050,7 +5049,7 @@ void DoPlayerBeginRemoteOperate(DSWPlayer* pp, SECTOR_OBJECT* sop) switch (sop->track) { case SO_VEHICLE: - if (pp->cmd.ucmd.vel.X || pp->cmd.ucmd.vel.Y) + if (!pp->cmd.ucmd.vel.XY().isZero()) PlaySOsound(pp->sop->mid_sector, SO_DRIVE_SOUND); else PlaySOsound(pp->sop->mid_sector, SO_IDLE_SOUND); @@ -6551,7 +6550,7 @@ void ChopsCheck(DSWPlayer* pp) { if (!M_Active() && !(pp->Flags & PF_DEAD) && !pp->sop_riding && numplayers <= 1) { - if (pp->cmd.ucmd.actions & ~SB_RUN || pp->cmd.ucmd.vel.X || pp->cmd.ucmd.vel.Y || pp->cmd.ucmd.avel || pp->cmd.ucmd.horz || + if (pp->cmd.ucmd.actions & ~SB_RUN || !pp->cmd.ucmd.vel.XY().isZero() || pp->cmd.ucmd.avel || pp->cmd.ucmd.horz || (pp->Flags & (PF_CLIMBING | PF_FALLING | PF_DIVING))) { // Hit a input key or other reason to stop chops @@ -6785,11 +6784,8 @@ void domovethings(void) pp->svel = pp->cmd.ucmd.vel.Y; // convert fvel/svel into a vector before performing actions. - const auto fvel = pp->cmd.ucmd.vel.X + pp->cmd.ucmd.vel.Z * (pp->DoPlayerAction == DoPlayerClimb); - const auto svel = pp->cmd.ucmd.vel.Y; - const auto velvect = DVector2(fvel, svel).Rotated(pp->GetActor()->spr.Angles.Yaw); - pp->cmd.ucmd.vel.X = (float)velvect.X; - pp->cmd.ucmd.vel.Y = (float)velvect.Y; + pp->cmd.ucmd.vel.X += pp->cmd.ucmd.vel.Z * (pp->DoPlayerAction == DoPlayerClimb); + pp->cmd.ucmd.vel.XY() = pp->cmd.ucmd.vel.XY().Rotated(pp->GetActor()->spr.Angles.Yaw); if (pp->DoPlayerAction) pp->DoPlayerAction(pp);