summaryrefslogtreecommitdiff
path: root/hammer/mapclass.cpp
blob: 22e6802546bb4da5508a32540f026948052eef50 (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
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "stdafx.h"
#include "ChunkFile.h"
#include "SaveInfo.h"
#include "MapClass.h"
#include "MapEntity.h"			// dvs: evil - base knows about the derived class
#include "MapGroup.h"			// dvs: evil - base knows about the derived class
#include "MapWorld.h"			// dvs: evil - base knows about the derived class
#include "GlobalFunctions.h"
#include "MapDoc.h"
#include "VisGroup.h"
#include "mapdefs.h"
#include "tier0/minidump.h"

int CMapAtom::s_nObjectIDCtr = 1;

static CUtlVector<MCMSTRUCT> s_Classes;

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


bool CMapClass::s_bLoadingVMF = false;


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : Type - 
//			pfnNew - 
//-----------------------------------------------------------------------------
CMapClassManager::CMapClassManager(MAPCLASSTYPE Type, CMapClass *(*pfnNew)())
{

	MCMSTRUCT mcms;
	mcms.Type = Type;
	mcms.pfnNew = pfnNew;
	s_Classes.AddToTail(mcms);
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CMapClassManager::~CMapClassManager(void)
{
	s_Classes.RemoveAll();
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : Type - 
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapClassManager::CreateObject(MAPCLASSTYPE Type)
{
	unsigned uLen = strlen(Type)+1;
	for (int i = s_Classes.Count() - 1; i >= 0; i--)
	{
		MCMSTRUCT &mcms = s_Classes[i];
		if (!memcmp(mcms.Type, Type, uLen))
		{
			return (*mcms.pfnNew)();
		}
	}

	Assert(FALSE);
	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Constructor. Initializes data members.
//-----------------------------------------------------------------------------
CMapClass::CMapClass(void)
{
	m_pSafeObject = CSafeObject<CMapClass>::Create( this );
	
	//
	// The document manages the unique object IDs. Eventually all object construction
	// should be done through the document, eliminating the need for CMapClass to know
	// about CMapDoc.
	//
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if (pDoc != NULL)
	{
		m_nID = pDoc->GetNextMapObjectID();
	}
	else
	{
		m_nID = 0;
	}

	dwKept = 0;
	m_bTemporary = FALSE;

	m_bVisible = true;
	m_bVisible2D = true;
	m_bVisGroupShown = true;
	m_bVisGroupAutoShown = true;
	m_pColorVisGroup = NULL;

	r = g = b = 220;
	m_pParent = NULL;
	m_nRenderFrame = 0;
	m_pEditorKeys = NULL;
	m_Dependents.Purge();
}


//-----------------------------------------------------------------------------
// Purpose: Destructor. Deletes all children.
//-----------------------------------------------------------------------------
CMapClass::~CMapClass(void)
{
	// Delete all of our children.
	m_Children.PurgeAndDeleteElements();

	delete m_pEditorKeys;
	
	// In case any CMapDocs are pointing at us, let them know we're gone.
	m_pSafeObject->m_pObject = NULL;
	
	// Show a warning if anyone is left pointing at us.
	static bool bCheckSafeObjects = true;
	if ( bCheckSafeObjects && m_pSafeObject->GetRefCount() != 1 )
	{
		int ret = AfxMessageBox(	"Warning: a CMapClass is being deleted but is still referenced by a CMapDoc.\n"
									"Please tell a programmer.\n"
									"Click Yes to write a minidump and continue.\n"
									"Click No to ignore.", 
						MB_YESNO );
		
		if ( ret == IDYES )
		{
			WriteMiniDump();
		}
		else if ( ret == IDNO )
		{
			// Ignore it and don't get in here again.
			bCheckSafeObjects = false;
		}		
	}
}


const CSmartPtr< CSafeObject< CMapClass > >& CMapClass::GetSafeObjectSmartPtr()
{
	return m_pSafeObject;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pDependent - 
//-----------------------------------------------------------------------------
void CMapClass::AddDependent(CMapClass *pDependent)
{
	//
	// Never add ourselves to our dependents. It creates a circular dependency
	// which is bad.
	//
	if (pDependent == this)
		return;

	//
	// Don't add the same dependent twice.
	//
	int nIndex = m_Dependents.Find(pDependent);
	if (nIndex != -1)
		return;

	//
	// Also, never add one of our ancestors as a dependent. This too creates a
	// nasty circular dependency.
	//
	bool bIsOurAncestor = false;
	CMapClass *pTestParent = GetParent();
	while (pTestParent != NULL)
	{
		if (pTestParent == pDependent)
		{
			bIsOurAncestor = true;
			break;
		}

		pTestParent = pTestParent->GetParent();
	}

	if (!bIsOurAncestor)
	{
		m_Dependents.AddToTail(pDependent);
		Assert(m_Dependents.Count() < 1000);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns a copy of this object. We should never call this implementation
//			since CMapClass cannot be instantiated.
// Input  : bUpdateDependencies - Whether to update object dependencies when copying object pointers.
//-----------------------------------------------------------------------------
CMapClass *CMapClass::Copy(bool bUpdateDependencies)
{
	Assert(FALSE);
	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Turns this object into a duplicate of the given object.
// Input  : pFrom - The object to replicate.
// Output : Returns a pointer to this object.
//-----------------------------------------------------------------------------
CMapClass *CMapClass::CopyFrom(CMapClass *pFrom, bool bUpdateDependencies)
{
	// Copy CMapPoint stuff. dvs: should be in CMapPoint implementation!
	m_Origin = pFrom->m_Origin;
	    
	//
	// Copy CMapClass stuff.
	//
	int nVisGroupCount = pFrom->GetVisGroupCount();
	for (int nVisGroup = 0; nVisGroup < nVisGroupCount; nVisGroup++)
	{
		CVisGroup *pVisGroup = pFrom->GetVisGroup(nVisGroup);
		if (!pVisGroup->IsAutoVisGroup())
		{
			AddVisGroup(pVisGroup);
		}
	}

	//m_bVisible = pFrom->m_bVisible;
	//m_bVisGroupShown = pFrom->m_bVisGroupShown;
	m_bTemporary = pFrom->m_bTemporary;
	m_bVisible2D = pFrom->m_bVisible2D;
	m_nRenderFrame = pFrom->m_nRenderFrame;
	m_CullBox = pFrom->m_CullBox;
	m_BoundingBox = pFrom->m_BoundingBox;
	m_Render2DBox = pFrom->m_Render2DBox;

	r = pFrom->r;
	g = pFrom->g;
	b = pFrom->b;

	m_Dependents.RemoveAll();
	m_Dependents.AddVectorToTail(pFrom->m_Dependents);

	// dvs: should I copy m_pEditorKeys?

	//
	// Don't link to the parent if we're not updating dependencies, just copy the pointer.
	//
	if (bUpdateDependencies)
	{
		UpdateParent( pFrom->GetParent() );
	}
	else
	{
		m_pParent = pFrom->GetParent();
	}

	return(this);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the culling bbox of this object.
// Input  : mins - receives the minima for culling
//			maxs - receives the maxima for culling.
//-----------------------------------------------------------------------------
void CMapClass::GetCullBox(Vector &mins, Vector &maxs)
{
	m_CullBox.GetBounds(mins, maxs);
}


//-----------------------------------------------------------------------------
// Purpose: Initialize the cull box with the bounds of the faces.
//-----------------------------------------------------------------------------
void CMapClass::SetCullBoxFromFaceList( CMapFaceList *pFaces )
{
	SetBoxFromFaceList( pFaces, m_CullBox );
}


//-----------------------------------------------------------------------------
// Purpose: Returns the bounding bbox of this object.
// Input  : mins - receives the minima for culling
//			maxs - receives the maxima for culling.
//-----------------------------------------------------------------------------
void CMapClass::GetBoundingBox( Vector &mins, Vector &maxs )
{
	m_BoundingBox.GetBounds( mins, maxs );
}


//-----------------------------------------------------------------------------
// Purpose: Initialize the bounding box with the bounds of the faces.
//-----------------------------------------------------------------------------
void CMapClass::SetBoundingBoxFromFaceList( CMapFaceList *pFaces )
{
	SetBoxFromFaceList( pFaces, m_BoundingBox );
}


//-----------------------------------------------------------------------------
// Purpose: Initialize box with the bounds of the faces.
//-----------------------------------------------------------------------------
void CMapClass::SetBoxFromFaceList( CMapFaceList *pFaces, BoundBox &Box )
{
	//
	// Calculate our 3D bounds.
	//
	Box.ResetBounds();
	for (int iFace = 0; iFace < pFaces->Count(); iFace++)
	{
		CMapFace *pFace = pFaces->Element( iFace );
		int nPoints = pFace->GetPointCount();
		for (int i = 0; i < nPoints; i++)
		{
			Vector point;
			pFace->GetPoint(point, i);

			//
			// Push the culling box out in all directions.
			// TODO: rotate the culling box based on the cone orientation
			//			
			for (int nDim = 0; nDim < 3; nDim++)
			{
				Box.bmins[0] = min(Box.bmins[0], m_Origin[0] - point[nDim]);
				Box.bmins[1] = min(Box.bmins[1], m_Origin[1] - point[nDim]);
				Box.bmins[2] = min(Box.bmins[2], m_Origin[2] - point[nDim]);

				Box.bmaxs[0] = max(Box.bmaxs[0], m_Origin[0] + point[nDim]);
				Box.bmaxs[1] = max(Box.bmaxs[1], m_Origin[1] + point[nDim]);
				Box.bmaxs[2] = max(Box.bmaxs[2], m_Origin[2] + point[nDim]);
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns the bbox for 2D rendering of this object.
//			FIXME: this can be removed if we do all our 2D rendering in this->Render2D.
// Input  : mins - receives the minima for culling
//			maxs - receives the maxima for culling.
//-----------------------------------------------------------------------------
void CMapClass::GetRender2DBox(Vector &mins, Vector &maxs)
{
	m_Render2DBox.GetBounds(mins, maxs);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the number of keys that were loaded from the "editor"
//			section of the VMF. These keys are held until they are handled, then
//			the memory is freed.
//-----------------------------------------------------------------------------
int CMapClass::GetEditorKeyCount(void)
{
	if (m_pEditorKeys == NULL)
	{
		return NULL;
	}

	return m_pEditorKeys->GetCount();
}


//-----------------------------------------------------------------------------
// Purpose: Returns the key name for the given editor key index.
//-----------------------------------------------------------------------------
const char *CMapClass::GetEditorKey(int nIndex)
{
	if (m_pEditorKeys == NULL)
	{
		return NULL;
	}

	return m_pEditorKeys->GetKey(nIndex);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the value for the given editor key index.
//-----------------------------------------------------------------------------
const char *CMapClass::GetEditorKeyValue(int nIndex)
{
	if (m_pEditorKeys == NULL)
	{
		return NULL;
	}

	return m_pEditorKeys->GetValue(nIndex);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the value for the given editor key name.
//			NOTE: this is used for unique keys and will return the value for the
//			FIRST key with the given name.
//-----------------------------------------------------------------------------
const char *CMapClass::GetEditorKeyValue(const char *szKey)
{
	if (m_pEditorKeys == NULL)
	{
		return NULL;
	}

	return m_pEditorKeys->GetValue(szKey);
}

//-----------------------------------------------------------------------------
// Purpose: Begins a depth-first search of the map heirarchy.
// Input  : pos - An iterator 
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapClass::GetFirstDescendent(EnumChildrenPos_t &pos)
{
	pos.nDepth = 0;
	pos.Stack[0].pParent = this;

	if ( m_Children.Count() )
	{
		pos.Stack[0].pos = 0;
		return(GetNextDescendent(pos));
	}
	else
	{
		pos.Stack[0].pos = -1;
		return NULL;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Continues a depth-first search of the map heirarchy.
// Input  : &pos - 
// Output : CMapClass
//-----------------------------------------------------------------------------
CMapClass *CMapClass::GetNextDescendent(EnumChildrenPos_t &pos)
{
	while (pos.nDepth >= 0)
	{
		while (pos.Stack[pos.nDepth].pos != -1)
		{
			//
			// Get the next child of the parent on top of the stack.
			//
			CMapClass *pParent = pos.Stack[pos.nDepth].pParent;
			CMapClass *pChild = pParent->m_Children[pos.Stack[pos.nDepth].pos];
			pos.Stack[pos.nDepth].pos++;

			if ( pos.Stack[pos.nDepth].pos == pParent->m_Children.Count() )
				pos.Stack[pos.nDepth].pos= -1;


			// If this object has children, push it onto the stack.

			if ( pChild->m_Children.Count() )
			{
				pos.nDepth++;

				if (pos.nDepth < MAX_ENUM_CHILD_DEPTH)
				{
					pos.Stack[pos.nDepth].pParent = pChild;
					pos.Stack[pos.nDepth].pos = 0;
				}
				else
				{
					// dvs: stack overflow!
					pos.nDepth--;
				}
			}
			//
			// If this object has no children, return it.
			//
			else
			{
				return(pChild);
			}
		}
		
		//
		// Finished with this object's children, pop the stack and return the object.
		//
		pos.nDepth--;
		if (pos.nDepth >= 0)
		{
			return(pos.Stack[pos.nDepth + 1].pParent);
		}
	}

	return(NULL);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the world object that the given object belongs to.
// Input  : pStart - Object to traverse up from to find the world object.
//-----------------------------------------------------------------------------
CMapWorld *CMapClass::GetWorldObject(CMapAtom *pStart)
{
	CMapAtom *pObject = pStart;

	while (pObject != NULL)
	{
		if ( IsWorldObject( pObject ) )
		{
			return (CMapWorld*)pObject;
		}
		pObject = pObject->GetParent();
	}

	// has no world:
	return NULL;
}


BOOL CMapClass::IsChildOf(CMapAtom *pObject)
{
	CMapAtom *pParent = m_pParent;

	while( pParent )
	{
		if( pParent == pObject )
			return TRUE;

		if( IsWorldObject(pParent) )
			return FALSE;	// world object, not parent .. return false.

		pParent = pParent->GetParent();
	}

	return FALSE;
}


//-----------------------------------------------------------------------------
// Purpose: Returns whether this object belongs to the given visgroup.
// Input  : pVisGroup - 
//-----------------------------------------------------------------------------
int CMapClass::IsInVisGroup(CVisGroup *pVisGroup)
{
	if (pVisGroup != NULL)
	{
		if ( m_VisGroups.Find( pVisGroup ) != -1 )
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
		
	return 0;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : Returns true if the color was specified by this call, false if not.
//-----------------------------------------------------------------------------
bool CMapClass::UpdateObjectColor(void)
{
	//
	// The user can choose a visgroup from which to get the color from.
	// If one was chosen, set our color from that visgroup.
	//
	if (m_pColorVisGroup)
	{
		color32 rgbColor = m_pColorVisGroup->GetColor();
		SetRenderColor(rgbColor);
		return true;
	}
	else if (m_pParent && !IsWorldObject(m_pParent))
	{
		color32 rgbColor = m_pParent->GetRenderColor();
		SetRenderColor(rgbColor);
		return true;
	}

	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Sets the visgroup that this object gets its color from.
//-----------------------------------------------------------------------------
void CMapClass::SetColorVisGroup(CVisGroup *pVisGroup)
{
	m_pColorVisGroup = pVisGroup;
	UpdateObjectColor();
}


//-----------------------------------------------------------------------------
// Purpose: Adds the given visgroup to the list of visgroups that this object
//			belongs to.
//-----------------------------------------------------------------------------
void CMapClass::AddVisGroup(CVisGroup *pVisGroup)
{
	if (m_VisGroups.Find(pVisGroup) == -1)
	{
		m_VisGroups.AddToTail(pVisGroup);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Removes the given visgroup from the list of visgroups that this object
//			belongs to.
//-----------------------------------------------------------------------------
void CMapClass::RemoveVisGroup(CVisGroup *pVisGroup)
{
	int nIndex = m_VisGroups.Find(pVisGroup);
	
	if (nIndex != -1 )
	{
		m_VisGroups.FastRemove(nIndex);
		CheckVisibility();
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CMapClass::GetVisGroupCount(void)
{
	return m_VisGroups.Count();
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CVisGroup *CMapClass::GetVisGroup(int nIndex)
{
	return m_VisGroups.Element(nIndex);
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMapClass::RemoveAllVisGroups(void)
{
	m_VisGroups.RemoveAll();

	// Remove all visgroups from children as well.
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->RemoveAllVisGroups();
	}

	// Not in any visgroups; can't be hidden that way.
	VisGroupShow(true);
}


//-----------------------------------------------------------------------------
// Purpose: Adds the specified child to this object.
// Input  : pChild - Object to add as a child of this object.
//-----------------------------------------------------------------------------
void CMapClass::AddChild(CMapClass *pChild)
{
	if ( m_Children.Find(pChild) != -1 )
	{
		pChild->m_pParent = this;
		return;
	}

	m_Children.AddToTail(pChild);
	pChild->m_pParent = this;

	//
	// Update our bounds with the child's bounds.
	//
	Vector vecMins;
	Vector vecMaxs;

	pChild->GetCullBox(vecMins, vecMaxs);
	m_CullBox.UpdateBounds(vecMins, vecMaxs);

	pChild->GetBoundingBox( vecMins, vecMaxs );
	m_BoundingBox.UpdateBounds( vecMins, vecMaxs );

	pChild->GetRender2DBox(vecMins, vecMaxs);
	m_Render2DBox.UpdateBounds(vecMins, vecMaxs);

	if (m_pParent != NULL)
	{
		GetParent()->UpdateChild(this);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Removes all of this object's children.
//-----------------------------------------------------------------------------
void CMapClass::RemoveAllChildren(void)
{
	//
	// Detach the children from us. They are no longer in our world heirarchy.
	//
	FOR_EACH_OBJ( m_Children, pos )
	{	
		m_Children[pos]->m_pParent = NULL;
	}	

	//
	// Remove them from our list.
	//
	m_Children.RemoveAll();
}


//-----------------------------------------------------------------------------
// Purpose: Removes the specified child from this object.
// Input  : pChild - The child to remove.
//			bUpdateBounds - TRUE to calculate new bounds, FALSE not to.
//-----------------------------------------------------------------------------
void CMapClass::RemoveChild(CMapClass *pChild, bool bUpdateBounds)
{
	int index = m_Children.Find(pChild);

	if (index == -1)
	{
		pChild->m_pParent = NULL;
		return;
	}

	m_Children.Remove(index);
	pChild->m_pParent = NULL;

	if (bUpdateBounds)
	{
		PostUpdate(Notify_Removed);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Copies all children of a given object as children of this object.
//			NOTE: The child objects are replicated, not merely added as children.
// Input  : pobj - The object whose children are to be copied.
//-----------------------------------------------------------------------------
void CMapClass::CopyChildrenFrom(CMapClass *pobj, bool bUpdateDependencies)
{
	FOR_EACH_OBJ( pobj->m_Children, pos )
	{
		CMapClass *pChild = pobj->m_Children.Element(pos);
		CMapClass *pChildCopy = pChild->Copy(bUpdateDependencies);
		pChildCopy->CopyChildrenFrom(pChild, bUpdateDependencies);
		AddChild(pChildCopy);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Recalculate's this object's bounding boxes. CMapClass-derived classes
//			should call this first, then update using their local data.
// Input  : bFullUpdate - When set to TRUE, call CalcBounds on all children
//				before updating our bounds.
//-----------------------------------------------------------------------------
void CMapClass::CalcBounds(BOOL bFullUpdate)
{
	if ( CMapClass::s_bLoadingVMF )
		return;
		
	m_CullBox.ResetBounds();
	m_BoundingBox.ResetBounds();
	m_Render2DBox.ResetBounds();

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		if (bFullUpdate)
		{
			pChild->CalcBounds(TRUE);
		}

		m_CullBox.UpdateBounds(&pChild->m_CullBox);
		m_BoundingBox.UpdateBounds(&pChild->m_BoundingBox);
		m_Render2DBox.UpdateBounds(&pChild->m_Render2DBox);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Sets the render color of this object and all its children.
// Input  : uchRed, uchGreen, uchBlue - Color components.
//-----------------------------------------------------------------------------
void CMapClass::SetRenderColor(color32 rgbColor)
{
	CMapAtom::SetRenderColor(rgbColor);

	//
	// Set the render color of all our children.
	//
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		if (pChild != NULL)
		{
			pChild->SetRenderColor(rgbColor);
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Sets the render color of this object and all its children.
// Input  : uchRed, uchGreen, uchBlue - Color components.
//-----------------------------------------------------------------------------
void CMapClass::SetRenderColor(unsigned char uchRed, unsigned char uchGreen, unsigned char uchBlue)
{
	CMapAtom::SetRenderColor(uchRed, uchGreen, uchBlue);

	//
	// Set the render color of all our children.
	//
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		if (pChild != NULL)
		{
			pChild->SetRenderColor(uchRed, uchGreen, uchBlue);
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns a pointer to the object that should be added to the selection
//			list because this object was clicked on with a given selection mode.
// Input  : eSelectMode - 
//-----------------------------------------------------------------------------
CMapClass *CMapClass::PrepareSelection(SelectMode_t eSelectMode)
{
	if ((eSelectMode == selectGroups) && (m_pParent != NULL) && !IsWorldObject(m_pParent))
	{
		return GetParent()->PrepareSelection(eSelectMode);
	}

	return this;
}


//-----------------------------------------------------------------------------
// Purpose: Calls an enumerating function for each of our children that are of
//			of a given type, recursively enumerating their children also.
// Input  : pfn - Enumeration callback function. Called once per child.
//			dwParam - User data to pass into the enumerating callback.
//			Type - Unless NULL, only objects of the given type will be enumerated.
// Output : Returns FALSE if the enumeration was terminated early, TRUE if it completed.
//-----------------------------------------------------------------------------
BOOL CMapClass::EnumChildren(ENUMMAPCHILDRENPROC pfn, unsigned int dwParam, MAPCLASSTYPE Type)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		if (!Type || pChild->IsMapClass(Type))
		{
			if(!(*pfn)(pChild, dwParam))
			{
				return FALSE;
			}
		}

		// enum this child's children
		if (!pChild->EnumChildren(pfn, dwParam, Type))
		{
			return FALSE;
		}
	}

	return TRUE;
}


//-----------------------------------------------------------------------------
// Purpose: Enumerates a this object's children, only recursing into groups.
//			Children of entities will not be enumerated.
// Input  : pfn - Enumeration callback function. Called once per child.
//			dwParam - User data to pass into the enumerating callback.
//			Type - Unless NULL, only objects of the given type will be enumerated.
// Output : Returns FALSE if the enumeration was terminated early, TRUE if it completed.
//-----------------------------------------------------------------------------
BOOL CMapClass::EnumChildrenRecurseGroupsOnly(ENUMMAPCHILDRENPROC pfn, unsigned int dwParam, MAPCLASSTYPE Type)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);

		if (!Type || pChild->IsMapClass(Type))
		{
			if (!(*pfn)(pChild, dwParam))
			{
				return FALSE;
			}
		}

		if (pChild->IsGroup())
		{
			if (!pChild->EnumChildrenRecurseGroupsOnly(pfn, dwParam, Type))
			{
				return FALSE;
			}
		}
	}

	return TRUE;
}


//-----------------------------------------------------------------------------
// Purpose: Iterates through an object, and all it's children, looking for an
//			entity with a matching key and value
// Input  : key - 
//			value - 
// Output : CMapEntity - the entity found
//-----------------------------------------------------------------------------
CMapEntity *CMapClass::FindChildByKeyValue( const char* key, const char* value, bool *bIsInInstance, VMatrix *InstanceMatrix )
{
	if ( !key || !value )
		return NULL;

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element( pos );
		CMapEntity *e = pChild->FindChildByKeyValue( key, value, bIsInInstance, InstanceMatrix );
		if ( e )
			return e;
	}

	return NULL;
}


//-----------------------------------------------------------------------------
// Purpose: Called after this object is added to the world.
//
//			NOTE: This function is NOT called during serialization. Use PostloadWorld
//				  to do similar bookkeeping after map load.
//
// Input  : pWorld - The world that we have been added to.
//-----------------------------------------------------------------------------
void CMapClass::OnAddToWorld(CMapWorld *pWorld)
{
	//
	// Notify all our children.
	//
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->OnAddToWorld(pWorld);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called to notify the object that it has just been cloned
//			iterates through and notifies all the children of their cloned state
//			NOTE: assumes that the children are in the same order in both the
//			original and the clone
// Input  : pNewObj - the clone of this object
//			OriginalList - The list of objects that were cloned
//			NewList - The parallel list of clones of objects in OriginalList
//-----------------------------------------------------------------------------
void CMapClass::OnClone( CMapClass *pNewObj, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList )
{
	Assert( m_Children.Count() == pNewObj->m_Children.Count() );
	
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element( pos );
		CMapClass *pNewChild = pNewObj->m_Children.Element( pos );
		pChild->OnClone( pNewChild, pWorld, OriginalList, NewList );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called to notify the object that it has just been cloned
//			iterates through and notifies all the children of their cloned state
//			NOTE: assumes that the children are in the same order in both the
//			original and the clone
// Input  : pNewObj - the clone of this object
//			OriginalList - The list of objects that were cloned
//			NewList - The parallel list of clones of objects in OriginalList
//-----------------------------------------------------------------------------
void CMapClass::OnPreClone( CMapClass *pNewObj, CMapWorld *pWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList )
{
	Assert( m_Children.Count() == pNewObj->m_Children.Count() );
	
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element( pos );
		CMapClass *pNewChild = pNewObj->m_Children.Element( pos );
		pChild->OnPreClone( pNewChild, pWorld, OriginalList, NewList );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Notifies this object that a copy of itself is about to be pasted.
//			Allows the object to generate new unique IDs in the copy of itself.
// Input  : pCopy - 
//			pSourceWorld - 
//			pDestWorld - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapClass::OnPrePaste(CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	Assert( m_Children.Count() == pCopy->m_Children.Count() );

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		CMapClass *pCopyChild = pCopy->m_Children.Element(pos);

		pChild->OnPrePaste(pCopyChild, pSourceWorld, pDestWorld, OriginalList, NewList);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Notifies this object that a copy of itself is being pasted.
//			Allows the object to fixup any references to other objects in the
//			clipboard with references to their copies.
// Input  : pCopy - 
//			pSourceWorld - 
//			pDestWorld - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapClass::OnPaste(CMapClass *pCopy, CMapWorld *pSourceWorld, CMapWorld *pDestWorld, const CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	Assert( m_Children.Count() == pCopy->m_Children.Count() );

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		CMapClass *pCopyChild = pCopy->m_Children.Element(pos);

		pChild->OnPaste(pCopyChild, pSourceWorld, pDestWorld, OriginalList, NewList);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called just after this object has been removed from the world so
//			that it can unlink itself from other objects in the world.
// Input  : pWorld - The world that we were just removed from.
//			bNotifyChildren - Whether we should forward notification to our children.
//-----------------------------------------------------------------------------
void CMapClass::OnRemoveFromWorld(CMapWorld *pWorld, bool bNotifyChildren)
{
	//
	// Since we are being removed from the world, we cannot have any dependents.
	// Notify any dependent objects, so they can release pointers to us.
	// Our dependencies will be regenerated if we are added back into the world.
	//
	NotifyDependents(Notify_Removed);
	m_Dependents.RemoveAll();

	if (bNotifyChildren)
	{
		FOR_EACH_OBJ( m_Children, pos )
		{
			CMapClass *pChild = m_Children.Element(pos);
			pChild->OnRemoveFromWorld(pWorld, true);
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called after a map file has been completely loaded.
// Input  : pWorld - The world that we are in.
//-----------------------------------------------------------------------------
void CMapClass::PostloadWorld(CMapWorld *pWorld)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->PostloadWorld(pWorld);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called after all visgroups have been completely loaded.  Checks for 
//			objects hidden but without a visgroup.
// Input  : void
//-----------------------------------------------------------------------------
bool CMapClass::PostloadVisGroups( bool bLoading )
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->PostloadVisGroups( bLoading);
	}
	return CheckVisibility( bLoading );
}

//-----------------------------------------------------------------------------
// Purpose: Calls RenderPreload for each of our children. This allows them to
//			cache any resources that they need for rendering.
// Input  : pRender - Pointer to the 3D renderer.
//-----------------------------------------------------------------------------
bool CMapClass::RenderPreload(CRender3D *pRender, bool bNewContext)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->RenderPreload(pRender, bNewContext);
	}

	return(true);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pRender - 
//-----------------------------------------------------------------------------
void CMapClass::Render2D(CRender2D *pRender)
{
// This is not needed because the recursion is performed in CMapView2D::Render
//	POSITION pos = Children.GetHeadPosition();
//	while (pos != NULL)
//	{
//		CMapClass *pChild = Children.GetNext(pos);
//		if (pChild->IsVisible() && pChild->IsVisible2D())
//		{
//			pChild->Render2D(pRender);
//		}
//	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pRender - 
//-----------------------------------------------------------------------------
void CMapClass::Render3D(CRender3D *pRender)
{
}


//-----------------------------------------------------------------------------
// Purpose: Transforms all children. Derived implementations should call this,
//			then do their own thing.
// Input  : t - Pointer to class containing transformation information.
//-----------------------------------------------------------------------------
void CMapClass::DoTransform(const VMatrix &matrix)
{
	CMapPoint::DoTransform(matrix);

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->Transform( matrix );
	}
}


//-----------------------------------------------------------------------------
// Default logical box 
//-----------------------------------------------------------------------------
void CMapClass::GetRenderLogicalBox( Vector2D &mins, Vector2D &maxs ) 
{ 
	mins.Init( COORD_NOTINIT, COORD_NOTINIT ); 
	maxs.Init( COORD_NOTINIT, COORD_NOTINIT ); 
}

const Vector2D& CMapClass::GetLogicalPosition( )
{
	static Vector2D pos( COORD_NOTINIT, COORD_NOTINIT ); 
	return pos; 
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
size_t CMapClass::GetSize(void)
{
	return(sizeof(*this));
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CMapClass::HitTest2D(CMapView2D *pView, const Vector2D &point, HitInfo_t &HitData)
{
	HitData.pObject = NULL;
	HitData.nDepth = g_MAX_MAP_COORD*3;
	HitData.uData = 0;
	bool bFoundHit = false;
	
	if ( !IsVisible() )
		return false;

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);

		HitInfo_t testHitData;

		if ( pChild->HitTest2D(pView, point, testHitData) )
		{
			Assert( testHitData.pObject != NULL );

			if ( testHitData.nDepth < HitData.nDepth )
			{
				HitData = testHitData;
				bFoundHit = true;
			}
		}
	}

	return bFoundHit;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CMapClass::HitTestLogical(CMapViewLogical *pView, const Vector2D &point, HitInfo_t &hitData)
{
	if ( !IsVisibleLogical() )
		return false;

	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		if ( pChild->HitTestLogical(pView, point, hitData) )
			return true;
	}

	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Sets the selection state of this object's children.
// Input  : eSelectionState - 
//-----------------------------------------------------------------------------
SelectionState_t CMapClass::SetSelectionState(SelectionState_t eSelectionState)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapAtom *pObject = m_Children.Element(pos);
		pObject->SetSelectionState(eSelectionState);
	}

	return CMapAtom::SetSelectionState(eSelectionState);
}


//-----------------------------------------------------------------------------
// Purpose: Our child's bounding box has changed - notify our parent. The real
//			work will be done in CMapWorld::UpdateChild.
// Input  : pChild - The child whose bounding box changed.
//-----------------------------------------------------------------------------
void CMapClass::UpdateChild(CMapClass *pChild)
{
	if (m_pParent != NULL)
	{
		GetParent()->UpdateChild(this);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns a coordinate frame to render in
// Input  : matrix - 
// Output : returns true if a new matrix is returned, false if it is invalid
//-----------------------------------------------------------------------------
bool CMapClass::GetTransformMatrix( VMatrix& matrix )
{
	// try and get our parents transform matrix
	CMapClass *p = CMapClass::GetParent();
	if ( p )
	{
		return p->GetTransformMatrix( matrix );
	}

	return false;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pLoadInfo - 
//			pWorld - 
// Output : 
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapClass::LoadEditorCallback(CChunkFile *pFile, CMapClass *pObject)
{
	return(pFile->ReadChunk((KeyHandler_t)LoadEditorKeyCallback, pObject));
}


//-----------------------------------------------------------------------------
// Purpose: Handles keyvalues when loading the editor chunk of an object from the
//			MAP file. Keys are transferred to a special keyvalue list for use after
//			the entire map has been loaded.
// Input  : szKey - Key to handle.
//			szValue - Value of key.
//			pObject - Object being loaded.
// Output : Returns ChunkFile_Ok.
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapClass::LoadEditorKeyCallback(const char *szKey, const char *szValue, CMapClass *pObject)
{
	if (!stricmp(szKey, "color"))
	{
		CChunkFile::ReadKeyValueColor(szValue, pObject->r, pObject->g, pObject->b);
	}
	else if (!stricmp(szKey, "id"))
	{
		CChunkFile::ReadKeyValueInt(szValue, pObject->m_nID);
	}
	else  if (!stricmp(szKey, "comments"))
	{
		//
		// Load the object comments.
		// HACK: upcast to CEditGameClass *
		//
		CEditGameClass *pEdit = dynamic_cast <CEditGameClass *> (pObject);
		if (pEdit != NULL)
		{
			pEdit->SetComments(szValue);
		}
	}
	else if (!stricmp(szKey, "visgroupshown"))
	{
		CChunkFile::ReadKeyValueBool(szValue, pObject->m_bVisGroupShown);
	}
	else if ( !stricmp(szKey, "visgroupautoshown") )
	{
		CChunkFile::ReadKeyValueBool(szValue, pObject->m_bVisGroupAutoShown);
	}
	else
	{
		pObject->SetEditorKeyValue(szKey, szValue);
	}

	return(ChunkFile_Ok);
}


//-----------------------------------------------------------------------------
// Purpose: Call this function after changing this object via transformation,
//			etc. Notifies dependents and updates the parent with this object's
//			new size.
//-----------------------------------------------------------------------------
void CMapClass::PostUpdate(Notify_Dependent_t eNotifyType)
{
	if (m_pParent != NULL)
	{
		GetParent()->UpdateChild(this);
	}
	else if (eNotifyType != Notify_Removed)
	{
		CalcBounds(TRUE);
	}

	NotifyDependents(eNotifyType);
}


//-----------------------------------------------------------------------------
// Purpose: Notifies all our dependents that something about us has changed,
//			giving them the chance to update themselves.
//-----------------------------------------------------------------------------
void CMapClass::NotifyDependents(Notify_Dependent_t eNotifyType)
{
	Assert(m_Dependents.Count() < 1000);

	if (m_Dependents.Count() != 0)
	{
		CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
		if (pDoc)
		{
			pDoc->NotifyDependents(this, eNotifyType);
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Informs us that an object that we are dependent upon has changed,
//			giving us the opportunity to update ourselves accordingly.
// Input  : pObject - Object that we are dependent upon that has changed.
//-----------------------------------------------------------------------------
void CMapClass::OnNotifyDependent(CMapClass *pObject, Notify_Dependent_t eNotifyType)
{
}


//-----------------------------------------------------------------------------
// Purpose: Default implementation for saving editor-specific data. Does nothing.
// Input  : pFile - 
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapClass::SaveEditorData(CChunkFile *pFile)
{
	return(ChunkFile_Ok);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pFile - 
// Output : ChunkFileResult_t
//-----------------------------------------------------------------------------
ChunkFileResult_t CMapClass::SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo)
{
	//
	// Write the editor chunk.
	//
	ChunkFileResult_t eResult = pFile->BeginChunk("editor");

	//
	// Save the object's color.
	//
	if (eResult == ChunkFile_Ok)
	{
		eResult = pFile->WriteKeyValueColor("color", r, g, b);
	}

	//
	// Save the group ID, if any.
	//
	if (eResult == ChunkFile_Ok)
	{
		CMapGroup *pGroup = dynamic_cast<CMapGroup *>(m_pParent);
		if (pGroup != NULL)
		{
			eResult = pFile->WriteKeyValueInt("groupid", pGroup->GetID());
		}
	}

	//
	// Save the visgroup IDs, if any.
	//
	if (m_VisGroups.Count())
	{
		if ((eResult == ChunkFile_Ok) && m_VisGroups.Count())
		{
			for (int i = 0; i < m_VisGroups.Count(); i++)
			{
				CVisGroup *pVisGroup = m_VisGroups.Element(i);
				if ( !pVisGroup->IsAutoVisGroup() )
				{
					eResult = pFile->WriteKeyValueInt("visgroupid", pVisGroup->GetID());
					if (eResult != ChunkFile_Ok)
					{
						break;
					}
				}
			}
		}
	}

	if (eResult == ChunkFile_Ok)
	{
		eResult = pFile->WriteKeyValueBool("visgroupshown", m_bVisGroupShown);
	}

	if (eResult == ChunkFile_Ok)
	{
		eResult = pFile->WriteKeyValueBool("visgroupautoshown", m_bVisGroupAutoShown);
	}

	//
	// Save the object comments, if any.
	// HACK: upcast to CEditGameClass *
	//
	CEditGameClass *pEdit = dynamic_cast <CEditGameClass *> (this);
	if (pEdit != NULL)
	{
		if ((eResult == ChunkFile_Ok) && (strlen(pEdit->GetComments()) > 0))
		{
			eResult = pFile->WriteKeyValue("comments", pEdit->GetComments());
		}
	}

	//
	// Save any other editor-specific data.
	//
	if (eResult == ChunkFile_Ok)
	{
		eResult = SaveEditorData(pFile);
	}

	if (eResult == ChunkFile_Ok)
	{
		eResult = pFile->EndChunk();
	}

	return(eResult);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pDependent - 
//-----------------------------------------------------------------------------
void CMapClass::RemoveDependent(CMapClass *pDependent)
{
	int nIndex = m_Dependents.Find(pDependent);
	if (nIndex != -1)
	{
		m_Dependents.FastRemove(nIndex);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Frees all the keys that were loaded from the editor chunk of the MAP file.
//-----------------------------------------------------------------------------
void CMapClass::RemoveEditorKeys(void)
{
	delete m_pEditorKeys;
	m_pEditorKeys = NULL;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *szOldName - 
//			*szNewName - 
//-----------------------------------------------------------------------------
void CMapClass::ReplaceTargetname(const char *szOldName, const char *szNewName)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pObject = m_Children.Element(pos);
		pObject->ReplaceTargetname(szOldName, szNewName);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Updates an object attachment, making this object no longer dependent
//			on changes to the old object, and dependent on changes to the new object.
// Input  : pOldAttached - Object that this object was attached to (possibly NULL).
//			pNewAttached - New object being attached to (possibly NULL).
// Output : Returns pNewAttached.
//-----------------------------------------------------------------------------
CMapClass *CMapClass::UpdateDependency(CMapClass *pOldAttached, CMapClass *pNewAttached)
{
	if (pOldAttached != pNewAttached)
	{
		//
		// If we were attached to another object via this pointer, detach us now.
		//
		if (pOldAttached != NULL)
		{
			pOldAttached->RemoveDependent(this);
		}

		//
		// Attach ourselves as a dependent of the other object. We will now be notified
		// of any changes to that object.
		//
		if (pNewAttached != NULL)
		{
			pNewAttached->AddDependent(this);
		}
	}

	return(pNewAttached);
}


//-----------------------------------------------------------------------------
// Purpose: Updates this object's parent, removing it from it's old parent (if any)
//			attaching it to the new parent (if any).
// Input  : pNewParent - A pointer to the new parent for this object.
// Output : Returns a pointer to the new parent.
//-----------------------------------------------------------------------------
void CMapClass::UpdateParent(CMapClass *pNewParent)
{
	CMapClass *pOldParent = GetParent();

	if (pOldParent != pNewParent)
	{
		if (pOldParent != NULL)
		{
			pOldParent->RemoveChild(this);
		}

		if (pNewParent != NULL)
		{
			pNewParent->AddChild(this);
		}

		m_pParent = pNewParent;

		UpdateObjectColor();
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *szKey - 
// Output : const char
//-----------------------------------------------------------------------------
void CMapClass::SetEditorKeyValue(const char *szKey, const char *szValue)
{
	if (m_pEditorKeys == NULL)
	{
		m_pEditorKeys = new WCKeyValuesVector;
	}

	Assert( m_pEditorKeys != NULL );
	
	m_pEditorKeys->AddKeyValue(szKey, szValue);
}


//-----------------------------------------------------------------------------
// Purpose: Sets the origin of this object and its children.
//			FIXME: Should our children necessarily have the same origin as us?
//				   Seems like we should translate our children by our origin delta
//-----------------------------------------------------------------------------
void CMapClass::SetOrigin( Vector &origin )
{
	CMapPoint::SetOrigin( origin );
 
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element( pos );
		pChild->SetOrigin( origin );
	}

	PostUpdate(Notify_Changed);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : bVisible - 
//-----------------------------------------------------------------------------
void CMapClass::SetVisible(bool bVisible)
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->SetVisible(bVisible);
	}

	m_bVisible = bVisible;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : bShow - 
//-----------------------------------------------------------------------------
void CMapClass::VisGroupShow(bool bShow, VisGroupSelection eVisGroup) 
{
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->VisGroupShow(bShow, eVisGroup);
	}

	if ( eVisGroup == AUTO )
	{
		m_bVisGroupAutoShown = bShow;
	}		
	if ( eVisGroup == USER )
	{
		//since user visgroup visibility has precedence over auto, it is possible to change an object's auto
		//visibility through an action in a user visgroup.
		if ( bShow )
		{
			m_bVisGroupAutoShown = bShow;
		}
		m_bVisGroupShown = bShow;

	}
}


//-----------------------------------------------------------------------------
// Purpose: Causes all objects in the world to update any object dependencies (pointers)
//			that they might be holding. This is a static function.
//-----------------------------------------------------------------------------
void CMapClass::UpdateAllDependencies(CMapClass *pObject)
{
	//
	// Try to locate the world object.
	//
	CMapWorld *pWorld;
	if (pObject == NULL)
	{
		CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
		if ((pDoc == NULL) || (pDoc->IsLoading()))
		{
			return;
		}
		
		pWorld = pDoc->GetMapWorld();
	}
	else
	{
		pWorld = pObject->GetWorldObject(pObject);
	}

	if (pWorld == NULL)
	{
		return;
	}

	pWorld->UpdateAllDependencies( pObject );

	EnumChildrenPos_t pos;
	CMapClass *pChild = pWorld->GetFirstDescendent( pos );
	while ( pChild != NULL )
	{
		pChild->UpdateDependencies( pWorld, pObject );
		pChild = pWorld->GetNextDescendent( pos );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Returns whether this object should be hidden based on the given
//			cordon bounds.
// Output : Returns true to cull the object, false to keep it.
//-----------------------------------------------------------------------------
bool CMapClass::IsCulledByCordon(const Vector &vecMins, const Vector &vecMaxs)
{
	return !IsIntersectingBox(vecMins, vecMaxs);
}

//-----------------------------------------------------------------------------
// Purpose: Checks to see if the object is hidden by auto or user visgroups
//			without being assigned to one.  This solves the problem of objects
//			being destructively hidden by obsolete visgroups.
//-----------------------------------------------------------------------------
bool CMapClass::CheckVisibility( bool bLoading )
{
	CVisGroup* pVisGroup;
	bool bInUser = false;
	bool bInAuto = false;
	int nVisGroupCount = m_VisGroups.Count();
	bool bFoundOrphans = false;
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	
	for ( int i = 0; i < nVisGroupCount; i++ )
	{
		pVisGroup = m_VisGroups.Element( i );
		if ( pVisGroup->IsAutoVisGroup() )
		{
			bInAuto = true;
		}
		else
		{
			bInUser = true;
		}
	}
	if ( !bInAuto && !m_bVisGroupAutoShown )
	{	
		VisGroupShow( true, AUTO );			
	}		
	if ( !bInUser && !m_bVisGroupShown )
	{
		VisGroupShow( true, USER );
		if ( bLoading && pDoc->VisGroups_ObjectCanBelongToVisGroup( this ) )
		{
			//if this object is an orphan, we want it to be hidden but placed in a new visgroup.			
			bFoundOrphans = true;						
			VisGroupShow( false, USER );
		}				
	}

	return bFoundOrphans;
}


//-----------------------------------------------------------------------------
// Purpose: this routine will indicate if the object is editable.  Generally it
//			will not be editable if it is located in a separate instance or
//			submap.
//-----------------------------------------------------------------------------
bool CMapClass::IsEditable( void ) 
{ 
	if ( GetParent() )
	{
		return GetParent()->IsEditable();
	}
	
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: this function will notify all children that the instance they belong to has been moved.
//			it will also notify dependents of a translation.  this function is currently not
//			used but may be.
//-----------------------------------------------------------------------------
void CMapClass::InstanceMoved( void )
{
#if 0
	FOR_EACH_OBJ( m_Children, pos )
	{
		CMapClass *pChild = m_Children.Element(pos);
		pChild->InstanceMoved();
	}

	CMapWorld *pThisWorld = GetWorldObject( this );

	for (int i = 0; i < m_Dependents.Count(); i++)
	{
		CMapClass *pDependent = m_Dependents.Element(i);

		CMapWorld *pDependentWorld = GetWorldObject( pDependent );
		if ( pDependentWorld != pThisWorld )
		{
			pDependent->OnNotifyDependent( this, Notify_Transform );
		}
	}
#endif
}