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
11static constexpr int PropSpawnFlags = 256;
12
13void GlowVision::DestroyPair(GlowPair& pair)
14{
15 if (!pair.Active())
16 {
17 return;
18 }
19
20 _visibility.ShowToEveryone(pair.Relay);
21 _visibility.ShowToEveryone(pair.Glow);
22
23 _entities.Get(pair.Glow).Remove();
24 _entities.Get(pair.Relay).Remove();
25
26 pair = {};
27}
28
29void GlowVision::CreatePair(int slot, GlowPair& pair)
30{
31 Pawn pawn = _entities.Pawn(slot);
32 if (!pawn)
33 {
34 return;
35 }
36
37 std::string model = pawn.ModelName();
38 const Team team = pawn.TeamNum();
39 if (model.empty())
40 {
41 return;
42 }
43
44 KeyValues relayKv;
45 relayKv.Set("model", model.c_str())
46 .Set("spawnflags", PropSpawnFlags)
47 .Set("rendermode", static_cast<int>(Schema::RenderMode_t::kRenderNone));
48 const Entity relay = _entities.Spawn("prop_dynamic", relayKv);
49 if (!relay)
50 {
51 return;
52 }
53
54 relay.AcceptInput("FollowEntity", "!activator", pawn);
55 const Entity glow =
56 _entities.SpawnOutline(relay, model, {.Color = team == Team::T ? _config.TerroristColor : _config.CtColor});
57 if (!glow)
58 {
59 relay.Remove();
60 return;
61 }
62
63 pair.Relay = relay.Ref();
64 pair.Glow = glow.Ref();
65 pair.Team = team;
66 pair.Model = std::move(model);
67
68 ShowToViewers(pair.Relay);
69 ShowToViewers(pair.Glow);
70}
71
72void GlowVision::ShowToViewers(EntityRef entity)
73{
74 if (_viewerSlot >= 0)
75 {
76 _visibility.ShowOnlyTo(entity, _viewerSlot);
77 }
78 else
79 {
80 _visibility.ShowOnlyToTeam(entity, _viewerTeam);
81 }
82}
83
85{
86 for (int slot = 0; slot < MaxPlayers; ++slot)
87 {
88 auto& pair = _pairs[slot];
89
90 Pawn pawn = _entities.Pawn(slot);
91 const Team team = pawn.TeamNum();
92 // Hidden pawns never reach the viewer, so a clone would follow nothing.
93 bool desired = slot != _viewerSlot && pawn && pawn.IsAlive() && IsPlaying(team) &&
94 !_visibility.IsPawnHidden(slot) && (!_config.Filter || _config.Filter(slot));
95
96 if (pair.Active())
97 {
98 bool stale = !desired || team != pair.Team || !_entities.Get(pair.Relay) || !_entities.Get(pair.Glow) ||
99 pawn.ModelName() != pair.Model;
100 if (stale)
101 {
102 DestroyPair(pair);
103 }
104 }
105
106 if (!pair.Active() && desired)
107 {
108 CreatePair(slot, pair);
109 }
110 }
111}
112
114{
115 for (auto& pair : _pairs)
116 {
117 DestroyPair(pair);
118 }
119}
120
121} // namespace VoltMod
VoltMod::Pawn Pawn(int slot)
Entity Get(EntityRef ref)
void Remove() const
Definition Entity.cpp:174
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
std::string ModelName() const
Definition Pawn.cpp:266
bool IsAlive() const
Definition Pawn.hpp:45
void ShowToEveryone(EntityRef entity)
constexpr bool IsPlaying(Team team)
Definition Team.hpp:18
static constexpr int PropSpawnFlags
constexpr int MaxPlayers
Definition Slot.hpp:7