#define HIE_FRIEND
#include "ACP_base.h"
 
 
#include "GMT.h"
#include "GEO.h"
#include "GLI.h"
#include "DPT.h"
#include "VIG.h"
 
#include "MEC.h"
 
#include "PCS.h"
#include "PO.h"
#include "IPO.h"
#include "LST.h"
#include "SND.h"
#include "SCT.h"
 
#include "SPO/HieMtStk.h"
#include "SPO/HieConst.h"
#include "SPO/HieExt.h"
#include "SPO/Specif/HieLoad.h"
#include "SPO/Specif/HieSpObj.h"
 
#include "GAM/Structur/3DOSLkTb.h" /* any other way ? perhaps...*/
 
/*ROMTEAM PLG Files Ionut Grozea 01-05-1998*/
#ifdef ACTIVE_EDITOR 
struct ISI_tdstObjectColorStruct_  m_ObjectColorStruct;
#endif
/*ENDROMTEAM PLG Files Ionut Grozea 01-05-1998*/
 
#include "ldt.h"
 
#define lTagAddChild			'CddA'
#define lTagPutMatrix			'MtuP'
#define lTagLinkedObject	'kniL'
#define lTagFlags					'galF'
#define lTagTransparency	'narT'
 
#define lTagMatrixTranslation	'narT' 
#define lTagMatrixScale				'lacS'
#define lTagMatrixRotation		'atoR'
 
HREF g_hRefSPO;
 
#ifdef ACTIVE_EDITOR 
extern void PLG_InitPLGLoad();
void LGT_fn_xLoadPlgObject();
#endif
 
 
/*****************************************************************
Function name   : fn_iCreateSuperObject
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 14-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateSuperObject( LDT_tdst_Link *pLink )
{
  VIG_fn_vAddToProgressBar(1);
	/* new SuperObject for that section*/
	pLink->pObject = (void*)HIE_fn_hCreateSuperObject();
 
  return 0;
}
 
 
 
