aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/server/team_control_point_master.cpp
blob: 8ad9796d9150aaf26783c4bf1671de5f0eb0306a (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//

#include "cbase.h"
#include "team_objectiveresource.h"
#include "team_control_point_master.h"
#include "teamplayroundbased_gamerules.h"

#if defined ( TF_DLL )
#include "tf_gamerules.h"
#endif

BEGIN_DATADESC( CTeamControlPointMaster )
	DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
	DEFINE_KEYFIELD( m_iszCapLayoutInHUD, FIELD_STRING, "caplayout" ),
	DEFINE_KEYFIELD( m_iInvalidCapWinner, FIELD_INTEGER, "cpm_restrict_team_cap_win" ),
	DEFINE_KEYFIELD( m_bSwitchTeamsOnWin, FIELD_BOOLEAN, "switch_teams" ),
	DEFINE_KEYFIELD( m_bScorePerCapture, FIELD_BOOLEAN, "score_style" ),
	DEFINE_KEYFIELD( m_bPlayAllRounds, FIELD_BOOLEAN, "play_all_rounds" ),

	DEFINE_KEYFIELD( m_flPartialCapturePointsRate, FIELD_FLOAT, "partial_cap_points_rate" ),

	DEFINE_KEYFIELD( m_flCustomPositionX, FIELD_FLOAT, "custom_position_x" ),
	DEFINE_KEYFIELD( m_flCustomPositionY, FIELD_FLOAT, "custom_position_y" ),

//	DEFINE_FIELD( m_ControlPoints, CUtlMap < int , CTeamControlPoint * > ),
//	DEFINE_FIELD( m_bFoundPoints, FIELD_BOOLEAN ),
//	DEFINE_FIELD( m_ControlPointRounds, CUtlVector < CTeamControlPointRound * > ),
//	DEFINE_FIELD( m_iCurrentRoundIndex, FIELD_INTEGER ),
//	DEFINE_ARRAY( m_iszTeamBaseIcons, FIELD_STRING, MAX_TEAMS ),
//	DEFINE_ARRAY( m_iTeamBaseIcons, FIELD_INTEGER, MAX_TEAMS ),
//	DEFINE_FIELD( m_bFirstRoundAfterRestart, FIELD_BOOLEAN ),

	DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
	DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),

	DEFINE_INPUTFUNC( FIELD_INTEGER, "SetWinner", InputSetWinner ),
	DEFINE_INPUTFUNC( FIELD_INTEGER, "SetWinnerAndForceCaps", InputSetWinnerAndForceCaps ),
	DEFINE_INPUTFUNC( FIELD_VOID, "RoundSpawn", InputRoundSpawn ),
	DEFINE_INPUTFUNC( FIELD_VOID, "RoundActivate", InputRoundActivate ),
	DEFINE_INPUTFUNC( FIELD_STRING, "SetCapLayout", InputSetCapLayout ),
	DEFINE_INPUTFUNC( FIELD_FLOAT, "SetCapLayoutCustomPositionX", InputSetCapLayoutCustomPositionX ),
	DEFINE_INPUTFUNC( FIELD_FLOAT, "SetCapLayoutCustomPositionY", InputSetCapLayoutCustomPositionY ),

	DEFINE_FUNCTION( CPMThink ),

	DEFINE_OUTPUT( m_OnWonByTeam1,	"OnWonByTeam1" ),
	DEFINE_OUTPUT( m_OnWonByTeam2,	"OnWonByTeam2" ),

END_DATADESC()

LINK_ENTITY_TO_CLASS( team_control_point_master, CTeamControlPointMaster );

ConVar mp_time_between_capscoring( "mp_time_between_capscoring", "30", FCVAR_GAMEDLL, "Delay between scoring of owned capture points.", true, 1, false, 0 );

// sort function for the list of control_point_rounds (we're sorting them by priority...highest first)
int ControlPointRoundSort( CTeamControlPointRound* const *p1, CTeamControlPointRound* const *p2 )
{
	// check the priority
	if ( (*p2)->GetPriorityValue() > (*p1)->GetPriorityValue() )
	{
		return 1;
	}

	return -1;
}

