/*******************************************************/
/**** For the structures and variables declarations ****/
/*******************************************************/
#define D_StdGame_StructureDefine
#define D_StdGame_VariableDefine
 
#define D_ObjsTbls_Define
 
#include "ToolsCPA.h"
 
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/3DOSLkTb.h"
#include "Structur/MemGame.h"
#include "Structur/ErrGame.h"
#include "Structur/Objects.h"
#include "Structur/GameScpt.h"
#include "Structur/EngMode.h"
#include "structur/StdObjSt.h"
 
#include "safe.h" /*BART*/
#include "Basic.h"
#include "Family.h"
#include "ObjInit.h"
#include "ObjType.h"
#include "ZeMem.h"
#include "MainChar.h"
#include "ToolCam.h"
#include "Structur/ObjsTbls.h"
#include "ChanList.h"
#include "CHLLoad.h"
 
#include "ldt.h"
 
/*******************************************************/
#define C_TRANSPARENCY_ZONE_MIN		70
#define C_TRANSPARENCY_ZONE_MAX		80
/*******************************************************/
 
 
HREF g_hRefStdGame; 
 
/*****************************************************************
Name: 
Description: Create callback for LDT
Author: Mircea Petrescu
Date: 8/21/98
Modified: Scripa Ovidiu 09/01/98
*****************************************************************/
int fn_iCreateStdGame( LDT_tdst_Link *pLink )
{
  MS_tdxHandleToStandardGame h_StdGame;
  struct tdstEngineObject_ *p_stEngineObject=(struct tdstEngineObject_ *)pLink->pParent->pObject;
 
		MMG_fn_vBeginMemoryInfo (MMG_C_lTypeMiniStructure , MMG_C_lSubTypeStdGame , p_stEngineObject);
  h_StdGame = fn_h_StdGameRealAlloc();
		MMG_fn_vEndMemoryInfo ();
 
 
  pLink->pObject=(void *)h_StdGame;
 
  p_stEngineObject->h_StandardGame=h_StdGame;
  /* JO 04/11/97*/
  fn_ucStandardGameSetPlatFormType(h_StdGame,(unsigned char)0);
  /*fn_ucStandardGameSetSpecialPositionFlag(h_StdGame,(unsigned char)0);*/
	fn_vStandardGameSetCustomBits(h_StdGame,(unsigned long)GAM_C_CustBitUsesTransparencyZone);
	fn_vStandardGameSetTransparencyZoneMin(h_StdGame,C_TRANSPARENCY_ZONE_MIN);
	fn_vStandardGameSetTransparencyZoneMax(h_StdGame,C_TRANSPARENCY_ZONE_MAX);
 
  return 0;
}
 
extern void fn_vLoadFamilyLDT(struct tdstFamilyList_ *p_stFamily);
 
