From 4ff469c32515d3cc9c551afce90b5dc6eab14bb8 Mon Sep 17 00:00:00 2001
From: Samual Lenks <samual@xonotic.org>
Date: Sun, 5 Jan 2014 10:54:01 -0500
Subject: [PATCH] Add debug in case a weapon causes negative ammo

---
 qcsrc/common/weapons/weapons.qc      | 26 +++++++++++++-------------
 qcsrc/common/weapons/weapons.qh      |  3 ++-
 qcsrc/server/weapons/weaponsystem.qc | 14 ++++++++++++++
 3 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/qcsrc/common/weapons/weapons.qc b/qcsrc/common/weapons/weapons.qc
index 425fe5f64a..628a70de5e 100644
--- a/qcsrc/common/weapons/weapons.qc
+++ b/qcsrc/common/weapons/weapons.qc
@@ -261,6 +261,19 @@ void W_RandomWeapons(entity e, float n)
 	e.weapons = result;
 }
 
+string GetAmmoPicture(.float ammotype)
+{
+	switch(ammotype)
+	{
+		case ammo_shells:  return "ammo_shells";
+		case ammo_nails:   return "ammo_nails";
+		case ammo_rockets: return "ammo_rockets";
+		case ammo_cells:   return "ammo_cells";
+		case ammo_fuel:    return "ammo_fuel";
+		default: return ""; // wtf, no ammo type?
+	}
+}
+
 #ifdef CSQC
 .float GetAmmoFieldFromNum(float i)
 {
@@ -275,19 +288,6 @@ void W_RandomWeapons(entity e, float n)
 	}
 }
 
-string GetAmmoPicture(.float ammotype)
-{
-	switch(ammotype)
-	{
-		case ammo_shells:  return "ammo_shells";
-		case ammo_nails:   return "ammo_nails";
-		case ammo_rockets: return "ammo_rockets";
-		case ammo_cells:   return "ammo_cells";
-		case ammo_fuel:    return "ammo_fuel";
-		default: return ""; // wtf, no ammo type?
-	}
-}
-
 float GetAmmoStat(.float ammotype)
 {
 	switch(ammotype)
diff --git a/qcsrc/common/weapons/weapons.qh b/qcsrc/common/weapons/weapons.qh
index ba2e8a15e1..720f65f6ea 100644
--- a/qcsrc/common/weapons/weapons.qh
+++ b/qcsrc/common/weapons/weapons.qh
@@ -72,9 +72,10 @@ string W_FixWeaponOrder_AllowIncomplete(string order);
 string W_FixWeaponOrder_ForceComplete(string order);
 void W_RandomWeapons(entity e, float n);
 
+string GetAmmoPicture(.float ammotype);
+
 #ifdef CSQC
 .float GetAmmoFieldFromNum(float i);
-string GetAmmoPicture(.float ammotype);
 float GetAmmoStat(.float ammotype);
 #endif
 
diff --git a/qcsrc/server/weapons/weaponsystem.qc b/qcsrc/server/weapons/weaponsystem.qc
index e99c450ec0..70b916cedb 100644
--- a/qcsrc/server/weapons/weaponsystem.qc
+++ b/qcsrc/server/weapons/weaponsystem.qc
@@ -813,7 +813,21 @@ void W_DecreaseAmmo(float ammo_use)
 		self.(weapon_load[self.weapon]) = self.clip_load;
 	}
 	else
+	{
 		self.(wep.ammo_field) -= ammo_use;
+		if(self.(wep.ammo_field) < 0)
+		{
+			backtrace(sprintf(
+				"W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
+				"Please notify Samual immediately with a copy of this backtrace!\n",
+				ammo_use,
+				wep.netname,
+				GetAmmoPicture(wep.ammo_field),
+				self.netname,
+				self.(wep.ammo_field)
+			));
+		}
+	}
 }
 
 // weapon reloading code
-- 
2.39.5