VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
GlowVision.cpp
Go to the documentation of this file.
6#include <utility>
7
8namespace VoltMod
9{
10
11// prop_dynamic keyvalues shared by the relay and glow clones.
12static constexpr int PropSpawnFlags = 256;
13static constexpr int GlowRangeUnits = 5000;
14static constexpr int GlowTeamAny = -1;
15static constexpr int GlowStateAlwaysOn = 3;
16static constexpr int GlowRenderAmt = 1;
17
18void GlowVision::DestroyPair(GlowPair& pair)
19{
20 if (!pair.Active())
21 {
22 return;
23 }
24
25 _visibility.ShowToEveryone(pair.Relay);
26 _visibility.ShowToEveryone(pair.Glow);
27
28 _entities.Get(pair.Glow).Remove();
29 _entities.Get(pair.Relay).Remove();
30
31 pair = {};
32}
33
34void GlowVision::CreatePair(int slot, GlowPair& pair)
35{
36 Pawn pawn = _entities.Pawn(slot);
37 if (!pawn)
38 {
39 return;
40 }
41
42 std::string model = pawn.ModelName();
43 const Team team = pawn.TeamNum();
44 if (model.empty())
45 {
46 return;
47 }
48
49 KeyValues relayKv;
50 relayKv.Set("model", model.c_str())
51 .Set("spawnflags", PropSpawnFlags)
52 .Set("rendermode", static_cast<int>(Schema::RenderMode_t::kRenderNone));
53 const Entity relay = _entities.Spawn("prop_dynamic", relayKv);
54 if (!relay)
55 {
56 return;
57 }
58
59 KeyValues glowKv;
60 glowKv.Set("model", model.c_str())
61 .Set("spawnflags", PropSpawnFlags)
62 .Set("glowcolor", team == Team::T ? _config.TerroristColor : _config.CtColor)
63 .Set("glowrange", GlowRangeUnits)
64 .Set("glowteam", GlowTeamAny)
65 .Set("glowstate", GlowStateAlwaysOn)
66 .Set("renderamt", GlowRenderAmt);
67 const Entity glow = _entities.Spawn("prop_dynamic", glowKv);
68 if (!glow)
69 {
70 relay.Remove();
71 return;
72 }
73
74 relay.AcceptInput("FollowEntity", "!activator", pawn);
75 glow.AcceptInput("FollowEntity", "!activator", relay);
76
77 pair.Relay = relay.Ref();
78 pair.Glow = glow.Ref();
79 pair.Team = team;
80 pair.Model = std::move(model);
81
82 ShowToViewers(pair.Relay);
83 ShowToViewers(pair.Glow);
84}
85
86void GlowVision::ShowToViewers(EntityRef entity)
87{
88 if (_viewerSlot >= 0)
89 {
90 _visibility.ShowOnlyTo(entity, _viewerSlot);
91 }
92 else
93 {
94 _visibility.ShowOnlyToTeam(entity, _viewerTeam);
95 }
96}
97
99{
100 for (int slot = 0; slot < MaxPlayers; ++slot)
101 {
102 auto& pair = _pairs[slot];
103
104 Pawn pawn = _entities.Pawn(slot);
105 const Team team = pawn.TeamNum();
106 // Hidden pawns never reach the viewer, so a clone would follow nothing.
107 bool desired = slot != _viewerSlot && pawn && pawn.IsAlive() && IsPlaying(team) &&
108 !_visibility.IsPawnHidden(slot) && (!_config.Filter || _config.Filter(slot));
109
110 if (pair.Active())
111 {
112 bool stale = !desired || team != pair.Team || !_entities.Get(pair.Relay) || !_entities.Get(pair.Glow) ||
113 pawn.ModelName() != pair.Model;
114 if (stale)
115 {
116 DestroyPair(pair);
117 }
118 }
119
120 if (!pair.Active() && desired)
121 {
122 CreatePair(slot, pair);
123 }
124 }
125}
126
128{
129 for (auto& pair : _pairs)
130 {
131 DestroyPair(pair);
132 }
133}
134
135} // namespace VoltMod
Entity Spawn(std::string_view className, KeyValues &values)
VoltMod::Pawn Pawn(int slot)
Entity Get(EntityRef ref)
void Remove() const
Definition Entity.cpp:165
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:19
std::string ModelName() const
Definition Pawn.cpp:201
void ShowOnlyToTeam(EntityRef entity, Team team)
bool IsPawnHidden(int slot) const
void ShowToEveryone(EntityRef entity)
void ShowOnlyTo(EntityRef entity, int slot)
constexpr bool IsPlaying(Team team)
Definition Team.hpp:18
static constexpr int GlowStateAlwaysOn
static constexpr int GlowTeamAny
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static constexpr int GlowRangeUnits
static constexpr int GlowRenderAmt
static constexpr int PropSpawnFlags
constexpr int MaxPlayers
Definition Slot.hpp:7
std::function< bool(int slot)> Filter