ham_get_user_weaponent [CSTRIKE]

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

This function returns the weapon entity index by player id weapon type, and optional weapon slot.

Code: Select all

#define SHOTGUNS_BITSUM ((1<<CSW_XM1014)|(1<<CSW_M3))
#define SMGS_BITSUM     ((1<<CSW_MAC10)|(1<<CSW_UMP45)|(1<<CSW_MP5NAVY)|(1<<CSW_TMP)|(1<<CSW_P90))
#define RIFFLES_BITSUM  ((1<<CSW_AUG)|(1<<CSW_GALIL)|(1<<CSW_FAMAS)|(1<<CSW_M249)|(1<<CSW_M4A1)|(1<<CSW_SG552)|(1<<CSW_AK47))
#define SNIPERS_BITSUM  ((1<<CSW_SCOUT)|(1<<CSW_SG550)|(1<<CSW_AWP)|(1<<CSW_G3SG1))

#define PRIMARIES_BITSUM    (SHOTGUNS_BITSUM|SMGS_BITSUM|RIFFLES_BITSUM|SNIPERS_BITSUM)
#define GUNS_BITSUM     ((1<<CSW_P228)|(1<<CSW_ELITE)|(1<<CSW_FIVESEVEN)|(1<<CSW_USP)|(1<<CSW_GLOCK18)|(1<<CSW_DEAGLE))
#define NADES_BITSUM    ((1<<CSW_HEGRENADE)|(1<<CSW_SMOKEGRENADE)|(1<<CSW_FLASHBANG))

#define MAX_ITEM_TYPES 6
// These offsets handle last weapon entity that the player gained
// If the player has more than one weapon in the same slot, 
// other weapons are linked to the last one with offset cbased m_pNext
// (weapon A is linked to B, weapon B is linked to C, etc..)
new const m_rgpPlayerItems_CBasePlayer[MAX_ITEM_TYPES] = {367,368,...} // added CBasePlayer to offset name because CWeaponBoc class has same offset name

// "weapon_..." offsets
#define LD_CBasePlayerItem  4

// This offset is used to link a weapon with another one
#define m_pNext             42 

// This offset handles CSW_... values of the weapons
#define m_iId               43  

ham_get_user_weaponent(id, iCswId, iSlot = 0)
{
    new iWeapon
    if( !iSlot )
    {
        if( PRIMARIES_BITSUM & (1<<iCswId) ) iSlot = 1
        else if( GUNS_BITSUM & (1<<iCswId) ) iSlot = 2
        else if( iCswId == CSW_KNIFE ) iSlot = 3
        else if( NADES_BITSUM & (1<<iCswId) ) iSlot = 4
        else if( iCswId == CSW_C4 ) iSlot = 5
    }

    // get the last weapon that the player gained in this slot
    iWeapon = get_pdata_cbase(id, m_rgpPlayerItems_CBasePlayer[iSlot])
    while( pev_valid(iWeapon) ) // >0 check should be enough, i let pev_valid for the tut
    {
        // Check if the weapon type (CSW_) is the one we are searching
        if( get_pdata_int(iWeapon, m_iId, LD_CBasePlayerItem) == iCswId ) // can use cs_get_weapon_id(iWeapon) instead
        {
            // return the weapon entity index
            return iWeapon
        }
        // if not, search if another weapon is linked to the previous one
        iWeapon = get_pdata_cbase(iWeapon, m_pNext, LD_CBasePlayerItem)
    }
    // return 0 on failure
    return 0
}
You can use this shortest but less efficient code :

Code: Select all

#define MAX_ITEM_TYPES 6
new const m_rgpPlayerItems_CBasePlayer[MAX_ITEM_TYPES] = {367,368,...}

#define LD_CBasePlayerItem    4
#define    m_pNext                42
#define    m_iId                43

ham_get_user_weaponent2(id, iCswId)
{
    new iWeapon
    for(new i=1; i<MAX_ITEM_TYPES; i++)
    {
        iWeapon = get_pdata_cbase(id, m_rgpPlayerItems_CBasePlayer[i])
        while( pev_valid(iWeapon) )
        {
            if( get_pdata_int(iWeapon, m_iId, LD_CBasePlayerItem) == iCswId )
            {
                return iWeapon
            }
            iWeapon = get_pdata_cbase(iWeapon, m_pNext, LD_CBasePlayerItem)
        }
    }
    return 0
} 
Or the often used only fakemeta way :

Code: Select all

fm_get_user_weaponent(id, iCswId)
{
    new szWeaponName[20]
    if( !get_weaponname(iCswId, szWeaponName, charsmax(szWeaponName)) )
        return FM_NULLENT

    new iWeapon = FM_NULLENT
    while(    (iWeapon = engfunc( EngFunc_FindEntityByString, iWeapon, "classname", szWeaponName )) > 0
            &&    pev(iWeapon, pev_owner) != id        )
    { /* do nothing */ }

    return iWeapon
} 
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

Sorry I don't understood, this is something like get_user_weapon?
This function returns the weapon entity index
This is the entity index, you need it for example to use cs_s/get_weapon_burst or cs_g/set_weapon_silen.
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

I was actually going to start looking into another method for this instead the the fakemeta way which i was currently using. Luckily I didn't have to look too long as you just posted this.

-----------------------

Oddly though, I came across this in someones code:

Code: Select all

new wEnt = ExecuteHam(Ham_Item_GetWeaponPtr, id, CSW_DEAGLE)
Anyone know if this would work as intended?

ham_const shows a different format for it's usage (could it's comments be wrong?):

Code: Select all

    /**
     * Description:     Returns the entity index if the item is a weapon, -1 otherwise.
     * Forward params:  function(this)
     * Return type:     Entity.
     * Execute Params:  ExecuteHam(Ham_Item_GetWeaponPtr, this)
     */
    Ham_Item_GetWeaponPtr,
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

I don't think so, it seems that you should use this function with the already entity index.

May be it's the same (or reverse) as get_pdata_cbase, i'm not sure though.

Something like :

iWeaponEnt = get_pdata_cbase(id, OFFSET, 5)

ExecuteHam(Ham_Item_GetWeaponPtr, iWeaponEnt) == get_pdata_int(id, OFFSET, 5)

More chances that it is something like index_to_pointer from vexdum
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

So far I understand ExecuteHam(Ham_Item_GetWeaponPtr, Entity) will return Entity ( not the pointer, the same provided entity ) if this one is a weapon, otherwise -1. Passing a player's id would return -1 since it's not a weapon.
User avatar
Archive-Poster
Posts: 1249
Gender:
United States of America

Re: ham_get_user_weaponent [CSTRIKE]

Post by Archive-Poster »

So basically it's a verification check to see if the ent id supplied is a weapon?
Yep, I tried and seems that it does nothing else.

In HLSDK :

Code: Select all

virtual CBasePlayerItem *GetWeaponPtr( void ) { return (CBasePlayerItem *)this; };
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests