Raised This Month: $12 Target: $400
 3% 

[TF2] Make every weapon headshot


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mikusch
AlliedModders Donor
Join Date: Oct 2019
Location: Germany
Old 12-23-2019 , 14:06   [TF2] Make every weapon headshot
Reply With Quote #1

I'm trying to allow all bullet-based weapons in TF2 to do headshots with the same method that sniper rifles, bows and the revolver hitlocation attribute use. Quite a bit of searching and the source code for CTFSniperRifle::GetDamageType and CTFProjectile_Arrow::GetDamageType lead me to believe that OR-ing in the DMG_USE_HITLOCATIONS damagebit would do the trick but of course I was wrong. The damagetype changed but the game still doesn't use hit location damage.

I simply can't figure out how to get this to work. The bits get modified but the weapon won't headshot. Tested with SMG, Pistol and Minigun.

PHP Code:
public Action Client_OnTakeDamage(int victimint &attackerint &inflictorfloat &damageint &damagetypeint &weaponfloat damageForce[3], float damagePosition[3], int damagecustom)
{
    if (
damagetype DMG_BULLET)
    {
        
damagetype |= DMG_USE_HITLOCATIONS;
        return 
Plugin_Changed;
    }
    
    return 
Plugin_Continue;

The above function is hooked to all clients.

Yes, I am aware that SDKHook_TraceAttack and the head hitgroup exists, however this comes with its own quirks and I'd rather use TF2's built in hitlocation damage.

Last edited by Mikusch; 12-23-2019 at 14:13.
Mikusch is offline
Scag
AlliedModders Donor
Join Date: May 2017
Location: Crashing Hale
Old 01-01-2020 , 20:34   Re: [TF2] Make every weapon headshot
Reply With Quote #2

Looking at this it appears you'll need to DHook CTFWeaponBase::CanFireCriticalShot. Including everything you still have under a TraceAttack hook.
__________________
Over-engineering is underrated.

GitHub
BTC
ETH

Retired
Scag is offline
Mikusch
AlliedModders Donor
Join Date: Oct 2019
Location: Germany
Old 01-02-2020 , 16:30   Re: [TF2] Make every weapon headshot
Reply With Quote #3

Quote:
Originally Posted by Ragenewb View Post
Looking at this it appears you'll need to DHook CTFWeaponBase::CanFireCriticalShot. Including everything you still have under a TraceAttack hook.
It appears that this function only gets called for weapons that are actually capable of doing headshots (sniper rifles and the ambassador) and only gets called on a successful headshot, meaning it is kinda pointless.

The reason I can't use SDKHook_TraceAttack is that I can't modify the damagecustom attribute, and SDKHook_OnTakeDamage has no information about the hitgroup, meaning I can't differentiate between normal crits and my custom headshot crits.
Mikusch is offline
Scag
AlliedModders Donor
Join Date: May 2017
Location: Crashing Hale
Old 01-02-2020 , 16:56   Re: [TF2] Make every weapon headshot
Reply With Quote #4

If you glanced at the link I posted you would be able to see that Valve code would have done all of the heavy lifting for you.

Even if you aren't that adept at reading C++ code, you can decipher that:
  • It's a TraceAttack function
  • CanFireCriticalShot is only fired if both the hitgroup == 1 (HITGROUP_HEAD) and damagetype & DMG_USE_HITLOCATIONS
  • If all of that stuff is true, it would both add the DMG_CRIT to the damagetype and set the custom to TF_CUSTOM_HEADSHOT

So in the end, you will need to use a TraceAttack hook and set the update the damagetype as you have it already.
__________________
Over-engineering is underrated.

GitHub
BTC
ETH

Retired
Scag is offline
sapphonie
Junior Member
Join Date: Aug 2020
Location: ohio
Old 10-05-2021 , 17:13   Re: [TF2] Make every weapon headshot
Reply With Quote #5

HTML Code:
public Action Client_TraceAttack (int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &ammotype, int hitbox, int hitgroup)
{

    // headbox
    if (hitgroup == 1)
    {
        damagetype |= (DMG_USE_HITLOCATIONS | TF_CUSTOM_HEADSHOT | DMG_CRIT);
    }
    return Plugin_Changed;
}

This works for me.
__________________
she/her

Last edited by sapphonie; 10-05-2021 at 17:14.
sapphonie is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 10-05-2021 , 18:52   Re: [TF2] Make every weapon headshot
Reply With Quote #6

Quote:
Originally Posted by sapphonie View Post
HTML Code:
public Action Client_TraceAttack (int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &ammotype, int hitbox, int hitgroup)
{

    // headbox
    if (hitgroup == 1)
    {
        damagetype |= (DMG_USE_HITLOCATIONS | TF_CUSTOM_HEADSHOT | DMG_CRIT);
    }
    return Plugin_Changed;
}

This works for me.
You should only return Plugin_Changed inside that hitgroup check and Plugin_Continue for any other scenario.
__________________
Psyk0tik is offline
sapphonie
Junior Member
Join Date: Aug 2020
Location: ohio
Old 10-07-2021 , 01:08   Re: [TF2] Make every weapon headshot
Reply With Quote #7

Actually I'm wrong here, TF_CUSTOM_HEADSHOT isn't a flag for damage, it's a flag for kills.

A working plugin would look something like this:


HTML Code:
public void OnPluginStart()
{
    // for lateloading
    for (int i = 1; i <= MaxClients; i++)
    {
        if (!IsValidClient(i))
        {
            continue;
        }
        OnClientPutInServer(i);
    }


}

public void OnClientPutInServer(int client)
{
    SDKHook(client, SDKHook_TraceAttack, OnTraceAttack);
}


public Action OnTraceAttack(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &ammotype, int hitbox, int hitgroup)
{
    // headbox
    if (hitgroup == 1)
    {
        // always headshot if we hit the head
        damagetype |= (DMG_USE_HITLOCATIONS | DMG_CRIT);
        return Plugin_Changed;
    }
    return Plugin_Continue;
}
__________________
she/her
sapphonie 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 07:12.


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