From 572654a2f3f8eccb1090a8aa5da4d89d673e2121 Mon Sep 17 00:00:00 2001 From: Peter0x44 Date: Sun, 9 Feb 2025 07:17:19 +0000 Subject: [PATCH 1/2] SERVER: Refactor W_Give_Ammo, ensure only the needed ammo gets added to the magazine The previous logic added the whole reserve to the magazine, which could overfill magazines when the reserve didn't have enough to fill both guns --- source/server/weapons/weapon_core.qc | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/source/server/weapons/weapon_core.qc b/source/server/weapons/weapon_core.qc index b1fe043..4457fa1 100644 --- a/source/server/weapons/weapon_core.qc +++ b/source/server/weapons/weapon_core.qc @@ -280,31 +280,37 @@ void(float side) W_Give_Ammo = if (self.weapons[0].weapon_reserve == 0) return; - float ammo_shot, max_mag, loadammo; + float ammo_needed, max_mag, loadammo; max_mag = getWeaponMag(self.weapon); + // Determine how much ammo is needed to reload the selected side if (side == S_LEFT) { - ammo_shot = max_mag - self.weapons[0].weapon_magazine_left; + ammo_needed = max_mag - self.weapons[0].weapon_magazine_left; } else { - ammo_shot = max_mag - self.weapons[0].weapon_magazine; + ammo_needed = max_mag - self.weapons[0].weapon_magazine; } - if (ammo_shot < self.weapons[0].weapon_reserve) + + // Ensure we don't take more ammo than is in the reserve + if (ammo_needed > self.weapons[0].weapon_reserve) { - self.weapons[0].weapon_reserve = self.weapons[0].weapon_reserve - ammo_shot; + loadammo = self.weapons[0].weapon_reserve; - loadammo = max_mag; + // Deplete reserve + self.weapons[0].weapon_reserve = 0; } else { - loadammo = self.weapons[0].weapon_magazine + self.weapons[0].weapon_reserve; - self.weapons[0].weapon_reserve = 0; + // Load the ammo needed to fill the magazine, and subtract it from the reserve + loadammo = ammo_needed; + self.weapons[0].weapon_reserve -= ammo_needed; } + // Apply ammo load to the correct side if (side == S_LEFT) { - self.weapons[0].weapon_magazine_left = loadammo; + self.weapons[0].weapon_magazine_left += loadammo; } else { - self.weapons[0].weapon_magazine = loadammo; + self.weapons[0].weapon_magazine += loadammo; } }; @@ -2186,4 +2192,4 @@ void() WeaponCore_ClientLogic = // Allow Melee to be pressed again self.semi_actions &= ~SEMIACTION_MELEE; } -}; \ No newline at end of file +}; From df9acf2c2b1dd3d3813ec81e67ceac65d8664c9e Mon Sep 17 00:00:00 2001 From: tanuki-billie Date: Sun, 9 Feb 2025 11:55:05 -0800 Subject: [PATCH 2/2] Improved logic in HUD_AmmoString() --- source/client/hud.qc | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/source/client/hud.qc b/source/client/hud.qc index d435702..a8e335e 100644 --- a/source/client/hud.qc +++ b/source/client/hud.qc @@ -160,45 +160,38 @@ void() HUD_AmmoString = vector textcolor = [1, 1, 1]; string message = ""; - float reserve_is_low; + float reserve_is_empty; float mag_is_low; // Is the Reserve low? - if (W_IsLowAmmo(getstatf(STAT_ACTIVEWEAPON), getstatf(STAT_AMMO), false)) { - reserve_is_low = true; - } else { - reserve_is_low = false; - } + reserve_is_empty = getstatf(STAT_AMMO) <= 0; // Is the Magazine low? - if (W_IsLowAmmo(getstatf(STAT_ACTIVEWEAPON), getstatf(STAT_CURRENTMAG), true)) { - mag_is_low = true; - } else { - mag_is_low = false; - } + mag_is_low = W_IsLowAmmo(getstatf(STAT_ACTIVEWEAPON), getstatf(STAT_CURRENTMAG), true); - // Nothing to do. - if (mag_is_low == false && reserve_is_low == false) { + // When the mag isn't low, there's nothing to do here. + if (mag_is_low == false) { ammoopac = 1; ammoloop = 0; return; } else { - // Display Reload text if the mag is low but reserve is not. - if (mag_is_low == true && reserve_is_low == false) { - message = "Reload"; - textcolor = [1, 1, 1]; - } - // Report NO AMMO if both are empty - else if (getstatf(STAT_CURRENTMAG) <= 0 && getstatf(STAT_AMMO) <= 0) + // Report NO AMMO if both current mag and reserve are empty + if (getstatf(STAT_CURRENTMAG) <= 0 && reserve_is_empty == true) { message = "NO AMMO"; textcolor = [215/255, 0, 0]; } // Display LOW AMMO if mag is low and reserve is empty - else if (mag_is_low == true && getstatf(STAT_AMMO) <= 0) + else if (reserve_is_empty == true) { message = "LOW AMMO"; - textcolor = [219/255, 203/255, 19/255]; + textcolor = [219/255, 203/255, 19/255]; + } + // Otherwise, just display reload text + else + { + message = "Reload"; + textcolor = [1, 1, 1]; } }