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

[SNIPPET] Properly hook and block weapons pickup


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 02-09-2014 , 06:47   [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #1

Lot of time i see people wondering how to know which weapon is picked up by players when those weapons are packed in a weaponbox.
Also, some people keep hooking FM_Touch or pfn_touch when we know those forwards are called very ofen and are bad to hook for servers performances.

I've decided to use engine register_touch system rather than hooking Ham_Touch because engine is also filtering the toucher so it is better for performances.
With ham you would need to check toucher index range to be sure it is a player.
If in your plugin you are creating custom weaponbox or armouries, first make sure you use "weaponbox" and "armoury_entity" as classes to create entities and then you can set your custom class name, that custom name you gonna use in register_touch.

I've put some comments in code.
If you have more questions i gonna answer later in thread, and put back important answer in main post.

Last, if you want to block pickup, return PLUGIN_HANDLED, if you want to let the game decide, return PLUGIN_CONTINUE.

PHP Code:
#include < amxmodx >
#include < engine >
#include < hamsandwich >
#include < cstrike >

const XO_CWEAPONBOX 4;
new const 
m_rgpPlayerItems_CWeaponBox[6] = {34,35,...};

public 
plugin_init()
{
    new const 
PLAYER_CLASS[] = "player";
    
register_touch("armoury_entity"PLAYER_CLASS"OnPlayerTouchArmoury");
    
register_touch("weaponbox"PLAYER_CLASS"OnPlayerTouchWeaponBox");
    
register_touch("weapon_shield"PLAYER_CLASS"OnPlayerTouchShield");
}

public 
OnPlayerTouchArmoury(entid)
{
    new 
iId cs_get_armoury_type(ent);

    
// player 'id' is gonna pickup armoury 'ent' weapon type 'iId'
    // games may not give it if already owns weapon or armor

    // note : iId can hold non weapon type : CSW_VEST and CSW_VESTHELM
    // Weapons type that don't exist in armouries : famas, galil, ump45, sg550, all pistols, knife, c4
}

public 
OnPlayerTouchWeaponBox(entid)
{
    new 
iId GetWeaponBoxWeaponTypeent );
    
// player 'id' is gonna touch weaponbox 'ent' and be possibliy given weapon type 'iId'
    // games may not give it if already owns weapon

    // you may want to restrict all except c4, then :
    // if( iId != CSW_C4 )
    // {
        // return PLUGIN_HANDLED;
    // }
    // return PLUGIN_CONTINUE;
}

public 
OnPlayerTouchShield(entid)
{
    
// player gonna possibly pickup a shield
}

GetWeaponBoxWeaponTypeent )
{
    new 
weapon;
    for(new 
1i<= 5i++)
    {
        
weapon get_pdata_cbase(entm_rgpPlayerItems_CWeaponBox[i], XO_CWEAPONBOX);
        if( 
weapon )
        {
            return 
cs_get_weapon_id(weapon);
        }
    }

    return 
0;

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 02-09-2014 at 09:23.
ConnorMcLeod is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 02-09-2014 , 08:33   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #2

Thx Connor,I know that my topic make you write this
HamletEagle is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 02-09-2014 , 12:27   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #3

nice snippet i wish you make another 1 that talk about hiting places and especially for hitting from back.
__________________
JusTGo is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 02-11-2014 , 09:28   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #4

Well, have other snippet: To do not drop weapons or remove from ground in specified time.
I do know if this will help this topic, but is good to merge with this topic.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
KORD_12.7
Senior Member
Join Date: Aug 2009
Location: Russia, Vladivostok
Old 02-14-2014 , 07:55   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #5

Offtop:
Spoiler
__________________

Vi Veri Veniversum Vivus Vici
Russian Half-Life and Adrenaline Gamer community

Last edited by KORD_12.7; 02-14-2014 at 07:57.
KORD_12.7 is offline
Send a message via ICQ to KORD_12.7
Compidence
Junior Member
Join Date: Feb 2014
Old 02-28-2014 , 14:18   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #6

Quote:
Originally Posted by ConnorMcLeod View Post
I've decided to use engine register_touch system rather than hooking Ham_Touch because engine is also filtering the toucher so it is better for performances.
Is the difference in performance significant?

And is it not better to use ham_touch in cases where you do not require weapons to be blocked at all times? As you can disable the forward?
Compidence is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 02-28-2014 , 19:53   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #7

may be both can be ok, i have not enough knowledge for this comparison.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 03-07-2014 , 09:00   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #8

Quote:
Originally Posted by ConnorMcLeod View Post
may be both can be ok, i have not enough knowledge for this comparison.
Ham_Touch hooks the virtual function on specific entity class only.

register_touch hooks pfnTouch (called for every entity) and then compares classname on the fly.

Better Ham_Touch that sticks on class

EDIT:

FM_Touch uses the same method than register_touch, but the classname comparison should be done in pawn, so it spent more time processing.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.

Last edited by joropito; 03-07-2014 at 09:02.
joropito is offline
Send a message via MSN to joropito
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 03-07-2014 , 14:41   Re: [SNIPPET] Properly hook and block weapons pickup
Reply With Quote #9

Quote:
Originally Posted by joropito View Post
Ham_Touch hooks the virtual function on specific entity class only.

register_touch hooks pfnTouch (called for every entity) and then compares classname on the fly.

Better Ham_Touch that sticks on class

EDIT:

FM_Touch uses the same method than register_touch, but the classname comparison should be done in pawn, so it spent more time processing.
But engine filter touched classname so on players are passed, Ham_Touch passes other entities, that's why i thought it would be hard to figure which one is the best.
Anyway FM_Touch is crap
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
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 13:37.


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