VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Pawn.cpp
Go to the documentation of this file.
6#include <algorithm>
7#include <entityhandle.h>
8#include <networksystem/networkvar.h>
9#include <shareddefs.h>
10#include <string>
11#include <utility>
12
13namespace VoltMod
14{
15
16static_assert(std::to_underlying(ObserverMode::None) == OBS_MODE_NONE);
17static_assert(std::to_underlying(ObserverMode::Fixed) == OBS_MODE_FIXED);
18static_assert(std::to_underlying(ObserverMode::InEye) == OBS_MODE_IN_EYE);
19static_assert(std::to_underlying(ObserverMode::Chase) == OBS_MODE_CHASE);
20static_assert(std::to_underlying(ObserverMode::Roaming) == OBS_MODE_ROAMING);
21
22// Passed by value across the plugin boundary, so the layout is ABI.
23// Further ahead than a client predicts.
24static constexpr int HoldFireTicks = 24;
25static constexpr int HoldFireStep = 12;
26
27int HoldFireTick(const Clock& clock)
28{
29 const int until = clock.Tick() + HoldFireTicks;
30 return (until / HoldFireStep + 1) * HoldFireStep;
31}
32
33static_assert(sizeof(Pawn) <= 160, "Pawn is a frame-local value; keep the field list tight.");
34
36{
37 return Origin() + ViewOffset();
38}
39
41{
42 return AngleToForward(EyeAngles());
43}
44
45void Pawn::SetGodmode(bool on) const
46{
47 SetFlags(on ? (Flags() | FL_GODMODE) : (Flags() & ~FL_GODMODE));
48}
49
50void Pawn::SetVelocity(Vector velocity) const
51{
52 // Written directly: a Teleport carrying only a velocity has crashed CS2 builds.
53 SetAbsVelocity(velocity);
54 // Otherwise the engine grounds the pawn again on the next tick.
55 SetFlags(Flags() & ~FL_ONGROUND);
56}
57
58bool Pawn::Heal(int amount) const
59{
60 const int health = Health();
61 const int healed = std::min(health + amount, MaxHealth());
62 if (!IsAlive() || healed <= health)
63 {
64 return false;
65 }
66 SetHealth(healed);
67 return true;
68}
69
70Entity Pawn::GiveItem(std::string_view item) const
71{
72 const Schema::CPlayer_ItemServices services = ItemServices();
73 if (!_sys || !services || item.empty())
74 {
75 return {};
76 }
77 const auto& give = _sys->Bindings().GiveNamedItem;
78 if (!give)
79 {
80 return {};
81 }
82
83 const std::string className(item);
84 if (void* given = give(services.Base(), className.c_str()))
85 {
86 return {*_sys, static_cast<CEntityInstance*>(given)};
87 }
88
89 // The engine refuses a weapon only the other team can buy; the swap is undone before anyone sees it.
90 const VoltMod::Team team = Team();
91 const VoltMod::Team other = Opposite(team);
92 if (other == VoltMod::Team::None)
93 {
94 return {};
95 }
96 SetTeamNum(other);
97 void* given = give(services.Base(), className.c_str());
98 SetTeamNum(team);
99
100 if (!given)
101 {
102 Log::Warn("The engine refused '{}' for both teams.", item);
103 }
104 return {*_sys, static_cast<CEntityInstance*>(given)};
105}
106
108{
109 return GiveItem(Team() == VoltMod::Team::T ? "weapon_knife_t" : "weapon_knife");
110}
111
112bool Pawn::StripWeapons(bool removeSuit) const
113{
114 const Schema::CPlayer_ItemServices services = ItemServices();
115 if (!_sys || !services || !_sys->Bindings().RemoveAllItems)
116 {
117 return false;
118 }
119 _sys->Bindings().RemoveAllItems(services.Base(), removeSuit);
120 return true;
121}
122
123std::vector<Entity> Pawn::Weapons() const
124{
125 std::vector<Entity> weapons;
126 const Schema::CPlayer_WeaponServices services = WeaponServices();
127 const auto* handles =
128 services ? static_cast<const CNetworkUtlVectorBase<CEntityHandle>*>(services.MyWeapons()) : nullptr;
129 if (!_sys || !handles)
130 {
131 return weapons;
132 }
133 for (int i = 0; i < handles->Count(); ++i)
134 {
135 if (const Entity weapon = _sys->Get(EntityRef{static_cast<uint32_t>(handles->Element(i).ToInt())}))
136 {
137 weapons.push_back(weapon);
138 }
139 }
140 return weapons;
141}
142
143void Pawn::SelectWeapon(const Entity& weapon) const
144{
145 const Schema::CPlayer_WeaponServices services = WeaponServices();
146 const bool bound = _sys && services && _sys->Bindings().SelectItem;
147 if (!weapon || !bound)
148 {
149 return;
150 }
151 const bool carried = std::ranges::any_of(Weapons(), [&](const Entity& held) { return held.Ref() == weapon.Ref(); });
152 if (!carried)
153 {
154 return;
155 }
156 _sys->Bindings().SelectItem(static_cast<EngineWeaponServices*>(services.Base()), weapon.Raw(), 0);
157}
158
159void Pawn::RemoveWeapon(const Entity& weapon) const
160{
161 const Schema::CPlayer_WeaponServices services = WeaponServices();
162 const bool inHand = weapon && _sys && services && services.ActiveWeapon() == weapon.Ref();
163 // Removing the weapon in hand otherwise leaves the pawn with empty hands.
164 if (inHand && IsAlive())
165 {
166 SelectWeapon(_sys->Get(services.LastWeapon()));
167 }
168 weapon.RemoveAfter(0.0f);
169}
170
172{
173 const Schema::CPlayer_WeaponServices services = WeaponServices();
174 return services && _sys ? _sys->Get(services.ActiveWeapon()) : Entity{};
175}
176
177void Pawn::HoldFire(int tick) const
178{
180 if (!weapon)
181 {
182 return;
183 }
184 if (weapon.NextPrimaryAttackTick() < tick)
185 {
186 weapon.SetNextPrimaryAttackTick(tick);
187 }
188 if (weapon.NextSecondaryAttackTick() < tick)
189 {
190 weapon.SetNextSecondaryAttackTick(tick);
191 }
192}
193
194void Pawn::HoldFire(const Clock& clock) const
195{
196 HoldFire(HoldFireTick(clock));
197}
198
199void Pawn::HoldCrouch(float amount) const
200{
201 const Schema::CCSPlayer_MovementServices movement{MovementServices().Base()};
202 if (!movement)
203 {
204 return;
205 }
206 // Each write is re-sent, so a crouch already held is left alone.
207 const bool crouched =
208 movement.DuckOverride() && movement.Ducked() && movement.DuckAmount() == amount && (Flags() & FL_DUCKING) != 0;
209 if (crouched)
210 {
211 return;
212 }
213 // The override keeps the game's own duck logic crouched; the rest shows it at once.
214 movement.SetDuckOverride(true);
215 movement.SetDucked(true);
216 movement.SetDuckAmount(amount);
217 SetFlags(Flags() | FL_DUCKING);
218}
219
221{
222 const Schema::CCSPlayer_MovementServices movement{MovementServices().Base()};
223 if (!movement)
224 {
225 return;
226 }
227 movement.SetDuckOverride(false);
228 // A crouch held past 1 drops back to an ordinary one.
229 if (movement.DuckAmount() > 1.0f)
230 {
231 movement.SetDuckAmount(1.0f);
232 }
233}
234
235void Pawn::Slay() const
236{
237 if (_e && _sys && _sys->Bindings().CommitSuicide)
238 {
239 _sys->Bindings().CommitSuicide(_e, false, true);
240 }
241}
242
244{
245 return (_sys && ClassName() == "player") ? Pawn{*_sys, _e} : Pawn{};
246}
247
249{
250 const Schema::CPlayer_ObserverServices services = ObserverServices();
251 return services ? static_cast<VoltMod::ObserverMode>(services.ObserverMode()) : VoltMod::ObserverMode::None;
252}
253
255{
256 const Schema::CPlayer_ObserverServices services = ObserverServices();
257 if (!services)
258 {
259 return std::unexpected(Error::NotReady("observer services unavailable"));
260 }
261
262 services.SetObserverMode(std::to_underlying(value));
263 return {};
264}
265
266std::string Pawn::ModelName() const
267{
268 const Schema::CSkeletonInstance skeleton{BodyComponent().SceneNode().Base()};
269 if (!skeleton)
270 {
271 return {};
272 }
273
274 const char* path = skeleton.ModelState().ModelName();
275 return path ? std::string(path) : std::string{};
276}
277
279{
280 return _sys ? _sys->Controller(Slot()) : VoltMod::Controller{};
281}
282
283int Pawn::Slot() const
284{
285 // Controllers sit at entity index slot + 1.
286 const Entity controller = _sys ? _sys->Get(ControllerRef()) : Entity{};
287 const int slot = controller.Index() - 1;
288 return controller && IsValidSlot(slot) ? slot : -1;
289}
290
291} // namespace VoltMod
The engine's simulation clock, read straight from IVEngineServer2::GetServerGlobals().
Definition Clock.hpp:19
int Tick() const
Definition Clock.cpp:15
A player's identity behind a slot: name, money, team, scoreboard row. It survives death and respawn; ...
int Index() const
Definition Entity.cpp:65
Pawn AsPawn() const
Definition Pawn.cpp:243
void RemoveAfter(float seconds) const
Definition Entity.cpp:190
std::string_view ClassName() const
Definition Entity.cpp:75
EntitySystem * _sys
Definition Entity.hpp:42
EntityRef Ref() const
Definition Entity.cpp:70
Vector Origin() const
Definition Entity.cpp:106
CEntityInstance * _e
Definition Entity.hpp:43
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:58
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
void HoldCrouch(float amount=1.0f) const
Definition Pawn.cpp:199
std::string ModelName() const
Definition Pawn.cpp:266
Entity GiveKnife() const
Definition Pawn.cpp:107
bool IsAlive() const
Definition Pawn.hpp:45
void SetGodmode(bool on) const
Definition Pawn.cpp:45
Vector EyePosition() const
Definition Pawn.cpp:35
void Slay() const
Definition Pawn.cpp:235
Vector EyeForward() const
Definition Pawn.cpp:40
void ReleaseCrouch() const
Definition Pawn.cpp:220
bool StripWeapons(bool removeSuit=true) const
Definition Pawn.cpp:112
void SelectWeapon(const Entity &weapon) const
Definition Pawn.cpp:143
std::vector< Entity > Weapons() const
Definition Pawn.cpp:123
Entity GiveItem(std::string_view item) const
Definition Pawn.cpp:70
VoltMod::ObserverMode ObserverMode() const
Definition Pawn.cpp:248
void RemoveWeapon(const Entity &weapon) const
Definition Pawn.cpp:159
Entity() noexcept
Definition Entity.hpp:47
Entity ActiveWeapon() const
Definition Pawn.cpp:171
Status SetObserverMode(VoltMod::ObserverMode mode) const
Definition Pawn.cpp:254
bool Heal(int amount) const
Definition Pawn.cpp:58
void HoldFire(int tick) const
Definition Pawn.cpp:177
VoltMod::Controller Controller() const
Definition Pawn.cpp:278
void SetVelocity(Vector velocity) const
Definition Pawn.cpp:50
int Slot() const
Definition Pawn.cpp:283
void SetNextPrimaryAttackTick(int32_t value) const
void SetNextSecondaryAttackTick(int32_t value) const
const char * ModelName() const
void * Base() const noexcept
Definition View.hpp:33
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
std::expected< void, Error > Status
Definition Result.hpp:67
Vector AngleToForward(const QAngle &angles)
Definition Math.cpp:9
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
::Vector Vector
Definition Math.hpp:12
static constexpr int HoldFireStep
Definition Pawn.cpp:25
constexpr Team Opposite(Team team)
Definition Team.hpp:24
int HoldFireTick(const Clock &clock)
Definition Pawn.cpp:27
static constexpr int HoldFireTicks
Definition Pawn.cpp:24
static Error NotReady(std::string detail)
Definition Result.hpp:50