From 860aaa75fa9af90c9a7e7e08458e7453c9377b6e Mon Sep 17 00:00:00 2001
From: Mircea Kitsune <sonichedgehog_hyperblast00@yahoo.com>
Date: Sat, 22 Jan 2011 02:01:28 +0200
Subject: [PATCH] Do the ammo maths at the beginning again, more correct and
 safe

---
 qcsrc/server/w_crylink.qc         | 42 +++++++++++++++----------------
 qcsrc/server/w_electro.qc         | 40 ++++++++++++++---------------
 qcsrc/server/w_grenadelauncher.qc | 38 ++++++++++++++--------------
 qcsrc/server/w_hlac.qc            | 42 +++++++++++++++----------------
 qcsrc/server/w_minelayer.qc       | 24 +++++++++---------
 qcsrc/server/w_shotgun.qc         | 24 +++++++++---------
 qcsrc/server/w_sniperrifle.qc     | 24 +++++++++---------
 qcsrc/server/w_uzi.qc             | 24 +++++++++---------
 8 files changed, 129 insertions(+), 129 deletions(-)

diff --git a/qcsrc/server/w_crylink.qc b/qcsrc/server/w_crylink.qc
index e73600eaee..54bbf3652e 100644
--- a/qcsrc/server/w_crylink.qc
+++ b/qcsrc/server/w_crylink.qc
@@ -373,6 +373,18 @@ void W_Crylink_Attack (void)
 	vector forward, right, up;
 	float maxdmg;
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_crylink_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_crylink_primary_ammo;
+			self.crylink_load = self.clip_load;
+		}
+		else
+			self.ammo_cells -= autocvar_g_balance_crylink_primary_ammo;
+	}
+
 	maxdmg = autocvar_g_balance_crylink_primary_damage*autocvar_g_balance_crylink_primary_shots;
 	maxdmg *= 1 + autocvar_g_balance_crylink_primary_bouncedamagefactor * autocvar_g_balance_crylink_primary_bounces;
 	if(autocvar_g_balance_crylink_primary_joinexplode)
@@ -465,25 +477,25 @@ void W_Crylink_Attack (void)
 		counter = counter + 1;
 	}
 	self.crylink_lastgroup = proj;
+}
+
+void W_Crylink_Attack2 (void)
+{
+	local float counter, shots;
+	local entity proj, prevproj, firstproj;
+	float maxdmg;
 
 	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
 	{
 		if(autocvar_g_balance_crylink_reload_ammo)
 		{
-			self.clip_load -= autocvar_g_balance_crylink_primary_ammo;
+			self.clip_load -= autocvar_g_balance_crylink_secondary_ammo;
 			self.crylink_load = self.clip_load;
 		}
 		else
-			self.ammo_cells -= autocvar_g_balance_crylink_primary_ammo;
+			self.ammo_cells -= autocvar_g_balance_crylink_secondary_ammo;
 	}
-}
-
-void W_Crylink_Attack2 (void)
-{
-	local float counter, shots;
-	local entity proj, prevproj, firstproj;
-	float maxdmg;
 
 	maxdmg = autocvar_g_balance_crylink_secondary_damage*autocvar_g_balance_crylink_secondary_shots;
 	maxdmg *= 1 + autocvar_g_balance_crylink_secondary_bouncedamagefactor * autocvar_g_balance_crylink_secondary_bounces;
@@ -562,18 +574,6 @@ void W_Crylink_Attack2 (void)
 		counter = counter + 1;
 	}
 	self.crylink_lastgroup = proj;
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_crylink_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_crylink_secondary_ammo;
-			self.crylink_load = self.clip_load;
-		}
-		else
-			self.ammo_cells -= autocvar_g_balance_crylink_secondary_ammo;
-	}
 }
 
 void spawnfunc_weapon_crylink (void)