/*****************************************************************
Function name   : fn_iLoadSuperObject
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 14-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadSuperObject( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	HIE_tdxHandleToSuperObject hSuperObject = (HIE_tdxHandleToSuperObject)pLink->pObject;
	POS_tdstCompletePosition *p_stMatrix;
	HIE_tdxHandleToVoid hLinkedObj;
	char a255_cLongName[255];
	HIE_tdxHandleToSuperObject hNewSuperObject;
	int nLen;
	static int iCount;
 
	nLen = LDT_ComputeSectionName(pLink, a255_cLongName);
	SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(HIE_fn_p_stGetSuperObjectLinkTable(), a255_cLongName, (unsigned long)hSuperObject), 1, nLen);
 
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
 
  	      case  lTagAddChild : /*  */
  	      {
				hNewSuperObject = (HIE_tdxHandleToSuperObject)LDT_LoadSection(LDT_szGetParam(1));
				HIE_fn_vSuperObjectAddTail(hSuperObject, hNewSuperObject);
  	      }
  	      break;
  	      case  lTagPutMatrix : /*  */
  	      {
				p_stMatrix = (POS_tdstCompletePosition *)LDT_LoadSection(LDT_szGetParam(1));										
				HIE_fn_vSetSuperObjectMatrix(hSuperObject, p_stMatrix);
  	      }
  	      break;
  	      case  lTagLinkedObject : /*  */
  	      {
 
				char szFile[SCR_CV_ui_Cfg_MaxLenName], szParent[SCR_CV_ui_Cfg_MaxLenName], szSection[SCR_CV_ui_Cfg_MaxLenName], szId[SCR_CV_ui_Cfg_MaxLenName];						
				char* szParams = LDT_szGetParam(1);
				long lType;
 
				LDT_SplitSectionName( szParams, szFile, szParent, szSection, szId ); 
 
				if (strcmpi(szSection,"Geometric")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)GLI_pLoadGeometricInFile(szParams);
					lType=HIE_C_ulEDT_Geometric;
				}
				else if (strcmpi(szSection,"Sector")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)LDT_LoadSection(szParams); 
					lType=HIE_C_ulSector;
				}
				else if (strcmpi(szSection,"SpecialEffect")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)LDT_LoadSection(szParams); 
					lType=HIE_C_ulSpecialEffect;
				}
				else if (strcmpi(szSection,"PHY")==0)
				{
					hLinkedObj = (HIE_tdxHandleToVoid)LDT_LoadSection(szParams);
					lType=HIE_C_ulPO;
				}
				else if (strcmpi(szSection,"IPO")==0)
				{
					hLinkedObj = (HIE_tdxHandleToVoid)LDT_LoadSection(szParams);
					lType=HIE_C_ulIPO;
				}
				else if (strcmpi(szSection,"Mirror")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)LDT_LoadSection(szParams); 
					lType=HIE_C_ulMirror;
				}
				else if (strcmpi(szSection,"Ipo_Mirror")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)LDT_LoadSection(szParams); 
					lType=HIE_C_ulIPO_Mirror;
				}
				else if (strcmpi(szSection,"PhyMirror")==0)
				{
					hLinkedObj =(HIE_tdxHandleToVoid)LDT_LoadSection(szParams); 
	 				lType=HIE_C_ulPO_Mirror;
				}
				/* Link the SuperObject with its Object*/
				HIE_fn_vSetSuperObjectObjectAndType( hSuperObject,hLinkedObj,lType);
  	      }
  	      break;
  	      case  lTagFlags : /*  */
  	      {
				HIE_fn_SO_vSetFlags( hSuperObject, atoi( LDT_szGetParam(1) ) );
  	      }
  	      break;
  	      case  lTagTransparency : /*  */
  	      {
				HIE_fn_vSetSuperObjectTransparenceLevel( hSuperObject, (float)atof( LDT_szGetParam(1) ) );
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
  LDT_AddToRefsTable( g_hRefSPO, pLink, 0, 0);
 
	/* By default, a loaded super object has the "box instead of sphere bounding volume" flag*/
  HIE_fn_SO_vSetFlags (hSuperObject, HIE_fn_SO_ulGetFlags (hSuperObject) | HIE_C_Flag_ulTypeOfBoundingVolume);
 
/*ROMTEAM PLG Files Ionut Grozea 01-05-1998*/
#ifdef ACTIVE_EDITOR 
	if (!iCount)
	{
		PLG_InitPLGLoad();
		LGT_fn_xLoadPlgObject();
		iCount = 1;
	}
#endif
/*ENDROMTEAM PLG Files Ionut Grozea 01-05-1998*/
 
  return 0;
}
 
 
/*****************************************************************
Function name   : fn_vSPOPostProcessLDT
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 14-Oct-98
Modified        : 
Return type     : void 
Argument        : HREF hRef
*****************************************************************/
void fn_vSPOPostProcessLDT( HREF hRef )
{
  LDT_tdst_Link *pLink;
  LDT_tdst_Link *pGetFrom;
  int iType;
  short xCount;
  long *pVal;
  MTH3D_tdstMatrix stIdentityMatrix;
  MTH3D_tdstMatrix * p_stTransformationMatrix;
 
  while( LDT_GetRefFromTable( hRef, &pLink, &pGetFrom, &iType, &xCount, &pVal )==0 )
  {
		HIE_tdxHandleToSuperObject hSuperObject = (HIE_tdxHandleToSuperObject)pLink->pObject;		
 
		/* If the matrix is the identity, we set the 'no transformation matrix' flag*/
		MTH3D_M_vSetIdentityMatrix (& stIdentityMatrix);
		p_stTransformationMatrix = & HIE_fn_hGetSuperObjectMatrix (hSuperObject) -> stTransformMatrix;
		if (MTH3D_M_bEqualMatrix (p_stTransformationMatrix, & stIdentityMatrix))
		{
				HIE_fn_SO_vSetFlags (hSuperObject, HIE_fn_SO_ulGetFlags (hSuperObject) | HIE_C_Flag_ulNoTransformationMatrix);
		}
 
  }
}
 
 
 
 
/*****************************************************************
Function name   : fn_iCreateOneMatrix
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 14-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateOneMatrix( LDT_tdst_Link *pLink )
{
	POS_tdstCompletePosition *p_stMatrix;
 
	p_stMatrix=GEO_fn_hCreateMatrix();
  POS_fn_vSetIdentityMatrix(p_stMatrix);
	pLink->pObject = (void*)p_stMatrix;
	
  return 0;
}
 
 
 
/*****************************************************************
Function name   : fn_iLoadOneMatrix
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 14-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadOneMatrix( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	POS_tdstCompletePosition *p_stMatrix = (POS_tdstCompletePosition *)pLink->pObject;
	long i;
	GLI_tdxValue a_xVertex[9];
	MTH3D_tdstVector stI,stJ,stK;
 
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)(szEntry+6))
  	    {
 
  	      case  lTagMatrixTranslation : /*  */
  	      {
						char szFileName[_MAX_PATH], *szP;
						LDT_ComputeSectionName(pLink, szFileName);
						szP = strchr(szFileName, '^');
						*szP = 0;
 
						for (i=1;i<4;i++)
							a_xVertex[i-1]=(GLI_tdxValue)GLI_M_FloatToValue(atof(LDT_szGetParam(i) ) * GLI_dGetUnitInMeterInFile(szFileName));
						
						MTH3D_M_vSetVectorElements(&stI,a_xVertex[0],a_xVertex[1],a_xVertex[2]);
			      POS_fn_vSetTranslationVector(p_stMatrix,&stI);
 
  	      }
  	      break;
  	      case  lTagMatrixScale : /*  */
  	      {
						for (i=1;i<10;i++)
							a_xVertex[i-1]=(GLI_tdxValue)GLI_M_FloatToValue(atof(LDT_szGetParam(i) ));
						
						if( !(  (a_xVertex[0] == 1.0)
							&& (a_xVertex[1] == 0.0)
							&& (a_xVertex[2] == 0.0)
							&& (a_xVertex[3] == 0.0)
							&& (a_xVertex[4] == 1.0)
							&& (a_xVertex[5] == 0.0)
							&& (a_xVertex[6] == 0.0)
							&& (a_xVertex[7] == 0.0)
							&& (a_xVertex[8] == 1.0))
							)
						{
							MTH3D_M_vSetVectorElements(&stI,a_xVertex[0],a_xVertex[1],a_xVertex[2]);
							MTH3D_M_vSetVectorElements(&stJ,a_xVertex[3],a_xVertex[4],a_xVertex[5]);
							MTH3D_M_vSetVectorElements(&stK,a_xVertex[6],a_xVertex[7],a_xVertex[8]);
				      POS_fn_vSetScaleMatrix(p_stMatrix,&stI,&stJ,&stK);
						}
 
  	      }
  	      break;
  	      case  lTagMatrixRotation : /*  */
  	      {
						for (i=1;i<10;i++)
							a_xVertex[i-1]=(GLI_tdxValue)GLI_M_FloatToValue(atof(LDT_szGetParam(i)));
						
						MTH3D_M_vSetVectorElements(&stI,a_xVertex[0],a_xVertex[1],a_xVertex[2]);
						MTH3D_M_vSetVectorElements(&stJ,a_xVertex[3],a_xVertex[4],a_xVertex[5]);
						MTH3D_M_vSetVectorElements(&stK,a_xVertex[6],a_xVertex[7],a_xVertex[8]);
			      POS_fn_vSetRotationMatrix(p_stMatrix,&stI,&stJ,&stK);
 
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
	/* Recalcul automatique du type*/
	POS_fn_vUpdateMatrixType(p_stMatrix);
 
  return 0;
}
 
