Page 1 of 1

trace attack

Posted: Thu Oct 14, 2021 8:22 am
by velocity
Hi! I was wondering if it is possible to detect a knife attack if it is a teammate and has godmode. I tried hooking "on_take_damage", but it doesn't fire. I have been looking at trace_attack, but it doesn't exist in the .ini data files. Any ideas? This is for css/csgo

Re: Detect knife attack

Posted: Thu Oct 14, 2021 4:30 pm
by L'In20Cible

Syntax: Select all

from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from core import PLATFORM
from core import SOURCE_ENGINE

def get_trace_attack_function(entity):
offset = {
'orangebox': {'windows': 60, 'linux': 61},
'csgo': {'windows': 66, 'linux': 67}
}[SOURCE_ENGINE][PLATFORM]
return entity.pointer.make_virtual_function(
offset,
Convention.THISCALL,
# this, CTakeDamageInfo, Vector, trace_t, CDmgAccumulator
(DataType.POINTER, DataType.POINTER, DataType.POINTER,
DataType.POINTER, DataType.POINTER),
DataType.VOID
)

@EntityPreHook(EntityCondition.is_player, get_trace_attack_function)
def pre_trace_attack(stack_data):
...

Re: Detect knife attack

Posted: Thu Oct 14, 2021 8:24 pm
by velocity
Works! I have a problem with stack_data[1]


Code: Select all

@EntityPreHook(EntityCondition.is_player, get_trace_attack_function)
def pre_trace_attack(stack_data):
    victim = make_object(Player, stack_data[0])
    attacker = make_object(Player, stack_data[1])


Traceback (most recent call last):
File "../addons/source-python/plugins/tr/boost.py", line 711, in pre_trace_attack
attacker = make_object(Entity, stack_data[1])
File "../addons/source-python/packages/source-python/entities/_base.py", line 324, in _obj
return cls(index_from_pointer(ptr))

ValueError: Conversion from "Pointer" (<_memory.Pointer object at 0xe8ac8890>) to "Index" failed.

Re: Detect knife attack

Posted: Thu Oct 14, 2021 8:40 pm
by satoon101
That's because the second argument isn't a player:

Syntax: Select all

# this, CTakeDamageInfo, Vector, trace_t, CDmgAccumulator

Re: Detect knife attack

Posted: Thu Oct 14, 2021 8:48 pm
by velocity
oh I see, I thought it followed this, just like in sourcemod:

// TraceAttack
function Action (int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &ammotype, int hitbox, int hitgroup);


Well they seems a little cryptic to me? How would I get the attacker from that

Re: Detect knife attack

Posted: Thu Oct 14, 2021 8:57 pm
by satoon101
untested:

Syntax: Select all

from entities import TakeDamageInfo
from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from memory import make_object


@EntityPreHook(EntityCondition.is_player, get_trace_attack_function)
def pre_trace_attack(stack_data):
victim = make_object(Player, stack_data[0])
take_damage_info = make_object(TakeDamageInfo, stack_data[1])

# player index of the player attacking
attacker_index = take_damage_info.attacker

# either the player index of the attacker or the index of the weapon if a projectile
# inflictor and attacker will be different if a projectile was used, but same if any gun/knife
inflictor = take_damage_info.inflictor

# index of the weapon that was used
# handles differentiating between attacker and inflictor automatically
# will be the attacking player's active weapon if a gun/knife was used
weapon = take_damage_info.weapon


* Edit: references for TakeDamageInfo:
http://wiki.sourcepython.com/developing ... DamageInfo

https://github.com/Source-Python-Dev-Te ... h#L84-L160

Re: Detect knife attack

Posted: Thu Oct 14, 2021 8:58 pm
by velocity
yep! just arrived at the same: http://wiki.sourcepython.com/developing ... DamageInfo

thanks, works!