VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Trace.cpp
Go to the documentation of this file.
3#include <bspflags.h>
4#include <const.h>
5#include <entity2/entityidentity.h>
6#include <entity2/entityinstance.h>
7#include <gametrace.h>
8#include <ray.h>
9#include <utility>
10
11namespace VoltMod
12{
13
14/** Windows are see-through and clips are invisible, so neither hides a player. */
17
19{
20 switch (layers)
21 {
23 return SightMask;
25 return SurfacesMask;
26 default:
27 return MASK_PLAYERSOLID;
28 }
29}
30
31// No entity iteration: the engine never calls back into this module through the filter.
33{
35 filter.SetPassEntity1(options.Ignore1.Raw());
36 filter.SetPassEntity2(options.Ignore2.Raw());
37 filter.SetPassEntityOwner1(options.IgnoreOwnedBy.Raw());
38 return filter;
39}
40
41static EntityRef RefOf(const CEntityInstance* entity)
42{
43 if (!entity || !entity->m_pEntity)
44 {
45 return {};
46 }
47 return {static_cast<uint32_t>(entity->GetRefEHandle().ToInt())};
48}
49
51{
52 return TraceHit{.Hit = trace.DidHit(),
53 .Fraction = trace.m_flFraction,
54 .End = trace.m_vEndPos,
55 .Normal = trace.m_vHitNormal,
56 .HitEntity = RefOf(trace.m_pEnt),
57 .HitWorld = trace.DidHit() && trace.m_pEnt && trace.m_pEnt->m_pEntity &&
58 trace.m_pEnt->GetEntityIndex().Get() == 0};
59}
60
61// CNavPhysicsInterface carries no state, so its class table stands in for the object, as in HookVirtual.
63{
64 return reinterpret_cast<EngineNavPhysics*>(&table);
65}
66
68{
69 if (!_bindings.NavTraceLine)
70 {
71 return std::unexpected(Error::Unsupported("the Nav_TraceLine vtable slot did not bind"));
72 }
73 return {};
74}
75
77{
79 {
80 return std::unexpected(available.error());
81 }
82
84 void* table = _bindings.NavTraceLine.Table();
85 // The Linux engine writes the trace with aligned SIMD stores.
86 alignas(16) CGameTrace trace;
87 _bindings.NavTraceLine(NavPhysics(table), &from, &to, &filter, &trace);
88 return ToHit(trace);
89}
90
92 const TraceOptions& options) const
93{
94 if (!_bindings.NavTraceShape)
95 {
96 return std::unexpected(Error::Unsupported("the Nav_TraceShape vtable slot did not bind"));
97 }
98
99 const Ray_t ray(mins, maxs);
101 void* table = _bindings.NavTraceShape.Table();
102 alignas(16) CGameTrace trace;
103 _bindings.NavTraceShape(NavPhysics(table), &ray, &from, &to, &filter, &trace);
104 return ToHit(trace);
105}
106
108{
109 const Vector eyes = pawn.EyePosition();
110 return Line(eyes, eyes + AngleToForward(pawn.EyeAngles()) * distance, {.Layers = layers, .Ignore1 = pawn});
111}
112
114{
116 if (!hit)
117 {
118 return std::unexpected(std::move(hit).error());
119 }
120 return !hit->Hit;
121}
122
123} // namespace VoltMod
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:19
Result< TraceHit > Line(const Vector &from, const Vector &to, const TraceOptions &options={}) const
Definition Trace.cpp:76
Result< TraceHit > Box(const Vector &from, const Vector &to, const Vector &mins, const Vector &maxs, const TraceOptions &options={}) const
Definition Trace.cpp:91
Result< bool > Clear(const Vector &from, const Vector &to, const TraceOptions &options={}) const
Definition Trace.cpp:113
Status Available() const
Definition Trace.cpp:67
Result< TraceHit > FromEyes(const Pawn &pawn, float distance, TraceLayers layers=TraceLayers::Solid) const
Definition Trace.cpp:107
static CTraceFilter MakeFilter(const TraceOptions &options)
Definition Trace.cpp:32
static EntityRef RefOf(const CEntityInstance *entity)
Definition Trace.cpp:41
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
Vector AngleToForward(const QAngle &angles)
Definition Math.cpp:7
static TraceHit ToHit(const CGameTrace &trace)
Definition Trace.cpp:50
static EngineNavPhysics * NavPhysics(void *&table)
Definition Trace.cpp:62
std::expected< void, Error > Status
Definition Result.hpp:67
::Vector Vector
Definition Math.hpp:11
static uint64_t MaskOf(TraceLayers layers)
Definition Trace.cpp:18
TraceLayers
Definition Trace.hpp:15
@ Sight
what hides one player from another
@ Surfaces
every drawn surface and the sky; no player clips or players
static constexpr uint64_t SightMask
Definition Trace.cpp:15
std::expected< T, Error > Result
Definition Result.hpp:64
static constexpr uint64_t SurfacesMask
Definition Trace.cpp:16
VirtualFn< void(EngineNavPhysics *, const Ray_t *, const Vector *, const Vector *, CTraceFilter *, CGameTrace *)> NavTraceShape
Definition Bindings.hpp:82
VirtualFn< bool(EngineNavPhysics *, const Vector *, const Vector *, CTraceFilter *, CGameTrace *)> NavTraceLine
Definition Bindings.hpp:80
static Error Unsupported(std::string detail)
Definition Result.hpp:57
bool Hit
also true when the start is inside a solid
Definition Trace.hpp:32