AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [STOCK] give_user_weapon (https://forums.alliedmods.net/showthread.php?t=278123)

Craxor 01-25-2016 02:57

[STOCK] give_user_weapon
 
Give User Weapon


This is pro-variant of default native give_item()

What's the difference between mine and default one?
  • Check if user is alive
  • Check if user already have respective weapon and set only clip/ammo without giving him the weapon and throw error.
  • Ability to set Clip of weapon
  • Ability to set BPAmmo of the weapon
  • Return the name of weapon without weapon_ prefix!
  • Return properly the Weapon Entity ID you give
  • Use CSW_ constants to give an weapon to a player instead of weapon_ id.
  • Error with "native cs_set_weapon_ammo" has been resolved.


Authors Craxor and Bugsy.

OverView

Code:
/*      give_user_weapon( index, CSW_weapon, Clip, BPAmmo, szWeapon, maxchars )         @param index: The Player Index to give an weapon.         @param iWeaponTypeID: Weapon ID ( CSW_* )         @param iClip: Clip of ammo (bullets).         @param iBPAmmo: Back Pack ammo of the weapon.         @param szWeapon[]:(Optionaly) The weapon name pointer to receive the weapon name.                                               The Weapon name is without 'weapon_' prefix. Example: m4a1         @param maxchars: (Optionaly if you use szWeapon param ) szWeapon[] size. The value 20 is recommended for most weapons.                 @return: Weapon Entity ID */
 give_user_weapon( index , iWeaponTypeID , iClip=0 , iBPAmmo=0 , szWeapon[]="" , maxchars=0 )

Function
Spoiler



Required modules:
CStrike, Engine, Fun.

Usage Example:
Code:

// Give to a index a deagle with 1 clip and 0 back pack ammo in stock.
give_user_weapon( id, CSW_DEAGLE, 1, 0 );

Code:

// Give to a index 10 hegrenades:
give_user_weapon( id, CSW_HEGRENADE, 5 );

  • Here's a note about get weapon name parameter.


Credit's:
HamletEagle(for idea get_weaponname and remove lot of switchs)
Nubo, Lux0r - some little modifications
Bugsy - a lot of things and rewrite the code with lot of optimization's
AMX Mod Dev(i forgot he help me with old code)
addons_zz( Parameter's doc help )

Changelog:
1.0: First realease.
1.1: Return the CSW_ Weapon ID
1.2: Bugsy: code cleaning/optimization.
1.3: Bugsy: Added grenade support.
1.4: Added C4 support.
1.5: Bugsy&Craxor: Added Weapon Name support, bomb icon, can skip Clip parameter if is grenade.
1.6: Removed uppercase first char, re-write weapon name code, etc, etc ...
1.7: Removed redundant if/else, use "?" comparator.
1.8: Bugsy update.
1.9: Bugsy added an condition for filltering attempt of giving ammo/bpammo to a knife.

Mistrick 01-25-2016 04:40

Re: [STOCK] give_user_weapon
 
give_item() return weapon entity index, no need to search it.
Code:

new weapon = give_item( index, wp_id );
if(pev_valid(weapon)) cs_set_weapon_ammo( weapon, iClip );
cs_set_user_bpammo(index, iWeaponTypeID, iBPAmmo);


Depresie 01-25-2016 19:56

Re: [STOCK] give_user_weapon
 
question, does the clip capacity resets after reloading?

Craxor 01-26-2016 00:00

Re: [STOCK] give_user_weapon
 
@Mistick - This isn't work in 1nd case, if user has weapon -> if use give_item to retrieve weapon index this will give me an bpammo error, only on else{} statement can be done.


@Depresie - I'm not shourt i understand what you wanna say, if i reload weapon it will give me exactly Clip of Weapon you give with give_user_weapon? Answer is NO. it will give you default clip of weapon it have

Edit: I just use give_item in else{} statement and working fine, but in first cond(if user have the weapon you wanna give) it throw errors, so i will let as it is.

Edit2: Remover .inc file from attach, members can directly copy the stock into they plugins.

Bugsy 01-26-2016 19:12

Re: [STOCK] give_user_weapon
 
I made a few tweaks to it.
  • Made it return a useful value. The weapon ID is already known since it is being passed to the function, so why return it? Instead, return the entity ID since this is unknown information and the scripter may want to use it for other stuff. If the checks fail, the function returns -1.
  • Renamed wp_id variable to something that makes sense since it is holding the weapon name, not id.
  • Added additional checks to prevent errors. Weapon ID must fall within a range that will make get_weaponname() not error. Clip and bpammo must be >= 0.
  • Eliminated if-else and redundant code by getting the entity index in one line based on if the player has it already or if it is given to the player.
  • Final safety check to make sure an entity index is in the variable before using cs_set ammos.
  • Added grenade support.
  • Fixed C4 support and added code to conditionally give clip/bp ammo.
PHP Code:

give_user_weaponindex iWeaponTypeID iClip=iBPAmmo=)
{
    if ( !( 
CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip ) || ( iBPAmmo ) || !is_user_aliveindex ) )
        return -
1;
    
    new 
szWeaponName20 ] , iWeaponEntity GrenadeBits;
    
    
GrenadeBits = ( ( << CSW_HEGRENADE ) | ( << CSW_FLASHBANG ) | ( << CSW_SMOKEGRENADE ) | ( << CSW_C4 ) );
    
