]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/w_shotgun.qc
Properly check the necessary amount of ammo for reloading
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_shotgun.qc
index 267c75ffb60ea3f3ce7551aa8ed257f9936c9964..118fbf08855c04287deb444c6e4dae168f73096c 100644 (file)
@@ -3,11 +3,55 @@ REGISTER_WEAPON(SHOTGUN, w_shotgun, IT_SHELLS, 2, WEP_FLAG_NORMAL | WEP_TYPE_HIT
 #else
 #ifdef SVQC
 
-void W_Shotgun_DoReload()
+.float shotgun_load;
+
+void W_Shotgun_SetAmmoCounter()
+{
+       // set ammo counter to the weapon we have switched to
+       if(!autocvar_g_balance_shotgun_reload_ammo)
+               self.ammo_counter = 0; // also keeps the crosshair ammo from displaying
+       else
+               self.ammo_counter = self.shotgun_load;
+}
+
+void W_Shotgun_ReloadedAndReady()
 {
+       float t;
+
+       // now do the ammo maths
+       self.ammo_counter = self.old_ammo_counter; // restore ammo counter, in case we still had ammo in the weapon while reloading
+       while(self.ammo_counter < autocvar_g_balance_shotgun_reload_ammo && self.ammo_shells) // make sure we don't add more than the amount of ammo we have
+       {
+               self.ammo_counter += 1;
+               self.ammo_shells -= 1;
+       }
+       self.shotgun_load = self.ammo_counter;
+
+       t = ATTACK_FINISHED(self) - autocvar_g_balance_shotgun_reload_time - 1;
+       ATTACK_FINISHED(self) = t;
        w_ready();
-       if(W_Reload(self.ammo_shells))
+}
+
+void W_Shotgun_Reload()
+{
+       // reloading is disabled for this weapon
+       if(!autocvar_g_balance_shotgun_reload_ammo)
                return;
+
+       float t;
+
+       if(!W_ReloadCheck(self.ammo_shells, autocvar_g_balance_shotgun_primary_ammo, 0))
+               return;
+
+       sound (self, CHAN_WEAPON2, "weapons/campingrifle_reload.wav", VOL_BASE, ATTN_NORM);
+
+       t = max(time, ATTACK_FINISHED(self)) + autocvar_g_balance_shotgun_reload_time + 1;
+       ATTACK_FINISHED(self) = t;
+
+       weapon_thinkf(WFRAME_RELOAD, autocvar_g_balance_shotgun_reload_time, W_Shotgun_ReloadedAndReady);
+
+       self.old_ammo_counter = self.ammo_counter;
+       self.ammo_counter = -1;
 }
 
 void W_Shotgun_Attack (void)
@@ -22,9 +66,7 @@ void W_Shotgun_Attack (void)
        float   bulletconstant;
        local entity flash;
 
-       if(self.ammo_counter <= 0)
-               W_Shotgun_DoReload();
-       if(self.ammo_counter < 0)
+       if(autocvar_g_balance_shotgun_reload_ammo && self.ammo_counter <= 0)
                return; // reloading, so we are done
 
        ammoamount = autocvar_g_balance_shotgun_primary_ammo;
@@ -40,7 +82,12 @@ void W_Shotgun_Attack (void)
                fireBallisticBullet(w_shotorg, w_shotdir, spread, bulletspeed, 5, d, 0, f, WEP_SHOTGUN, 0, 1, bulletconstant);
        endFireBallisticBullet();
        if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-               self.ammo_shells = self.ammo_shells - ammoamount;
+       {
+               if(!autocvar_g_balance_shotgun_reload_ammo)
+                       self.ammo_shells = self.ammo_shells - ammoamount;
+               else
+                       self.shotgun_load -= ammoamount;
+       }
 
        pointparticles(particleeffectnum("shotgun_muzzleflash"), w_shotorg, w_shotdir * 1000, autocvar_g_balance_shotgun_primary_ammo);
 
@@ -123,8 +170,8 @@ float w_shotgun(float req)
                        self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
        else if (req == WR_THINK)
        {
-               if(self.ammo_counter < 0) // forced reload (e.g. because interrupted)
-            self.wish_reload = 1;
+               if(autocvar_g_balance_shotgun_reload_ammo && self.ammo_counter <= 0) // forced reload
+                       W_Shotgun_Reload();
                else
                {
                        if (self.BUTTON_ATCK)
@@ -153,7 +200,7 @@ float w_shotgun(float req)
                 if(self.weaponentity.state == WS_READY)
                 {
                     self.wish_reload = 0;
-                    W_Reload(self.ammo_shells);
+                    W_Shotgun_Reload();
                 }
             }
         }
@@ -169,7 +216,10 @@ float w_shotgun(float req)
                precache_sound ("weapons/shotgun_melee.wav");
        }
        else if (req == WR_SETUP)
+       {
                weapon_setup(WEP_SHOTGUN);
+               W_Shotgun_SetAmmoCounter();
+       }
        else if (req == WR_CHECKAMMO1)
                return self.ammo_shells >= autocvar_g_balance_shotgun_primary_ammo;
        else if (req == WR_CHECKAMMO2)