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

Solved Knife detection


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OneMore
Senior Member
Join Date: Feb 2019
Old 03-24-2019 , 11:48   Knife detection
Reply With Quote #1

Hi guys,

As I understand there are a lot of types of knives in CS GO:
CSWeapon_KNIFE_CT
CSWeapon_KNIFE_T
CSWeapon_KNIFE_FLIP
CSWeapon_KNIFE_GUT
CSWeapon_KNIFE_KARAMBIT
CSWeapon_KNIFE_M9_BAYONET
CSWeapon_KNIFE_TATICAL
CSWeapon_KNIFE_FALCHION
CSWeapon_KNIFE_SURVIVAL_BOWIE
CSWeapon_KNIFE_BUTTERFLY
CSWeapon_KNIFE_PUSH

If I use this function:
public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3])

Please advise how to detect that the weapon is knife doesn't matter which type of knives it is.

Last edited by OneMore; 03-29-2019 at 07:54.
OneMore is offline
quasemago
Senior Member
Join Date: Dec 2018
Location: Brazil
Old 03-24-2019 , 14:06   Re: Knife detection
Reply With Quote #2

Just smth like that:
PHP Code:
public Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    if (
IsWeaponKnife(weapon))
    {
        
// blah
    
}
    return 
Plugin_Continue;
}

stock bool IsWeaponKnife(int weapon)
{
    if (!
IsValidEdict(weapon)) {
        return 
false;
    }

    
char classname[64];
    
GetEdictClassname(weaponclassnamesizeof(classname));

    if ((
StrContains(classname"knife") > -&& strcmp(classname"weapon_knifegg") != 0) || StrContains(classname"bayonet") > -1) {
        return 
true;
    }

    return 
false;

quasemago is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 03-24-2019 , 16:35   Re: Knife detection
Reply With Quote #3

PHP Code:

int g_Knives
[] =
{
    
"41"// knifegg
    
"42"// knife
    
"59"// knife_t
    
"80"// knife_ghost
    
"500"// weapon_bayonet
    
"505"// knife_flip
    
"506"// knife_gut
    
"507"// knife_karambit
    
"508"// knife_m9_bayonet
    
"509"// knife_tactical
    
"512"// knife_falchion
    
"514"// knife_survival_bowie
    
"515"// knife_butterfly
    
"516"// knife_push
    
"519"// knife_ursus
    
"520"// knife_gypsy_jackknife
    
"522"// knife_stiletto
    
"523" // knife_widowmaker
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    if (
IsWeaponKnife(weapon))
    {
        
// blah
    
}
    
    return 
Plugin_Continue;
}

stock bool IsWeaponKnife(int weapon)
{
    if (
weapon == -1)
    {
        return 
false;
    }

    
int item GetEntProp(weaponProp_Send"m_iItemDefinitionIndex");
    
    for (
int i 0sizeof(g_Knives); i++)
    {
        if (
g_Knives[i] == item)
        {
            return 
true;
        }
    }

    return 
false;

or

PHP Code:


stock bool IsWeaponKnife
(int weapon)
{
    if (
weapon == -1)
    {
        return 
false;
    }

    
int item GetEntProp(weaponProp_Send"m_iItemDefinitionIndex");
    
CSWeaponID id CS_ItemDefIndexToID(item);

    if (
id CSWeapon_MAX_WEAPONS_NO_KNIFES || id == CSWeapon_KNIFE_T || id == CSWeapon_KNIFE)
    {
        return 
true;
    }


    return 
false;

__________________

Last edited by Ilusion9; 03-24-2019 at 16:39.
Ilusion9 is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-24-2019 , 17:38   Re: Knife detection
Reply With Quote #4

Quote:
Originally Posted by quasemago View Post
Just smth like that:
PHP Code:
    if ((StrContains(classname"knife") > -&& strcmp(classname"weapon_knifegg") != 0) || StrContains(classname"bayonet") > -1
Thanks for the help. I tried with string comparison as well but missed something.
Just one question about your code. Does the second part (strcmp(classname, "weapon_knifegg")) not covered by the first part (StrContains(classname, "knife"))? The string "weapon_knifegg" contains "knife" isn't it?
OneMore is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 03-24-2019 , 17:41   Re: Knife detection
Reply With Quote #5

This can be read from the items file like GiveNamedItemEx does.

Check each item and check if its base type is a knife.

Each match can be stored into an array.

This way the indicies and classnames dont have to be hardcoded.
__________________
Neuro Toxin is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-24-2019 , 18:32   Re: Knife detection
Reply With Quote #6

Quote:
Originally Posted by Ilusion9 View Post
PHP Code:
 
I'm not professional in programming for Sourcemod and I do not understand your code completely. However, I will try it as well. Thanks!
OneMore is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 03-26-2019 , 03:25   Re: Knife detection
Reply With Quote #7

I'm almost certain that checking for if strncmp is weapon_knife will get all knives but for the classname ( to include weapon_knifegg as well )

You should test if I'm not wrong. I did make my Knife Headshot plugin based on this

When using weapon name from player_hurt event my advice ceases to work, unless you take classname.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 04-02-2019 at 02:47.
eyal282 is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-28-2019 , 17:06   Re: Knife detection
Reply With Quote #8

The second variant of Ilusion9 code the shortest and works perfectly. I got problems with detection by string before, that's why I decided to stay on Ilusion9's variant
OneMore is offline
OneMore
Senior Member
Join Date: Feb 2019
Old 03-28-2019 , 17:10   Re: Knife detection
Reply With Quote #9

Guys, how to mark the thread as solved? Can't find this option...
OneMore is offline
McSnaggit
Junior Member
Join Date: Feb 2019
Old 03-29-2019 , 05:56   Re: Knife detection
Reply With Quote #10

Had that problem too. Edit the thread in advanced mode. Next to title select solved in the dropdown.
McSnaggit 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 23:15.


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