//-----------------------------------------------------------------------------
// Purpose: init
//-----------------------------------------------------------------------------
CTeamControlPointMaster::CTeamControlPointMaster()
{
	m_flPartialCapturePointsRate = 0.0f;
	m_flCustomPositionX = -1.f;
	m_flCustomPositionY = -1.f;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::Spawn( void )
{
	Precache();

	SetTouch( NULL );
	m_bFoundPoints = false;
	SetDefLessFunc( m_ControlPoints );

	m_iCurrentRoundIndex = -1;
  	m_bFirstRoundAfterRestart = true;
	m_flLastOwnershipChangeTime = -1;

	BaseClass::Spawn();

	if ( g_hControlPointMasters.Find(this) == g_hControlPointMasters.InvalidIndex() )
	{
		g_hControlPointMasters.AddToTail( this );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::UpdateOnRemove( void )
{
	BaseClass::UpdateOnRemove();

	g_hControlPointMasters.FindAndRemove( this );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::KeyValue( const char *szKeyName, const char *szValue )
{	
	if ( !Q_strncmp( szKeyName, "team_base_icon_", 15 ) )
	{
		int iTeam = atoi(szKeyName+15);
		Assert( iTeam >= 0 && iTeam < MAX_TEAMS );

		m_iszTeamBaseIcons[iTeam] = AllocPooledString(szValue);
	}
	else
	{
		return BaseClass::KeyValue( szKeyName, szValue );
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::Precache( void )
{
	for ( int i = 0; i < MAX_TEAMS; i++ )
	{
		if ( m_iszTeamBaseIcons[i] != NULL_STRING )
		{
			PrecacheMaterial( STRING( m_iszTeamBaseIcons[i] ) );
			m_iTeamBaseIcons[i] = GetMaterialIndex( STRING( m_iszTeamBaseIcons[i] ) );
			Assert( m_iTeamBaseIcons[i] != 0 );
		}
	}

	BaseClass::Precache();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::Activate( void )
{
	BaseClass::Activate();

	// Find control points right away. This allows client hud elements to know the
	// number & starting state of control points before the game actually starts.
	FindControlPoints();
	FindControlPointRounds();

	SetBaseControlPoints();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::RoundRespawn( void )
{

}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::Reset( void )
{

}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::FindControlPoints( void )
{
	//go through all the points
	CBaseEntity *pEnt = gEntList.FindEntityByClassname( NULL, GetControlPointName() );

	int numFound = 0;
	
	while( pEnt )
	{
		CTeamControlPoint *pPoint = assert_cast<CTeamControlPoint *>(pEnt);

		if( pPoint->IsActive() && !pPoint->IsMarkedForDeletion() )
		{
			int index = pPoint->GetPointIndex();

			Assert( index >= 0 );

			if( m_ControlPoints.Find( index ) == m_ControlPoints.InvalidIndex())
			{
				DevMsg( 2, "**** Adding control point %s with index %d to control point master\n", pPoint->GetName(), index );
				m_ControlPoints.Insert( index, pPoint );
				numFound++;
			}
			else
			{
				Warning( "!!!!\nMultiple control points with the same index, duplicates ignored\n!!!!\n" );
				UTIL_Remove( pPoint );
			}
		}

		pEnt = gEntList.FindEntityByClassname( pEnt, GetControlPointName() );
	}

	if( numFound > MAX_CONTROL_POINTS )
	{
		Warning( "Too many control points! Max is %d\n", MAX_CONTROL_POINTS );
	}

	//Remap the indeces of the control points so they are 0-based
	//======================
	unsigned int j;

	bool bHandled[MAX_CONTROL_POINTS];
	memset( bHandled, 0, sizeof(bHandled) );

	unsigned int numPoints = m_ControlPoints.Count();
	unsigned int newIndex = 0;

	while( newIndex < numPoints )
	{
		//Find the lowest numbered, unhandled point
		int lowestIndex = -1;
		int lowestValue = 999;

		//find the lowest unhandled index
		for( j=0; j<numPoints; j++ )
		{
			if( !bHandled[j] && m_ControlPoints[j]->GetPointIndex() < lowestValue )
			{
				lowestIndex = j;
				lowestValue = m_ControlPoints[j]->GetPointIndex();
			}
		}

		//Don't examine this point again
		bHandled[lowestIndex] = true;

		//Give it its new index
		m_ControlPoints[lowestIndex]->SetPointIndex( newIndex );
		newIndex++;
	}
	
	if( m_ControlPoints.Count() == 0 ) 
	{
		Warning( "Error! No control points found in map!\n");
		return false;	
	}

	// Now setup the objective resource
	ObjectiveResource()->SetNumControlPoints( m_ControlPoints.Count() );
	for ( unsigned int i = 0; i < m_ControlPoints.Count(); i++ )
	{
		CTeamControlPoint *pPoint = m_ControlPoints[i];

		int iPointIndex = m_ControlPoints[i]->GetPointIndex();

		ObjectiveResource()->SetOwningTeam( iPointIndex, pPoint->GetOwner() );
		ObjectiveResource()->SetCPVisible( iPointIndex, pPoint->PointIsVisible() );
		ObjectiveResource()->SetCPPosition( iPointIndex, pPoint->GetAbsOrigin() );
		ObjectiveResource()->SetWarnOnCap( iPointIndex, pPoint->GetWarnOnCap() );
		ObjectiveResource()->SetWarnSound( iPointIndex, pPoint->GetWarnSound() );
		ObjectiveResource()->SetCPGroup( iPointIndex, pPoint->GetCPGroup() );
		for ( int team = 0; team < GetNumberOfTeams(); team++ )
		{
			ObjectiveResource()->SetCPIcons( iPointIndex, team, pPoint->GetHudIconIndexForTeam(team) );
			ObjectiveResource()->SetCPOverlays( iPointIndex, team, pPoint->GetHudOverlayIndexForTeam(team) );
			for ( int prevpoint = 0; prevpoint < MAX_PREVIOUS_POINTS; prevpoint++ )
			{
				ObjectiveResource()->SetPreviousPoint( iPointIndex, team, prevpoint, pPoint->GetPreviousPointForTeam(team, prevpoint) );
			}
		}
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::SetBaseControlPoints( void )
{
	for ( int team = 0; team < GetNumberOfTeams(); team++ )
	{
		ObjectiveResource()->SetTeamBaseIcons( team, m_iTeamBaseIcons[team] );
		ObjectiveResource()->SetBaseCP( GetBaseControlPoint(team), team );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::FindControlPointRounds( void )
{
	bool bFoundRounds = false;

	m_ControlPointRounds.RemoveAll();
	CBaseEntity *pEnt = gEntList.FindEntityByClassname( NULL, GetControlPointRoundName() );

	while( pEnt )
	{
		CTeamControlPointRound *pRound = assert_cast<CTeamControlPointRound *>( pEnt );

		if( pRound && ( m_ControlPointRounds.Find( pRound ) == m_ControlPointRounds.InvalidIndex() ) )
		{
			DevMsg( 2, "**** Adding control point round %s to control point master\n", pRound->GetEntityName().ToCStr() );
			m_ControlPointRounds.AddToHead( pRound );
		}

		pEnt = gEntList.FindEntityByClassname( pEnt, GetControlPointRoundName() );
	}

	if ( m_ControlPointRounds.Count() > 0 ) 
	{
		// sort them in our list by priority (highest priority first)
		m_ControlPointRounds.Sort( ControlPointRoundSort );
		bFoundRounds = true;
	}

	if ( g_pObjectiveResource )
	{
		g_pObjectiveResource->SetPlayingMiniRounds( bFoundRounds );
		g_pObjectiveResource->SetCapLayoutInHUD( STRING(m_iszCapLayoutInHUD) );
		g_pObjectiveResource->SetCapLayoutCustomPosition( m_flCustomPositionX, m_flCustomPositionY );
	}

	return bFoundRounds;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::IsInRound( CTeamControlPoint *pPoint )
{
	// are we playing a round and is this point in the round?
	if ( m_ControlPointRounds.Count() > 0 && m_iCurrentRoundIndex != -1 )
	{
		return m_ControlPointRounds[m_iCurrentRoundIndex]->IsControlPointInRound( pPoint );
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTeamControlPointMaster::NumPlayableControlPointRounds( void )
{
	int nRetVal = 0;

	for ( int i = 0 ; i < m_ControlPointRounds.Count() ; ++i )
	{
		CTeamControlPointRound *pRound = m_ControlPointRounds[i];

		if ( pRound )
		{
			if ( pRound->IsPlayable() )
			{
				// we found one that's playable
				nRetVal++;
			}
		}
	}

	return nRetVal;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::SelectSpecificRound( void )
{
	CTeamControlPointRound *pRound = NULL;
	CTeamplayRoundBasedRules *pRules = dynamic_cast<CTeamplayRoundBasedRules*>( GameRules() );

	if ( pRules )
	{
		if ( pRules->GetRoundToPlayNext() != NULL_STRING )
		{
			// do we have the name of a round?
			pRound = dynamic_cast<CTeamControlPointRound*>( gEntList.FindEntityByName( NULL, STRING( pRules->GetRoundToPlayNext() ) ) );

			if ( pRound )
			{
				if ( ( m_ControlPointRounds.Find( pRound )== m_ControlPointRounds.InvalidIndex() ) || 
					 ( !pRound->IsPlayable() && !pRound->MakePlayable() ) )
				{
					pRound = NULL;
				}
			}

			pRules->SetRoundToPlayNext( NULL_STRING );
		}
	}

	// do we have a round to play?
	if ( pRound )
	{
		m_iCurrentRoundIndex = m_ControlPointRounds.Find( pRound );
		m_ControlPointRounds[m_iCurrentRoundIndex]->SelectedToPlay();
		
		if ( pRules )
		{
			pRules->SetRoundOverlayDetails();
		}

		FireRoundStartOutput();
		DevMsg( 2, "**** Selected round %s to play\n", m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName().ToCStr() );

		if ( !pRules->IsInWaitingForPlayers() )
		{
			UTIL_LogPrintf( "World triggered \"Mini_Round_Selected\" (round \"%s\")\n", m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName().ToCStr() );
			UTIL_LogPrintf( "World triggered \"Mini_Round_Start\"\n" );
		}
		return true;
	}
	
	return false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::RegisterRoundBeingPlayed( void )
{
	// let the game rules know what round we're playing
	CTeamplayRoundBasedRules *pRules = dynamic_cast<CTeamplayRoundBasedRules*>( GameRules() );
	if ( pRules )
	{
		string_t iszEntityName = m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName();

		pRules->AddPlayedRound( iszEntityName );

		if ( m_bFirstRoundAfterRestart )
		{
			pRules->SetFirstRoundPlayed( iszEntityName );
			m_bFirstRoundAfterRestart = false;
		}
	}

	IGameEvent *event = gameeventmanager->CreateEvent( "teamplay_round_selected" );	
	if ( event )
	{
		event->SetString( "round", m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName().ToCStr() );
		gameeventmanager->FireEvent( event );
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::GetControlPointRoundToPlay( void )
{
	int i = 0;

	// are we trying to pick a specific round?
	if ( SelectSpecificRound() )
	{
		SetBaseControlPoints();
		RegisterRoundBeingPlayed();
		return true;
	}

	// rounds are sorted with the higher priority rounds first
	for ( i = 0 ; i < m_ControlPointRounds.Count() ; ++i )
	{
		CTeamControlPointRound *pRound = m_ControlPointRounds[i];

		if ( pRound )
		{
			if ( pRound->IsPlayable() )
			{
				// we found one that's playable
				break;
			}
		}
	}

	if ( i >= m_ControlPointRounds.Count() || m_ControlPointRounds[i] == NULL )
	{
		// we didn't find one to play
		m_iCurrentRoundIndex = -1;
		return false;
	}

    // we have a priority value, now we need to randomly pick a round with this priority that's playable
	int nPriority = m_ControlPointRounds[i]->GetPriorityValue();
	CUtlVector<int> nRounds;

	CTeamplayRoundBasedRules *pRules = dynamic_cast<CTeamplayRoundBasedRules*>( GameRules() );
	string_t iszLastRoundPlayed = pRules ? pRules->GetLastPlayedRound() : NULL_STRING;
	int iLastRoundPlayed = -1;

	string_t iszFirstRoundPlayed = pRules ? pRules->GetFirstRoundPlayed() : NULL_STRING;
	int iFirstRoundPlayed = -1; // after a full restart

	// loop through and find the rounds with this priority value
	for ( i = 0 ; i < m_ControlPointRounds.Count() ; ++i )
	{
		CTeamControlPointRound *pRound = m_ControlPointRounds[i];

		if ( pRound )
		{
			string_t iszRoundName = pRound->GetEntityName();

			if ( pRound->IsPlayable() && pRound->GetPriorityValue() == nPriority )
			{
				if ( iszLastRoundPlayed == iszRoundName ) // is this the last round we played?
				{
					iLastRoundPlayed = i;
				}

				if ( m_bFirstRoundAfterRestart )
				{
					// is this the first round we played after the last full restart?
					if ( ( iszFirstRoundPlayed != NULL_STRING ) && ( iszFirstRoundPlayed == iszRoundName ) )
					{
						iFirstRoundPlayed = i;
					}
				}

				nRounds.AddToHead(i);
			}
		}
	}

	if ( nRounds.Count() <= 0 )
	{
		// we didn't find one to play
		m_iCurrentRoundIndex = -1;
		return false;
	}

	// if we have more than one and the last played round is in our list, remove it
	if ( nRounds.Count() > 1 )
	{
		if ( iLastRoundPlayed != -1 )
		{
			int elementIndex = nRounds.Find( iLastRoundPlayed );
			nRounds.Remove( elementIndex );
		}
	}

	// if this is the first round after a full restart, we still have more than one round in our list, 
	// and the first played round (after the last full restart) is in our list, remove it
	if ( m_bFirstRoundAfterRestart )
	{
		if ( nRounds.Count() > 1 )
		{
			if ( iFirstRoundPlayed != -1 )
			{
				int elementIndex = nRounds.Find( iFirstRoundPlayed );
				nRounds.Remove( elementIndex );
			}
		}
	}

	// pick one to play but try to avoid picking one that we have recently played if there are other rounds to play
	int index = random->RandomInt( 0, nRounds.Count() - 1 );
	
	// only need to check this if we have more than one round with this priority value
	if ( pRules && nRounds.Count() > 1 )
	{
		// keep picking a round until we find one that's not a previously played round
		// or until we don't have any more rounds to choose from
		while ( pRules->IsPreviouslyPlayedRound( m_ControlPointRounds[ nRounds[ index ] ]->GetEntityName() ) && 
				nRounds.Count() > 1 )
		{
			nRounds.Remove( index ); // we have played this round recently so get it out of the list
			index = random->RandomInt( 0, nRounds.Count() - 1 );
		}
	}

	// pick one to play and fire its OnSelected output
	m_iCurrentRoundIndex = nRounds[ index ];
	m_ControlPointRounds[m_iCurrentRoundIndex]->SelectedToPlay();

	if ( pRules )
	{
		pRules->SetRoundOverlayDetails();
	}

	FireRoundStartOutput();
	DevMsg( 2, "**** Selected round %s to play\n", m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName().ToCStr() );

	if ( !pRules->IsInWaitingForPlayers() )
	{
		UTIL_LogPrintf( "World triggered \"Mini_Round_Selected\" (round \"%s\")\n", m_ControlPointRounds[m_iCurrentRoundIndex]->GetEntityName().ToCStr() );
		UTIL_LogPrintf( "World triggered \"Mini_Round_Start\"\n" );
	}

	SetBaseControlPoints();
	RegisterRoundBeingPlayed();

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Called every 0.1 seconds and checks the status of all the control points
// if one team owns them all, it gives points and resets
// Think also gives the time based points at the specified time intervals
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::CPMThink( void )
{
	if ( m_bDisabled || !TeamplayGameRules()->PointsMayBeCaptured() )
	{
		SetContextThink( &CTeamControlPointMaster::CPMThink, gpGlobals->curtime + 0.2, CPM_THINK );
		return;
	}

	// If we call this from team_control_point, this function should never 
	// trigger a win. but we'll leave it here just in case.
	CheckWinConditions();

	// the next time we 'think'
	SetContextThink( &CTeamControlPointMaster::CPMThink, gpGlobals->curtime + 0.2, CPM_THINK );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::CheckWinConditions( void )
{
	if ( m_bDisabled )
		return;

	if ( m_ControlPointRounds.Count() > 0 )
	{
		if ( m_iCurrentRoundIndex != -1 )
		{
			// Check the current round to see if one team is a winner yet
			int iWinners = m_ControlPointRounds[m_iCurrentRoundIndex]->CheckWinConditions();
			if ( iWinners != -1 && iWinners >= FIRST_GAME_TEAM )
			{
				bool bForceMapReset = ( NumPlayableControlPointRounds() == 0 ); // are there any more rounds to play?

				if ( !bForceMapReset )
				{
					// we have more rounds to play
					TeamplayGameRules()->SetWinningTeam( iWinners, WINREASON_ALL_POINTS_CAPTURED, bForceMapReset );
				}
				else
				{
					// we have played all of the available rounds
					TeamplayGameRules()->SetWinningTeam( iWinners, WINREASON_ALL_POINTS_CAPTURED, bForceMapReset, m_bSwitchTeamsOnWin );
				}

				FireTeamWinOutput( iWinners );
			}
		}
	}
	else
	{
		// Check that the points aren't all held by one team...if they are
		// this will reset the round and will reset all the points
		int iWinners = TeamOwnsAllPoints();
		if ( ( m_iInvalidCapWinner != 1 ) &&
			 ( iWinners >= FIRST_GAME_TEAM ) && 
			 ( iWinners != m_iInvalidCapWinner ) )
		{
			bool bWinner = true;

#if defined( TF_DLL)
			if ( TFGameRules() && TFGameRules()->IsInKothMode() )
			{
				CTeamRoundTimer *pTimer = NULL;
				if ( iWinners == TF_TEAM_RED )
				{
					pTimer = TFGameRules()->GetRedKothRoundTimer();
				}
				else if ( iWinners == TF_TEAM_BLUE )
				{
					pTimer = TFGameRules()->GetBlueKothRoundTimer();
				}

				if ( pTimer )
				{
					if ( pTimer->GetTimeRemaining() > 0 || TFGameRules()->TimerMayExpire() == false )
					{
						bWinner = false;
					}
				}
			}
#endif
			if ( bWinner )
			{
				TeamplayGameRules()->SetWinningTeam( iWinners, WINREASON_ALL_POINTS_CAPTURED, true, m_bSwitchTeamsOnWin );
				FireTeamWinOutput( iWinners );
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputSetWinner( inputdata_t &input )
{
	int iTeam = input.value.Int();
	InternalSetWinner( iTeam );
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputSetWinnerAndForceCaps( inputdata_t &input )
{
	int iTeam = input.value.Int();

	// Set all cap points in the current round to be owned by the winning team
	for ( unsigned int i = 0; i < m_ControlPoints.Count(); i++ )
	{
		CTeamControlPoint *pPoint = m_ControlPoints[i];
		if ( pPoint && (!PlayingMiniRounds() || ObjectiveResource()->IsInMiniRound(pPoint->GetPointIndex()) ) )
		{
			pPoint->ForceOwner( iTeam );
		}
	}

	InternalSetWinner( iTeam );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InternalSetWinner( int iTeam )
{
	bool bForceMapReset = true;

	if ( m_ControlPointRounds.Count() > 0 )
	{
		// if we're playing rounds and there are more to play, don't do a full reset
		bForceMapReset = ( NumPlayableControlPointRounds() == 0 );
	}

	if ( iTeam == TEAM_UNASSIGNED )
	{
		TeamplayGameRules()->SetStalemate( STALEMATE_TIMER, bForceMapReset );
	}
	else
	{
		if ( !bForceMapReset )
		{
			TeamplayGameRules()->SetWinningTeam( iTeam, WINREASON_ALL_POINTS_CAPTURED, bForceMapReset );
		}
		else
		{
			TeamplayGameRules()->SetWinningTeam( iTeam, WINREASON_ALL_POINTS_CAPTURED, bForceMapReset, m_bSwitchTeamsOnWin );
		}

		FireTeamWinOutput( iTeam );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::HandleRandomOwnerControlPoints( void )
{
	CUtlVector<CTeamControlPoint*> vecPoints; 
	CUtlVector<int> vecTeams; 

	int i = 0;

	// loop through and find all of the points that want random owners after a full restart
	for ( i = 0 ; i < (int)m_ControlPoints.Count() ; i++ )
	{
		CTeamControlPoint *pPoint = m_ControlPoints[i];

		if ( pPoint && pPoint->RandomOwnerOnRestart() )
		{
			vecPoints.AddToHead( pPoint );
			vecTeams.AddToHead( pPoint->GetTeamNumber() );
		}
	}

	// now loop through and mix up the owners (if we found any points with this flag set)
	for ( i = 0 ; i < vecPoints.Count() ; i++ )
	{
		CTeamControlPoint *pPoint = vecPoints[i];

		if ( pPoint )
		{
			int index = random->RandomInt( 0, vecTeams.Count() - 1 );
			pPoint->ForceOwner( vecTeams[index] );

			vecTeams.Remove( index );
		}
	}

	vecPoints.RemoveAll();
	vecTeams.RemoveAll();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputRoundSpawn( inputdata_t &input )
{
	//clear out old control points
	m_ControlPoints.RemoveAll();

	//find the control points, and if successful, do CPMThink
	if ( FindControlPoints() )
	{
/*		if ( m_bFirstRoundAfterRestart )
		{
			CTeamplayRoundBasedRules *pRules = dynamic_cast<CTeamplayRoundBasedRules*>( GameRules() );
			if ( pRules && ( pRules->GetRoundToPlayNext() == NULL_STRING ) )
			{
				// we only want to handle the random points if we don't have a specific round to play next
				// (prevents points being randomized again after "waiting for players" has finished and we're going to play the same round)
				HandleRandomOwnerControlPoints();
			}
		}
*/
		SetContextThink( &CTeamControlPointMaster::CPMThink, gpGlobals->curtime + 0.1, CPM_THINK );
	}

	// clear out the old rounds
	m_ControlPointRounds.RemoveAll();

	// find the rounds (if the map has any)
	FindControlPointRounds();

	SetBaseControlPoints();
	
	ObjectiveResource()->ResetControlPoints();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputRoundActivate( inputdata_t &input )
{
	// if we're using mini-rounds and haven't picked one yet, find one to play
	if ( PlayingMiniRounds() && GetCurrentRound() == NULL )
	{
		GetControlPointRoundToPlay();
	}

	if ( PlayingMiniRounds() )
	{
		// Tell the objective resource what control points are in use in the selected mini-round
		CTeamControlPointRound *pRound = GetCurrentRound();
		if ( pRound )
		{
			for ( unsigned int i = 0; i < m_ControlPoints.Count(); i++ )
			{
				CTeamControlPoint *pPoint = m_ControlPoints[i];
				if ( pPoint )
				{
					ObjectiveResource()->SetInMiniRound( pPoint->GetPointIndex(), pRound->IsControlPointInRound( pPoint ) );
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputSetCapLayout( inputdata_t &inputdata )
{
	m_iszCapLayoutInHUD = inputdata.value.StringID();
	g_pObjectiveResource->SetCapLayoutInHUD( STRING(m_iszCapLayoutInHUD) );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputSetCapLayoutCustomPositionX( inputdata_t &inputdata )
{
	m_flCustomPositionX = inputdata.value.Float();
	g_pObjectiveResource->SetCapLayoutCustomPosition( m_flCustomPositionX, m_flCustomPositionY );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputSetCapLayoutCustomPositionY( inputdata_t &inputdata )
{
	m_flCustomPositionY = inputdata.value.Float();
	g_pObjectiveResource->SetCapLayoutCustomPosition( m_flCustomPositionX, m_flCustomPositionY );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::FireTeamWinOutput( int iWinningTeam )
{
	// Remap team so that first game team = 1
	switch( iWinningTeam - FIRST_GAME_TEAM+1 )
	{
	case 1:
		m_OnWonByTeam1.FireOutput(this,this);
		break;
	case 2:
		m_OnWonByTeam2.FireOutput(this,this);
		break;
	default:
		Assert(0);
		break;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::FireRoundStartOutput( void )
{
	CTeamControlPointRound *pRound = GetCurrentRound();

	if ( pRound )
	{
		pRound->FireOnStartOutput();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::FireRoundEndOutput( void )
{
	CTeamControlPointRound *pRound = GetCurrentRound();

	if ( pRound )
	{
		pRound->FireOnEndOutput();
		m_iCurrentRoundIndex = -1;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CTeamControlPointMaster::PointLastContestedAt( int point )
{
	CTeamControlPoint *pPoint = GetControlPoint(point);
	if ( pPoint )
		return pPoint->LastContestedAt();

	return -1;
}

//-----------------------------------------------------------------------------
// Purpose: This function returns the team that owns all the cap points. 
//			If its not the case that one team owns them all, it returns 0.
//			CPs are broken into groups. A team can win by owning all flags within a single group.
//			
//			Can be passed an overriding team. If this is not null, the passed team
//			number will be used for that cp. Used to predict if that CP changing would
//			win the game.
//-----------------------------------------------------------------------------
int CTeamControlPointMaster::TeamOwnsAllPoints( CTeamControlPoint *pOverridePoint /* = NULL */, int iOverrideNewTeam /* = TEAM_UNASSIGNED */ )
{
	unsigned int i;

	int iWinningTeam[MAX_CONTROL_POINT_GROUPS];

	for( i=0;i<MAX_CONTROL_POINT_GROUPS;i++	)
	{
		iWinningTeam[i] = TEAM_INVALID;
	}

	// if TEAM_INVALID, haven't found a flag for this group yet
	// if TEAM_UNASSIGNED, the group is still being contested 

	// for each control point
	for( i=0;i<m_ControlPoints.Count();i++ )
	{
		int group = m_ControlPoints[i]->GetCPGroup();
		int owner = m_ControlPoints[i]->GetOwner();

		if ( pOverridePoint == m_ControlPoints[i] )
		{
			owner = iOverrideNewTeam;
		}

		// the first one we find in this group, set the win to true
		if ( iWinningTeam[group] == TEAM_INVALID )
		{
			iWinningTeam[group] = owner;
		}
		// unassigned means this group is already contested, move on
		else if ( iWinningTeam[group] == TEAM_UNASSIGNED )
		{
			continue;
		}
		// if we find another one in the group that isn't the same owner, set the win to false
		else if ( owner != iWinningTeam[group] )
		{
			iWinningTeam[group] = TEAM_UNASSIGNED;
		}		
	}

	// report the first win we find as the winner
	for ( i=0;i<MAX_CONTROL_POINT_GROUPS;i++ )
	{
		if ( iWinningTeam[i] >= FIRST_GAME_TEAM )
			return iWinningTeam[i];
	}

	// no wins yet
	return TEAM_UNASSIGNED;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::WouldNewCPOwnerWinGame( CTeamControlPoint *pPoint, int iNewOwner )
{
	return ( TeamOwnsAllPoints( pPoint, iNewOwner ) == iNewOwner );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTeamControlPointMaster::IsBaseControlPoint( int iPointIndex )
{
	bool retVal = false;

	for ( int iTeam = LAST_SHARED_TEAM+1; iTeam < GetNumberOfTeams(); iTeam++ )
	{
		if ( GetBaseControlPoint( iTeam ) == iPointIndex )
		{
			retVal = true;
			break;
		}
	}

	return retVal;
}

//-----------------------------------------------------------------------------
// Purpose: Get the control point for the specified team that's at their end of
//			the control point chain.
//-----------------------------------------------------------------------------
int	CTeamControlPointMaster::GetBaseControlPoint( int iTeam )
{
	int iRetVal = -1;
	int nLowestValue = 999, nHighestValue = -1;
	int iLowestIndex = 0, iHighestIndex = 0;

	for( int i = 0 ; i < (int)m_ControlPoints.Count() ; i++ )
	{
		CTeamControlPoint *pPoint = m_ControlPoints[i];

		int iPointIndex = m_ControlPoints[i]->GetPointIndex();

		if ( PlayingMiniRounds() && iTeam > LAST_SHARED_TEAM )
		{
			if ( IsInRound( pPoint ) ) // is this point in the current round?
			{
				if ( iPointIndex > nHighestValue )
				{
					nHighestValue = iPointIndex;
					iHighestIndex = i;
				}

				if ( iPointIndex < nLowestValue )
				{
					nLowestValue = iPointIndex;
					iLowestIndex = i;
				}
			}
		}
		else
		{
			if ( pPoint->GetDefaultOwner() != iTeam )
			{
				continue;
			}

			// If it's the first or the last point, it's their base
			if ( iPointIndex == 0 || iPointIndex == (((int)m_ControlPoints.Count())-1) )
			{
				iRetVal = iPointIndex;
				break;
			}
		}
	}

	if ( PlayingMiniRounds() && iTeam > LAST_SHARED_TEAM )
	{
		if ( nLowestValue != 999 && nHighestValue != -1 )
		{
			CTeamControlPoint *pLowestPoint = m_ControlPoints[iLowestIndex];
			CTeamControlPoint *pHighestPoint = m_ControlPoints[iHighestIndex];

			// which point is owned by this team?
			if ( ( pLowestPoint->GetDefaultOwner() == iTeam && pHighestPoint->GetDefaultOwner() == iTeam ) || // if the same team owns both, take the highest value to be the last point
				 ( pHighestPoint->GetDefaultOwner() == iTeam ) )
			{
				iRetVal = nHighestValue;
			}
			else if ( pLowestPoint->GetDefaultOwner() == iTeam )
			{
				iRetVal = nLowestValue;
			}
		}
	}
	
	return iRetVal;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputEnable( inputdata_t &input )
{ 
	m_bDisabled = false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::InputDisable( inputdata_t &input )
{ 
	m_bDisabled = true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------	
int CTeamControlPointMaster::GetNumPointsOwnedByTeam( int iTeam )
{
	int nCount = 0;

	for( int i = 0 ; i < (int)m_ControlPoints.Count() ; i++ )
	{
		CTeamControlPoint *pPoint = m_ControlPoints[i];

		if ( pPoint && ( pPoint->GetTeamNumber() == iTeam ) )
		{
			nCount++;
		}
	}

	return nCount;
}

//-----------------------------------------------------------------------------
// Purpose: returns how many more mini-rounds it will take for specified team
//			to win, if they keep winning every mini-round
//-----------------------------------------------------------------------------	
int CTeamControlPointMaster::CalcNumRoundsRemaining( int iTeam )
{
	// To determine how many rounds remain for a given team if it consistently wins mini-rounds, we have to 
	// simulate forward each mini-round and track the control point ownership that would result

	// vector of control points the team owns in our forward-simulation
	CUtlVector<CTeamControlPoint *> vecControlPointsOwned;
	
	// start with all the control points the team currently owns 
	FOR_EACH_MAP_FAST( m_ControlPoints, iControlPoint )
	{
		if ( m_ControlPoints[iControlPoint]->GetOwner() == iTeam )
		{
			vecControlPointsOwned.AddToTail( m_ControlPoints[iControlPoint] );
		}
	}

	int iRoundsRemaining = 0;

	// keep simulating what will happen next if this team keeps winning, until
	// it owns all the control points in the map 
	while ( vecControlPointsOwned.Count() < (int) m_ControlPoints.Count() )
	{
		iRoundsRemaining++;

		// choose the next highest-priority round that is playable 
		for ( int i = 0 ; i < m_ControlPointRounds.Count() ; ++i )
		{
			CTeamControlPointRound *pRound = m_ControlPointRounds[i];
			if ( !pRound )
				continue;

			// see if one team owns all control points in this round
			int iRoundOwningTeam = TEAM_INVALID;
			int iControlPoint;
			for ( iControlPoint = 0; iControlPoint < pRound->m_ControlPoints.Count(); iControlPoint++ )
			{
				CTeamControlPoint *pControlPoint = pRound->m_ControlPoints[iControlPoint];
				int iControlPointOwningTeam = TEAM_INVALID;

				// determine who owns this control point.
				// First, check our simulated ownership
				if ( vecControlPointsOwned.InvalidIndex() != vecControlPointsOwned.Find( pControlPoint ) )
				{
					// This team has won this control point in forward simulation
					iControlPointOwningTeam = iTeam;
				}
				else
				{
					// use actual control point ownership
					iControlPointOwningTeam = pControlPoint->GetOwner();
				}
				
				if ( 0 == iControlPoint )
				{
					// if this is the first control point, assign ownership to the team that owns this control point
					iRoundOwningTeam = iControlPointOwningTeam;
				}
				else
				{
					// for all other control points, if the control point ownership does not match other control points, reset
					// round ownership to no team
					if ( iRoundOwningTeam != iControlPointOwningTeam )
					{
						iRoundOwningTeam = TEAM_INVALID;
					}
				}
			}
			// this round is playable if all control points are not owned by one team (or owned by a team that can't win by capping them)
			bool bPlayable = ( ( iRoundOwningTeam < FIRST_GAME_TEAM ) || ( pRound->GetInvalidCapWinner() == 1 ) || ( iRoundOwningTeam == pRound->GetInvalidCapWinner() ) );
			if ( !bPlayable )
				continue;

			// Pretend this team played and won this round.  It now owns all control points from this round.  Add all the
			// control points from this round that are not already own the owned list to the owned list
			int iNewControlPointsOwned = 0;
			FOR_EACH_VEC( pRound->m_ControlPoints, iControlPoint )
			{
				CTeamControlPoint *pControlPoint = pRound->m_ControlPoints[iControlPoint];
				if ( vecControlPointsOwned.InvalidIndex() == vecControlPointsOwned.Find( pControlPoint ) )
				{
					vecControlPointsOwned.AddToTail( pControlPoint );
					iNewControlPointsOwned++;
				}
			}
			// sanity check: team being simulated should be owning at least one more new control point per round, or they're not making progress
			Assert( iNewControlPointsOwned > 0 );	

			// now go back and pick the next playable round (if any) given the control points this team now owns,
			// repeat until all control points are owned.  The number of iterations it takes is the # of rounds remaining
			// for this team to win.
			break;
		}				
	}

	return iRoundsRemaining;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CTeamControlPointMaster::GetPartialCapturePointRate( void )
{
	return m_flPartialCapturePointsRate;
}

/*
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamControlPointMaster::ListRounds( void )
{
	if ( PlayingMiniRounds() )
	{
		ConMsg( "Rounds in this map:\n\n" );

		for ( int i = 0; i < m_ControlPointRounds.Count() ; ++i )
		{
			CTeamControlPointRound* pRound = m_ControlPointRounds[i];

			if ( pRound )
			{
				const char *pszName = STRING( pRound->GetEntityName() );
				ConMsg( "%s\n", pszName );
			}
		}
	}
	else
	{
		ConMsg( "* No rounds in this map *\n" );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------	
void cc_ListRounds( void )
{
	CTeamControlPointMaster *pMaster = g_hControlPointMasters.Count() ? g_hControlPointMasters[0] : NULL;
	if ( pMaster )
	{
		pMaster->ListRounds();
	}
}

static ConCommand listrounds( "listrounds", cc_ListRounds, "List the rounds for the current map", FCVAR_CHEAT );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------	
void cc_PlayRound( const CCommand& args )
{
	if ( args.ArgC() > 1 )
	{
		CTeamplayRoundBasedRules *pRules = dynamic_cast<CTeamplayRoundBasedRules*>( GameRules() );
		CTeamControlPointMaster *pMaster = g_hControlPointMasters.Count() ? g_hControlPointMasters[0] : NULL;

		if ( pRules && pMaster )
		{
			if ( pMaster->PlayingMiniRounds() )
			{
				// did we get the name of a round?
				CTeamControlPointRound *pRound = dynamic_cast<CTeamControlPointRound*>( gEntList.FindEntityByName( NULL, args[1] ) );

				if ( pRound )
				{
					pRules->SetRoundToPlayNext( pRound->GetEntityName() );
					mp_restartgame.SetValue( 5 );
				}
				else
				{
					ConMsg( "* Round \"%s\" not found in this map *\n", args[1] );
				}
			}
		}
	}
	else
	{
		ConMsg( "Usage:  playround < round name >\n" );
	}
}

static ConCommand playround( "playround", cc_PlayRound, "Play the selected round\n\tArgument: {round name given by \"listrounds\" command}", FCVAR_CHEAT );
*/