diff --git a/qcsrc/server/w_electro.qc b/qcsrc/server/w_electro.qc
index 73ebfced76..1b67d04089 100644
--- a/qcsrc/server/w_electro.qc
+++ b/qcsrc/server/w_electro.qc
@@ -164,6 +164,18 @@ void W_Electro_Attack()
 {
 	local entity proj;
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_electro_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_electro_primary_ammo;
+			self.electro_load = self.clip_load;
+		}
+		else
+			self.ammo_cells -= autocvar_g_balance_electro_primary_ammo;
+	}
+
 	W_SetupShot_ProjectileSize (self, '0 0 -3', '0 0 -3', FALSE, 2, "weapons/electro_fire.wav", CHAN_WEAPON, autocvar_g_balance_electro_primary_damage);
 
 	pointparticles(particleeffectnum("electro_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
@@ -193,23 +205,23 @@ void W_Electro_Attack()
 	CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO_BEAM, TRUE);
 
 	other = proj; MUTATOR_CALLHOOK(EditProjectile);
+}
+
+void W_Electro_Attack2()
+{
+	local entity proj;
 
 	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
 	{
 		if(autocvar_g_balance_electro_reload_ammo)
 		{
-			self.clip_load -= autocvar_g_balance_electro_primary_ammo;
+			self.clip_load -= autocvar_g_balance_electro_secondary_ammo;
 			self.electro_load = self.clip_load;
 		}
 		else
-			self.ammo_cells -= autocvar_g_balance_electro_primary_ammo;
+			self.ammo_cells -= autocvar_g_balance_electro_secondary_ammo;
 	}
-}
-
-void W_Electro_Attack2()
-{
-	local entity proj;
 
 	W_SetupShot_ProjectileSize (self, '0 0 -4', '0 0 -4', FALSE, 2, "weapons/electro_fire2.wav", CHAN_WEAPON, autocvar_g_balance_electro_secondary_damage);
 
@@ -255,18 +267,6 @@ void W_Electro_Attack2()
 	CSQCProjectile(proj, TRUE, PROJECTILE_ELECTRO, FALSE); // no culling, it has sound
 
 	other = proj; MUTATOR_CALLHOOK(EditProjectile);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_electro_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_electro_secondary_ammo;
-			self.electro_load = self.clip_load;
-		}
-		else
-			self.ammo_cells -= autocvar_g_balance_electro_secondary_ammo;
-	}
 }
 
 .vector hook_start, hook_end;
@@ -331,9 +331,9 @@ void lgbeam_think()
 
 	float dt, f;
 	dt = frametime;
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 	if not(self.owner.items & IT_UNLIMITED_WEAPON_AMMO)
 	{
-		// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 		if(autocvar_g_balance_electro_primary_ammo)
 		{
 			if(autocvar_g_balance_electro_reload_ammo)
diff --git a/qcsrc/server/w_grenadelauncher.qc b/qcsrc/server/w_grenadelauncher.qc
index d252c1f1ca..e803ac77f2 100644
--- a/qcsrc/server/w_grenadelauncher.qc
+++ b/qcsrc/server/w_grenadelauncher.qc
@@ -215,6 +215,18 @@ void W_Grenade_Attack (void)
 {
 	local entity gren;
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_grenadelauncher_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_grenadelauncher_primary_ammo;
+			self.grenadelauncher_load = self.clip_load;
+		}
+		else
+			self.ammo_rockets -= autocvar_g_balance_grenadelauncher_primary_ammo;
+	}
+
 	W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CHAN_WEAPON, autocvar_g_balance_grenadelauncher_primary_damage);
 	w_shotdir = v_forward; // no TrueAim for grenades please
 
@@ -254,23 +266,23 @@ void W_Grenade_Attack (void)
 		CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
 
 	other = gren; MUTATOR_CALLHOOK(EditProjectile);
+}
+
+void W_Grenade_Attack2 (void)
+{
+	local entity gren;
 
 	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
 	{
 		if(autocvar_g_balance_grenadelauncher_reload_ammo)
 		{
-			self.clip_load -= autocvar_g_balance_grenadelauncher_primary_ammo;
+			self.clip_load -= autocvar_g_balance_grenadelauncher_secondary_ammo;
 			self.grenadelauncher_load = self.clip_load;
 		}
 		else
-			self.ammo_rockets -= autocvar_g_balance_grenadelauncher_primary_ammo;
+			self.ammo_rockets -= autocvar_g_balance_grenadelauncher_secondary_ammo;
 	}
-}
-
-void W_Grenade_Attack2 (void)
-{
-	local entity gren;
 
 	W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 4, "weapons/grenade_fire.wav", CHAN_WEAPON, autocvar_g_balance_grenadelauncher_secondary_damage);
 	w_shotdir = v_forward; // no TrueAim for grenades please
@@ -310,18 +322,6 @@ void W_Grenade_Attack2 (void)
 		CSQCProjectile(gren, TRUE, PROJECTILE_GRENADE_BOUNCING, TRUE);
 
 	other = gren; MUTATOR_CALLHOOK(EditProjectile);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_grenadelauncher_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_grenadelauncher_secondary_ammo;
