-
Notifications
You must be signed in to change notification settings - Fork 707
/
portal_gamemovement.cpp
5245 lines (4448 loc) · 182 KB
/
portal_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
//========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Special handling for Portal usable ladders
//
//=============================================================================//
#include "cbase.h"
#include "portal_gamemovement.h"
#include "in_buttons.h"
#include "utlrbtree.h"
#include "movevars_shared.h"
#include "portal_shareddefs.h"
#include "portal_collideable_enumerator.h"
#include "portal_base2d_shared.h"
#include "rumble_shared.h"
#include "portal_mp_gamerules.h"
#include "tier0/stacktools.h"
#include "portal_util_shared.h"
#include "iclient.h"
#if defined( CLIENT_DLL )
#include "c_portal_player.h"
#include "c_rumble.h"
#include "prediction.h"
#include "c_weapon_portalgun.h"
#include "c_projectedwallentity.h"
#define CRecipientFilter C_RecipientFilter
#else
#include "portal_player.h"
#include "env_player_surface_trigger.h"
#include "portal_gamestats.h"
#include "physicsshadowclone.h"
#include "recipientfilter.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "weapon_portalgun.h"
#include "projectedwallentity.h"
#include "paint_power_info.h"
#include "particle_parse.h"
#endif
#include "coordsize.h" // for DIST_EPSILON
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar sv_player_trace_through_portals("sv_player_trace_through_portals", "1", FCVAR_REPLICATED | FCVAR_CHEAT, "Causes player movement traces to trace through portals." );
ConVar sv_player_funnel_into_portals("sv_player_funnel_into_portals", "1", FCVAR_REPLICATED, "Causes the player to auto correct toward the center of floor portals." );
ConVar sv_player_funnel_snap_threshold("sv_player_funnel_snap_threshold", "10.f", FCVAR_REPLICATED);
ConVar sv_player_funnel_speed_bonus("sv_player_funnel_speed_bonus", "2.f", FCVAR_REPLICATED | FCVAR_CHEAT);
ConVar sv_player_well_above_height("sv_player_funnel_well_above", "256.f", FCVAR_REPLICATED);
ConVar sv_player_funnel_height_adjust("sv_player_funnel_height_adjust", "128.f", FCVAR_REPLICATED);
ConVar sv_player_funnel_gimme_dot("sv_player_funnel_gimme_dot", "0.9", FCVAR_REPLICATED);
// Convars for paint powerups
ConVar sv_speed_normal("sv_speed_normal", "175.f", FCVAR_REPLICATED | FCVAR_CHEAT, "For tweaking the normal speed when off speed paint.");
ConVar sv_speed_paint_max("sv_speed_paint_max", "800.0f", FCVAR_REPLICATED | FCVAR_CHEAT, "For tweaking the max speed for speed paint.");
ConVar sv_speed_paint_side_move_factor("sv_speed_paint_side_move_factor", "0.5f", FCVAR_REPLICATED | FCVAR_CHEAT);
ConVar speed_funnelling_enabled("speed_funnelling_enabled", "1", FCVAR_REPLICATED, "Toggle whether the player is funneled into portals while running on speed paint.");
ConVar sv_paintairacceleration("sv_paintairacceleration", "5.0f", FCVAR_REPLICATED | FCVAR_CHEAT, "Air acceleration in Paint");
ConVar eggbot_sink_speed("eggbot_sink_speed", "120.0f", FCVAR_REPLICATED | FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
ConVar ballbot_sink_speed("ballbot_sink_speed", "120.0f", FCVAR_REPLICATED | FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
ConVar coop_sink_speed_decay("coop_sink_speed_decay","0.02f", FCVAR_REPLICATED | FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
ConVar coop_impact_velocity_threshold("coop_impact_velocity_threshold", "250.0", FCVAR_REPLICATED | FCVAR_CHEAT );
#define sv_can_carry_both_guns 0 //extern ConVar sv_can_carry_both_guns;
ConVar sv_portal_new_player_trace( "sv_portal_new_player_trace", "1", FCVAR_REPLICATED | FCVAR_CHEAT );
ConVar portal_funnel_debug( "portal_funnel_debug", "0", FCVAR_REPLICATED | FCVAR_CHEAT );
//DIST_EPSILON == 0.03125
ConVar portal_player_interaction_quadtest_epsilon( "portal_player_interaction_quadtest_epsilon", "-0.03125", FCVAR_REPLICATED | FCVAR_CHEAT );
#if defined( CLIENT_DLL )
ConVar cl_vertical_elevator_fix( "cl_vertical_elevator_fix", "1" );
#endif
class CReservePlayerSpot;
#define PORTAL_FUNNEL_AMOUNT 6.0f
#define MAX_CLIP_PLANES 5
//#define DEBUG_FLINGS
// Roughly how often we want to update the info about the ground surface we're on.
// We don't need to do this very often.
#define CATEGORIZE_GROUND_SURFACE_INTERVAL 0.3f
#define CATEGORIZE_GROUND_SURFACE_TICK_INTERVAL ( (int)( CATEGORIZE_GROUND_SURFACE_INTERVAL / TICK_INTERVAL ) )
#define CHECK_STUCK_INTERVAL 1.0f
#define CHECK_STUCK_TICK_INTERVAL ( (int)( CHECK_STUCK_INTERVAL / TICK_INTERVAL ) )
#define CHECK_STUCK_INTERVAL_SP 0.2f
#define CHECK_STUCK_TICK_INTERVAL_SP ( (int)( CHECK_STUCK_INTERVAL_SP / TICK_INTERVAL ) )
#define CHECK_LADDER_INTERVAL 0.2f
#define CHECK_LADDER_TICK_INTERVAL ( (int)( CHECK_LADDER_INTERVAL / TICK_INTERVAL ) )
#define NUM_CROUCH_HINTS 3
#define CRITICAL_SLOPE 0.7f
#define PLAYER_FLING_HELPER_MIN_SPEED 200.0f
const float COS_PI_OVER_SIX = 0.86602540378443864676372317075294f; // cos( 30 degrees ) in radians
extern bool g_bMovementOptimizations;
extern bool g_bAllowForcePortalTrace;
extern bool g_bForcePortalTrace;
// Unnamed namespace is the C++ way of specifying static at file scope.
// It prevents this function from leaking out of this translation unit.
namespace
{
inline void DoTrace( ITraceListData *pTraceListData, const Ray_t &ray, uint32 fMask, ITraceFilter *filter, trace_t *ptr, int *counter )
{
++*counter;
if ( pTraceListData && pTraceListData->CanTraceRay(ray) )
{
enginetrace->TraceRayAgainstLeafAndEntityList( ray, pTraceListData, fMask, filter, ptr );
}
else
{
enginetrace->TraceRay( ray, fMask, filter, ptr );
}
}
} // Unnamed namespace
#if defined( DEBUG_FLINGS )
class CDebugCrouchOverlay : public CAutoGameSystemPerFrame
{
public:
CDebugCrouchOverlay( void )
{
pPlayer = NULL;
pPortal = NULL;
vCenterNudge = vTeleportVel = vTeleportPos = vUnduckVel = vUnduckPos = vUnduckJumpPos = vUnduckJumpVel = vec3_origin;
bWatchFlingVelocity = false;
}
#if defined( CLIENT_DLL )
virtual void PreRender ()
#else
virtual void PreClientUpdate()
#endif
{
if( pPlayer )
{
Vector vDuckMins = pPlayer->GetDuckHullMins();
Vector vDuckMaxs = pPlayer->GetDuckHullMaxs();
Vector vDuckExtents = (vDuckMaxs - vDuckMins) * 0.5f;
Vector vStandMins = pPlayer->GetStandHullMins();
Vector vStandMaxs = pPlayer->GetStandHullMaxs();
Vector vStandExtents = (vStandMaxs - vStandMins) * 0.5f;
if( vTeleportPos != vec3_origin )
{
NDebugOverlay::Box( vTeleportPos, -vTeleportExtents, vTeleportExtents, 255, 0, 0, 100, 0.0f );
NDebugOverlay::HorzArrow( vTeleportPos, vTeleportPos + vTeleportVel * 2.0f, 2.0f, 255, 0, 0, 100, true, 0.0f );
if( vCenterNudge != vec3_origin )
{
NDebugOverlay::HorzArrow( vTeleportPos - vCenterNudge, vTeleportPos, 2.0f, 0, 255, 0, 100, true, 0.0f );
}
}
if( vUnduckPos != vec3_origin )
{
NDebugOverlay::Box( vUnduckPos, -vStandExtents, vStandExtents, 0, 255, 0, 100, 0.0f );
NDebugOverlay::HorzArrow( vUnduckPos, vUnduckPos + vUnduckVel * 2.0f, 2.0f, 0, 255, 0, 100, true, 0.0f );
}
if( vUnduckJumpPos != vec3_origin )
{
NDebugOverlay::Box( vUnduckJumpPos, -vStandExtents, vStandExtents, 0, 0, 255, 100, 0.0f );
NDebugOverlay::HorzArrow( vUnduckJumpPos, vUnduckJumpPos + vUnduckJumpVel * 2.0f, 2.0f, 0, 0, 255, 100, true, 0.0f );
}
}
}
CPortal_Base2D *pPortal;
CPortal_Player *pPlayer;
Vector vTeleportPos;
Vector vTeleportVel;
Vector vTeleportExtents;
Vector vCenterNudge;
Vector vUnduckPos;
Vector vUnduckVel;
Vector vUnduckJumpPos;
Vector vUnduckJumpVel;
bool bWatchFlingVelocity;
};
static CDebugCrouchOverlay s_MovementDebug;
#endif
//trace that has special understanding of how to handle portals
CTrace_PlayerAABB_vs_Portals::CTrace_PlayerAABB_vs_Portals( void )
{
UTIL_ClearTrace( *this );
m_bContactedPortalTransitionRamp = false;
}
bool CTrace_PlayerAABB_vs_Portals::HitPortalRamp( const Vector &vUp )
{
return sv_portal_new_player_trace.GetBool() && (m_bContactedPortalTransitionRamp && DidHit() && (plane.normal.Dot( vUp ) > 0.0f));
}
void PortalTracePlayerBBoxForGround( const Vector& start, const Vector& end, const Vector& minsSrc,
const Vector& maxsSrc, IHandleEntity *player, unsigned int fMask,
int collisionGroup, CTrace_PlayerAABB_vs_Portals& pm, const Vector& vStickNormal );
static inline CBaseEntity *TranslateGroundEntity( CBaseEntity *pGroundEntity )
{
#ifndef CLIENT_DLL
CPhysicsShadowClone *pClone = dynamic_cast<CPhysicsShadowClone *>(pGroundEntity);
if( pClone && pClone->IsUntransformedClone() )
{
CBaseEntity *pSource = pClone->GetClonedEntity();
if( pSource )
return pSource;
}
#endif //#ifndef CLIENT_DLL
return pGroundEntity;
}
//should force player to crouch for this transition
bool ShouldPortalTransitionCrouch( const CPortal_Base2D *pEnterPortal )
{
const float flUpDot = fabs( pEnterPortal->m_matrixThisToLinked.m[2][2] ); //How much does zUp still look like zUp after going through this portal
return (flUpDot < COS_PI_OVER_SIX && (sv_portal_new_player_trace.GetBool() || (flUpDot >= EQUAL_EPSILON)));
}
//if player is already crouched, do NOT automatically uncrouch. You don't actually have to check that the player is exiting the portal, but we assume that's the intent
bool ShouldMaintainFlingAssistCrouch( const CPortal_Base2D *pExitPortal, const Vector &vExitVelocity )
{
return (pExitPortal->m_vForward.z > 0.1f && pExitPortal->m_vForward.z < 0.9) && (vExitVelocity.z > 1.0f) && (vExitVelocity.Dot( pExitPortal->m_vForward ) > PLAYER_FLING_HELPER_MIN_SPEED);
}
#if defined( CLIENT_DLL )
void CPortalGameMovement::ClientVerticalElevatorFixes( CBasePlayer *pPlayer, CMoveData *pMove )
{
//find root move parent of our ground entity
CBaseEntity *pRootMoveParent = pPlayer->GetGroundEntity();
while( pRootMoveParent )
{
C_BaseEntity *pTestParent = pRootMoveParent->GetMoveParent();
if( !pTestParent )
break;
pRootMoveParent = pTestParent;
}
//if it's a C_BaseToggle (func_movelinear / func_door) then enable prediction if it chooses to
bool bRootMoveParentIsLinearMovingBaseToggle = false;
bool bAdjustedRootZ = false;
if( pRootMoveParent && !pRootMoveParent->IsWorld() )
{
C_BaseToggle *pPredictableGroundEntity = dynamic_cast<C_BaseToggle *>(pRootMoveParent);
if( pPredictableGroundEntity && (pPredictableGroundEntity->m_movementType == MOVE_TOGGLE_LINEAR) )
{
bRootMoveParentIsLinearMovingBaseToggle = true;
if( !pPredictableGroundEntity->GetPredictable() )
{
pPredictableGroundEntity->SetPredictionEligible( true );
pPredictableGroundEntity->m_hPredictionOwner = pPlayer;
}
else if( cl_vertical_elevator_fix.GetBool() )
{
Vector vNewOrigin = pPredictableGroundEntity->PredictPosition( player->PredictedServerTime() + TICK_INTERVAL );
if( (vNewOrigin - pPredictableGroundEntity->GetLocalOrigin()).LengthSqr() > 0.01f )
{
bAdjustedRootZ = (vNewOrigin.z != pPredictableGroundEntity->GetLocalOrigin().z);
pPredictableGroundEntity->SetLocalOrigin( vNewOrigin );
//invalidate abs transforms for upcoming traces
C_BaseEntity *pParent = pPlayer->GetGroundEntity();
while( pParent )
{
pParent->AddEFlags( EFL_DIRTY_ABSTRANSFORM );
pParent = pParent->GetMoveParent();
}
}
}
}
}
//re-seat player on vertical elevators
if( bRootMoveParentIsLinearMovingBaseToggle &&
cl_vertical_elevator_fix.GetBool() &&
bAdjustedRootZ )
{
trace_t trElevator;
TracePlayerBBox( pMove->GetAbsOrigin(), pMove->GetAbsOrigin() - Vector( 0.0f, 0.0f, GetPlayerMaxs().z ), MASK_PLAYERSOLID, COLLISION_GROUP_PLAYER_MOVEMENT, trElevator );
if( trElevator.startsolid )
{
//started in solid, and we think it's an elevator. Pop up the player if at all possible
//trace up, ignoring the ground entity hierarchy
Ray_t playerRay;
playerRay.Init( pMove->GetAbsOrigin(), pMove->GetAbsOrigin() + Vector( 0.0f, 0.0f, GetPlayerMaxs().z ), GetPlayerMins(), GetPlayerMaxs() );
CTraceFilterSimpleList ignoreGroundEntityHeirarchy( COLLISION_GROUP_PLAYER_MOVEMENT );
{
ignoreGroundEntityHeirarchy.AddEntityToIgnore( pPlayer );
C_BaseEntity *pParent = pPlayer->GetGroundEntity();
while( pParent )
{
ignoreGroundEntityHeirarchy.AddEntityToIgnore( pParent );
pParent = pParent->GetMoveParent();
}
}
enginetrace->TraceRay( playerRay, MASK_PLAYERSOLID, &ignoreGroundEntityHeirarchy, &trElevator );
if( !trElevator.startsolid ) //success
{
//now trace back down
Vector vStart = trElevator.endpos;
TracePlayerBBox( vStart, pMove->GetAbsOrigin(), MASK_PLAYERSOLID, COLLISION_GROUP_PLAYER_MOVEMENT, trElevator );
if( !trElevator.startsolid &&
(trElevator.m_pEnt == pPlayer->GetGroundEntity()) )
{
//if we landed back on the ground entity, call it good
pMove->SetAbsOrigin( trElevator.endpos );
pPlayer->SetNetworkOrigin( trElevator.endpos ); //paint code loads from network origin after handling paint powers
}
}
}
else if( (trElevator.endpos.z < pMove->GetAbsOrigin().z) && (trElevator.m_pEnt == pPlayer->GetGroundEntity()) )
{
//re-seat on ground entity
pMove->SetAbsOrigin( trElevator.endpos );
pPlayer->SetNetworkOrigin( trElevator.endpos ); //paint code loads from network origin after handling paint powers
}
}
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CPortalGameMovement::CPortalGameMovement()
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
inline CPortal_Player* CPortalGameMovement::GetPortalPlayer() const
{
return assert_cast< CPortal_Player* >( player );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pMove -
//-----------------------------------------------------------------------------
void CPortalGameMovement::ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMove )
{
AssertMsg( pPlayer && pMove, "Null pointers are bad, and you should feel bad." );
if ( pPlayer && pMove )
{
float flStoreFrametime = gpGlobals->frametime;
// Save the current paint player and move data
player = pPlayer;
mv = pMove;
#if defined( CLIENT_DLL )
ClientVerticalElevatorFixes( pPlayer, pMove ); //fixup vertical elevator discrepancies between client and server as best we can
#endif
m_vMoveStartPosition = mv->GetAbsOrigin();
// Get the paint player
CPortal_Player *pPlayer = GetPortalPlayer();
//!!HACK HACK: Adrian - slow down all player movement by this factor.
//!!Blame Yahn for this one.
gpGlobals->frametime *= pPlayer->GetLaggedMovementValue();
// Reset point contents for water check.
ResetGetWaterContentsForPointCache();
// Cropping movement speed scales mv->m_fForwardSpeed etc. globally
// Once we crop, we don't want to recursively crop again, so we set the crop
// flag globally here once per usercmd cycle.
m_bSpeedCropped = false;
m_bInPortalEnv = (((CPortal_Player *)pPlayer)->m_hPortalEnvironment != NULL);
g_bAllowForcePortalTrace = m_bInPortalEnv;
g_bForcePortalTrace = m_bInPortalEnv;
// Figure out move direction
const Vector& stickNormal = pPlayer->GetPortalPlayerLocalData().m_StickNormal;
Vector vForward, vRight;
Vector vPlayerUp = pPlayer->GetPortalPlayerLocalData().m_Up;
AngleVectors( mv->m_vecViewAngles, &vForward, &vRight, NULL ); // Determine movement angles
const Vector worldUp( 0, 0, 1 );
bool shouldProjectInputVectorsOntoGround = pPlayer->GetGroundEntity() != NULL;
if( shouldProjectInputVectorsOntoGround )
{
vForward -= DotProduct( vForward, stickNormal ) * stickNormal;
vRight -= DotProduct( vRight, stickNormal ) * stickNormal;
vForward.NormalizeInPlace();
vRight.NormalizeInPlace();
}
Vector vWishVel = vForward*mv->m_flForwardMove + vRight*mv->m_flSideMove;
vWishVel -= vPlayerUp * DotProduct( vWishVel, vPlayerUp );
// Make sure the gravity direction is the negation of the current stick normal
// Note: This is cached off before the powers are used only because, if stick
// deactivates because of a jump, the jump direction must be the surface
// (stick) normal.
m_vGravityDirection = -stickNormal;
// Figure out paint power
pPlayer->SetInputVector( vWishVel );
pPlayer->UpdatePaintPowers();
// Using the paint power may change the velocity
mv->m_vecVelocity = player->GetAbsVelocity();
// The stick power may cause the abs origin and collision bounds to change
#if defined( GAME_DLL )
mv->SetAbsOrigin( player->GetAbsOrigin() );
#else
mv->SetAbsOrigin( player->GetNetworkOrigin() ); //GetAbsOrigin() introduces prediction delay
#endif
// If the player is sticking this frame, make sure she stays on the surface
// Note: Currently, the only case where stick is the power and the player is
// not on the surface is when we expand the box to try to find sticky
// surfaces immediately after teleporting.
//StayStuckToSurface();
// Use this player's max speed (dependent on whether he's on speed paint)
const float maxSpeed = pPlayer->MaxSpeed();
mv->m_flClientMaxSpeed = mv->m_flMaxSpeed = maxSpeed;
// Run the command.
PlayerMove();
HandlePortalling();
FinishMove();
//if( gpGlobals->frametime != 0.0f )
//{
// DevMsg( "Max Speed: %f\n", maxSpeed );
// DevMsg( "Speed: %f\n", mv->m_vecVelocity.Length() );
// DevMsg( "XY Speed: %f\n", mv->m_vecVelocity.Length2D() );
// DevMsg( "XY Player Speed: %f\n", player->GetLocalVelocity().Length2D() );
// DevMsg( "Eye Offset: (%f, %f, %f)\n", XYZ( pPlayer->GetViewOffset() ) );
// DevMsg( "Final Player Velocity: (%f, %f, %f)\n", XYZ( player->GetAbsVelocity() ) );
//}
#ifndef CLIENT_DLL
pPlayer->UnforceButtons( IN_DUCK );
pPlayer->UnforceButtons( IN_JUMP );
// Try to collide with thrown weapons if we don't currently have a weapon
if( pPlayer->HasWeapons() == false || sv_can_carry_both_guns )
{
// Trace for the portal and paint guns
trace_t pm;
TracePlayerBBox( player->GetAbsOrigin(), player->GetAbsOrigin() + gpGlobals->frametime * player->GetAbsVelocity(), MASK_PLAYERSOLID, COLLISION_GROUP_WEAPON, pm );
if ( pm.m_pEnt && ( FClassnameIs( pm.m_pEnt, "weapon_portalgun" ) || FClassnameIs( pm.m_pEnt, "weapon_paintgun" ) ) )
{
CBasePortalCombatWeapon *pWeap = dynamic_cast< CBasePortalCombatWeapon* >( pm.m_pEnt );
// Only pick up the weapon if there's been enough time since it was thrown
if ( pWeap && pWeap->EnoughTimeSinceThrown() )
{
pPlayer->BumpWeapon( pWeap );
CWeaponPortalgun* pPortalgun = dynamic_cast< CWeaponPortalgun* >( pWeap );
unsigned char iPortalLinkage = 0;
if( pPortalgun )
{
iPortalLinkage = pPortalgun->GetLinkageGroupID();
}
pWeap->OnPickedUp( pPlayer ); // We bump into weapons instead of weapons bumping into us, so call OnPickedUp on the gun's behalf
if( pPortalgun )
{
pPortalgun->SetLinkageGroupID( iPortalLinkage );
}
}
}
}
#endif
//This is probably not needed, but just in case.
gpGlobals->frametime = flStoreFrametime;
}
}
//-----------------------------------------------------------------------------
// Purpose: Base jump behavior, plus an anim event
// Input : -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CPortalGameMovement::CheckJumpButton()
{
if (player->pl.deadflag)
return false;
// Cannot jump will ducked.
if ( player->GetFlags() & FL_DUCKING )
return false;
// No more effect
if (player->GetGroundEntity() == NULL || player->GetGroundEntity()->IsPlayer() )
{
mv->m_nOldButtons |= IN_JUMP;
return false; // in air, so no effect
}
if ( mv->m_nOldButtons & IN_JUMP )
return false; // don't pogo stick
// Cannot jump will in the unduck transition.
if ( player->m_Local.m_bDucking && ( player->GetFlags() & FL_DUCKING ) )
return false;
// Still updating the eye position.
if ( player->m_Local.m_nDuckJumpTimeMsecs > 0 )
return false;
// Start jump animation and player sound (specific TF animation and flags).
GetPortalPlayer()->DoAnimationEvent( PLAYERANIMEVENT_JUMP, 0 );
player->PlayStepSound( (Vector &)mv->GetAbsOrigin(), player->m_pSurfaceData, 1.0, true );
// In the air now.
GetPortalPlayer()->SetJumpedThisFrame( true );
SetGroundEntity( NULL );
player->PlayStepSound( (Vector &)mv->GetAbsOrigin(), player->m_pSurfaceData, 1.0, true );
MoveHelper()->PlayerSetAnimation( PLAYER_JUMP );
float flGroundFactor = 1.0f;
if (player->m_pSurfaceData)
{
flGroundFactor = player->m_pSurfaceData->game.jumpFactor;
}
float flMul = sqrt( 2 * sv_gravity.GetFloat() * 45.f );
// Acclerate upward
// If we are ducking...
Vector vPreJumpVel = mv->m_vecVelocity;
if ( ( player->m_Local.m_bDucking ) || ( player->GetFlags() & FL_DUCKING ) )
{
// d = 0.5 * g * t^2 - distance traveled with linear accel
// t = sqrt(2.0 * 45 / g) - how long to fall 45 units
// v = g * t - velocity at the end (just invert it to jump up that high)
// v = g * sqrt(2.0 * 45 / g )
// v^2 = g * g * 2.0 * 45 / g
// v = sqrt( g * 2.0 * 45 )
//mv->m_vecVelocity[2] = flGroundFactor * flMul; // 2 * gravity * height
mv->m_vecVelocity -= m_vGravityDirection * DotProduct( mv->m_vecVelocity, m_vGravityDirection );
mv->m_vecVelocity -= m_vGravityDirection * flGroundFactor * flMul;
}
else
{
//mv->m_vecVelocity[2] += flGroundFactor * flMul; // 2 * gravity * height
mv->m_vecVelocity -= m_vGravityDirection * flGroundFactor * flMul;
}
FinishGravity();
mv->m_outJumpVel += mv->m_vecVelocity - vPreJumpVel;
mv->m_outStepHeight += 0.15f;
#if !defined( PORTAL2 )
bool bSetDuckJump = (gpGlobals->maxClients == 1); //most games we only set duck jump if the game is single player
#else
//const bool bSetDuckJump = true; //in portal 2, do it for both single and multiplayer
const bool bSetDuckJump = false; // FIX THIS?: This is set to false as a temp fix for camera snapping when ducking in the air due to player maintain the center of the box ( NO DUCKJUMP for now )
#endif
// Set jump time.
if ( bSetDuckJump )
{
player->m_Local.m_nJumpTimeMsecs = GAMEMOVEMENT_JUMP_TIME;
player->m_Local.m_bInDuckJump = true;
}
// Flag that we jumped.
mv->m_nOldButtons |= IN_JUMP; // don't jump again until released
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : wishdir -
// accel -
//-----------------------------------------------------------------------------
void CPortalGameMovement::AirAccelerate( Vector& wishdir, float wishspeed, float accel )
{
int i;
float addspeed, accelspeed, currentspeed;
float wishspd;
wishspd = wishspeed;
if (player->pl.deadflag)
return;
if (player->m_flWaterJumpTime)
return;
// Cap speed
if (wishspd > 60.0f)
wishspd = 60.0f;
// Determine veer amount
currentspeed = mv->m_vecVelocity.Dot(wishdir);
// See how much to add
addspeed = wishspd - currentspeed;
#if defined CLIENT_DLL
RANDOM_CEG_TEST_SECRET();
#endif
// If not adding any, done.
if (addspeed <= 0)
return;
CPortal_Player* pPortalPlayer = GetPortalPlayer();
if( pPortalPlayer == NULL )
return;
float fInputScale = pPortalPlayer->GetPortalPlayerLocalData().m_flAirInputScale;
// Determine acceleration speed after acceleration
accelspeed = accel * wishspeed * gpGlobals->frametime * player->m_surfaceFriction * fInputScale;
// Cap it
if (accelspeed > addspeed)
accelspeed = addspeed;
// Adjust pmove vel.
for (i=0 ; i<3 ; i++)
{
mv->m_vecVelocity[i] += accelspeed * wishdir[i];
mv->m_outWishVel[i] += accelspeed * wishdir[i];
}
}
void CPortalGameMovement::TBeamMove( void )
{
CPortal_Player *pPortalPlayer = GetPortalPlayer();
if ( !pPortalPlayer )
return;
CTrigger_TractorBeam *pTractorBeam = pPortalPlayer->m_PortalLocal.m_hTractorBeam.Get();
if ( !pTractorBeam )
return;
if ( gpGlobals->frametime > 0.0f )
{
Vector vLinear;
AngularImpulse angAngular;
vLinear.Init();
angAngular.Init();
pTractorBeam->CalculateFrameMovement( NULL, pPortalPlayer, gpGlobals->frametime, vLinear, angAngular );
mv->m_vecVelocity += vLinear * gpGlobals->frametime;
}
TryPlayerMove( 0, 0 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPortalGameMovement::AirMove( void )
{
// Determine movement angles
Vector forward, right, up;
AngleVectors(mv->m_vecViewAngles, &forward, &right, &up);
// Copy movement amounts
float fmove = mv->m_flForwardMove;
float smove = mv->m_flSideMove;
{
// Disregard the player's air movement if they're in a phys controller
CBasePlayer *pPlayer = GetPortalPlayer();
if ( pPlayer->HasPhysicsFlag( PFLAG_VPHYSICS_MOTIONCONTROLLER ) )
{
fmove = 0;
smove = 0;
}
}
// Looking mostly straight forward?
if( forward[2] < 0.5f && forward[2] > -0.5f )
{
// Normalize forward vector if they are looking mostly forward
forward -= DotProduct( forward, m_vGravityDirection ) * m_vGravityDirection;
VectorNormalize(forward);
}
else
{
// Do not normalize if they are not. This prevents the player from screwing up their momentum after
// exiting floor portals or jumping off sticky ceilings while looking straight up/down.
forward -= DotProduct( forward, m_vGravityDirection ) * m_vGravityDirection;
}
// Zero out components of movement vectors in the direction of gravity
right -= DotProduct( right, m_vGravityDirection ) * m_vGravityDirection;
// Normalize remainder of vectors
VectorNormalize(right);
Vector targetVel = fmove * forward + smove * right;
// Zero out velocity in gravity direction
targetVel -= DotProduct( targetVel, m_vGravityDirection ) * m_vGravityDirection;
Vector wishdir;
VectorCopy( targetVel, wishdir );
Vector vFunnelForce;
vFunnelForce.Zero();
//
// Don't let the player screw their fling because of adjusting into a floor portal
//
if ( mv->m_vecVelocity[ 0 ] * mv->m_vecVelocity[ 0 ] + mv->m_vecVelocity[ 1 ] * mv->m_vecVelocity[ 1 ] > MIN_FLING_SPEED * MIN_FLING_SPEED )
{
if ( mv->m_vecVelocity[ 0 ] > MIN_FLING_SPEED * 0.5f && wishdir[ 0 ] < 0.0f )
wishdir[ 0 ] = 0.0f;
else if ( mv->m_vecVelocity[ 0 ] < -MIN_FLING_SPEED * 0.5f && wishdir[ 0 ] > 0.0f )
wishdir[ 0 ] = 0.0f;
if ( mv->m_vecVelocity[ 1 ] > MIN_FLING_SPEED * 0.5f && wishdir[ 1 ] < 0.0f )
wishdir[ 1 ] = 0.0f;
else if ( mv->m_vecVelocity[ 1 ] < -MIN_FLING_SPEED * 0.5f && wishdir[ 1 ] > 0.0f )
wishdir[ 1 ] = 0.0f;
}
//
// Try to autocorrect the player to fall into the middle of the portal
//
else if ( sv_player_funnel_into_portals.GetBool() )
{
vFunnelForce = PortalFunnel( wishdir );
}
// See if we're suppressing air control
CPortal_Player *pPlayer = GetPortalPlayer();
if ( pPlayer && pPlayer->IsSuppressingAirControl() )
{
wishdir.Zero();
}
// Add in funnel force after wishdir potentionally gets nuked. We still want
// to funnel, even if the player isnt allowed to move themself
wishdir += vFunnelForce;
// Compute speed and direction from velocity
float targetSpeed = VectorNormalize(wishdir);
// Clamp to server defined max speed
if ( targetSpeed != 0 && (targetSpeed > mv->m_flMaxSpeed) )
{
VectorScale(targetVel, mv->m_flMaxSpeed/targetSpeed, targetVel);
targetSpeed = mv->m_flMaxSpeed;
}
AirAccelerate( wishdir, targetSpeed, sv_paintairacceleration.GetFloat() );
// Add in any base velocity to the current velocity.
VectorAdd(mv->m_vecVelocity, player->GetBaseVelocity(), mv->m_vecVelocity );
TryPlayerMove( 0, 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 );
}
bool CPortalGameMovement::IsInPortalFunnelVolume( const Vector& vPlayerToPortal, const CPortal_Base2D* pPortal, const float flExtentX, const float flExtentY ) const
{
const Vector& portalNormal = pPortal->m_plane_Origin.normal;
Vector vPortalRight = pPortal->m_PortalSimulator.GetInternalData().Placement.vRight;
vPortalRight -= DotProduct( vPortalRight, portalNormal ) * portalNormal;
VectorNormalize( vPortalRight );
float fTestDist = flExtentX;
fTestDist *= fTestDist;
float flDist = ( vPlayerToPortal.Dot( vPortalRight ) * vPortalRight ).LengthSqr();
// Make sure we're in the 2D portal rectangle
if ( flDist > fTestDist )
return false;
Vector vPortalUp = pPortal->m_PortalSimulator.GetInternalData().Placement.vUp;
vPortalUp -= DotProduct( vPortalUp, portalNormal ) * portalNormal;
VectorNormalize( vPortalUp );
fTestDist = flExtentY;
fTestDist *= fTestDist;
flDist = ( vPlayerToPortal.Dot( vPortalUp ) * vPortalUp ).LengthSqr();
if ( flDist > fTestDist )
return false;
return true;
}
bool CPortalGameMovement::PlayerShouldFunnel( const CPortal_Base2D* pPortal, const Vector& vPlayerLook, const Vector& wishdir ) const
{
// In the air
if( player->GetGroundEntity() == NULL )
{
// We are more liberal about funneling into a ceiling portal - All we care about is if we are going up, in general we aren't going
// to be hitting these by accident so we can do the funnel without the requirement that we are looking at the portal
const bool bFunnelUp = mv->m_vecVelocity[ 2 ] > 165.0f;
if( ( fabsf( wishdir[ 0 ] ) > 64.0f || fabsf( wishdir[ 1 ] ) > 64.0f ) ||
!( bFunnelUp || ( vPlayerLook.z < -0.7f && mv->m_vecVelocity[ 2 ] < -165.0f ) ) )
{
return false;
}
// Make sure it's a floor or ceiling portal
const Vector vPlayerToPortal = pPortal->WorldSpaceCenter() - player->WorldSpaceCenter();
if( ( bFunnelUp && !pPortal->IsCeilingPortal() ) ||
( !bFunnelUp && !pPortal->IsFloorPortal() ) )
{
return false;
}
// Make sure that the portal isn't too far away and we aren't past it.
const float fPeakRelativeHeight = (mv->m_vecVelocity[2] * mv->m_vecVelocity[2]) / (2.0f * sv_gravity.GetFloat());
if( ( bFunnelUp && ( (vPlayerToPortal.z > 1024.0f) || (vPlayerToPortal.z <= 0.0f) || (vPlayerToPortal.z > fPeakRelativeHeight) ) ) ||
( !bFunnelUp && ( (vPlayerToPortal.z < -1024.0f) || (vPlayerToPortal.z >= 0.0f) ) ) )
{
return false;
}
float flConeScale = RemapValClamped( fabs(vPlayerToPortal.z), 256.f, 1024.f, 1.5, 3.f );
// Make sure we're directly in line with the portal
if( !IsInPortalFunnelVolume( vPlayerToPortal,
pPortal,
pPortal->m_PortalSimulator.GetInternalData().Placement.fHalfWidth * flConeScale,
pPortal->m_PortalSimulator.GetInternalData().Placement.fHalfHeight * flConeScale) )
{
return false;
}
}
// On the ground and speed funnelling enabled
else if( speed_funnelling_enabled.GetBool() )
{
// The player's max speed isn't larger than walk speed
if( player->MaxSpeed() <= sv_speed_normal.GetFloat() )
return false;
// Not moving toward the portal
const Vector vPlayerToPortal = pPortal->WorldSpaceCenter() - player->WorldSpaceCenter();
const Vector& portalNormal = pPortal->m_plane_Origin.normal;
if( fabs( DotProduct( player->Forward(), portalNormal ) ) < STEEP_SLOPE ||
DotProduct( player->GetAbsVelocity().Normalized(), portalNormal ) >= -STEEP_SLOPE ||
DotProduct( wishdir.Normalized(), portalNormal ) >= -STEEP_SLOPE ||
!IsInPortalFunnelVolume( vPlayerToPortal,
pPortal,
pPortal->m_PortalSimulator.GetInternalData().Placement.fHalfWidth * 1.5f,
pPortal->m_PortalSimulator.GetInternalData().Placement.fHalfHeight * 1.5f ) )
{
return false;
}
}
else
{
return false;
}
return true;
}
Vector CPortalGameMovement::PortalFunnel( const Vector& wishdir )
{
Vector vPlayerLook;
player->EyeVectors( &vPlayerLook );
const CPortal_Base2D *pFunnelInto = NULL;
Vector vPlayerToFunnelPortal;
float fClosestFunnelPortalDistSqr = FLT_MAX;
CPortal_Base2D **pPortals = CPortal_Base2D_Shared::AllPortals.Base();
const int iPortalCount = CPortal_Base2D_Shared::AllPortals.Count();
for( int i = 0; i != iPortalCount; ++i )
{
CPortal_Base2D *pTempPortal = pPortals[i];
if( pTempPortal->IsActivedAndLinked() )
{
const Vector vPlayerToPortal = pTempPortal->WorldSpaceCenter() - player->WorldSpaceCenter();
const float fDistSqr = vPlayerToPortal.LengthSqr();
if( PlayerShouldFunnel( pTempPortal, vPlayerLook, wishdir ) &&
fDistSqr < fClosestFunnelPortalDistSqr )
{
fClosestFunnelPortalDistSqr = fDistSqr;
pFunnelInto = pTempPortal;
vPlayerToFunnelPortal = vPlayerToPortal;
}
}
}
Vector vFunnelDir;
vFunnelDir.Zero();
if( pFunnelInto )
{
// In the air
if( player->GetGroundEntity() == NULL )
{
const float flHeightFromPortal = -vPlayerToFunnelPortal.z - sv_player_funnel_height_adjust.GetFloat();
// Extra funnel force based on how fast we're going
float flExtraFunnelForce = RemapValClamped( mv->m_vecVelocity[2], 0.f, 1065.f, 1.f, sv_player_funnel_speed_bonus.GetFloat() );
// Figure out how long until we hit the portal
const float flVerticalSpeed = mv->m_vecVelocity.z;
const float flGravity = player->GetGravity() != 0 ? player->GetGravity() * sv_gravity.GetFloat() : sv_gravity.GetFloat();
// Solve when we hit
float flRoot1, flRoot2;
if( SolveQuadratic( -flGravity, 2.f * flVerticalSpeed, 2.f * flHeightFromPortal, flRoot1, flRoot2 ) )
{
float flTimeToPortal = flRoot1 > 0.f ? flRoot1 : flRoot2;
if( flRoot2 < flRoot1 && flRoot2 >= 0.f && flRoot1 >= 0.f )
flTimeToPortal = flRoot2;
AirPortalFunnel( vFunnelDir, vPlayerToFunnelPortal, flExtraFunnelForce, flTimeToPortal );
}
}
// On the ground
else
{
const float flExtraFunnelForce = 1.f + (player->MaxSpeed() / 800.0f) * sv_player_funnel_speed_bonus.GetFloat() - 1.f;
// Compute how long it will take to reach the portal, assuming the player moves at max speed
const Vector& portalNormal = pFunnelInto->m_plane_Origin.normal;
const float distanceFromPortal = DotProduct( player->WorldSpaceCenter(), portalNormal ) - pFunnelInto->m_plane_Origin.dist;
const float flTimeToPortal = distanceFromPortal / player->MaxSpeed();
GroundPortalFunnel( vFunnelDir, vPlayerToFunnelPortal, pFunnelInto->m_plane_Origin.normal, flExtraFunnelForce, flTimeToPortal );
}
}//if funnelling
return vFunnelDir;
}
void CPortalGameMovement::AirPortalFunnel( Vector& wishdir,
const Vector& vPlayerToFunnelPortal,
float flExtraFunnelForce,
float flTimeToPortal )
{
float flExponent = RemapValClamped( fabs(vPlayerToFunnelPortal.z), 128.f, 1024.f, 0.01f, 0.15 );
const float flDecay = ExponentialDecay( flExponent, gpGlobals->frametime );
for( int i = 0; i < 2; ++i )
{
// Find out if we'll make it to our destination in time
bool bMakeItInTime = false;
if( mv->m_vecVelocity[i] )
{
const float flTimeToCenter = vPlayerToFunnelPortal[i] / mv->m_vecVelocity[i];
bMakeItInTime = flTimeToCenter < flTimeToPortal;
}
// If we're not going to make it, then accellerate
if( !bMakeItInTime )