Raised This Month: $19 Target: $400
 4% 

Solved SourceMod bug? (SDKHooks_OnTakeDamage)


Post New Thread Reply   
 
Thread Tools Display Modes
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-06-2025 , 12:21   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #11

Quote:
Originally Posted by Mystik Spiral View Post
There may be better ways to do this, but this is the workaround I used to set the weapon index in L4D1:

PHP Code:
public void OnClientPutInServer(int client)
{
    
SDKHook(clientSDKHook_OnTakeDamageOnTakeDamage);
}

public 
Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    
// start weapon workaround
    
if (!g_bL4D2 && inflictor <= MaxClients && HasEntProp(attackerProp_Send"m_hActiveWeapon"))
    {
        
weapon GetEntPropEnt(attackerProp_Send"m_hActiveWeapon");
    }
    
// end weapon workaround
    
PrintToServer("Vic: %i, Atk: %i, Inf: %i, Dam: %f, DamTyp: %i, Wpn: %i"victimattackerinflictordamagedamagetypeweapon);

Thanks for the feedback that this is expected with L4D1.
https://sm.alliedmods.net/new-api/sdkhooks/SDKHookType
I would switch to SDKHook_OnTakeDamagePost or SDKHook_OnTakeDamageAlivePost
Latter one will show real damage, damage which have calculate friendly-fire filter or else. Depends game.
Code:
public void YourCallBack(int victim, int attacker, int inflictor, float damage, int damagetype)
- SDKHook_OnTakeDamage again require return value (public Action), and afterall you not really block or change damage value, I assume.
__________________
Do not Private Message @me
Bacardi is offline
Mystik Spiral
Senior Member
Join Date: Oct 2020
Location: Orlando, FL
Old 05-06-2025 , 23:45   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #12

It is for this plugin:
https://forums.alliedmods.net/showthread.php?t=329035
[L4D & L4D2] Reverse Friendly-Fire [v2.9.1 (03-May-2025)]
full code -> https://github.com/Mystik-Spiral/l4d..._reverse_ff.sp

It generally reverses friendly-fire so the attacker takes damage instead of the victim, but there are many options including options for the victim to take damage, and options that control how much damage, as a percentage, is dealt to the attacker and/or victim. I have not tried it, but I was under the impression you could not change the damage amount in Post, so that is not an option if true.

The sample code I posted is not complete, just an over simplified example to show the same code executed in L4D2 works as expected, but in L4D1 it was not working because the weapon index was always -1. The workaround I posted is what I put into the actual code and it seems to work fine. The workaround only executes when the game is L4D1 (g_bL4D2 is a global boolean set during AskPluginLoad2 and a test on GetEngineVersion), when Inflictor = Attacker (Inflictor <= MaxClients), and then it gets the active weapon index from the Attacker if that weapon has the m_hActiveWeapon property. I just leave weapon index at -1 for anything else that fires OnTakeDamage since I am not trying to reverse friendly-fire for tank rocks, throwables (molotov, pipe), and there are no (grenade/rocket) launchers in L4D1.

I am already using the Inflictor Classname (sInflictorClass) to determine other things, like chainsaw, melee, grenade launcher, and mounted gun (prop_minigun[_l4d1], prop_mounted_machine_gun), regardless of what the weapon index is.

However, I think I should adjust it to ignore world, so change...
from:
PHP Code:
if (!g_bL4D2 && inflictor <= MaxClients && HasEntProp(attackerProp_Send"m_hActiveWeapon")) 
to:
PHP Code:
if (!g_bL4D2 && inflictor <= MaxClients && HasEntProp(attackerProp_Send"m_hActiveWeapon")) 
__________________

Last edited by Mystik Spiral; 05-06-2025 at 23:48.
Mystik Spiral is offline
HarryPotter
Veteran Member
Join Date: Sep 2017
Location: Taiwan, Asia
Old 05-10-2025 , 05:30   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #13

Quote:
Originally Posted by Mystik Spiral View Post

Run that in L4D2 and weapon will have a value as expected.
Run that in L4D1 and weapon will have a value of -1.

