]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/w_uzi.qc
Merge branch 'master' into mirceakitsune/universal_reload_system
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_uzi.qc
index 0e563405a1fa858ebdbd6719868e30859fd89eef..360c8913345dee4cca9b38adfb9a0b15573ab357 100644 (file)
@@ -3,8 +3,63 @@ REGISTER_WEAPON(UZI, w_uzi, IT_NAILS, 3, WEP_FLAG_NORMAL | WEP_TYPE_HITSCAN, BOT
 #else
 #ifdef SVQC
 
+// weapon load persistence, for weapons that support reloading
+.float uzi_load;
+
+void W_UZI_SetAmmoCounter()
+{
+       // set clip_load to the weapon we have switched to, if the gun uses reloading
+       if(!autocvar_g_balance_uzi_reload_ammo)
+               self.clip_load = 0; // also keeps crosshair ammo from displaying
+       else
+       {
+               self.clip_load = self.uzi_load;
+               self.clip_size = autocvar_g_balance_uzi_reload_ammo; // for the crosshair ammo display
+       }
+}
+
+void W_UZI_ReloadedAndReady()
+{
+       float t;
+
+       // now do the ammo transfer
+       self.clip_load = self.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
+       while(self.clip_load < autocvar_g_balance_uzi_reload_ammo && self.ammo_nails) // make sure we don't add more ammo than we have
+       {
+               self.clip_load += 1;
+               self.ammo_nails -= 1;
+       }
+       self.uzi_load = self.clip_load;
+
+       t = ATTACK_FINISHED(self) - autocvar_g_balance_uzi_reload_time - 1;
+       ATTACK_FINISHED(self) = t;
+       w_ready();
+}
+
+void W_UZI_Reload()
+{
+       // return if reloading is disabled for this weapon
+       if(!autocvar_g_balance_uzi_reload_ammo)
+               return;
+
+       if(!W_ReloadCheck(self.ammo_nails, min(max(autocvar_g_balance_uzi_sustained_ammo, autocvar_g_balance_uzi_first_ammo), autocvar_g_balance_uzi_burst_ammo)))
+               return;
+
+       float t;
+
+       sound (self, CHAN_WEAPON2, "weapons/reload.wav", VOL_BASE, ATTN_NORM);
+
+       t = max(time, ATTACK_FINISHED(self)) + autocvar_g_balance_uzi_reload_time + 1;
+       ATTACK_FINISHED(self) = t;
+
+       weapon_thinkf(WFRAME_RELOAD, autocvar_g_balance_uzi_reload_time, W_UZI_ReloadedAndReady);
+
+       self.old_clip_load = self.clip_load;
+       self.clip_load = -1;
+}
+
 // leilei's fancy muzzleflash stuff
-void Uzi_Flash_Go() 
+void UZI_Flash_Go() 
 {      
        self.frame = self.frame + 2;
        self.scale = self.scale * 0.5;
@@ -30,7 +85,7 @@ void UziFlash()
        setmodel(self.muzzle_flash, "models/uziflash.md3"); // precision set below
        
        self.muzzle_flash.scale = 0.75;
-       self.muzzle_flash.think = Uzi_Flash_Go;
+       self.muzzle_flash.think = UZI_Flash_Go;
        self.muzzle_flash.nextthink = time + 0.02;
        self.muzzle_flash.frame = 2;
        self.muzzle_flash.alpha = 0.75;
@@ -39,15 +94,8 @@ void UziFlash()
        self.muzzle_flash.owner = self;
 }
 
-void W_Uzi_Attack (float deathtype)
+void W_UZI_Attack (float deathtype)
 {
-       if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
-       {
-               if (self.misc_bulletcounter == 1)
-                       self.ammo_nails = self.ammo_nails - autocvar_g_balance_uzi_first_ammo;
-               else
-                       self.ammo_nails = self.ammo_nails - autocvar_g_balance_uzi_sustained_ammo;
-       }
        W_SetupShot (self, autocvar_g_antilag_bullets && autocvar_g_balance_uzi_speed >= autocvar_g_antilag_bullets, 0, "weapons/uzi_fire.wav", CHAN_WEAPON, ((self.misc_bulletcounter == 1) ? autocvar_g_balance_uzi_first_damage : autocvar_g_balance_uzi_sustained_damage));
        if (!g_norecoil)
        {
@@ -72,6 +120,26 @@ void W_Uzi_Attack (float deathtype)
        // casing code
        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_uzi_reload_ammo)
+               {
+                       if (self.misc_bulletcounter == 1)
+                               self.clip_load -= autocvar_g_balance_uzi_first_ammo;
+                       else
+                               self.clip_load -= autocvar_g_balance_uzi_sustained_ammo;
+                       self.uzi_load = self.clip_load;
+               }
+               else
+               {
+                       if (self.misc_bulletcounter == 1)
+                               self.ammo_nails -= autocvar_g_balance_uzi_first_ammo;
+                       else
+                               self.ammo_nails -= autocvar_g_balance_uzi_sustained_ammo;
+               }
+       }
 }
 
 // weapon frames
@@ -91,7 +159,7 @@ void uzi_fire1_02()
                        return;
                }
                self.misc_bulletcounter = self.misc_bulletcounter + 1;
-               W_Uzi_Attack(WEP_UZI);
+               W_UZI_Attack(WEP_UZI);
                weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_uzi_sustained_refire, uzi_fire1_02);
        }
        else
