VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
EntitySystemSpawn.cpp
Go to the documentation of this file.
4#include <string>
5
6namespace VoltMod
7{
8
9// The engine's SolidType_t values the `solid` key takes.
10static constexpr int SolidNone = 0;
11static constexpr int SolidVPhysics = 6;
12
14{
15 if (!_interfaces.GameResourceService)
16 {
17 return std::unexpected(Error::NotReady("IGameResourceService not available"));
18 }
19 if (!_bindings.GameEntitySystem)
20 {
21 return std::unexpected(Error::Unsupported("the GameEntitySystem offset did not bind"));
22 }
23 if (!_bindings.CreateEntityByName || !_bindings.DispatchSpawn)
24 {
25 return std::unexpected(Error::Unsupported("CreateEntityByName or DispatchSpawn did not bind"));
26 }
27 return {};
28}
29
31{
32 // Checked here so every create-then-spawn path gets it.
33 if (!Available() || className.empty())
34 {
35 return {};
36 }
37 return {*this, _bindings.CreateEntityByName(std::string(className).c_str(), -1)};
38}
39
41{
43 entity.Spawn(values);
44 return entity;
45}
46
48{
50 values.Set("model", prop.Model)
51 .Set("origin", prop.Origin)
52 .Set("angles", prop.Angles)
53 .Set("solid", prop.Solid ? SolidVPhysics : SolidNone);
54 if (!prop.Skin.empty())
55 {
56 values.Set("skin", prop.Skin);
57 }
58 if (!prop.CastsShadow)
59 {
60 values.Set("disableshadows", 1);
61 }
62
63 Entity entity = Create("prop_dynamic");
64 // The physics shapes take their owner as they are created.
65 if (prop.Owner)
66 {
67 entity.SetOwnerEntity(prop.Owner);
68 }
69 entity.Spawn(values);
70 // "solid" 0 alone leaves the model's hull blocking traces.
71 if (entity && !prop.Solid)
72 {
73 entity.AcceptInput("DisableCollision");
74 }
75 if (entity && prop.Scale != 1.0f)
76 {
77 entity.SetScale(prop.Scale);
78 }
79 return entity;
80}
81
83{
84 if (effect.empty())
85 {
86 return {};
87 }
89 values.Set("effect_name", effect).Set("start_active", 1).Set("origin", origin).Set("angles", angles);
90 return Spawn("info_particle_system", values);
91}
92
94{
95 if (effect.empty())
96 {
97 return {};
98 }
99 Entity line = Create("info_particle_system");
101 if (!particle)
102 {
103 return {};
104 }
105 // Before it spawns: an effect that starts active reads its control points then.
107
109 values.Set("effect_name", effect).Set("start_active", 1).Set("origin", from);
110 line.Spawn(values);
111 return line;
112}
113
115{
116 // Not env_beam: a spawned env_beam brings the server down within seconds.
117 Entity line = Create("beam");
118 const Schema::CBeam beam{line.Raw()};
119 if (!beam)
120 {
121 return {};
122 }
124 beam.SetEndWidth(width);
125 beam.SetEndPos(to);
127
129 values.Set("origin", from);
130 line.Spawn(values);
131 return line;
132}
133
134} // 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})
A live entity, valid for this frame only.
Definition Entity.hpp:29
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 SetWidth(float value) const
Definition CBeam.cpp:26
void SetControlPointRef(size_t index, EntityRef value) const
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static constexpr int SolidNone
::QAngle QAngle
Definition Math.hpp:14
std::expected< void, Error > Status
Definition Result.hpp:67
::Vector Vector
Definition Math.hpp:11
static constexpr int SolidVPhysics
Fn< void(CEntityInstance *, CEntityKeyValues *)> DispatchSpawn
Definition Bindings.hpp:27
Fn< CEntityInstance *(const char *, int)> CreateEntityByName
Definition Bindings.hpp:25
Offset< CGameEntitySystem * > GameEntitySystem
Definition Bindings.hpp:94
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
IGameResourceService * GameResourceService