summaryrefslogtreecommitdiff
path: root/game/server/tf/tf_passtime_ball.cpp
blob: 72ea39cf6d268fa0aba282b614d3bccb4cf5a8c0 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
//========= 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(), &params, 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 )
		{
			return; // that should never happen
		}


		float frac = 0.0f;
		if ( prevRecord && 
			 (record->flSimulationTime < flTargetTime) &&
			 (record->flSimulationTime < prevRecord->flSimulationTime) )
		{
			// we didn't find the exact time but have a valid previous record
			// so interpolate between these two records;

			Assert( prevRecord->flSimulationTime > record->flSimulationTime );
			Assert( flTargetTime < prevRecord->flSimulationTime );

			// calc fraction between both records
			frac = ( flTargetTime - record->flSimulationTime ) / 
				( prevRecord->flSimulationTime - record->flSimulationTime );

			Assert( frac > 0 && frac < 1 ); // should never extrapolate

			org	= Lerp( frac, record->vecOrigin, prevRecord->vecOrigin );
		}
		else
		{
			// we found the exact record or no other record to interpolate with
			// just copy these values since they are the best we have
			org	= record->vecOrigin;
		}
	}

	Vector orgdiff = GetAbsOrigin() - org;
	m_lagCompensationRestore.flSimulationTime = GetSimulationTime();
	m_lagCompensationRestore.vecOrigin = GetAbsOrigin();
	SetAbsOrigin( org );
	SetSimulationTime( flTargetTime );
	m_bLagCompensationNeedsRestore = true;
}

//-----------------------------------------------------------------------------
void CPasstimeBall::FinishLagCompensation( CBasePlayer *player )
{
	// adapted from CLagCompensationManager::BacktrackPlayer

	if ( !m_bLagCompensationNeedsRestore )
	{
		return;
	}

	SetAbsOrigin( m_lagCompensationRestore.vecOrigin ); // this is probably not correct?
	SetSimulationTime( m_lagCompensationRestore.flSimulationTime );
}

//-----------------------------------------------------------------------------
bool CPasstimeBall::BIgnorePlayer( CTFPlayer *pPlayer )
{
	// NOTE: it's possible to be !alive and !dead at the same time
	if ( !pPlayer || !pPlayer->IsAlive() )
	{
		return true;
	}

	if ( !m_bLeftOwner && (pPlayer == GetThrower()) )
	{
		const float flDist = CalcDistanceToAABB( 
			pPlayer->WorldAlignMins(),
			pPlayer->WorldAlignMaxs(),
			GetAbsOrigin() - pPlayer->GetAbsOrigin() );
		m_bLeftOwner = flDist > s_flClearDist;
		return !m_bLeftOwner;
	}
	else
	{
		m_bLeftOwner = true;
		return false;
	}
}

