summaryrefslogtreecommitdiff
path: root/game/server/tf2/tf_team.cpp
blob: 0bec38054993b3cf03468fb8a1cfa8e2d6f7ff58 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Team management class. Contains all the details for a specific team
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "team.h"
#include "tf_team.h"
#include "tf_func_resource.h"
#include "tf_player.h"
#include "techtree.h"
#include "tf_obj.h"
#include "tf_obj_resupply.h"
#include "orders.h"
#include "entitylist.h"
#include "team_spawnpoint.h"
#include "team_messages.h"
#include "tf_obj_powerpack.h"
#include "tf_gamerules.h"
#include "engine/IEngineSound.h"
#include "tier1/strtools.h"
#include "tf_stats.h"
#include "tf_obj_buff_station.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define OBJECT_COVERED_DIST	1000
#define RESOURCE_GIVE_TIME 30
#define RESOURCE_GIVE_AMOUNT 150
#define RESOURCE_DONATION_AMT_PER_PLAYER 10

bool IsEntityVisibleToTactical( int iLocalTeamNumber, int iLocalTeamPlayers, 
	int iLocalTeamObjects, int iEntIndex, const char *pEntName, int pEntTeamNumber, const Vector &pEntOrigin );
extern ConVar tf_destroyobjects;

//-----------------------------------------------------------------------------
// Purpose: SendProxy that converts the UtlVector list of radar scanners to entindexes, where it's reassembled on the client
//-----------------------------------------------------------------------------
void SendProxy_ObjectList( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID )
{
	CTFTeam *pTeam = (CTFTeam*)pData;

	// If this fails, then SendProxyArrayLength_TeamObjects didn't work.
	Assert( iElement < pTeam->GetNumObjects() );

	CBaseObject *pObject = pTeam->GetObject(iElement);
	EHANDLE hObject;
	hObject = pObject;

	SendProxy_EHandleToInt( pProp, pStruct, &hObject, pOut, iElement, objectID );
}


int SendProxyArrayLength_TeamObjects( const void *pStruct, int objectID )
{
	CTFTeam *pTeam = (CTFTeam*)pStruct;
	int iObjects = pTeam->GetNumObjects();
	Assert( iObjects < MAX_OBJECTS_PER_TEAM );
	return iObjects;
}


// Datatable
IMPLEMENT_SERVERCLASS_ST(CTFTeam, DT_TFTeam)
	SendPropFloat( SENDINFO(m_fResources), 16, SPROP_NOSCALE ),
	SendPropFloat( SENDINFO(m_fPotentialResources), 16, SPROP_NOSCALE ),
	SendPropInt( SENDINFO(m_bHaveZone), 1, SPROP_UNSIGNED ), 
	
	SendPropArray2( 
		SendProxyArrayLength_TeamObjects,
		SendPropInt("object_array_element", 0, SIZEOF_IGNORE, NUM_NETWORKED_EHANDLE_BITS, SPROP_UNSIGNED, SendProxy_ObjectList), 
		MAX_OBJECTS_PER_TEAM, 
		0, 
		"object_array"
		 )
END_SEND_TABLE()

LINK_ENTITY_TO_CLASS( tf_team_manager, CTFTeam );

//-----------------------------------------------------------------------------
// Purpose: Get a pointer to the specified TF team manager
//-----------------------------------------------------------------------------
CTFTeam *GetGlobalTFTeam( int iIndex )
{
	return (CTFTeam*)GetGlobalTeam( iIndex );
}