For l4d1
PHP Code:
    PrintToChatAll("%d attack %d, inflictor: %d, damage: %.2f damagetype: %d, weapon: %d, damageForce: %.1f %.1f %.1f, damagePosition: %.1f %.1f %.1f"
        
attackervictiminflictordamagedamagetypeweapondamageForce[0], damageForce[1], damageForce[2], damagePosition[0], damagePosition[1], damagePosition[2]);

    
int real_weapon = -1;
    if(
attacker <= MaxClients && IsClientInGame(attacker
        && 
inflictor && IsValidEntity(inflictor))
    {
        if(
attacker == inflictor && HasEntProp(attackerProp_Send"m_hActiveWeapon"))
        {
            
real_weapon GetEntPropEnt(attackerProp_Send"m_hActiveWeapon");
        }
        else
        {
            
real_weapon inflictor;
        }
    }

    if(
real_weapon 0)
    {
        static 
char strClassName[64];
        
GetEntityClassname(real_weaponstrClassNamesizeof(strClassName));
        
PrintToChatAll("real weapon: %d, classname: %s"real_weaponstrClassName);
    } 
__________________

Last edited by HarryPotter; 05-10-2025 at 05:31.
HarryPotter is offline
Mystik Spiral
Senior Member
Join Date: Oct 2020
Location: Orlando, FL
Old 05-10-2025 , 20:29   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #14

I like your style Harry and used this with a small change. Even in L4D2, SDKHooks_OnTakeDamage always returns weapon as -1 for throwables (like molotov "inferno" and "entityflame") and projectiles (like "grenade_launcher_projectile"). This means you do not want to set weapon = inflictor (in an else statement) if you just want to make SDKHooks_OnTakeDamage behave the same in L4D1 as it does in L4D2.

PHP Code:
public Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    
//workaround to get weapon index to behave the same in L4D1 as it does in L4D2
    
if(!g_bL4D2 && attacker <= MaxClients && IsClientInGame(attacker) && inflictor && IsValidEntity(inflictor))
    {
        if(
attacker == inflictor && HasEntProp(attackerProp_Send"m_hActiveWeapon"))
        {
            
weapon GetEntPropEnt(attackerProp_Send"m_hActiveWeapon");
        }
    } 
__________________

Last edited by Mystik Spiral; 05-10-2025 at 20:37.
Mystik Spiral is offline
HarryPotter
Veteran Member
Join Date: Sep 2017
Location: Taiwan, Asia
Old Yesterday , 08:47   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #15

Quote:
Originally Posted by Mystik Spiral View Post
I like your style Harry and used this with a small change. Even in L4D2, SDKHooks_OnTakeDamage always returns weapon as -1 for throwables (like molotov "inferno" and "entityflame") and projectiles (like "grenade_launcher_projectile"). This means you do not want to set weapon = inflictor (in an else statement) if you just want to make SDKHooks_OnTakeDamage behave the same in L4D1 as it does in L4D2.

PHP Code:
public Action OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3])
{
    
//workaround to get weapon index to behave the same in L4D1 as it does in L4D2
    
if(!g_bL4D2 && attacker <= MaxClients && IsClientInGame(attacker) && inflictor && IsValidEntity(inflictor))
    {
        if(
attacker == inflictor && HasEntProp(attackerProp_Send"m_hActiveWeapon"))
        {
            
weapon GetEntPropEnt(attackerProp_Send"m_hActiveWeapon");
        }
    } 
I won't set weapon = inflictor if I were you, (In fact, I don't like to change any parameters in pre hooks unless necessary)
If I remembered correctly, it would affect "player_hurt", "player_death" event, which causes other plugins to get the wrong "weapon" valve.

https://wiki.alliedmods.net/Left_4_d...ts#player_hurt
https://wiki.alliedmods.net/Left_4_d...s#player_death
__________________

Last edited by HarryPotter; Yesterday at 08:48.
HarryPotter is offline
Mystik Spiral
Senior Member
Join Date: Oct 2020
Location: Orlando, FL
Old Yesterday , 18:38   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #16

Quote:
Originally Posted by HarryPotter View Post
I won't set weapon = inflictor if I were you, (In fact, I don't like to change any parameters in pre hooks unless necessary)
If I remembered correctly, it would affect "player_hurt", "player_death" event, which causes other plugins to get the wrong "weapon" valve.

https://wiki.alliedmods.net/Left_4_d...ts#player_hurt
https://wiki.alliedmods.net/Left_4_d...s#player_death
It is set to behave for L4D1 like it does in L4D2. The only time my code sets a value for weapon is when the attacker = inflictor, the attacker is an in-game client, and it sets it to the active weapon of the attacker. The weapon returned is accurate in my testing and the feedback so far is positive. I believe you are suggesting using a local variable like "real_weapon" instead of changing the value of "weapon", and if any problems show up I will probably make that change. Thank you for the feedback!
__________________

Last edited by Mystik Spiral; Yesterday at 18:40.
Mystik Spiral is offline
Cthulhu88
Junior Member
Join Date: Mar 2025
Old Yesterday , 20:54   Re: SourceMod bug? (SDKHooks_OnTakeDamage)
Reply With Quote #17

Setting the weapon for L4D1 has no effect as I mentioned in my first post: https://github.com/alliedmodders/sou...hack.h#L65-L70
SetWeapon member function is no-op.

In the other hand, changing weapon does affect the value passed to callbacks after yours, also mentioned in my first post: https://github.com/alliedmodders/sou...sion.cpp#L1146 and https://github.com/alliedmodders/sou...sion.cpp#L1166

Unless you can guarantee there are no conflicts between your plugin and others listening into the same event, don't write to weapon (if you do, restore it to the original value before returning).
Cthulhu88 is offline
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 09:48.


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