- Exhumed: Further clean up updatePlayerWeapon().

* Rename a few variables for better clarity as to what's going on.
* Add new inline to the backend `getWrappedIndex()` that properly wraps around negative values based on the supplied maximum using modulo.
* Directly set the weapon when doing the next/prev tests to avoid unnecessary tests to see whether the player has the weapon or not.
This commit is contained in:
Mitchell Richters 2023-03-27 18:47:05 +11:00
parent ebedf46434
commit 1ed655e0e7
2 changed files with 18 additions and 17 deletions

View file

@ -486,4 +486,9 @@ inline void setFreeAimVelocity(double& vel, double& zvel, const DAngle pitch, co
zvel = pitch.Sin() * zvspeed;
}
inline int getWrappedIndex(const int index, const int maxvalue)
{
return ((index % maxvalue) + maxvalue) % maxvalue;
}
#include "updatesector.h"

View file

@ -1227,33 +1227,29 @@ static void updatePlayerWeapon(Player* const pPlayer)
return;
}
auto weap2 = pPlayer->input.getNewWeapon();
const auto newWeap = pPlayer->input.getNewWeapon();
if (const auto weapDir = (weap2 == WeaponSel_Next) - (weap2 == WeaponSel_Prev))
if (const auto weapDir = (newWeap == WeaponSel_Next) - (newWeap == WeaponSel_Prev))
{
const auto currWeap = pPlayer->nCurrentWeapon;
auto wrapFwd = weapDir > 0 && currWeap == 6;
auto wrapBck = weapDir < 0 && currWeap == 0;
auto newWeap = wrapFwd ? 0 : wrapBck ? 6 : (currWeap + weapDir);
auto hasWeap = pPlayer->nPlayerWeapons & (1 << newWeap);
int nextWeap = getWrappedIndex(pPlayer->nCurrentWeapon + weapDir, kMaxWeapons);
int haveWeap = pPlayer->nPlayerWeapons & (1 << nextWeap);
while (newWeap && (!hasWeap || (hasWeap && !pPlayer->nAmmo[newWeap])))
while (nextWeap && (!haveWeap || (haveWeap && !pPlayer->nAmmo[nextWeap])))
{
newWeap += weapDir;
if (newWeap > 6) newWeap = 0;
hasWeap = pPlayer->nPlayerWeapons & (1 << newWeap);
nextWeap = getWrappedIndex(nextWeap + weapDir, kMaxWeapons);
haveWeap = pPlayer->nPlayerWeapons & (1 << nextWeap);
}
weap2 = newWeap + 1;
SetNewWeapon(pPlayer->nPlayer, nextWeap);
}
else if (weap2 == WeaponSel_Alt)
else if (newWeap == WeaponSel_Alt)
{
// todo
}
// this is where we actually set the weapon in the game.
if (pPlayer->nPlayerWeapons & (1 << (weap2 - 1)))
SetNewWeapon(pPlayer->nPlayer, weap2 - 1);
else if (pPlayer->nPlayerWeapons & (1 << (newWeap - 1)))
{
SetNewWeapon(pPlayer->nPlayer, newWeap - 1);
}
pPlayer->bIsFiring = bIsFiring;
}