/*=========================================================================
*
* SctSave.c	- Saving functions for SCT file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <time.h>

#include "SctSave.h"

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

#include "Levels.h"
#include "Submaps.h"

#include "SpoLoad.h"
#include "SrfSave.h"


//--- Global defines --------------------------------------------------------

xString g_a_sSector[500];
long	g_lNbSector = 0;

xString g_a_sOldSector[500];
long	g_lNbOldSector = 0;


//--- Global Statics --------------------------------------------------------

BOOL gs_bCreatedSct = TRUE;	


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



/****************************************************************************
*		Description:	test if the sector is valid
*
*		Parameters:		sSector	: name of sector to test
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL MLT_bIsGoodSector(char *sSector)
{
  long i;

  for(i=0; i<g_lNbSector; i++)
  {
    if(stricmp(sSector, g_a_sSector[i]) == 0)
      return TRUE;
  }

  return FALSE;
}


/****************************************************************************
*		Description:	test if the string  contain a bad sector
*
*		Parameters:		sSector	: name of sector to test
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL MLT_bContainBadSector (char *_sSector)
{
  xString	sSector;
  char		*p_cTemp, *p_cTmp, *p_Char;
  char		sString[50];
  char		sString1[50];
  long		i;

  sprintf(sSector, "%s", _sSector);

  if ((p_Char = strstr(sSector, "{Sector:"))!=NULL)
  {   strcpy(sString, p_Char+8); 
	  sString[strlen(sString) -1] = '\0';
	  strcpy(sString1, "SPO_");
	  strcat(sString1, sString);
	  if ((MLT_hFindInHierarchySector(sString, g_hRoot)==NULL) && (MLT_hFindInHierarchySector(sString1, g_hRoot2)==NULL)) 
	  { 
		strcpy(g_a_sOldSector[g_lNbOldSector], sString) ;
		g_lNbOldSector ++;
		return TRUE;
	  }
  }

  if((p_cTemp = strstr(sSector, "SuperObject:")) == NULL)
      return FALSE;
  
  if((p_cTemp = strstr(sSector, "sct")) == NULL)
      return FALSE;

  p_cTmp = strstr(p_cTemp, "\","); 
  if(p_cTmp != NULL)
    *p_cTmp = 0;
  else
  {
    p_cTmp = strstr(p_cTemp, "\")"); 
    if(p_cTmp != NULL)
      *p_cTmp = 0;
    else
      p_cTemp[strlen(p_cTemp)-1] = 0; 
  }

   for(i=0; i<g_lNbSector; i++)
    if(stricmp(p_cTemp, g_a_sSector[i]) == 0)
      return FALSE;

  return TRUE;
}


/****************************************************************************
*		Description:	Add Sector to the list of sector
*
*		Parameters:		sSector	: name of sector to test
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vAddSector(char *sSector)
{
  sprintf(g_a_sSector[g_lNbSector], "%s", sSector);
  g_lNbSector++;
}



/****************************************************************************
*		Description:	save the SCT file
*
*		Parameters:		hRoot	: hierarchy root
*						sFile	: file name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
// Save Sct file
void MLT_vSaveSctFile(MLT_tdxHandleToSuperObject hRoot, xString sFile)
{
  char					sIdent1[20];
  SCR_tdst_Cxt_Values	*p_stVal;
  xString				sFile1, sAction, sIdent, sSection, sSection2;
  long					i, j;

  if(! SCR_fn_c_RdL0_IsSectionExists(sFile))
  { 
	  SCR_fn_v_SvL1_RegisterNotify(sFile, MLT_vSaveSctHeader, NULL, SCR_EA_Ntfy_AddSection);
	  if (g_bRaymanII)
		  gs_bCreatedSct = FALSE;
  }

  for(i=0; i<hRoot->lNbChild-(g_iUnivSectors+1); i++)
  {
    // recover the old sector
    SCR_fn_v_RdL0_SplitSectionName(hRoot->d_hChild[i]->stSector.sName, sFile1, sSection, sIdent);
    SCR_fn_v_RdL0_ComputeSectionName(sAction, sFile, M_Sector, sIdent);

	// virtual flag
    if (g_bRaymanII && !gs_bCreatedSct)	
	{	
		if (strstr(hRoot->d_hChild[i]->stSector.sName, "sctv")!=NULL)
			hRoot->d_hChild[i]->stSector.bIsVirtual = TRUE;
		else
			hRoot->d_hChild[i]->stSector.bIsVirtual = FALSE;
	}

	if(SCR_fn_c_RdL0_IsSectionExists(sAction))
	{
	  p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sAction, SCR_CDF_uw_Anl_Normal);
	  if (!strstr(sFile, "Submap"))
	  {
		  hRoot->d_hChild[i]->stSector.p_stOldSector = (MLT_tdstSector*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
		  //ignore the data saved in .sct file
		  if (g_bRaymanII && g_bIgnoreSct)  
		  {   
			   if (strstr(hRoot->d_hChild[i]->stSector.sName, "sctv")!=NULL)
					hRoot->d_hChild[i]->stSector.bIsVirtual = TRUE;
			   else
					hRoot->d_hChild[i]->stSector.bIsVirtual = FALSE;
		  }
		  //take the data from .sct
		  else if (g_bRaymanII) 
		  {	
			   hRoot->d_hChild[i]->stSector.bIsVirtual = hRoot->d_hChild[i]->stSector.p_stOldSector->bIsVirtual;
		  }
		  hRoot->d_hChild[i]->stSector.bHasPriority = hRoot->d_hChild[i]->stSector.p_stOldSector->bHasPriority;
		  hRoot->d_hChild[i]->stSector.bHasCameraType = hRoot->d_hChild[i]->stSector.p_stOldSector->bHasCameraType;
		  hRoot->d_hChild[i]->stSector.lPriority = hRoot->d_hChild[i]->stSector.p_stOldSector->lPriority;
		  hRoot->d_hChild[i]->stSector.lCameraType = hRoot->d_hChild[i]->stSector.p_stOldSector->lCameraType;
	  }
	  else
		  hRoot->d_hChild[i]->stSector.p_stSubmapOldSector = (MLT_tdstSector*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	}
	else
	{
	  if (!strstr(sFile, "Submap"))
	  {
		  hRoot->d_hChild[i]->stSector.p_stOldSector = NULL;
		  //new sector found, so for it look at the name to see if it is virtual or not.
		  if (g_bRaymanII && gs_bCreatedSct)
		  {	
			  if (strstr(hRoot->d_hChild[i]->stSector.sName, "sctv")!=NULL)
				  hRoot->d_hChild[i]->stSector.bIsVirtual = TRUE;
			  else
				  hRoot->d_hChild[i]->stSector.bIsVirtual = FALSE;
		  }
	  }
	  else
		  hRoot->d_hChild[i]->stSector.p_stSubmapOldSector = NULL;
	}

    // section sector
    SCR_fn_v_RdL0_SplitSectionName(hRoot->d_hChild[i]->stSector.sName, sFile1, sSection, sIdent);
    SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Sector, sIdent);
    SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveSct, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);

    // border for Rayman2
	if (g_bRaymanII)	
	{
	    sprintf(sAction, "%s", M_Border, SCR_CC_sz_Cfg_SectionIdMark);
	    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
	    SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveBorder, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);
	}
    // sub section gothrough
    else 	
	{
		sprintf(sAction, "%s", M_Gothrough, SCR_CC_sz_Cfg_SectionIdMark);
		SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
		SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctGot, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);
	}
  
    // sub section ENVAndSRF
    sprintf(sAction, "%s", M_ENVAndSRF, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
    SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctEnv, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);

    // sub section StaticLights
    sprintf(sAction, "%s", M_StaticLights, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
    SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctLgt, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);

    // sub section SectorsLink
    sprintf(sAction, M_SectorsLink, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
	SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctLnk, &hRoot->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);
  }


  // Univers_SubMaps
  for (i=0; i<g_iUnivSectors; i++)
  {
	SCR_fn_v_RdL0_SplitSectionName(g_hRoot2->d_hChild[i]->sName, sFile1, M_Sector, sIdent);
	sprintf(sFile1, "%s.sct", g_sFileIn);
	sprintf(sIdent1, "Univers_%s", sIdent+7);
    SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Sector, sIdent1);
	strcpy(g_hRoot2->d_hChild[i]->stSector.sName, sSection);
	SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveSct, &g_hRoot2->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);

    // sub section ENVAndSRF
    sprintf(sAction, "%s", M_ENVAndSRF, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");
    SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctEnv, &g_hRoot2->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);
	
    // sub section SectorsLink
    sprintf(sAction, M_SectorsLink, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sSection, sAction, "");

    for (j=0; j< g_hRoot2->d_hChild[i]->lNbChild; j++)
	{
		SCR_fn_v_RdL0_SplitSectionName(g_hRoot2->d_hChild[i]->d_hChild[j]->sName, sFile1, sAction, sIdent);
		// create graphic link
		strcpy(g_hRoot2->d_hChild[i]->stSector.a_sSecteurGraphic[j], sIdent);
		g_hRoot2->d_hChild[i]->stSector.a_lSecteurGraphic[j] = 1;
		g_hRoot2->d_hChild[i]->stSector.a_lSecteurGraphicLook[j] = 1;
		// create acctivity link
		strcpy(g_hRoot2->d_hChild[i]->stSector.a_sSecteurActivity[j], sIdent);
		// create submap graphic link
		strcpy(g_hRoot2->d_hChild[i]->stSector.a_sSubmapSecteurGraphic[j], sIdent);
		g_hRoot2->d_hChild[i]->stSector.a_lSubmapSecteurGraphic[j] = 1;
		g_hRoot2->d_hChild[i]->stSector.a_lSubmapSecteurGraphicLook[j] = 1;
		// create submap activity link
		strcpy(g_hRoot2->d_hChild[i]->stSector.a_sSubmapSecteurActivity[j], sIdent);
	}
	g_hRoot2->d_hChild[i]->stSector.lNbSecteurGraphic = g_hRoot2->d_hChild[i]->lNbChild;
	g_hRoot2->d_hChild[i]->stSector.lNbSecteurActivity = g_hRoot2->d_hChild[i]->lNbChild;
	g_hRoot2->d_hChild[i]->stSector.lNbSubmapSecteurGraphic = g_hRoot2->d_hChild[i]->lNbChild;
	g_hRoot2->d_hChild[i]->stSector.lNbSubmapSecteurActivity = g_hRoot2->d_hChild[i]->lNbChild;

    SCR_fn_v_SvL1_RegisterNotify(sSection2, MLT_vSaveSctLnk, &g_hRoot2->d_hChild[i]->stSector, SCR_EA_Ntfy_AddOrModifySection);
  }


  // section sector Univers
  SCR_fn_v_RdL0_SplitSectionName(hRoot->d_hChild[hRoot->lNbChild-1]->stSector.sName, sFile1, sSection, sIdent);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Sector, sIdent);
  SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveSctUnivers, hRoot, SCR_EA_Ntfy_AddOrModifySection);
}


/****************************************************************************
*		Description:	save Sct file header
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctHeader(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString sComment, sTime, sDate;

  SCR_M_SvL0_SaveScriptFileHeader(p_stFile);
  sprintf(sComment, ".SCT (Sector Information) : %s", p_stFile->a_szFileName);
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Generated by Max23Dos Moulinette");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  _strtime(sTime);
  _strdate(sDate);
  sprintf(sComment, "Created date : %s %s", sDate, sTime);
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Version directive, the version number is stored in file result 0");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveDirective(p_stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 0, 5.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Unit directive, the unit is stored in file result 1");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  sprintf(sComment, "And correspond to the value of one unit exprimed in meter");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveDirective(p_stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 1, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
}


/****************************************************************************
*		Description:	save sector section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSct(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstSector *p_stSect;

  p_stSect = (MLT_tdstSector*)p_vPtr;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }
  else
    return;
  
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(p_stSect->sName), SCR_CC_C_Cfg_EOL);
  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}


/****************************************************************************
*		Description:	save border sub-section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveBorder (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString sSection;
  MLT_tdstSector *p_stSect, *p_stOldSector;

  p_stSect = (MLT_tdstSector*)p_vPtr;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
  {
    SCR_tdst_Cxt_Values *p_stVal;
    p_stVal = SCR_fnp_st_RdL0_AnalyseSection(p_stSect->sName, SCR_CDF_uw_Anl_Normal);
	p_stOldSector = (MLT_tdstSector*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

	//get the old ZMaxOfBorder
	if (p_stOldSector->bExistZMax)
	{	p_stSect->fZMax = p_stOldSector->fZMax;
		p_stSect->bExistZMax=p_stOldSector->bExistZMax;
	}
	//delete the section
	SCR_fn_v_SvL1_DeleteSection(p_stFile);
  }

  if (p_stSect->stPointMin.xX != MLT_C_InfinitPlus)
  {	SCR_g_ui_SvL0_IndentationLevel = 1;
	//save the sub-section Border
	sprintf(sSection, "%s%s", M_Border, SCR_CC_sz_Cfg_SectionIdMark);
	SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionMinPointOfBorder, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%0.3f,%0.3f,%0.3f", p_stSect->stPointMin.xX, p_stSect->stPointMin.xY, p_stSect->stPointMin.xZ);

	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionMaxPointOfBorder, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%0.3f,%0.3f,%0.3f", p_stSect->stPointMax.xX, p_stSect->stPointMax.xY, p_stSect->stPointMax.xZ);

	if (p_stSect->bExistZMax && !g_bIgnoreSct && (p_stSect->fZMax > p_stSect->stPointMax.xZ))
	{
	  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionZmaxOfBorder, SCR_CC_C_Cfg_NoChar);
	  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%0.3f",  p_stSect->fZMax);
	}

	SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

	SCR_g_ui_SvL0_IndentationLevel = 0;
  }
}


/****************************************************************************
*		Description:	save gothrough sub-section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctGot (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString sFile, sSection, sIdent, sIdent2, sBorderName;
  MLT_tdstSector *p_stSect;

  p_stSect = (MLT_tdstSector*)p_vPtr;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  if(!p_stSect->bIsVirtual)
  {
    SCR_g_ui_SvL0_IndentationLevel = 1;
  
    sprintf(sSection, "%s%s", M_Gothrough, SCR_CC_sz_Cfg_SectionIdMark);
    SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

    SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sSection, sIdent);
    sprintf(sIdent2, "%s_%s", M_NameGothrough, sIdent);
    sprintf(sFile, "%s%s.mod", g_sDirectorySave, g_sFileIn);
    SCR_fn_v_RdL0_ComputeSectionName(sBorderName, sFile, M_Geometric, sIdent2);

    SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionGothrough, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sBorderName);
  
    SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

    SCR_g_ui_SvL0_IndentationLevel = 0;
  }
}


/****************************************************************************
*		Description:	save env&surf sub-section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctEnv(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstSector	*p_stSect;
  MLT_tdstSector	*p_stOldSector;
  xString			sFile, sSection, sSection2, sIdent;
  long				i;

  p_stSect = (MLT_tdstSector*)p_vPtr;
  p_stOldSector = p_stSect->p_stOldSector;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  SCR_g_ui_SvL0_IndentationLevel = 1;

  sprintf(sSection, "%s%s", M_ENVAndSRF, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

  if( (p_stOldSector != NULL) ) 
  {
    if(p_stOldSector->lNbSurface < 1) 
      SCR_fn_v_RdL0_ComputeSectionName(sSection, "ED.gev", "Environment", "EnvAir");
    else
      sprintf(sSection, "%s", p_stOldSector->a_sEnv[0]);
  }
  else
    SCR_fn_v_RdL0_ComputeSectionName(sSection, "ED.gev", "Environment", "EnvAir");

  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstEnv, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);

  // Save surfaces, compare with old sector
  for(i=0; i<p_stSect->lNbSurface; i++)
  {
    sprintf(sFile, "%s%s.srf", g_sDirectorySave, g_sFileIn);
    MLT_vGetSurfaceName(sIdent, p_stSect->a_stSurface[i].sGeometric);
    SCR_fn_v_RdL0_ComputeSectionName(sSection2, sFile, M_SurfaceDescription, sIdent);

    SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSurface , SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection2);

    if( (p_stOldSector != NULL) && (i < (p_stOldSector->lNbSurface-1)) )
      sprintf(sSection, "%s", p_stOldSector->a_sEnv[i+1]);
    else
      SCR_fn_v_RdL0_ComputeSectionName(sSection, "ED.gev", "Environment", "EnvWater");

    SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstEnv, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);
  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  SCR_g_ui_SvL0_IndentationLevel = 0;
}

/****************************************************************************
*		Description:	save links sub-section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctLnk (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstSector	*p_stSect;
  MLT_tdstSector	*p_stOldSector;
  xString			sFile, sSection, sIdent, sIdent1, sAction, sUnivers;
  xString			sGraphic, sActivity, sCollision, sSound, sSoundEvent1, sSoundEvent2;
  xString			sOldGraphic, sOldActivity, sOldCollision, sOldSound;
  xString			sSubUnivers, sSubmap;
  BOOL				bUniversGraphic = FALSE;
  BOOL				bUniversCollision = FALSE;
  BOOL				bUniversSound = FALSE;
  BOOL				bSubmap, bFound;
  long				lNbGraphics, lNbActivity, lNbCollision, lNbSound, lNbSoundEvent;
  long				lNbOldGraphics, lNbOldActivity, lNbOldCollision, lNbOldSound;
  long				lGraphic, lLook, lSound;
  long				lOldGraphic, lOldLook, lOldSound;
  long				lNumSubmap = -1;
  long				i, j;
  char				*p_stUnivers;

  // check the typr of SCT file
  bSubmap = (strstr(p_stFile->a_szFileName, "_Submap") != 0);

  // get sector and old sector
  p_stSect = (MLT_tdstSector*)p_vPtr;
  p_stOldSector = (bSubmap) ? p_stSect->p_stSubmapOldSector : p_stSect->p_stOldSector;
  // first conversion for submaps -> take links of SCT file
  if (bSubmap && !p_stOldSector)
  {
	  p_stOldSector = p_stSect->p_stOldSector;
	  if (p_stOldSector)
	  {
		  // copy graphic links
		  p_stOldSector->lNbSubmapSecteurGraphic = p_stOldSector->lNbSecteurGraphic;
		  for (i=0; i < p_stOldSector->lNbSecteurGraphic; i++)
		  {
			  strcpy(p_stOldSector->a_sSubmapSecteurGraphic[i], p_stOldSector->a_sSecteurGraphic[i]);
			  p_stOldSector->a_lSubmapSecteurGraphic[i] = p_stOldSector->a_lSecteurGraphic[i];
			  p_stOldSector->a_lSubmapSecteurGraphicLook[i] = p_stOldSector->a_lSecteurGraphicLook[i];
		  }
		  // copy activity links
		  p_stOldSector->lNbSubmapSecteurActivity = p_stOldSector->lNbSecteurActivity;
		  for (i=0; i < p_stOldSector->lNbSecteurActivity; i++)
			  strcpy(p_stOldSector->a_sSubmapSecteurActivity[i], p_stOldSector->a_sSecteurActivity[i]);
		  // copy collision links
		  p_stOldSector->lNbSubmapSecteurCollision = p_stOldSector->lNbSecteurCollision;
		  for (i=0; i < p_stOldSector->lNbSecteurCollision; i++)
			  strcpy(p_stOldSector->a_sSubmapSecteurCollision[i], p_stOldSector->a_sSecteurCollision[i]);
		  // copy sound links
		  p_stOldSector->lNbSubmapSecteurSound = p_stOldSector->lNbSecteurSound;
		  for (i=0; i < p_stOldSector->lNbSecteurSound; i++)
		  {
			  strcpy(p_stOldSector->a_sSubmapSecteurSound[i], p_stOldSector->a_sSecteurSound[i]);
			  p_stOldSector->a_lSubmapSecteurSound[i] = p_stOldSector->a_lSecteurSound[i];
		  }
		  // copy sound event links
		  p_stOldSector->lNbSubmapSoundEvent = p_stOldSector->lNbSoundEvent;
		  for (i=0; i < p_stOldSector->lNbSoundEvent; i++)
		  {
			  strcpy(p_stOldSector->a_sSubmapSoundEvent1[i], p_stOldSector->a_sSoundEvent1[i]);
			  strcpy(p_stOldSector->a_sSubmapSoundEvent2[i], p_stOldSector->a_sSoundEvent2[i]);
		  }
	  }
  }

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  SCR_g_ui_SvL0_IndentationLevel = 1;

  // save begin section
  sprintf(sSection, "%s%s", M_SectorsLink, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

  //if the sector contains a new frontier generate an entry NewModelisation at the SectorLinks
  if (p_stSect->iFlag & IS_NEWFRONTIER)
	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionNewModelisation, SCR_CC_C_Cfg_EOL);

  // virtual
  if ((g_lNbSector>1) && p_stSect->bIsVirtual) 
  {
	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionVirtual, SCR_CC_C_Cfg_EOL);
  }

  // camera type
  if (p_stSect->bHasCameraType) 
  {
	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionCameraType, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "%d", p_stSect->lCameraType);
  }

  // priority
  if (p_stSect->bHasPriority) 
  {
	SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionPriority, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "%d", p_stSect->lPriority);
  }

  // for submap, find the name of the appropriate Univers sector
  if (bSubmap)
  {
	  sprintf(sIdent, "");
	  SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sAction, sIdent1);
	  p_stUnivers = strstr(sIdent1, "Univers_");
	  if (p_stUnivers)
		  strcpy(sSubUnivers, p_stUnivers);
	  else
		  strcpy(sSubUnivers,"");
	  bFound = FALSE;

	  for (i=0; (i<g_hRoot2->lNbChild && !bFound); i++)
	  {	
		  if (p_stUnivers) 
		  {
			  SCR_fn_v_RdL0_SplitSectionName(g_hRoot2->d_hChild[i]->sName, sFile, sAction, sIdent1);
			  strcpy(sSubmap, strstr(sIdent1, "GS_"));

			  if (!strcmp(sSubmap+3, sSubUnivers+8))
			  {	
				  sprintf(sIdent, "SPO_Univers_%s", sIdent1+7);
				  lNumSubmap = i;
				  bFound = TRUE;
			  }
		  }
		  else
		  {
			  for (j=0; j<g_hRoot2->d_hChild[i]->lNbChild; j++)
			  {	
				  char sLwrName[256], sLwrIdent1[256];
				  
				  strcpy(sLwrName, g_hRoot2->d_hChild[i]->d_hChild[j]->sName);
				  strcpy(sLwrIdent1, sIdent1);

				  if (strstr(strlwr(sLwrName), strlwr(sLwrIdent1)))
				  {	
					  SCR_fn_v_RdL0_SplitSectionName(g_hRoot2->d_hChild[i]->sName, sFile, sAction, sIdent1);
					  sprintf(sIdent, "SPO_Univers_%s", sIdent1+7);
					  lNumSubmap = i;
					  bFound = TRUE;
					  break;
				  }
			  }
		  }
	  }
	  if ((strlen(sIdent)>0) && (strlen(sSubUnivers)==0))
	  {
		  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
		  SCR_fn_v_RdL0_ComputeSectionName(sUnivers, sFile, M_SuperObject, sIdent);
	  }
	  else
		  sprintf(sUnivers, "");
  }
  // for complete map, take the global Univers
  else
  {
	  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
	  SCR_fn_v_RdL0_ComputeSectionName(sUnivers, sFile, M_SuperObject, M_SPO_Univers);
  }
  
  // save the links with graphics sectors
  lNbGraphics = (bSubmap) ? p_stSect->lNbSubmapSecteurGraphic : p_stSect->lNbSecteurGraphic;
  for (i=0; i < lNbGraphics; i++)
  {
	  // get parameters
	  strcpy(sGraphic, (bSubmap) ? p_stSect->a_sSubmapSecteurGraphic[i] : p_stSect->a_sSecteurGraphic[i]);
	  lGraphic = (bSubmap) ? p_stSect->a_lSubmapSecteurGraphic[i] : p_stSect->a_lSecteurGraphic[i];
	  lLook = (bSubmap) ? p_stSect->a_lSubmapSecteurGraphicLook[i] : p_stSect->a_lSecteurGraphicLook[i];
	  // test for correction
	  if (lGraphic == 0)
		  lGraphic = 1;
	  // compute section
	  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
	  if (!strstr(sGraphic, "SPO_"))
		  sprintf(sIdent, "SPO_%s", sGraphic);
	  else
		  sprintf(sIdent, "%s", sGraphic);
	  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);
	  
	  // normal sector
	  if (!strstr(sGraphic, "Univers"))
	  {
		  if (MLT_hFindInHierarchySector(sIdent, g_hRoot) && MLT_bIsNotSameSector(sIdent, p_stSect->sName)) 
		  {
			  // submap : check coherence
			  if (!bSubmap || MLT_hFindInHierarchySector(sIdent, g_hRoot2->d_hChild[lNumSubmap]))
			  {
				  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstGraphic, SCR_CC_C_Cfg_NoChar);
				  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sSection, lGraphic, lLook);
			  }
		  }
	  }
	  else if (!bUniversGraphic)
	  {
		  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstGraphic, SCR_CC_C_Cfg_NoChar);
		  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sUnivers, 1, 1);
		  bUniversGraphic = TRUE;
	  }
  }

  // save the link with actifs sectors
  lNbActivity = (bSubmap) ? p_stSect->lNbSubmapSecteurActivity : p_stSect->lNbSecteurActivity;
  for (i=0; i < lNbActivity; i++)
  {
	  // get parameters
	  strcpy(sActivity, (bSubmap) ? p_stSect->a_sSubmapSecteurActivity[i] : p_stSect->a_sSecteurActivity[i]);
	  // compute section
	  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
	  if (!strstr(sActivity, "SPO_"))
		  sprintf(sIdent, "SPO_%s", sActivity);
	  else	
		  sprintf(sIdent, "%s", sActivity);
	  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

	  // normal sector
	  if (!strstr(sActivity, "Univers"))
	  {
		  if (MLT_hFindInHierarchySector(sIdent, g_hRoot) && MLT_bIsNotSameSector(sIdent, p_stSect->sName))  
		  {
			  // submap : check coherence
			  if (!bSubmap || MLT_hFindInHierarchySector(sIdent, g_hRoot2->d_hChild[lNumSubmap]))
			  {
				  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstActivity, SCR_CC_C_Cfg_NoChar);
				  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%d", sSection, 1);
			  }
		  }
	  }
  }
  // Univers Sector
  if (strlen(sUnivers) > 0)
  {
	  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstActivity, SCR_CC_C_Cfg_NoChar);
	  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%d", sUnivers, 1);
  }

  // save the link with collision sectors
  lNbCollision = (bSubmap) ? p_stSect->lNbSubmapSecteurCollision : p_stSect->lNbSecteurCollision;
  for (i=0; i < lNbCollision; i++)
  {
	  // get parameters
	  strcpy(sCollision, (bSubmap) ? p_stSect->a_sSubmapSecteurCollision[i] : p_stSect->a_sSecteurCollision[i]);
	  // compute section
	  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
	  if (!strstr(sCollision, "SPO_"))
		  sprintf(sIdent, "SPO_%s", sCollision);
	  else	
		  sprintf(sIdent, "%s", sCollision);
	  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

	  // normal sector
	  if (!strstr(sCollision, "Univers"))
	  {
		  if (MLT_hFindInHierarchySector(sIdent, g_hRoot) && MLT_bIsNotSameSector(sIdent, p_stSect->sName))
		  {
			  // submap : check coherence
			  if (!bSubmap || MLT_hFindInHierarchySector(sIdent, g_hRoot2->d_hChild[lNumSubmap]))
			  {
				  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstCollision, SCR_CC_C_Cfg_NoChar);
				  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%d", sSection, 1);
			  }
		  }
	  }
	  else if (!bUniversCollision)
	  {
		  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstCollision, SCR_CC_C_Cfg_NoChar);
		  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%d", sUnivers, 1);
		  bUniversCollision = TRUE;
	  }
  }

  // save the links with sound sectors
  lNbSound = (bSubmap) ? p_stSect->lNbSubmapSecteurSound : p_stSect->lNbSecteurSound;
  for (i=0; i < lNbSound; i++)
  {
	  // get parameters
	  strcpy(sSound, (bSubmap) ? p_stSect->a_sSubmapSecteurSound[i] : p_stSect->a_sSecteurSound[i]);
	  lSound = (bSubmap) ? p_stSect->a_lSubmapSecteurSound[i] : p_stSect->a_lSecteurSound[i];
	  // compute section
	  sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
	  if (!strstr(sSound, "SPO_"))
		  sprintf(sIdent, "SPO_%s", sSound);
	  else
		  sprintf(sIdent, "%s", sSound);
	  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);
	  
	  // normal sector
	  if (!strstr(sSound, "Univers"))
	  {
		  if (MLT_hFindInHierarchySector(sIdent, g_hRoot) && MLT_bIsNotSameSector(sIdent, p_stSect->sName)) 
		  {
			  // submap : check coherence
			  if (!bSubmap || MLT_hFindInHierarchySector(sIdent, g_hRoot2->d_hChild[lNumSubmap]))
			  {
				  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSound, SCR_CC_C_Cfg_NoChar);
				  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sSection, lSound, 1);
			  }
		  }
	  }
	  else if (!bUniversSound)
	  {
		  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSound, SCR_CC_C_Cfg_NoChar);
		  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sUnivers, 1, 1);
		  bUniversSound = TRUE;
	  }
  }

  if (p_stOldSector != NULL)
  {
	// save links with graphic sectors
	lNbOldGraphics = (bSubmap) ? p_stOldSector->lNbSubmapSecteurGraphic : p_stOldSector->lNbSecteurGraphic;
	for (i=0; i < lNbOldGraphics; i++)
	{
		xString sIdent2, sAction;

		strcpy(sOldGraphic, (bSubmap) ? p_stOldSector->a_sSubmapSecteurGraphic[i] : p_stOldSector->a_sSecteurGraphic[i]);
		lOldGraphic = (bSubmap) ? p_stOldSector->a_lSubmapSecteurGraphic[i] : p_stOldSector->a_lSecteurGraphic[i];
		lOldLook = (bSubmap) ? p_stOldSector->a_lSubmapSecteurGraphicLook[i] : p_stOldSector->a_lSecteurGraphicLook[i];
		  // test for correction
		  if (lOldGraphic == 0)
			  lOldGraphic = 1;

		// check with sector name
		SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sAction, sIdent);
		sprintf(sIdent2, "SPO_%s", sIdent);
		sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
		SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent2);
		if (!stricmp(sOldGraphic, sSection)) 
			continue;

		// check with new links
		lNbGraphics = (bSubmap) ? p_stSect->lNbSubmapSecteurGraphic : p_stSect->lNbSecteurGraphic;
	    for (j=0; j < lNbGraphics; j++)
		{
			strcpy(sGraphic, (bSubmap) ? p_stSect->a_sSubmapSecteurGraphic[j] : p_stSect->a_sSecteurGraphic[j]);
			sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
			sprintf(sIdent, "SPO_%s", sGraphic);
			SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

	        if (!stricmp(sSection, sOldGraphic))
				break;
		}

		// this link is not exist yet
		if (j == lNbGraphics) 
		{
			for (j=i+1; j < lNbOldGraphics; j++)
			{
				strcpy(sGraphic, (bSubmap) ? p_stOldSector->a_sSubmapSecteurGraphic[j] : p_stOldSector->a_sSecteurGraphic[j]);
				if (!stricmp(sGraphic, sOldGraphic))
					break;
			}

			// this link is not duplicate
			if (j == lNbOldGraphics) 
			{
				xString sFile3, sAction3, sIdent3;

				SCR_fn_v_RdL0_SplitSectionName(sOldGraphic, sFile3, sAction3, sIdent3);

				if (!strstr(sOldGraphic, "Univers"))
				{
					if (MLT_hFindInHierarchySector(sIdent3, g_hRoot) && MLT_bIsNotSameSector(sIdent3, p_stSect->sName)) 
					{
						// submap : check coherence
						if (!bSubmap || MLT_hFindInHierarchySector(sIdent3, g_hRoot2->d_hChild[lNumSubmap]))
						{
							SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstGraphic, SCR_CC_C_Cfg_NoChar);
							SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sOldGraphic, lOldGraphic, lOldLook);
						}
					}
				}
				else if (!bUniversGraphic)
				{
					SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstGraphic, SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sUnivers, 1, 1);
					bUniversGraphic = TRUE;
				}
			}
		}
	}

	// save links with activity sectors
	lNbOldActivity = (bSubmap) ? p_stOldSector->lNbSubmapSecteurActivity : p_stOldSector->lNbSecteurActivity;
	for (i=0; i < lNbOldActivity; i++)
	{
		xString sIdent2, sAction;

		strcpy(sOldActivity, (bSubmap) ? p_stOldSector->a_sSubmapSecteurActivity[i] : p_stOldSector->a_sSecteurActivity[i]);

		// check with sector name
		SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sAction, sIdent);
		sprintf(sIdent2, "SPO_%s", sIdent);
		sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
		SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent2);
		if (!stricmp(sOldActivity, sSection)) 
			continue;

		// check with new links
		lNbActivity = (bSubmap) ? p_stSect->lNbSubmapSecteurActivity : p_stSect->lNbSecteurActivity;
	    for (j=0; j<lNbActivity; j++)
		{
			strcpy(sActivity, (bSubmap) ? p_stSect->a_sSubmapSecteurActivity[j] : p_stSect->a_sSecteurActivity[j]);
			sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
			sprintf(sIdent, "SPO_%s", sActivity);
			SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

			if (!stricmp(sSection, sOldActivity))
				break;
		}

		// this link is not exist yet
		if (j == lNbActivity)
		{
			for (j=i+1; j<lNbOldActivity; j++)
			{
				strcpy(sActivity, (bSubmap) ? p_stOldSector->a_sSubmapSecteurActivity[j] : p_stOldSector->a_sSecteurActivity[j]);
				if (!stricmp(sActivity, sOldActivity))
					break;
			}

			// this link is not duplicate
			if (j == lNbOldActivity) 
			{
				xString sFile3, sAction3, sIdent3;

				SCR_fn_v_RdL0_SplitSectionName(sOldActivity, sFile3, sAction3, sIdent3);

				if (!strstr(sOldActivity, "Univers"))
				{
					if (MLT_hFindInHierarchySector(sIdent3, g_hRoot) && MLT_bIsNotSameSector(sIdent3, p_stSect->sName)) 
					{
						// submap : check coherence
						if (!bSubmap || MLT_hFindInHierarchySector(sIdent3, g_hRoot2->d_hChild[lNumSubmap]))
						{
							SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstActivity, SCR_CC_C_Cfg_NoChar);
							SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d", sOldActivity, 1);
						}
					}
				}
			}
		}
    }

	// save links with collision sectors
	lNbOldCollision = (bSubmap) ? p_stOldSector->lNbSubmapSecteurCollision : p_stOldSector->lNbSecteurCollision;
    for (i=0; i<lNbOldCollision; i++)
    {
		xString sIdent2, sAction;

		strcpy(sOldCollision, (bSubmap) ? p_stOldSector->a_sSubmapSecteurCollision[i] : p_stOldSector->a_sSecteurCollision[i]);

		// check with sector name
		SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sAction, sIdent);
		sprintf(sIdent2, "SPO_%s", sIdent);
		sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
		SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent2);
		if (!stricmp(sOldCollision, sSection)) 
			continue;

		// check with new links
		lNbCollision = (bSubmap) ? p_stSect->lNbSubmapSecteurCollision : p_stSect->lNbSecteurCollision;
		for (j=0; j<lNbCollision; j++)
		{
			strcpy(sCollision, (bSubmap) ? p_stSect->a_sSubmapSecteurCollision[j] : p_stSect->a_sSecteurCollision[j]);
			sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
			sprintf(sIdent, "SPO_%s", sCollision);
			SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

			if (!stricmp(sSection, sOldCollision))
				break;
		}

		// this link is not exist yet
		if (j == lNbCollision)
		{
			for (j=i+1; j<lNbOldCollision; j++)
			{
				strcpy(sCollision, (bSubmap) ? p_stOldSector->a_sSubmapSecteurCollision[j] : p_stOldSector->a_sSecteurCollision[j]);
				if (!stricmp(sCollision, sOldCollision))
					break;
			}

			// this link is not duplicate
			if (j == lNbOldCollision) 
			{
				xString sFile3, sAction3, sIdent3;

				SCR_fn_v_RdL0_SplitSectionName(sOldCollision, sFile3, sAction3, sIdent3);

				if (!strstr(sOldCollision, "Univers"))
				{
					if (MLT_hFindInHierarchySector(sIdent3, g_hRoot) && MLT_bIsNotSameSector(sIdent3, p_stSect->sName)) 
					{
						// submap : check coherence
						if (!bSubmap || MLT_hFindInHierarchySector(sIdent3, g_hRoot2->d_hChild[lNumSubmap]))
						{
							SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstCollision, SCR_CC_C_Cfg_NoChar);
							SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d", sOldCollision, 1);
						}
					}
				}
				else if (!bUniversCollision)
				{
					SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstCollision, SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d", sUnivers, 1);
					bUniversCollision = TRUE;
				}
			}
		}
	}

	// save links with sound sectors
	lNbOldSound = (bSubmap) ? p_stOldSector->lNbSubmapSecteurSound : p_stOldSector->lNbSecteurSound;
	for (i=0; i < lNbOldSound; i++)
	{
		xString sIdent2, sAction;

		strcpy(sOldSound, (bSubmap) ? p_stOldSector->a_sSubmapSecteurSound[i] : p_stOldSector->a_sSecteurSound[i]);
		lOldSound = (bSubmap) ? p_stOldSector->a_lSubmapSecteurSound[i] : p_stOldSector->a_lSecteurSound[i];

		// check with sector name
		SCR_fn_v_RdL0_SplitSectionName(p_stSect->sName, sFile, sAction, sIdent);
		sprintf(sIdent2, "SPO_%s", sIdent);
		sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
		SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent2);
		if (!stricmp(sOldSound, sSection)) 
			continue;

		// check with new links
		lNbSound = (bSubmap) ? p_stSect->lNbSubmapSecteurSound : p_stSect->lNbSecteurSound;
	    for (j=0; j < lNbSound; j++)
		{
			strcpy(sSound, (bSubmap) ? p_stSect->a_sSubmapSecteurSound[j] : p_stSect->a_sSecteurSound[j]);
			sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
			sprintf(sIdent, "SPO_%s", sSound);
			SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SuperObject, sIdent);

	        if (!stricmp(sSection, sOldSound))
				break;
		}

		// this link is not exist yet
		if (j == lNbSound) 
		{
			for (j=i+1; j < lNbOldSound; j++)
			{
				strcpy(sSound, (bSubmap) ? p_stOldSector->a_sSubmapSecteurSound[j] : p_stOldSector->a_sSecteurSound[j]);
				if (!stricmp(sSound, sOldSound))
					break;
			}

			// this link is not duplicate
			if (j == lNbOldSound) 
			{
				xString sFile3, sAction3, sIdent3;

				SCR_fn_v_RdL0_SplitSectionName(sOldSound, sFile3, sAction3, sIdent3);

				if (!strstr(sOldSound, "Univers"))
				{
					if (MLT_hFindInHierarchySector(sIdent3, g_hRoot) && MLT_bIsNotSameSector(sIdent3, p_stSect->sName)) 
					{
						// submap : check coherence
						if (!bSubmap || MLT_hFindInHierarchySector(sIdent3, g_hRoot2->d_hChild[lNumSubmap]))
						{
							SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSound, SCR_CC_C_Cfg_NoChar);
							SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sOldSound, lOldSound, 1);
						}
					}
				}
				else if (!bUniversSound)
				{
					SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSound, SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sUnivers, 1, 1);
					bUniversSound = TRUE;
				}
			}
		}
	}
	// save the link with the sound events
	lNbSoundEvent = (bSubmap) ? p_stOldSector->lNbSubmapSoundEvent : p_stOldSector->lNbSoundEvent;
	for (i=0; i < lNbSoundEvent; i++)
	{
		// get parameters
		strcpy(sSoundEvent1, (bSubmap) ? p_stOldSector->a_sSubmapSoundEvent1[i] : p_stOldSector->a_sSoundEvent1[i]);
		strcpy(sSoundEvent2, (bSubmap) ? p_stOldSector->a_sSubmapSoundEvent2[i] : p_stOldSector->a_sSoundEvent2[i]);
		// save entry
		SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstSoundEvent, SCR_CC_C_Cfg_NoChar);
		SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%s", sSoundEvent1, sSoundEvent2);
	}


  }


  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  SCR_g_ui_SvL0_IndentationLevel = 0;
}


/****************************************************************************
*		Description:	save static lights sub-section in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctLgt(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  static BOOL		first=TRUE;
  MLT_tdstSector	*p_stSect;
  MLT_tdstSector	*p_stOldSector;
  xString			sSection;
  BOOL				bSubmap;
  long				i;

  // check the typr of SCT file
  bSubmap = (strstr(p_stFile->a_szFileName, "_Submap") != 0);

  // get sector and old sector
  p_stSect = (MLT_tdstSector*)p_vPtr;
  p_stOldSector = (bSubmap) ? p_stSect->p_stSubmapOldSector : p_stSect->p_stOldSector;
  // first conversion for submaps -> take lights of SCT file
  if (bSubmap && !p_stOldSector)
  {
	  p_stOldSector = p_stSect->p_stOldSector;
	  if (p_stOldSector)
	  {
		  SCR_g_ui_SvL0_IndentationLevel = 1;

		  sprintf(sSection, "%s%s", M_StaticLights, SCR_CC_sz_Cfg_SectionIdMark);
		  SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);
		  // copy static lights
		  p_stOldSector->lNbSubmapStaticLights = p_stOldSector->lNbStaticLights;
		  for (i=0; i < p_stOldSector->lNbStaticLights; i++)
		  {
			  strcpy(p_stOldSector->a_sSubmapStaticLights[i], p_stOldSector->a_sStaticLights[i]);
			  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddStaticLight, SCR_CC_C_Cfg_NoChar);
			  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%s", p_stOldSector->a_sSubmapStaticLights[i]);
		  }
		  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

		  SCR_g_ui_SvL0_IndentationLevel = 0;
	  }
  }

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    return;
  }

  if(first)
  {
    xString sFile;
    FILE *hFile;

    sprintf(sFile, "%s.lgt", g_sFileIn);
    if(hFile = fopen(sFile, "rb"))
    {
      fclose(hFile);
    }
    else
    {
	  MLT_vOutput( C_ComRes_cWarningLine, "\nWarning : No LGT file");
    }

    first = FALSE;
  }
}


/****************************************************************************
*		Description:	save univers sector in SCT file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSctUnivers(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdxHandleToSuperObject hSprObj;
  xString sSection, sFile, sAction, sIdent;
  long i;

  hSprObj = (MLT_tdxHandleToSuperObject)p_vPtr; 

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }
  else
  {
    SCR_fn_v_SvL1_DeleteSection(p_stFile);
  }

  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(hSprObj->d_hChild[hSprObj->lNbChild-1]->stSector.sName), SCR_CC_C_Cfg_EOL);

  SCR_g_ui_SvL0_IndentationLevel = 1;

  sprintf(sSection, "%s%s", M_ENVAndSRF, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstEnv, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, "ED.gev", "Environment", "EnvAir");
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  sprintf(sSection, "%s%s", M_SectorsLink, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, sSection, SCR_CC_C_Cfg_EOL);

  SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionVirtual, SCR_CC_C_Cfg_EOL);

  for(i=0; i<hSprObj->lNbChild-(g_iUnivSectors+1); i++)
  {
    SCR_fn_v_RdL0_SplitSectionName(hSprObj->d_hChild[i]->sName, sFile, sAction, sIdent);
    sprintf(sFile, "%s%s.spo", g_sDirectorySave, g_sFileIn);
    SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, sAction, sIdent);
    SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstGraphic, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%s,%d,%d", sSection, 1, 1);

    SCR_M_SvL0_SaveEntry(p_stFile, M_SCTActionAddLstActivity, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%d", sSection, 1);
  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  SCR_g_ui_SvL0_IndentationLevel = 0;

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}




/****************************************************************************
*		Description:	copy temporary file into sct file
*
*		Parameters:		sFile	: file name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vUpdateSctFile (xString sFile)
{
  FILE *fileR, *fileW;
  xString sRead, sName, sBegin;

  sprintf(sBegin, "%c", SCR_CC_c_Cfg_SectionBegMark);

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

  if (CopyFile(sName, "temp.sct", FALSE)==0) 
	  return;

  fileR = fopen("temp.sct", "rt");
  if (fileR == NULL) 
	  return;

  fileW = fopen(sName, "wt");
  if (fileW == NULL) 
	  return;

  while(fgets(sRead, 256, fileR) != NULL)
  {
    if( MLT_bContainBadSector(sRead) )
    {
      if( strstr(sRead, sBegin) != NULL)
      {
        MLT_vToEndSection(fileR);
      }

      continue;
    }

    fputs(sRead, fileW);
  }
  
  fclose(fileR);
  fclose(fileW);

  _chmod("temp.sct", _S_IREAD | _S_IWRITE );
  remove("temp.sct");
}








/****************************************************************************
*		Description:	Read the file until the end of the sectionr
*
*		Parameters:		file	: file pointer
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vToEndSection(FILE *file)
{
  xString sRead, sEnd, sBegin;
  long lIndentation;

  sprintf(sBegin, "%c", SCR_CC_c_Cfg_SectionBegMark);
  sprintf(sEnd, "%c", SCR_CC_c_Cfg_SectionEndMark);
  
  lIndentation = 1;
  while( fgets(sRead, 256, file) != NULL )
  {
    if( strstr(sRead, sBegin) != NULL)
      lIndentation++;
    if( strstr(sRead, sEnd) != NULL)
      lIndentation--;

    if(lIndentation == 0)
      break;
  }
}










