#if defined (WIN32)
#include <stdio.h>
#endif /* WIN32 */
 
/* to be Friend*/
#define D_ObjsTbls_Define
#define D_State_Define
 
#include "ToolsCPA.h"
#include "Sna.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/3DOSLkTb.h"
#include "Structur/Objects.h"
#include "Structur/ErrGame.h"
#include "Structur/GameScpt.h"
#include "Structur/MemGame.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
 
#include "ObjType.h"
 
#include "Basic.h"
#include "Family.h"
#include "ZeMem.h"
#include "Structur/ObjsTbls.h"
#include "TBLLoad.h"
#include "Structur/anim_s.h"
 
#include "ldt.h"
 
#define lTagVersionNumber 'sreV'
#define lTagMaximumIndex 'ixaM'
#define lTagDuplicate 'lpuD'
 
#define lTagPHY 'HP'
#define lTagMIRROR 'RRIM'
#define lTagLipsSynchro 'spiL'
#define lTagCustomZoom 'tsuC'
 
#define lTagObjectType 'ejbO'
#define lTagGenericEvent 'eneG'
#define lTagFirstCall 'sriF'
#define lTagPeriod 'ireP'
#define lTagPriority 'oirP'
#define lTagSoundEvent 'nuoS'
 
/* global for TBL Link Table*/
extern SCR_tdst_Link_Table TBL_g_stLinkTable;
 
HREF g_hRefTBL;
#define iTBLGeneral 1
 
 
 
/*****************************************************************
Function name   : fn_iCreateTBLFile
Description     : call-back for header section of TBL script file
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateTBLFile( LDT_tdst_Link *pLink ) /* for .tbl files*/
{
 
			MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLList , 0 );
	pLink->pObject = (void*)fn_hObjectsTablesListAlloc();
  
	return 0;
}
 
 
 
/*****************************************************************
Function name   : fn_iLoadTBLHeader
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadTBLHeader( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	char szSectionName[_MAX_PATH];
	tdxHandleToObjectsTablesList h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)pLink->pParent->pObject;
 
	LDT_SetFileLong(10, 0); /* flag Duplicate*/
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
  	      case  lTagVersionNumber : /* VersionNumber */
  	      {
						/* not used yet*/
						/*gs_wTBLFileVersion = (short)atoi(LDT_szGetParam(1));*/
  	      }
  	      break;
  	      case  lTagMaximumIndex : /* MaximumIndex */
  	      {
        			MMG_fn_vBeginMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , h_ObjectsTablesListElement );
						fn_vObjectsTablesAlloc(h_ObjectsTablesListElement, (unsigned short)atoi(LDT_szGetParam(1)) );
        			MMG_fn_vEndMemoryInfo();
 
                    strcpy (szSectionName, fn_szGetFamiliesDataPath());
                    strcat(szSectionName, "\\");
                    strcat(szSectionName, pLink->szFile);
 
						SCR_fnp_st_Link_SetValue(&TBL_g_stLinkTable, szSectionName, (unsigned long)h_ObjectsTablesListElement);
            h_ObjectsTablesListElement -> wZDxUsed = 0;    /* For the moment, no Zdd and no Zde*/
  	      }
  	      break;
  	      case  lTagDuplicate : /* Duplicate */
  	      {
						LDT_SetFileLong(10, 1); /* flag Duplicate*/
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
 
  return 0;
}
 
 
 
/*****************************************************************
Function name   : fn_iLoadTBLGeneral
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadTBLGeneral( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	long l_NumberInTable;
	tdxHandleToObjectsTablesList h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)pLink->pParent->pObject;
	long l_FlagDuplicate;
/*  static int i = 0;*/
/*  char s[180], s1[300];*/
 
	l_NumberInTable = atoi(LDT_szGetSectionName());
	pLink->pObject = pLink->pParent->pObject;
	l_FlagDuplicate = LDT_GetFileLong(10);
 
