#define D_State_Define
#define D_ZdxStuff_StructureDefine
#define D_ZdxStuff_VariableDefine
 
#include "ToolsCPA.h"
#include "GameEng.h"
#include "Structur/State.h"
#include "Actions/AllActs.h"
#include "Structur/GameScpt.h"
#include "Structur/ErrGame.h"
#include "Structur/MemGame.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
#include "Structur/Objects.h"
#include "Structur/ObjsTbls.h"
 
#include "Basic.h"
#include "Family.h"
#include "Physicol.h"
#include "ZdxStuff.h"
#include "ToolCam.h"
#include "Zemem.h"
#include "Structur/3DOSLkTb.h"
#include "ChanList.h"
#include "Collisio.h"
#include "GLI\GLI_Defn.h"
#include "ObjType.h"
 
#include "LDT.h"
 
HREF g_hRefActivation;
 
/*----------------------------------------------------------------------
 * Creation of the List of Zdx Geometric objects
 *--------------------------------------------------------------------*/
#if defined(GAM_USE_SCRIPT) 
/* MP - Callback for LDT */
 
extern HREF g_hRefZDX;
/*****************************************************************
Name: 
Description: Create callback for LDT
Author: Mircea Petrescu
Date: 7/8/98
Modified: 
*****************************************************************/
int fn_iCreateZdxArrayDescription( LDT_tdst_Link *pLink )
{
  tdstZdxList *              p_stZdxList;
 
  p_stZdxList=fn_p_stCreateZdxListElement();
 
  pLink->pObject=(void *)p_stZdxList;
  return 0;
}
 
 
/*****************************************************************
Name: 
Description: Load callback for LDT
Author: Mircea Petrescu
Date: 7/8/98
Modified: 
*****************************************************************/
int fn_iLoadZdxArrayDescription( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  tdstZdxList * p_stZdxList=( tdstZdxList * )pLink->pObject;
  tdstGeometricZdxList *     p_stGeoZdxList;
  ACP_tdxHandleOfObject      hGeoObj;
 
  #if defined(ACTIVE_EDITOR)
  char a255_cLinkKey[255];
  #endif /* ACTIVE_EDITOR */
 
 
  p_stZdxList->uwNumberOfZdx=(unsigned short)fn_lAToI( LDT_szGetParam( 2 ) );
 
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
  	      case 'moeG' : /* GeometricObject */
  	      {
 
			  int iLen;
 
			  p_stGeoZdxList=fn_p_stCreateGeometricZdxListElement();
         	  hGeoObj=GLI_pLoadGeometricInFile(LDT_szGetParam( 1 ) );
         	  p_stGeoZdxList->hGeoObj=hGeoObj;
         	  LST2_M_StaticAddTail(&p_stZdxList->hGeoZdxList,p_stGeoZdxList);
				
			  LDT_AddToRefsTable( g_hRefZDX, pLink, 0, 0 );
			  
			  /* // I see no use for this code !! - MP
         	  strcpy(szForScript,fn_szGetZdxDataPath());
         	  strcat(szForScript,"\\");
         	  fn_v_File_ComputeFileSectionName(sz,szFileName,szForScript+strlen(szForScript));
			  */
 
			  /* LinkTable management*/
#if defined(ACTIVE_EDITOR)
 
			  iLen=LDT_ComputeSectionName(pLink,a255_cLinkKey);
			  SCR_M_v_Link_SetAdditionalLong(
               SCR_fnp_st_Link_SetValue(&g_st3DOSLinkTable.stCharacterZone,
               a255_cLinkKey,(unsigned long)p_stGeoZdxList),
               1,
               iLen);
 #endif /* ACTIVE_EDITOR */
 
  	      }
  	      break;
  	      case 'htoN' : /* Nothing */
  	      {
			  p_stGeoZdxList=fn_p_stCreateGeometricZdxListElement();
			  p_stGeoZdxList->hGeoObj = NULL;
			  LST2_M_StaticAddTail(&p_stZdxList->hGeoZdxList,p_stGeoZdxList);
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
 
  return 0;
}
 
void fn_vPostProcessZDX( HREF hRef )
{
  LDT_tdst_Link *pLink;
  LDT_tdst_Link *pGetFrom;
  int iType;
  short xCount;
  long *pVal;
 
  ACP_tdxHandleOfObject      hGeoObj;
 
  while( LDT_GetRefFromTable( hRef, &pLink, &pGetFrom, &iType, &xCount, &pVal )==0 )
  {
	  hGeoObj=(ACP_tdxHandleOfObject)pGetFrom->pObject;
 
   	  GEO_vCreateObjectListOfPointsMaterial ( hGeoObj );
   	  GEO_vComputeObjectListOfPointsMaterial(hGeoObj);
  }
}
 
#endif /*GAM_USE_SCRIPT */
/* MP - end LDT */
 
 
/*****************************************************************
Function name   : fn_iCreateNewZoneSetArray
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateNewZoneSetArray( LDT_tdst_Link *pLink )
{
 
	pLink->pObject = (void*)fn_p_stCreateCsaListElement();
	
  return 0;
}
 
/* Load for section 'CreateNewArrayOfZoneSet' is default. */
 
extern void fn_vInsertZSEInZoneSetList(tdstCsaList * p_stCsaList,tdstZoneSetEncapsulation *p_stZSE);
 
 
/*****************************************************************
Function name   : fn_iLoadActivation
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadActivation( LDT_tdst_Link *pLink ) /* CB-ul pt. sectiunea AddActivation*/
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
 
	tdstCsaList * p_stCsaList = (tdstCsaList *)pLink->pParent->pObject;
	tdstZoneSetEncapsulation * p_stZSE;
	tdstZoneSetList * p_stZoneSetList;
	
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry : ActivationZone */
  	  {
				char * szP0, * szP1, *szFileName;
 
				szP0 = (char*)LDT_M_malloc(_MAX_PATH);
				szP1 = (char*)LDT_M_malloc(_MAX_PATH);
				szFileName = (char*)LDT_M_malloc(_MAX_PATH+2);
				strcpy(szP0, LDT_szGetParam(1));
				strcpy(szP1, LDT_szGetParam(2));
				strcpy(szFileName, pLink->szFile); /* ???*/
 
				p_stZSE = fn_p_stCreateZoneSetEncapsulation();
				p_stZoneSetList=(tdstZoneSetList *)LDT_LoadSection(szP0);
 
        p_stZSE->p_stZoneSetList = p_stZoneSetList;
/*				fn_vInsertZSEInZoneSetList(p_stCsaList,p_stZSE);*/
 
				LDT_AddToRefsTable( g_hRefActivation, pLink->pParent, 0, 4, szP0, szP1, szFileName, p_stZSE );
 
  	  }
  	}
  }
 
 
 
  return 0;
}
 
 
 
