//-----------------------------------------------------------------------------
// Purpose: Searches for a face with the given unique ID in the list of objects.
//			FIXME: should this be in CMapObjectList?
// Input  : nFaceID - Face ID to search for.
//			List - List of objects to search.
// Output : Returns the face with the given ID if it was found, NULL if not.
//-----------------------------------------------------------------------------
CMapFace *CMapSideList::FindFaceIDInList(int nFaceID, CMapObjectList &List)
{
	POSITION pos = List.GetHeadPosition();
	while (pos != NULL)
	{
		//
		// If this object is a solid, look for the face there.
		//
		CMapClass *pObject = List.GetNext(pos);
		CMapSolid *pSolid = dynamic_cast <CMapSolid *>(pObject);
		if (pSolid != NULL)
		{
			CMapFace *pFace = pSolid->FindFaceID(nFaceID);
			if (pFace != NULL)
			{
				return(pFace);
			}
		}

		//
		// Check all of this object's solid children.
		//
		EnumChildrenPos_t pos2;
		CMapClass *pChild = pObject->GetFirstDescendent(pos2);
		while (pChild != NULL)
		{
			pSolid = dynamic_cast <CMapSolid *>(pChild);
			if (pSolid != NULL)
			{
				CMapFace *pFace = pSolid->FindFaceID(nFaceID);
				if (pFace != NULL)
				{
					return(pFace);
				}
			}	

			pChild = pObject->GetNextDescendent(pos2);
		}
	}

	return(NULL);
}
//-----------------------------------------------------------------------------
// Purpose: Called from OnClone and OnPaste. Replaces references (in the
//			cloned object) to faces in the original list of objects with references
//			to corresponding faces in the new list of objects.
// Input  : pClone - 
//			OriginalList - 
//			NewList - 
//-----------------------------------------------------------------------------
void CMapSideList::ReplaceFacesInCopy(CMapSideList *pCopy, CMapObjectList &OriginalList, CMapObjectList &NewList)
{
	POSITION pos1 = OriginalList.GetHeadPosition();
	POSITION pos2 = NewList.GetHeadPosition();
	while (pos1 != NULL)
	{
		ASSERT(pos2 != NULL);

		CMapClass *pOriginal = OriginalList.GetNext(pos1);
		CMapClass *pNew = NewList.GetNext(pos2);

		if (pOriginal != this)
		{
			//
			// Check to see if these two objects are solids.
			//
			CMapSolid *pOrigSolid = dynamic_cast <CMapSolid *>(pOriginal);
			if (pOrigSolid != NULL)
			{
				CMapSolid *pNewSolid = dynamic_cast <CMapSolid *>(pNew);
				ASSERT(pNewSolid != NULL);

				if (pNewSolid != NULL)
				{
					pCopy->ReplaceSolidFaces(pOrigSolid, pNewSolid);
				}
			}

			//
			// Check all of these objects' children.
			//
			EnumChildrenPos_t e1;
			EnumChildrenPos_t e2;

			CMapClass *pOrigChild = pOriginal->GetFirstDescendent(e1);
			CMapClass *pNewChild = pNew->GetFirstDescendent(e2);

			while (pOrigChild != NULL)
			{
				ASSERT(pNewChild != NULL);

				pOrigSolid = dynamic_cast <CMapSolid *>(pOrigChild);
				if (pOrigSolid != NULL)
				{
					CMapSolid *pNewSolid = dynamic_cast <CMapSolid *>(pNewChild);
					ASSERT(pNewSolid != NULL);

					if (pNewSolid != NULL)
					{
						pCopy->ReplaceSolidFaces(pOrigSolid, pNewSolid);
					}
				}

				pOrigChild = pOriginal->GetNextDescendent(e1);
				pNewChild = pNew->GetNextDescendent(e2);
			}
		}
	}
	
	//
	// Update the keyvalue in the copy's parent entity.
	//
	pCopy->UpdateParentKey();
}