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/**
20 * @brief A live entity, valid for this frame only.
21 *
22 * The engine can free it between frames: store an @ref EntityRef and resolve it again where you
23 * use it. `if (entity)` is the validity check. A falsy entity reads zero and ignores writes and
24 * verbs. Copies are cheap; assignment is deleted so a rebind never reads like a field write.
25 *
26 * Fields are generated pairs: `Health()` reads, `SetHealth(100)` writes and replicates.
27 */
28class Entity
29{
30protected:
31 EntitySystem* _sys = nullptr;
32 CEntityInstance* _e = nullptr;
33
34public:
35 // Not `= default`: GCC then needs the member initializers for AcceptInput's `activator = {}`.
37
38 /** Prefer `runtime.Entities` lookups over building one by hand. */
39 Entity(EntitySystem& entities, CEntityInstance* raw) noexcept : _sys(&entities), _e(raw) {}
40
41 Entity(const Entity&) = default;
42 Entity& operator=(const Entity&) = delete;
43
44 explicit operator bool() const noexcept { return _e != nullptr; }
45
46 /** The engine entity, for calls the framework does not wrap. */
47 CEntityInstance* Raw() const noexcept { return _e; }
48
49 /** Network index; -1 when falsy. */
50 int Index() const;
51
52 /** The handle to store; unset when falsy. */
53 EntityRef Ref() const;
54
55 /** Such as "player". Points into engine memory: copy it to keep it. */
56 std::string_view ClassName() const;
57
58 /** This entity as a player pawn; falsy for anything else, such as a prop or a grenade. */
59 Pawn AsPawn() const;
60
61 /** @name CBaseEntity fields */
62 /** @{ */
63#include <VoltMod/Schema/Generated/Wrappers/Entity.inc>
64 /** @} */
65
66 /** World position and rotation, from the scene node. */
67 Vector Origin() const;
68 QAngle Angles() const;
69
70 /** Move the entity; std::nullopt leaves that part unchanged. Does nothing when the Teleport slot
71 * did not bind. */
72 void Teleport(std::optional<Vector> origin, std::optional<QAngle> angles, std::optional<Vector> velocity) const;
73
74 Schema::MoveType_t MoveType() const { return MoveTypeRaw(); }
75
76 /** Writes both move-type fields; the engine reverts a lone one next tick. */
77 void SetMoveType(Schema::MoveType_t type) const;
78
79 /** Spawn an entity from @ref EntitySystem::Create; the engine takes the keyvalues. */
80 void Spawn(KeyValues& values) const;
81
82 /** Fire an entity input, such as "Explode" or `("FollowEntity", "!activator", pawn)`. */
83 void AcceptInput(std::string_view input, std::string_view value = {}, const Entity& activator = {}) const;
84
85 /** Remove now. */
86 void Remove() const;
87
88 /** Remove after @p seconds, through the engine's input queue. */
89 void RemoveAfter(float seconds) const;
90
91 /** @name Model entities only
92 * Props, pawns, weapons and beams. On any other entity these do nothing. */
93 /** @{ */
94 void SetModel(std::string_view path) const;
95
96 /** Render and collision scale, clamped to 0.05-3: larger values have crashed the server. */
97 void SetScale(float scale) const;
98
99 /** Write `m_nRenderMode` and `m_clrRender` together. `kRenderTransAlpha` makes the alpha show. */
100 void SetRender(Schema::RenderMode_t mode, Color color) const;
101
102 /** Fade to @p alpha, or restore. A pawn's held weapons and wearables stay visible; hide a player
103 * completely with @ref Visibility. */
104 void SetVisible(bool visible, uint8_t alpha = 0) const;
105
106 /** Play @p animation once from its first frame, then loop @p idle. */
107 void PlayAnimation(std::string_view animation, std::string_view idle = {}) const;
108 /** @} */
109
110 /** Play a `.vsndevts` event from this entity, to everyone or to @p recipients. */
111 void EmitSound(std::string_view soundEvent, float volume = 1.0f) const;
112 void EmitSound(std::string_view soundEvent, IRecipientFilter& recipients, float volume = 1.0f) const;
113 /** Stop every @p soundEvent this entity is playing, for everyone. */
114 void StopSound(std::string_view soundEvent) const;
115};
116
117} // namespace VoltMod
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
Entity(EntitySystem &entities, CEntityInstance *raw) noexcept
Definition Entity.hpp:39
Entity & operator=(const Entity &)=delete
void SetRender(Schema::RenderMode_t mode, Color color) const
Definition Entity.cpp:216
QAngle Angles() const
Definition Entity.cpp:103
void SetModel(std::string_view path) const
Definition Entity.cpp:198
void SetMoveType(Schema::MoveType_t type) const
Definition Entity.cpp:120
int Index() const
Definition Entity.cpp:77
Pawn AsPawn() const
Definition Pawn.cpp:178
void SetVisible(bool visible, uint8_t alpha=0) const
Definition Entity.cpp:227
void RemoveAfter(float seconds) const
Definition Entity.cpp:181
std::string_view ClassName() const
Definition Entity.cpp:87
void AcceptInput(std::string_view input, std::string_view value={}, const Entity &activator={}) const
Definition Entity.cpp:134
void EmitSound(std::string_view soundEvent, float volume=1.0f) const
Definition Entity.cpp:242
EntitySystem * _sys
Definition Entity.hpp:31
EntityRef Ref() const
Definition Entity.cpp:82
void Spawn(KeyValues &values) const
Definition Entity.cpp:126
void PlayAnimation(std::string_view animation, std::string_view idle={}) const
Definition Entity.cpp:233
void Remove() const
Definition Entity.cpp:165
Entity() noexcept
Definition Entity.hpp:36
Entity(const Entity &)=default
Vector Origin() const
Definition Entity.cpp:97
void SetScale(float scale) const
Definition Entity.cpp:206
void StopSound(std::string_view soundEvent) const
Definition Entity.cpp:266
CEntityInstance * _e
Definition Entity.hpp:32
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:47
Schema::MoveType_t MoveType() const
Definition Entity.hpp:74
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:19
Report player pawn moves through CBaseEntity::Teleport.
Definition Teleport.hpp:29
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
::QAngle QAngle
Definition Math.hpp:14
::Vector Vector
Definition Math.hpp:11