/*****************************************************************
Function name   : fn_vActivationPostProcessLDT
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : void 
Argument        : HREF hRef
*****************************************************************/
void fn_vActivationPostProcessLDT( HREF hRef )
{
  LDT_tdst_Link *pLink;
  LDT_tdst_Link *pGetFrom;
  int iType;
  short xCount;
  long *pVal;
 
 
 
  while( LDT_GetRefFromTable( hRef, &pLink, &pGetFrom, &iType, &xCount, &pVal )==0 )
  {
		char * szP0, * szP1, *szFileName;
		char *p_cEnd;
		tdstZoneSetEncapsulation * p_stZSE;
    
    tdstCsaList * p_stCsaList = (tdstCsaList *)pLink->pObject;	
 
		tdxHandleToFamilyList hFamily;
 
		tdxHandleToState hState2,hState;
		char szStateSectionName[_MAX_PATH];
		char szLinkTableKey[_MAX_PATH];
		SCR_tdst_Link_Value *stLinkValue;
		short uwIndex;
 
		szP0 = (char*)(*pVal);
		szP1 = (char*)(*(pVal+1));
		szFileName = (char*)(*(pVal+2));
		p_stZSE = (tdstZoneSetEncapsulation *)(*(pVal+3));
 
		strcpy(szLinkTableKey,fn_szGetFamiliesDataPath());
		strcat(szLinkTableKey,"\\");
		strcat(szLinkTableKey, szFileName);
		strcat(szLinkTableKey, szP0+1);
 
 
		hFamily = NULL;
		p_cEnd = strchr (szFileName, '\\');
		if (p_cEnd)
		{
			tdObjectType otType;
 
			*p_cEnd = 0;
			otType = fn_otFindFamilyTypeOfFamilyTypeName (szFileName);
			if (otType != -1)
			hFamily = fn_hFindFamily (otType);
		}
    
		strcpy(szStateSectionName,fn_szGetFamiliesDataPath());
	    strcat(szStateSectionName,"\\");
		strcat(szStateSectionName, szP1);
		stLinkValue=SCR_fnp_st_Link_CreateOrGetLinkFromKey( &g_st3DOSLinkTable.stState, szStateSectionName);
		/* Compute complete name of current section*/
		/* Get state from link table*/
		hState=(tdxHandleToState)stLinkValue->ulValue;
		/* Get index of state in array of state*/
		LST3_M_StaticWhatElementNumber(hState,&hFamily->hForStateArray,hState2,uwIndex);
 
		stLinkValue=SCR_fnp_st_Link_CreateOrGetLinkFromKey( &g_st3DOSLinkTable.stACZ, szLinkTableKey); 
 
		p_stZSE->uwIndex=uwIndex;
 
		fn_vInsertZSEInZoneSetList(p_stCsaList,p_stZSE);
 
		LDT_M_free(szP0);
		LDT_M_free(szP1);
		LDT_M_free(szFileName);
		LDT_FreeRefValues( pVal );
  }
 
}
 
 
 
/*****************************************************************
Function name   : fn_iCreateNewActivationZone
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateNewActivationZone( LDT_tdst_Link *pLink )
{
  tdstZoneSetList * p_stZoneSetList;
 
	p_stZoneSetList = fn_p_stCreateZoneSetListElement(); 
	pLink->pObject = (void*)p_stZoneSetList;
 
  return 0;
}
 
 
extern void fn_vInsertZdxIndexInZoneSetList(ZDX_tdxHandleToZoneSetList _hZoneSetList,ZDX_tdxHandleToZdxIndexList _hIndex);
 
 
/*****************************************************************
Function name   : fn_iLoadNewActivationZone
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadNewActivationZone( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  tdstZdxIndexList * p_stZdxIndexList;
  tdstZoneSetList * p_stZoneSetList = (tdstZoneSetList *)pLink->pObject;
	int nLen;
	char a255_cLinkKey[MAX_PATH];
 
	nLen = LDT_ComputeSectionName( pLink, a255_cLinkKey );
  SCR_M_v_Link_SetAdditionalLong(
     SCR_fnp_st_Link_SetValue(&g_st3DOSLinkTable.stACZ,	a255_cLinkKey, (unsigned long)p_stZoneSetList),
     1,  nLen);
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry : ActivationZone */
  	  {
 
				p_stZdxIndexList = fn_p_stCreateZdxIndexListElement();
				p_stZdxIndexList->uwZdxIndex = (unsigned short)atoi(LDT_szGetParam(1));
 
				fn_vInsertZdxIndexInZoneSetList(p_stZoneSetList, p_stZdxIndexList);
 
  	  }
  	}
  }
 
		
  return 0;
}
