/
tf_passtime_ball.cpp
1437 lines (1240 loc) · 42.3 KB
/
tf_passtime_ball.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 Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "tf_passtime_ball.h"
#include "tf_passtime_logic.h"
#include "passtime_ballcontroller.h"
#include "passtime_convars.h"
#include "passtime_game_events.h"
#include "func_passtime_no_ball_zone.h"
#include "tf_shareddefs.h"
#include "tf_player.h"
#include "vcollide_parse.h"
#include "SpriteTrail.h"
#include "soundenvelope.h"
#include "soundent.h"
#include "tf_gamerules.h"
#include "inetchannelinfo.h"
#include "tf_gamestats.h"
#include "tf_team.h"
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
static const float s_flPickupDist = 1000.f;
static const float s_flBlockDist = 30.0f;
static const float s_flClearDist = 50.0f;
static const char *s_pHalloweenBallModel = "models/passtime/ball/passtime_ball_halloween.mdl";
//-----------------------------------------------------------------------------
static objectparams_t SBallVPhysicsObjectParams()
{
objectparams_t params = g_PhysDefaultObjectParams;
params.mass = tf_passtime_ball_mass.GetFloat();
params.dragCoefficient = tf_passtime_ball_drag_coefficient.GetFloat();
params.damping = tf_passtime_ball_damping_scale.GetFloat();
params.rotdamping = tf_passtime_ball_rotdamping_scale.GetFloat();
params.inertia = tf_passtime_ball_inertia_scale.GetFloat();
return params;
}
//-----------------------------------------------------------------------------
// CBallPlayerToucher exists because we need the ball to touch both players and
// triggers. If the ball has FSOLID_TRIGGER, it will touch players but not
// triggers. And if it doesn't have that, it will touch triggers but not players.
// So this is a hack (there's probably a right way to do this) so the ball can
// just be solid and touch triggers, and this will touch players.
class CBallPlayerToucher : public CBaseEntity
{
public:
DECLARE_CLASS( CBallPlayerToucher, CBaseEntity );
CBallPlayerToucher() : m_pBall( 0 ) {}
//-----------------------------------------------------------------------------
virtual void Spawn() OVERRIDE
{
// NOTE: this used to create its own vphysics sphere, but it turns out that
// the engine totally ignores it.
SetCollisionGroup( COLLISION_GROUP_PROJECTILE );
SetModelIndex( m_pBall->GetModelIndex() );
SetMoveType( MOVETYPE_NONE ); // DIFFERENT
m_takedamage = DAMAGE_NO;
SetNextThink( TICK_NEVER_THINK );
m_iHealth = 0;
m_iMaxHealth = 1;
VPhysicsInitNormal( SOLID_NONE, 0, false );
SetSolid( SOLID_VPHYSICS );
SetSolidFlags( FSOLID_TRIGGER );
SetMoveType( MOVETYPE_NONE ); // DIFFERENT
SetParent( m_pBall );
SetLocalOrigin( Vector( 0,0,0 ) );
SetLocalAngles( QAngle( 0,0,0 ) );
SetTransmitState( FL_EDICT_DONTSEND );
AddEffects( EF_NODRAW );
SetTouch( &CBallPlayerToucher::OnTouch );
}
//-----------------------------------------------------------------------------
bool ShouldCollide( int iCollisionGroup, int iContentsMask ) const OVERRIDE
{
NOTE_UNUSED( iContentsMask );
return iCollisionGroup == COLLISION_GROUP_PLAYER_MOVEMENT;
}
private:
friend class CPasstimeBall;
CPasstimeBall *m_pBall;
void OnTouch( CBaseEntity *pOther )
{
m_pBall->OnTouch( pOther );
}
};
LINK_ENTITY_TO_CLASS( _ballplayertoucher, CBallPlayerToucher );
//-----------------------------------------------------------------------------
IMPLEMENT_SERVERCLASS_ST( CPasstimeBall, DT_PasstimeBall )
SendPropInt(SENDINFO(m_iCollisionCount)),
SendPropEHandle(SENDINFO(m_hHomingTarget)),
SendPropEHandle(SENDINFO(m_hCarrier)),
SendPropEHandle(SENDINFO(m_hPrevCarrier)),
END_SEND_TABLE()
//-----------------------------------------------------------------------------
LINK_ENTITY_TO_CLASS( passtime_ball, CPasstimeBall );
PRECACHE_REGISTER( passtime_ball );
CTFPlayer *CPasstimeBall::GetCarrier() const { return m_hCarrier; }
CTFPlayer *CPasstimeBall::GetPrevCarrier() const { return m_hPrevCarrier; }
//-----------------------------------------------------------------------------
CPasstimeBall::CPasstimeBall()
{
m_bLeftOwner = false;
m_pHumLoop = 0;
m_pBeepLoop = 0;
m_pPlayerToucher = 0;
m_flLastTeamChangeTime = 0;
m_flBeginCarryTime = 0;
m_flIdleRespawnTime = 0;
m_bTrailActive = false;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::Precache()
{
PrecacheModel( "passtime/passtime_balltrail_red.vmt" );
PrecacheModel( "passtime/passtime_balltrail_blu.vmt" );
PrecacheModel( "passtime/passtime_balltrail_unassigned.vmt" );
if ( TFGameRules() && TFGameRules()->IsHolidayActive( kHoliday_Halloween ) )
{
PrecacheModel( s_pHalloweenBallModel );
}
else
{
PrecacheModel( tf_passtime_ball_model.GetString() );
}
PrecacheScriptSound( "Passtime.BallSmack" );
PrecacheScriptSound( "Passtime.BallGet" );
PrecacheScriptSound( "Passtime.BallIdle" );
PrecacheScriptSound( "Passtime.BallHoming" );
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
CTFPlayer *CPasstimeBall::GetThrower() const
{
return m_hThrower.Get();
}
//-----------------------------------------------------------------------------
void CPasstimeBall::SetThrower( CTFPlayer *pPlayer )
{
m_hThrower = pPlayer;
if ( !pPlayer )
{
ChangeTeam( TEAM_UNASSIGNED );
}
else
{
ChangeTeam( pPlayer->GetTeamNumber() );
}
}
//-----------------------------------------------------------------------------
unsigned int CPasstimeBall::PhysicsSolidMaskForEntity() const
{
return MASK_PLAYERSOLID; // must include CONTENT_PLAYERCLIP
}
//-----------------------------------------------------------------------------
int CPasstimeBall::GetCollisionCount() const { return m_iCollisionCount; }
//-----------------------------------------------------------------------------
int CPasstimeBall::GetCarryDuration() const
{
return ( (m_flBeginCarryTime > 0) && (m_flBeginCarryTime < gpGlobals->curtime) )
? (gpGlobals->curtime - m_flBeginCarryTime)
: 0;
}
//-----------------------------------------------------------------------------
static const char *GetTrailEffectForTeam( int iTeam )
{
switch ( iTeam )
{
case TF_TEAM_RED: return "passtime/passtime_balltrail_red.vmt";
case TF_TEAM_BLUE: return "passtime/passtime_balltrail_blu.vmt";
default: return "passtime/passtime_balltrail_unassigned.vmt";
};
}
//-----------------------------------------------------------------------------
void CPasstimeBall::ChangeTeam( int iTeam )
{
// this isn't really the right place for this stats code, but its function
// is directly dependent on m_flLastTeamChangeTime so I wanted to keep it
// here to help avoid bugs creeping in.
// NOTE you can't rely on m_hCarrier being valid or correct here, the order
// of operations on calling ChangeTeam isn't stable between all the
// different places where it's called.
float flElapsedTimeOnThisTeam = gpGlobals->curtime - m_flLastTeamChangeTime;
if ( TFGameRules() && TFGameRules()->IsPasstimeMode() && g_pPasstimeLogic )
{
gamerules_roundstate_t state = TFGameRules()->State_Get();
if ( ((state == GR_STATE_RND_RUNNING) || (state == GR_STATE_STALEMATE) || (state == GR_STATE_TEAM_WIN)) && (flElapsedTimeOnThisTeam > 0) )
{
int nElapsedTimeOnThisTeam = MAX( 0, Float2Int( flElapsedTimeOnThisTeam ) );
if ( GetTeamNumber() == TEAM_UNASSIGNED )
{
CTF_GameStats.m_passtimeStats.summary.nBallNeutralSec += nElapsedTimeOnThisTeam;
}
else
{
CTF_GameStats.m_passtimeStats.summary.nTotalCarrySec += nElapsedTimeOnThisTeam;
}
CTFPlayer *pPlayer = GetThrower();
if ( !pPlayer ) pPlayer = GetCarrier(); // this happens when the round ends or player dies or something
if ( pPlayer )
{
CTFTeam *pPlayerTeam = GetGlobalTFTeam( pPlayer->GetTeamNumber() );
CTFTeam *pPlayerEnemyTeam = GetGlobalTFTeam( GetEnemyTeam( pPlayer->GetTeamNumber() ) );
// NOTE: if the ball carrier switches teams and suicides, this will incorrectly
// attribute the time to the wrong team, but I don't care.
if ( pPlayerTeam->GetFlagCaptures() > pPlayerEnemyTeam->GetFlagCaptures() )
{
CTF_GameStats.m_passtimeStats.summary.nTotalWinningTeamBallCarrySec += Float2Int( flElapsedTimeOnThisTeam );
}
else if ( pPlayerTeam->GetFlagCaptures() < pPlayerEnemyTeam->GetFlagCaptures() )
{
CTF_GameStats.m_passtimeStats.summary.nTotalLosingTeamBallCarrySec += Float2Int( flElapsedTimeOnThisTeam );
}
}
}
}
m_flLastTeamChangeTime = gpGlobals->curtime;
BaseClass::ChangeTeam( iTeam );
// teams: TEAM_UNASSIGNED, spectator, TF_TEAM_RED, TF_TEAM_BLUE
// skins: red, blu, unassigned
// NOTE: skins are in this order because we use the same model as the weapon viewmodel
// and m_bHasTeamSkins_Viewmodel expects them in this order
const int skinForTeam[] = { 2, 2, 0, 1 };
iTeam = GetTeamNumber(); // paranoia; set by BaseClass::ChangeTeam
Assert( iTeam >= 0 && iTeam < 4 );
if ( iTeam >= 0 && iTeam < 4 ) // paranoia
{
m_nSkin = skinForTeam[iTeam];
}
if ( m_bTrailActive )
{
const char *pszTrailEffectName = GetTrailEffectForTeam( iTeam );
m_pTrail->SetModel( pszTrailEffectName );
}
if ( iTeam == TEAM_UNASSIGNED )
{
// NOTE: don't call SetThrower here, it'll be recursive.
m_hThrower = 0;
}
}
//-----------------------------------------------------------------------------
bool CPasstimeBall::CreateModelCollider()
{
solid_t tmpSolid;
PhysModelParseSolid( tmpSolid, this, GetModelIndex() );
tmpSolid.params = SBallVPhysicsObjectParams();
tmpSolid.params.pGameData = static_cast<void *>( this );
auto *pPhysObj = VPhysicsInitNormal( SOLID_VPHYSICS, 0, false, &tmpSolid );
if ( !pPhysObj )
{
return false;
}
SetSolidFlags( FSOLID_NOT_STANDABLE );
AddFlag( FL_GRENADE ); // required for airblast deflection to work
pPhysObj->Wake();
return true;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::CreateSphereCollider()
{
// NOTE: calling VPhysicsInitNormal(SOLID_BBOX) doesn't work right.
// Not calling SetSolid after also doesn't work right.
// In order for CreateSphereObject to work and not crash, you must do
// VPhysicsInitNormal( SOLID_NONE followed by SetSolid(whatever)
// Seems like VPHYSICS or BBOX do the same thing.
// Must have FSOLID_TRIGGER to touch players. Unfortunately, triggers can't trigger triggers.
VPhysicsInitNormal( SOLID_NONE, 0, false );
SetSolid( SOLID_VPHYSICS );
SetSolidFlags( FSOLID_NOT_STANDABLE );
AddFlag( FL_GRENADE ); // required for airblast deflection to work
auto params = SBallVPhysicsObjectParams();
params.pGameData = static_cast<void *>( this );
const float flBallRadius = tf_passtime_ball_sphere_radius.GetFloat();
const float flFourThirdsPi = 4.1888f;
params.volume = flFourThirdsPi * (flBallRadius*flBallRadius*flBallRadius);
const int iPhysMat = physprops->GetSurfaceIndex("passtime_ball");
IPhysicsObject *pPhysObj = physenv->CreateSphereObject( flBallRadius, iPhysMat, GetAbsOrigin(), GetAbsAngles(), ¶ms, false );
VPhysicsSetObject( pPhysObj );
SetMoveType( MOVETYPE_VPHYSICS );
pPhysObj->Wake();
}
//-----------------------------------------------------------------------------
void CPasstimeBall::Spawn()
{
// not sure why this has to come first, but iirc it does.
SetCollisionGroup( COLLISION_GROUP_NONE );
// === CBaseProp::Spawn
const char *pszModelName = (char*) STRING( GetModelName() );
if ( !pszModelName || !*pszModelName )
{
if ( TFGameRules() && TFGameRules()->IsHolidayActive( kHoliday_Halloween ) )
{
pszModelName = s_pHalloweenBallModel;
}
else
{
pszModelName = tf_passtime_ball_model.GetString();
}
}
PrecacheModel( pszModelName );
Precache();
SetModel( pszModelName );
SetMoveType( MOVETYPE_PUSH );
m_takedamage = DAMAGE_NO;
SetNextThink( TICK_NEVER_THINK );
m_flAnimTime = gpGlobals->curtime;
m_flPlaybackRate = 0.0f;
SetCycle( 0 );
// === CBreakableProp::Spawn
m_flFadeScale = 1;
m_iHealth = 0;
m_takedamage = tf_passtime_ball_takedamage.GetBool()
? DAMAGE_EVENTS_ONLY
: DAMAGE_NO;
m_iMaxHealth = 1;
// === CPhysicsProp::Spawn
if( IsMarkedForDeletion() )
{
return;
}
m_pPlayerToucher = CreateEntityByName( "_ballplayertoucher" );
((CBallPlayerToucher*)m_pPlayerToucher)->m_pBall = this;
DispatchSpawn( m_pPlayerToucher );
if ( tf_passtime_ball_sphere_collision.GetBool() || !CreateModelCollider() )
{
CreateSphereCollider();
}
// === My spawn
m_flLastTeamChangeTime = gpGlobals->curtime;
m_flBeginCarryTime = -1;
ResetTrail();
ChangeTeam( TEAM_UNASSIGNED );
if ( TFGameRules()->IsPasstimeMode() )
{
// TODO the ball used to be functional in non-wasabi maps, but I haven't maintained it
SetThink( &CPasstimeBall::DefaultThink );
SetNextThink( gpGlobals->curtime );
SetTransmitState( FL_EDICT_ALWAYS );
m_playerSeek.SetIsEnabled( true );
}
m_flLastCollisionTime = gpGlobals->curtime;
m_flAirtimeDistance = 0;
m_eState = STATE_OUT_OF_PLAY;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::SetIdleRespawnTime()
{
auto *pTimer = TFGameRules()->GetActiveRoundTimer();
if ( !pTimer ) return;
auto ts = pTimer->GetTimerState();
auto grs = TFGameRules()->State_Get();
m_flIdleRespawnTime = ((grs == GR_STATE_RND_RUNNING) && (ts == RT_STATE_NORMAL))
? (gpGlobals->curtime + tf_passtime_ball_reset_time.GetFloat())
: 0;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::DisableIdleRespawnTime()
{
m_flIdleRespawnTime = 0;
}
//-----------------------------------------------------------------------------
bool CPasstimeBall::ShouldCollide( int iCollisionGroup, int iContentsMask ) const
{
// note: returning false for COLLISION_GROUP_PLAYER_MOVEMENT means the ball won't
// stop player movement. the only real visible effect when this function doesn't
// return false for COLLISION_GROUP_PLAYER_MOVEMENT is that the ball is unable
// to impart physics forces on itself when a player blocks it, since the player
// will set velocity to zero due to being "stuck" on the ball, even though the
// ball won't actually prevent the player from moving through it.
return (iCollisionGroup != COLLISION_GROUP_PLAYER_MOVEMENT);
}
//-----------------------------------------------------------------------------
void CPasstimeBall::ResetTrail()
{
// ideally this would just drop all of the existing trail points instead of
// re-creating all the entities, but I couldn't find a clean way to do it in
// a reasonable amount of time.
HideTrail();
const char *pszTrailEffect = GetTrailEffectForTeam( GetTeamNumber() );
Vector origin = GetAbsOrigin();
float flStartRadius = tf_passtime_ball_sphere_radius.GetFloat() * 2;
float flEndRadius = tf_passtime_ball_sphere_radius.GetFloat() * 3;
m_pTrail = CSpriteTrail::SpriteTrailCreate( pszTrailEffect, origin, true );
m_pTrail->SetAttachment( this, 0 );
m_pTrail->SetTransmit( true ); // this actually controls whether the attachment parent receives it
m_pTrail->SetTransparency( kRenderTransAlpha, 255, 255, 255, 200, kRenderFxNone );
m_pTrail->SetStartWidth( flStartRadius );
m_pTrail->SetEndWidth( flEndRadius );
m_pTrail->SetTextureResolution( 1 );
m_pTrail->SetLifeTime( 3.0f );
m_bTrailActive = true;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::HideTrail()
{
// ideally this would just hide the existing trails instead of deleting
// them all, but I couldn't find a clean way to do it in a reasonable
// amount of time.
if ( !m_bTrailActive )
{
return;
}
// this is sometimes called from a physics callback (reset trail on collision)
// so use PhysCallbackRemove instead of UTIL_Remove
PhysCallbackRemove( m_pTrail->NetworkProp() );
m_pTrail = nullptr;
m_bTrailActive = false;
}
//-----------------------------------------------------------------------------
CPasstimeBall::~CPasstimeBall()
{
// trail is automatically removed because it's a child
// m_pPlayerToucher is automatically removed because it's a child
if ( m_pHumLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pHumLoop );
}
if ( m_pBeepLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pBeepLoop );
}
}
//-----------------------------------------------------------------------------
// OnBecomeNotCarried: common boilerplate between SetStateFree/OutOfPlay
void CPasstimeBall::OnBecomeNotCarried()
{
CTFPlayer *pCarrier = m_hCarrier;
//
// Carrier management and events
//
if ( pCarrier && pCarrier->m_Shared.HasPasstimeBall() )
{
pCarrier->m_Shared.SetHasPasstimeBall( false );
pCarrier->m_Shared.RemoveCond( TF_COND_SPEED_BOOST, true );
pCarrier->m_Shared.RemoveCond( TF_COND_PASSTIME_INTERCEPTION, true );
pCarrier->TeamFortress_SetSpeed();
PasstimeGameEvents::BallFree( pCarrier->entindex() ).Fire();
}
//
// Stats
//
if( m_flBeginCarryTime > 0 )
{
int nClass = pCarrier->GetPlayerClass()->GetClassIndex();
int nCarrySec = MAX( 0, Float2Int( gpGlobals->curtime - m_flBeginCarryTime ) );
CTF_GameStats.m_passtimeStats.classes[ nClass].nTotalCarrySec += nCarrySec;
m_flBeginCarryTime = -1;
}
//
// Reset various tracking and counters
//
m_iCollisionCount = 0;
m_flAirtimeDistance = 0;
m_flLastCollisionTime = gpGlobals->curtime;
m_bLeftOwner = false;
//m_playerSeek.SetIsEnabled( false ); // TODO: seek will re-enable itself
SetParent( 0 );
}
//-----------------------------------------------------------------------------
void CPasstimeBall::SetStateFree()
{
if ( BOutOfPlay() )
{
// this is a hack to prevent the out-of-play time from counting in the stats
m_flLastTeamChangeTime = gpGlobals->curtime;
}
//
// Change state
//
m_eState = STATE_FREE;
OnBecomeNotCarried();
//
// Make interactive
//
DisableIdleRespawnTime();
RemoveEffects( EF_NODRAW );
m_pPlayerToucher->RemoveSolidFlags( FSOLID_NOT_SOLID );
m_pPlayerToucher->SetSolid( SOLID_VPHYSICS );
m_takedamage = tf_passtime_ball_takedamage.GetBool() ? DAMAGE_EVENTS_ONLY : DAMAGE_NO;
SetMoveType( MOVETYPE_VPHYSICS );
SetSolid( SOLID_VPHYSICS );
SetSolidFlags( FSOLID_NOT_STANDABLE );
SetThrower( m_hCarrier );
TFGameRules()->SetObjectiveObserverTarget( this );
VPhysicsGetObject()->EnableGravity( true );
VPhysicsGetObject()->Wake();
//
// Trail management
//
if ( !m_bTrailActive )
{
// create trails if there aren't any
ResetTrail();
}
//
// Sounds
//
if ( !m_pHumLoop )
{
CReliableBroadcastRecipientFilter filter;
m_pHumLoop = CSoundEnvelopeController::GetController().SoundCreate(
filter, entindex(), "Passtime.BallIdle" );
CSoundEnvelopeController::GetController().Play( m_pHumLoop, 1, PITCH_NORM );
}
//
// Bookeeping
//
if ( m_hCarrier )
{
m_hPrevCarrier = m_hCarrier;
}
m_hCarrier = 0;
}
//-----------------------------------------------------------------------------
bool CPasstimeBall::BOutOfPlay() const { return m_eState == STATE_OUT_OF_PLAY; }
//-----------------------------------------------------------------------------
void CPasstimeBall::SetStateOutOfPlay()
{
// This can be called redundantly during RespawnBall
if ( BOutOfPlay() )
{
return;
}
// this is a hack to make sure the carrier stats are captured because
// ChangeTeam updates some stats and may not be called at end of round.
ChangeTeam( TEAM_UNASSIGNED );
//
// Change state
//
m_eState = STATE_OUT_OF_PLAY;
OnBecomeNotCarried();
//
// Make noninteractive
//
DisableIdleRespawnTime();
AddEffects( EF_NODRAW );
m_pPlayerToucher->AddSolidFlags( FSOLID_NOT_SOLID );
m_pPlayerToucher->SetSolid( SOLID_NONE );
m_takedamage = DAMAGE_NO;
SetMoveType( MOVETYPE_NONE );
SetSolid( SOLID_NONE );
SetSolidFlags( FSOLID_NOT_SOLID );
SetThrower( 0 );
TFGameRules()->SetObjectiveObserverTarget( 0 );
VPhysicsGetObject()->EnableGravity( false );
//
// Trail management
//
HideTrail();
//
// Sounds
//
if ( m_pHumLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pHumLoop );
m_pHumLoop = 0;
}
if ( m_pBeepLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pBeepLoop );
m_pBeepLoop = 0;
}
//
// Bookeeping
//
if ( m_hCarrier )
{
m_hPrevCarrier = m_hCarrier;
}
m_hCarrier = 0;
}
//-----------------------------------------------------------------------------
void CPasstimeBall::SetStateCarried( CTFPlayer *pCarrier )
{
// this can be called when m_eState==STATE_CARRIED when the ball is being
// directly transferred between players.
m_eState = STATE_CARRIED;
Assert( pCarrier );
if ( !pCarrier )
{
SetStateOutOfPlay();
return;
}
//
// Carrier management and events
// FIXME move all of the event handling for ball events into CTFPasstimeLogic
//
Assert( !pCarrier->m_Shared.HasPasstimeBall() );
pCarrier->RemoveInvisibility();
pCarrier->RemoveDisguise();
pCarrier->EndClassSpecialSkill(); // abort demo charge
pCarrier->m_Shared.SetHasPasstimeBall( true );
if ( pCarrier != m_hPrevCarrier )
{
pCarrier->m_Shared.AddCond( TF_COND_SPEED_BOOST, tf_passtime_speedboost_on_get_ball_time.GetFloat() );
// Limit points by time so we can't just throw back and forth a ton for points.
// FIXME awarding points here and also in passtime_logic?
if ( gpGlobals->realtime - g_pPasstimeLogic->GetLastPassTime(pCarrier) > 6.0f ) // FIXME literal balance value
{
CTF_GameStats.Event_PlayerAwardBonusPoints(pCarrier, 0, 5); // FIXME literal balance value
g_pPasstimeLogic->SetLastPassTime(pCarrier);
}
}
pCarrier->TeamFortress_SetSpeed();
//
// Adjust things common to all states
//
DisableIdleRespawnTime();
AddEffects( EF_NODRAW );
m_iCollisionCount = 0;
m_flAirtimeDistance = 0;
m_flLastCollisionTime = gpGlobals->curtime;
m_bLeftOwner = false;
//m_playerSeek.SetIsEnabled( false ); // TODO: seek will re-enable itself
m_pPlayerToucher->AddSolidFlags( FSOLID_NOT_SOLID );
m_pPlayerToucher->SetSolid( SOLID_NONE );
m_takedamage = DAMAGE_NO;
SetMoveType( MOVETYPE_NONE );
SetParent( pCarrier, pCarrier->LookupAttachment( "effect_hand_R" ) );
SetSolid( SOLID_NONE );
SetSolidFlags( FSOLID_NOT_SOLID );
TFGameRules()->SetObjectiveObserverTarget( pCarrier );
VPhysicsGetObject()->EnableGravity( false );
//
// Unique to this state
//
m_bTouchedSinceSpawn = true;
SetLocalOrigin( Vector( 0,0,0 ) ); // because SetParent(pCarrier)
//
// Sounds
//
EmitSound( "Passtime.BallGet" );
if ( m_pHumLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pHumLoop );
m_pHumLoop = 0;
}
if ( m_pBeepLoop )
{
CSoundEnvelopeController::GetController().SoundDestroy( m_pBeepLoop );
m_pBeepLoop = 0;
}
//
// Stats
//
m_flBeginCarryTime = gpGlobals->curtime;
//
// Bookeeping
//
if ( m_hCarrier )
{
m_hPrevCarrier = m_hCarrier;
}
m_hCarrier = pCarrier;
ChangeTeam( pCarrier->GetTeamNumber() );
}
//-----------------------------------------------------------------------------
void CPasstimeBall::MoveToSpawner( const Vector &pos )
{
MoveTo( pos, Vector( 0,0,0 ) );
m_bTouchedSinceSpawn = false;
m_hPrevCarrier = 0;
}
//-----------------------------------------------------------------------------
bool CPasstimeBall::IsDeflectable()
{
return m_eState == STATE_FREE;
}
//-----------------------------------------------------------------------------
int CPasstimeBall::UpdateTransmitState()
{
if ( !TFGameRules()->IsPasstimeMode() )
{
return BaseClass::UpdateTransmitState();
}
return SetTransmitState(FL_EDICT_ALWAYS);
}
//-----------------------------------------------------------------------------
void CPasstimeBall::MoveTo( const Vector &pos, const Vector &vecVel )
{
// NOTE: using Teleport() causes some weird interpolation errors
// because it handles it specially as a "teleport list" etc
SetAbsOrigin( pos );
SetAbsVelocity( vecVel );
SetAbsAngles( QAngle( 0, 0, 0 ) );
IPhysicsObject *pPhys = VPhysicsGetObject();
pPhys->SetPosition( pos, QAngle( 0, 0, 0 ), true );
Vector fwd = vecVel.Normalized();
AngularImpulse angular( fwd.x * 0, fwd.y * 0, fwd.z * 1 ); // TODO
pPhys->SetVelocity( &vecVel, &angular );
PhysicsTouchTriggers();
m_vecPrevOrigin = pos; // used for tracking pass distance
CPasstimeBallController::BallSpawned( this );
}
//-----------------------------------------------------------------------------
bool CPasstimeBall::BShouldPanicRespawn() const
{
if ( !TFGameRules()
|| ( TFGameRules()->State_Get() != GR_STATE_RND_RUNNING )
|| ( m_eState != STATE_FREE ) )
{
return false;
}
if ( ( m_flIdleRespawnTime > 0 ) && ( m_flIdleRespawnTime < gpGlobals->curtime ) )
{
return true;
}
return ( enginetrace->GetPointContents( GetAbsOrigin() ) == CONTENTS_SOLID );
}
//-----------------------------------------------------------------------------
void CPasstimeBall::DefaultThink()
{
UpdateLagCompensationHistory();
if( IsMarkedForDeletion() || !g_pPasstimeLogic )
{
return;
}
SetNextThink( gpGlobals->curtime );
if ( BShouldPanicRespawn() )
{
g_pPasstimeLogic->RespawnBall();
return;
}
//
// Eject the ball if the carrier isn't allowed to carry it
//
CTFPlayer *pCarrier = m_hCarrier;
if ( pCarrier )
{
HudNotification_t ejectReason;
if ( !g_pPasstimeLogic->BCanPlayerPickUpBall( pCarrier, &ejectReason ) )
{
if ( ejectReason && TFGameRules() )
{
CSingleUserReliableRecipientFilter filter( pCarrier );
TFGameRules()->SendHudNotification( filter, ejectReason );
}
g_pPasstimeLogic->EjectBall( pCarrier, pCarrier );
SetIdleRespawnTime(); // have to do this here because need to guarantee it happens for no ball zones
EmitSound( "Passtime.BallDropped");
return;
}
}
//
// Track airtime and apply controllers
//
if ( m_eState == STATE_FREE )
{
{
Vector vecOrigin = GetAbsOrigin();
m_flAirtimeDistance += vecOrigin.DistTo( m_vecPrevOrigin );
m_vecPrevOrigin = vecOrigin;
}
IPhysicsObject *pPhysObj = VPhysicsGetObject();
Vector vecVel;
pPhysObj->GetVelocity( &vecVel, 0 );
SetAbsVelocity( vecVel );
// this is a hack to work around some issues where GetAbsVelocity was just
// returning some huge value. this seems to fix it, so something is probably fubar in physics :/
// hopefully just related to using the sphere collider that nothing else uses.
pPhysObj->Wake(); // NEVER SLEEP
//m_playerSeek.SetIsEnabled( !m_bTouchedSinceSpawn );
CPasstimeBallController::ApplyTo( this );
}
}
//-----------------------------------------------------------------------------
extern ConVar sv_maxunlag;
void CPasstimeBall::UpdateLagCompensationHistory()
{
// adapted from CLagCompensationManager::FrameUpdatePostEntityThink
Assert( m_lagCompensationHistory.Count() < 1000 ); // insanity check
m_flLagCompensationTeleportDistanceSqr = 64*64;
// remove tail records that are too old
int tailIndex = m_lagCompensationHistory.Tail();
int flDeadtime = gpGlobals->curtime - sv_maxunlag.GetFloat();
while ( m_lagCompensationHistory.IsValidIndex( tailIndex ) )
{
LagRecord &tail = m_lagCompensationHistory.Element( tailIndex );
// if tail is within limits, stop
if ( tail.flSimulationTime >= flDeadtime )
break;
// remove tail, get new tail
m_lagCompensationHistory.Remove( tailIndex );
tailIndex = m_lagCompensationHistory.Tail();
}
// check if head has same simulation time
if ( m_lagCompensationHistory.Count() > 0 )
{
LagRecord &head = m_lagCompensationHistory.Element( m_lagCompensationHistory.Head() );
// check if player changed simulation time since last time updated
if ( head.flSimulationTime >= GetSimulationTime() )
return; // don't add new entry for same or older time
}
// add new record to player track
LagRecord &record = m_lagCompensationHistory.Element( m_lagCompensationHistory.AddToHead() );
record.flSimulationTime = GetSimulationTime();
record.vecOrigin = GetAbsOrigin();
}
//-----------------------------------------------------------------------------
void CPasstimeBall::StartLagCompensation( CBasePlayer *player, CUserCmd *cmd )
{
m_bLagCompensationNeedsRestore = false; // set to true if it actually backtracks
if ( m_lagCompensationHistory.Count() <= 0 )
return;
// adapted from CLagCompensationManager::StartLagCompensation
int targettick = cmd->tick_count;
{
// correct is the amout of time we have to correct game time
float correct = 0.0f;
INetChannelInfo *nci = engine->GetPlayerNetInfo( player->entindex() );
if ( nci )
{
// add network latency
correct+= nci->GetLatency( FLOW_OUTGOING );
}
// calc number of view interpolation ticks - 1
int lerpTicks = TIME_TO_TICKS( player->m_fLerpTime );
// add view interpolation latency see C_BaseEntity::GetInterpolationAmount()
correct += TICKS_TO_TIME( lerpTicks );
// check bouns [0,sv_maxunlag]
correct = clamp( correct, 0.0f, sv_maxunlag.GetFloat() );
// correct tick send by player
targettick = cmd->tick_count - lerpTicks;
// calc difference between tick send by player and our latency based tick
float deltaTime = correct - TICKS_TO_TIME(gpGlobals->tickcount - targettick);
if ( fabs( deltaTime ) > 0.2f )
{
// difference between cmd time and latency is too big > 200ms, use time correction based on latency
// DevMsg("StartLagCompensation: delta too big (%.3f)\n", deltaTime );
targettick = gpGlobals->tickcount - TIME_TO_TICKS( correct );
}
}
// copied from BacktrackPlayer
Vector org;
float flTargetTime = TICKS_TO_TIME( targettick );
{
int curr = m_lagCompensationHistory.Head();
LagRecord *prevRecord = 0;
LagRecord *record = 0;
Vector prevOrg = GetAbsOrigin();
// Walk context looking for any invalidating pEvent
while( m_lagCompensationHistory.IsValidIndex(curr) )
{
// remember last record
prevRecord = record;
// get next record
record = &m_lagCompensationHistory.Element( curr );
Vector delta = record->vecOrigin - prevOrg;
if ( delta.Length2DSqr() > m_flLagCompensationTeleportDistanceSqr )
{
// lost track, too much difference
return;
}
// did we find a context smaller than target time ?
if ( record->flSimulationTime <= flTargetTime )
break; // hurra, stop
prevOrg = record->vecOrigin;
// go one step back
curr = m_lagCompensationHistory.Next( curr );
}
Assert( record );
if ( !record )
{