VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
EntitySystemSpawn.cpp
Go to the documentation of this file.
4#include <const.h>
5#include <string>
6#include <utility>
7
8namespace VoltMod
9{
10
11static constexpr int GlowTeamAny = -1;
12static constexpr int GlowStateAlwaysOn = 3;
13// Draws the glow outline alone.
14static constexpr int OutlineRenderAmount = 1;
15// An info_target with this flag reaches the clients, so a particle can read its position.
16static constexpr int TargetSentToClients = 1;
17
19{
20 if (!_interfaces.GameResourceService)
21 {
22 return std::unexpected(Error::NotReady("IGameResourceService not available"));
23 }
24 if (!_bindings.CreateEntityByName || !_bindings.DispatchSpawn)
25 {
26 return std::unexpected(Error::Unsupported("CreateEntityByName or DispatchSpawn did not bind"));
27 }
28 return {};
29}
30
31Entity EntitySystem::Create(std::string_view className)
32{
33 // Checked here so every create-then-spawn path gets it.
34 if (!Available() || className.empty())
35 {
36 return {};
37 }
38 return {*this, _bindings.CreateEntityByName(std::string(className).c_str(), -1)};
39}
40
41Entity EntitySystem::Spawn(std::string_view className, KeyValues& values)
42{
43 Entity entity = Create(className);
44 entity.Spawn(values);
45 return entity;
46}
47
49{
50 return SpawnProp(prop, false);
51}
52
53Entity EntitySystem::SpawnOutline(const Entity& target, std::string_view model, const PropGlow& glow)
54{
55 if (!target)
56 {
57 return {};
58 }
59 const Entity outline = SpawnProp(
60 {.Model = model, .Origin = target.Origin(), .Solid = false, .CastsShadow = false, .Glow = glow}, true);
61 outline.Follow(target, true);
62 return outline;
63}
64
65Entity EntitySystem::SpawnProp(const PropSpec& prop, bool outlineOnly)
66{
67 KeyValues values;
68 values.Set("model", prop.Model)
69 .Set("origin", prop.Origin)
70 .Set("angles", prop.Angles)
71 .Set("solid", static_cast<int>(prop.Solid ? SOLID_VPHYSICS : SOLID_NONE));
72 if (!prop.Skin.empty())
73 {
74 values.Set("skin", prop.Skin);
75 }
76 if (!prop.CastsShadow)
77 {
78 values.Set("disableshadows", 1);
79 }
80 if (prop.Glow)
81 {
82 const PropGlow& glow = *prop.Glow;
83 const int team = glow.Team == Team::None ? GlowTeamAny : std::to_underlying(glow.Team);
84 values.Set("glowcolor", glow.Color)
85 .Set("glowrange", glow.Range)
86 .Set("glowteam", team)
87 .Set("glowstate", GlowStateAlwaysOn);
88 if (outlineOnly)
89 {
90 values.Set("renderamt", OutlineRenderAmount);
91 }
92 }
93
94 Entity entity = Create("prop_dynamic");
95 // The physics shapes take their owner as they are created.
96 if (prop.Owner)
97 {
98 entity.SetOwnerEntity(prop.Owner);
99 }
100 entity.Spawn(values);
101 // "solid" 0 alone leaves the model's hull blocking traces.
102 if (entity && !prop.Solid)
103 {
104 entity.AcceptInput("DisableCollision");
105 }
106 if (entity && prop.Scale != 1.0f)
107 {
108 entity.SetScale(prop.Scale);
109 }
110 return entity;
111}
112
113Entity EntitySystem::SpawnParticle(std::string_view effect, const Vector& origin, const QAngle& angles)
114{
115 if (effect.empty())
116 {
117 return {};
118 }
119 KeyValues values;
120 values.Set("effect_name", effect).Set("start_active", 1).Set("origin", origin).Set("angles", angles);
121 return Spawn("info_particle_system", values);
122}
123
124Entity EntitySystem::SpawnLine(std::string_view effect, const Vector& from, EntityRef end)
125{
126 if (effect.empty())
127 {
128 return {};
129 }
130 Entity line = Create("info_particle_system");
131 const Schema::CParticleSystem particle{line.Raw()};
132 if (!particle)
133 {
134 return {};
135 }
136 // Before it spawns: an effect that starts active reads its control points then.
137 particle.SetControlPointRef(1, end);
138
139 KeyValues values;
140 values.Set("effect_name", effect).Set("start_active", 1).Set("origin", from);
141 line.Spawn(values);
142 return line;
143}
144
145Entity EntitySystem::SpawnLine(std::string_view effect, const Vector& from, const Vector& to)
146{
147 if (effect.empty())
148 {
149 return {};
150 }
151 KeyValues values;
152 values.Set("origin", to).Set("spawnflags", TargetSentToClients);
153 const Entity end = Spawn("info_target", values);
154 if (!end)
155 {
156 return {};
157 }
158 const Entity line = SpawnLine(effect, from, end.Ref());
159 if (!line)
160 {
161 end.Remove();
162 return {};
163 }
164 end.SetParent(line);
165 return line;
166}
167
168Entity EntitySystem::SpawnBeam(const Vector& from, const Vector& to, float width, Color color)
169{
170 // Not env_beam: a spawned env_beam brings the server down within seconds.
171 Entity line = Create("beam");
172 const Schema::CBeam beam{line.Raw()};
173 if (!beam)
174 {
175 return {};
176 }
177 beam.SetWidth(width);
178 beam.SetEndWidth(width);
179 beam.SetEndPos(to);
181
182 KeyValues values;
183 values.Set("origin", from);
184 line.Spawn(values);
185 return line;
186}
187
188} // namespace VoltMod
Entity Create(std::string_view className)
Entity SpawnProp(const PropSpec &prop)
Entity SpawnBeam(const Vector &from, const Vector &to, float width, Color color)
Entity Spawn(std::string_view className, KeyValues &values)
Entity SpawnLine(std::string_view effect, const Vector &from, EntityRef end)
Entity SpawnParticle(std::string_view effect, const Vector &origin, const QAngle &angles={0.0f, 0.0f, 0.0f})
Entity SpawnOutline(const Entity &target, std::string_view model, const PropGlow &glow)
A live entity, valid for this frame only.
Definition Entity.hpp:40
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 Follow(const Entity &parent, bool copyBones) const
Definition Entity.cpp:215
EntityRef Ref() const
Definition Entity.cpp:70
void Spawn(KeyValues &values) const
Definition Entity.cpp:135
void Remove() const
Definition Entity.cpp:174
Vector Origin() const
Definition Entity.cpp:106
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:58
Spawn keyvalues such as "origin", "spawnflags" or "effect_name".
Definition KeyValues.hpp:16
KeyValues & Set(std::string_view key, std::string_view value)
Definition KeyValues.cpp:24
void SetEndWidth(float value) const
Definition CBeam.cpp:48
void SetEndPos(Vector value) const
Definition CBeam.cpp:69
void SetWidth(float value) const
Definition CBeam.cpp:27
void SetControlPointRef(size_t index, EntityRef value) const
static constexpr int TargetSentToClients
static constexpr int GlowStateAlwaysOn
static constexpr int GlowTeamAny
std::expected< void, Error > Status
Definition Result.hpp:67
::QAngle QAngle
Definition Math.hpp:15
::Vector Vector
Definition Math.hpp:12
static constexpr int OutlineRenderAmount
An RGBA color in the engine's byte order, as m_clrRender and glow colors hold it.
Definition Color.hpp:15
static Error Unsupported(std::string detail)
Definition Result.hpp:57
static Error NotReady(std::string detail)
Definition Result.hpp:50
std::optional< PropGlow > Glow
std::string_view Skin
std::string_view Model