AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [CS:S] Hooking Secondary Knife Attack (https://forums.alliedmods.net/showthread.php?t=307436)

Komodo3000 05-10-2018 00:11

[CS:S] Hooking Secondary Knife Attack
 
Is there a way to hook secondary knife attack in CS:S?

The 'weapon_fire' event is not fired by secondary attack:
PHP Code:

HookEvent("weapon_fire"EventWeaponFireEventHookMode_Post); 

PHP Code:

public EventWeaponFire(Handle:event,const String:name[],bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    static 
String:Weapon[32];
    
GetEventString(event"weapon"Weaponsizeof(Weapon));
    if(
StrEqual(Weapon"knife"))
    {
        
//Code
    
}


I also tried using
PHP Code:

AddCommandListener(SecondaryAttack"+attack2"

or
PHP Code:

public Action:OnPlayerRunCmd(client, &buttons)
{
    if(
buttons IN_ATTACK2)
    {
        
//Code
    
}
    return 
Plugin_Continue;


but they keep getting called as long as "+attack2" button is pushed and not when the actual attack is performed.
I also need it to work, every time secondary attack is performed and not only when someone is hurt/hit by it, so 'player_hurt' event and 'SDKHook_TraceAttack' is not an option.

MasterMind420 05-10-2018 11:01

Re: [CS:S] Hooking Secondary Knife Attack
 
Quote:

Originally Posted by Komodo3000 (Post 2591513)
Is there a way to hook secondary knife attack in CS:S?

The 'weapon_fire' event is not fired by secondary attack:
PHP Code:

HookEvent("weapon_fire"EventWeaponFireEventHookMode_Post); 

PHP Code:

public EventWeaponFire(Handle:event,const String:name[],bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    static 
String:Weapon[32];
    
GetEventString(event"weapon"Weaponsizeof(Weapon));
    if(
StrEqual(Weapon"knife"))
    {
        
//Code
    
}


I also tried using
PHP Code:

AddCommandListener(SecondaryAttack"+attack2"

or
PHP Code:

public Action:OnPlayerRunCmd(client, &buttons)
{
    if(
buttons IN_ATTACK2)
    {
        
//Code
    
}
    return 
Plugin_Continue;


but they keep getting called as long as "+attack2" button is pushed and not when the actual attack is performed.
I also need it to work, every time secondary attack is performed and not only when someone is hurt/hit by it, so 'player_hurt' event and 'SDKHook_TraceAttack' is not an option.

Use a timer....time exactly when u press attack2. So if it happens 0.3 seconds or whatever after u press the button

Lux 05-10-2018 13:51

Re: [CS:S] Hooking Secondary Knife Attack
 
sound hook should work?
https://sm.alliedmods.net/api/index....d=show&id=787&

Only ever tried to mod CSS for viewmodel legs never messed with anything else

backwards 05-10-2018 14:07

Re: [CS:S] Hooking Secondary Knife Attack
 
Maybe something like this if the sound hook isn't a reliable method.

PHP Code:

#include <sourcemod>
#include <sdktools>

new g_offsNextSecondaryAttackg_offsTimeWeaponIdle;

public 
OnPluginStart()
{
    
g_offsNextSecondaryAttack FindSendPropOffs("CBaseCombatWeapon""m_flNextSecondaryAttack");
    
g_offsTimeWeaponIdle FindSendPropOffs("CBaseCombatWeapon""m_flTimeWeaponIdle");
}

public 
Action:OnPlayerRunCmd(client, &buttons
{
    if(
IsValidClient(client))
    {
        new 
weapon GetEntPropEnt(clientProp_Send"m_hActiveWeapon");
        if (
weapon != -1)
        {
            new 
knife_weapon GetPlayerWeaponSlot(client2);
            if(
knife_weapon != -1)
            {
                if(
knife_weapon == weapon &&
                
buttons IN_ATTACK2
                
&& GetEntDataFloat(weapong_offsNextSecondaryAttack) <= GetGameTime()
                )
//&& GetEntDataFloat(weapon, g_offsTimeWeaponIdle) >= GetGameTime()) 
                
{
                    
PrintToChat(client"Second Attack %f %f"GetEntDataFloat(weapong_offsNextSecondaryAttack), GetEntDataFloat(weapong_offsTimeWeaponIdle));
                }
            }
        }
    }

    return 
Plugin_Continue


bool IsValidClient(int client)
{
    if (!(
<= client <= MaxClients) || !IsClientInGame(client) || IsClientSourceTV(client) || IsClientReplay(client))
        return 
false;

    return 
true;


Note: I'm not sure what m_flTimeWeaponIdle exactly for as it doesn't seem to behave as i originally assumed.
You have to account for weapon draw time and primary attack cool down times as well but this should get you in the right direction.

Komodo3000 05-10-2018 14:18

Re: [CS:S] Hooking Secondary Knife Attack
 
Thanks for your fast replies...

@MasterMind420
Quote:

Originally Posted by MasterMind420 (Post 2591577)
Use a timer....time exactly when u press attack2. So if it happens 0.3 seconds or whatever after u press the button

I already tried that but soon encountered a problem:
When trying to figure out the exact interval betweet two secondary knife attacks using this code:

PHP Code:

public OnClientAuthorized(client)
{
    
SDKHook(clientSDKHook_TraceAttackOnTraceAttack);
}

public 
Action:OnTraceAttack(victim, &attacker, &inflictor, &Float:damage, &damagetype, &ammotypehitboxhitgroup)
{
    
decl String:Weapon[32];            
    
GetClientWeapon(attackerWeaponsizeof(Weapon));
    if(
StrEqual(Weapon[7], "knife"false))
    {
        
PrintToChat(attacker"Attacked with Knife | Time: %f"GetEngineTime());
    }
    return 
Plugin_Continue;


the output is the following.:
PHP Code:

Attacked with Knife Time549.686523
Attacked with Knife 
Time550.781555
Attacked with Knife 
Time551.906555
Attacked with Knife 
Time553.001525
Attacked with Knife 
Time554.111572
Attacked with Knife 
Time555.446594
Attacked with Knife 
Time556.556518 

As you can see the interval is not constant, it varies from '1.09497' to '1.335022'. Using this method is too inaccurate for me.


@Lux
Quote:

Originally Posted by Lux (Post 2591601)
sound hook should work?
https://sm.alliedmods.net/api/index....d=show&id=787&

Only ever tried to mod CSS for viewmodel legs never messed with anything else

You mean hooking the swing sound of knife attack? That actually would be possible but i never used this method. Where do i get the path of the swing sound? :shock:

backwards 05-10-2018 22:56

Re: [CS:S] Hooking Secondary Knife Attack
 
you can use "soundlist" command to print all precached sounds in console. There's a few issues with this though, i think the same sound plays for left click stabs as well as right click. There's also probably a lot of specific knife sounds for when you hit materials like metal or flesh that play which aren't specific to being a knife but is equal to a bullet impact sound. The sounds i found are "weapons\knife\knife_hitwall1.wav" and "weapons\knife\knife_stab.wav" for example.

hmmmmm 05-11-2018 03:25

Re: [CS:S] Hooking Secondary Knife Attack
 
PHP Code:

public Action OnPlayerRunCmd(int clientintbuttons)
{
    
int pressedButtons GetEntProp(clientProp_Data"m_afButtonPressed");
    if(
pressedButtons IN_ATTACK2)
    { 
        
//Code 
    
}

    return 
Plugin_Continue;


This would tell you when +attack2 was initially pressed. The prop exists in CS:GO, you'll have to check if it exists in CS:S. If not you can always just keep track of last buttons to find out when the button is pressed.

EDIT: After thinking about it this might not be the most accurate method since clicking the button might not always correspond to the secondary attack happening, might need a different method.

MasterMind420 05-11-2018 13:40

Re: [CS:S] Hooking Secondary Knife Attack
 
I think the most accurate method may be checking the animation sequence using m_nsequence entprop

Lux 05-11-2018 23:47

Re: [CS:S] Hooking Secondary Knife Attack
 
Quote:

Originally Posted by hmmmmm (Post 2591658)
PHP Code:

public Action OnPlayerRunCmd(int clientintbuttons)
{
    
int pressedButtons GetEntProp(clientProp_Data"m_afButtonPressed");
    if(
pressedButtons IN_ATTACK2)
    { 
        
//Code 
    
}

    return 
Plugin_Continue;


This would tell you when +attack2 was initially pressed. The prop exists in CS:GO, you'll have to check if it exists in CS:S. If not you can always just keep track of last buttons to find out when the button is pressed.

EDIT: After thinking about it this might not be the most accurate method since clicking the button might not always correspond to the secondary attack happening, might need a different method.


This might be a longshot but.

Is the button press in the same gameframe of the sound being emitted?

if so we can use the button press to determine which attack is being performed?
example in my own way

[Framestart]
OnPlayerRunCmd
Source Processing buttons
Starts attack
SoundHook checks
SoundEmitted

[frameend]

it maywork.

Quote:

Originally Posted by Komodo3000 (Post 2591603)
@Lux

You mean hooking the swing sound of knife attack? That actually would be possible but i never used this method. Where do i get the path of the swing sound? :shock:


Its in old syntax sorry i prefer it.
PHP Code:

public OnPluginStart()
{
    
AddNormalSoundHook(SoundHook);
}

public 
Action:SoundHook(iClients[64], &iNumClientsString:sSampleFile[PLATFORM_MAX_PATH], &iEntity, &iChannel, &Float:fVolume, &fLevel, &iPitch, &iFlags)
{
    
//checks for sounds
    //Note besure to optimize heavily this will forward all sounds that are emitted!
    //like storing ent refs of already checked entitys that are not knife or client
    


I'm not a css coder so its just a shot in the dark of how i would go about doing it in l4d2 :D hope this helps

BraveFox 05-12-2018 04:18

Re: [CS:S] Hooking Secondary Knife Attack
 
Use SDKHook_OnTakeDamage and add this to your part after you check if the weapon is a knife:
Quote:

if (damage == 65 || damage == 180)
{
//Attack2
}


All times are GMT -4. The time now is 18:06.

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