VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Damage.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <cstdint>
15#include <string_view>
16
17namespace VoltMod
18{
19
20/** @defgroup DamageTypes Damage type bits (DamageTypes_t); combine with `|`. */
21/** @{ */
22constexpr uint32_t DamageGeneric = 0;
23constexpr uint32_t DamageCrush = 1u << 0;
24constexpr uint32_t DamageBullet = 1u << 1;
25constexpr uint32_t DamageSlash = 1u << 2;
26constexpr uint32_t DamageBurn = 1u << 3;
27constexpr uint32_t DamageFall = 1u << 5;
28constexpr uint32_t DamageBlast = 1u << 6;
29constexpr uint32_t DamageClub = 1u << 7;
30constexpr uint32_t DamageShock = 1u << 8;
31constexpr uint32_t DamageHeadshot = 1u << 19;
32/** @} */
33
34/** Who deals the damage, how much, and of which kind. */
36{
37 EntityRef Attacker; ///< credited in the kill feed and `player_death`
38 EntityRef Inflictor; ///< what did it, such as a grenade or a turret; empty means the attacker
39 float Amount = 0.0f;
40 uint32_t Type = DamageGeneric; ///< @ref DamageTypes bits
41 /** The weapon a kill's `player_death` names, and so the kill feed's icon
42 * (`panorama/images/icons/equipment/<Weapon>.svg`); empty keeps the engine's. */
43 std::string_view Weapon;
44 /** What the victim's own `player_death` names, for their death panel; empty sends everyone @ref Weapon. */
45 std::string_view VictimWeapon;
46};
47
48/** One hit on its way to the engine, as a @ref Damage::Before handler sees it. */
50{
52 /** Read-only: the engine keeps its own copy of who is credited. */
55 /** Edits to Amount and Type reach the engine. */
56 float Amount = 0.0f;
57 uint32_t Type = DamageGeneric; ///< @ref DamageTypes bits
58 /** Set to cancel the hit; nothing is dealt and no damage event fires. */
59 bool Blocked = false;
60};
61
62/**
63 * @brief Every entity's damage, from `CBaseEntity::TakeDamageOld`, and a way to deal it.
64 *
65 * The hook sits where the function starts, so it sees players, props and anything else that takes
66 * damage, including hits from @ref Apply. It installs on the first `Before` subscription.
67 *
68 * @code
69 * _damage = runtime.Damage.Before += [this](VoltMod::DamageHit& hit) {
70 * if (IsStructure(hit.Victim.Ref()))
71 * hit.Blocked = HitStructure(hit.Victim.Ref(), hit.Attacker, hit.Amount);
72 * };
73 * runtime.Damage.Apply(bot, {.Attacker = owner.Ref(), .Inflictor = turret, .Amount = 25,
74 * .Type = VoltMod::DamageBullet});
75 * @endcode
76 *
77 * Game-thread only.
78 */
79class Damage
80{
81public:
82 /** @p entities resolves the refs, @p bindings supplies the two damage functions, @p interfaces the
83 * event manager and @p events each client's listener. All must outlive this service; the Runtime
84 * declares them above. */
86 Damage(const Damage&) = delete;
87 Damage& operator=(const Damage&) = delete;
88
89private:
90 LazyHook _hook;
91
92public:
93 /** Raised before the engine applies a hit. */
95
96 /** Why damage cannot be hooked or dealt: a damage signature did not bind. */
97 Status Available() const;
98
99 /**
100 * Deal @p info to @p victim through the engine, so death, the kill feed and `player_death`
101 * credit @p info's attacker as if its own weapon had hit. Does nothing for a falsy victim or
102 * while @ref Available fails.
103 */
104 void Apply(const Entity& victim, const DamageInfo& info);
105
106private:
107 Result<Subscription> Install();
108 HookResult<int64_t> OnTakeDamage(CEntityInstance& victim, void* info);
109 /** Renames the weapon in each `player_death` fired while @ref Apply deals a named weapon's hit. */
110 void HookDeathEvents();
112 /** Fires @p death at each client, with @p info's @ref DamageInfo::VictimWeapon for the victim. */
113 void SendDeath(IGameEvent& death, const DamageInfo& info);
114
115 EntitySystem& _entities;
116 const Bindings& _bindings;
117 Interfaces& _interfaces;
118 GameEvents& _events;
119 /** The hit @ref Apply is dealing, or null. */
120 const DamageInfo* _hit = nullptr;
121 Subscription _deathEvents;
122};
123
124} // namespace VoltMod
Every entity's damage, from CBaseEntity::TakeDamageOld, and a way to deal it.
Definition Damage.hpp:80
Event< DamageHit & > Before
Definition Damage.hpp:94
Status Available() const
Definition Damage.cpp:78
void Apply(const Entity &victim, const DamageInfo &info)
Definition Damage.cpp:91
Damage(const Damage &)=delete
Damage & operator=(const Damage &)=delete
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
A live entity, valid for this frame only.
Definition Entity.hpp:29
A multicast signal with a fixed handler signature: the one way to subscribe in VoltMod.
Definition Event.hpp:54
Creates, fires and listens to game events.
A hook before-handler's verdict.
An engine hook that exists only while one of its events has handlers.
Definition LazyHook.hpp:30
Owns one registration and releases it on destruction.
constexpr uint32_t DamageCrush
Definition Damage.hpp:23
constexpr uint32_t DamageBullet
Definition Damage.hpp:24
constexpr uint32_t DamageFall
Definition Damage.hpp:27
constexpr uint32_t DamageHeadshot
Definition Damage.hpp:31
constexpr uint32_t DamageBurn
Definition Damage.hpp:26
constexpr uint32_t DamageShock
Definition Damage.hpp:30
constexpr uint32_t DamageSlash
Definition Damage.hpp:25
constexpr uint32_t DamageClub
Definition Damage.hpp:29
constexpr uint32_t DamageGeneric
Definition Damage.hpp:22
constexpr uint32_t DamageBlast
Definition Damage.hpp:28
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::expected< void, Error > Status
Definition Result.hpp:67
std::expected< T, Error > Result
Definition Result.hpp:64
const EntityRef Inflictor
Definition Damage.hpp:54
uint32_t Type
Damage type bits (DamageTypes_t); combine with |. bits
Definition Damage.hpp:57
const EntityRef Attacker
Definition Damage.hpp:53
EntityRef Inflictor
what did it, such as a grenade or a turret; empty means the attacker
Definition Damage.hpp:38
uint32_t Type
Damage type bits (DamageTypes_t); combine with |. bits
Definition Damage.hpp:40
EntityRef Attacker
credited in the kill feed and player_death
Definition Damage.hpp:37
std::string_view Weapon
Definition Damage.hpp:43
std::string_view VictimWeapon
Definition Damage.hpp:45
Centralized holder for all HL2SDK interface pointers.