get_weaponnameiWeaponTypeID szWeaponName charsmaxszWeaponName ) );
    
    if ( ( 
iWeaponEntity user_has_weaponindex iWeaponTypeID ) ? find_ent_by_owner( -szWeaponName index ) : give_itemindex szWeaponName ) ) > )
    {
        if ( 
iClip && !( GrenadeBits & ( << iWeaponTypeID ) ) )
            
cs_set_weapon_ammoiWeaponEntity iClip );

        if ( 
iWeaponTypeID == CSW_C4 
            
cs_set_user_plantindex );
        else if ( 
iBPAmmo )
            
cs_set_user_bpammoindex iWeaponTypeID iBPAmmo ); 
    }
    
    return 
iWeaponEntity;



Craxor 01-27-2016 00:12

Re: [STOCK] give_user_weapon
 
Bugsy, thanks for code cleaning and optimization i will post in first topic :D

Edit: Your stock doesn't support CSW_HE/FL/SK if i'm right no?

I will add info in first topic.

Bugsy 01-27-2016 18:34

Re: [STOCK] give_user_weapon
 
I fixed it to make it work with grenades. The problem is/was that you only want to set bpammo for grenades, not weapon ammo.

Craxor 01-28-2016 01:05

Re: [STOCK] give_user_weapon
 
Tested and work fine, for giving a nade you need to do that:

// Example, give 10 hegrenades to a index:
Code:

give_user_weapon( id, CSW_HEGRENADE, _, 10);
By the way, i made a new version of supports also C4! I tested and working fine, but Bugsy, do you have some ideas for improve/optimize or i should let how is it:

Code:

give_user_weapon( index , iWeaponTypeID , iClip=0 , iBPAmmo=0 )
{
        if( !( CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip < 0 ) || ( iBPAmmo < 0 ) || !is_user_alive( index ) )
                return -1;
   
        new GrenadeBits = ( ( 1 << CSW_HEGRENADE ) | ( 1 << CSW_FLASHBANG ) | ( 1 << CSW_SMOKEGRENADE ) | ( 1 << CSW_C4 ) );

        new szWeaponName[ 20 ] , iWeaponEntity;
        get_weaponname( iWeaponTypeID , szWeaponName , charsmax( szWeaponName ) );
   
        if ( ( iWeaponEntity = user_has_weapon( index , iWeaponTypeID ) ? find_ent_by_owner( -1 , szWeaponName , index ) : give_item( index , szWeaponName ) ) > 0 )
        {
                if ( !( GrenadeBits & ( 1 << iWeaponTypeID ) ) )
                        cs_set_weapon_ammo( iWeaponEntity , iClip );

                if( iWeaponTypeID == CSW_C4 )
                        cs_set_user_plant( index, 1 )
       
                cs_set_user_bpammo( index , iWeaponTypeID , iBPAmmo );
        }
        return iWeaponEntity;
}


Bugsy 01-28-2016 18:36

Re: [STOCK] give_user_weapon
 
Looks good, I changed the following:
  • Added an else above cs_set_user_bpammo() since this is not needed for C4.
  • Added showbombicon=1 so the green c4 icon appears in the HUD.
  • Added code to conditionally call cs_set_weapon_ammo() and cs_set_user_bpammo() only if a value is specified.
This is up to you, but you could also add the ability to optionally get the weapons name, this could eliminate the need for the scripter to use another get_weaponname() call if this is needed.
PHP Code:


#include <amxmodx>
#include <fun>
#include <engine>
#include <cstrike>

public plugin_init() 
{
    
register_clcmd"say test" "GiveWeapon" )
}

public 
GiveWeaponid )
{
    new 
szWeapon20 ];
    
    
give_user_weapon(id CSW_M4A1 12 14 szWeapon charsmaxszWeapon ) );
    
client_printid print_chat "You were given a %s" szWeapon] );
}

give_user_weaponindex iWeaponTypeID iClip=iBPAmmo=szWeapon[]="" maxchars=)
{
    if ( !( 
CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip ) || ( iBPAmmo ) || !is_user_aliveindex ) )
        return -
1;
    
    new 
szWeaponName20 ] , iWeaponEntity GrenadeBits;
    
    
GrenadeBits = ( ( << CSW_HEGRENADE ) | ( << CSW_FLASHBANG ) | ( << CSW_SMOKEGRENADE ) | ( << CSW_C4 ) );
    
get_weaponnameiWeaponTypeID szWeaponName charsmaxszWeaponName ) );
    
    if ( ( 
iWeaponEntity user_has_weaponindex iWeaponTypeID ) ? find_ent_by_owner( -szWeaponName index ) : give_itemindex szWeaponName ) ) > )
    {
        if ( 
iClip && !( GrenadeBits & ( << iWeaponTypeID ) ) )
            
cs_set_weapon_ammoiWeaponEntity iClip );

        if ( 
iWeaponTypeID == CSW_C4 
            
cs_set_user_plantindex );
        else if ( 
iBPAmmo )
            
cs_set_user_bpammoindex iWeaponTypeID iBPAmmo ); 
            
        if ( 
maxchars )
            
copyszWeapon maxchars szWeaponName );
    }
    
    return 
iWeaponEntity;



Craxor 01-29-2016 01:30

Re: [STOCK] give_user_weapon
 
Note:

Edit: Just a problem, if you want take the name of hegrenade/sk/fl or c4, you should do something like that:

Code:
// Example of get weapon name on a smokegrenade: new WeaponName[20]; give_user_weapon( id, CSW_SMOKEGRENADE, _, 3, WeaponName, charsmax( WeaponName ) ); client_print( id, print_chat, " You got 3 %s", WeaponName );

You need to skip an parameter ( "iClip" ), as is not used.

( Only on Hegrenade, FlashBang,Smokegrenade & C4 )


All times are GMT -4. The time now is 09:56.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.