summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_item_inventory.cpp
blob: 57c674ad086c344bacc9cc1aa75d3b1a1290af3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "cbase.h"
#include "tf_item_inventory.h"
#include "econ_entity_creation.h"
#include "tf_item_system.h"
#include "vgui/ILocalize.h"
#include "tier3/tier3.h"
#ifdef CLIENT_DLL
#include "c_tf_player.h"
#include "item_pickup_panel.h"
#include "KeyValues.h"
#include "filesystem.h"
#include "character_info_panel.h"
#include "ienginevgui.h"
#include "c_tf_gamestats.h"
#include "econ_notifications.h"
#include "achievementmgr.h"
#include "baseachievement.h"
#include "achievements_tf.h"
#include "econ/econ_item_preset.h"
#include "tf_shared_content_manager.h"
#include "c_playerresource.h"
#include "quest_log_panel.h"
#include "backpack_panel.h"
#include "materialsystem/itexture.h"
#else
#include "tf_player.h"
#endif
#include "gc_clientsystem.h"
#include "econ_game_account_client.h"
#include "gcsdk/gcclientsdk.h"
#include "econ_gcmessages.h"
#include "tf_gamerules.h"
#include "tf_gcmessages.h"
#include "econ_item.h"
#include "game_item_schema.h"

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

using namespace GCSDK;

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
CEconNotification_HasNewItems::CEconNotification_HasNewItems() : CEconNotification()
{
	m_bHasTriggered = false;
	m_flExpireTime = -1.0f;		// Does not initially expire

	m_bShowInGame = false;

	// Check to see if any items are not drops, if so show in game
	// do not show in game if the new items are only drops
	CPlayerInventory *pLocalInv = InventoryManager()->GetLocalInventory();
	if ( pLocalInv )
	{
		// Go through the root inventory and find any items that are in the "found" position
		int iCount = pLocalInv->GetItemCount();
		for ( int i = 0; i < iCount; i++ )
		{
			CEconItemView *pTmp = pLocalInv->GetItem(i);
			if ( !pTmp )
				continue;

			if ( pTmp->GetStaticData()->IsHidden() )
				continue;

			uint32 iPosition = pTmp->GetInventoryPosition();
			if ( IsUnacknowledged(iPosition) == false )
				continue;
			if ( InventoryManager()->GetBackpackPositionFromBackend(iPosition) != 0 )
				continue;

			// Now make sure we haven't got a clientside saved ack for this item.
			if ( InventoryManager()->HasBeenAckedByClient( pTmp ) )
				continue;

			// If item is not a drop we want to show the notification otherwise they'll get the notification on death
			int iFoundMethod = GetUnacknowledgedReason( iPosition );
			if ( iFoundMethod > UNACK_ITEM_DROPPED )
			{
				m_bShowInGame = true;
				break;
			}
		}
	}
}
	

CEconNotification_HasNewItems::~CEconNotification_HasNewItems()
{
	if ( !m_bHasTriggered )
	{
		m_bHasTriggered = true;
		TFInventoryManager()->ShowItemsPickedUp( true, true, true );
	}
}

//-----------------------------------------------------------------------------
CEconNotification_HasNewItemsOnKill::CEconNotification_HasNewItemsOnKill( int iVictimID )
{
	m_bHasTriggered = false;
	SetLifetime( 3.0f );
	SetText( "#TF_EnemyDroppedItem" );

	wchar_t wszPlayerName[MAX_PLAYER_NAME_LENGTH];
	g_pVGuiLocalize->ConvertANSIToUnicode( g_PR->GetPlayerName( iVictimID ), wszPlayerName, sizeof( wszPlayerName ) );
	AddStringToken( "victim", wszPlayerName );

	m_bShowInGame = true;
}

/*static*/ bool CEconNotification_HasNewItemsOnKill::HasUnacknowledgedItems () 
{
	// Check to see if any items are not drops, if so show in game
	// do not show in game if the new items are only drops
	CPlayerInventory *pLocalInv = InventoryManager()->GetLocalInventory();
	if ( pLocalInv )
	{
		// Go through the root inventory and find any items that are in the "found" position
		int iCount = pLocalInv->GetItemCount();
		for ( int i = 0; i < iCount; i++ )
		{
			CEconItemView *pTmp = pLocalInv->GetItem( i );
			if ( !pTmp )
				continue;

			if ( pTmp->GetStaticData()->IsHidden() )
				continue;

			uint32 iPosition = pTmp->GetInventoryPosition();
			if ( IsUnacknowledged( iPosition ) == false )
				continue;
			if ( InventoryManager()->GetBackpackPositionFromBackend( iPosition ) != 0 )
				continue;

			// Now make sure we haven't got a clientside saved ack for this item.
			if ( InventoryManager()->HasBeenAckedByClient( pTmp ) )
				continue;

			// If item is not a drop we want to show the notification otherwise they'll get the notification on death
			int iFoundMethod = GetUnacknowledgedReason( iPosition );
			if ( iFoundMethod > UNACK_ITEM_DROPPED )
			{
				return true;
			}
		}
	}
	return false;
}


#endif

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool AreSlotsConsideredIdentical( EEquipType_t eEquipType, int iBaseSlot, int iTestSlot )
{
	if ( eEquipType == EQUIP_TYPE_CLASS )
	{
		if ( iBaseSlot == LOADOUT_POSITION_MISC )
		{
			return IsMiscSlot( iTestSlot );
		}

		if ( iBaseSlot == LOADOUT_POSITION_BUILDING )
		{
			return IsBuildingSlot( iTestSlot );
		}

		if ( iBaseSlot == LOADOUT_POSITION_TAUNT )
		{
			return IsTauntSlot( iTestSlot );
		}
	}
	else if ( eEquipType == EQUIP_TYPE_ACCOUNT )
	{
		if ( iBaseSlot == ACCOUNT_LOADOUT_POSITION_ACCOUNT1 )
		{
			return IsQuestSlot( iTestSlot );
		}
	}

	return iBaseSlot == iTestSlot;
}

