/*
 *=======================================================================================
 * Name        :EditorPO.cpp
 *
 * Author      :Vincent Lhullier             Date :09/04/97
 *
 * Description :implement member function of EditorPO object
 *=======================================================================================
 * Modification -> Author :                  Date :
 * Description :
 *=======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"
#include "acp_base.h"
#include "ITF.h"
#include "vig.h"
#include "DPT.h"
#include "incGLI.h"

#include "incOPD.h"
#include "OZO.h"
#include "x:\Cpa\Main\inc\_EditId.h"

//ROMTEAM WorldEditor (Cristian Stegaru 12/97)
#include "ogd.h"
#include "po\postruc.h"
#include "PO_res.h"
#include "x:\cpa\tempgrp\gliGLOU\MULTIDRV\inc\visus_st.h"
#include "DPT.h"
//ENDROMTEAM WorldEditor (Cristian Stegaru)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

DeclareTemplateStatic(PO_tdxHandleToPhysicalObject);

/*
 =======================================================================================
	CALLBACK (not class functions)
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : callback to save Physical Collide Set
  p_stFile            -> file
  p_szSectionName     -> section name
  p_vData             -> pointeur on data
  eAction             -> notification action
 ----------------------------------------------------------------------------------------
 */
void CallBackSaveCollideSet( SCR_tdst_File_Description *p_stFile, char *p_szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
	EditorPO *p_oPO = (EditorPO *) p_vData;

	switch (eAction)
	{
		case SCR_EA_Ntfy_AddSection:
		case SCR_EA_Ntfy_RebuildSection:
			//p_oPO->fn_vSectionSaved();
			p_oPO->fn_p_oGetPCSSection()->fn_vSectionSaved();
			{
				char				cZone;
				static char			*szZoneName[] = {"ZDD", "ZDM", "ZDE", "ZDR" };

				/*
				 * save beginning of section
				 */
                char                szName[SCR_CV_ui_Cfg_MaxLenName];
                char                szActionName[SCR_CV_ui_Cfg_MaxLenName];
                char                szSectionName[SCR_CV_ui_Cfg_MaxLenName];
                SCR_fn_v_RdL0_SplitSectionName(p_szSectionName, NULL, szActionName, szName);    // Remove the
                SCR_fn_v_RdL0_ComputeSectionName(szSectionName, NULL, szActionName, szName);    // file name
				SCR_M_SvL0_SaveBeginSection(p_stFile, szSectionName, SCR_CC_C_Cfg_EOL);

				for (cZone = 0; cZone < 4; cZone ++)
				{
                    CPA_SaveObject * pObjectToSave = NULL;
                    if (p_oPO->GetZone( cZone ))
                    {
                        pObjectToSave = (CPA_SaveObject *) p_oPO->GetZone( cZone );
                        if (pObjectToSave -> fn_bIsOfType (C_szPhysicalObjectTypeName))
                            pObjectToSave = (CPA_SaveObject *) ((EditorPO *) pObjectToSave) -> m_fnp_oGetGeometricWithDistance (0);
                    }

                    if (pObjectToSave)
                    {
    					SCR_M_SvL0_SaveEntry(p_stFile, szZoneName[ cZone ], SCR_CC_C_Cfg_NoChar);
					    SCR_fn_v_SvL0_SaveParameters_MP
						    (
							    p_stFile,
							    SCR_EF_SvL0_Normal,
							    1,
							    pObjectToSave->GetReferencedSectionName()
						    );
                    }
				}

				/*
				 * save end of section
				 */
				SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
			}
			break;

		case SCR_EA_Ntfy_DeleteSection:
			//p_oPO->fn_vSectionDeleted();
			p_oPO->fn_p_oGetPCSSection()->fn_vSectionDeleted();
			break;
	}
}


/*
 ----------------------------------------------------------------------------------------
  Description   : script callback function to save Vse Section
  p_stFile            -> file info 
  p_szSectionName     -> section name
  p_vData             -> pointer on data ( a visual set object handle )
  eAction             -> notify action
 ----------------------------------------------------------------------------------------
 */
