VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Trace.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <optional>
10
11namespace VoltMod
12{
13
14/** Which interaction layers a trace stops at. */
15enum class TraceLayers
16{
17 Sight, ///< what hides one player from another
18 Solid, ///< what a player body collides with, players included
19 Surfaces, ///< every drawn surface and the sky; no player clips or players
20};
21
23{
27 /** Also skips everything this entity owns; see @ref PropSpec::Owner. */
29};
30
32{
33 bool Hit = false; ///< also true when the start is inside a solid
34 float Fraction = 1.0f; ///< share of the path travelled
36 Vector Normal; ///< zero without a hit
37 EntityRef HitEntity; ///< the world included
38 bool HitWorld = false;
39};
40
41/**
42 * @brief `runtime.Trace`: line and box traces against the physics world. Game-thread only.
43 *
44 * @code
45 * const auto clear = runtime.Trace.Clear(eye, target, {.Ignore1 = self, .Ignore2 = other});
46 * if (clear && *clear)
47 * ...
48 * @endcode
49 */
50class Trace
51{
52public:
53 /** @p bindings must outlive this service. */
54 explicit Trace(const Bindings& bindings) : _bindings(bindings) {}
55 Trace(const Trace&) = delete;
56 Trace& operator=(const Trace&) = delete;
57
58 /** Unsupported when the Nav_TraceLine slot did not bind. */
59 Status Available() const;
60
61 Result<TraceHit> Line(const Vector& from, const Vector& to, const TraceOptions& options = {}) const;
62
63 /** Sweeps @p mins..@p maxs along the path; `End` is the box origin. Unsupported when the
64 * Nav_TraceShape slot did not bind. */
65 Result<TraceHit> Box(const Vector& from, const Vector& to, const Vector& mins, const Vector& maxs,
66 const TraceOptions& options = {}) const;
67
68 /** Along @p pawn's aim, ignoring the pawn. */
69 Result<TraceHit> FromEyes(const Pawn& pawn, float distance, TraceLayers layers = TraceLayers::Solid) const;
70
71 Result<bool> Clear(const Vector& from, const Vector& to, const TraceOptions& options = {}) const;
72
73 /** @name What a trace hit
74 * As @ref Line, @ref Box and @ref FromEyes, empty both when nothing is in the way and when the trace
75 * cannot run. Use those where the two need telling apart. */
76 /** @{ */
77 std::optional<TraceHit> FirstHit(const Vector& from, const Vector& to, const TraceOptions& options = {}) const;
78 std::optional<TraceHit> FirstHit(const Vector& from, const Vector& to, const Vector& mins, const Vector& maxs,
79 const TraceOptions& options = {}) const;
80 std::optional<TraceHit> FirstHit(const Pawn& pawn, float distance, TraceLayers layers = TraceLayers::Solid) const;
81 /** @} */
82
83private:
84 const Bindings& _bindings;
85};
86
87} // namespace VoltMod
A live entity, valid for this frame only.
Definition Entity.hpp:40
Result< TraceHit > Line(const Vector &from, const Vector &to, const TraceOptions &options={}) const
Definition Trace.cpp:82
Result< TraceHit > Box(const Vector &from, const Vector &to, const Vector &mins, const Vector &maxs, const TraceOptions &options={}) const
Definition Trace.cpp:97
Trace(const Bindings &bindings)
Definition Trace.hpp:54
std::optional< TraceHit > FirstHit(const Vector &from, const Vector &to, const TraceOptions &options={}) const
Definition Trace.cpp:119
Result< bool > Clear(const Vector &from, const Vector &to, const TraceOptions &options={}) const
Definition Trace.cpp:135
Status Available() const
Definition Trace.cpp:73
Trace & operator=(const Trace &)=delete
Result< TraceHit > FromEyes(const Pawn &pawn, float distance, TraceLayers layers=TraceLayers::Solid) const
Definition Trace.cpp:113
Trace(const Trace &)=delete
std::expected< void, Error > Status
Definition Result.hpp:67
std::expected< T, Error > Result
Definition Result.hpp:64
::Vector Vector
Definition Math.hpp:12
TraceLayers
Definition Trace.hpp:16
@ Sight
what hides one player from another
Definition Trace.hpp:17
@ Surfaces
every drawn surface and the sky; no player clips or players
Definition Trace.hpp:19
@ Solid
what a player body collides with, players included
Definition Trace.hpp:18
Vector Normal
zero without a hit
Definition Trace.hpp:36
EntityRef HitEntity
the world included
Definition Trace.hpp:37
float Fraction
share of the path travelled
Definition Trace.hpp:34
bool Hit
also true when the start is inside a solid
Definition Trace.hpp:33
TraceLayers Layers
Definition Trace.hpp:24