//-----------------------------------------------------------------------------
CTFInventoryManager g_TFInventoryManager;
CInventoryManager *InventoryManager( void )
{
	return &g_TFInventoryManager;
}
CTFInventoryManager *TFInventoryManager( void )
{
	return &g_TFInventoryManager;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFInventoryManager::CTFInventoryManager( void )

{
}

CTFInventoryManager::~CTFInventoryManager( void )
{
	m_pBaseLoadoutItems.PurgeAndDeleteElements();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFInventoryManager::PostInit( void )
{
	BaseClass::PostInit();
	GenerateBaseItems();
}

//-----------------------------------------------------------------------------
// Purpose: Generate & store the base item details for each class & loadout slot
//-----------------------------------------------------------------------------
void CTFInventoryManager::GenerateBaseItems( void )
{
	// Purge our lists and make new
	m_pBaseLoadoutItems.PurgeAndDeleteElements();
	
	// Load a base top level invalid item
	{
		m_pDefaultItem = new CEconItemView;
		m_pDefaultItem->Invalidate();
	}
	//
	const CEconItemSchema::BaseItemDefinitionMap_t& mapItems = GetItemSchema()->GetBaseItemDefinitionMap();
	int iStart = 0;
	for ( int it = iStart; it != mapItems.InvalidIndex(); it = mapItems.NextInorder( it ) )
	{
		CEconItemView *pItem = new CEconItemView;
		pItem->Init( mapItems[it]->GetDefinitionIndex(), AE_USE_SCRIPT_VALUE, AE_USE_SCRIPT_VALUE, false );
		m_pBaseLoadoutItems.AddToTail( pItem );
	}
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFInventoryManager::EquipItemInLoadout( int iClass, int iSlot, itemid_t iItemID )
{
	if ( !steamapicontext || !steamapicontext->SteamUser() )
		return false;

	// If they pass in a INVALID_ITEM_ID item id, we're just clearing the loadout slot
	if ( iItemID == INVALID_ITEM_ID )
		return m_LocalInventory.ClearLoadoutSlot( iClass, iSlot );

	CEconItemView *pItem = m_LocalInventory.GetInventoryItemByItemID( iItemID );
	if ( !pItem )
		return false;

	// We check for validity on the GC when we equip items, but we can't really trust anyone
	// and so we check here as well.
	if ( !AreSlotsConsideredIdentical( pItem->GetStaticData()->GetEquipType(), pItem->GetStaticData()->GetLoadoutSlot(iClass), iSlot ) )
	{
		return false;
	}

	if ( !pItem->GetStaticData()->CanBeUsedByClass( iClass ) )
	{
		return false;
	}

	// Equip the new item
	UpdateInventoryEquippedState( &m_LocalInventory, iItemID, iClass, iSlot );

	// TODO: Prediction
	// Item has been moved, so update our loadout.
	//m_LoadoutItems[iClass][iSlot] = iItemID;
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Fills out pList with all inventory items that could fit into the specified loadout slot for a given class
//-----------------------------------------------------------------------------
int	CTFInventoryManager::GetAllUsableItemsForSlot( int iClass, int iSlot, CUtlVector<CEconItemView*> *pList )
{
	bool bIsAccountIndex = iClass == GEconItemSchema().GetAccountIndex();
	if ( bIsAccountIndex )
	{
		Assert( IsQuestSlot( iSlot ) );
	}
	else
	{
		Assert( iClass >= TF_FIRST_NORMAL_CLASS && iClass < TF_CLASS_COUNT );
		Assert( iSlot >= -1 && iSlot < CLASS_LOADOUT_POSITION_COUNT );
	}

	int iCount = m_LocalInventory.GetItemCount();
	for ( int i = 0; i < iCount; i++ )
	{
		CEconItemView *pItem = m_LocalInventory.GetItem(i);
		CTFItemDefinition *pItemData = pItem->GetStaticData();

		if ( bIsAccountIndex != ( pItemData->GetEquipType() == EEquipType_t::EQUIP_TYPE_ACCOUNT ) )
			continue;

		if ( !bIsAccountIndex && !pItemData->CanBeUsedByClass(iClass) )
			continue;

		// Passing in iSlot of -1 finds all items usable by the class
		if ( iSlot >= 0 && pItem->GetStaticData()->GetLoadoutSlot( iClass ) != iSlot )
			continue;

		// Ignore unpack'd items
		if ( IsUnacknowledged( pItem->GetInventoryPosition() ) )
			continue;

		pList->AddToTail( m_LocalInventory.GetItem(i) );
	}

	return pList->Count();
}

//-----------------------------------------------------------------------------
// Purpose: Fills out pList with all quest item in the local inventory
//-----------------------------------------------------------------------------
int CTFInventoryManager::GetAllQuestItems( CUtlVector<CEconItemView*> *pList )
{
	if ( !pList )
		return 0;

	pList->RemoveAll();

	for ( int i = 0 ; i < m_LocalInventory.GetItemCount() ; ++i )
	{
		CEconItemView *pItem = m_LocalInventory.GetItem( i );

		// Cheapest test
		if ( pItem->GetItemDefinition()->GetQuestDef() == NULL ) 
			continue;

		pList->AddToTail( pItem );
	}

	return pList->Count();
}

#endif // CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CEconItemView *CTFInventoryManager::GetItemInLoadoutForClass( int iClass, int iSlot, CSteamID *pID )
{
#ifdef CLIENT_DLL
	if ( !pID )
	{
		// If they didn't specify a steamID, use the local player
		if ( !steamapicontext || !steamapicontext->SteamUser() )
			return NULL;

		CSteamID localSteamID = steamapicontext->SteamUser()->GetSteamID();
		pID = &localSteamID;
	}
#endif

	CTFPlayerInventory *pInv = GetInventoryForPlayer( *pID );
	if ( !pInv )
		return GetBaseItemForClass( iClass, iSlot );

	return pInv->GetItemInLoadout( iClass, iSlot );
}

CEconItemView *CTFInventoryManager::GetItemInLoadoutForAccount( int iSlot, CSteamID *pID )
{
#ifdef CLIENT_DLL
	if ( !pID )
	{
		// If they didn't specify a steamID, use the local player
		if ( !steamapicontext || !steamapicontext->SteamUser() )
			return NULL;

		CSteamID localSteamID = steamapicontext->SteamUser()->GetSteamID();
		pID = &localSteamID;
	}
#endif

	CTFPlayerInventory *pInv = GetInventoryForPlayer( *pID );
	if ( !pInv )
		return NULL;

	return pInv->GetItemInLoadout( GEconItemSchema().GetAccountIndex(), iSlot );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFPlayerInventory *CTFInventoryManager::GetInventoryForPlayer( const CSteamID &playerID )
{
	for ( int i = 0; i < m_pInventories.Count(); i++ )
	{
		if ( m_pInventories[i].pInventory->GetOwner() != playerID )
			continue;

		return assert_cast<CTFPlayerInventory*>( m_pInventories[i].pInventory );
	}

	return NULL;
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTFInventoryManager::GetNumItemPickedUpItems( void )
{
	int iResult = 0;
	int iCount = m_LocalInventory.GetItemCount();
	for ( int i = 0; i < iCount; i++ )
	{
		if ( IsUnacknowledged( m_LocalInventory.GetItem(i)->GetInventoryPosition() ) && !m_LocalInventory.GetItem(i)->GetStaticData()->IsHidden() )
		{
			++iResult;
		}
	}
	return iResult;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFInventoryManager::ShowItemsPickedUp( bool bForce, bool bReturnToGame, bool bNoPanel )
{
	// don't show new items in training, unless forced to do so
	// i.e. purchased something or traded...
	if ( bForce == false && TFGameRules() && ( TFGameRules()->IsInTraining() || TFGameRules()->IsCompetitiveMode() ) )
	{
		return false;
	}

	// Don't bring it up if we're already browsing something in the gameUI
	vgui::VPANEL gameuiPanel = enginevgui->GetPanel( PANEL_GAMEUIDLL );
	if ( !bForce && vgui::ipanel()->IsVisible( gameuiPanel ) )
		return false;

	CUtlVector<CEconItemView*> aItemsFound;

	// Go through the root inventory and find any items that are in the "found" position
	int iCount = m_LocalInventory.GetItemCount();
	for ( int i = 0; i < iCount; i++ )
	{
		CEconItemView *pTmp = m_LocalInventory.GetItem(i);
		if ( !pTmp )
			continue;

		if ( pTmp->GetStaticData()->IsHidden() )
			continue;

		uint32 iPosition = pTmp->GetInventoryPosition();
		if ( IsUnacknowledged(iPosition) == false )
			continue;
		if ( GetBackpackPositionFromBackend(iPosition) != 0 )
			continue;

		// Now make sure we haven't got a clientside saved ack for this item.
		// This makes sure we don't show multiple pickups for items that we've found,
		// but haven't been able to move out of unack'd position due to the GC being unavailable.
		if ( HasBeenAckedByClient( pTmp ) )
			continue;

		aItemsFound.AddToTail( pTmp );
	}

	if ( !aItemsFound.Count() )
		return CheckForRoomAndForceDiscard();

	// We're not forcing the player to make room yet. Just show the pickup panel.
	NotificationQueue_Remove( &CEconNotification_HasNewItems::IsNotificationType );
	CItemPickupPanel *pItemPanel = bNoPanel ? NULL : EconUI()->OpenItemPickupPanel();

	if ( pItemPanel )
	{
		pItemPanel->SetReturnToGame( bReturnToGame );
	}

	// Only acknowledge items if there is no panel
	// Panel will make calls to acknowledge items itself 
	for ( int i = 0; i < aItemsFound.Count(); i++ )
	{
		if ( pItemPanel )
		{
			pItemPanel->AddItem( aItemsFound[i] );
		}
		else 
		{
			AcknowledgeItem( aItemsFound[i] );
		}
	}

	if ( pItemPanel )
	{
		pItemPanel->MoveToFront();
	}
	else
	{
		SaveAckFile();
	}
	
	aItemsFound.Purge();
	return true;
}

//-----------------------------------------------------------------------------
void CTFInventoryManager::AcknowledgeItem( CEconItemView *pItem, bool bMoveToBackpack /* = true */ )
{
	SetAckedByClient( pItem );

	int iMethod = GetUnacknowledgedReason( pItem->GetInventoryPosition() ) - 1;
	if ( iMethod >= ARRAYSIZE( g_pszItemPickupMethodStringsUnloc ) || iMethod < 0 )
		iMethod = 0;
	EconUI()->Gamestats_ItemTransaction( IE_ITEM_RECEIVED, pItem, g_pszItemPickupMethodStringsUnloc[iMethod] );

	if ( iMethod+1 == UNACK_ITEM_PREVIEW_ITEM_PURCHASED )
	{
		// If we found a purchased preview item, we want to refresh the store view to remove the discount indicator.
		CStorePage* pStorePage = dynamic_cast<CStorePage*>( EconUI()->GetStorePanel()->GetActivePage() );
		if ( pStorePage )
		{
			pStorePage->UpdateModelPanels();
		}
	}

	// Move it to the first empty backpack position.
	if ( bMoveToBackpack )
	{
		SetItemBackpackPosition( pItem, 0, false, true );
	}
}

void CTFInventoryManager::Update( float frametime )
{
	TM_ZONE_DEFAULT( TELEMETRY_LEVEL0 );
	m_LocalInventory.UpdateWeaponSkinRequest();

	BaseClass::Update( frametime );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFInventoryManager::ShowItemsCrafted( CUtlVector<itemid_t> *vecCraftedIndices )
{
	CUtlVector<CEconItemView*> aItemsFound;
	FOR_EACH_VEC( *vecCraftedIndices, i )
	{
		CEconItemView *pItem = m_LocalInventory.GetInventoryItemByItemID( vecCraftedIndices->Element(i) );
		if ( pItem )
		{
			// Now make sure we haven't got a clientside saved ack for this item.
			// This makes sure we don't show multiple pickups for items that we've found,
			// but haven't been able to move out of unack'd position due to the GC being unavailable.
			if ( !HasBeenAckedByClient( pItem ) )
			{
				aItemsFound.AddToTail( pItem );
			}
		}
	}

	if ( !aItemsFound.Count() )
		return;

	NotificationQueue_Remove( &CEconNotification_HasNewItems::IsNotificationType );
	CItemPickupPanel *pItemPanel = EconUI()->OpenItemPickupPanel();
	for ( int i = 0; i < aItemsFound.Count(); i++ )
	{
		pItemPanel->AddItem( aItemsFound[i] );

		SetAckedByClient( aItemsFound[i] );

#ifdef CLIENT_DLL
		EconUI()->Gamestats_ItemTransaction( IE_ITEM_RECEIVED, aItemsFound[i], "crafted" );
#endif

		// Then move it to the first empty backpack position
		SetItemBackpackPosition( aItemsFound[i], 0, false, true );
	}
	SaveAckFile();
	pItemPanel->MoveToFront();

	aItemsFound.Purge();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFInventoryManager::CheckForRoomAndForceDiscard( void )
{
	// Go through the inventory and attempt to move any items outside the backpack into valid positions.
	// Remember the first item that we failed to move, so we can force a discard later.
	CEconItemView *pItem = NULL;
	const int iMaxItems = m_LocalInventory.GetMaxItemCount();
	int iCount = m_LocalInventory.GetItemCount();
	for ( int i = 0; i < iCount; i++ )
	{
		CEconItemView *pTmp = m_LocalInventory.GetItem(i);
		if ( !pTmp )
			continue;

		if ( pTmp->GetStaticData()->IsHidden() )
			continue;

		uint32 iPosition = pTmp->GetInventoryPosition();
		if ( IsUnacknowledged(iPosition) || GetBackpackPositionFromBackend(iPosition) > iMaxItems )
		{
			if ( !SetItemBackpackPosition( pTmp, 0, false, false ) )
			{
				pItem = pTmp;
				break;
			}
		}
	}

	// If we're not over the limit, we're done.
	if ( ( iCount - m_iPredictedDiscards ) <= iMaxItems )
		return false;

	if ( !pItem )
		return false;

	// We're forcing the player to make room for items he's found. Bring up that panel with the first item over the limit.
	CItemDiscardPanel *pDiscardPanel = EconUI()->OpenItemDiscardPanel();
	pDiscardPanel->SetItem( pItem );
	return true;
}
#endif

bool CTFInventoryManager::SlotContainsBaseItems( EEquipType_t eType, int iSlot )
{
	Assert( eType != EEquipType_t::EQUIP_TYPE_INVALID );

	if ( eType == EEquipType_t::EQUIP_TYPE_ACCOUNT )
	{
		return !IsQuestSlot( iSlot );
	}

	// Passtime gun
	if ( (iSlot == LOADOUT_POSITION_UTILITY) && TFGameRules() && TFGameRules()->IsPasstimeMode() )
	{
		return true;
	}

	// Halloween spellbook
	if ( iSlot == LOADOUT_POSITION_ACTION )
	{
		if ( TFGameRules() && TFGameRules()->IsUsingSpells() )
			return true;

		if ( TFGameRules() && TFGameRules()->IsUsingGrapplingHook() )
			return true;
	}
#ifdef STAGING_ONLY	
	return ( ( iSlot < LOADOUT_POSITION_HEAD && iSlot != LOADOUT_POSITION_UTILITY && !IsTauntSlot( iSlot ) )	// Allow utility slots to be empty
			|| iSlot == LOADOUT_POSITION_PDA3 );
#else // STAGING_ONLY
	// Normal game
	return iSlot < LOADOUT_POSITION_HEAD;
#endif // #else
}

//-----------------------------------------------------------------------------
// Purpose: Returns the item data for the base item in the loadout slot for a given class
//-----------------------------------------------------------------------------
CEconItemView *CTFInventoryManager::GetBaseItemForClass( int iClass, int iSlot )
{
	// There is no base account item
	if ( iClass == GEconItemSchema().GetAccountIndex() )
		return m_pDefaultItem;

	if ( !HushAsserts() )
	{
		AssertMsg( iClass >= TF_FIRST_NORMAL_CLASS && iClass < TF_CLASS_COUNT, "Invalid TF_CLASS_: %d", iClass );
	}
	Assert( iSlot >= 0 && iSlot < CLASS_LOADOUT_POSITION_COUNT );

	if ( iClass < TF_FIRST_NORMAL_CLASS || iClass >= TF_CLASS_COUNT || iSlot < 0 || iSlot >= CLASS_LOADOUT_POSITION_COUNT )
		return m_pDefaultItem;

	// Halloween spellbook
	if ( iSlot == LOADOUT_POSITION_ACTION )
	{
		CUtlVector< item_definition_index_t > stockActionItemDefIndices;

		static CSchemaItemDefHandle pItemDef_SpellBook( "TF_WEAPON_SPELLBOOK" );
		if ( TFGameRules() && TFGameRules()->IsUsingSpells() && pItemDef_SpellBook )
		{
			stockActionItemDefIndices.AddToTail( pItemDef_SpellBook->GetDefinitionIndex() );
		}

		static CSchemaItemDefHandle pItemDef_GrapplingHook( "TF_WEAPON_GRAPPLINGHOOK" );
		if ( TFGameRules() && TFGameRules()->IsUsingGrapplingHook() && pItemDef_GrapplingHook )
		{
			stockActionItemDefIndices.AddToTail( pItemDef_GrapplingHook->GetDefinitionIndex() );
		}

		static CSchemaItemDefHandle pItemDef_MvMCanteen( "Default Power Up Canteen (MvM)" );
		if ( TFGameRules() && TFGameRules()->IsMannVsMachineMode() && pItemDef_MvMCanteen )
		{
			stockActionItemDefIndices.AddToTail( pItemDef_MvMCanteen->GetDefinitionIndex() );
		}
		
		// Traverse List
		for ( CEconItemView *pActionItem : m_pBaseLoadoutItems )
		{
			for ( item_definition_index_t defIndex : stockActionItemDefIndices )
			{
				if ( pActionItem->GetItemDefIndex() == defIndex )
					return pActionItem;
			}
		}
	}

	if ( iSlot >= LOADOUT_POSITION_HEAD )
		return m_pDefaultItem;

	// Traverse List
	FOR_EACH_VEC( m_pBaseLoadoutItems, iItem )
	{
		if ( m_pBaseLoadoutItems[iItem]->GetItemDefinition()->GetLoadoutSlot( iClass ) == iSlot )
			return m_pBaseLoadoutItems[iItem];
	}

	return m_pDefaultItem;
}

//-----------------------------------------------------------------------------
// Purpose: Fills out the vector with the sets that are currently active on the specified player & class
//-----------------------------------------------------------------------------
void CTFInventoryManager::GetActiveSets( CUtlVector<const CEconItemSetDefinition *> *pItemSets, CSteamID steamIDForPlayer, int iClass )
{
	pItemSets->Purge();

	CEconItemSchema *pSchema = ItemSystem()->GetItemSchema();
	if ( !pSchema )
		return;

	// Loop through all the items we have equipped, and build a list of only set items
	// Accumulate a list of our equipped set items.
	CUtlVector<CEconItemView*> equippedSetItems;
	for ( int i = 0; i < CLASS_LOADOUT_POSITION_COUNT; i++ )
	{
		CEconItemView *pItem = NULL;

#ifdef GAME_DLL
		// On the server we need to look at what the player actually has equipped
		// because they might be on a tournament server that's using an item_whitelist
		CTFPlayer *pPlayer = ToTFPlayer( GetPlayerBySteamID( steamIDForPlayer ) );
		if ( pPlayer && pPlayer->Inventory() )
		{
			pItem = pPlayer->GetEquippedItemForLoadoutSlot( i );
		}
#else
		pItem = TFInventoryManager()->GetItemInLoadoutForClass( iClass, i, &steamIDForPlayer );
#endif		

		if ( !pItem )
			continue;

		CEconItemDefinition* pData = pItem->GetStaticData();
		if ( !pData )
			continue;

		// Ignore items that don't have set bonuses.
		if ( !pData->GetItemSetDefinition() )
			continue;

		// Make sure this item isn't failing a Holiday restriction before giving out the set bonus!
		if ( pData->GetHolidayRestriction() )
		{
			int iHolidayRestriction = UTIL_GetHolidayForString( pData->GetHolidayRestriction() );
			if ( iHolidayRestriction != kHoliday_None && (!TFGameRules() || !TFGameRules()->IsHolidayActive( iHolidayRestriction )) )
				continue;
		}

		equippedSetItems.AddToTail( pItem );
	}

	// Find out which sets to apply.
	CUtlVector<const char*> testedSets;
	for ( int inv = 0; inv < equippedSetItems.Count(); inv++ )
	{
		CEconItemView *pItem = equippedSetItems[inv];
		if ( !pItem )
			continue;

		const CEconItemSetDefinition *pItemSet = pItem->GetStaticData()->GetItemSetDefinition();
		if ( !pItemSet )
			continue;

		if ( testedSets.HasElement( pItemSet->m_pszName ) )
			continue; // Don't try to apply set bonuses we have already tested.

		testedSets.AddToTail( pItemSet->m_pszName );

		// Count how much of this set we have equipped.
		int iSetItemsEquipped = 0;
		for ( int i=0; i<pItemSet->m_iItemDefs.Count(); i++ )
		{
			unsigned int iIndex = pItemSet->m_iItemDefs[i];

			for ( int j=0; j<equippedSetItems.Count(); j++ )
			{
				const CEconItemView *pTestItem = equippedSetItems[j];
				const item_definition_index_t unEquippedItemSetDefIndex = pTestItem->GetItemDefinition()->GetSetItemRemap();

				if ( iIndex == unEquippedItemSetDefIndex )
				{
					iSetItemsEquipped++;
					break;
				}
			}
		}

		// Note: this logic will break if we ever have sets that have misc-slot items where we can have multiple items
		// of the same type with different remaps equipped.
		if ( iSetItemsEquipped == pItemSet->m_iItemDefs.Count() )
		{
			// The entire set is equipped. 
			pItemSets->AddToTail( pItemSet );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: We're generating a base item. We need to add the game-specific keys to the criteria so that it'll find the right base item.
//-----------------------------------------------------------------------------
void CTFInventoryManager::AddBaseItemCriteria( baseitemcriteria_t *pCriteria, CItemSelectionCriteria *pSelectionCriteria )
{
	pSelectionCriteria->BAddCondition( "used_by_classes", k_EOperator_Subkey_Contains, ItemSystem()->GetItemSchema()->GetClassUsabilityStrings()[pCriteria->iClass], true );
	pSelectionCriteria->BAddCondition( "item_slot", k_EOperator_String_EQ, ItemSystem()->GetItemSchema()->GetLoadoutStrings( EEquipType_t::EQUIP_TYPE_CLASS )[pCriteria->iSlot], true );
}


//=======================================================================================================================
// TF PLAYER INVENTORY
//=======================================================================================================================
// Inventory Less function.
// Used to sort the inventory items into their positions.
class CTFInventoryListLess
{
public:
	bool Less( const CEconItemView &src1, const CEconItemView &src2, void *pCtx )
	{
		if ( src1.GetInventoryPosition() > src2.GetInventoryPosition() )
			return true;

		return false;
	}
};


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFPlayerInventory::CTFPlayerInventory()
{
	m_aInventoryItems.SetLessContext( this );
#ifdef CLIENT_DLL
	for ( int i = 0; i < TF_TEAM_COUNT; ++i ) 
		m_CachedBaseTextureLowRes[ i ].SetLessFunc( DefLessFunc( itemid_t ) );
#endif

	memset( m_LoadoutItems, LOADOUT_SLOT_USE_BASE_ITEM, sizeof( m_LoadoutItems ) );
	memset( m_AccountLoadoutItems, LOADOUT_SLOT_USE_BASE_ITEM, sizeof( m_AccountLoadoutItems ) );
	ClearClassLoadoutChangeTracking();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFPlayerInventory::~CTFPlayerInventory()
{
#ifdef CLIENT_DLL
	for ( int iTeam = 0; iTeam < TF_TEAM_COUNT; ++iTeam )
	{
		FOR_EACH_MAP_FAST( m_CachedBaseTextureLowRes[ iTeam ], i )
			m_CachedBaseTextureLowRes[ iTeam ][ i ]->Release();
		m_CachedBaseTextureLowRes[ iTeam ].RemoveAll();
	}
#endif
}


#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::CheckSaxtonMaskAchievement( const CEconItem *pEconItem )
{
	if ( pEconItem )
	{
		if ( pEconItem->GetDefinitionIndex() == 277 && pEconItem->GetOrigin() == kEconItemOrigin_Crafted )	// Saxton mask is item index 277
		{
			g_AchievementMgrTF.OnAchievementEvent( ACHIEVEMENT_TF_HALLOWEEN_CRAFT_SAXTON_MASK );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::UpdateCachedServerLoadoutItems()
{
	V_memcpy( m_CachedServerLoadoutItems, m_LoadoutItems, sizeof( itemid_t ) * ARRAYSIZE( m_CachedServerLoadoutItems ) * ARRAYSIZE( m_CachedServerLoadoutItems[0] ) );
}
#endif // CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::SOUpdated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
	BaseClass::SOUpdated( steamIDOwner, pObject, eEvent );

#ifdef CLIENT_DLL
	if ( pObject->GetTypeID() != CEconItem::k_nTypeID )
		return;

	// Clear out any predicted backpack slots when items move into them
	CEconItem *pEconItem = (CEconItem *)pObject;
	if ( eEvent == eSOCacheEvent_Incremental )
	{
		EconUI()->GetBackpackPanel()->MarkItemIDDirty( pEconItem->GetItemID() );
	}
	int iBackpackPos = TFInventoryManager()->GetBackpackPositionFromBackend( pEconItem->GetInventoryToken() );
	TFInventoryManager()->PredictedBackpackPosFilled( iBackpackPos );
#endif // CLIENT_DLL
}

void CTFPlayerInventory::OnHasNewItems()
{
	BaseClass::OnHasNewItems();
#ifdef CLIENT_DLL
	if ( TFGameRules() && TFGameRules()->IsInTraining() )
		return;

	NotificationQueue_Remove( &CEconNotification_HasNewItems::IsNotificationType );
	CEconNotification_HasNewItems *pNotification = new CEconNotification_HasNewItems();
	pNotification->SetText( "TF_HasNewItems" );
	pNotification->SetLifetime( 7.0f );
	NotificationQueue_Add( pNotification );	
#endif
}

void CTFPlayerInventory::OnHasNewQuest()
{
#ifdef CLIENT_DLL

#endif
}

#ifdef _DEBUG
#ifdef CLIENT_DLL
CON_COMMAND( cl_newitem_test, "Tests the new item ui notification." )
{
	if ( steamapicontext == NULL || steamapicontext->SteamUser() == NULL )
		return;

	CEconNotification_HasNewItems *pNotification = new CEconNotification_HasNewItems();
	pNotification->SetText( "TF_HasNewItems" );
	pNotification->SetLifetime( 7.0f );
	NotificationQueue_Add( pNotification );
}
#endif
#endif // _DEBUG

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::ValidateInventoryPositions( void )
{
	BaseClass::ValidateInventoryPositions();

#ifdef CLIENT_DLL
	bool bHasNewItems = false;
	const int iMaxItems = GetMaxItemCount();
	// First, check for duplicate positions
	int iCount = m_aInventoryItems.Count();
	for ( int i = iCount-1; i >= 0; i-- )
	{
		CEconItemView *pEconItemView = &m_aInventoryItems[i];

		CheckSaxtonMaskAchievement( pEconItemView->GetSOCData() );

		uint32 iPosition = pEconItemView->GetInventoryPosition();

		// Waiting to be acknowledged?
		if ( IsUnacknowledged(iPosition) )
		{
			if ( !pEconItemView->GetStaticData()->IsHidden() )
			{
				bHasNewItems = true;
			}
			continue;
		}

		bool bInvalidSlot = false;
		if ( !IsNewPositionFormat(iPosition) )
		{
			ConvertOldFormatInventoryToNew();
			break;
		}

		// Inside the backpack?
		if ( i < (iCount-1) )
		{
			// We're not in an invalid slot yet. But if we're in the same position as another item, we should be moved too.
			int iPos1 = TFInventoryManager()->GetBackpackPositionFromBackend(iPosition);
			int iPos2 = TFInventoryManager()->GetBackpackPositionFromBackend(m_aInventoryItems[i+1].GetInventoryPosition());
			if ( iPos1 == iPos2 )
			{
				Warning("WARNING: Found item in a duplicate backpack position. Moving to the backpack end.\n" );
				bInvalidSlot = true;
			}
		}

		// Make sure it's not outside the backpack extents
		if ( !bInvalidSlot )
		{
			bInvalidSlot = (TFInventoryManager()->GetBackpackPositionFromBackend(iPosition) > iMaxItems);
		}

		if ( bInvalidSlot  )
		{
			// The item is NOT hidden and is in an invalid slot. Move it back to the backpack.
			if ( pEconItemView->GetItemDefinition() && !pEconItemView->GetItemDefinition()->IsHidden() && !TFInventoryManager()->SetItemBackpackPosition( pEconItemView, 0, true ) )
			{
				// We failed to move it to the backpack, because the player has no room.
				// Force them to "refind" the item, which will make them throw something out.
				TFInventoryManager()->UpdateInventoryPosition( this, pEconItemView->GetItemID(), GetUnacknowledgedPositionFor(UNACK_ITEM_DROPPED) );
			}
		}

		// Make sure it isn't equipped by any invalid classes.
		for ( int j = TF_FIRST_NORMAL_CLASS; j <= TF_LAST_NORMAL_CLASS; j++ )
		{
			if ( !pEconItemView->GetStaticData()->CanBeUsedByClass( j ) &&
				 pEconItemView->IsEquippedForClass( j ) )
			{
				// Unequip this item from this class.
				InventoryManager()->UpdateInventoryEquippedState( this, INVALID_ITEM_ID, j, pEconItemView->GetEquippedPositionForClass( j ) );
			}
		}
	}

	if ( bHasNewItems )
	{
		OnHasNewItems();
	}
#endif
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::ConvertOldFormatInventoryToNew( void )
{
	uint32 iBackpackPos = 1;

	// Loop through all items in the inventory. Move them all to the backpack, and in order.
	int iCount = m_aInventoryItems.Count();
	for ( int i = 0; i < iCount; i++ )
	{
		uint32 iPosition = m_aInventoryItems[i].GetInventoryPosition();

		// Waiting to be acknowledged?
		if ( IsUnacknowledged(iPosition) )
			continue;

		TFInventoryManager()->SetItemBackpackPosition( &m_aInventoryItems[i], iBackpackPos, true );
		iBackpackPos++;
	}
}
#endif

//-----------------------------------------------------------------------------
// Purpose: Creates a script item and associates it with this econ item
//-----------------------------------------------------------------------------
void CTFPlayerInventory::SOCreated( const CSteamID & steamIDOwner, const GCSDK::CSharedObject *pObject, GCSDK::ESOCacheEvent eEvent )
{
	BaseClass::SOCreated( steamIDOwner, pObject, eEvent );

	if ( pObject->GetTypeID() != CEconItem::k_nTypeID )
		return;

	CEconItem *pEconItem = (CEconItem *)pObject;
 //	CEconItem *pEconItem = assert_cast<CEconItem*>( pObject );

#ifdef CLIENT_DLL
	if ( InventoryManager()->GetLocalInventory() == this && GetOwner() == steamIDOwner )
	{
		if ( pObject->GetTypeID() == CEconItem::k_nTypeID )
		{
			CheckSaxtonMaskAchievement( (CEconItem*)pObject );
		}
	}

//	CSteamID ownerSteamID( pEconItem->GetAccountID(), GetUniverse(), k_EAccountTypeIndividual );
//	if ( ownerSteamID == ClientSteamContext().GetLocalPlayerSteamID() )
//	{
//		CheckSaxtonMaskAchievement( pEconItem );
//	}

	static CSchemaItemDefHandle pItemDef_HardyLaurel( "The Hardy Laurel" );
	if ( pEconItem->GetItemDefinition() == pItemDef_HardyLaurel )
	{
		if ( TFSharedContentManager() )
		{
			TFSharedContentManager()->OfferSharedVision( TF_VISION_FILTER_ROME, pEconItem->GetAccountID() );
		}
	}
 #else
	// Summer 2015 Operation Pass so players can display the coin
	//tagES remove this when we have a coin equip slot
	static CSchemaItemDefHandle pItemDef_Summer2015Operation( "Activated Summer 2015 Operation Pass" );
	if ( pEconItem->GetItemDefinition() == pItemDef_Summer2015Operation )
	{
		CTFPlayer *pPlayer = ToTFPlayer( GetPlayerBySteamID( GetOwner() ) );
		if ( pPlayer )
		{
			pPlayer->SetCampaignMedalActive( CAMPAIGN_MEDAL_SUMMER2015 );
		}
	}

	// Invasion Community Update Pass so players can display the coin
	//tagES remove this when we have a coin equip slot
	static CSchemaItemDefHandle pItemDef_InvasionPass( "Activated Invasion Pass" );
	if ( pEconItem->GetItemDefinition() == pItemDef_InvasionPass )
	{
		CTFPlayer *pPlayer = ToTFPlayer( GetPlayerBySteamID( GetOwner() ) );
		if ( pPlayer )
		{
			pPlayer->SetCampaignMedalActive( CAMPAIGN_MEDAL_INVASION );
		}
	}

	// Halloween Pass so players can display the coin
	//tagES remove this when we have a coin equip slot
	static CSchemaItemDefHandle pItemDef_HalloweenPass( "Activated Halloween Pass" );
	if ( pEconItem->GetItemDefinition() == pItemDef_HalloweenPass )
	{
		CTFPlayer *pPlayer = ToTFPlayer( GetPlayerBySteamID( GetOwner() ) );
		if ( pPlayer )
		{
			pPlayer->SetCampaignMedalActive( CAMPAIGN_MEDAL_HALLOWEEN );
		}
	}

	// Winter2016 Pass so players can display the stamp
	//tagES remove this when we have a coin equip slot
	static CSchemaItemDefHandle pItemDef_Winter2016Pass( "Activated Operation Tough Break Pass" );
	if ( pEconItem->GetItemDefinition() == pItemDef_Winter2016Pass )
	{
		CTFPlayer *pPlayer = ToTFPlayer( GetPlayerBySteamID( GetOwner() ) );
		if ( pPlayer )
		{
			pPlayer->SetCampaignMedalActive( CAMPAIGN_MEDAL_WINTER2016 );
		}
	}
#endif
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::DumpInventoryToConsole( bool bRoot )
{
	if ( bRoot )
	{
		Msg("========================================\n");
#ifdef CLIENT_DLL
		Msg("(CLIENT) Inventory:\n");
#else
		Msg("(SERVER) Inventory for account (%d):\n", m_OwnerID.GetAccountID() );
#endif
		Msg("  Version: %llu:\n", m_pSOCache ? m_pSOCache->GetVersion() : -1 );
	}

	int iCount = m_aInventoryItems.Count();
	Msg("   Num items: %d\n", iCount );
	for ( int i = 0; i < iCount; i++ )
	{
		Msg("      %s (ID %llu) at backpack slot %d\n", m_aInventoryItems[i].GetStaticData()->GetDefinitionName(), m_aInventoryItems[i].GetItemID(), TFInventoryManager()->GetBackpackPositionFromBackend( m_aInventoryItems[i].GetInventoryPosition() ) );

		int iEquipped = 0;
		for ( equipped_class_t eq = TF_FIRST_NORMAL_CLASS; eq < TF_LAST_NORMAL_CLASS; eq++ )
		{
			if ( m_aInventoryItems[i].IsEquippedForClass( eq ) )
			{
				if ( iEquipped == 0 )
				{
					iEquipped++;
					Msg("         -> EQUIPPED: ");
				}
				Msg("%s ", g_aPlayerClassNames_NonLocalized[eq] );
			}
		}

		if ( iEquipped )
		{
			Msg("\n");
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::ClearClassLoadoutChangeTracking( void )
{
	memset(m_bLoadoutChanged,0, sizeof(m_bLoadoutChanged));
#ifdef CLIENT_DLL
	UpdateCachedServerLoadoutItems();
#endif // CLIENT_DLL
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: Find the low res 
//-----------------------------------------------------------------------------
ITexture *CTFPlayerInventory::GetWeaponSkinBaseLowRes( itemid_t nItemId, int iTeam ) const
{
	Assert( iTeam >= 0 && iTeam < TF_TEAM_COUNT );
	int index = m_CachedBaseTextureLowRes[ iTeam ].Find( nItemId );
	if ( index == m_CachedBaseTextureLowRes[ iTeam ].InvalidIndex() )
		return NULL;

	return m_CachedBaseTextureLowRes[ iTeam ][ index ];
}
#endif

//-----------------------------------------------------------------------------
// Purpose: Find the first item in the local user's inventory with the given def index
//-----------------------------------------------------------------------------
CEconItemView *CTFPlayerInventory::GetFirstItemOfItemDef( item_definition_index_t nDefIndex, CPlayerInventory* pInventory )
{
#ifdef CLIENT_DLL
	if ( pInventory == NULL )
	{
		pInventory = InventoryManager()->GetLocalInventory();
	}
#endif

	if ( !pInventory )
		return NULL;

	for ( int i = 0; i < pInventory->GetItemCount(); i++ )
	{
		CEconItemView *pItem = pInventory->GetItem(i);
		if ( pItem->GetItemDefIndex() == nDefIndex )
		{
			return pItem;
		}
	}

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Returns the item in the specified loadout slot for a given class
//-----------------------------------------------------------------------------
CEconItemView *CTFPlayerInventory::GetItemInLoadout( int iClass, int iSlot )
{
	if ( iSlot < 0 || iSlot >= CLASS_LOADOUT_POSITION_COUNT )
		return NULL;

	if ( iClass == GEconItemSchema().GetAccountIndex() )
	{
		return GetInventoryItemByItemID( m_AccountLoadoutItems[ iSlot ] );
	}
	else
	{
		if ( iClass < TF_FIRST_NORMAL_CLASS || iClass >= TF_LAST_NORMAL_CLASS  )
			return NULL;

		// If we don't have an item in the loadout at that slot, we return the base item
		if ( m_LoadoutItems[iClass][iSlot] != LOADOUT_SLOT_USE_BASE_ITEM )
		{
			CEconItemView *pItem = GetInventoryItemByItemID( m_LoadoutItems[iClass][iSlot] );

			// To protect against users lying to the backend about the position of their items,
			// we need to validate their position on the server when we retrieve them.
			if ( pItem && AreSlotsConsideredIdentical( pItem->GetStaticData()->GetEquipType(), pItem->GetStaticData()->GetLoadoutSlot( iClass ), iSlot ) )
				return pItem;
		}
	}

	return TFInventoryManager()->GetBaseItemForClass( iClass, iSlot );
}

#ifdef CLIENT_DLL
CEconItemView *CTFPlayerInventory::GetCacheServerItemInLoadout( int iClass, int iSlot )
{
	if ( iSlot < 0 || iSlot >= CLASS_LOADOUT_POSITION_COUNT )
		return NULL;
	if ( iClass < TF_FIRST_NORMAL_CLASS || iClass >= TF_LAST_NORMAL_CLASS )
		return NULL;

	// If we don't have an item in the loadout at that slot, we return the base item
	if ( m_CachedServerLoadoutItems[iClass][iSlot] != LOADOUT_SLOT_USE_BASE_ITEM )
	{
		CEconItemView *pItem = GetInventoryItemByItemID( m_CachedServerLoadoutItems[iClass][iSlot] );

		// To protect against users lying to the backend about the position of their items,
		// we need to validate their position on the server when we retrieve them.
		if ( pItem && AreSlotsConsideredIdentical( pItem->GetStaticData()->GetEquipType(), pItem->GetStaticData()->GetLoadoutSlot( iClass ), iSlot ) )
			return pItem;
	}

	return TFInventoryManager()->GetBaseItemForClass( iClass, iSlot );
}
#endif // CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
static CEconGameAccountClient *GetSOCacheGameAccountClient( CGCClientSharedObjectCache *pSOCache )
{
	if ( !pSOCache )
		return NULL;

	return pSOCache->GetSingleton<CEconGameAccountClient>();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CTFPlayerInventory::GetPreviewItemDef( void ) const
{
	CEconGameAccountClient *pGameAccountClient = GetSOCacheGameAccountClient( m_pSOCache );
	if ( !pGameAccountClient  )
		return 0;

	return pGameAccountClient->Obj().preview_item_def();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFPlayerInventory::CanPurchaseItems( int iItemCount ) const
{
	// If we're not a free trial account, we fall back to our default logic of "do
	// we have enough empty slots?".
	CEconGameAccountClient *pGameAccountClient = GetSOCacheGameAccountClient( m_pSOCache );
	if ( !pGameAccountClient || !pGameAccountClient->Obj().trial_account() )
		return BaseClass::CanPurchaseItems( iItemCount );

	// We're a free trial account, so when we purchase these items, our inventory
	// will actually expand. We check to make sure that we have room for these
	// items against what will be our new maximum backpack size, not our current
	// backpack limit.
	int iNewItemCount			   = GetItemCount() + iItemCount,
		iAfterPurchaseMaxItemCount = DEFAULT_NUM_BACKPACK_SLOTS
								   + (pGameAccountClient ? pGameAccountClient->Obj().additional_backpack_slots() : 0);

	return iNewItemCount <= iAfterPurchaseMaxItemCount;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int	CTFPlayerInventory::GetMaxItemCount( void ) const
{
	int iMaxItems = DEFAULT_NUM_BACKPACK_SLOTS;
	CEconGameAccountClient *pGameAccountClient = GetSOCacheGameAccountClient( m_pSOCache );
	if ( pGameAccountClient )
	{
		if ( pGameAccountClient->Obj().trial_account() )
		{
			iMaxItems = DEFAULT_NUM_BACKPACK_SLOTS_FREE_TRIAL_ACCOUNT;
		}
		iMaxItems += pGameAccountClient->Obj().additional_backpack_slots();
	}
	return MIN( iMaxItems, MAX_NUM_BACKPACK_SLOTS );
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: Removes any item in a loadout slot. If the slot has a base item,
//			the player essentially returns to using that item.
//-----------------------------------------------------------------------------
bool CTFPlayerInventory::ClearLoadoutSlot( int iClass, int iSlot )
{
	if ( iSlot < 0 || iSlot >= CLASS_LOADOUT_POSITION_COUNT )
		return false;

	if ( iClass == GEconItemSchema().GetAccountIndex() )
	{
		if ( m_AccountLoadoutItems[iSlot] == LOADOUT_SLOT_USE_BASE_ITEM )
			return false;
	}
	else
	{
		if ( iClass < TF_FIRST_NORMAL_CLASS || iClass >= TF_LAST_NORMAL_CLASS )
			return false;

		if ( m_LoadoutItems[iClass][iSlot] == LOADOUT_SLOT_USE_BASE_ITEM )
			return false;
	}
	
	CEconItemView *pItemInSlot = GetItemInLoadout( iClass, iSlot );
	if ( !pItemInSlot )
		return false;

	InventoryManager()->UpdateInventoryEquippedState( this, INVALID_ITEM_ID, iClass, iSlot );

	// TODO: Prediction
	// It's been moved to the backpack, so clear out loadout entry
	//m_LoadoutItems[iClass][iSlot] = LOADOUT_SLOT_USE_BASE_ITEM;
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Update weapon skin request list
//-----------------------------------------------------------------------------
void CTFPlayerInventory::UpdateWeaponSkinRequest()
{
	TM_ZONE_DEFAULT( TELEMETRY_LEVEL0 );

	FOR_EACH_VEC_BACK( m_vecWeaponSkinRequestList, i )
	{
		SkinRequest_t &req = m_vecWeaponSkinRequestList[i];
		CEconItemView *pItem = GetInventoryItemByItemID( req.m_nID );
		Assert( pItem );
		if ( !pItem ) 
		{
			m_vecWeaponSkinRequestList.Remove( i );
			continue;
		}

		// Have we loaded this one yet? If not, do it here.
		if ( mdlcache->IsErrorModel( req.m_hModel ) )
		{
			const char *pszModelName = pItem->GetPlayerDisplayModel( 0, 0 );
			if ( pszModelName )
			{
				MDLHandle_t hItemMDL = mdlcache->FindMDL( pszModelName );
				if ( mdlcache->IsErrorModel( hItemMDL ) )
				{
					AssertMsg( 0, "failed to find %s from mdlcache", pszModelName );
					m_vecWeaponSkinRequestList.Remove( i );
					continue;
				}

				req.m_hModel = hItemMDL;
			}
			else
			{
				AssertMsg( 0, "failed to precache weapon skin because there's no model" );
				m_vecWeaponSkinRequestList.Remove( i );
			}
		}
		
		// Draw!
		{
			CMDL mdl;
			mdl.SetMDL( req.m_hModel );
			mdl.m_pProxyData = static_cast<IClientRenderable*>( pItem );

			pItem->SetWeaponSkinUseLowRes( true );
			int nRestoreTeam = pItem->GetTeamNumber();
			pItem->SetTeamNumber( req.m_nTeam );

			matrix3x4_t matIdentity;
			SetIdentityMatrix( matIdentity );
			IMaterial *pOverrideMaterial = pItem->GetMaterialOverride( pItem->GetTeamNumber() );

			if ( pOverrideMaterial )
				modelrender->ForcedMaterialOverride( pOverrideMaterial );

			mdl.Draw( matIdentity );

			if ( pOverrideMaterial )
				modelrender->ForcedMaterialOverride( NULL );

			pItem->SetTeamNumber( nRestoreTeam );
			pItem->SetWeaponSkinUseLowRes( false );
		}

		// Don't remove until it's complete. 
		if ( pItem->GetWeaponSkinBase() )
		{
			Assert( pItem->GetWeaponSkinBaseCompositor() == NULL );
			ITexture* pTex = pItem->GetWeaponSkinBase();
			if ( pTex )
			{
				pTex->AddRef(); // We need to hold a ref to the texture.
				m_CachedBaseTextureLowRes[ req.m_nTeam ].Insert( req.m_nID, pTex );
				pItem->SetWeaponSkinBase( NULL ); // Clean up.
			}

			// We do RED, then BLUE. Then drop it out of the list. 
			if ( req.m_nTeam == TF_TEAM_BLUE )
			{
				Assert( !mdlcache->IsErrorModel( req.m_hModel ) );
				mdlcache->Release( req.m_hModel );
				m_vecWeaponSkinRequestList.Remove( i );
			}
			else
			{
				Assert( req.m_nTeam == TF_TEAM_RED );
				req.m_nTeam = TF_TEAM_BLUE;
			}
		}

		// Only do one of these per frame to avoid hitching.
		break;
	}
}

#endif // CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::ItemHasBeenUpdated( CEconItemView *pItem, bool bUpdateAckFile, bool bWriteAckFile )
{
	Assert( pItem );

	BaseClass::ItemHasBeenUpdated( pItem, bUpdateAckFile, bWriteAckFile );
	const CEconItem *pEconItem = pItem->GetSOCData();

#ifdef CLIENT_DLL
	static CSchemaAttributeDefHandle pAttrib_WeaponAllowInspect( "weapon_allow_inspect" );
	bool bLocalInv = InventoryManager()->GetLocalInventory() == this;
#endif // CLIENT_DLL

	for ( equipped_class_t iClass = TF_FIRST_NORMAL_CLASS; iClass < TF_LAST_NORMAL_CLASS; iClass++ )
	{
		equipped_slot_t unSlot = pEconItem ? pEconItem->GetEquippedPositionForClass( iClass ) : INVALID_EQUIPPED_SLOT;

		Assert( GetInventoryItemByItemID( pItem->GetItemID() ) );

		bool bThisClassLoadoutChanged = UpdateEquipStateForClass( pItem->GetItemID(), unSlot, m_LoadoutItems[iClass], ARRAYSIZE( m_LoadoutItems[iClass] ) );

		if ( bThisClassLoadoutChanged )
		{
			m_bLoadoutChanged[iClass] = true;

#ifdef CLIENT_DLL
			// if we can inspect this item, it has unique skin.
			// draw it once per team to tell the system to generate unique skin
			float flInspect = 0;
			if ( bLocalInv && FindAttribute_UnsafeBitwiseCast<attrib_value_t>( pItem, pAttrib_WeaponAllowInspect, &flInspect ) )
			{
				SkinRequest_t req = { TF_TEAM_RED, pItem->GetItemID(), MDLHANDLE_INVALID };
				m_vecWeaponSkinRequestList.AddToTail( req );
			}
#endif // CLIENT_DLL
		}
	}

	// Update account items as well
	equipped_slot_t unSlot = pEconItem ? pEconItem->GetEquippedPositionForClass( GEconItemSchema().GetAccountIndex() ) : INVALID_EQUIPPED_SLOT;
	UpdateEquipStateForClass( pItem->GetItemID(), unSlot, m_AccountLoadoutItems, ARRAYSIZE( m_AccountLoadoutItems ) );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFPlayerInventory::UpdateEquipStateForClass( const itemid_t& itemID, equipped_slot_t nSlot, itemid_t *pLoadout, int nCount )
{
	bool bThisClassLoadoutChanged = false;

	// For each slot this item may have been equipped in, set to the base item, unless that slot
	// is the current slot. (ie., if an item moves from slot 10 to slot 12, slot 10 will be set to
	// base item, and slot 12 will be set to this item ID)
	for ( int i = 0; i < nCount; i++ )
	{
		itemid_t& refLoadoutItemID = *( pLoadout + i );

		if ( i == nSlot )
		{
			// This may be an invalid slot for the item, but CTFPlayerInventory::InventoryReceived()
			// will have detected that and sent off a request already to move it. The response
			// to that will clear this loadout slot.
			refLoadoutItemID = itemID;
			bThisClassLoadoutChanged = true;
		}
		else if ( refLoadoutItemID == itemID )
		{
			refLoadoutItemID = LOADOUT_SLOT_USE_BASE_ITEM;
			bThisClassLoadoutChanged = true;
		}
	}

	return bThisClassLoadoutChanged;
}

#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::PostSOUpdate( const CSteamID & steamIDOwner, GCSDK::ESOCacheEvent eEvent )
{
	BaseClass::PostSOUpdate( steamIDOwner, eEvent );

	VerifyChangedLoadoutsAreValid();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::SOCacheSubscribed( const CSteamID & steamIDOwner, GCSDK::ESOCacheEvent eEvent )
{
	BaseClass::SOCacheSubscribed( steamIDOwner, eEvent );

	VerifyChangedLoadoutsAreValid();
	UpdateCachedServerLoadoutItems();
}

//-----------------------------------------------------------------------------
// Purpose: Helper function to add a new item for a econ item
//-----------------------------------------------------------------------------
bool CTFPlayerInventory::AddEconItem( CEconItem * pItem, bool bUpdateAckFile, bool bWriteAckFile, bool bCheckForNewItems )
{
	if ( BaseClass::AddEconItem( pItem, bUpdateAckFile, bWriteAckFile, bCheckForNewItems ) )
	{
		if ( bCheckForNewItems && InventoryManager()->GetLocalInventory() == this )
		{
			if ( IsUnacknowledged( pItem->GetInventoryToken() ) )
			{
				OnHasNewQuest();
			}
		}

		return true;
	}

	return false;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::VerifyLoadoutItemsAreValid( int iClass )
{
	// Important note: currently this function walks the loadout slots in order causing slots towards
	// the beginning of the list to take priority over slots later in the list. This means that currently
	// weapons take priority over hats, hats take priority over misc slots, etc. which is all well and good.
	// If later we want the order in which slots claim their equip regions to change, we'll want to change
	// the iteration order here and also change GenerateEquipRegionMaskUpToSlot(), which is used for
	// filling out the UI.
	equip_region_mask_t unCumulativeRegionMask = 0;
	for ( int i = 0; i < CLASS_LOADOUT_POSITION_COUNT; i++ )
	{
		CEconItemView *pEquippedItemView = GetItemInLoadout( iClass, i );
		if ( !pEquippedItemView )
			continue;

		// Does this item use the same regions as some item that we already have equipped?
		equip_region_mask_t unItemEquipMask = pEquippedItemView->GetItemDefinition()->GetEquipRegionMask();
		if ( unItemEquipMask & unCumulativeRegionMask )
		{
			// Unequip this item. This will wind up calling into ::ItemHasBeenUpdated() once the
			// unequip makes it to the GC and back.
			InventoryManager()->UpdateInventoryEquippedState( this, INVALID_ITEM_ID, iClass, pEquippedItemView->GetEquippedPositionForClass( iClass ) );
		}
		else
		{
			unCumulativeRegionMask |= unItemEquipMask;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::VerifyChangedLoadoutsAreValid()
{
	// We maybe changed equip state for an item and it's possible if we're sending bad messages and/or
	// hacking state that we'll now have conflicting items equipped. Walk all of our items for this class
	// to verify our state is good.
	for ( equipped_class_t iClass = TF_FIRST_NORMAL_CLASS; iClass < TF_LAST_NORMAL_CLASS; iClass++ )
	{
		if ( m_bLoadoutChanged[iClass] )
		{
			VerifyLoadoutItemsAreValid( iClass );
		}
	}
}
#endif // CLIENT_DLL

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFPlayerInventory::ItemIsBeingRemoved( CEconItemView *pItem )
{
	for ( int iClass = TF_FIRST_NORMAL_CLASS; iClass < TF_LAST_NORMAL_CLASS; iClass++ )
	{
		if ( pItem->IsEquippedForClass( iClass ) )
		{
			for ( int iSlot = 0; iSlot < CLASS_LOADOUT_POSITION_COUNT; iSlot++ )
			{
				if ( m_LoadoutItems[iClass][iSlot] == pItem->GetItemID() )
				{
					m_LoadoutItems[iClass][iSlot] = LOADOUT_SLOT_USE_BASE_ITEM;
					m_bLoadoutChanged[iClass] = true;
				}
			}
		}
	}
}


//=======================================================================================================================
// TF PLAYER INVENTORY
//=======================================================================================================================

#ifdef CLIENT_DLL
// WTF: Declaring this inline caused a compiler bug.
CTFPlayerInventory	*CTFInventoryManager::GetLocalTFInventory( void ) 
{ 
	return &m_LocalInventory; 
}
#endif

#ifdef _DEBUG
#if defined(CLIENT_DLL)
CON_COMMAND_F( item_dumpinv_other, "Dumps the contents of a specified client inventory. Format: item_dumpinv_other <player index>", FCVAR_NONE )
{
	if ( args.ArgC() > 1 )
	{
		C_TFPlayer *pOther = ToTFPlayer( UTIL_PlayerByIndex( atoi(args[1]) ) );
		if ( pOther )
		{
			pOther->Inventory()->DumpInventoryToConsole( true );
			return;
		}
	}
	Msg("Couldn't find specified player.\nFormat: item_dumpinv_other <player index>\n");
}
#endif
#endif // _DEBUG

#ifdef _DEBUG
#if defined(CLIENT_DLL)
CON_COMMAND_F( item_dumpinv, "Dumps the contents of a specified client inventory. Format: item_dumpinv", FCVAR_CHEAT )
#else
CON_COMMAND_F( item_dumpinv_sv, "Dumps the contents of a specified server inventory. Format: item_dumpinv_sv", FCVAR_CHEAT )
#endif
{
#if defined(CLIENT_DLL)
	CSteamID steamID = steamapicontext->SteamUser()->GetSteamID();
#else
	CSteamID steamID;
	CTFPlayer *pPlayer = ToTFPlayer( UTIL_GetCommandClient() ); 
	pPlayer->GetSteamID( &steamID );
#endif

	CPlayerInventory *pInventory = TFInventoryManager()->GetInventoryForPlayer( steamID );
	if ( !pInventory )
	{
		Msg("No inventory for that player.\n");
		return;
	}

	pInventory->DumpInventoryToConsole( true );
}
#endif

#ifdef _DEBUG
#if defined(CLIENT_DLL)
CON_COMMAND_F( item_deleteunknowns, "Deletes all items in your inventory that we don't have static data for. Useful for removing items that have been removed from the backend.", FCVAR_CHEAT )
{
	int iDeleted = TFInventoryManager()->DeleteUnknowns( InventoryManager()->GetLocalInventory() );
	Msg("Deleted %d unknown items.\n", iDeleted);
}
#endif
#endif


#if defined( TF_CLIENT_DLL )
CON_COMMAND( load_itempreset, "Equip all items for a given preset on the player." )
{
	C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pPlayer )
		return;

	if ( args.ArgC() != 2 )
	{
		Msg( "Usage: \"load_itempreset <preset index>\" - <preset index> can be 0 to 3." );
		return;
	}

	if ( pPlayer->m_Shared.InCond( TF_COND_TAUNTING ) )
		return;

	equipped_class_t unClass = pPlayer->GetPlayerClass()->GetClassIndex();
	equipped_preset_t unPreset = atoi( args[1] );
	if ( TFInventoryManager()->LoadPreset( unClass, unPreset ) )
	{
		// Tell the GC to tell server that we should respawn if we're in a respawn room
		extern ConVar tf_respawn_on_loadoutchanges;
		if ( tf_respawn_on_loadoutchanges.GetBool() )
		{
			GCSDK::CGCMsg< ::MsgGCEmpty_t > msg( k_EMsgGCRespawnPostLoadoutChange );
			GCClientSystem()->BSendMessage( msg );
		}
	}
}
#endif	// TF_CLIENT_DLL