ReactiveDrop / reactivedrop_public_src

Alien Swarm: Reactive Drop game source code and issue tracker.
https://reactivedrop.com
131 stars 36 forks source link

How can I get the max clips that a marine can carry by Vscripts? #817

Closed IAFJaeger closed 2 months ago

IAFJaeger commented 3 months ago

We now have the function CBaseCombatWeapon::GetMaxClips in Vscript, but it only returns the weapon's max clips. The question is, if the marine is carrying 2 weapons which use the same ammo type, I don't know how to get the max clips that a marine can carry. For an example, if a marine carries a asw_weapon_prifle and a asw_weapon_rifle, then the max clips that the marine can carry is 10 because the 2 weapons use the same ammo type. But the function CBaseCombatWeapon::GetMaxClips only returns 5.

I wish there would be some vscripts functions like:

// ======================================
// A marine picks up ammo from something.
// ======================================

local hMarine      = Entities.FindByClassname( null, "asw_mairine" );
local hWeapon      = NetProps.GetPropEntityArray( hMarine, "m_hMyWeapons", 0 );
local nAmmoIndex   = hWeapon.GetAmmoIndexPrimary();             // we don't have this now
local nMaxAmmo     = hMarine.GetMaxAmmoCanCarry( nAmmoIndex );  // we don't have this now
local nCurrentAmmo = NetProps.GetPropIntArray( hMarine, "m_iAmmo", nAmmoIndex )

if( nCurrentAmmo < nMaxAmmo )
{
    if( nCurrentAmmo + hWeapon.GetDefaultClip1() >= nMaxAmmo )
    {
        local nAmount = nMaxAmmo - nCurrentAmmo;
        hMarine.GiveAmmo( nAmount, nAmmoIndex );
    }
    else
    {
        local nAmount = hWeapon.GetDefaultClip1();
        hMarine.GiveAmmo( nAmount, nAmmoIndex );
    }
}
IAFJaeger commented 2 months ago

I have found we can get the ammo type ( also called "ammo index" ) of a weapon like this:

local nAmmoIndex = NetProps.GetPropInt( hWeapon, "m_iPrimaryAmmoType" );

And we can define a function ourself like:

function GetMaxAmmoMarineCanCarry( hMarine, nAmmoType )
{
    local nMaxAmmo = 0;
    for( local nSlot = 0; nSlot < 3; nSlot += 1 )
    {
        local hWeapon = NetProps.GetPropEntityArray( hMarine, "m_hMyWeapons", nSlot )
        if( hWeapon && hWeapon.IsValid() ){
            if( nAmmoType == NetProps.GetPropInt( hWeapon, "m_iPrimaryAmmoType" ) )
            {
                nMaxAmmo += hWeapon.GetMaxClip1();
            }
            if( nAmmoType == NetProps.GetPropInt( hWeapon, "m_iSecondaryAmmoType" ) )
            {
                nMaxAmmo += hWeapon.GetMaxClip2();
            }
        }
    }
    return nMaxAmmo;
}