]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/cl_weaponsystem.qc
Attempt to further simplify the reload code, as requested. First part of the first...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_weaponsystem.qc
index fd1da3162973fed859974ec84b7362eb1245bc72..4465c190b7d8d7e6f4675ab1eb5d09fe59a99c50 100644 (file)
@@ -928,13 +928,6 @@ float client_hasweapon(entity cl, float wpn, float andammo, float complain)
                                f = weapon_action(wpn, WR_CHECKAMMO1);
                                f = f + weapon_action(wpn, WR_CHECKAMMO2);
 
-                               // allow switching to reloadable weapons, even if we're out of ammo, since the weapon itself
-                               // might still be loaded. The reload code takes care of complaining and forced switching
-                               entity e;
-                               e = get_weaponinfo(self.switchweapon);
-                               if(cvar(strcat("g_balance_", e.netname, "_reload_ammo")))
-                                       f = 1;
-
                                // always allow selecting the Mine Layer if we placed mines, so that we can detonate them
                                local entity mine;
                                if(wpn == WEP_MINE_LAYER)
@@ -1054,13 +1047,6 @@ float weapon_prepareattack_checkammo(float secondary)
        if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
        if (!weapon_action(self.weapon, WR_CHECKAMMO1 + secondary))
        {
-               // allow switching to reloadable weapons, even if we're out of ammo, since the weapon itself
-               // might still be loaded. The reload code takes care of complaining and forced switching
-               entity e;
-               e = get_weaponinfo(self.weapon);
-               if(cvar(strcat("g_balance_", e.netname, "_reload_ammo")))
-                       return FALSE;
-
                // always keep the Mine Layer if we placed mines, so that we can detonate them
                local entity mine;
                if(self.weapon == WEP_MINE_LAYER)
@@ -1626,19 +1612,21 @@ void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"), FALSE)
 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"), FALSE)
 
-// shared weapon reload code
+// weapon reloading code
+
+..float reload_ammo_player;
+.float reload_ammo_amount, reload_ammo_min, reload_time;
 .float reload_complain;
+.string reload_sound;
+
 float W_ReloadCheck(float ammo_amount, float ammo_shot)
 {
-       entity e;
-       e = get_weaponinfo(self.weapon);
-
        // our weapon is fully loaded, no need to reload
-       if (self.clip_load >= cvar(strcat("g_balance_", e.netname, "_reload_ammo")))
-               return 0;
+       if (self.clip_load >= self.reload_ammo_amount)
+               return FALSE;
 
        // no ammo, so nothing to load
-       if(!ammo_amount)
+       if(!ammo_amount && ammo_shot)
        {
                if(clienttype(self) == CLIENTTYPE_REAL && self.reload_complain < time)
                {
@@ -1646,23 +1634,82 @@ float W_ReloadCheck(float ammo_amount, float ammo_shot)
                        sprint(self, strcat("You don't have enough ammo to reload the ^2", W_Name(self.weapon), "\n"));
                        self.reload_complain = time + 1;
                }
-               // switch away if the loaded amount of ammo is not enough to keep using this weapon
-               if(self.clip_load < ammo_shot)
+               // switch away if the amount of ammo is not enough to keep using this weapon
+               if not(weapon_action(self.weapon, WR_CHECKAMMO1) + weapon_action(self.weapon, WR_CHECKAMMO2))
                {
                        self.clip_load = -1; // reload later
                        W_SwitchToOtherWeapon(self);
                }
-               return 0;
+               return FALSE;
        }
 
        if (self.weaponentity)
        {
                if (self.weaponentity.wframe == WFRAME_RELOAD)
-                       return 0;
+                       return FALSE;
 
                // allow switching away while reloading, but this will cause a new reload!
                self.weaponentity.state = WS_READY;
        }
 
-       return 1;
+       return TRUE;
+}
+
+void W_ReloadEnd()
+{
+       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
+
+       // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
+       if(!self.reload_ammo_min)
+               self.clip_load = self.reload_ammo_amount;
+       else
+       {
+               while(self.clip_load < self.reload_ammo_amount && self.(self.reload_ammo_player)) // make sure we don't add more ammo than we have
+               {
+                       self.clip_load += 1;
+                       self.(self.reload_ammo_player) -= 1;
+               }
+       }
+       self.weapon_load[self.weapon] = self.clip_load;
+
+       t = ATTACK_FINISHED(self) - self.reload_time - 1;
+       ATTACK_FINISHED(self) = t;
+       w_ready();
+}
+
+void W_ReloadStart()
+{
+       // return if reloading is disabled for this weapon
+       if(!self.reload_ammo_amount)
+               return;
+
+       if(!W_ReloadCheck(self.(self.reload_ammo_player), self.reload_ammo_min))
+               return;
+
+       float t;
+
+       sound (self, CHAN_WEAPON2, self.reload_sound, VOL_BASE, ATTN_NORM);
+
+       t = max(time, ATTACK_FINISHED(self)) + self.reload_time + 1;
+       ATTACK_FINISHED(self) = t;
+
+       weapon_thinkf(WFRAME_RELOAD, self.reload_time, W_ReloadEnd);
+
+       self.old_clip_load = self.clip_load;
+       self.clip_load = -1;
+}
+
+void W_Reload(.float sent_ammo_player, float sent_ammo_min, float sent_ammo_amount, float sent_time, string sent_sound)
+{
+       self.reload_ammo_player = sent_ammo_player;
+       self.reload_ammo_min = sent_ammo_min;
+       self.reload_ammo_amount = sent_ammo_amount;
+       self.reload_time = sent_time;
+       self.reload_sound = sent_sound;
+
+       W_ReloadStart();
 }
\ No newline at end of file