Raised This Month: $32 Target: $400
 8% 

[STOCK] give_user_weapon


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-29-2016 , 07:03   Re: [STOCK] give_user_weapon
Reply With Quote #11

You don't need replace(), use szWeaponName[7] in copy(). I would leave out the uppercase first char, that's just me though.
__________________

Last edited by Bugsy; 01-29-2016 at 07:04.
Bugsy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-29-2016 , 08:32   Re: [STOCK] give_user_weapon
Reply With Quote #12

Make GrenadeBits a static constant.
klippy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-29-2016 , 10:32   Re: [STOCK] give_user_weapon
Reply With Quote #13

Quote:
Originally Posted by KliPPy View Post
Make GrenadeBits a static constant.
I don't think static is necessary here, it's a single cell in a function that likely will not be hammered with usage. I would change it to a const, though.
__________________
Bugsy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-29-2016 , 11:28   Re: [STOCK] give_user_weapon
Reply With Quote #14

Quote:
Originally Posted by Bugsy View Post
I don't think static is necessary here, it's a single cell in a function that likely will not be hammered with usage. I would change it to a const, though.
Yeah, while writing that post I was thinking if static is necessary. I think that const on local variables implies static. Anyway, single cell const should make better code than a variable.

Last edited by klippy; 01-29-2016 at 11:29.
klippy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-30-2016 , 02:20   Re: [STOCK] give_user_weapon
Reply With Quote #15

Done.

Tested and works ( see attach example ).
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-30-2016 , 07:23   Re: [STOCK] give_user_weapon
Reply With Quote #16

Did you re-arrange stuff so that you can pass bpammo param as clip for grenades? If so, I would do the below instead. Though, I don't know if you want to do this or not as it may confuse people who know grenades are stored as a bpammo. They may try to pass the ammo as bpammo which will result in them getting none. You could do something like max( iClip , iBPAmmo ) which will make it grab whichever one is not zero, or whichever is larger. But I think sticking to bpammo param for grenades is better and more consistent.
PHP Code:
give_user_weaponindex iWeaponTypeID iClip=iBPAmmo=szWeapon[]="" maxchars=)
{
    if ( !( 
CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip ) || ( iBPAmmo ) || !is_user_aliveindex ) )
        return -
1;
        
    const 
GrenadeBits = ( ( << CSW_HEGRENADE ) | ( << CSW_FLASHBANG ) | ( << CSW_SMOKEGRENADE ) | ( << CSW_C4 ) );
    new 
szWeaponName20 ] , iWeaponEntity bool:bIsGrenade;
    
    
get_weaponnameiWeaponTypeID szWeaponName charsmaxszWeaponName ) );
    
    if ( ( 
iWeaponEntity user_has_weaponindex iWeaponTypeID ) ? find_ent_by_owner( -szWeaponName index ) : give_itemindex szWeaponName ) ) > )
    {
        
bIsGrenade bool:!!( GrenadeBits & ( << iWeaponTypeID ) );
        
        if ( 
iClip && !bIsGrenade )
            
cs_set_weapon_ammoiWeaponEntity iClip );

        if ( 
iWeaponTypeID == CSW_C4 
            
cs_set_user_plantindex );
        else if ( ( 
bIsGrenade && iClip ) || ( !bIsGrenade && iBPAmmo ) )
            
cs_set_user_bpammoindex iWeaponTypeID bIsGrenade iClip iBPAmmo ); 
            
        if ( 
maxchars )
            
copyszWeapon maxchars szWeaponName );
    }
    
    return 
iWeaponEntity;

For your post above about getting weapon name without specifying clip, you can skip the param, or specify it directly:
PHP Code:
new szWeaponName20 ];
    
give_user_weapon(id CSW_C4 , .szWeapon=szWeaponName , .maxchars=charsmaxszWeaponName ) );
client_printid print_chat "You were given a %s" szWeaponName] ); 
__________________

Last edited by Bugsy; 01-30-2016 at 11:20.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-31-2016 , 01:59   Re: [STOCK] give_user_weapon
Reply With Quote #17

Now i re-arrange so that can use bpammo & iclip parameter for grenades, will be readed anyway as BpAmmo.
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-31-2016 , 02:09   Re: [STOCK] give_user_weapon
Reply With Quote #18