@@ -102,6 +170,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);
@@ -139,10 +219,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 not(self.items & IT_UNLIMITED_WEAPON_AMMO)   
-               self.ammo_nails = self.ammo_nails - autocvar_g_balance_uzi_sustained_ammo;
-       
 }
 
 void uzi_mode1_fire_burst()
@@ -183,6 +259,7 @@ void spawnfunc_weapon_machinegun(); // defined in t_items.qc
 
 float w_uzi(float req)
 {
+       float ammo_amount;
        if (req == WR_AIM)
                if(vlen(self.origin-self.enemy.origin) < 3000 - bound(0, skill, 10) * 200)
                        self.BUTTON_ATCK = bot_aim(1000000, 0, 0.001, FALSE);
@@ -192,7 +269,9 @@ float w_uzi(float req)
                }
        else if (req == WR_THINK)
        {
-               if(autocvar_g_balance_uzi_mode == 1)
+               if(autocvar_g_balance_uzi_reload_ammo && self.clip_load < min(max(autocvar_g_balance_uzi_sustained_ammo, autocvar_g_balance_uzi_first_ammo), autocvar_g_balance_uzi_burst_ammo)) // forced reload
+                       W_UZI_Reload();
+               else if(autocvar_g_balance_uzi_mode == 1)
                {
                        if (self.BUTTON_ATCK)
                        if (weapon_prepareattack(0, 0))
@@ -210,9 +289,18 @@ float w_uzi(float req)
                                        w_ready();
                                        return FALSE;
                                }
-                               
-                               if not(self.items & IT_UNLIMITED_WEAPON_AMMO)   
-                                       self.ammo_nails = self.ammo_nails - autocvar_g_balance_uzi_burst_ammo;
+
+                               // 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_burst_ammo;
+                                               self.uzi_load = self.clip_load;
+                                       }
+                                       else
+                                               self.ammo_nails -= autocvar_g_balance_uzi_burst_ammo;
+                               }
 
                                self.misc_bulletcounter = autocvar_g_balance_uzi_burst * -1;
                                uzi_mode1_fire_burst();
@@ -225,7 +313,7 @@ float w_uzi(float req)
                        if (weapon_prepareattack(0, 0))
                        {
                                self.misc_bulletcounter = 1;
-                               W_Uzi_Attack(WEP_UZI); // sets attack_finished
+                               W_UZI_Attack(WEP_UZI); // sets attack_finished
                                weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_uzi_sustained_refire, uzi_fire1_02);
                        }
 
@@ -233,7 +321,7 @@ float w_uzi(float req)
                        if (weapon_prepareattack(1, 0))
                        {
                                self.misc_bulletcounter = 1;
-                               W_Uzi_Attack(WEP_UZI | HITTYPE_SECONDARY); // sets attack_finished
+                               W_UZI_Attack(WEP_UZI | HITTYPE_SECONDARY); // sets attack_finished
                                weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_uzi_first_refire, w_ready);
                        }
                }
@@ -245,19 +333,54 @@ float w_uzi(float req)
                precache_model ("models/weapons/v_uzi.md3");
                precache_model ("models/weapons/h_uzi.iqm");
                precache_sound ("weapons/uzi_fire.wav");
+               precache_sound ("weapons/reload.wav");
        }
        else if (req == WR_SETUP)
+       {
                weapon_setup(WEP_UZI);
+               W_UZI_SetAmmoCounter();
+       }
        else if (req == WR_CHECKAMMO1)
+       {
                if(autocvar_g_balance_uzi_mode == 1)
-                       return self.ammo_nails >= autocvar_g_balance_uzi_sustained_ammo;
+                       ammo_amount = self.ammo_nails >= autocvar_g_balance_uzi_sustained_ammo;
                else
-                       return self.ammo_nails >= autocvar_g_balance_uzi_first_ammo;
+                       ammo_amount = self.ammo_nails >= autocvar_g_balance_uzi_first_ammo;
+
+               if(autocvar_g_balance_uzi_reload_ammo)
+               {
+                       if(autocvar_g_balance_uzi_mode == 1)
+                               ammo_amount += self.uzi_load >= autocvar_g_balance_uzi_sustained_ammo;
+                       else
+                               ammo_amount += self.uzi_load >= autocvar_g_balance_uzi_first_ammo;
+               }
+               return ammo_amount;
+       }
        else if (req == WR_CHECKAMMO2)
+       {
                if(autocvar_g_balance_uzi_mode == 1)
-                       return self.ammo_nails >= autocvar_g_balance_uzi_burst_ammo;
+                       ammo_amount = self.ammo_nails >= autocvar_g_balance_uzi_burst_ammo;
                else
-                       return self.ammo_nails >= autocvar_g_balance_uzi_first_ammo;
+                       ammo_amount = self.ammo_nails >= autocvar_g_balance_uzi_first_ammo;
+
+               if(autocvar_g_balance_uzi_reload_ammo)
+               {
+                       if(autocvar_g_balance_uzi_mode == 1)
+                               ammo_amount += self.uzi_load >= autocvar_g_balance_uzi_burst_ammo;
+                       else
+                               ammo_amount += self.uzi_load >= autocvar_g_balance_uzi_first_ammo;
+               }
+               return ammo_amount;
+       }
+       else if (req == WR_RESETPLAYER)
+       {
+               // all weapons must be fully loaded when we spawn
+               self.uzi_load = autocvar_g_balance_uzi_reload_ammo;
+       }
+       else if (req == WR_RELOAD)
+       {
+               W_UZI_Reload();
+       }
        return TRUE;
 };
 #endif