From 1ed655e0e7e6ab3b3701287b1a282d453eed84a3 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Mon, 27 Mar 2023 18:47:05 +1100 Subject: [PATCH] - 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. --- source/core/gamefuncs.h | 5 +++++ source/games/exhumed/src/player.cpp | 30 +++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/source/core/gamefuncs.h b/source/core/gamefuncs.h index 19bd4e24f..e63e575cd 100644 --- a/source/core/gamefuncs.h +++ b/source/core/gamefuncs.h @@ -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" diff --git a/source/games/exhumed/src/player.cpp b/source/games/exhumed/src/player.cpp index 1e59887b4..1765256fb 100644 --- a/source/games/exhumed/src/player.cpp +++ b/source/games/exhumed/src/player.cpp @@ -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; }