//-----------------------------------------------------------------------------
void CPasstimeBall::TouchPlayer( CTFPlayer *pPlayer )
{
	if ( !TFGameRules() )
	{
		return;
	}

	//
	// Is this player close enough to hit it?
	// TODO is this still necessary since we use actual physics touching now?
	//
	{
		const Vector& vecMyOrigin = GetAbsOrigin();
		const Vector& vecOtherOrigin = pPlayer->GetAbsOrigin();
		const Vector vecOtherHead = vecOtherOrigin + Vector( 0, 0, pPlayer->BoundingRadius() + 8 );
		float t = 0;
		const float flDist = CalcDistanceToLineSegment( vecMyOrigin, vecOtherOrigin, vecOtherHead, &t );
		if ( (flDist > s_flBlockDist) && (flDist > s_flPickupDist) )
		{
			return;
		}
	}

	const bool bSameTeam = GetThrower() && (pPlayer->GetTeamNumber() == GetThrower()->GetTeamNumber());

	//
	// Can this player get the ball?
	//
	bool bCanPickUp = false;
	{
		HudNotification_t cantPickUpReason;
		bCanPickUp = g_pPasstimeLogic->BCanPlayerPickUpBall( pPlayer, &cantPickUpReason );
		if ( cantPickUpReason )
		{
			CSingleUserReliableRecipientFilter filter( pPlayer );
			TFGameRules()->SendHudNotification( filter, cantPickUpReason );
		}
	}


	if ( bCanPickUp )
	{
		m_bTouchedSinceSpawn = true;
		g_pPasstimeLogic->OnPlayerTouchBall( pPlayer, this );
	}
	else if ( !bSameTeam )
	{
		// can't pick it up and not on the same team = block

		// NOTE: BlockDamage has to come after BlockReflect in order for 
		// the reflection to work right. BlockDamage might apply a force
		// to the player, which will taint the reflection vector.
		// NOTE: because some of these functions might change the ball's
		// velocity, get it once and then pass it to each.
		IPhysicsObject* pPhysObj = VPhysicsGetObject();
		Vector vecBallVel; 
		pPhysObj->GetVelocity( &vecBallVel, 0 );

		BlockReflect( pPlayer, pPlayer->GetAbsOrigin(), vecBallVel );
		BlockDamage( pPlayer, vecBallVel );

		if ( GetThrower() )
		{
			// ball was in flight
			PasstimeGameEvents::BallBlocked( GetThrower()->entindex(), pPlayer->entindex() ).Fire();
		}
			
		CPasstimeBallController::DisableOn( this );
		m_iCollisionCount++;
		SetThrower( 0 );
		m_flAirtimeDistance = 0;
		m_flLastCollisionTime = gpGlobals->curtime;
	}
}

//-----------------------------------------------------------------------------
void CPasstimeBall::BlockReflect( CTFPlayer *pPlayer, const Vector& vecBallOrigin, const Vector& vecBallVel )
{
	if ( m_hBlocker == pPlayer )
	{
		// this helps prevent the ball from getting stuck inside players
		return;
	}

	m_hBlocker = pPlayer;

	const Vector vecMyOrigin = GetAbsOrigin();
	Vector vecBallDir = vecBallVel;
	vecBallDir.z = 0;
	const float flBallSpeed = vecBallDir.NormalizeInPlace();

	Vector vecReflectVel = vecMyOrigin - vecBallOrigin;
	vecReflectVel.z = 0;
	vecReflectVel.NormalizeInPlace();
	vecReflectVel = vecReflectVel.Cross( vecBallDir );
	vecReflectVel.NormalizeInPlace();
	vecReflectVel = vecBallDir.Cross( vecReflectVel );
	vecReflectVel.NormalizeInPlace();
	vecReflectVel -= vecBallDir;
	vecReflectVel *= flBallSpeed / 2.0f;
	vecReflectVel += pPlayer->GetAbsVelocity();

	AngularImpulse spin(0,0,0);
	SetAbsVelocity( vecReflectVel );
	VPhysicsGetObject()->SetVelocity( &vecReflectVel, &spin );

	if ( flBallSpeed > 300 )
	{
		EmitSound( "Passtime.BallSmack" );
	}
}

//-----------------------------------------------------------------------------
void CPasstimeBall::BlockDamage( CTFPlayer *pPlayer, const Vector& vecBallVel )
{
	const float flSpeed = vecBallVel.Length();
	const float flDamageSpeed = 1000;
	
	pPlayer->m_Shared.OnSpyTouchedByEnemy();

	if ( flSpeed >= flDamageSpeed )
	{
		CTakeDamageInfo di;
		di.SetAttacker( GetThrower() );
		di.SetDamage( 1 );
		di.SetDamageType( DMG_CLUB );
		di.SetInflictor( this );
		di.SetDamagePosition( GetAbsOrigin() );
		di.SetDamageForce( vecBallVel ); // needs to be set to nonzero
		if ( flSpeed > 1200 )
		{
			di.AddDamageType( DMG_CRITICAL );
		}
		pPlayer->TakeDamage( di );
	}
}