-			self.grenadelauncher_load = self.clip_load;
-		}
-		else
-			self.ammo_rockets -= autocvar_g_balance_grenadelauncher_secondary_ammo;
-	}
 }
 
 void spawnfunc_weapon_grenadelauncher (void)
diff --git a/qcsrc/server/w_hlac.qc b/qcsrc/server/w_hlac.qc
index 5385101e7e..218045d718 100644
--- a/qcsrc/server/w_hlac.qc
+++ b/qcsrc/server/w_hlac.qc
@@ -76,6 +76,18 @@ void W_HLAC_Attack (void)
 	local entity missile;
     float spread;
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_hlac_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_hlac_primary_ammo;
+			self.hlac_load = self.clip_load;
+		}
+		else
+			self.ammo_cells -= autocvar_g_balance_hlac_primary_ammo;
+	}
+
     spread = autocvar_g_balance_hlac_primary_spread_min + (autocvar_g_balance_hlac_primary_spread_add * self.misc_bulletcounter);
     spread = min(spread,autocvar_g_balance_hlac_primary_spread_max);
     if(self.crouch)
@@ -116,18 +128,6 @@ void W_HLAC_Attack (void)
 	CSQCProjectile(missile, TRUE, PROJECTILE_HLAC, TRUE);
 
 	other = missile; MUTATOR_CALLHOOK(EditProjectile);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_hlac_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_hlac_primary_ammo;
-			self.hlac_load = self.clip_load;
-		}
-		else
-			self.ammo_cells -= autocvar_g_balance_hlac_primary_ammo;
-	}
 }
 
 void W_HLAC_Attack2f (void)
@@ -177,15 +177,6 @@ void W_HLAC_Attack2 (void)
 {
     float i;
 
-    for(i=autocvar_g_balance_hlac_secondary_shots;i>0;--i)
-        W_HLAC_Attack2f();
-
-	if (!g_norecoil)
-	{
-		self.punchangle_x = random () - 0.5;
-		self.punchangle_y = random () - 0.5;
-	}
-
 	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
 	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
 	{
@@ -197,6 +188,15 @@ void W_HLAC_Attack2 (void)
 		else
 			self.ammo_cells -= autocvar_g_balance_hlac_secondary_ammo;
 	}
+
+    for(i=autocvar_g_balance_hlac_secondary_shots;i>0;--i)
+        W_HLAC_Attack2f();
+
+	if (!g_norecoil)
+	{
+		self.punchangle_x = random () - 0.5;
+		self.punchangle_y = random () - 0.5;
+	}
 }
 
 // weapon frames
diff --git a/qcsrc/server/w_minelayer.qc b/qcsrc/server/w_minelayer.qc
index 691eccfe1c..dfef721953 100644
--- a/qcsrc/server/w_minelayer.qc
+++ b/qcsrc/server/w_minelayer.qc
@@ -268,6 +268,18 @@ void W_Mine_Attack (void)
 		}
 	}
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_minelayer_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_minelayer_ammo;
+			self.minelayer_load = self.clip_load;
+		}
+		else
+			self.ammo_rockets -= autocvar_g_balance_minelayer_ammo;
+	}
+
 	W_SetupShot_ProjectileSize (self, '-4 -4 -4', '4 4 4', FALSE, 5, "weapons/mine_fire.wav", CHAN_WEAPON, autocvar_g_balance_minelayer_damage);
 	pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
 
@@ -313,18 +325,6 @@ void W_Mine_Attack (void)
 	// common properties
 
 	other = mine; MUTATOR_CALLHOOK(EditProjectile);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_minelayer_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_minelayer_ammo;
-			self.minelayer_load = self.clip_load;
-		}
-		else
-			self.ammo_rockets -= autocvar_g_balance_minelayer_ammo;
-	}
 }
 
 void spawnfunc_weapon_minelayer (void); // defined in t_items.qc
diff --git a/qcsrc/server/w_shotgun.qc b/qcsrc/server/w_shotgun.qc
index 2bee7080d1..a8e4bb47c4 100644
--- a/qcsrc/server/w_shotgun.qc
+++ b/qcsrc/server/w_shotgun.qc
@@ -77,6 +77,18 @@ void W_Shotgun_Attack (void)
 	bulletspeed = autocvar_g_balance_shotgun_primary_speed;
 	bulletconstant = autocvar_g_balance_shotgun_primary_bulletconstant;
 
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_shotgun_reload_ammo)
+		{
+			self.clip_load -= ammoamount;
+			self.shotgun_load = self.clip_load;
+		}
+		else
+			self.ammo_shells -= ammoamount;
+	}
+
 	W_SetupShot (self, autocvar_g_antilag_bullets && bulletspeed >= autocvar_g_antilag_bullets, 5, "weapons/shotgun_fire.wav", CHAN_WEAPON, d * bullets);
 	for (sc = 0;sc < bullets;sc = sc + 1)
 		fireBallisticBullet(w_shotorg, w_shotdir, spread, bulletspeed, 5, d, 0, f, WEP_SHOTGUN, 0, 1, bulletconstant);
