summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/include/gamechaos/tracing.inc
blob: 65d54a838aec9812d16eea06261afc13df2eb3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

#if defined _gamechaos_stocks_tracing_included
	#endinput
#endif
#define _gamechaos_stocks_tracing_included

#include <sdktools_trace>

#define GC_TRACING_VERSION 0x01_00_00
#define GC_TRACING_VERSION_STRING "1.0.0"

/**
 * Trace ray filter that filters players from being traced.
 *
 * @param entity			Entity.
 * @param data				Data.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceEntityFilterPlayer(int entity, any data)
{
	return entity > MAXPLAYERS;
}

/**
 * Traces the player hull beneath the player in the direction of
 * the player's velocity. This should be used on the tick when the player lands
 *
 * @param client			Player's index.
 * @param pos				Player's position vector.
 * @param velocity			Player's velocity vector. This shuold have the current tick's x and y velocities, but the previous tick's z velocity, since when you're on ground, your z velocity is 0.
 * @param result			Trace endpoint on success, player's position on failure.
 * @param bugged			Whether to add gravity to the player's velocity or not.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceLandPos(int client, const float pos[3], const float velocity[3], float result[3], float fGravity, bool bugged = false)
{
	float newVel[3];
	newVel = velocity;
	
	if (bugged)
	{
		// add 0.5 gravity
		newVel[2] -= fGravity * GetTickInterval() * 0.5;
	}
	else
	{
		// add 1.5 gravity
		newVel[2] -= fGravity * GetTickInterval() * 1.5;
	}
	
	ScaleVector(newVel, GetTickInterval() * 2.0);
	float pos2[3];
	AddVectors(pos, newVel, pos2);
	
	float mins[3];
	float maxs[3];
	GetClientMins(client, mins);
	GetClientMaxs(client, maxs);
	
	Handle trace = TR_TraceHullFilterEx(pos, pos2, mins, maxs, MASK_PLAYERSOLID, GCTraceEntityFilterPlayer);
	
	if (!TR_DidHit(trace))
	{
		result = pos;
		CloseHandle(trace);
		return false;
	}
	
	TR_GetEndPosition(result, trace);
	CloseHandle(trace);
	
	return true;
}

/**
 * Traces the player hull 2 units straight down beneath the player.
 *
 * @param client			Player's index.
 * @param pos				Player's position vector.
 * @param result			Trace endpoint on success, player's position on failure.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceGround(int client, const float pos[3], float result[3])
{
	float mins[3];
	float maxs[3];
	
	GetClientMins(client, mins);
	GetClientMaxs(client, maxs);
	
	float startpos[3];
	float endpos[3];
	
	startpos = pos;
	endpos = pos;
	
	endpos[2] -= 2.0;
	
	TR_TraceHullFilter(startpos, endpos, mins, maxs, MASK_PLAYERSOLID, GCTraceEntityFilterPlayer);
	
	if (TR_DidHit())
	{
		TR_GetEndPosition(result);
		return true;
	}
	else
	{
		result = endpos;
		return false;
	}
}

/**
 * Traces a hull between 2 positions.
 *
 * @param pos1				Position 1.
 * @param pos2				Position 2
 * @param result			Trace endpoint on success, player's position on failure.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceBlock(const float pos1[3], const float pos2[3], float result[3])
{
	float mins[3] =  {-16.0, -16.0, -1.0};
	float maxs[3] =  { 16.0,  16.0,  0.0};
	
	TR_TraceHullFilter(pos1, pos2, mins, maxs, MASK_PLAYERSOLID, GCTraceEntityFilterPlayer);
	
	if (TR_DidHit())
	{
		TR_GetEndPosition(result);
		return true;
	}
	else
	{
		return false;
	}
}

/**
 * Traces from player eye position in the direction of where the player is looking.
 *
 * @param client			Client index.
 * @param result			Resultant vector.
 * @return 					True on success, false otherwise.
 */
stock bool GCGetEyeRayPosition(int client, float result[3], TraceEntityFilter filter, any data = 0, int flags = MASK_PLAYERSOLID)
{
	float start[3];
	float angle[3];
	
	GetClientEyePosition(client, start);
	GetClientEyeAngles(client, angle);
	
	TR_TraceRayFilter(start, angle, flags, RayType_Infinite, filter, data);
	
	if (TR_DidHit(INVALID_HANDLE))
	{
		TR_GetEndPosition(result, INVALID_HANDLE);
		return true;
	}
	return false;
}

/**
 * Traces from player eye position in the direction of where the player is looking, up to a certain distance.
 *
 * @param client			Client index.
 * @param result			Resultant vector.
 * @param distance			Maximum distance to trace.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceEyeRayPositionDistance(int client, float result[3], float distance)
{
	float start[3];
	float angle[3];
	
	GetClientEyePosition(client, start);
	GetClientEyeAngles(client, angle);
	
	float endpoint[3];
	float zsine = Sine(DegToRad(-angle[0]));
	float zcos = Cosine(DegToRad(-angle[0]));
	
	endpoint[0] = Cosine(DegToRad(angle[1])) * zcos;
	endpoint[1] = Sine(DegToRad(angle[1])) * zcos;
	endpoint[2] = zsine;
	
	ScaleVector(endpoint, distance);
	AddVectors(start, endpoint, endpoint);
	
	TR_TraceRayFilter(start, endpoint, MASK_PLAYERSOLID, RayType_EndPoint, GCTraceEntityFilterPlayer, client);
	
	if (TR_DidHit())
	{
		TR_GetEndPosition(result);
		return true;
	}
	
	result = endpoint;
	return false;
}

/**
 * Traces a hull in a certain direction and distance.
 *
 * @param origin			Position to trace from.
 * @param direction			Trace direction.
 * @param mins				Minimum size of the hull.
 * @param maxs				Maximum size of the hull.
 * @param result			Resultant vector.
 * @return 					True on success, false otherwise.
 */
stock bool GCTraceHullDirection(const float origin[3],
	const float direction[3],
	const float mins[3],
	const float maxs[3],
	float result[3],
	float distance,
	TraceEntityFilter filter,
	any data = 0,
	int flags = MASK_PLAYERSOLID)
{
	float pos2[3];
	float zsine = Sine(DegToRad(-direction[0]));
	float zcos = Cosine(DegToRad(-direction[0]));
	
	pos2[0] = Cosine(DegToRad(direction[1])) * zcos;
	pos2[1] = Sine(DegToRad(direction[1])) * zcos;
	pos2[2] = zsine;
	
	ScaleVector(pos2, distance);
	AddVectors(origin, pos2, pos2);
	
	TR_TraceHullFilter(origin, pos2, mins, maxs, flags, filter, data);
	if (TR_DidHit())
	{
		TR_GetEndPosition(result);
		return true;
	}
	result = pos2;
	return false;
}