//-----------------------------------------------------------------------------
static bool IsGroundCollision( int index, const gamevcollisionevent_t *pEvent )
{
	// this little arcane incantation stolen from somewhere else
	const int otherindex = !index;
	IPhysicsObject *pPhysObj = pEvent->pObjects[otherindex];
	CBaseEntity *pOther = static_cast<CBaseEntity *>(pPhysObj->GetGameData());

	if ( !pOther || !pEvent->pInternalData )
	{
		return false; // paranoia
	}

	Vector vecNormal;
	pEvent->pInternalData->GetSurfaceNormal( vecNormal );
	return Vector( 0, 0, 1 ).Dot( vecNormal ) < -0.7f; // why is this backwards?
}

//-----------------------------------------------------------------------------
void CPasstimeBall::OnTouch( CBaseEntity *pOther )
{
	// If two players touch the ball in the same frame inside the physics system,
	// the ball will get a touch callback for both regardless of what happens
	// in response to the first call (i.e. it's just iterating a contact list).
	// This catches the case where the ball was already picked up this frame.
	if ( !TFGameRules()->IsPasstimeMode() || (m_eState != STATE_FREE) )
	{
		return;
	}

	CTFPlayer *pPlayer = ToTFPlayer( pOther );
	if ( !BIgnorePlayer( pPlayer ) )
	{
		TouchPlayer( pPlayer );
	}
}

//-----------------------------------------------------------------------------
void CPasstimeBall::VPhysicsCollision( int index, gamevcollisionevent_t *pEvent ) 
{
	BaseClass::VPhysicsCollision( index, pEvent );
	
	if ( !TFGameRules()->IsPasstimeMode() )
	{
		return;
	}

	if ( g_pPasstimeLogic && (g_pPasstimeLogic->GetBall() == this) 
		&& g_pPasstimeLogic->OnBallCollision( this, index, pEvent )
		&& IsGroundCollision( index, pEvent ) )
	{
		OnCollision();
	}
	CPasstimeBallController::BallCollision( this, index, pEvent );
	m_hBlocker.Term();
}

//-----------------------------------------------------------------------------
void CPasstimeBall::OnCollision()
{
	m_flAirtimeDistance = 0;
	m_flLastCollisionTime = gpGlobals->curtime;
	++m_iCollisionCount;
	if ( m_iCollisionCount == 1 ) 
	{
		SetThrower( 0 );
		if ( m_bTouchedSinceSpawn )
		{
			SetIdleRespawnTime();
		}
	}
	m_hBlocker.Term();
}

//-----------------------------------------------------------------------------
int	CPasstimeBall::OnTakeDamage( const CTakeDamageInfo &info ) 
{
	if ( !tf_passtime_ball_takedamage.GetBool() )
	{
		// this can happen if the cvar is disabled after the ball has spawned
		return 0;
	}

	if ( !m_bTouchedSinceSpawn && (GetCollisionCount() == 0) )
	{
		++CTF_GameStats.m_passtimeStats.summary.nTotalBallSpawnShots;
	}

	if ( TFGameRules()->IsPasstimeMode() )
	{
		CPasstimeBallController::BallDamaged( this );
		CPasstimeBallController::DisableOn( this );
		OnCollision();
	}

	if ( IPhysicsObject* pPhysObj = VPhysicsGetObject() )
	{
		pPhysObj->EnableMotion( true );
		pPhysObj->ApplyForceOffset( info.GetDamageForce().Normalized() * tf_passtime_ball_takedamage_force.GetFloat(), GetAbsOrigin() );
	}

	return 0;
}

