VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Visibility.cpp
Go to the documentation of this file.
5#include <algorithm>
6#include <cstdint>
7#include <engine2/isource2server.h>
8#include <entity2/entityinstance.h>
9#include <entityhandle.h>
10#include <networksystem/networkvar.h>
11#include <utility>
12
13namespace VoltMod
14{
15
16static constexpr int MaxIndicesPerPlayer = 24;
17
18// The schema's elements are CHandle<T>, which is a CEntityHandle.
19using HandleVector = CNetworkUtlVectorBase<CEntityHandle>;
20
21struct HiddenPlayer
22{
23 int Slot = -1;
25 CEntityInstance* Pawn = nullptr;
26 int IndexCount = 0;
27 std::array<int, MaxIndicesPerPlayer> PawnIndices{}; // pawn, weapons and wearables
28};
29
30static void AddIndex(HiddenPlayer& player, int index)
31{
32 if (index > 0 && player.IndexCount < MaxIndicesPerPlayer)
33 {
34 player.PawnIndices[player.IndexCount++] = index;
35 }
36}
37
38static void AddHandleVector(EntitySystem& entities, HiddenPlayer& player, const void* vector)
39{
40 const auto* handles = static_cast<const HandleVector*>(vector);
41 if (!handles)
42 {
43 return;
44 }
45 for (int i = 0; i < handles->Count() && i < MaxIndicesPerPlayer; ++i)
46 {
47 AddIndex(player, entities.Get(EntityRef{static_cast<uint32_t>(handles->Element(i).ToInt())}).Index());
48 }
49}
50
51// The pawn `recipientSlot` spectates, which stays transmitted.
52static CEntityInstance* ObserverTarget(EntitySystem& entities, int recipientSlot)
53{
54 // The observer pawn carries the camera while dead or spectating.
55 Pawn pawn = entities.Controller(recipientSlot).InputPawn();
56 const Schema::CPlayer_ObserverServices services = pawn.ObserverServices();
57 if (!services)
58 {
59 return nullptr;
60 }
61
62 return entities.Get(services.ObserverTarget()).Raw();
63}
64
65static void CollectHiddenPlayer(EntitySystem& entities, int slot, bool pawnHidden, bool controllerHidden,
66 HiddenPlayer& out)
67{
68 out.Slot = slot;
69
70 Controller controller = entities.Controller(slot);
71 if (!controller)
72 {
73 return;
74 }
75
76 if (controllerHidden)
77 {
78 out.ControllerIndex = controller.Index();
79 }
80
81 if (!pawnHidden)
82 {
83 return;
84 }
85
86 Pawn pawn = controller.Pawn();
87 if (!pawn)
88 {
89 return;
90 }
91
92 out.Pawn = pawn.Raw();
93 AddIndex(out, pawn.Index());
94
95 AddHandleVector(entities, out, pawn.WeaponServices().MyWeapons());
96 AddHandleVector(entities, out, pawn.MyWearables());
97}
98
99Visibility::Visibility(EntitySystem& entities, SlotEvents& slots) : _entities(entities)
100{
101 _slotListener = slots.Changed += [this](int slot) {
102 if (!IsValidSlot(slot))
103 {
104 return;
105 }
106 _state[slot] = {};
107 std::erase_if(_private, [slot](const PrivateEntity& e) { return e.Viewer == slot; });
108 };
109}
110
111void Visibility::SetPawnHidden(int slot, bool hidden)
112{
113 if (IsValidSlot(slot))
114 {
115 _state[slot].PawnHidden = hidden;
116 }
117}
118
119void Visibility::SetControllerHidden(int slot, bool hidden)
120{
121 if (IsValidSlot(slot))
122 {
123 _state[slot].ControllerHidden = hidden;
124 }
125}
126
127bool Visibility::IsPawnHidden(int slot) const
128{
129 return IsValidSlot(slot) && _state[slot].PawnHidden;
130}
131
132void Visibility::ShowOnlyTo(EntityRef entity, int slot)
133{
134 if (!entity || !IsValidSlot(slot))
135 {
136 return;
137 }
138
139 SetPrivate({.Entity = entity, .Viewer = slot});
140}
141
143{
144 if (!entity || !IsPlaying(team))
145 {
146 return;
147 }
148
149 SetPrivate({.Entity = entity, .ShownTo = team});
150}
151
153{
154 if (!entity || !IsPlaying(team))
155 {
156 return;
157 }
158
159 SetPrivate({.Entity = entity, .HiddenFrom = team});
160}
161
162void Visibility::SetPrivate(const PrivateEntity& entry)
163{
164 const auto found = std::ranges::find(_private, entry.Entity, &PrivateEntity::Entity);
165 if (found != _private.end())
166 {
167 *found = entry;
168 }
169 else
170 {
171 _private.push_back(entry);
172 }
173}
174
176{
177 std::erase_if(_private, [entity](const PrivateEntity& e) { return e.Entity == entity; });
178}
179
180std::shared_ptr<GlowVision> Visibility::CreateGlow(int viewerSlot, GlowConfig config)
181{
182 return std::make_shared<GlowVision>(_entities, *this, viewerSlot, std::move(config));
183}
184
185std::shared_ptr<GlowVision> Visibility::CreateGlow(Team viewerTeam, GlowConfig config)
186{
187 return std::make_shared<GlowVision>(_entities, *this, viewerTeam, std::move(config));
188}
189
190bool Visibility::PrivateEntity::IsHiddenFrom(int recipient, Team recipientTeam) const
191{
192 if (Viewer >= 0)
193 {
194 return Viewer != recipient;
195 }
196 if (ShownTo != Team::None)
197 {
198 return ShownTo != recipientTeam;
199 }
200 return HiddenFrom == recipientTeam;
201}
202
203void Visibility::OnCheckTransmit(CCheckTransmitInfo** infoList, int infoCount)
204{
205 // Even unbound: indices are recycled, and the list must not grow.
206 for (auto& entry : _private)
207 {
208 const Entity entity = _entities.Get(entry.Entity);
209 entry.Index = entity ? entity.Index() : -1;
210 }
211 std::erase_if(_private, [](const PrivateEntity& e) { return e.Index <= 0; });
212
213 if (!infoList)
214 {
215 return;
216 }
217 if (_private.empty() && std::ranges::none_of(_state, &SlotState::Any))
218 {
219 return;
220 }
221
222 // Shared by every recipient; only the self and spectator exemptions vary.
223 std::array<HiddenPlayer, MaxPlayers> hidden;
224 int hiddenCount = 0;
225 for (int slot = 0; slot < MaxPlayers; ++slot)
226 {
227 const auto& state = _state[slot];
228 if (state.Any())
229 {
230 CollectHiddenPlayer(_entities, slot, state.PawnHidden, state.ControllerHidden, hidden[hiddenCount++]);
231 }
232 }
233
234 for (int i = 0; i < infoCount; ++i)
235 {
236 auto* info = infoList[i];
237 if (!info || !info->m_pTransmitEntity)
238 {
239 continue;
240 }
241
242 const int recipient = info->m_nPlayerSlot.Get();
243 CEntityInstance* observed = hiddenCount > 0 ? ObserverTarget(_entities, recipient) : nullptr;
244 const Team recipientTeam = _entities.Controller(recipient).TeamNum();
245
246 for (int h = 0; h < hiddenCount; ++h)
247 {
248 const auto& player = hidden[h];
249 if (player.Slot == recipient)
250 {
251 continue;
252 }
253
254 if (observed != player.Pawn)
255 {
256 for (int n = 0; n < player.IndexCount; ++n)
257 {
258 info->m_pTransmitEntity->Clear(player.PawnIndices[n]);
259 }
260 }
261
262 if (player.ControllerIndex > 0)
263 {
264 info->m_pTransmitEntity->Clear(player.ControllerIndex);
265 }
266 }
267
268 for (const auto& entry : _private)
269 {
270 if (entry.IsHiddenFrom(recipient, recipientTeam))
271 {
272 info->m_pTransmitEntity->Clear(entry.Index);
273 }
274 }
275 }
276}
277
278} // namespace VoltMod
A player's identity behind a slot: name, money, team, scoreboard row. It survives death and respawn; ...
VoltMod::Pawn InputPawn() const
VoltMod::Pawn Pawn() const
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
VoltMod::Controller Controller(int slot)
Entity Get(EntityRef ref)
A live entity, valid for this frame only.
Definition Entity.hpp:40
int Index() const
Definition Entity.cpp:65
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:58
A player turned into a nameless spectator nobody else sees, who sees every player through walls.
std::array< int, MaxIndicesPerPlayer > PawnIndices
CEntityInstance * Pawn
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Event< int > Changed
void ShowOnlyToTeam(EntityRef entity, Team team)
void SetPawnHidden(int slot, bool hidden)
bool IsPawnHidden(int slot) const
void HideFromTeam(EntityRef entity, Team team)
void ShowToEveryone(EntityRef entity)
void SetControllerHidden(int slot, bool hidden)
std::shared_ptr< GlowVision > CreateGlow(int viewerSlot, GlowConfig config={})
void ShowOnlyTo(EntityRef entity, int slot)
void OnCheckTransmit(CCheckTransmitInfo **infoList, int infoCount)
Visibility(EntitySystem &entities, SlotEvents &slots)
constexpr bool IsPlaying(Team team)
Definition Team.hpp:18
CNetworkUtlVectorBase< CEntityHandle > HandleVector
static void CollectHiddenPlayer(EntitySystem &entities, int slot, bool pawnHidden, bool controllerHidden, HiddenPlayer &out)
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
static CEntityInstance * ObserverTarget(EntitySystem &entities, int recipientSlot)
static void AddHandleVector(EntitySystem &entities, HiddenPlayer &player, const void *vector)
constexpr int MaxPlayers
Definition Slot.hpp:7
static constexpr int MaxIndicesPerPlayer
static void AddIndex(HiddenPlayer &player, int index)