/*=========================================================================
*
* ObjLoad.c	- Loading functions for OBJ file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "ObjLoad.h"

#include "conventi.h"
#include "print.h"
#include "system.h"

#include "Banks.h"

#include "PhySave.h"
#include "ZooSave.h"


//--- Global statics --------------------------------------------------------

MLT_tdstObjElement **gs_hModule			= NULL; 
long				 gs_lModuleIndex	= 0;


//--------------------------------------------------------------------


/****************************************************************************
*		Description:	register loading functions for OBJ file
*
*		Parameters:		sRawData	: source directory
*						sGameData	: destination directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vInitOBJLoad ()
{    
  SCR_fn_v_RdL0_RegisterCallback(M_A3dObjectsTableELEMENT, MLT_xLoadObj, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_A3dObjectsTableGENERAL, MLT_xLoadObjHeader, SCR_CRC_c_RdL0_ForSection);
}


/****************************************************************************
*		Description:	Load all the object in Obj file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vLoadModuleLib()
{
  SCR_tdst_Cxt_Values	*p_stVal;
  xString				szFileName;

  //Inits 
  sprintf(szFileName, "%s.obj", g_sFileIn);

  p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);
}


/****************************************************************************
*		Description:	Delete the list of objects
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vDeleteModuleLib()
{
  long i;
 
  for (i=0; i<gs_lModuleIndex; i++)
    free(gs_hModule[i]);

  free(gs_hModule);
}


/****************************************************************************
*		Description:	Load Obj file header
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadObjHeader(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
			gs_hModule = NULL;
			break;

		case SCR_EA_Anl_Entry:
			if (strcmp(szAction,M_OBJActionMaximumIndex)==0)
			{
		        gs_lModuleIndex = atol(szParams[0]);
				gs_hModule = (MLT_tdstObjElement**)calloc(gs_lModuleIndex, sizeof(MLT_tdstObjElement*));
			}
			break;

		case SCR_EA_Anl_EndSection:
    break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


/****************************************************************************
*		Description:	Load Obj file 
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadObj(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  static xString	sFile, sSectionObj, sIdent, sIdent2, sData, sObject;
  static xString	sTete,sLetter,sNeutral;
  static double		a9_dCustomZoom[9];
  static long		lNumberInTable, lValue, lVal, lFirstCall, lPriority, lPeriod;
  static BOOL		bIsObject;
  static BOOL		bCustomZoom;
  static BOOL		bNocol;
  static int		iType;
  xString			sSectionMod, sSection, sAction, sFile2, sFamily;
  char				*p_Char1;

  switch (cType)
  {
		case SCR_EA_Anl_BeginSection:
			lNumberInTable = atol(szAction);
			sprintf(sSectionObj, "%s", SCR_M_RdL0_GetCompleteSectionNameR(0));
			sprintf(sTete, "");
			sprintf(sLetter, "");
			sprintf(sNeutral, "");
			lValue = 0;
			bCustomZoom = FALSE;
			bIsObject = FALSE;
			iType=-1;
			lVal=-1;
			lFirstCall=-1;
			lPeriod=-1;
			lPriority=-1;
			sprintf(sObject, "");
			sprintf(sData, "");
			bNocol=FALSE;
		break;

		case SCR_EA_Anl_Entry:
			if (strcmp(szAction,M_OBJActionStatus)==0)
			{
				if(strcmp(szParams[0], M_Object) == 0)
					bIsObject = TRUE;
			}
			
			if (strcmp(szAction,M_OBJActionObjectType)==0)
			{
		      if(strcmp(szParams[0], M_3dObject) == 0)
				iType=0;
			  if(!strcmp(szParams[0], M_SoundEvent))  
				iType=1;
			  if(!strcmp(szParams[0], M_GenericEvent))	
				iType=3;
			  if(!strcmp(szParams[0], M_GenerateEvent))	
				iType=4;
			  if(!strcmp(szParams[0], M_CameraEvent))	
				iType=5;
			  if(!strcmp(szParams[0], M_MechanicEvent))	
				iType=2;
			  if(!strcmp(szParams[0], M_Fake))			
				iType=6;
			}
			if (strcmp(szAction,M_OBJActionSoundReference)==0)	
			{
			    lVal = atol(szParams[0]);
			}
			if (strcmp(szAction,M_OBJActionFirstCall)==0)	
			{
			    lFirstCall = atol(szParams[0]);
			}
			if (strcmp(szAction,M_OBJActionPeriod)==0)	
			{
			    lPeriod = atol(szParams[0]);
			}
			if (strcmp(szAction,M_OBJActionPriority)==0)	
			{
			    lPriority = atol(szParams[0]);
			}
			if (strcmp(szAction,M_OBJActionObject)==0)		
			{
				sprintf(sObject, "%s", szParams[1]);
			}
			if (strcmp(szAction,M_OBJActionData)==0)		
			{
				sprintf(sData, "%s", szParams[0]);
			}
			if (strcmp(szAction,M_OBJActionBank)==0)
			{
				sprintf(sFile, "%s", szParams[1]);
			}
			if (strcmp(szAction,M_OBJActionObject)==0)
			{
				sprintf(sIdent, "%s", szParams[1]);
			}
			if(strcmp(szAction, M_OBJActionLipsSynchro)==0)
			{
				sprintf(sTete, "%s", szParams[0]);
				sprintf(sLetter, "%s", szParams[1]);
				lValue = atol(szParams[2]);
				sprintf(sNeutral, "%s", szParams[3]);
			}
			if(strcmp(szAction, M_OBJActionCustomZoom)==0)
			{
				long i;

				bCustomZoom = TRUE;
				for(i=0 ; i<9 ; i++)
				a9_dCustomZoom[i] = atof(szParams[i]);
			}
			break;

		case SCR_EA_Anl_EndSection:
			if(!bIsObject)
			{
				break;
			}

			if (iType == -1)
			{
				break;
			}

			sprintf(sFamily, "%s", g_sFileMaterial);

			sprintf(sFile2, "%s", sFile);

			p_Char1=sIdent;
			MLT_vRemoveFlags(&p_Char1);
			strcpy(sIdent, p_Char1);
			p_Char1=sIdent;
			MLT_vRemoveGeomProp(&p_Char1);
			strcpy(sIdent, p_Char1);

		    if (strstr(sIdent, "!NOCOL") || strstr(sIdent, "!nocol"))
			{	p_Char1 = MLT_vRemoveFromName(sIdent);
				strcpy(sIdent, p_Char1);
				bNocol = TRUE;
			}

			SCR_fn_v_RdL0_ComputeSectionName(sSectionMod, sFile2, M_Geometric, sIdent);

			if((iType==0) && (!MLT_bFindFileInBankInGameData(sFile, sIdent, sFamily)))
			{
				MLT_vOutput( C_ComRes_cErrorLine, "\nError : Can't load %s in Banks directory", sSectionMod);
			}
	
			gs_hModule[lNumberInTable-1] = (MLT_tdstObjElement*)malloc(sizeof(MLT_tdstObjElement));
   			sprintf(gs_hModule[lNumberInTable-1]->sSectionObj, "%s", sSectionObj);
			sprintf(gs_hModule[lNumberInTable-1]->sSectionMod, "%s", sSectionMod);
			gs_hModule[lNumberInTable-1]->bFlag = bNocol;
			if (iType==0) //bIs3dObject) 
			{	gs_hModule[lNumberInTable-1]->iType = 0;
				sprintf(gs_hModule[lNumberInTable-1]->sTete,"%s", sTete);
				sprintf(gs_hModule[lNumberInTable-1]->sLetter,"%s", sLetter);
				sprintf(gs_hModule[lNumberInTable-1]->sNeutral,"%s", sNeutral);
				gs_hModule[lNumberInTable-1]->lValue = lValue;
			}

			if (iType==1) //bIsSoundEvent) 
			{	gs_hModule[lNumberInTable-1]->iType = 1;
				gs_hModule[lNumberInTable-1]->lSoundReference = lVal;  
			}
			if (iType==2) //bIsMechanicEvent) 
			{	gs_hModule[lNumberInTable-1]->iType = 2;
				strcpy(gs_hModule[lNumberInTable-1]->sObject, sObject);  
			}
			if (iType==3)//bIsGenericEvent)
			{	gs_hModule[lNumberInTable-1]->iType = 3;
				strcpy(gs_hModule[lNumberInTable-1]->sData, sData);	
			}
			if (iType==4) //bIsGenerateEvent)
			{	gs_hModule[lNumberInTable-1]->iType = 4;
				strcpy(gs_hModule[lNumberInTable-1]->sData, sData);	
			}
			if (iType==5) //bIsCameraEvent)
			{	gs_hModule[lNumberInTable-1]->iType = 5;
				strcpy(gs_hModule[lNumberInTable-1]->sData, sData);
			}
			if (iType)	//if is an event
			{	gs_hModule[lNumberInTable-1]->lFirstCall = lFirstCall;
				gs_hModule[lNumberInTable-1]->lPeriod = lPeriod;
				gs_hModule[lNumberInTable-1]->lPriority = lPriority;
			}
			if (iType==6) // bIsFake)
	  			gs_hModule[lNumberInTable-1]->iType = 6;

			if(bCustomZoom)
			{ 
				long i;

				gs_hModule[lNumberInTable-1]->bCustomZoom = TRUE;
				for(i=0 ; i<9 ; i++)
					gs_hModule[lNumberInTable-1]->a9_dCustomZoom[i] = a9_dCustomZoom[i];
			}

			sprintf(gs_hModule[lNumberInTable-1]->sTete,"%s", sTete);
			sprintf(gs_hModule[lNumberInTable-1]->sLetter,"%s", sLetter);
			sprintf(gs_hModule[lNumberInTable-1]->sNeutral,"%s", sNeutral);
			gs_hModule[lNumberInTable-1]->lValue = lValue;

		    // save Tbl 
			SCR_fn_v_RdL0_SplitSectionName(gs_hModule[lNumberInTable-1]->sSectionObj, sFile, sAction, sIdent);
			sprintf(sFile, "%s.tbl", g_sFileIn);
			sprintf(sIdent, "%d", lNumberInTable);
			gs_hModule[lNumberInTable-1]->lNumberInTable = lNumberInTable;
			SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Tbl, sIdent);
			SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveTblFromObj, gs_hModule[lNumberInTable-1], SCR_EA_Ntfy_AddOrModifySection);

			// the geometric entries must be saved only for the objects !
			if (!iType)
			{
				// Save Phy 
				SCR_fn_v_RdL0_SplitSectionName(gs_hModule[lNumberInTable-1]->sSectionObj, sFile, sAction, sIdent);
				sprintf(sFile, "%s.phy", g_sFileIn);
				sprintf(sIdent, "PO_%d", lNumberInTable);
				SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Phy, sIdent);
				SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSavePhyFromObj, gs_hModule[lNumberInTable-1], SCR_EA_Ntfy_AddOrModifySection);

				// Save Zoo 
				SCR_fn_v_RdL0_SplitSectionName(gs_hModule[lNumberInTable-1]->sSectionObj, sFile, sAction, sIdent);
				sprintf(sFile2, "%s.zoo", g_sFileIn);
				SCR_fn_v_RdL0_ComputeSectionName(sFile, sFile2, M_AllCollideSets, "");
				sprintf(sIdent2, "CS_%s", sIdent);
				SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_CS, sIdent2);
				SCR_fn_v_SvL1_RegisterNotify(sFile, MLT_vSaveAllCollideSetsSection, NULL, SCR_EA_Ntfy_AddOrModifySection);
				SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveZooFromObj, gs_hModule[lNumberInTable-1], SCR_EA_Ntfy_AddOrModifySection);
			}
			break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


