-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmovement.inc
530 lines (468 loc) · 11.3 KB
/
movement.inc
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
MovementAPI Function Stock Library
Website: https://github.com/danzayau/MovementAPI
*/
#if defined _movement_included_
#endinput
#endif
#define _movement_included_
#include <sdktools>
// =====[ STOCKS ]=====
/**
* Calculates the horizontal distance between two vectors.
*
* @param vec1 First vector.
* @param vec2 Second vector.
* @return Vector horizontal distance.
*/
stock float GetVectorHorizontalDistance(const float vec1[3], const float vec2[3])
{
return SquareRoot(Pow(vec2[0] - vec1[0], 2.0) + Pow(vec2[1] - vec1[1], 2.0));
}
/**
* Calculates a vector's horizontal length.
*
* @param vec Vector.
* @return Vector horizontal length (magnitude).
*/
stock float GetVectorHorizontalLength(const float vec[3])
{
return SquareRoot(Pow(vec[0], 2.0) + Pow(vec[1], 2.0));
}
/**
* Scales a vector to a certain horizontal length.
*
* @param vec Vector.
* @param length New horizontal length.
*/
stock void SetVectorHorizontalLength(float vec[3], float length)
{
float newVec[3];
newVec = vec;
newVec[2] = 0.0;
NormalizeVector(newVec, newVec);
ScaleVector(newVec, length);
newVec[2] = vec[2];
vec = newVec;
}
/**
* Gets a player's currently pressed buttons.
*
* @param client Client index.
* @return Bitsum of buttons.
*/
stock int Movement_GetButtons(int client)
{
return GetClientButtons(client);
}
/**
* Gets a player's origin.
*
* @param client Client index.
* @param result Resultant vector.
*/
stock void Movement_GetOrigin(int client, float result[3])
{
GetClientAbsOrigin(client, result);
}
/**
* Gets a player's origin.
* If the player is on the ground, a trace hull is used to find the
* exact height of the ground the player is standing on. This is thus
* more accurate than Movement_GetOrigin when player is on ground.
*
* @param client Client index.
* @param result Resultant vector.
*/
stock void Movement_GetOriginEx(int client, float result[3])
{
if (!Movement_GetOnGround(client))
{
GetClientAbsOrigin(client, result);
return;
}
// Get the coordinate of the solid beneath the player's origin
// More accurate than GetClientAbsOrigin when on ground
float startPosition[3], endPosition[3];
GetClientAbsOrigin(client, startPosition);
endPosition = startPosition;
endPosition[2] = startPosition[2] - 2.0; // Should be less than 2.0 units away
Handle trace = TR_TraceHullFilterEx(
startPosition,
endPosition,
view_as<float>( { -16.0, -16.0, 0.0 } ), // Players are 32 x 32 x 72
view_as<float>( { 16.0, 16.0, 72.0 } ),
MASK_PLAYERSOLID,
TraceEntityFilterPlayers,
client);
if (TR_DidHit(trace))
{
TR_GetEndPosition(result, trace);
// Do not get rid of the offset. The offset is correct, as the player must be
// at least 0.03125 units away from the ground.
}
else
{
result = startPosition; // Fallback to GetClientAbsOrigin
}
delete trace;
}
public bool TraceEntityFilterPlayers(int entity, int contentsMask)
{
return entity > MaxClients;
}
/**
* Sets a player's origin by teleporting them.
*
* @param client Client index.
* @param origin New origin.
*/
stock void Movement_SetOrigin(int client, const float origin[3])
{
TeleportEntity(client, origin, NULL_VECTOR, NULL_VECTOR);
}
/**
* Gets a player's velocity.
*
* @param client Client index.
* @param result Resultant vector.
*/
stock void Movement_GetVelocity(int client, float result[3])
{
GetEntPropVector(client, Prop_Data, "m_vecVelocity", result);
}
/**
* Sets a player's velocity by teleporting them.
*
* @param client Client index.
* @param velocity New velocity.
*/
stock void Movement_SetVelocity(int client, const float velocity[3])
{
TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, velocity);
}
/**
* Gets a player's horizontal speed.
*
* @param client Client index.
* @return Player's horizontal speed.
*/
stock float Movement_GetSpeed(int client)
{
float velocity[3];
Movement_GetVelocity(client, velocity);
return GetVectorHorizontalLength(velocity);
}
/**
* Sets a player's horizontal speed.
*
* @param client Client index.
* @param value New horizontal speed.
* @param applyBaseVel Whether to apply base velocity as well.
*/
stock void Movement_SetSpeed(int client, float value, bool applyBaseVel = false)
{
float velocity[3];
Movement_GetVelocity(client, velocity);
SetVectorHorizontalLength(velocity, value)
if (applyBaseVel)
{
float baseVelocity[3];
Movement_GetBaseVelocity(client, baseVelocity);
AddVectors(velocity, baseVelocity, velocity);
}
Movement_SetVelocity(client, velocity);
}
/**
* Gets a player's vertical velocity.
*
* @param client Client index.
* @return Player's vertical velocity.
*/
stock float Movement_GetVerticalVelocity(int client)
{
float velocity[3];
Movement_GetVelocity(client, velocity);
return velocity[2];
}
/**
* Sets a player's vertical velocity.
*
* @param client Client index.
* @param value New vertical velocity.
*/
stock void Movement_SetVerticalVelocity(int client, float value)
{
float velocity[3];
Movement_GetVelocity(client, velocity);
velocity[2] = value;
Movement_SetVelocity(client, velocity);
}
/**
* Gets a player's base velocity.
*
* @param client Client index.
* @param result Resultant vector.
*/
stock void Movement_GetBaseVelocity(int client, float result[3])
{
GetEntPropVector(client, Prop_Data, "m_vecBaseVelocity", result);
}
/**
* Sets a player's base velocity.
*
* @param client Client index.
* @param baseVelocity New base velocity.
*/
stock void Movement_SetBaseVelocity(int client, const float baseVelocity[3])
{
SetEntPropVector(client, Prop_Data, "m_vecBaseVelocity", baseVelocity);
}
/**
* Gets a player's eye angles.
*
* @param client Client index.
* @param result Resultant vector.
*/
stock void Movement_GetEyeAngles(int client, float result[3])
{
GetClientEyeAngles(client, result);
}
/**
* Sets a player's eye angles by teleporting them.
*
* @param client Client index.
* @param eyeAngles New eye angles.
*/
stock void Movement_SetEyeAngles(int client, const float eyeAngles[3])
{
TeleportEntity(client, NULL_VECTOR, eyeAngles, NULL_VECTOR);
}
/**
* Gets whether a player is on the ground.
*
* @param client Client index.
* @return Whether player is on the ground.
*/
stock bool Movement_GetOnGround(int client)
{
return view_as<bool>(GetEntityFlags(client) & FL_ONGROUND);
}
/**
* Gets whether a player is ducking or ducked.
*
* @param client Client index.
* @return Whether player is ducking or ducked.
*/
stock bool Movement_GetDucking(int client)
{
return GetEntProp(client, Prop_Send, "m_bDucked") || GetEntProp(client, Prop_Send, "m_bDucking");
}
/**
* Gets a player's "m_flDuckSpeed" value.
*
* @param client Client index.
* @return Value of "m_flDuckSpeed".
*/
stock float Movement_GetDuckSpeed(int client)
{
return GetEntPropFloat(client, Prop_Send, "m_flDuckSpeed");
}
/**
* Sets a player's "m_flDuckSpeed" value.
*
* @param client Client index.
* @param value New "m_flDuckSpeed" value.
*/
stock void Movement_SetDuckSpeed(int client, float value)
{
SetEntPropFloat(client, Prop_Send, "m_flDuckSpeed", value);
}
/**
* Gets a player's "m_flVelocityModifier" value.
*
* @param client Client index.
* @return Value of "m_flVelocityModifier".
*/
stock float Movement_GetVelocityModifier(int client)
{
return GetEntPropFloat(client, Prop_Send, "m_flVelocityModifier");
}
/**
* Sets a player's "m_flVelocityModifier" value.
*
* @param client Client index.
* @param value New "m_flVelocityModifier" value.
*/
stock void Movement_SetVelocityModifier(int client, float value)
{
SetEntPropFloat(client, Prop_Send, "m_flVelocityModifier", value);
}
/**
* Gets a player's gravity scale factor.
*
* @param client Client index.
* @return Gravity scale factor.
*/
stock float Movement_GetGravity(int client)
{
return GetEntityGravity(client);
}
/**
* Sets a player's gravity scale factor.
*
* @param client Client index.
* @param value Desired gravity scale factor.
*/
stock void Movement_SetGravity(int client, float value)
{
SetEntityGravity(client, value);
}
/**
* Gets a player's movetype.
*
* @param client Client index.
* @return Player's movetype.
*/
stock MoveType Movement_GetMovetype(int client)
{
return GetEntityMoveType(client);
}
/**
* Sets a player's movetype.
*
* @param client Client index.
* @param movetype New movetype.
*/
stock void Movement_SetMovetype(int client, MoveType movetype)
{
SetEntityMoveType(client, movetype);
}
/**
* Gets whether a player is on a ladder.
*
* @param client Client index.
* @return Whether player is on a ladder.
*/
stock bool Movement_GetOnLadder(int client)
{
return GetEntityMoveType(client) == MOVETYPE_LADDER;
}
/**
* Gets whether a player is noclipping.
*
* @param client Client index.
* @return Whether player is noclipping.
*/
stock bool Movement_GetNoclipping(int client)
{
return GetEntityMoveType(client) == MOVETYPE_NOCLIP;
}
// =====[ METHODMAP ]=====
methodmap MovementPlayer {
public MovementPlayer(int client) {
return view_as<MovementPlayer>(client);
}
property int ID {
public get() {
return view_as<int>(this);
}
}
property int Buttons {
public get() {
return Movement_GetButtons(this.ID);
}
}
public void GetOrigin(float result[3]) {
Movement_GetOrigin(this.ID, result);
}
public void SetOrigin(const float origin[3]) {
Movement_SetOrigin(this.ID, origin);
}
public void GetVelocity(float result[3]) {
Movement_GetVelocity(this.ID, result);
}
public void SetVelocity(const float velocity[3]) {
Movement_SetVelocity(this.ID, velocity);
}
property float Speed {
public get() {
return Movement_GetSpeed(this.ID);
}
public set(float value) {
Movement_SetSpeed(this.ID, value);
}
}
property float VerticalVelocity {
public get() {
return Movement_GetVerticalVelocity(this.ID);
}
public set(float value) {
Movement_SetVerticalVelocity(this.ID, value);
}
}
public void GetBaseVelocity(float result[3]) {
Movement_GetBaseVelocity(this.ID, result);
}
public void SetBaseVelocity(const float baseVelocity[3]) {
Movement_SetBaseVelocity(this.ID, baseVelocity);
}
public void GetEyeAngles(float result[3]) {
Movement_GetEyeAngles(this.ID, result);
}
public void SetEyeAngles(const float eyeAngles[3]) {
Movement_SetEyeAngles(this.ID, eyeAngles);
}
property bool OnGround {
public get() {
return Movement_GetOnGround(this.ID);
}
}
property bool Ducking {
public get() {
return Movement_GetDucking(this.ID);
}
}
property float DuckSpeed {
public get() {
return Movement_GetDuckSpeed(this.ID);
}
public set(float value) {
Movement_SetDuckSpeed(this.ID, value);
}
}
property float VelocityModifier {
public get() {
return Movement_GetVelocityModifier(this.ID);
}
public set(float value) {
Movement_SetVelocityModifier(this.ID, value);
}
}
property float Gravity {
public get() {
return Movement_GetGravity(this.ID);
}
public set(float value) {
Movement_SetGravity(this.ID, value);
}
}
property MoveType Movetype {
public get() {
return Movement_GetMovetype(this.ID);
}
public set(MoveType movetype) {
Movement_SetMovetype(this.ID, movetype);
}
}
property bool OnLadder {
public get() {
return Movement_GetOnLadder(this.ID);
}
}
property bool Noclipping {
public get() {
return Movement_GetNoclipping(this.ID);
}
}
}