/
mom_gamemovement.cpp
executable file
·4433 lines (3710 loc) · 151 KB
/
mom_gamemovement.cpp
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cbase.h"
#include "in_buttons.h"
#include "mom_gamemovement.h"
#include "mom_player_shared.h"
#include "mom_shareddefs.h"
#include "movevars_shared.h"
#include "coordsize.h"
#include "mom_system_gamemode.h"
#ifdef CLIENT_DLL
#include "c_mom_triggers.h"
#else
#include "env_player_surface_trigger.h"
#include "momentum/mom_triggers.h"
#include "momentum/mom_system_saveloc.h"
#include "momentum/mom_timer.h"
#endif
#include "tier0/memdbgon.h"
#define NO_REFL_NORMAL_CHANGE -2.0f // not used
#define BHOP_DELAY_TIME 15 // Time to delay successive bhops by, in ticks
#define STOP_EPSILON 0.1
#define MAX_CLIP_PLANES 5
#define STAMINA_MAX 100.0f
#define STAMINA_COST_JUMP 25.0f
#define STAMINA_COST_FALL 20.0f // not used
#define STAMINA_RECOVER_RATE 19.0f
#define CS_WALK_SPEED 135.0f
#define DUCK_SPEED_MULTIPLIER 0.34f
#define GROUND_FACTOR_MULTIPLIER 301.99337741082998788946739227784f // not used
#define NON_JUMP_VELOCITY ((g_pGameModeSystem->IsTF2BasedMode()) ? 250.0f : 140.0f)
// remove this eventually
ConVar sv_slope_fix("sv_slope_fix", "1");
ConVar sv_ramp_fix("sv_ramp_fix", "1");
ConVar sv_ramp_bumpcount("sv_ramp_bumpcount", "8", 0, "Helps with fixing surf/ramp bugs", true, 4, true, 16);
ConVar sv_ramp_initial_retrace_length("sv_ramp_initial_retrace_length", "0.2", 0,
"Amount of units used in offset for retraces", true, 0.2f, true, 5.f);
ConVar sv_jump_z_offset("sv_jump_z_offset", "1.5", 0, "Amount of units in axis z to offset every time a player jumps",
true, 0.0f, true, 5.f);
ConVar sv_ladder_dampen("sv_ladder_dampen", "0.2", FCVAR_REPLICATED,
"Amount to dampen perpendicular movement on a ladder", true, 0.0f, true, 1.0f);
ConVar sv_ladder_angle("sv_ladder_angle", "-0.707", FCVAR_REPLICATED,
"Cos of angle of incidence to ladder perpendicular for applying ladder_dampen", true, -1.0f,
true, 1.0f);
ConVar sv_rngfix_enable("sv_rngfix_enable", "0", FCVAR_MAPPING);
#ifndef CLIENT_DLL
#include "env_player_surface_trigger.h"
static ConVar dispcoll_drawplane("dispcoll_drawplane", "0");
#endif
CMomentumGameMovement::CMomentumGameMovement() : m_pPlayer(nullptr) {}
void CMomentumGameMovement::ProcessMovement(CBasePlayer *pPlayer, CMoveData *data)
{
m_pPlayer = ToCMOMPlayer(pPlayer);
Assert(m_pPlayer);
BaseClass::ProcessMovement(pPlayer, data);
}
float CMomentumGameMovement::LadderDistance() const
{
if (player->GetMoveType() == MOVETYPE_LADDER)
return 10.0f;
return 2.0f;
}
bool CMomentumGameMovement::GameHasLadders() const
{
return !g_pGameModeSystem->IsTF2BasedMode();
}
void CMomentumGameMovement::DecayPunchAngle()
{
if (g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR))
{
BaseClass::DecayPunchAngle();
return;
}
Vector vPunchAngle;
vPunchAngle.x = m_pPlayer->m_Local.m_vecPunchAngle->x;
vPunchAngle.y = m_pPlayer->m_Local.m_vecPunchAngle->y;
vPunchAngle.z = m_pPlayer->m_Local.m_vecPunchAngle->z;
float len = VectorNormalize(vPunchAngle);
len -= (10.0 + len * 0.5) * gpGlobals->frametime;
len = max(len, 0.0);
VectorScale(vPunchAngle, len, vPunchAngle);
m_pPlayer->m_Local.m_vecPunchAngle.Set(0, vPunchAngle.x);
m_pPlayer->m_Local.m_vecPunchAngle.Set(1, vPunchAngle.y);
m_pPlayer->m_Local.m_vecPunchAngle.Set(2, vPunchAngle.z);
}
float CMomentumGameMovement::LadderLateralMultiplier() const { return mv->m_nButtons & IN_DUCK ? 1.0f : 0.5f; }
bool CMomentumGameMovement::IsValidMovementTrace(trace_t &tr)
{
trace_t stuck;
// Apparently we can be stuck with pm.allsolid without having valid plane info ok..
if (tr.allsolid || tr.startsolid)
{
return false;
}
// Maybe we don't need this one
if (CloseEnough(tr.fraction, 0.0f, FLT_EPSILON))
{
return false;
}
if (CloseEnough(tr.fraction, 0.0f, FLT_EPSILON) &&
CloseEnough(tr.plane.normal, Vector(0.0f, 0.0f, 0.0f), FLT_EPSILON))
{
return false;
}
// Is the plane deformed or some stupid shit?
if (fabs(tr.plane.normal.x) > 1.0f || fabs(tr.plane.normal.y) > 1.0f || fabs(tr.plane.normal.z) > 1.0f)
{
return false;
}
TracePlayerBBox(tr.endpos, tr.endpos, PlayerSolidMask(), COLLISION_GROUP_PLAYER_MOVEMENT, stuck);
if (stuck.startsolid || !CloseEnough(stuck.fraction, 1.0f, FLT_EPSILON))
{
return false;
}
return true;
}
float CMomentumGameMovement::ClimbSpeed() const
{
return (mv->m_nButtons & IN_DUCK ? BaseClass::ClimbSpeed() * DUCK_SPEED_MULTIPLIER : BaseClass::ClimbSpeed());
}
void CMomentumGameMovement::WalkMove()
{
int i;
Vector wishvel;
float spd;
float fmove, smove;
Vector wishdir;
float wishspeed;
Vector dest;
trace_t pm;
Vector forward, right, up;
if (g_pGameModeSystem->GameModeIs(GAMEMODE_KZ))
{
if (m_pPlayer->m_flStamina > 0)
{
float flRatio = (STAMINA_MAX - ((m_pPlayer->m_flStamina / 1000.0f) * STAMINA_RECOVER_RATE)) / STAMINA_MAX;
// This Goldsrc code was run with variable timesteps and it had framerate dependencies.
// People looking at Goldsrc for reference are usually
// (these days) measuring the stoppage at 60fps or greater, so we need
// to account for the fact that Goldsrc was applying more stopping power
// since it applied the slowdown across more frames.
float flReferenceFrametime = 1.0f / 70.0f;
float flFrametimeRatio = gpGlobals->frametime / flReferenceFrametime;
flRatio = pow(flRatio, flFrametimeRatio);
mv->m_vecVelocity.x *= flRatio;
mv->m_vecVelocity.y *= flRatio;
}
}
AngleVectors(mv->m_vecViewAngles, &forward, &right, &up); // Determine movement angles
CHandle<CBaseEntity> oldground;
oldground = player->GetGroundEntity();
// Copy movement amounts
fmove = mv->m_flForwardMove;
smove = mv->m_flSideMove;
// Zero out z components of movement vectors
if (forward[2] != 0)
{
forward[2] = 0;
VectorNormalize(forward);
}
if (right[2] != 0)
{
right[2] = 0;
VectorNormalize(right);
}
for (i = 0; i < 2; i++) // Determine x and y parts of velocity
wishvel[i] = forward[i] * fmove + right[i] * smove;
wishvel[2] = 0.0f; // Zero out z part of velocity
if (g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR) &&
m_pPlayer->m_bIsPowerSliding &&
(sv_slide_lock.GetBool()
#ifndef CLIENT_DLL
|| (player->GetGroundVPhysics() && wishvel.Length2D() == 0.0f)
#endif
))
{
// can't change direction in slide if sv_slide_lock on.
// Also there's some weird behaviour with sliding on
// physics objects which can be fixed by pretending they
// are holding the button down
VectorCopy(mv->m_vecVelocity, wishdir);
Vector direction = mv->m_vecVelocity;
VectorNormalizeFast(direction);
// float leanProjection = DotProduct(direction, );
QAngle angles;
float player_yaw = AngleNormalizePositive(mv->m_vecAbsViewAngles[YAW]);
VectorAngles(direction, angles);
float velocity_yaw = AngleNormalizePositive(angles[YAW]);
float angle = 15 * sinf(DEG2RAD(velocity_yaw - player_yaw));
player->m_Local.m_punchRollOverrideTarget = angle;
}
else
{
VectorCopy(wishvel, wishdir); // Determine maginitude of speed of move
}
wishspeed = VectorNormalize(wishdir);
//
// Clamp to server defined max speed
//
if ((wishspeed != 0.0f) && (wishspeed > mv->m_flMaxSpeed))
{
VectorScale(wishvel, mv->m_flMaxSpeed / wishspeed, wishvel);
wishspeed = mv->m_flMaxSpeed;
}
float oldspeed = mv->m_vecVelocity.Length2D();
// Set pmove velocity
Accelerate(wishdir, wishspeed, sv_accelerate.GetFloat());
// Cap ground speed if the speed is gained from the above Accelerate()
if (g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR))
{
if (mv->m_vecVelocity.Length2D() >= wishspeed)
{
float cappedSpeed = Max(wishspeed, oldspeed);
Vector direction = mv->m_vecVelocity;
VectorNormalizeFast(direction);
mv->m_vecVelocity = direction * cappedSpeed;
}
}
// Cap ground movement speed in tf2 modes
if (g_pGameModeSystem->IsTF2BasedMode())
{
float flNewSpeed = VectorLength(mv->m_vecVelocity);
if (flNewSpeed > mv->m_flMaxSpeed)
{
float flScale = (mv->m_flMaxSpeed / flNewSpeed);
mv->m_vecVelocity.x *= flScale;
mv->m_vecVelocity.y *= flScale;
}
// Scale backwards movement if going faster than 100u/s
if (VectorLength(mv->m_vecVelocity) > 100.0f)
{
float flDot = DotProduct(forward, mv->m_vecVelocity);
// are we moving backwards at all?
if (flDot < 0)
{
Vector vecBackMove = forward * flDot;
Vector vecRightMove = right * DotProduct(right, mv->m_vecVelocity);
// clamp the back move vector if it is faster than max
float flBackSpeed = VectorLength(vecBackMove);
float flMaxBackSpeed = (mv->m_flMaxSpeed * 0.9f);
if (flBackSpeed > flMaxBackSpeed)
{
vecBackMove *= flMaxBackSpeed / flBackSpeed;
}
// reassemble velocity
mv->m_vecVelocity = vecBackMove + vecRightMove;
flNewSpeed = VectorLength(mv->m_vecVelocity);
if (flNewSpeed > mv->m_flMaxSpeed)
{
float flScale = (mv->m_flMaxSpeed / flNewSpeed);
mv->m_vecVelocity.x *= flScale;
mv->m_vecVelocity.y *= flScale;
}
}
}
}
// Add in any base velocity to the current velocity.
VectorAdd(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
spd = VectorLength(mv->m_vecVelocity);
if (CloseEnough(spd, 0.0f))
{
mv->m_vecVelocity.Init();
// Now pull the base velocity back out. Base velocity is set if you are on a moving object, like a conveyor
// (or maybe another monster?)
VectorSubtract(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
return;
}
// first try just moving to the destination
dest[0] = mv->GetAbsOrigin()[0] + mv->m_vecVelocity[0] * gpGlobals->frametime;
dest[1] = mv->GetAbsOrigin()[1] + mv->m_vecVelocity[1] * gpGlobals->frametime;
// The original code was "+ mv->m_vecVelocity[1]" which was obviously incorrect and should be [2] but after changing
// it to [2] the sliding on sloped grounds started happening, so now I think this is be the solution
dest[2] = mv->GetAbsOrigin()[2];
// first try moving directly to the next spot
TracePlayerBBox(mv->GetAbsOrigin(), dest, PlayerSolidMask(), COLLISION_GROUP_PLAYER_MOVEMENT, pm);
// If we made it all the way, then copy trace end as new player position.
mv->m_outWishVel += wishdir * wishspeed;
if (pm.fraction == 1)
{
mv->SetAbsOrigin(pm.endpos);
// Now pull the base velocity back out. Base velocity is set if you are on a moving object, like a conveyor
// (or maybe another monster?)
VectorSubtract(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
StayOnGround();
return;
}
// Don't walk up stairs if not on ground.
if (oldground == nullptr && player->GetWaterLevel() == 0)
{
// Now pull the base velocity back out. Base velocity is set if you are on a moving object, like a conveyor
// (or maybe another monster?)
VectorSubtract(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
return;
}
// If we are jumping out of water, don't do anything more.
if (player->m_flWaterJumpTime)
{
// Now pull the base velocity back out. Base velocity is set if you are on a moving object, like a conveyor
// (or maybe another monster?)
VectorSubtract(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
return;
}
StepMove(dest, pm);
// Now pull the base velocity back out. Base velocity is set if you are on a moving object, like a conveyor (or
// maybe another monster?)
VectorSubtract(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity);
StayOnGround();
}
void CMomentumGameMovement::StepMove(Vector &vecDestination, trace_t &trace)
{
if (!sv_rngfix_enable.GetBool() || g_pGameModeSystem->GameModeIs(GAMEMODE_AHOP))
{
BaseClass::StepMove(vecDestination, trace);
return;
}
Vector vecEndPos;
VectorCopy(vecDestination, vecEndPos);
// Try sliding forward both on ground and up 16 pixels
// take the move that goes farthest
Vector vecPos, vecVel;
VectorCopy(mv->GetAbsOrigin(), vecPos);
VectorCopy(mv->m_vecVelocity, vecVel);
// Slide move down.
TryPlayerMove(&vecEndPos, &trace);
// The original StepMove will use the up result but with the down result's z-velocity if the up result goes farther.
// This generates z-velocity from nowhere, which means it is beneficial to hit uphill slopes without pressing jump
// to do ground movement instead of air movement. Instead, we should use one entire result or the other instead of
// combining them. The main reason we might want to use the down result over the up result even if the up result
// goes farther is if the down result causes the player to not be grounded in the future.
if (mv->m_vecVelocity.z > NON_JUMP_VELOCITY)
{
float flStepDist = mv->GetAbsOrigin().z - vecPos.z;
if (flStepDist > 0.0f)
{
mv->m_outStepHeight += flStepDist;
}
return;
}
// Down results.
Vector vecDownPos, vecDownVel;
VectorCopy(mv->GetAbsOrigin(), vecDownPos);
VectorCopy(mv->m_vecVelocity, vecDownVel);
// Reset original values.
mv->SetAbsOrigin(vecPos);
VectorCopy(vecVel, mv->m_vecVelocity);
// Move up a stair height.
VectorCopy(mv->GetAbsOrigin(), vecEndPos);
if (player->m_Local.m_bAllowAutoMovement)
{
vecEndPos.z += player->m_Local.m_flStepSize + DIST_EPSILON;
}
TracePlayerBBox(mv->GetAbsOrigin(), vecEndPos, PlayerSolidMask(), COLLISION_GROUP_PLAYER_MOVEMENT, trace);
if (!trace.startsolid && !trace.allsolid)
{
mv->SetAbsOrigin(trace.endpos);
}
// Slide move up.
TryPlayerMove();
// Move down a stair (attempt to).
VectorCopy(mv->GetAbsOrigin(), vecEndPos);
if (player->m_Local.m_bAllowAutoMovement)
{
vecEndPos.z -= player->m_Local.m_flStepSize + DIST_EPSILON;
}
TracePlayerBBox(mv->GetAbsOrigin(), vecEndPos, PlayerSolidMask(), COLLISION_GROUP_PLAYER_MOVEMENT, trace);
// If we are not on the ground any more then use the original movement attempt.
if (trace.plane.normal[2] < 0.7)
{
mv->SetAbsOrigin(vecDownPos);
VectorCopy(vecDownVel, mv->m_vecVelocity);
float flStepDist = mv->GetAbsOrigin().z - vecPos.z;
if (flStepDist > 0.0f)
{
mv->m_outStepHeight += flStepDist;
}
return;
}
// If the trace ended up in empty space, copy the end over to the origin.
if (!trace.startsolid && !trace.allsolid)
{
mv->SetAbsOrigin(trace.endpos);
}
// Copy this origin to up.
Vector vecUpPos;
VectorCopy(mv->GetAbsOrigin(), vecUpPos);
// decide which one went farther
float flDownDist =
(vecDownPos.x - vecPos.x) * (vecDownPos.x - vecPos.x) + (vecDownPos.y - vecPos.y) * (vecDownPos.y - vecPos.y);
float flUpDist =
(vecUpPos.x - vecPos.x) * (vecUpPos.x - vecPos.x) + (vecUpPos.y - vecPos.y) * (vecUpPos.y - vecPos.y);
if (flDownDist > flUpDist)
{
mv->SetAbsOrigin(vecDownPos);
VectorCopy(vecDownVel, mv->m_vecVelocity);
}
float flStepDist = mv->GetAbsOrigin().z - vecPos.z;
if (flStepDist > 0)
{
mv->m_outStepHeight += flStepDist;
}
}
bool CMomentumGameMovement::LadderMove()
{
trace_t pm;
bool onFloor;
Vector floor;
Vector wishdir;
Vector end;
if (player->GetMoveType() == MOVETYPE_NOCLIP)
return false;
if (!GameHasLadders())
{
return false;
}
if (m_pPlayer->GetGrabbableLadderTime() > 0.0f)
{
m_pPlayer->SetGrabbableLadderTime(m_pPlayer->GetGrabbableLadderTime() - gpGlobals->frametime);
}
// If I'm already moving on a ladder, use the previous ladder direction
if (player->GetMoveType() == MOVETYPE_LADDER)
{
wishdir = -player->m_vecLadderNormal;
}
else
{
// otherwise, use the direction player is attempting to move
if (mv->m_flForwardMove || mv->m_flSideMove)
{
for (int i = 0; i < 3; i++) // Determine x and y parts of velocity
wishdir[i] = m_vecForward[i] * mv->m_flForwardMove + m_vecRight[i] * mv->m_flSideMove;
VectorNormalize(wishdir);
}
else
{
// Player is not attempting to move, no ladder behavior
return false;
}
}
// wishdir points toward the ladder if any exists
if (m_pPlayer->GetGrabbableLadderTime() > 0.0f && m_bCheckForGrabbableLadder)
{
Vector temp = mv->m_vecVelocity * 2.0f;
temp.z = -temp.z;
VectorNormalize(temp);
VectorMA(mv->GetAbsOrigin(), 10.0f, -temp, end);
}
else
{
VectorMA(mv->GetAbsOrigin(), LadderDistance(), wishdir, end);
}
TracePlayerBBox(mv->GetAbsOrigin(), end, LadderMask(), COLLISION_GROUP_PLAYER_MOVEMENT, pm);
// no ladder in that direction, return
if (pm.fraction == 1.0f || pm.plane.normal.z == 1.0f || !OnLadder(pm))
{
return false;
}
if (m_pPlayer->GetGrabbableLadderTime() > 0.0f && m_bCheckForGrabbableLadder)
{
mv->m_vecVelocity.Init();
mv->SetAbsOrigin(pm.endpos);
m_pPlayer->SetGrabbableLadderTime(0.1f);
}
player->SetMoveType(MOVETYPE_LADDER);
player->SetMoveCollide(MOVECOLLIDE_DEFAULT);
// On ladder, convert movement to be relative to the ladder
player->SetLadderNormal(pm.plane.normal);
VectorCopy(mv->GetAbsOrigin(), floor);
floor[2] += GetPlayerMins()[2] - 1;
if (enginetrace->GetPointContents(floor) == CONTENTS_SOLID || player->GetGroundEntity() != nullptr)
{
onFloor = true;
}
else
{
onFloor = false;
}
player->SetGravity(1.0f); // Should be always set on 1.0..
float climbSpeed = ClimbSpeed();
float forwardSpeed = 0, rightSpeed = 0;
if (mv->m_nButtons & IN_BACK)
forwardSpeed -= climbSpeed;
if (mv->m_nButtons & IN_FORWARD)
forwardSpeed += climbSpeed;
if (mv->m_nButtons & IN_MOVELEFT)
rightSpeed -= climbSpeed;
if (mv->m_nButtons & IN_MOVERIGHT)
rightSpeed += climbSpeed;
if (mv->m_nButtons & IN_JUMP)
{
player->SetMoveType(MOVETYPE_WALK);
player->SetMoveCollide(MOVECOLLIDE_DEFAULT);
VectorScale(pm.plane.normal, 270, mv->m_vecVelocity);
}
else
{
if (forwardSpeed != 0 || rightSpeed != 0)
{
Vector velocity, perp, cross, lateral, tmp;
// ALERT(at_console, "pev %.2f %.2f %.2f - ",
// pev->velocity.x, pev->velocity.y, pev->velocity.z);
// Calculate player's intended velocity
// Vector velocity = (forward * gpGlobals->v_forward) + (right * gpGlobals->v_right);
VectorScale(m_vecForward, forwardSpeed, velocity);
VectorMA(velocity, rightSpeed, m_vecRight, velocity);
// Perpendicular in the ladder plane
VectorCopy(vec3_origin, tmp);
tmp[2] = 1;
CrossProduct(tmp, pm.plane.normal, perp);
VectorNormalize(perp);
// decompose velocity into ladder plane
float normal = DotProduct(velocity, pm.plane.normal);
// This is the velocity into the face of the ladder
VectorScale(pm.plane.normal, normal, cross);
// This is the player's additional velocity
VectorSubtract(velocity, cross, lateral);
// This turns the velocity into the face of the ladder into velocity that
// is roughly vertically perpendicular to the face of the ladder.
// NOTE: It IS possible to face up and move down or face down and move up
// because the velocity is a sum of the directional velocity and the converted
// velocity through the face of the ladder -- by design.
CrossProduct(pm.plane.normal, perp, tmp);
//=============================================================================
// HPE_BEGIN
// [sbodenbender] make ladders easier to climb in cstrike
//=============================================================================
// break lateral into direction along tmp (up the ladder) and direction along perp (perpendicular to ladder)
float tmpDist = DotProduct(tmp, lateral);
float perpDist = DotProduct(perp, lateral);
Vector angleVec = perp * perpDist;
angleVec += cross;
// angleVec is our desired movement in the ladder normal/perpendicular plane
VectorNormalize(angleVec);
float angleDot = DotProduct(angleVec, pm.plane.normal);
// angleDot is our angle of incidence to the laddernormal in the ladder normal/perpendicular plane
if (angleDot < sv_ladder_angle.GetFloat())
lateral = (tmp * tmpDist) + (perp * sv_ladder_dampen.GetFloat() * perpDist);
//=============================================================================
// HPE_END
//=============================================================================
VectorMA(lateral, -normal, tmp, mv->m_vecVelocity);
if (onFloor && normal > 0) // On ground moving away from the ladder
{
VectorMA(mv->m_vecVelocity, MAX_CLIMB_SPEED, pm.plane.normal, mv->m_vecVelocity);
}
// pev->velocity = lateral - (CrossProduct( trace.vecPlaneNormal, perp ) * normal);
}
else
{
mv->m_vecVelocity.Init();
}
}
return true;
}
void CMomentumGameMovement::HandleDuckingSpeedCrop()
{
if (m_pPlayer->m_bIsPowerSliding)
return;
if (g_pGameModeSystem->IsTF2BasedMode() || g_pGameModeSystem->GameModeIs(GAMEMODE_AHOP) || g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR))
{
return BaseClass::HandleDuckingSpeedCrop();
}
if (!(m_iSpeedCropped & SPEED_CROPPED_DUCK))
{
if ((mv->m_nButtons & IN_DUCK) || (player->m_Local.m_bDucking) || (player->GetFlags() & FL_DUCKING))
{
mv->m_flForwardMove *= DUCK_SPEED_MULTIPLIER;
mv->m_flSideMove *= DUCK_SPEED_MULTIPLIER;
mv->m_flUpMove *= DUCK_SPEED_MULTIPLIER;
m_iSpeedCropped |= SPEED_CROPPED_DUCK;
}
}
}
float CMomentumGameMovement::GetTimeToDuck()
{
return g_pGameModeSystem->IsTF2BasedMode() ? 0.2f : BaseClass::GetTimeToDuck();
}
float CMomentumGameMovement::GetDuckTimer()
{
return (g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR) || g_pGameModeSystem->GameModeIs(GAMEMODE_CONC)) ? 400.0f : BaseClass::GetDuckTimer();
}
bool CMomentumGameMovement::CanUnduck()
{
trace_t trace;
Vector newOrigin;
VectorCopy(mv->GetAbsOrigin(), newOrigin);
if (player->GetGroundEntity() != nullptr || m_pPlayer->m_CurrentSlideTrigger)
{
newOrigin += VEC_DUCK_HULL_MIN - VEC_HULL_MIN;
}
else
{
// If in air and letting go of crouch, make sure we can offset origin to make
// up for uncrouching
Vector hullSizeNormal = VEC_HULL_MAX - VEC_HULL_MIN;
Vector hullSizeCrouch = VEC_DUCK_HULL_MAX - VEC_DUCK_HULL_MIN;
newOrigin += -g_pGameModeSystem->GetGameMode()->GetViewScale() * (hullSizeNormal - hullSizeCrouch);
}
UTIL_TraceHull(mv->GetAbsOrigin(), newOrigin, VEC_HULL_MIN, VEC_HULL_MAX, PlayerSolidMask(), player,
COLLISION_GROUP_PLAYER_MOVEMENT, &trace);
if (trace.startsolid || (trace.fraction != 1.0f))
return false;
return true;
}
void CMomentumGameMovement::Friction()
{
if (g_pGameModeSystem->GameModeIs(GAMEMODE_AHOP))
{
BaseClass::Friction();
return;
}
// Friction shouldn't be affected by z velocity
Vector velocity = mv->m_vecVelocity;
velocity.z = 0.0f;
DoFriction(velocity);
mv->m_vecVelocity.x = velocity.x;
mv->m_vecVelocity.y = velocity.y;
}
float CMomentumGameMovement::GetPlayerGravity()
{
if (g_pGameModeSystem->IsTF2BasedMode())
return BaseClass::GetPlayerGravity();
// We otherwise don't mind if player gravity is set to 0
return player->GetGravity();
}
float CMomentumGameMovement::GetWaterWaistOffset()
{
return g_pGameModeSystem->IsTF2BasedMode() ? 12.0f : BaseClass::GetWaterWaistOffset();
}
float CMomentumGameMovement::GetWaterJumpUpZVelocity()
{
return g_pGameModeSystem->IsTF2BasedMode() ? 300.0f : BaseClass::GetWaterJumpUpZVelocity();
}
float CMomentumGameMovement::GetWaterJumpForward()
{
return g_pGameModeSystem->IsTF2BasedMode() ? 30.0f : BaseClass::GetWaterJumpForward();
}
void CMomentumGameMovement::CalculateWaterWishVelocityZ(Vector &wishVel, const Vector &forward)
{
const bool bTF2Mode = g_pGameModeSystem->IsTF2BasedMode();
// if we have the jump key down, move us up as well
if (mv->m_nButtons & IN_JUMP)
{
if (bTF2Mode && m_pPlayer->GetWaterLevel() != WL_Eyes)
return;
wishVel[2] += mv->m_flClientMaxSpeed;
}
// Sinking after no other movement occurs
else if (!mv->m_flForwardMove && !mv->m_flSideMove && !mv->m_flUpMove)
{
wishVel[2] -= 60.0f; // drift towards bottom
}
else
{
if (bTF2Mode)
{
wishVel[2] += mv->m_flUpMove;
}
else
{
// exaggerate upward movement along forward as well
float upwardMovememnt = mv->m_flForwardMove * forward.z * 2;
upwardMovememnt = clamp(upwardMovememnt, 0.f, mv->m_flClientMaxSpeed);
wishVel[2] += mv->m_flUpMove + upwardMovememnt;
}
}
}
void CMomentumGameMovement::Duck()
{
if (player->GetMoveType() == MOVETYPE_NOCLIP)
return;
if (g_pGameModeSystem->GameModeIs(GAMEMODE_AHOP))
{
BaseClass::Duck();
return;
}
if (g_pGameModeSystem->IsTF2BasedMode())
{
// Don't allow ducking if deep enough in water
if ((player->GetWaterLevel() >= WL_Feet && player->GetGroundEntity() == nullptr) ||
player->GetWaterLevel() >= WL_Eyes)
{
mv->m_nButtons &= ~IN_DUCK;
}
if (gpGlobals->curtime < m_pPlayer->m_fDuckTimer && player->GetGroundEntity() != nullptr)
{
mv->m_nButtons &= ~IN_DUCK;
}
if (player->m_Local.m_bDucked && player->m_Local.m_bDucking)
{
mv->m_nButtons &= ~IN_DUCK;
}
}
int buttonsChanged = (mv->m_nOldButtons ^ mv->m_nButtons); // These buttons have changed this frame
int buttonsPressed = buttonsChanged & mv->m_nButtons; // The changed ones still down are "pressed"
int buttonsReleased = buttonsChanged & mv->m_nOldButtons; // The changed ones which were previously down are "released"
if (mv->m_nButtons & IN_DUCK)
{
mv->m_nOldButtons |= IN_DUCK;
}
else
{
mv->m_nOldButtons &= ~IN_DUCK;
}
if (IsDead())
{
// Unduck
if (player->GetFlags() & FL_DUCKING)
{
FinishUnDuck();
}
return;
}
HandleDuckingSpeedCrop();
// Holding duck, in process of ducking or fully ducked?
if ((mv->m_nButtons & IN_DUCK) || (player->m_Local.m_bDucking) || (player->GetFlags() & FL_DUCKING))
{
if (mv->m_nButtons & IN_DUCK)
{
DoDuck(buttonsPressed);
}
else
{
DoUnduck(buttonsReleased);
}
}
}
void CMomentumGameMovement::DoDuck(int iButtonsPressed)
{
const bool bFullyCrouched = (player->GetFlags() & FL_DUCKING) == FL_DUCKING;
const bool bInAir = player->GetGroundEntity() == nullptr;
float duckTimer = GetDuckTimer();
if (g_pGameModeSystem->GameModeIs(GAMEMODE_PARKOUR))
{
float speed = mv->m_vecVelocity.Length2D();
if (speed > PK_POWERSLIDE_MIN_SPEED && player->GetGroundEntity() != nullptr &&
player->m_Local.m_slideBoostCooldown <= 0 && !bFullyCrouched)
{
duckTimer = 200.0f;
Vector velocityDirection = mv->m_vecVelocity;
VectorNormalizeFast(velocityDirection);
Accelerate(velocityDirection, 400.0f, 400.0f);
player->m_Local.m_slideBoostCooldown = 2.0f;
}
}
if (iButtonsPressed & IN_DUCK)
{
if (m_pPlayer->m_nWallRunState == WALLRUN_RUNNING)
{
Vector vecWallPush;
VectorScale(m_pPlayer->m_vecWallNorm, 30.0f, vecWallPush);
VectorAdd(mv->m_vecVelocity, vecWallPush, mv->m_vecVelocity);
EndWallRun();
}
if (!bFullyCrouched)
{
// Use 1 second so super long jump will work
player->m_Local.m_flDucktime = duckTimer;
player->m_Local.m_bDucking = true;
if (!bInAir)
{
CheckPowerSlide();
}
}
else if (player->m_Local.m_bDucking)
{
// Invert time if released before fully unducked
float remainingDuckMilliseconds = (duckTimer - player->m_Local.m_flDucktime) * (GetTimeToDuck() / TIME_TO_UNDUCK);
player->m_Local.m_flDucktime = duckTimer - (GetTimeToDuck() * 1000.0f) + remainingDuckMilliseconds;
}
}
if (player->m_Local.m_bDucking)
{
float duckmilliseconds = max(0.0f, duckTimer - player->m_Local.m_flDucktime);
float duckseconds = duckmilliseconds / 1000.0f;
const bool bIsSliding = m_pPlayer->m_CurrentSlideTrigger != nullptr;
// Finish ducking immediately if duck time is over or not on ground
//MOM_TODO: Fzzy's original code replaces both GetTimeToDuck()s here with (duckTimer / 1000.0f) ... is that correct?
if (duckseconds > GetTimeToDuck() || (!bIsSliding && bInAir))
{
FinishDuck();
}
else
{
// Calc parametric time
float duckFraction = SimpleSpline(duckseconds / GetTimeToDuck());
SetDuckedEyeOffset(duckFraction);
}
}
}
void CMomentumGameMovement::DoUnduck(int iButtonsReleased)
{
if (g_pGameModeSystem->IsTF2BasedMode())
{
if (iButtonsReleased & IN_DUCK)
{
m_pPlayer->m_fDuckTimer = gpGlobals->curtime + 0.3f;
}
}
const bool bIsSliding = m_pPlayer->m_CurrentSlideTrigger != nullptr;
const bool bInAir = player->GetGroundEntity() == nullptr;
// Try to unduck unless automovement is not allowed
// NOTE: When not onground, you can always unduck
if (player->m_Local.m_bAllowAutoMovement || (!bIsSliding && bInAir))
{
if (iButtonsReleased & IN_DUCK)
{
if (player->GetFlags() & FL_DUCKING)
{
// Use 1 second so super long jump will work
player->m_Local.m_flDucktime = GetDuckTimer();
player->m_Local.m_bDucking = true; // or unducking
}
else if (player->m_Local.m_bDucking)
{
// Invert time if released before fully ducked
float remainingUnduckMilliseconds = (GetDuckTimer() - player->m_Local.m_flDucktime) * (TIME_TO_UNDUCK / GetTimeToDuck());
player->m_Local.m_flDucktime = GetDuckTimer() - TIME_TO_UNDUCK_MS + remainingUnduckMilliseconds;
}
}