/*  LDT_ComputeSectionName( pLink , s1 );*/
/*		i++;*/
/*    sprintf(s, "%d.%s\n", i, s1);*/
/*    OutputDebugString(s);*/
 
 
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
				if ( *(short *)szEntry == lTagPHY )
				{
					PO_tdxHandleToPhysicalObject  hPO;
 
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wTypeOfTarget = C_wTdO_PhysicalObject;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].h_Target = hPO = CS_fn_xLoadPhysicalObject(LDT_szGetParam(1));
 
					LDT_AddToRefsTable( g_hRefTBL, (void *)pLink, iTBLGeneral, 2, l_FlagDuplicate, hPO );  
 
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].uwChannelNumber=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucPhoneme=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucIntensity=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucExpression=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].p_stCustomZoom=NULL;
#ifdef _DEBUG
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wCounter=0;
#endif
 
 
				}
				else if ( *(long *)szEntry ==  lTagMIRROR)
				{
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wTypeOfTarget = C_wTdO_Mirror;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].h_Target = CS_fn_xLoadPhysicalObject(LDT_szGetParam(1));
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].uwChannelNumber=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucPhoneme=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucIntensity=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucExpression=255;
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].p_stCustomZoom=NULL;
#ifdef _DEBUG
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wCounter=0;
#endif
 
				}
				else if ( *(long *)szEntry ==  lTagLipsSynchro)
				{
					char * szP2;
                      MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , h_ObjectsTablesListElement );
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].uwChannelNumber=(unsigned long)TMP_M_p_Malloc(strlen(LDT_szGetParam(1))+1);
					strcpy((char *)h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].uwChannelNumber, LDT_szGetParam(1));
					szP2 = LDT_szGetParam(2);
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucPhoneme=(unsigned char)(szP2[0]-'A');
					h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucIntensity=(unsigned char)atoi(LDT_szGetParam(3));
					if (!stricmp("Neutral", LDT_szGetParam(4)))
						h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].ucExpression=0;
#ifdef _DEBUG
		      h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wCounter=0;
#endif
				}
				else if ( *(long *)szEntry ==  lTagCustomZoom)
				{
					float f0,f4,f8;
 
					f0 = (float)atof(LDT_szGetParam(1));
					f4 = (float)atof(LDT_szGetParam(5));
					f8 = (float)atof(LDT_szGetParam(9));
 
					if( f0!=1.0f || f4!=1.0f || f8!=1.0f )
					{
          				MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , h_ObjectsTablesListElement );
						h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].p_stCustomZoom = (MTH3D_tdstVector *)M_p_GameMallocInHLM(sizeof(struct MTH3D_tdstVector_));
						MTH3D_M_vSetVectorElements(h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].p_stCustomZoom, f0, f4, f8);
					}		
 
				}
  	  }
  	}
  }
 
  return 0;
}
 
 
 
 
/*****************************************************************
Function name   : fn_vTBLPostProcessLDT
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : void 
Argument        : HREF hRef
*****************************************************************/
void fn_vTBLPostProcessLDT( 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 )
  {
		switch( iType )
		{
			case iTBLGeneral:
			{
				tdxHandleToObjectsTablesList h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)pLink->pObject;
				PO_tdxHandleToPhysicalObject  hPO = (PO_tdxHandleToPhysicalObject)pVal[1];
        
				if (PO_fn_hGetCollideSet (hPO))
        {
          if (PCS_fn_hGetGeoObjOfPhysicalCollSet (C_ucTypeZdd , PO_fn_hGetCollideSet (hPO)))  
						h_ObjectsTablesListElement->wZDxUsed |= C_wObjectTableHasZdd;
          if (PCS_fn_hGetGeoObjOfPhysicalCollSet (C_ucTypeZde , PO_fn_hGetCollideSet (hPO)))  
						h_ObjectsTablesListElement->wZDxUsed |= C_wObjectTableHasZde;
        }
				
				if((long)(*pVal)) 
					fn_vDuplicateObjectTable(h_ObjectsTablesListElement);
 
        LDT_FreeRefValues(pVal);
			}
			break;
 
		}
  }
 
}
 
extern HREF hRefCSB;
void fn_PostProcess1CSB(HREF hRefCSB);
 
