Raised This Month: $12 Target: $400
 3% 

[STOCK] give_user_weapon


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-25-2016 , 02:57   [STOCK] give_user_weapon
Reply With Quote #1

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.

Last edited by Craxor; 05-13-2017 at 04:57.
Craxor is offline
Send a message via ICQ to Craxor
Mistrick
Senior Member
Join Date: Aug 2012
Location: Russia
Old 01-25-2016 , 04:40   Re: [STOCK] give_user_weapon
Reply With Quote #2

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);
Mistrick is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 01-25-2016 , 19:56   Re: [STOCK] give_user_weapon
Reply With Quote #3

question, does the clip capacity resets after reloading?
__________________
Depresie is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-26-2016 , 00:00   Re: [STOCK] give_user_weapon
Reply With Quote #4

@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.

Last edited by Craxor; 01-26-2016 at 01:01. Reason: Information
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-26-2016 , 19:12   Re: [STOCK] give_user_weapon
Reply With Quote #5

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;

__________________

Last edited by Bugsy; 01-28-2016 at 18:50.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-27-2016 , 00:12   Re: [STOCK] give_user_weapon
Reply With Quote #6

Bugsy, thanks for code cleaning and optimization i will post in first topic

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

I will add info in first topic.

Last edited by Craxor; 01-27-2016 at 00:57.
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-27-2016 , 18:34   Re: [STOCK] give_user_weapon
Reply With Quote #7

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.
__________________

Last edited by Bugsy; 01-27-2016 at 21:58.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-28-2016 , 01:05   Re: [STOCK] give_user_weapon
Reply With Quote #8

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;
}

Last edited by Craxor; 01-28-2016 at 09:48. Reason: forgot "and" :)
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-28-2016 , 18:36   Re: [STOCK] give_user_weapon
Reply With Quote #9

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;

__________________

Last edited by Bugsy; 01-28-2016 at 18:53.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-29-2016 , 01:30   Re: [STOCK] give_user_weapon
Reply With Quote #10

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 )

Last edited by Craxor; 02-02-2016 at 04:56.
Craxor is offline
Send a message via ICQ to Craxor
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:16.


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