//-----------------------------------------------------------------------------
// Purpose: Needed because this is an entity, but should never be used
//-----------------------------------------------------------------------------
void CTFTeam::Init( const char *pName, int iNumber )
{
	BaseClass::Init( pName, iNumber );

	InitializeTeamResources();
	InitializeTechTree();
	InitializeOrders();
	ClearMessages();

	m_flNextResourceTime = 0;

	// Only detect changes every half-second.
	NetworkProp()->SetUpdateInterval( 0.75f );

	m_flTotalResourcesSoFar = m_iLastUpdateSentAt = 0;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFTeam::~CTFTeam( void )
{
	m_aResourcesBeingCollected.Purge();
	m_aResupplyBeacons.Purge();
	m_aObjects.Purge();
	m_aOrders.Purge();

	delete m_pTechnologyTree;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::Precache( void )
{
	// Precache all the technologies in the techtree
	for ( int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		PrecacheTechnology( m_pTechnologyTree->GetTechnology(i) );
	}

	PrecacheScriptSound( "TFTeam.CapturedZone" );
	PrecacheScriptSound( "TFTeam.LostZone" );
	PrecacheScriptSound( "TFTeam.ObtainStolenTechnology" );
	PrecacheScriptSound( "TFTeam.BoughtPreferredTechnology" );
	PrecacheScriptSound( "TFTeam.AddOrder" );
}


//-----------------------------------------------------------------------------
// Purpose: Precache a technology's files
//-----------------------------------------------------------------------------
void CTFTeam::PrecacheTechnology( CBaseTechnology *pTech )
{
	// Precache sounds for every class result
	for (int i = 0; i < TFCLASS_CLASS_COUNT; i++ )
	{
		if ( pTech->GetSoundFile(i) && (pTech->GetSoundFile(i)[0] != 0) )
		{
			PrecacheScriptSound( pTech->GetSoundFile(i) );
			pTech->SetClassResultSound( i, 0 );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called every frame
//-----------------------------------------------------------------------------
void CTFTeam::Think( void )
{
	UpdateOrders();
	UpdateMessages();

	// FIXME: Try this out?
	/*
	// Give resources to the team at regular intervals
	if (gpGlobals->curtime >= m_flNextResourceTime)
	{
		AddTeamResources( RESOURCE_GIVE_AMOUNT );
		m_flNextResourceTime = gpGlobals->curtime + RESOURCE_GIVE_TIME;
	}
	*/

	UpdateTechnologies();
		    
	/* FIXME: Re-enable once we figure out what the correct orders should be
	// Create new personal orders
	if ( m_flPersonalOrderUpdateTime < gpGlobals->curtime )
	{
		CreatePersonalOrders();
		m_flPersonalOrderUpdateTime = gpGlobals->curtime + PERSONAL_ORDER_UPDATE_TIME;
	}
	*/
}

//-----------------------------------------------------------------------------
// DATA HANDLING
//-----------------------------------------------------------------------------
// Purpose: Check to see if we should resend the entire tech tree to a player on Hud reinitialisation, which happens every player respawn
//-----------------------------------------------------------------------------
void CTFTeam::UpdateClientData( CBasePlayer *pPlayer )
{
	CBaseTFPlayer *pTFPlayer = (CBaseTFPlayer *)pPlayer;
	// If we're initialising the hud, update all technologies
	if ( pTFPlayer->HUDNeedsRestart() )
	{
		// Check all the technologies and resend any that differ from this client's representation
		for ( int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
		{
			// Update all technologies
			CBaseTechnology *technology = m_pTechnologyTree->GetTechnology(i);
			if ( technology )
			{
				// Check to see if any resource levels have changed
				if ( pTFPlayer->AvailableTech(i).m_nResourceLevel != technology->GetResourceLevel() )
				{
					UpdateClientTechnology( i, pTFPlayer );
					continue;
				}

				if ( technology->GetAvailable() != pTFPlayer->AvailableTech(i).m_nAvailable )
				{
					UpdateClientTechnology( i, pTFPlayer );
					continue;
				}

				byte pcount = technology->GetPreferenceCount();
				if ( pTFPlayer->GetPreferredTechnology() == i )
				{
					pcount |= 0x80;
				}

				if ( pcount != pTFPlayer->AvailableTech(i).m_nUserCount )
				{
					UpdateClientTechnology( i, pTFPlayer );
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Update a technology for a player
//-----------------------------------------------------------------------------
void CTFTeam::UpdateClientTechnology( int iTechID, CBaseTFPlayer *pPlayer )
{
	CBaseTechnology *pTechnology = m_pTechnologyTree->GetTechnology( iTechID );
	if ( !pTechnology )
		return;

	byte pcount = pTechnology->GetPreferenceCount();

	if ( pPlayer->GetPreferredTechnology() == iTechID )
	{
		pcount |= 0x80;
	}

	CSingleUserRecipientFilter user( pPlayer );
	user.MakeReliable();

	// Update this technology
	UserMessageBegin( user, "Technology" );
		WRITE_BYTE( iTechID );
		WRITE_BYTE( pTechnology->GetAvailable() );
		WRITE_BYTE( pcount );
		WRITE_SHORT( (short)pTechnology->GetResourceLevel() );
	MessageEnd();

	// Update the player's client tech representation
	pPlayer->AvailableTech(iTechID).m_nAvailable = pTechnology->GetAvailable();
	pPlayer->AvailableTech(iTechID).m_nUserCount = pcount;
	pPlayer->AvailableTech(iTechID).m_nResourceLevel = pTechnology->GetResourceLevel();

	/*
	Msg( "Sent %s(%d) to %s:\n", pTechnology->GetName(), iTechID, pPlayer->GetPlayerName() );
	Msg( "   Available: %d\n", pTechnology->GetAvailable() );
	Msg( "   PrefCount: %d\n", pTechnology->GetPreferenceCount() );
	Msg( "   Level    : %0.2f\n", pTechnology->GetResourceLevel() );
	*/
}

//-----------------------------------------------------------------------------
// Purpose: Check to see if any technology has changed, and resend it to players if it has
//-----------------------------------------------------------------------------
void CTFTeam::UpdateTechnologyData( void )
{
	for ( int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		// Update all technologies
		CBaseTechnology *pTechnology = m_pTechnologyTree->GetTechnology(i);
		if ( pTechnology && pTechnology->IsDirty() )
		{
			// Send it to all our clients
			for ( int iPlayer = 0; iPlayer < m_aPlayers.Count(); iPlayer++ )
			{
				CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[iPlayer];
				UpdateClientTechnology( i, pPlayer );
			}

			pTechnology->SetDirty( false );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFTeam::ShouldTransmitToPlayer( CBasePlayer* pRecipient, CBaseEntity* pEntity )
{
	return IsEntityVisibleToTactical( pEntity );
}

//-----------------------------------------------------------------------------
// Purpose: Is the specified entity visible on this team's tactical view?
//-----------------------------------------------------------------------------
bool CTFTeam::IsEntityVisibleToTactical( CBaseEntity *pEntity )
{
	return ::IsEntityVisibleToTactical( GetTeamNumber(), GetNumPlayers(), 
		GetNumObjects(), pEntity->entindex(), (char*)STRING(pEntity->m_iClassname), 
		pEntity->GetTeamNumber(), pEntity->GetAbsOrigin() );
}

//-----------------------------------------------------------------------------
// RESOURCES
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Add a resource zone to the list of zones to collect from
//-----------------------------------------------------------------------------
void CTFTeam::AddResourceZone( CResourceZone *pResource )
{
	TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::AddResourceZone adding res zone %p to team %s\n", gpGlobals->curtime, 
		pResource, GetName() ) );

	// If this resource is already owned by another team, remove it from them
	CTFTeam *pOwners = pResource->GetOwningTeam();
	if ( pOwners )
	{
		pOwners->RemoveResourceZone( pResource );
	}

	pResource->SetOwningTeam( GetTeamNumber() );

	m_aResourcesBeingCollected.AddToTail( pResource );
	m_bHaveZone = true;

	// Tell all the team's members
	for ( int i = 0; i < m_aPlayers.Count(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[i];
		CSingleUserRecipientFilter filter( pPlayer );
		filter.MakeReliable();
		CBaseEntity::EmitSound( filter, pPlayer->entindex(), "TFTeam.CapturedZone" );
	}

	// Recalculate team's orders
	RecalcOrders();
}

//-----------------------------------------------------------------------------
// Purpose: Remove a resource zone from the list of zones being collected from
//-----------------------------------------------------------------------------
void CTFTeam::RemoveResourceZone( CResourceZone *pResource )
{
	TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::RemoveResourceZone removing res zone %p from team %s\n", gpGlobals->curtime, 
		pResource, GetName() ) );

	// Now remove the zone from our list
	m_aResourcesBeingCollected.FindAndRemove( pResource );

	// Still have a zone if there are other zones in the list
	m_bHaveZone = ( m_aResourcesBeingCollected.Count() > 0 );

	// Tell all the team's members
	for ( int i = 0; i < m_aPlayers.Count(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[i];
		CSingleUserRecipientFilter filter( pPlayer );
		filter.MakeReliable();
		CBaseEntity::EmitSound( filter, pPlayer->entindex(),"TFTeam.LostZone" );
	}

	// Recalculate team's orders
	RecalcOrders();
}

//-----------------------------------------------------------------------------
// Purpose: Recalculate the potential resources
//-----------------------------------------------------------------------------
void CTFTeam::UpdatePotentialResources( void )
{
	// Set potential to current amount
	m_fPotentialResources = GetTeamResources();

	// This used to be used for collectors.
	// It could be updated to count all incoming resources in en-route resource boxes.
}

//-----------------------------------------------------------------------------
// Purpose: Return the amount of resources a player should get when joining this team
//-----------------------------------------------------------------------------
float CTFTeam::GetJoiningPlayerResources( void )
{
	// If we had our banks set recently, use that amount
	if ( gpGlobals->curtime < (m_flLastBankSetTime + 30.0) )
		return m_flLastBankSetAmount;

	if ( !GetNumPlayers() )
		return 0;

	// Otherwise, take the average of all the players on the team
	RecomputeTeamResources();
	return ( GetTeamResources() / GetNumPlayers() );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::SetRecentBankSet( float flResources )
{
	m_flLastBankSetAmount = flResources;
	m_flLastBankSetTime = gpGlobals->curtime;
}

//------------------------------------------------------------------------------------------------------------------
// TECHNOLOGY TREE
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::InitializeTechTree( void )
{
	m_pTechnologyTree = new CTechnologyTree( filesystem, GetTeamNumber() );

	// Now iterate through added techs and automatically make level 0 techs available
	for (int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		CBaseTechnology *tech = m_pTechnologyTree->GetTechnology(i);
		if ( !tech )
			continue;

		if ( tech->GetLevel() == 0 )
		{
			EnableTechnology( tech );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTechnologyTree *CTFTeam::GetTechnologyTree( void )
{
	return m_pTechnologyTree;
}

//-----------------------------------------------------------------------------
// Purpose: A new technology has been attained by this team. Give it to every player.
//-----------------------------------------------------------------------------
void CTFTeam::EnableTechnology( CBaseTechnology *technology, bool bStolen )
{
	CTeamFortress *rules = TFGameRules();
	if ( rules )
	{
		// Disable autoswitching if we are getting a weapon
		rules->SetAllowWeaponSwitch( false );
	}

	// Set the technology's resources to the costs
	// Needed because technologies can be enabled through other means than resource spending
	technology->ForceComplete();

	// Apply technology to team first.
	technology->AddTechnologyToTeam( this );

	// Iterate though players
	for (int i = 0; i < m_aPlayers.Count(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[i];
		technology->AddTechnologyToPlayer( pPlayer );

		CSingleUserRecipientFilter filter( pPlayer );
		filter.MakeReliable();

		// Play the sound
		if (bStolen)
		{
			CBaseEntity::EmitSound( filter, pPlayer->entindex(), "TFTeam.ObtainStolenTechnology" );
		}
		else
		{
			if ( technology->GetSoundFile(0) && technology->GetSoundFile(0)[0] )
			{
				EmitSound_t ep;
				ep.m_nChannel = CHAN_STATIC;
				ep.m_pSoundName = technology->GetSoundFile(0);

				CBaseEntity::EmitSound( filter, pPlayer->entindex(), ep );
			}
		}

		// Remove all the player's votes on this technology
		CBaseTechnology *pPreferredTech = m_pTechnologyTree->GetTechnology( pPlayer->GetPreferredTechnology() );
		if ( pPreferredTech && pPreferredTech == technology )
		{
			// Tell the player his preferred tech has been bought
			if (!bStolen)
			{
				CBaseEntity::EmitSound( filter, pPlayer->entindex(), "TFTeam.BoughtPreferredTechnology" );
			}

			pPlayer->SetPreferredTechnology( m_pTechnologyTree, -1 );
		}
	}

	// Let the team see if it wants to do anything with this specific technology
	GainedNewTechnology( technology );

	// Reenable autoswitching
	if ( rules )
	{
		rules->SetAllowWeaponSwitch( true );
	}
}

//-----------------------------------------------------------------------------
// Purpose: For debugging..
//-----------------------------------------------------------------------------
void CTFTeam::EnableAllTechnologies()
{
	for ( int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		CBaseTechnology *technology = m_pTechnologyTree->GetTechnology(i);
		if ( !technology || technology->IsHidden() )
			continue;

		EnableTechnology( technology );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called any time a player votes/changes preferences on the client
//-----------------------------------------------------------------------------
void CTFTeam::RecomputePreferences( void )
{
	// Zero total counters
	m_pTechnologyTree->ClearPreferenceCount();

	// Zero out all preferences and iterate through active players
	int i;
	for ( i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		CBaseTechnology *technology = m_pTechnologyTree->GetTechnology(i);
		if ( technology )
		{
			// Zero internal counters
			technology->ZeroPreferences();
		}
	}

	// Now loop through players and see what's preferred
	for ( i = 0; i < m_aPlayers.Count(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[i];
		if ( !pPlayer )
			continue;
	
		int preferred =  pPlayer->GetPreferredTechnology();
		// No preference set, don't worry about this player
		if ( preferred == -1 )
			continue;

		if ( preferred < 0 || preferred >= MAX_TECHNOLOGIES )
		{
			Msg( "Player %s tried to set preference to out of range tech %i\n", preferred );
			continue;
		}

		// Reference technology
		CBaseTechnology *technology = m_pTechnologyTree->GetTechnology(preferred);
		Assert( technology );
		if ( !technology )
			continue;

		// Msg( "player %s prefers %s\n", pPlayer->GetPlayerName(), technology->GetPrintName() );

		// Add one vote
		technology->IncrementPreferences();
		// Add one vote to totals
		m_pTechnologyTree->IncrementPreferences();
	}

	// Any time preferences are changed/set, see if we should make any purchases immediately.
	RecomputePurchases();
}

//-----------------------------------------------------------------------------
// Purpose: Figure our how many resources we've got in the team
//-----------------------------------------------------------------------------
void CTFTeam::RecomputeTeamResources( void )
{
	// Recalculate the total amount of resources the team has
	m_fResources = 0.0f;
	for ( int i = 0; i < GetNumPlayers(); i++ )
	{
		m_fResources += ((CBaseTFPlayer*)GetPlayer(i))->GetBankResources();
	}

	UpdatePotentialResources();
}	

//-----------------------------------------------------------------------------
// Purpose: Attempt to spend resources according to player's preferences
//-----------------------------------------------------------------------------
void CTFTeam::RecomputePurchases( void )
{
	RecomputeTeamResources();

	// Cycle through all players, and spend their resources on the technologies they're voting for
	for ( int iPlayer = 0; iPlayer < m_aPlayers.Count(); iPlayer++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)GetPlayer( iPlayer );

		// See if he has any resources to spend on a tech
		if ( pPlayer->GetBankResources() <= 0 )
			continue;

		// Has he got a preffered tech?
		if ( pPlayer->GetPreferredTechnology() != -1 )
		{
			// Get the player's voted-for technology
			CBaseTechnology *pPreferredTech = m_pTechnologyTree->GetTechnology( pPlayer->GetPreferredTechnology() );
			if ( pPreferredTech && pPreferredTech->GetAvailable() == false )
			{
				if ( !pPreferredTech->GetResourceCost() )
					continue;

				// Try to spend resources on the tech
				int iResourcesSpent = MIN( pPlayer->GetBankResources(), pPreferredTech->GetResourceCost() - pPreferredTech->GetResourceLevel() );
				if ( pPreferredTech->IncreaseResourceLevel( iResourcesSpent ) )
				{
					// The technology's had enough resources spent to buy it, so enable it
					EnableTechnology( pPreferredTech );
					Msg( "%s bought %s\n",	GetName(), pPreferredTech->GetPrintName() );
				}

				// Reduce the player's bank
				if ( iResourcesSpent )
				{
					pPlayer->RemoveBankResources( iResourcesSpent );
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Return true if the team owns the specified technology
//-----------------------------------------------------------------------------
bool CTFTeam::HasNamedTechnology( const char *name )
{
	// Look it up
	// FIXME:  This could be too slow, consider using #define'd/indexed names?
	CBaseTechnology *tech = m_pTechnologyTree->GetTechnology( name ); 
	if ( !tech )
		return false;
	if ( !tech->GetAvailable() )
		return false;

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: A new technology has been received by the team. Do anything specific to this technology here.
//-----------------------------------------------------------------------------
void CTFTeam::GainedNewTechnology( CBaseTechnology *pTechnology )
{
}


//-----------------------------------------------------------------------------
// Purpose: Called by the team's Think function
//-----------------------------------------------------------------------------
void CTFTeam::UpdateTechnologies( void )
{
	// Update clients
	UpdateTechnologyData();
}


//------------------------------------------------------------------------------------------------------------------
// PLAYERS
//-----------------------------------------------------------------------------
// Purpose: Add the specified player to this team. Remove them from their current team, if any.
//-----------------------------------------------------------------------------
void CTFTeam::AddPlayer( CBasePlayer *pPlayer )
{
	BaseClass::AddPlayer( pPlayer );
	
	// Give the player this team's technology
	for ( int i = 0; i < m_pTechnologyTree->GetNumberTechnologies(); i++ )
	{
		CBaseTechnology *technology = m_pTechnologyTree->GetTechnology(i);
		if ( !technology )
			continue;

		if ( technology->IsHidden() )
			continue;

		// Not yet available to team, skip
		if ( !technology->GetAvailable() )
			continue;

		// Add it.
		technology->AddTechnologyToPlayer( (CBaseTFPlayer*)pPlayer );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Clean up the player's objects when they leave
//-----------------------------------------------------------------------------
void CTFTeam::RemovePlayer( CBasePlayer *pPlayer )
{
	BaseClass::RemovePlayer( pPlayer );

	// Destroy all objects belonging to this player
	if ( tf_destroyobjects.GetFloat() )
	{
		// Work backwards through the list because objects remove themselves
		int iSize = m_aObjects.Count();
		for (int i = iSize-1; i >= 0; i--)
		{
			if ( (m_aObjects[i]->GetBuilder() == pPlayer) && (m_aObjects[i]->ShouldAutoRemove()) )
			{
				UTIL_Remove( m_aObjects[i] );
			}
		}
	}

	RecomputePreferences();
}

//-----------------------------------------------------------------------------
// Purpose: Return the number of team members of the specified class
//-----------------------------------------------------------------------------
int	CTFTeam::GetNumOfClass( TFClass iClass )
{
	int iNumber = 0;
	for ( int i = 0; i < GetNumPlayers(); i++ )
	{
		if ( ((CBaseTFPlayer*)GetPlayer(i))->IsClass(iClass) )
		{
			iNumber++;
		}
	}
	return iNumber;
}

//------------------------------------------------------------------------------------------------------------------
// RESOURCE BANK
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::InitializeTeamResources( void )
{
	m_fResources = 0.0f;
	m_fPotentialResources = 0.0f;
	m_bHaveZone = false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
float CTFTeam::GetTeamResources( void )
{
	return m_fResources;
}

//-----------------------------------------------------------------------------
// Purpose: Add resources to this team
//-----------------------------------------------------------------------------
int CTFTeam::AddTeamResources( float fAmount, int nStat )
{
	fAmount = clamp(fAmount, 0, 9999.f);

	m_flTotalResourcesSoFar += fAmount;

	TFStats()->IncrementTeamStat( GetTeamNumber(), TF_TEAM_STAT_RESOURCES_COLLECTED, fAmount );

	// Divvy the resources out to the players
	int iAmountPerPlayer = Ceil2Int( fAmount / GetNumPlayers() );	// Yes, this does create some resources in the roundoff.
	for ( int i = 0; i < GetNumPlayers(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer*)GetPlayer(i);
		pPlayer->AddBankResources( iAmountPerPlayer );
		TFStats()->IncrementPlayerStat( pPlayer, TF_PLAYER_STAT_RESOURCES_ACQUIRED, fAmount );

		if (nStat >= 0)
		{
			TFStats()->IncrementPlayerStat( pPlayer, (TFPlayerStatId_t)nStat, fAmount );
		}
	}

	ResourceLoadDeposited();

	return iAmountPerPlayer;
}

//-----------------------------------------------------------------------------
// Purpose: Give resources to the player
//-----------------------------------------------------------------------------
void CTFTeam::DonateResources( CBaseTFPlayer *pPlayer )
{
	int nPlayerCount = GetNumPlayers();
	if (nPlayerCount <= 1)
		return;

	int pResourceCount;
	int pResourcePerPlayer;
	int pDonationCount;

	bool bDonating = false;
	pResourceCount = pPlayer->GetBankResources();

	// Figure out how many resources per player to donate
	pResourcePerPlayer = pResourceCount / (nPlayerCount - 1); 
	if (pResourceCount % (nPlayerCount - 1) != 0)
		++pResourcePerPlayer;

	// Clamp to max amt per teammate for each hit...
	if (pResourcePerPlayer > RESOURCE_DONATION_AMT_PER_PLAYER)
		pResourcePerPlayer = RESOURCE_DONATION_AMT_PER_PLAYER;

	// Figure out if we are donating anything at all
	if (pResourceCount > 0)
		bDonating = true;
	if (!bDonating)
		return;

	// Now that we've figured how much to donate, do it!
	for ( int i = 0; i < nPlayerCount; i++ )
	{
		CBaseTFPlayer *pDest = (CBaseTFPlayer*)GetPlayer(i);
		if (pDest == pPlayer)
			continue;

		// The last guy(s) gets the scraps... too bad.
		int nCountToDonate = pResourceCount;
		if (nCountToDonate > pResourcePerPlayer)
			nCountToDonate = pResourcePerPlayer;
		pResourceCount -= nCountToDonate;
		pDonationCount = nCountToDonate;

		pPlayer->DonateResources( pDest, pDonationCount );
	}
}


//-----------------------------------------------------------------------------
// Purpose: New resources have just been dumped in the bank
//-----------------------------------------------------------------------------
void CTFTeam::ResourceLoadDeposited( void )
{
	// HACK TEST CODE
	// Remove after Resource Experiment!
	static int iIncrements = 250;
	if ( m_flTotalResourcesSoFar >= (m_iLastUpdateSentAt + iIncrements) )
	{
		while ( m_flTotalResourcesSoFar >= (m_iLastUpdateSentAt + iIncrements) )
		{
			m_iLastUpdateSentAt += iIncrements;
		}

		EntityMessageBegin( (CBaseEntity*)this );
			WRITE_LONG( m_iLastUpdateSentAt );
		MessageEnd();
	}

	// Now see if we should buy anything
	RecomputePurchases();
}

//------------------------------------------------------------------------------------------------------------------
// RESUPPLY BEACONS
//-----------------------------------------------------------------------------
// Purpose: Add the specified resupply beacon to this team. 
//-----------------------------------------------------------------------------
void CTFTeam::AddResupply( CObjectResupply *pResupply )
{
	TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::AddResupply adding resupply %p to team %s\n", gpGlobals->curtime, 
		pResupply, GetName() ) );

	m_aResupplyBeacons.AddToTail( pResupply );
}

//-----------------------------------------------------------------------------
// Purpose: Remove this resupply beacon from the team
//-----------------------------------------------------------------------------
void CTFTeam::RemoveResupply( CObjectResupply *pResupply )
{
	TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::RemoveResupply remove resupply %p from team %s\n", gpGlobals->curtime, 
		pResupply, GetName() ) );

	// Now remove the beacon from our list
	m_aResupplyBeacons.FindAndRemove( pResupply );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTFTeam::GetNumObjects( int iObjectType )
{
	// Asking for a count of a specific object type?
	if ( iObjectType > 0 )
	{
		int iCount = 0;
		for ( int i = 0; i < GetNumObjects(); i++ )
		{
			CBaseObject *pObject = GetObject(i);
			if ( pObject && pObject->GetType() == iObjectType )
			{
				iCount++;
			}
		}
		return iCount;
	}

	return m_aObjects.Count();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CBaseObject *CTFTeam::GetObject( int num )
{
 	Assert( num >= 0 && num < m_aObjects.Count() );
	return m_aObjects[ num ];
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTFTeam::GetNumResupplies( void )
{
	return m_aResupplyBeacons.Count();
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CObjectResupply *CTFTeam::GetResupply( int num )
{
	Assert( num >= 0 && num < m_aResupplyBeacons.Count() );
	return m_aResupplyBeacons[ num ];
}


bool CTFTeam::IsCoveredBySentryGun( const Vector &vPos )
{
	for( int i=0; i < m_aObjects.Count(); i++ )
	{
		CBaseObject *pObj = m_aObjects[i];
		
		if ( pObj->IsSentrygun() && vPos.DistTo( pObj->GetAbsOrigin() ) < OBJECT_COVERED_DIST )
			return true;

	}

	return false;
}


int CTFTeam::GetNumShieldWallsCoveringPosition( const Vector &vPos )
{
	int count = 0;

	for ( int i=0; i < m_aObjects.Count(); i++ )
	{
		CBaseObject *pObj = m_aObjects[i];
		
		if ( pObj->GetType() == OBJ_SHIELDWALL )
		{
			if ( vPos.DistTo( pObj->GetAbsOrigin() ) < OBJECT_COVERED_DIST )
				++count;
		}
	}

	return count;
}


int CTFTeam::GetNumResuppliesCoveringPosition( const Vector &vPos )
{
	int count = 0;

	for ( int i=0; i < m_aResupplyBeacons.Count(); i++ )
	{
		CBaseObject *pObj = m_aResupplyBeacons[i];
		if ( vPos.DistTo( pObj->GetAbsOrigin() ) < RESUPPLY_COVER_DIST )
			++count;
	}

	return count;
}


int CTFTeam::GetNumRespawnStationsCoveringPosition( const Vector &vPos )
{
	int count = 0;

	for ( int i=0; i < m_aObjects.Count(); i++ )
	{
		CBaseObject *pObj = m_aObjects[i];

		if ( pObj->GetType() == OBJ_RESPAWN_STATION )
		{
			if ( vPos.DistTo( pObj->GetAbsOrigin() ) < OBJECT_COVERED_DIST )
			{
				++count;
			}
		}
	}

	return count;
}


//------------------------------------------------------------------------------------------------------------------
// OBJECTS
//-----------------------------------------------------------------------------
// Purpose: Add the specified object to this team.
//-----------------------------------------------------------------------------
void CTFTeam::AddObject( CBaseObject *pObject )
{
	TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::AddObject adding object %p:%s to team %s\n", gpGlobals->curtime, 
		pObject, pObject->GetClassname(), GetName() ) );

	bool alreadyInList = IsObjectOnTeam( pObject );
	Assert( !alreadyInList );
	if ( !alreadyInList )
	{
		m_aObjects.AddToTail( pObject );
	}
}

//-----------------------------------------------------------------------------
// Returns true if the object is in the team's list of objects
//-----------------------------------------------------------------------------
bool CTFTeam::IsObjectOnTeam( CBaseObject *pObject ) const
{
	return ( m_aObjects.Find( pObject ) != -1 );
}


//-----------------------------------------------------------------------------
// Purpose: Remove this object from the team
//  Removes all references from all sublists as well
//-----------------------------------------------------------------------------
void CTFTeam::RemoveObject( CBaseObject *pObject )
{									   
	if ( m_aObjects.Count() <= 0 )
		return;

	if ( m_aObjects.Find( pObject ) != -1 )
	{
		TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::RemoveObject removing %p:%s from %s\n", gpGlobals->curtime, 
			pObject, pObject->GetClassname(), GetName() ) );

		m_aObjects.FindAndRemove( pObject );
	}
	else
	{
		TRACE_OBJECT( UTIL_VarArgs( "%0.2f CTFTeam::RemoveObject couldn't remove %p:%s from %s\n", gpGlobals->curtime, 
			pObject, pObject->GetClassname(), GetName() ) );
	}
}


//------------------------------------------------------------------------------------------------------------------
// ORDERS
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::InitializeOrders( void )
{
	m_flPersonalOrderUpdateTime = 0;
}

//-----------------------------------------------------------------------------
// Purpose: Add a new order to our list. If it already exists, bump it's priority to the new priority.
//-----------------------------------------------------------------------------
COrder* CTFTeam::AddOrder( 
	int iOrderType, 
	CBaseEntity *pTarget, 
	CBaseTFPlayer *pPlayer, 
	float flDistanceToRemove,
	float flLifetime,
	COrder *pNewOrder
	)
{
	// Remove any orders to the player.
	RemoveOrdersToPlayer( pPlayer );

	// The new system requires order class to be passed in.
	Assert( pNewOrder );

	// All the order create functions should just use new to create the order class,
	// then we'll attach the edict in here. There's no reason to use LINK_ENTITY_TO_CLASS
	// and CreateEntityByName.
	Assert( !pNewOrder->edict() );
	pNewOrder->NetworkProp()->AttachEdict();
	
	pNewOrder->ChangeTeam( GetTeamNumber() );
	OrderHandle hOrder;
	hOrder = pNewOrder;
	m_aOrders.AddToTail( hOrder );

	// Update target
	pNewOrder->SetTarget( pTarget );
	pNewOrder->SetDistance( flDistanceToRemove );

	// Update lifetime.
	pNewOrder->SetLifetime( flLifetime );

	Assert( pPlayer->GetOrder() == NULL );

	pNewOrder->SetOwner( pPlayer );
	pPlayer->SetOrder( pNewOrder );

	// "New Order Received!"
	CSingleUserRecipientFilter filter( pPlayer );
	filter.MakeReliable();
	CBaseEntity::EmitSound( filter, pPlayer->entindex(),"TFTeam.AddOrder" );

	// Debug check.. it should never create an order with its termination conditions
	// already met.	
	Assert( !pNewOrder->Update() );

	return pNewOrder;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::RemoveOrder( COrder *pOrder )
{
	OrderHandle hOrder;
	hOrder = pOrder;

	m_aOrders.FindAndRemove( hOrder );
}

//-----------------------------------------------------------------------------
// Purpose: Recalculate the team's orders & their priorities
//-----------------------------------------------------------------------------
void CTFTeam::RecalcOrders( void )
{
	// Update all existing orders
	UpdateOrders();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::UpdateOrders( void )
{
	// Tell all our current orders to update themselves. Walk backwards because we may remove them.
	int iSize = m_aOrders.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		// Orders without owners should be removed
		bool bShouldRemove = ( 
			!m_aOrders[i] || 
			!m_aOrders[i]->GetOwner() || 
			m_aOrders[i]->Update() );
		if ( bShouldRemove )
		{
			COrder *pOrder = m_aOrders[i];
			m_aOrders.Remove( i );
			UTIL_Remove( pOrder );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: An event has just occurred that affects orders. Tell all our orders that
//			have the specified entity as a target.
//-----------------------------------------------------------------------------
void CTFTeam::UpdateOrdersOnEvent( COrderEvent_Base *pOrder )
{
	// Tell all our current orders to update themselves. Walk backwards because we may remove them.
	int iSize = m_aOrders.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		bool bShouldRemove = m_aOrders[i]->UpdateOnEvent( pOrder );
		if ( bShouldRemove )
		{
			COrder *pOrder = m_aOrders[i];
			m_aOrders.Remove( i );
			UTIL_Remove( pOrder );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Create personal orders for all the team's members
//-----------------------------------------------------------------------------
void CTFTeam::CreatePersonalOrders( void )
{
	// Create personal orders for each player
	for ( int i = 0; i < m_aPlayers.Count(); i++ )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)m_aPlayers[i];

		// Don't create orders for bots, undefined or dead people
		if ( !(pPlayer->GetFlags() & FL_FAKECLIENT) && pPlayer->IsAlive() && !pPlayer->IsClass( TFCLASS_UNDECIDED ) )
		{
			CreatePersonalOrder( pPlayer );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Create personal orders for specified player
//-----------------------------------------------------------------------------
void CTFTeam::CreatePersonalOrder( CBaseTFPlayer *pPlayer )
{
	// We still haven't made a personal order, so ask the class if it wants to
	if ( pPlayer->GetPlayerClass() )
	{
		pPlayer->GetPlayerClass()->CreatePersonalOrder();
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::RemoveOrdersToPlayer( CBaseTFPlayer *pPlayer )
{
	// Walk backwards because we're removing them.
	int iSize = m_aOrders.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		// Orders without owners should be removed
		if ( m_aOrders[i].Get() )
		{
			if( m_aOrders[i]->GetOwner() == pPlayer )
			{
				COrder *pOrder = m_aOrders[i];
				m_aOrders.Remove( i );
				
				pOrder->DetachFromPlayer();
				UTIL_Remove( pOrder );
			}
		}
		else
		{
			m_aOrders.Remove( i );
		}
	}

	pPlayer->SetOrder( NULL );
}


//-----------------------------------------------------------------------------
// Purpose: Count and return the number of orders of the type with the specified target
//-----------------------------------------------------------------------------
int CTFTeam::CountOrders( int flags, int iOrderType, CBaseEntity *pTarget, CBaseTFPlayer *pOwner )
{
	int iOrderCount = 0;

	// Count the number of global orders
	for ( int i = 0; i < m_aOrders.Count(); i++ )
	{
		COrder *pOrder = m_aOrders[i];

		if( flags & COUNTORDERS_TYPE )
			 if( pOrder->GetType() != iOrderType )
				continue;

		if( flags & COUNTORDERS_TARGET )
			if( pOrder->GetTargetEntity() != pTarget )
				continue;

		if( flags & COUNTORDERS_OWNER )
			if( pOrder->GetOwner() != pOwner )
				continue;
		
		// Ok, this order matches the criteria.
		iOrderCount++;
	}

	return iOrderCount;
}


int CTFTeam::CountOrdersOwnedByPlayer( CBaseTFPlayer *pPlayer )
{
	return CountOrders( COUNTORDERS_OWNER, 0, 0, pPlayer );
}


//------------------------------------------------------------------------------------------------------------------
// MESSAGES
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::ClearMessages( void )
{
	int iSize = m_aMessages.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		CTeamMessage *pMessage = m_aMessages[i];
		m_aMessages.Remove( i );
		delete pMessage;
	}

	m_aMessages.Purge();
}

//-----------------------------------------------------------------------------
// Purpose: Post a message of the specified type
//-----------------------------------------------------------------------------
void CTFTeam::PostMessage( int iMessageID, CBaseEntity *pEntity, char *sData )
{
	// First see if we've got this message in the queue already
	for ( int i = 0; i < m_aMessages.Count(); i++ )
	{
		CTeamMessage *pMessage = m_aMessages[i];
		if ( (pMessage->GetID() == iMessageID) && (pMessage->GetEntity() == pEntity) )
		{
			// Already in the queue, abort.
			return;
		}
	}

	// Create a new message and add it to my tail
	CTeamMessage *pMessage = CTeamMessage::Create( this, iMessageID, pEntity );
	if ( sData && sData[0] )
	{
		pMessage->SetData( sData );
	}
	m_aMessages.AddToTail( pMessage );

	// Tell the message to fire
	pMessage->FireMessage();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeam::UpdateMessages( void )
{
	// Go through my messages and kill any that have reached their TTL
	int iSize = m_aMessages.Count();
	for (int i = iSize-1; i >= 0; i--)
	{
		CTeamMessage *pMessage = m_aMessages[i];
		if ( gpGlobals->curtime > pMessage->GetTTL() )
		{
			m_aMessages.Remove( i );
			delete pMessage;
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Tell all our powerpacks to update their powered objects.
//			pPackToIgnore: Pack to ignore, because it's dying.
//			pObjectToTarget: An object looking for power, because it's being placed
//-----------------------------------------------------------------------------
void CTFTeam::UpdatePowerpacks( CObjectPowerPack *pPackToIgnore, CBaseObject *pObjectToTarget )
{
	for ( int i = 0; i < GetNumObjects(); i++ )
	{
		CBaseObject *pObject = GetObject(i);
		assert(pObject);
		if ( pObject == pPackToIgnore || pObject->GetType() != OBJ_POWERPACK )
			continue;

		((CObjectPowerPack*)pObject)->PowerNearbyObjects( pObjectToTarget );

		// Quit as soon as we've powered the specified one, if there is one
		if ( pObjectToTarget && pObjectToTarget->IsPowered() )
			break;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Tell all our buff stations to update and look for objects.
//   Input:	pBuffStationToIgnore: Buff station to ignore, because it is dying
//          pObjectToTarget: an object looking for a buff station, because it is being placed
//-----------------------------------------------------------------------------
void CTFTeam::UpdateBuffStations( CObjectBuffStation *pBuffStationToIgnore, CBaseObject *pObjectToTarget, bool bPlacing )
{
	for ( int iObject = 0; iObject < GetNumObjects(); ++iObject )
	{
		CBaseObject *pObject = GetObject( iObject );
		assert( pObject );
	
		if ( pObject->GetType() != OBJ_BUFF_STATION )
			continue;

		CObjectBuffStation *pBuffStation = static_cast<CObjectBuffStation*>( pObject );
		if ( pBuffStation == pBuffStationToIgnore )
			continue;

		pBuffStation->BuffNearbyObjects( pObjectToTarget, bPlacing );

		// Quit as soon as we've powered the specified one, if there is one.
		if ( pObjectToTarget && pObjectToTarget->IsHookedAndBuffed() )
			break;
	}
}

//------------------------------------------------------------------------------------------------------------------
// UTILITY FUNCS
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFTeam* CTFTeam::GetEnemyTeam()
{
	// Look for nearby enemy objects we can capture.
	int iMyTeam = GetTeamNumber();
	if( iMyTeam == 0 )
		return NULL;

	int iEnemyTeam = !(iMyTeam - 1) + 1;
	return (CTFTeam*)GetGlobalTeam( iEnemyTeam );
}