extern struct tdstEventInTable_* GAM_fn_p_stCreateEventInTBL();
 
 
/*****************************************************************
Function name   : fn_iLoadTBLEvent
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadTBLEvent( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	tdxHandleToObjectsTablesList h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)pLink->pParent->pObject;
	long l_NumberInTable;
	tdstEventInTable* p_stEvent;
 
	l_NumberInTable = atoi(LDT_szGetSectionName());
	
	h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].wTypeOfTarget=C_wTdO_Event;
  				MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , h_ObjectsTablesListElement );
	p_stEvent = GAM_fn_p_stCreateEventInTBL();
	h_ObjectsTablesListElement->d_stObjectsTable[l_NumberInTable-1].h_Target=(void*)p_stEvent;
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
 
  	      case  lTagObjectType : /* ObjectType */
  	      {
						if (!stricmp("GenericEvent",LDT_szGetParam(1)))
						{
							p_stEvent->ucTypeOfEvent=C_ucGENERIC_EVENT;
						}
						else if(!stricmp("MechanicEvent",LDT_szGetParam(1)))
						{
							p_stEvent->ucTypeOfEvent=C_ucMECHANIC_EVENT;
						}
						else if(!stricmp("SoundEvent",LDT_szGetParam(1)))
						{
							p_stEvent->ucTypeOfEvent=C_ucSOUND_EVENT;
						}
						else if(!stricmp("GenerateEvent",LDT_szGetParam(1)))
						{
							p_stEvent->ucTypeOfEvent=C_ucGENERATE_EVENT;
						}
 
  	      }
  	      break;
  	      case  lTagGenericEvent : /* GenericEvent */
  	      {
						long lEvent;
						char * szP1 = LDT_szGetParam(1);
 
						lEvent=(long)p_stEvent->p_xEvent;
						if (!strcmp(szP1,"TakeObjectEvent"))
						{
							lEvent|=C_ucTAKE_OBJECT_EVENT;
						}
						else
						if (!strcmp(szP1,"TakeInObjectEvent"))
						{	
							lEvent|=C_ucTAKE_IN_OBJECT_EVENT;
						}
						else
						if (!strcmp(szP1,"TakeOutObjectEvent"))
						{	
							lEvent|=C_ucTAKE_OUT_OBJECT_EVENT;
						}
						else
						if (!strcmp(szP1,"DropObjectEvent"))
						{	
							lEvent|=C_ucDROP_OBJECT_EVENT;
						}
						else
						if (!strcmp(szP1,"ThrowObjectEvent"))
						{	
							lEvent|=C_ucTHROW_OBJECT_EVENT;
						}
						else if ( (strncmp( szP1,"User", 4 ) == 0) && (szP1[5] == 0) )
						{
							unsigned char ucUserEvent;
							ucUserEvent = (unsigned char) (szP1[4] - '0');
							if (ucUserEvent < 8)
								lEvent |= (unsigned char) (C_ucEvent_User0 + ucUserEvent);
						}
						p_stEvent->p_xEvent=(void*)lEvent;
 
  	      }
  	      break;
  	      case  lTagFirstCall : /* FirstCall */
  	      {
						p_stEvent->ucFirstCall = (unsigned char)atoi(LDT_szGetParam(1));
  	      }
  	      break;
  	      case  lTagPeriod : /* Period */
  	      {
						p_stEvent->ucPeriod = (unsigned char)atoi(LDT_szGetParam(1));
  	      }
  	      break;
  	      case  lTagPriority : /* Priority */
  	      {
						p_stEvent->ucPriority = (unsigned char)atoi(LDT_szGetParam(1));
  	      }
  	      break;
  	      case  lTagSoundEvent : /* SoundEvent */
  	      {
			LDT_LoadSection(LDT_szGetParam(1));
			LDT_AddToRefsTable(hRefCSB,pLink,1,1,p_stEvent);
/* -> OS : THIS CODE SHOULD BE CHANGED 27-Aug-98 (is not used)*/
/*						SCR_tdst_Cxt_Values* p_stValue;*/
/**/
/*						p_stValue=SCR_fnp_st_RdL0_AnalyseSection(LDT_szGetParam(1), SCR_CDF_uw_Anl_Normal);*/
/*						p_stEvent->p_xEvent=(void*)SCR_M_ul_RdL0_ExtractLongValue(p_stValue, 0);*/
/*						p_stEvent->ulBinaryEventId=SCR_M_ul_RdL0_ExtractLongValue(p_stValue, 1);	// used for the binarisation only*/
/* <- OS : THIS CODE SHOULD BE CHANGED 27-Aug-98*/
			      /* we save the id of the sound event and the pointer on the structure that contains the id and the pointer of the sound event	*/
/*			      SNA_fn_vWriteSoundEntryInSoundTableFile(SND_fn_ulGetEventBinIdFromPointer(p_stEvent->p_xEvent),(unsigned long)p_stEvent);*/
/*			      p_stEvent->ulBinaryEventId=SND_fn_ulGetEventBinIdFromPointer(p_stEvent->p_xEvent);*/
 
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
  return 0;
}
 
 
 
/*BEGIN FS 10/7/98*/
void fn_PostProcess1CSB(HREF hRefCSB)
{
	tdstEventInTable* p_stEvent;
	int iType=1;
	short shCount=1;
	long *pVal;
	LDT_tdst_Link *pObj, *pGetFrom;
 
 
	while (!(LDT_GetRefFromTable(hRefCSB, &pObj, &pGetFrom, &iType, &shCount, &pVal )))
	{
		switch(iType)
		{
		case 10:
			SND_fn_bEndLoadBanks();
			break;
		case 1:
			p_stEvent=(tdstEventInTable*)pVal[0];
			LDT_FreeRefValues(pVal);
			p_stEvent->p_xEvent=(void*)pGetFrom->pObject;
			p_stEvent->ulBinaryEventId=(unsigned long)LDT_RemoveLinkValue(pGetFrom);	/* used for the binarisation only*/
			SNA_fn_vWriteSoundEntryInSoundTableFile(SND_fn_ulGetEventBinIdFromPointer(p_stEvent->p_xEvent),(unsigned long)p_stEvent);
			p_stEvent->ulBinaryEventId=SND_fn_ulGetEventBinIdFromPointer(p_stEvent->p_xEvent);
		}
	}
}
/*END FS 10/7/98*/