Hi, nice stock, but it is missing from its documentation:
Code:
@return: Weapon Entity ID
@return: Weapon Entity ID. On failure -1, due the user being dead or an invalid parameter.
And before that 'return -1;' could be some useful information as:
Code:
    if ( !( CSW_P228 <= iWeaponTypeID <= CSW_P90 ) || ( iClip < 0 ) || ( iBPAmmo < 0 ) || !is_user_alive( index ) )     {         log_amx( "The item could not given. iWeaponTypeID: %d, iClip: %d, iBPAmmo: %d, is_user_alive? %s",                 iWeaponTypeID, iClip, iBPAmmo, is_user_alive( index ) ? "true" : "false" );         return -1;     }
But I am not sure about if this function should log such events.

These lines look trickier:
Code:
        @param szWeapon: Weapon Name you give, without weapon_ prefix + with first letter by UpperCase.
        @maxchars: lengh of szWeapon, 20 recomandated.
Because you do not provide them to function, they recive the weapon name by the given maxchars. So, what do you think about this:
Code:
        @param szWeapon[]: The weapon name pointer to receive the weapon name for the given iWeaponTypeID. The Weapon Name is 
                             without 'weapon_' prefix with its first letter uppercase. Example: Glock
        @param maxchars: The szWeapon[] pointer max name length accepted. The value 20 is recommended for most weapons.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 01-31-2016 at 15:33. Reason: spelling fix
addons_zz is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-31-2016 , 02:20   Re: [STOCK] give_user_weapon
Reply With Quote #19

Thank , i updadated the doc, about adding an log message if is not respect requrimenets for taking a weapon, answer is No. I will not add something there.
__________________
Project: Among Us
Craxor is offline
Send a message via ICQ to Craxor
Old 01-31-2016, 02:57
addons_zz
This message has been deleted by addons_zz. Reason: repeated post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-31-2016 , 07:22   Re: [STOCK] give_user_weapon
Reply With Quote #20

You are setting bpammo for c4 and you shouldn't. You should try to avoid calling things that have no impact and also avoid redundant code, you now have set_bpammo() twice when this can be accomplished with 1 like I have in my code.

I agree, I don't think the log should be printed on fail. The function returns -1 in this case, the scripter should be handling this. That would be more of a debugging thing.

I think this works nicely:
PHP Code:
#include <amxmodx>
#include <fun>
#include <engine>
#include <cstrike>

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

public 
GiveWeaponid )
{
    
//Gives M4A1 with 12 clip, 0 bpammo
    
give_user_weaponid CSW_M4A1 12 );
    
    
//Gives AK with full clip (default) and 12 bpammo
    
give_user_weaponid CSW_AK47 12 );
    
    
//Gives C4
    
give_user_weaponid CSW_C4 );
    
    
//Gives 1 grenade
    
give_user_weaponid CSW_HEGRENADE );
    
    
//Gives 2 flashbang
    
give_user_weaponid CSW_FLASHBANG );
    
    
//Gives 3 smoke
    
give_user_weaponid CSW_SMOKEGRENADE );
}

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 bool:bIsGrenade;
    
    
GrenadeBits = ( ( << CSW_HEGRENADE ) | ( << CSW_FLASHBANG ) | ( << CSW_SMOKEGRENADE ) | ( << CSW_C4 ) );
    
    if ( ( 
bIsGrenade bool:!!( GrenadeBits & ( << iWeaponTypeID ) ) ) )
        
iClip clampiClip iClip iBPAmmo );
    
    
get_weaponnameiWeaponTypeID szWeaponName charsmaxszWeaponName ) );
    
    if ( ( 
iWeaponEntity user_has_weaponindex iWeaponTypeID ) ? find_ent_by_owner( -szWeaponName index ) : give_itemindex szWeaponName ) ) > )
    {
        if ( 
iClip && !bIsGrenade )
            
cs_set_weapon_ammoiWeaponEntity iClip );

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

__________________

Last edited by Bugsy; 01-31-2016 at 10:19.
Bugsy is offline
Old 02-04-2016, 02:36
Craxor
This message has been deleted by Craxor.
Reply



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 00:14.


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