VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Entity.hpp
Go to the documentation of this file.
1#pragma once
2
11// Flags_t: the FL_* bits Flags() returns.
12#include <const.h>
13#include <optional>
14#include <string_view>
15
16namespace VoltMod
17{
18
19/** What a weapon's own VData says about it; a subclassed weapon reads its subclass's VData. */
21{
24 bool FullAuto = false;
25 /** The stock weapon it is built on, such as "weapon_taser" for a subclassed Zeus. Points into
26 * the game's symbol table, which lasts for the process. */
27 std::string_view BaseName;
28};
29
30/**
31 * @brief A live entity, valid for this frame only.
32 *
33 * The engine can free it between frames: store an @ref EntityRef and resolve it again where you
34 * use it. `if (entity)` is the validity check. A falsy entity reads zero and ignores writes and
35 * verbs. Copies are cheap; assignment is deleted so a rebind never reads like a field write.
36 *
37 * Fields are generated pairs: `Health()` reads, `SetHealth(100)` writes and replicates.
38 */
39class Entity
40{
41protected:
42 EntitySystem* _sys = nullptr;
43 CEntityInstance* _e = nullptr;
44
45public:
46 // Not `= default`: GCC then needs the member initializers for AcceptInput's `activator = {}`.
47 Entity() noexcept {}
48
49 /** Prefer `runtime.Entities` lookups over building one by hand. */
50 Entity(EntitySystem& entities, CEntityInstance* raw) noexcept : _sys(&entities), _e(raw) {}
51
52 Entity(const Entity&) = default;
53 Entity& operator=(const Entity&) = delete;
54
55 explicit operator bool() const noexcept { return _e != nullptr; }
56
57 /** The engine entity, for calls the framework does not wrap. */
58 CEntityInstance* Raw() const noexcept { return _e; }
59
60 /** Network index; -1 when falsy. */
61 int Index() const;
62
63 /** The handle to store; unset when falsy. */
64 EntityRef Ref() const;
65
66 /** Such as "player". Points into engine memory: copy it to keep it. */
67 std::string_view ClassName() const;
68
69 /** This entity as a player pawn; falsy for anything else, such as a prop or a grenade. */
70 Pawn AsPawn() const;
71
72 /** This weapon's VData, or empty for anything that is not a weapon. Read it where you use it:
73 * a subclass change swaps it. */
74 std::optional<WeaponData> FindWeaponData() const;
75
76 /** @name CBaseEntity fields */
77 /** @{ */
78#include <VoltMod/Schema/Generated/Wrappers/Entity.inc>
79 /** @} */
80
81 /** World position and rotation, from the scene node. */
82 Vector Origin() const;
83 QAngle Angles() const;
84
85 /** Move the entity; std::nullopt leaves that part unchanged. Does nothing when the Teleport slot
86 * did not bind. */
87 void Teleport(std::optional<Vector> origin, std::optional<QAngle> angles, std::optional<Vector> velocity) const;
88
89 Schema::MoveType_t MoveType() const { return MoveTypeRaw(); }
90
91 /** Writes both move-type fields; the engine reverts a lone one next tick. */
92 void SetMoveType(Schema::MoveType_t type) const;
93
94 /** Spawn an entity from @ref EntitySystem::Create; the engine takes the keyvalues. */
95 void Spawn(KeyValues& values) const;
96
97 /** Fire an entity input, such as "Explode" or `("FollowEntity", "!activator", pawn)`. */
98 void AcceptInput(std::string_view input, std::string_view value = {}, const Entity& activator = {}) const;
99
100 /** Remove now. */
101 void Remove() const;
102
103 /** Remove after @p seconds, through the engine's input queue. */
104 void RemoveAfter(float seconds) const;
105
106 /** @name Model entities only
107 * Props, pawns, weapons and beams. On any other entity these do nothing. */
108 /** @{ */
109 void SetModel(std::string_view path) const;
110
111 /** Parent this entity to @p parent at the parent's origin. With @p copyBones its bones take the
112 * parent's pose by name, and traces such as bullets stop hitting it, as with worn gear, even after
113 * `ClearParent`. @ref BoneFollow keeps a pawn shootable. */
114 void Follow(const Entity& parent, bool copyBones) const;
115
116 /** Render and collision scale, clamped to 0.05-3: larger values have crashed the server. */
117 void SetScale(float scale) const;
118
119 /** Write `m_nRenderMode` and `m_clrRender` together. `kRenderTransAlpha` makes the alpha show. */
120 void SetRender(Schema::RenderMode_t mode, Color color) const;
121
122 /** Fade to @p alpha, or restore. A pawn's held weapons and wearables stay visible; hide a player
123 * completely with @ref Visibility. */
124 void SetVisible(bool visible, uint8_t alpha = 0) const;
125
126 /** Play @p animation once from its first frame, then loop @p idle. */
127 void PlayAnimation(std::string_view animation, std::string_view idle = {}) const;
128
129 /** Loop @p animation from its first frame. */
130 void LoopAnimation(std::string_view animation) const;
131
132 /** Speed the current animation up or down; 1 is its own pace. A new animation resets it. */
133 void SetPlaybackRate(float rate) const;
134
135 /** Material group by index. */
136 void SetSkin(int skin) const;
137
138 /** Pick choice @p value of the model's bodygroup @p group, such as `("warhead", 1)`. */
139 void SetBodygroup(std::string_view group, int value) const;
140 /** @} */
141
142 /** Move with @p parent, keeping this entity where it is; with @p attachment, snap to that attachment
143 * on the parent's model, such as a hand. A child is removed with its parent: call @ref ClearParent
144 * first to keep it. */
145 void SetParent(const Entity& parent, std::string_view attachment = {}) const;
146 void ClearParent() const;
147
148 /** Stop a particle effect: new particles stop, and those drawn fade out, or vanish at once when
149 * @p immediately. The entity stays until removed; removing a looping effect alone can leave it drawn. */
150 void StopEffect(bool immediately) const;
151
152 /** Play a `.vsndevts` event from this entity, to everyone or to @p recipients. */
153 void EmitSound(std::string_view soundEvent, float volume = 1.0f) const;
154 void EmitSound(std::string_view soundEvent, IRecipientFilter& recipients, float volume = 1.0f) const;
155 /** Play a `.vsndevts` event from this entity to the player in @p slot alone. */
156 void EmitSoundTo(std::string_view soundEvent, int slot, float volume = 1.0f) const;
157 /** Stop every @p soundEvent this entity is playing, for everyone. */
158 void StopSound(std::string_view soundEvent) const;
159};
160
161} // namespace VoltMod
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
std::optional< WeaponData > FindWeaponData() const
Definition Entity.cpp:85
Entity(EntitySystem &entities, CEntityInstance *raw) noexcept
Definition Entity.hpp:50
Entity & operator=(const Entity &)=delete
void SetParent(const Entity &parent, std::string_view attachment={}) const
Definition Entity.cpp:283
void SetRender(Schema::RenderMode_t mode, Color color) const
Definition Entity.cpp:233
void SetPlaybackRate(float rate) const
Definition Entity.cpp:264
QAngle Angles() const
Definition Entity.cpp:112
void SetModel(std::string_view path) const
Definition Entity.cpp:207
void SetMoveType(Schema::MoveType_t type) const
Definition Entity.cpp:129
int Index() const
Definition Entity.cpp:65
Pawn AsPawn() const
Definition Pawn.cpp:243
void SetVisible(bool visible, uint8_t alpha=0) const
Definition Entity.cpp:244
void RemoveAfter(float seconds) const
Definition Entity.cpp:190
std::string_view ClassName() const
Definition Entity.cpp:75
void Follow(const Entity &parent, bool copyBones) const
Definition Entity.cpp:215
void AcceptInput(std::string_view input, std::string_view value={}, const Entity &activator={}) const
Definition Entity.cpp:143
void EmitSound(std::string_view soundEvent, float volume=1.0f) const
Definition Entity.cpp:306
void LoopAnimation(std::string_view animation) const
Definition Entity.cpp:259
EntitySystem * _sys
Definition Entity.hpp:42
void EmitSoundTo(std::string_view soundEvent, int slot, float volume=1.0f) const
Definition Entity.cpp:330
void ClearParent() const
Definition Entity.cpp:296
EntityRef Ref() const
Definition Entity.cpp:70
void Spawn(KeyValues &values) const
Definition Entity.cpp:135
void PlayAnimation(std::string_view animation, std::string_view idle={}) const
Definition Entity.cpp:250
void Remove() const
Definition Entity.cpp:174
Entity() noexcept
Definition Entity.hpp:47
void SetBodygroup(std::string_view group, int value) const
Definition Entity.cpp:278
void Teleport(std::optional< Vector > origin, std::optional< QAngle > angles, std::optional< Vector > velocity) const
Definition Entity.cpp:118
void StopEffect(bool immediately) const
Definition Entity.cpp:301
Entity(const Entity &)=default
Vector Origin() const
Definition Entity.cpp:106
void SetScale(float scale) const
Definition Entity.cpp:223
void StopSound(std::string_view soundEvent) const
Definition Entity.cpp:339
CEntityInstance * _e
Definition Entity.hpp:43
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:58
void SetSkin(int skin) const
Definition Entity.cpp:273
Schema::MoveType_t MoveType() const
Definition Entity.hpp:89
Spawn keyvalues such as "origin", "spawnflags" or "effect_name".
Definition KeyValues.hpp:16
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
::QAngle QAngle
Definition Math.hpp:15
::Vector Vector
Definition Math.hpp:12
Schema::CSWeaponType Type
Definition Entity.hpp:23
Schema::gear_slot_t GearSlot
Definition Entity.hpp:22
std::string_view BaseName
Definition Entity.hpp:27