//-----------------------------------------------------------------------------
void CPasstimeBall::Deflected(CBaseEntity *pDeflectedBy, Vector& vecDir )
{
	NOTE_UNUSED( pDeflectedBy );
	IPhysicsObject* pPhysObj = VPhysicsGetObject();
	if ( !pPhysObj )
	{
		return;
	}

	// WeaponBase::DeflectEntity will redirect the velocity with the same flSpeed, 
	// which means that a stationary ball won't move since it has 0 flSpeed. this 
	// will just make sure the velocity is what it should be

	// vecDir points from the point under the player's crosshair to the ball's origin.
	// this will make ball deflection work just like rockets, except the velocity
	// is normalized instead of just being whatever magnitude it was before deflection.
	Vector vecVel = -vecDir * tf_passtime_ball_takedamage_force.GetFloat();
	pPhysObj->SetVelocity( &vecVel, 0 );

	if ( TFGameRules()->IsPasstimeMode() )
	{
		++CTF_GameStats.m_passtimeStats.summary.nTotalBallDeflects;

		// stop passing, etc
		CPasstimeBallController::DisableOn( this );

		// count as a collision
		OnCollision();
	}
}

//-----------------------------------------------------------------------------
//static 
CPasstimeBall *CPasstimeBall::Create( Vector vecPosition, QAngle angles )
{
	// mostly copied from CreatePhysicsToy
	MDLCACHE_CRITICAL_SECTION();
	MDLHandle_t hMdl = mdlcache->FindMDL( tf_passtime_ball_model.GetString() );
	Assert( hMdl != MDLHANDLE_INVALID );
	if( hMdl == MDLHANDLE_INVALID )
	{
		return 0;
	}

	studiohdr_t *pStudioHdr = mdlcache->GetStudioHdr( hMdl );
	Assert( pStudioHdr );
	if( !pStudioHdr ) 
	{
		return 0;
	}

	// i don't know what this "allow precache" stuff does, 
	// i copied it from other code and forgot to note where it was
	bool oldAllowPrecache = CBaseEntity::IsPrecacheAllowed();
	CBaseEntity::SetAllowPrecache( true ); 

	CPasstimeBall *pBall = dynamic_cast< CPasstimeBall* >( CreateEntityByName( "passtime_ball" ) ); 

	char pszBuf[512];
	Q_snprintf( pszBuf, sizeof( pszBuf ), "%.10f %.10f %.10f", vecPosition.x, vecPosition.y, vecPosition.z );
	pBall->KeyValue( "origin", pszBuf );
	Q_snprintf( pszBuf, sizeof( pszBuf ), "%.10f %.10f %.10f", angles.x, angles.y, angles.z );
	pBall->KeyValue( "angles", pszBuf );
	pBall->KeyValue( "fademindist", "-1" );
	pBall->KeyValue( "fademaxdist", "0" );
	pBall->KeyValue( "fadescale", "1" );
	DispatchSpawn( pBall );
	pBall->Activate();

	CBaseEntity::SetAllowPrecache( oldAllowPrecache );
	mdlcache->Release( hMdl );
	return pBall;
}

//-----------------------------------------------------------------------------
void CPasstimeBall::SetHomingTarget( CTFPlayer *pPlayer ) 
{ 
	m_hHomingTarget = pPlayer; 
	if ( m_hHomingTarget )
	{
		if ( !m_pBeepLoop )
		{
			CReliableBroadcastRecipientFilter filter;
			m_pBeepLoop = CSoundEnvelopeController::GetController().SoundCreate( 
				filter, entindex(), "Passtime.BallHoming" );
			CSoundEnvelopeController::GetController().Play( m_pBeepLoop, 1, PITCH_NORM );
		}
	}
	else
	{
		if ( m_pBeepLoop )
		{
			CSoundEnvelopeController::GetController().SoundDestroy( m_pBeepLoop );
			m_pBeepLoop = 0;
		}
	}
}

//-----------------------------------------------------------------------------
CTFPlayer *CPasstimeBall::GetHomingTarget() const
{
	return m_hHomingTarget;
}

//-----------------------------------------------------------------------------
float CPasstimeBall::GetAirtimeSec() const 
{
	return MAX( 0, gpGlobals->curtime - m_flLastCollisionTime );
}

//-----------------------------------------------------------------------------
float CPasstimeBall::GetAirtimeDistance() const 
{
	return m_flAirtimeDistance;
}