TUTORIAL: Leaving ammo in dropped mag (Insurgency Sandstorm style) Created 4 months ago2023-12-05 19:03:34 UTC by jonnymartini jonnymartini

Created 4 months ago2023-12-05 19:03:34 UTC by jonnymartini jonnymartini

Posted 4 months ago2023-12-05 19:03:34 UTC Post #348158
Hey folks!

Here's a quick and easy tutorial on how to implement a rushed gun reloading mechanism.
In a nutshell, when you reload, you lose your mag and any bullets left inside.
Nice feature if you want to make a realistic mode (have you ever removed bullets from large-size mags and put them back in another one after?? If so, you know it doesn't take 1 second to do).

I'm using the latest version of SamVanheer SDK (half-life updated)

First, open weapons_shared.cpp and go to Line 120.
You will see the following:
void CBasePlayerWeapon::ItemPostFrame()
{
    if ((m_fInReload) && (m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase()))
    {
        // complete the reload.
        int j = V_min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);

        // Add them to the clip
        m_iClip += j;
        m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j;
To better understand how this code works.
  • int j = we are looking for how many bullets we need in our current clip to get a full clip (or the maximum ammo amounts available)
  • V_min(a,b) = "j" will be either the necessary amount to make a full clip OR the remaining ammo amount left. (You can't get a full Glock clip if you only have 3 bullets in your inventory)
  • Now that we know "j" we add the amount to the current clip ( m_iClip += j; )
  • Obviously, we subtract the amount from our ammo inventory ( `m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j;` )
Now that you know how everything works let's build our new feature.

Before starting, let's see what we need:
  • If I have 12/17 bullets left in my Glock and I reload, I want to get 17 (max amount) if possible
  • If not possible, I want to get my only non-full mag. (If currently at 12/17 in my mag and I have 13 in my inventory, I'll get a mag with 13/17 and be at 0 in my inventory).
  • Any mag dropped is lost permanently. (If currently at 12/17 and I have 117 in my inventory, I'll be at 17/17 and 100, so I lost 12 bullets in my dropped mag).
To make it happen, we will need to use simple if and else statements.

Here's how it looks. (Replace the previous lines with these)
void CBasePlayerWeapon::ItemPostFrame()
{
    if ((m_fInReload) && (m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase()))
    {
        // get j count
        int j = V_min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);

        // Add a full clip and permanently remove the previous one if a full clip is available
        // m_iClip = MaxClip(); could work as well.
        if (m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] >= iMaxClip())
        {
            m_iClip += j;
            m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= iMaxClip();
        }
        // Add the only remaining clip (full or not) and permanently remove the previous one.
        else
        {
            m_iClip = m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType];
            m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] = 0;
        }
Now, the only thing to do is to ensure that none of your guns share the same type of ammo.
For Original Half-Life, you must create a specific 9mm ammo type for either the Glock (9mm_glock) or the mp5 (9mm_mp5).

With this feature, if you drop a mp5 mag, it shouldn't affect your 9mm mags.

To create Custom Ammo Types you can refer to *this tutorial*

I hope it helps!
Posted 4 months ago2023-12-07 01:24:24 UTC Post #348161
I don´t want to be that guy, but shouldn´t this be on the Tutorials section?
Posted 4 months ago2023-12-07 03:13:01 UTC Post #348162
New tutorials are always appreciated, so much so we have a special place for them: the TWHL wiki
Admer456 Admer456If it ain't broken, don't fox it!
Posted 4 months ago2023-12-07 13:01:59 UTC Post #348163
Oops. You are right.
I'll bump it there a bit later today.
Thanks!
You must be logged in to post a response.