3#include <igameevents.h>
12#include <mathlib/vector.h>
13#include <shareddefs.h>
32static void SetWeapon(IGameEvent& event, std::string_view weapon)
34 event.SetString(
"weapon", std::string(weapon).c_str());
38 : _hook(
"Damage", [this] {
return Install(); }),
39 Before(_hook.MakeLifecycle()),
42 _interfaces(interfaces),
48 const auto onTakeDamage = [
this](CEntityInstance& victim,
void* info,
void*) {
return OnTakeDamage(victim, info); };
49 return HookFunction(
"Damage", _bindings.TakeDamage, onTakeDamage);
54 auto* info =
static_cast<EngineDamageInfo*
>(rawInfo);
55 DamageHit hit{.Victim = Entity{_entities, &victim},
56 .Attacker = {
static_cast<uint32_t
>(info->Attacker.ToInt())},
57 .Inflictor = {
static_cast<uint32_t
>(info->Inflictor.ToInt())},
58 .Amount = info->Damage,
59 .Type =
static_cast<uint32_t
>(info->DamageType)};
66 if (hit.Amount != info->Damage)
69 if (info->Damage != 0.0f)
71 info->TotalledDamage *= hit.Amount / info->Damage;
73 info->Damage = hit.Amount;
75 info->DamageType =
static_cast<int32_t
>(hit.Type);
81 if (!_bindings.TakeDamage)
83 return std::unexpected(
Error::Unsupported(
"the CBaseEntity::TakeDamageOld signature did not bind"));
85 if (!_bindings.BuildDamageInfo)
87 return std::unexpected(
Error::Unsupported(
"the CTakeDamageInfo constructor signature did not bind"));
99 CEntityInstance* attacker = _entities.Get(info.
Attacker).Raw();
100 CEntityInstance* inflictor = _entities.Get(info.
Inflictor).Raw();
104 inflictor = attacker;
110 push.NormalizeInPlace();
114 _bindings.BuildDamageInfo(&damage, inflictor, attacker,
nullptr, &push, &position, info.
Amount,
115 static_cast<int>(info.
Type), 0,
nullptr);
130 const DamageInfo* outer = std::exchange(_hit, &info);
131 _bindings.TakeDamage(victim.
Raw(), &damage, &result);
135void Damage::HookDeathEvents()
141 const auto onFireEvent = [
this](IGameEventManager2& manager, IGameEvent* event,
bool dontBroadcast) {
142 return OnFireEvent(manager, event, dontBroadcast);
144 _deathEvents =
HookInterface(&IGameEventManager2::FireEvent, _interfaces.GameEventManager, onFireEvent);
147HookResult<bool> Damage::OnFireEvent(IGameEventManager2& manager, IGameEvent* event,
bool dontBroadcast)
150 if (!_hit || _hit->Weapon.empty())
158 if (_hit->VictimWeapon.empty() || dontBroadcast)
163 SendDeath(*event, *_hit);
168void Damage::SendDeath(IGameEvent& death,
const DamageInfo& info)
170 const int victim = death.GetPlayerSlot(
"userid").Get();
173 IGameEventListener2* listener = _events.GetClientLegacyListener(slot);
178 SetWeapon(death, slot == victim ? info.VictimWeapon : info.Weapon);
179 listener->FireGameEvent(&death);
Event< DamageHit & > Before
void Apply(const Entity &victim, const DamageInfo &info)
Damage(EntitySystem &entities, const Bindings &bindings, Interfaces &interfaces, GameEvents &events)
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
A live entity, valid for this frame only.
CEntityInstance * Raw() const noexcept
Creates, fires and listens to game events.
A hook before-handler's verdict.
static HookResult Block(Ret value)
constexpr uint32_t DamageCrush
constexpr uint32_t DamageBullet
constexpr uint32_t DamageFall
constexpr uint32_t DamageHeadshot
constexpr uint32_t DamageBurn
constexpr uint32_t DamageShock
constexpr uint32_t DamageSlash
constexpr uint32_t DamageClub
constexpr uint32_t DamageGeneric
constexpr uint32_t DamageBlast
Ret CallOriginal(Ret(Iface::*method)(Args...), Iface *instance, Passed &&... args)
Result< Subscription > HookFunction(std::string_view name, const Fn< Ret(Object *, Args...)> &function, Before &&before, After &&after=nullptr)
Hook a signature-bound function at its entry.
std::expected< void, Error > Status
std::expected< T, Error > Result
static void SetWeapon(IGameEvent &event, std::string_view weapon)
Subscription HookInterface(Ret(Iface::*method)(Args...), Iface *instance, Before &&before, After &&after=nullptr)
Hook one virtual method of an engine interface for a subscription's lifetime.
EntityRef Inflictor
what did it, such as a grenade or a turret; empty means the attacker
uint32_t Type
Damage type bits (DamageTypes_t); combine with |. bits
EntityRef Attacker
credited in the kill feed and player_death
int32_t TotalledHealthLost
float TotalledDamageDealt
EngineDamageInfo * OriginatingInfo
static Error Unsupported(std::string detail)
Centralized holder for all SDK interface pointers.
IGameEventManager2 * GameEventManager
static constexpr std::string_view EventName