/*=========================================================================
*
* SctLoad.c	- Loading functions for SCT file
*
* Version			1.0
* Revision date
*
*=======================================================================*/

#include "SctLoad.h"

#include "conventi.h"
#include "util.h"

#include "sctobj.h"


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



/****************************************************************************
*		Description:	register loading functions for SCT file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vInitSCTLoad()
{    
  SCR_fn_v_RdL0_RegisterCallback(M_ENVAndSRF, MLT_xLoadSectorENVAndSRF,SCR_CRC_c_RdL0_ForSection);  
  SCR_fn_v_RdL0_RegisterCallback(M_Border, MLT_xLoadSectorBorder,SCR_CRC_c_RdL0_ForSection);  
  SCR_fn_v_RdL0_RegisterCallback(M_SectorsLink, MLT_xLoadSectorLink,SCR_CRC_c_RdL0_ForSection);  
  SCR_fn_v_RdL0_RegisterCallback(M_StaticLights, MLT_xLoadSectorStaticLights,SCR_CRC_c_RdL0_ForSection);  
  SCR_fn_v_RdL0_RegisterCallback(M_Sector, MLT_xLoadSector,SCR_CRC_c_RdL0_ForSection);  
}


/****************************************************************************
*		Description:	load sector section in SCT 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_xLoadSector(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstSector *p_stSector;
	
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
	      p_stSector = (MLT_tdstSector*)calloc(1,sizeof(MLT_tdstSector));
		  p_stSector->lNbStaticLights = 0;
		  p_stSector->lNbSubmapStaticLights = 0;
      
		  SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)p_stSector);
		  SCR_M_RdL0_SetContextLong(0,0,(unsigned long)p_stSector);
			break;

		case SCR_EA_Anl_Entry:
			break;

		case SCR_EA_Anl_EndSection:
			break;
	}
  return  SCR_ERV_Anl_NormalReturn;
}




/****************************************************************************
*		Description:	load border sub-section in SCT 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_xLoadSectorBorder(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstSector *p_stSector;
	
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			p_stSector->fZMax=MLT_C_InfinitMinus;
			p_stSector->bExistZMax=FALSE;
		break;

		case SCR_EA_Anl_Entry:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			if (strcmp(szAction,M_SCTActionZmaxOfBorder)==0)	
			{	
				//read the value of ZmaxOfBorder
				p_stSector->fZMax = atof(szParams[0]);
				p_stSector->bExistZMax=TRUE;
			}
			break;

		case SCR_EA_Anl_EndSection:
			break;
	}
  return  SCR_ERV_Anl_NormalReturn;
}

/****************************************************************************
*		Description:	load env&surf sub-section in SCT 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_xLoadSectorENVAndSRF (SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstSector *p_stSector;
	
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			p_stSector->lNbSurface = 0; 
			break;

		case SCR_EA_Anl_Entry:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);

			if (strcmp(szAction,M_SCTActionAddLstEnv)==0)
			{
				sprintf(p_stSector->a_sEnv[p_stSector->lNbSurface], "%s", szParams[0]);
				p_stSector->lNbSurface++;
			}
			break;

		case SCR_EA_Anl_EndSection:
			break;
	}
  return  SCR_ERV_Anl_NormalReturn;
}

/****************************************************************************
*		Description:	load links sub-section in SCT 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_xLoadSectorLink(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstSector *p_stSector;
  static long lIndexGraphic;
  static long lIndexCollision;
  static long lIndexActivity;
	
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			if (!strstr(p_fFile->a_szFileName, "_Submap"))
			{
				p_stSector->lNbSecteurGraphic = 0;
				p_stSector->lNbSecteurCollision = 0;
				p_stSector->lNbSecteurActivity = 0;
				p_stSector->lNbSecteurSound = 0;
				p_stSector->lNbSoundEvent = 0;
				p_stSector->bIsVirtual = FALSE;	
				p_stSector->bHasCameraType = FALSE;
				p_stSector->bHasPriority = FALSE;
			}
			else
			{
				p_stSector->lNbSubmapSecteurGraphic = 0;
				p_stSector->lNbSubmapSecteurCollision = 0;
				p_stSector->lNbSubmapSecteurActivity = 0;
				p_stSector->lNbSubmapSecteurSound = 0;
				p_stSector->lNbSubmapSoundEvent = 0;
			}
			break;

		case SCR_EA_Anl_Entry:
			// Here is my own handle
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);

			if (strcmp(szAction,M_SCTActionVirtual)==0)	
			{
				p_stSector->bIsVirtual = TRUE;
			}
			if (strcmp(szAction,M_SCTActionCameraType)==0)	
			{
				p_stSector->bHasCameraType = TRUE;
				p_stSector->lCameraType = atol(szParams[0]);
			}
			if (strcmp(szAction,M_SCTActionPriority)==0)	
			{
				p_stSector->bHasPriority = TRUE;
				p_stSector->lPriority = atol(szParams[0]);
			}

			if (strcmp(szAction,M_SCTActionAddLstGraphic)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sSecteurGraphic[p_stSector->lNbSecteurGraphic], "%s", szParams[0]);
					p_stSector->a_lSecteurGraphic[p_stSector->lNbSecteurGraphic] = atol(szParams[1]);
					p_stSector->a_lSecteurGraphicLook[p_stSector->lNbSecteurGraphic] = atol(szParams[2]);
					p_stSector->lNbSecteurGraphic++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapSecteurGraphic[p_stSector->lNbSubmapSecteurGraphic], "%s", szParams[0]);
					p_stSector->a_lSubmapSecteurGraphic[p_stSector->lNbSecteurGraphic] = atol(szParams[1]);
					p_stSector->a_lSubmapSecteurGraphicLook[p_stSector->lNbSecteurGraphic] = atol(szParams[2]);
					p_stSector->lNbSubmapSecteurGraphic++;
				}
			}
			if (strcmp(szAction,M_SCTActionAddLstActivity)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sSecteurActivity[p_stSector->lNbSecteurActivity], "%s", szParams[0]);
					p_stSector->lNbSecteurActivity++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapSecteurActivity[p_stSector->lNbSubmapSecteurActivity], "%s", szParams[0]);
					p_stSector->lNbSubmapSecteurActivity++;
				}
			}
			if (strcmp(szAction,M_SCTActionAddLstCollision)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sSecteurCollision[p_stSector->lNbSecteurCollision], "%s", szParams[0]);
					p_stSector->lNbSecteurCollision++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapSecteurCollision[p_stSector->lNbSubmapSecteurCollision], "%s", szParams[0]);
					p_stSector->lNbSubmapSecteurCollision++;
				}
			}
			if (strcmp(szAction,M_SCTActionAddLstSound)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sSecteurSound[p_stSector->lNbSecteurSound], "%s", szParams[0]);
					p_stSector->a_lSecteurSound[p_stSector->lNbSecteurSound] = atol(szParams[1]);
					p_stSector->lNbSecteurSound++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapSecteurSound[p_stSector->lNbSubmapSecteurSound], "%s", szParams[0]);
					p_stSector->a_lSubmapSecteurSound[p_stSector->lNbSecteurSound] = atol(szParams[1]);
					p_stSector->lNbSubmapSecteurSound++;
				}
			}
			if (strcmp(szAction,M_SCTActionAddLstSoundEvent)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sSoundEvent1[p_stSector->lNbSoundEvent], "%s", szParams[0]);
					sprintf(p_stSector->a_sSoundEvent2[p_stSector->lNbSoundEvent], "%s", szParams[1]);
					p_stSector->lNbSoundEvent++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapSoundEvent1[p_stSector->lNbSubmapSoundEvent], "%s", szParams[0]);
					sprintf(p_stSector->a_sSubmapSoundEvent2[p_stSector->lNbSubmapSoundEvent], "%s", szParams[1]);
					p_stSector->lNbSubmapSoundEvent++;
				}
			}
			break;

		case SCR_EA_Anl_EndSection:
			break;
	}
  return  SCR_ERV_Anl_NormalReturn;
}

/****************************************************************************
*		Description:	load static lights sub-section in SCT 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_xLoadSectorStaticLights(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstSector *p_stSector;
  static long lIndexGraphic;
  static long lIndexCollision;
  static long lIndexActivity;
	
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			if (!strstr(p_fFile->a_szFileName, "_Submap"))
				p_stSector->lNbSubmapStaticLights = 0;
			else
				p_stSector->lNbStaticLights = 0;
			break;

		case SCR_EA_Anl_Entry:
			SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstSector*,p_stSector);
			if (strcmp(szAction,M_SCTActionAddStaticLight)==0)
			{
				if (!strstr(p_fFile->a_szFileName, "_Submap"))
				{
					sprintf(p_stSector->a_sStaticLights[p_stSector->lNbStaticLights], "%s", szParams[0]);
					p_stSector->lNbStaticLights++;
				}
				else
				{
					sprintf(p_stSector->a_sSubmapStaticLights[p_stSector->lNbSubmapStaticLights], "%s", szParams[0]);
					p_stSector->lNbSubmapStaticLights++;
				}
			}

		case SCR_EA_Anl_EndSection:
			break;
	}
  return  SCR_ERV_Anl_NormalReturn;
}