void CallBackSaveVisualSet( SCR_tdst_File_Description *p_stFile, char *p_szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
	EditorPO *p_oPO = (EditorPO *) p_vData;

	switch (eAction)
	{
		case SCR_EA_Ntfy_AddSection:
		case SCR_EA_Ntfy_RebuildSection:
			{
				/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
				char							*p_cCur, szString[20];
				PO_tdxHandleToPhysicalObject	hPO;
				GEO_tdxHandleToVisualSet		hVse;
				ACP_tdxIndex					xNbLods, xLod;
				MTH_tdxReal						xThreshold;
				ACP_tdxHandleOfObject			xGeom;
				SCR_tdst_Link_Table				*p_stGeomLinkTable;
				SCR_tdst_Link_Value				*p_stGeomLinkValue;
				/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
			
				hPO = p_oPO->GetStruct();
				hVse = PO_fn_hGetVisualSet( hPO );
				xNbLods = (ACP_tdxIndex) GLI_lGetVisualSetNumbertOfLOD( hVse );
                
				/*
				 * write section name and number of lods
				 */
				p_cCur = strrchr( p_szSectionName, '^');
				if (p_cCur == NULL)
					p_cCur = p_szSectionName;
				else
					p_cCur++;
                
				SCR_M_SvL0_SaveBeginSection(p_stFile, p_cCur, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP( p_stFile, SCR_EF_SvL0_Scanf, 2, "%d", xNbLods );

				/*
				 * save now each sub section that correspond to each LOD
				 */
				p_stGeomLinkTable = GLI_p_stGetLinkTableOfGeometric();

				for (xLod = 0; xLod < xNbLods; xLod ++)
				{
					GLI_vGetVisualSetLOD( hVse, xLod, &xThreshold, &xGeom );


					sprintf( szString, "LOD:%d", xLod + 1);
					SCR_M_SvL0_SaveBeginSection(p_stFile, szString, SCR_CC_C_Cfg_EOL);
					SCR_M_SvL0_SaveEntry( p_stFile, "LOD_Treshold", SCR_CC_C_Cfg_NoChar );
					SCR_fn_v_SvL0_SaveParameters_MP( p_stFile, SCR_EF_SvL0_Scanf, 2, "%g", xThreshold );

					p_stGeomLinkValue = SCR_fnp_st_Link_SearchValue( p_stGeomLinkTable, (unsigned long) xGeom );
					if (p_stGeomLinkValue != NULL)
					{
						p_cCur = SCR_M_p_sz_Link_GetKey( p_stGeomLinkValue ) + SCR_M_ul_Link_GetAdditionalLong( p_stGeomLinkValue, 1 );
						SCR_M_SvL0_SaveEntry( p_stFile, "FichierMod", SCR_CC_C_Cfg_NoChar );
						SCR_fn_v_SvL0_SaveParameters_MP( p_stFile, SCR_EF_SvL0_Scanf, 2, "%s", p_cCur );
					}

				
//ROMTEAM WorldEditor (Cristian Stegaru 12/97)
					else if (p_oPO->IsCandidatePO ())
					{
						ASSERT (NULL != p_oPO->m_pOrigGeom);
						p_cCur = (char*)(LPCSTR)p_oPO->m_pOrigGeom->GetReferencedSectionName ();
						SCR_M_SvL0_SaveEntry( p_stFile, "FichierMod", SCR_CC_C_Cfg_NoChar );
						SCR_fn_v_SvL0_SaveParameters_MP( p_stFile, SCR_EF_SvL0_Scanf, 2, "%s", p_cCur );
					}
//ENDROMTEAM WorldEditor (Cristian Stegaru)

					SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
				}

				/*
				 * save end of section
				 */
				SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

				p_oPO->fn_p_oGetVSESection()->fn_vSectionSaved();
			}
			break;

		case SCR_EA_Ntfy_DeleteSection:
			p_oPO->fn_p_oGetPCSSection()->fn_vSectionDeleted();
			break;
	}

}


/*
 ----------------------------------------------------------------------------------------
  Description   : script callback function to save Po Section
  p_stFile            -> file info 
  p_szSectionName     -> section name
  p_vData             -> pointer on data ( a visual set object handle )
  eAction             -> notify action
 ----------------------------------------------------------------------------------------
 */
void CallBackSavePhysicalObject( SCR_tdst_File_Description *p_stFile, char *p_szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
//ROMTEAM WorldEditor (Cristian Stegaru 16/01/98) 
	EditorPO *p_oPO = (EditorPO *) p_vData;

	switch (eAction)
	{
		case SCR_EA_Ntfy_AddSection:
		case SCR_EA_Ntfy_RebuildSection:
			{
				p_oPO->fn_vSectionSaved();

				/*
				 * save beginning of section
				 */
                char                szName[SCR_CV_ui_Cfg_MaxLenName];
                char                szActionName[SCR_CV_ui_Cfg_MaxLenName];
                char                szSectionName[SCR_CV_ui_Cfg_MaxLenName];
                SCR_fn_v_RdL0_SplitSectionName(p_szSectionName, NULL, szActionName, szName);    // Remove the
                SCR_fn_v_RdL0_ComputeSectionName(szSectionName, NULL, szActionName, szName);    // file name
				SCR_M_SvL0_SaveBeginSection(p_stFile, szSectionName, SCR_CC_C_Cfg_EOL);
				p_oPO->fn_vComputePCSSectionName( szSectionName );
				CString csPCSSection, csOldPCSSection = szSectionName;
				int iInx = csOldPCSSection.Find ('^');
				ASSERT (-1 != iInx);
				csPCSSection = csOldPCSSection.Left (iInx);
				iInx = csOldPCSSection.ReverseFind ('^');
				ASSERT (-1 != iInx);
				csPCSSection += csOldPCSSection.Right (csOldPCSSection.GetLength () - iInx);

    			SCR_M_SvL0_SaveEntry(p_stFile, "CS", SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP
					(
						p_stFile,
						SCR_EF_SvL0_Normal,
						1,
						csPCSSection
					);

				p_oPO->fn_vComputeVSESectionName( szSectionName );
    			SCR_M_SvL0_SaveEntry(p_stFile, "VS", SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP
					(
						p_stFile,
						SCR_EF_SvL0_Normal,
						1,
						szSectionName
					);

				/*
				 * save end of section
				 */
				SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
			}
			break;

		case SCR_EA_Ntfy_DeleteSection:
			break;
	}
//ENDROMTEAM WorldEditor (Cristian Stegaru) 
}

/*
 =======================================================================================
	Construction/destruction and initialization 
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : constructor
  p_oDLL              -> owner dll
  p_oMainWorld        -> main world that contain EditorPO object
  hObj                -> handle on engine structure
  csName              -> name of object
  csFileName          -> file name were object is stored
  csSectionName       -> section name for object
 ----------------------------------------------------------------------------------------
 */
EditorPO::EditorPO
					(
						CPA_EditorBase *p_oDLL,
						PO_tdxHandleToPhysicalObject hObj,
						CPA_BaseObject	*p_oOwner,
						CString csName,
						CString csFileName,
						CString csSectionName
					)
		:CPA_SaveObject
					(
						p_oDLL,
						C_szPhysicalObjectTypeName,
						E_ss_Responsible,
						p_oOwner,
						TRUE,
						NULL,
						CallBackSavePhysicalObject
					)
		,CPA_EdMot<PO_tdxHandleToPhysicalObject>(hObj)
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	char						szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	char						*szPath; 
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//ROMTEAM WorldEditor (Nicu 07/11/97) 
	m_pGeom = NULL;
	m_pOrigGeom = NULL;
	m_iIdx = -2;	// not a geometric object
	m_bIsClone = FALSE;
//ENDROMTEAM WorldEditor (Nicu) 


	/*
	 * section object
	 */
	SetSectionData( this );

	/*
	 * compute origin and get datapath corresponding to origin
	 */
	szPath = fn_szComputeOrigin( (char *) (LPCTSTR) csFileName, &m_cOrigin );
	SetDataPath( szPath );
	
	/*
	 * section name of PO object
	 */
	SCR_fn_v_RdL0_ComputeSectionName
		(
			szSectionName, 
			(char *)(LPCTSTR) fn_csConstructFileName(csFileName), 
			(char *)(LPCTSTR) csSectionName, 
			(char *)(LPCTSTR) csName
		);
	SetReferencedSectionName( szSectionName );

	/*
	 * PCS Section
	 */
	/*
	fn_vComputePCSSectionName( szSectionName );
	m_p_oPCSSection = new CPA_SectionObject(szSectionName, szPath, CallBackSaveCollideSet, this, TRUE);
	*/

	/*
	fn_vComputeVSESectionName( szSectionName );
	m_p_oVSESection = new CPA_SectionObject(szSectionName, szPath, CallBackSaveVisualSet, this, TRUE);
	*/
	
	/*
	 * set object name 
	 */
	if (fn_eRename(csName) != E_mc_None)
		SetDefaultValidName();
	fn_vUpdateSectionName();

	if (hObj != NULL)
	{
		m_fn_vInitDataMembers();
//ROMTEAM WorldEditor (Nicu 06/12/97) 
		if(p_oOwner && p_oOwner->GetName() != "Geometric")
		{	
			fn_vComputePCSSectionName( szSectionName );
			if(!m_p_oPCSSection) 
			{
				m_p_oPCSSection = new CPA_SectionObject(szSectionName, szPath, CallBackSaveCollideSet, this, FALSE);
				ASSERT (m_p_oPCSSection);
				m_p_oPCSSection->fn_vWriteSection ();
			}
			fn_vComputeVSESectionName( szSectionName );
			if(!m_p_oVSESection) 
			{
				m_p_oVSESection = new CPA_SectionObject(szSectionName, szPath, CallBackSaveVisualSet, this, FALSE);
				ASSERT (m_p_oVSESection);
				m_p_oVSESection->fn_vWriteSection ();
			}
		}
//ENDROMTEAM WorldEditor (Nicu) 
// fbolefeysot - 06/08/98
// bidouille pour morphing premiere frame{
		if(!m_p_oVSESection) 
		{
			fn_vComputeVSESectionName( szSectionName );
			m_p_oVSESection = new CPA_SectionObject(szSectionName, szPath, CallBackSaveVisualSet, this, FALSE);
			ASSERT (m_p_oVSESection);
		}
//END fbolefeysot}
	}
	else
	{
		SetAvailable( FALSE );
	}

    SetExistingSection( TRUE );    // A module can only be created during loading

	m_fn_vSetIndexInObjectTable( C_uwNotInTable );
//ANNECY TQ 22/06/98{
	this->fn_vSetEditorPOBoundingVolume(GEO_fn_hCreateGeometricSphere());
//ENDANNECY TQ}
};


/*
 ----------------------------------------------------------------------------------------
  Description   : Destructor
 ----------------------------------------------------------------------------------------
 */
EditorPO::~EditorPO()
{
	/*	
	GEO_tdxHandleToVisualSet	hVSE;
	ACP_tdxIndex				xNumberOfLod;
	ACP_tdxIndex				xIndexOfLod;
	CPA_ObjectDLLBase			*p_oGeoDLL;


	if (GetDLL()->M_GetMultiDevice3D()->mbOnDestroy == FALSE)
	{

		hVSE = PO_fn_hGetVisualSet( GetStruct() );
		xNumberOfLod = GLI_lGetVisualSetNumbertOfLOD( hVSE );

		// Get Geometry3D DLL interface
		p_oGeoDLL = GetMainWorld()->GetObjectDLLWithName(C_szDLLGeometryName);

		// create a Geometric editor object for each Lod Definition
		for (xIndexOfLod = 0; xIndexOfLod < xNumberOfLod; xIndexOfLod ++)
		{
			if (m_dp_oLodGeom[xIndexOfLod] != NULL)
				p_oGeoDLL->OnQueryAction( p_oGeoDLL, C_uiDeleteGeometricObject, (LPARAM) m_dp_oLodGeom[xIndexOfLod] );
		}
	}
	*/
	free ( m_dp_oLodGeom );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : dummy function
 ----------------------------------------------------------------------------------------
 */
void fn_vEmptyDeleteFunction(PO_tdxHandleToPhysicalObject h){}

/*
 ----------------------------------------------------------------------------------------
  Description   : Static Initialization
 ----------------------------------------------------------------------------------------
 */
void EditorPO::StaticInit()
{
	CPA_EdMot<PO_tdxHandleToPhysicalObject>::Init(PO_fn_hAllocPhysicalObject,NULL,fn_vEmptyDeleteFunction);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : init data member of editor object with engine object
 ----------------------------------------------------------------------------------------
 */
void EditorPO::m_fn_vInitDataMembers( BOOL _bSecondPass /* = FALSE */ )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	PO_tdxHandleToPhysicalObject	hObj;
	GEO_tdxHandleToVisualSet		hVSE;
	PCS_tdxHandleToPhysicalCollSet	hPCS;
	ACP_tdxIndex					xNumberOfLod;
	ACP_tdxIndex					xIndexOfLod;
	MTH_tdxReal						xThreshold;
	CPA_ObjectDLLBase				*p_oOGDDLL;
	CPA_ObjectDLLBase				*p_oOZODLL;
	ACP_tdxHandleOfObject			hGeoObj;
	char							cZone;
	SCR_tdst_Link_Value				*p_stValue;
	char							*p_szPath, *p_szEndPath;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	hObj = (PO_tdxHandleToPhysicalObject) GetStruct();

	hVSE = PO_fn_hGetVisualSet( hObj );
	xNumberOfLod = (ACP_tdxIndex) GLI_lGetVisualSetNumbertOfLOD( hVSE );


    if (! _bSecondPass)
    {
	    /*
	     * init visual set section object
	     */
	    p_stValue = SCR_fnp_st_Link_SearchValue(VS_fn_p_stGetLinkTable(), (unsigned long) hVSE );
//ROMTEAM WorldEditor (Nicu 10/11/97) 
		if(p_stValue) {
//ENDROMTEAM WorldEditor (Nicu) 
	    p_szEndPath = p_stValue->p_szKey + p_stValue->stInfos.ulLong1 - 1;
	    *p_szEndPath = 0;
	    p_szPath = fn_szComputeOrigin( p_stValue->p_szKey, NULL );
	    *p_szEndPath++ = '\\';
	    m_p_oVSESection = new CPA_SectionObject( p_szEndPath, p_szPath, CallBackSaveVisualSet, this, TRUE);
//ROMTEAM WorldEditor (Nicu 07/11/97) 
		}
		else m_p_oVSESection = NULL;
//ENDROMTEAM WorldEditor (Nicu) 


	    /* allocate memory to store an array of pointer to Geometric Object */
	    m_dp_oLodGeom = (Geometry3D **) malloc ( xNumberOfLod * sizeof( Geometry3D *) );

	    /* Get Geometry3D DLL interface */
	    p_oOGDDLL = GetMainWorld()->GetObjectDLLWithName(C_szDLLGeometryName);

	    /* create a Geometric editor object for each Lod Definition */
	    for (xIndexOfLod = 0; xIndexOfLod < xNumberOfLod; xIndexOfLod ++)
	    {
		    if (p_oOGDDLL == NULL)
			    m_dp_oLodGeom[xIndexOfLod] = NULL;
		    else
		    {
			    GLI_vGetVisualSetLOD( hVSE, xIndexOfLod, &xThreshold, &hGeoObj );
			    m_dp_oLodGeom[xIndexOfLod] = (Geometry3D *) p_oOGDDLL->OnQueryAction( GetEditor(), C_uiCreateGeometricObject, (LPARAM) hGeoObj );
		    }
	    }

        /*
	     * Init PhysicalCollSet section object
	     */
    	hPCS = PO_fn_hGetCollideSet( hObj );

//ROMTEAM WorldEditor (Nicu 10/11/97) 
		if(hPCS) {
//ENDROMTEAM WorldEditor (Nicu) 
	    p_stValue = SCR_fnp_st_Link_SearchValue(CS_fn_p_stGetLinkTable(), (unsigned long) hPCS );
	    p_szEndPath = p_stValue->p_szKey + p_stValue->stInfos.ulLong1 - 1;
	    *p_szEndPath = 0;
	    p_szPath = fn_szComputeOrigin( p_stValue->p_szKey, NULL );
	    *p_szEndPath++ = '\\';

		 
		m_p_oPCSSection = new CPA_SectionObject(p_szEndPath, p_szPath, CallBackSaveCollideSet, this, TRUE);
 //ROMTEAM WorldEditor (Nicu 10/11/97) 
		}
		else m_p_oPCSSection = NULL;
//ENDROMTEAM WorldEditor (Nicu) 
    }


	/* now create Zone editor object */
	hPCS = PO_fn_hGetCollideSet( hObj );
	p_oOZODLL = GetMainWorld()->GetObjectDLLWithName( C_szDLLZDxName );


	for (cZone = 0; cZone < 4; cZone ++)
	{
		if ( (p_oOZODLL == NULL) || (hPCS == NULL) )
			m_ap_oZone[ cZone ] = NULL;
		else
		{
			hGeoObj = PCS_fn_hGetGeoObjOfPhysicalCollSet( cZone, hPCS );
			if (hGeoObj == NULL)
				m_ap_oZone[ cZone ] = NULL;
			else
			{
                static char	        *szZoneName[] = {C_szZddName, C_szZdmName, C_szZdeName, C_szZdrName };
                tdstCreateZoneMess  stZoneMess;
                char                *p_szSectionName = GLI_p_cSearchValueInLinkTableOfGeometric((unsigned long) hGeoObj);
                char                szName[SCR_CV_ui_Cfg_MaxLenName];

                SCR_fn_v_RdL0_SplitSectionName(p_szSectionName, NULL, NULL, szName);
                stZoneMess . csTypeName =  szZoneName[ cZone ];
                stZoneMess . hEngineZone = hGeoObj;
                stZoneMess . csName = szName;
                stZoneMess . pOwner = GetOwner();
                stZoneMess . szDataPath = GetDataPath();
                stZoneMess . csReferenceSectionName = p_szSectionName + strlen( stZoneMess . szDataPath ) + 1;
				m_ap_oZone[ cZone ] = (CPA_BaseObject *) p_oOZODLL->OnQueryAction( GetEditor(), C_uiQueryCreateZDxObject, (LPARAM) & stZoneMess );
// for ZdR, if zone was not found : create the collision object
				if ((m_ap_oZone[cZone] == NULL) && (cZone == 3) && (GetOwner() == NULL))
					m_ap_oZone[ cZone ] = (Geometry3D *)p_oOGDDLL->OnQueryAction( GetEditor(), C_uiCreateGeometricObject, (LPARAM) hGeoObj );

			}
		}
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : load engine PO associated with an empty Editor PO
  return (BOOL) TRUE if engine object is correctly loaded
 ----------------------------------------------------------------------------------------
 */
BOOL EditorPO::m_fn_bLoadEnginePO( void )
{
	CString							csRefSection;
	SCR_tdst_Cxt_Values				*p_stResult;
	PO_tdxHandleToPhysicalObject	hObj;
	CPA_ToolDLLBase					*p_oMaterialDLL;

	if (!fn_bIsAvailable() )
	{
		csRefSection = GetReferencedSectionName();
		p_stResult = SCR_fnp_st_RdL0_AnalyseSection( (char *) (LPCTSTR) csRefSection, SCR_CDF_uw_Anl_Normal );
		GLI_vEndofGeometricLoad();
		p_oMaterialDLL = GetMainWorld()->GetToolDLLWithName( C_szDLLMaterialName );
		if (p_oMaterialDLL != NULL)
			p_oMaterialDLL->OnQueryAction( GetEditor(), C_cCreateNewGameMaterial, 0 );
		hObj = (PO_tdxHandleToPhysicalObject) SCR_M_ul_RdL0_ExtractLongValue( p_stResult, 0 );
		fn_vUpdateData(hObj);

//ROMTEAM WorldEditor (Nicu 20/01/98)
		POInterface *p_oOPD = (POInterface *)GetMainWorld()->GetObjectDLLWithName( C_szDLLPhysicalObjectName );
		Shape3D* pGO;
		if (p_oOPD->mfn_bIsACandidateEPO (GetName()))
		{
			pGO = p_oOPD->mfn_pGetGeomObjFromEPOName (GetName());
			GMT_tdxHandleToGameMaterial hMaterial;
			GEO_vGetGameMaterialOfIndexedTriangles (*hObj->_hVisualSet->d_p_stLodDefinitions, 0, &hMaterial);

			struct GEO_tdstVisualSet_ *hVS = hObj->_hVisualSet;
			*hVS->d_p_stLodDefinitions = pGO->GetStruct();
			GEO_vSetGameMaterialOfIndexedTriangles (pGO->GetStruct(), 0, hMaterial);
		}
//ENDROMTEAM WorldEditor (Nicu)

		SetStruct( hObj );
		SetAvailable( TRUE );		
		m_fn_vInitDataMembers();

//ROMTEAM WorldEditor (Nicu 20/01/98)
		if (p_oOPD->mfn_bIsACandidateEPO (GetName()))
		{
			SetZone(3, pGO, FALSE);
			m_iIdx = p_oOPD->mfn_iGetEPOIdx (GetName());
			mfn_vSetClone (TRUE);
			SetExistingSection (TRUE);
			fn_vNotifySave ();
			p_oOPD->m_lstOfCandidateEPO.AddTail (this);
		}
//ENDROMTEAM WorldEditor (Nicu)

		return TRUE;
	}
	return FALSE;
}



/*
 ----------------------------------------------------------------------------------------
  Description   : compute origin of an editor PO object
	return data path for file that contain the PO
 ----------------------------------------------------------------------------------------
 */
char *EditorPO::fn_szComputeOrigin( char *_szFileName, char *_cOrigin )
{
	char *szPath;

	szPath = fn_szGetFamiliesDataPath();
	if (strnicmp( _szFileName, szPath, strlen( szPath ) ) == 0)
	{
		if (_cOrigin != NULL)
			*_cOrigin = C_cFromFamilies;
	}
	else
	{
		szPath = fn_szGetLevelsDataPath();
		if (strnicmp( _szFileName, szPath, strlen( szPath) ) == 0)
		{
			if (_cOrigin != NULL)
				*_cOrigin = C_cFromLevels;
		}
		else
		{
			szPath = fn_szGetGraphicsClassesDataPath();
			if (strnicmp( _szFileName, szPath, strlen( szPath) ) == 0)
			{
				if (_cOrigin != NULL)
					*_cOrigin = C_cFromClasses;
			}
			else
			{
				// this is not a PO, actually can be a VSE : don't need to return origin.
				szPath = fn_szGetGraphicsBanksDataPath();
				
			}
		}
	}

	return szPath;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : return part of filename that correspond to short file name
 ----------------------------------------------------------------------------------------
 */
CString EditorPO::fn_csConstructFileName(CString csFileName)
{
	csFileName = csFileName.Mid( strlen( GetDataPath() ) + 1 );
	return csFileName;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : return PCS section Name associated with the PO object
  szPCSSectionName    -> string were result string is copied
 ----------------------------------------------------------------------------------------
 */
void EditorPO::fn_vComputePCSSectionName( char *szPCSSectionName )
{
	char szEndSectionName[ SCR_CV_ui_Cfg_MaxLenName ];
	char *p_cCur;

	strcpy( szPCSSectionName, (char *) (LPCTSTR) GetReferencedSectionName());

	p_cCur = strchr( szPCSSectionName, '.');
	strcpy( szEndSectionName, p_cCur + 12 );

	strcpy( p_cCur, ".zoo^AllCollideSets^CS:CS_");
	strcat( p_cCur, szEndSectionName );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : return PCS section Name associated with the PO object
  szPCSSectionName    -> string were result string is copied
 ----------------------------------------------------------------------------------------
 */
void EditorPO::fn_vComputeVSESectionName( char *szVSESectionName )
{
	char szEndSectionName[ SCR_CV_ui_Cfg_MaxLenName ];
	char *p_cCur;

	strcpy( szVSESectionName, (char *) (LPCTSTR) GetReferencedSectionName());

	p_cCur = strchr( szVSESectionName, '.');
	if (p_cCur)
	{
		strcpy( szEndSectionName, p_cCur + 12 );
		strcpy( p_cCur, ".vse^VS:VSE_");
		strcat( p_cCur, szEndSectionName );
	}
	else
	{
		p_cCur = strchr(szVSESectionName, '^');
		strcpy(szEndSectionName, p_cCur + 8);
		strcpy( p_cCur, "^VS:VSE_");
		strcat( p_cCur, szEndSectionName );
	}
}


/*
 =======================================================================================
	Notification
 =======================================================================================
 */

//--------------------------------------------------------------------------------------
void EditorPO::fn_vNotifySave	 (void)
{
//ROMTEAM WorldEditor (Cristian Stegaru 12/97)
	fn_vWriteSection ();
//ENDROMTEAM WorldEditor (Cristian Stegaru)
	m_p_oPCSSection->fn_vWriteSection();
}
//--------------------------------------------------------------------------------------
void EditorPO::fn_vNotifyUnSave	 (void)
{
	m_p_oPCSSection->fn_vDeleteSection();
}
//--------------------------------------------------------------------------------------
void EditorPO::fn_vNotifyRestore (void)
{
	m_p_oPCSSection->fn_vRestoreSection();
}
//--------------------------------------------------------------------------------------
void EditorPO::fn_vNotifyRename	 (void)
{
	char szSectionName[ SCR_CV_ui_Cfg_MaxLenName ];

	fn_vComputePCSSectionName( szSectionName );
	m_p_oPCSSection->fn_vRenameSection( szSectionName );
	/*
	fn_vComputeVSESectionName( szSectionName );
	m_p_oVSESection->fn_vRenameSection( szSectionName );
	*/
}

//--------------------------------------------------------------------------------------
void EditorPO::fn_vUpdateSectionName (void)
{
	// update main section
	CPA_SaveObject::fn_vUpdateSectionName();
	// update PCS section
	//fn_vNotifyRename();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_csGetFullGeomSectionName 
// Date   : 98-03
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
CString EditorPO::mfn_csGetFullGeomSectionName ()
{
	char							*p_cCur;
	PO_tdxHandleToPhysicalObject	hPO;
	GEO_tdxHandleToVisualSet		hVse;
	ACP_tdxIndex					xNbLods, xLod;
	MTH_tdxReal						xThreshold;
	ACP_tdxHandleOfObject			xGeom;
	SCR_tdst_Link_Table				*p_stGeomLinkTable;
	SCR_tdst_Link_Value				*p_stGeomLinkValue;
	CString							csFullGeomSectionName;

	CPA_ObjectDLLBase *p_oPODll = GetMainWorld()->GetObjectDLLWithName (C_szDLLPhysicalObjectName);
	HINSTANCE hOldInst = AfxGetResourceHandle();
	if (p_oPODll)
		AfxSetResourceHandle (p_oPODll->GetDLLIdentity()->hModule);

	hPO = GetStruct();
	hVse = PO_fn_hGetVisualSet( hPO );
	xNbLods = (ACP_tdxIndex) GLI_lGetVisualSetNumbertOfLOD( hVse );
	ASSERT (1 == xNbLods);
    xLod = 1;            
	p_stGeomLinkTable = GLI_p_stGetLinkTableOfGeometric();
	GLI_vGetVisualSetLOD( hVse, xLod, &xThreshold, &xGeom );
	p_stGeomLinkValue = SCR_fnp_st_Link_SearchValue( p_stGeomLinkTable, (unsigned long) xGeom );

	if (p_stGeomLinkValue != NULL)
	{
		p_cCur = SCR_M_p_sz_Link_GetKey( p_stGeomLinkValue ) + SCR_M_ul_Link_GetAdditionalLong( p_stGeomLinkValue, 1 );
		csFullGeomSectionName = p_cCur;
	}
	else
		csFullGeomSectionName = mfn_csGetModSectionName ();

	if (p_oPODll)
		AfxSetResourceHandle(hOldInst);

	return csFullGeomSectionName;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_csGetModSectionName 
// Date   : 98-03
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
CString EditorPO::mfn_csGetModSectionName ()
{
  ASSERT (m_dp_oLodGeom);
  Geometry3D *pG3d = m_dp_oLodGeom [0];
  ASSERT (pG3d);
  return pG3d->GetReferencedSectionName ();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_iGetFacesNumber 
// Date   : 98-05
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
ACP_tdxIndex EditorPO::mfn_iGetFacesNumber ()
{
	ACP_tdxIndex iFacesNumber = 0;
	Geometry3D *p3D = m_fnp_oGetGeometricWithDistance (0);
	if (p3D)
		iFacesNumber = p3D->mfn_iGetFacesNumber ();

	return iFacesNumber;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_3dvGetMinCoordinates 
// Date   : 98-05
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
MTH3D_tdstVector EditorPO::mfn_3dvGetMinCoordinates ()
{
	MTH3D_tdstVector tdstMinCoordintes;
	tdstMinCoordintes.xX = tdstMinCoordintes.xY = tdstMinCoordintes.xZ = 0;
	Geometry3D *p3D = m_fnp_oGetGeometricWithDistance (0);
	if (p3D)
		tdstMinCoordintes = p3D->mfn_3dvGetMinCoordinates ();

	return tdstMinCoordintes;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_3dvGetMaxCoordinates 
// Date   : 98-05
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
MTH3D_tdstVector EditorPO::mfn_3dvGetMaxCoordinates ()
{
	MTH3D_tdstVector tdstMaxCoordintes;
	tdstMaxCoordintes.xX = tdstMaxCoordintes.xY = tdstMaxCoordintes.xZ = 0;
	Geometry3D *p3D = m_fnp_oGetGeometricWithDistance (0);
	if (p3D)
		tdstMaxCoordintes = p3D->mfn_3dvGetMaxCoordinates ();

	return tdstMaxCoordintes;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_3dvGetSizes 
// Date   : 98-05
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
MTH3D_tdstVector EditorPO::mfn_3dvGetSizes ()	//dX, dY, dZ
{
	MTH3D_tdstVector tdstSize;
	tdstSize.xX = tdstSize.xY = tdstSize.xZ = 0;
	Geometry3D *p3D = m_fnp_oGetGeometricWithDistance (0);
	if (p3D)
		tdstSize = p3D->mfn_3dvGetSizes ();

	return tdstSize;
}