@@ -96,18 +108,6 @@ void W_Shotgun_Attack (void)
 	flash.nextthink = time + 0.06;
 	flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
 	W_AttachToShotorg(flash, '5 0 0');
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_shotgun_reload_ammo)
-		{
-			self.clip_load -= ammoamount;
-			self.shotgun_load = self.clip_load;
-		}
-		else
-			self.ammo_shells -= ammoamount;
-	}
 }
 
 void shotgun_meleethink (void)
diff --git a/qcsrc/server/w_sniperrifle.qc b/qcsrc/server/w_sniperrifle.qc
index 469b2db0b1..7cfea7d2a4 100644
--- a/qcsrc/server/w_sniperrifle.qc
+++ b/qcsrc/server/w_sniperrifle.qc
@@ -63,6 +63,18 @@ void W_SniperRifle_Reload()
 
 void W_SniperRifle_FireBullet(float pSpread, float pDamage, float pHeadshotAddedDamage, float pForce, float pSpeed, float pLifetime, float pAmmo, float deathtype, float pBulletConstant)
 {
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_sniperrifle_reload_ammo)
+		{
+			self.clip_load -= pAmmo;
+			self.sniperrifle_load = self.clip_load;
+		}
+		else
+			self.ammo_nails -= pAmmo;
+	}
+
 	if(deathtype & HITTYPE_SECONDARY)
 		W_SetupShot (self, autocvar_g_antilag_bullets && pSpeed >= autocvar_g_antilag_bullets, 2, "weapons/campingrifle_fire2.wav", CHAN_WEAPON, autocvar_g_balance_sniperrifle_secondary_damage + autocvar_g_balance_sniperrifle_secondary_headshotaddeddamage);
 	else
@@ -84,18 +96,6 @@ void W_SniperRifle_FireBullet(float pSpread, float pDamage, float pHeadshotAdded
 
 	if (autocvar_g_casings >= 2)
 		SpawnCasing (((random () * 50 + 50) * v_right) - (v_forward * (random () * 25 + 25)) - ((random () * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, self);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_sniperrifle_reload_ammo)
-		{
-			self.clip_load -= pAmmo;
-			self.sniperrifle_load = self.clip_load;
-		}
-		else
-			self.ammo_nails -= pAmmo;
-	}
 }
 
 void W_SniperRifle_Attack()
diff --git a/qcsrc/server/w_uzi.qc b/qcsrc/server/w_uzi.qc
index 4693b58422..ebeedc2ec6 100644
--- a/qcsrc/server/w_uzi.qc
+++ b/qcsrc/server/w_uzi.qc
@@ -149,6 +149,18 @@ void uzi_fire1_02()
 void uzi_mode1_fire_auto()
 {
 	float uzi_spread;
+
+	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
+	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
+	{
+		if(autocvar_g_balance_uzi_reload_ammo)
+		{
+			self.clip_load -= autocvar_g_balance_uzi_sustained_ammo;
+			self.uzi_load = self.clip_load;
+		}
+		else
+			self.ammo_nails -= autocvar_g_balance_uzi_sustained_ammo;
+	}
 	
 	if (self.BUTTON_ATCK)
 		weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_uzi_sustained_refire, uzi_mode1_fire_auto);
@@ -186,18 +198,6 @@ void uzi_mode1_fire_auto()
 	
 	if (autocvar_g_casings >= 2) // casing code
 		SpawnCasing (((random () * 50 + 50) * v_right) - (v_forward * (random () * 25 + 25)) - ((random () * 5 - 70) * v_up), 2, vectoangles(v_forward),'0 250 0', 100, 3, self);
-
-	// if this weapon is reloadable, decrease its load. Else decrease the player's ammo
-	if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-	{
-		if(autocvar_g_balance_uzi_reload_ammo)
-		{
-			self.clip_load -= autocvar_g_balance_uzi_sustained_ammo;
-			self.uzi_load = self.clip_load;
-		}
-		else
-			self.ammo_nails -= autocvar_g_balance_uzi_sustained_ammo;
-	}
 }
 
 void uzi_mode1_fire_burst()
-- 
2.39.5