Skip to content

Commit

Permalink
- Remove some unnecessary branching and abs() calls in `processMove…
Browse files Browse the repository at this point in the history
…ment()`.

* Since fvel/svel is clamped at the end of the function, these tests just aren't necessary.
* Reversed the logic in some if statements so the most likely outcome doesn't fall to the else branch.
  • Loading branch information
mjr4077au committed Jul 23, 2022
1 parent ea17352 commit c68e112
Showing 1 changed file with 33 additions and 39 deletions.
72 changes: 33 additions & 39 deletions source/core/gameinput.cpp
Expand Up @@ -189,15 +189,16 @@ void processMovement(InputPacket* const currInput, InputPacket* const inputBuffe
// set up variables
int const running = !!(inputBuffer->actions & SB_RUN);
int const keymove = gi->playerKeyMove() << running;
bool const strafing = buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe;
float const mousevelscale = keymove * (1.f / 160.f);
double const hidprescale = g_gameType & GAMEFLAG_PSEXHUMED ? 5. : 1.;
double const hidspeed = getTicrateScale(running ? RUNNINGTURNBASE : NORMALTURNBASE) * BAngToDegree;

// process mouse and initial controller input.
if (buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe)
currInput->svel -= int16_t(((hidInput->mousemovex * mousevelscale) + (scaleAdjust * hidInput->dyaw * keymove)) * hidprescale);
else
if (!strafing)
currInput->avel += float(hidInput->mouseturnx + (scaleAdjust * hidInput->dyaw * hidspeed * turnscale));
else
currInput->svel -= int16_t(((hidInput->mousemovex * mousevelscale) + (scaleAdjust * hidInput->dyaw * keymove)) * hidprescale);

if (!(inputBuffer->actions & SB_AIMMODE))
currInput->horz -= hidInput->mouseturny;
Expand All @@ -216,18 +217,7 @@ void processMovement(InputPacket* const currInput, InputPacket* const inputBuffe
currInput->fvel += int16_t(scaleAdjust * hidInput->dz * keymove * hidprescale);

// process keyboard turning keys.
if (buttonMap.ButtonDown(gamefunc_Strafe) && allowstrafe)
{
if (abs(inputBuffer->svel) < keymove)
{
if (buttonMap.ButtonDown(gamefunc_Turn_Left))
currInput->svel += keymove;

if (buttonMap.ButtonDown(gamefunc_Turn_Right))
currInput->svel -= keymove;
}
}
else
if (!strafing)
{
bool const turnleft = buttonMap.ButtonDown(gamefunc_Turn_Left) || (buttonMap.ButtonDown(gamefunc_Strafe_Left) && !allowstrafe);
bool const turnright = buttonMap.ButtonDown(gamefunc_Turn_Right) || (buttonMap.ButtonDown(gamefunc_Strafe_Right) && !allowstrafe);
Expand All @@ -248,39 +238,43 @@ void processMovement(InputPacket* const currInput, InputPacket* const inputBuffe
resetTurnHeldAmt();
}
}

// process keyboard forward/side velocity keys.
if (abs(inputBuffer->svel) < keymove)
else
{
if (buttonMap.ButtonDown(gamefunc_Strafe_Left) && allowstrafe)
if (buttonMap.ButtonDown(gamefunc_Turn_Left))
currInput->svel += keymove;

if (buttonMap.ButtonDown(gamefunc_Strafe_Right) && allowstrafe)
if (buttonMap.ButtonDown(gamefunc_Turn_Right))
currInput->svel -= keymove;
}
if (abs(inputBuffer->fvel) < keymove)

// process keyboard side velocity keys.
if (buttonMap.ButtonDown(gamefunc_Strafe_Left) && allowstrafe)
currInput->svel += keymove;

if (buttonMap.ButtonDown(gamefunc_Strafe_Right) && allowstrafe)
currInput->svel -= keymove;

// process keyboard forward velocity keys.
if (!(isRR() && drink_amt >= 66 && drink_amt <= 87))
{
if (isRR() && drink_amt >= 66 && drink_amt <= 87)
{
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
{
currInput->fvel += keymove;
currInput->svel += drink_amt & 1 ? keymove : -keymove;
}
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
currInput->fvel += keymove;

if (buttonMap.ButtonDown(gamefunc_Move_Backward))
{
currInput->fvel -= keymove;
currInput->svel -= drink_amt & 1 ? keymove : -keymove;
}
}
else
if (buttonMap.ButtonDown(gamefunc_Move_Backward))
currInput->fvel -= keymove;
}
else
{
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
{
if (buttonMap.ButtonDown(gamefunc_Move_Forward))
currInput->fvel += keymove;
currInput->fvel += keymove;
currInput->svel += drink_amt & 1 ? keymove : -keymove;
}

if (buttonMap.ButtonDown(gamefunc_Move_Backward))
currInput->fvel -= keymove;
if (buttonMap.ButtonDown(gamefunc_Move_Backward))
{
currInput->fvel -= keymove;
currInput->svel -= drink_amt & 1 ? keymove : -keymove;
}
}

Expand Down

0 comments on commit c68e112

Please sign in to comment.