/*****************************************************************
Name: 
Description: Load callback for LDT
Author: Mircea Petrescu
Date: 8/21/98
Modified: Scripa Ovidiu 09/01/98
*****************************************************************/
int fn_iLoadStdGame( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  MS_tdxHandleToStandardGame h_StdGame=(MS_tdxHandleToStandardGame)pLink->pObject;
  struct tdstEngineObject_ *p_stEngineObject=(struct tdstEngineObject_ *)pLink->pParent->pObject;
	/* .tbl files were loaded in the callback for 3dData section */
  tdxHandleToObjectsTablesList h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)LDT_GetFileLong(20);
	
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
  	      case 'epyT' : /* Type */
  	      {
						 tdxHandleToState h_InitialState=NULL;
						 /*SCR_tdst_Link_Value *p_xIndex;*/
             struct tdstFamilyList_ *p_stFamily = NULL;
             tdObjectType otFamilyType;
 
						 fn_vStandardGameSetPersonalType(h_StdGame,fn_otFindOrAddPersonalTypeOfPersonalTypeName(LDT_szGetParam( 1 )));
						 fn_vStandardGameSetFamilyType(h_StdGame,fn_otFindOrAddFamilyTypeOfFamilyTypeName(LDT_szGetParam( 3 )) );
						 fn_vStandardGameSetModelType(h_StdGame,fn_otFindOrAddModelTypeOfModelTypeName(LDT_szGetParam( 2 )) );
						 
             /* LoadFamilyLDT loads .chl, .bdv, and .sta files asociated with this family*/
             
             otFamilyType = fn_otStandardGameGetFamilyType(h_StdGame);
             p_stFamily = fn_hFindFamily(otFamilyType);
             if (p_stFamily==NULL)
             {
				MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeFamily , 0 );
				p_stFamily = (struct tdstFamilyList_*)M_p_GameMallocInHLM(sizeof(struct tdstFamilyList_));
                p_stFamily->otObjectFamilyType = otFamilyType;
 
                fn_vInitObjectsTablesList(p_stFamily); /* tmp SD Gizmo 14/04/97*/
 
                fn_vLoadFamilyLDT(p_stFamily);
					      
                LDT_AddToRefsTable( g_hRefStdGame, (void *)pLink, 0, 1, p_stFamily );  
 
                LST2_M_DynamicAddTail(&g_stEngineStructure.hFamilyList,p_stFamily);
                p_stFamily->ucPriority = GAM_g_ucHLMMemoryBlock;    
             }
             fn_v3dDataSetFamily(p_stEngineObject->h_3dData, p_stFamily);		 
 
						 if(h_ObjectsTablesListElement!=NULL)
						 {
							 fn_v3dDataSetCurrentObjectsTable(p_stEngineObject->h_3dData,h_ObjectsTablesListElement);
							 fn_v3dDataSetInitialObjectsTable(p_stEngineObject->h_3dData,h_ObjectsTablesListElement);
							 fn_vAddAnObjectsTablesInList(fn_h3dDataGetFamily(p_stEngineObject->h_3dData),h_ObjectsTablesListElement);
						 }
						 else
						 {
							 fn_v3dDataSetCurrentObjectsTable(p_stEngineObject->h_3dData,fn_vGetDefaultObjectsTableOfFamily(fn_h3dDataGetFamily(p_stEngineObject->h_3dData)));
							 fn_v3dDataSetInitialObjectsTable(p_stEngineObject->h_3dData,fn_vGetDefaultObjectsTableOfFamily(fn_h3dDataGetFamily(p_stEngineObject->h_3dData)));
						 }
						 fn_vCheckObjectsTables(p_stEngineObject); /*Gizmo  do nothing in release*/
 
             LDT_AddToRefsTable( g_hRefStdGame, (void *)pLink, 1, 1, p_stEngineObject );  
 
 
  	      }
  	      break;
  	      case 'PtiH' : 
  	      {
						int iLen=strlen( szEntry );
						switch( iLen )
						{
  						case 13 : /* HitPointsInit */
  						{
						   fn_vStandardGameSetHitPoints(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
  						}
  						break;
  						case 9 : /* HitPoints */
  						{
  						}
  						break;
  						case 12 : /* HitPointsMax */
  						{
  						}
  						break;
  						case 15 : /* HitPointsMaxMax */
  						{
						   fn_vStandardGameSetHitPointsMaxMax(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
  						}
  						break;
  						case 17 : /* HitPointsMaxInit */
  						{
						   fn_vStandardGameSetHitPointsMax(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
  						}
  						break;
						}
					}
					break;
  	      case 'tinI' : /* InitFlags */
  	      {
						 char *sz=LDT_szGetParam(1);
						 if (!stricmp(sz,C_ValueStdInitFlagNeverBack))
							fn_vStandardGameSetInitFlagWhenOutOfZone(h_StdGame,OI_NeverBackWhenTaken);
						 else if (!stricmp(sz,C_ValueStdInitFlagGoOutOfZone))
							fn_vStandardGameSetInitFlagWhenOutOfZone(h_StdGame,OI_WhenPlayerGoOutOfActionZone);
						 sz=LDT_szGetParam(2);
						 if (!stricmp(sz,C_ValueStdInitFlagNeverBack))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_NeverBackWhenTaken);
						 else if (!stricmp(sz,C_ValueStdInitFlagAlways))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_Always);
						 else if (!stricmp(sz,C_ValueStdInitFlagGoOutOfZone))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenPlayerGoOutOfActionZone);
						 else if (!stricmp(sz,C_ValueStdInitFlagPlayerDead))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenPlayerIsDead);
						 else if (!stricmp(sz,C_ValueStdInitFlagMapLoaded))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenMapJustLoaded);
						 else if (!stricmp(sz,C_ValueStdInitFlagSavedGameLoaded))
							fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenSavedGameJustLoaded);
  	      }
  	      break;
  	      case 'talP' : /* PlatFormType */
  	      {
				     fn_ucStandardGameSetPlatFormType(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
  	      }
  	      break;
  	      case 'tsuC' : /* CustomBitsInit */
  	      {
			     fn_vStandardGameSetCustomBits(h_StdGame,(unsigned long)atoi( LDT_szGetParam( 1 ) ));
  	      }
  	      break;
  	      case 'apaC' : /* Capabilities */
  	      {
						unsigned long ulReturn;
						char* dummy;
						ulReturn = strtoul(LDT_szGetParam(1), &dummy, 2);
						fn_vStandardGameSetCapabilities(h_StdGame, ulReturn);
  	      }
  	      break;
  	      case 'carT' : /* TractionFactor */
  	      {
				    fn_vStandardGameSetTractionFactor(h_StdGame, (unsigned char)atoi( LDT_szGetParam( 1 ) ));
  	      }
  	      break;
  	      case 'narT' : /* TransparencyZone */
  	      {
						fn_vStandardGameSetTransparencyZoneMin(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
						fn_vStandardGameSetTransparencyZoneMax(h_StdGame,(unsigned char)atoi( LDT_szGetParam( 2 ) ));
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
 
  return 0;
}
 
 
void fn_vStdGamePostProcessLDT( 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 0:
      {
        struct tdstFamilyList_ *p_stFamily ;
	      tdxHandleToObjectsTablesList hObjectsTable;
        int i, j;
 
		    p_stFamily = (struct tdstFamilyList_ *)(*pVal);
    		    
	      LST2_M_StaticForEachElementOf(&p_stFamily->hForObjectsTablesList,hObjectsTable,i)
	      {	
		      for (j=0;j<hObjectsTable->wNumberOfElement;j++)
		      { char szFamily[255],*szChannel;
		        SCR_tdst_Link_Value * _p_stLinkValue;
 
			      if (
				        (
				          (hObjectsTable->d_stObjectsTable[j].wTypeOfTarget==C_wTdO_PhysicalObject)
					      ||
					      (hObjectsTable->d_stObjectsTable[j].wTypeOfTarget==C_wTdO_Mirror)
				        )
				       && (hObjectsTable->d_stObjectsTable[j].uwChannelNumber!=255)
				      )
			      {	szChannel=(char *)(hObjectsTable->d_stObjectsTable[j].uwChannelNumber);
 
				      strcpy(szFamily,fn_szFindFamilyTypeNameOfFamilyType(p_stFamily->otObjectFamilyType));
				      strcat(szFamily,"-");
				      strcat(szFamily,szChannel);
 
				      _p_stLinkValue = SCR_fnp_st_Link_SearchKey(&CHL_g_stLinkTable,szFamily);
				      if(_p_stLinkValue)
				      {
					      hObjectsTable->d_stObjectsTable[j].uwChannelNumber = (unsigned long)SCR_M_ul_Link_GetValue(_p_stLinkValue);
				      }
				      else
				      {
					      hObjectsTable->d_stObjectsTable[j].uwChannelNumber = (unsigned short)C_ucUnknownChannel;
				      }
                MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , hObjectsTable );
				      TMP_M_Free(szChannel);
			      }				
		      }
 
        }
        LDT_FreeRefValues(pVal);
      }
      break;
      case 1:
      {
         struct tdstEngineObject_ *p_stEngineObject=(struct tdstEngineObject_ *)(*pVal);
         tdxHandleToState h_InitialState = NULL;
				 SCR_tdst_Link_Value *p_xIndex;
 
				 h_InitialState = fn_h3dDataGetInitialState(p_stEngineObject->h_3dData);
				 if(h_InitialState)
				 {
					 p_xIndex = SCR_fnp_st_Link_SearchKey(&g_st3DOSLinkTable.stState,(char *)h_InitialState);
					 if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_Initialized)
  				 {
	     			 MMG_fn_vAddMemoryInfo( MMG_C_lTypeMiniStructure , MMG_C_lSubType3dData , p_stEngineObject);
						 TMP_M_Free(h_InitialState);
						 fn_v3dDataSetInitialState(p_stEngineObject->h_3dData,(tdxHandleToState)(p_xIndex->ulValue));
					 }
           else
           {
					   fn_v3dDataSetInitialState(p_stEngineObject->h_3dData,NULL);
					   M_GameWarningError(E_uwGamePLA_BadInitialState);
           }
 
				 }
         LDT_FreeRefValues(pVal);
 
      }
      break;
    }
 
 
  }
 
}
