/*=========================================================================
*
* Material.c	- Material functions
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "Material.h"

#include <time.h>

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

#include "Textures.h"

#include "SaveModifLst.h"


//--- Defines ---------------------------------------------------------------

#define MLT_C_lNoCycling 0
#define MLT_C_lCyclingU  1
#define MLT_C_lCyclingV  2
#define MLT_C_lCyclingUV 3


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



/****************************************************************************
*		Description:	load a game material
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadGameMaterial(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstGameMaterial	*p_hMaterial;
  xString				sFile, sAction, sIdent, sIdent2;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_hMaterial = (MLT_tdstGameMaterial*)malloc(sizeof(MLT_tdstGameMaterial));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_hMaterial);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_hMaterial);
      SCR_fn_v_RdL0_SplitSectionName(SCR_M_RdL0_GetCompleteSectionNameR(0), sFile, sAction, sIdent);
      sprintf(sFile, "%s.gmt", g_sFileIn);
      SCR_fn_v_RdL0_ComputeSectionName(p_hMaterial->sName, sFile, sAction, sIdent);
	  strcpy(p_hMaterial->sNameCMT, "");
	  p_hMaterial->bMaxMaterial = TRUE;
	  p_hMaterial->lSoundType = -1;
      break;

    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstGameMaterial*,p_hMaterial);

      // Loading Line
      if (strcmp(szAction,M_GMTActionVisualMat)==0)
      {
		  strcpy(sIdent, szParams[0]);
	      SCR_fn_v_RdL0_SplitSectionName(sIdent, sFile, sAction, sIdent2);
		  SCR_fn_v_RdL0_ComputeSectionName(p_hMaterial->sName, sFile, sAction, sIdent2+4);
      }

      if (strcmp(szAction,M_GMTActionCollideMat)==0)
      {
		  strcpy(sIdent, szParams[0]);
	      SCR_fn_v_RdL0_SplitSectionName(sIdent, sFile, sAction, sIdent2);
		  strcpy( p_hMaterial->sNameCMT, sIdent2);
		  p_hMaterial->bMaxMaterial = FALSE;
      }

      if (strcmp(szAction,M_GMTActionSoundMat)==0)
      {
		  p_hMaterial->lSoundType = atoi(szParams[0]);
      }

      break;

    case SCR_EA_Anl_EndSection:
      break;
        }
  return  SCR_ERV_Anl_NormalReturn;
}

/****************************************************************************
*		Description:	load a material
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadMaterial(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstMaterial	*p_hMaterial;
  xString			sFile, sAction, sIdent;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_hMaterial = (MLT_tdstMaterial*)malloc(sizeof(MLT_tdstMaterial));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_hMaterial);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_hMaterial);
      SCR_fn_v_RdL0_SplitSectionName(SCR_M_RdL0_GetCompleteSectionNameR(0), sFile, sAction, sIdent);
      sprintf(sFile, "%s.vmt", g_sFileIn);
      SCR_fn_v_RdL0_ComputeSectionName(p_hMaterial->sName, sFile, sAction, sIdent);
      memset(p_hMaterial->sTexture, 0, 100*sizeof(xString));
      memset(p_hMaterial->fTexture1, 0, 100*sizeof(float));
      sprintf(p_hMaterial->sMaterialType, "Flat");
      p_hMaterial->stAmbient.xR = 0.0f;
      p_hMaterial->stAmbient.xG = 0.0f;
      p_hMaterial->stAmbient.xB = 0.0f;
      p_hMaterial->stDiffuse.xR = 0.0f;
      p_hMaterial->stDiffuse.xG = 0.0f;
      p_hMaterial->stDiffuse.xB = 0.0f;
      p_hMaterial->stSpecular.xR = 0.0f;
      p_hMaterial->stSpecular.xG = 0.0f;
      p_hMaterial->stSpecular.xB = 0.0f;
      p_hMaterial->lSpecularExponent = 0;
      p_hMaterial->bAlreadySave = FALSE;
      p_hMaterial->lTexture = 0;
      p_hMaterial->lNbScroll = 0;
	  p_hMaterial->bExistAmbient = FALSE;
	  p_hMaterial->bExistDiffuse = FALSE;
      strcpy(p_hMaterial->sBackface, "");
      strcpy(p_hMaterial->sNameCMT, "");
	  p_hMaterial->bMaxMaterial = TRUE;
      strcpy(p_hMaterial->sMipMapping, "ON");
      strcpy(p_hMaterial->sTilingFirst, "");
      strcpy(p_hMaterial->sTilingSecond, "");
// multitextures
      p_hMaterial->lMultiTexture = 0;
      p_hMaterial->lUVComputing = 0;
      break;

    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstMaterial*,p_hMaterial);

      // Loading Line
      if (strcmp(szAction,M_VMTActionType)==0)
      {
        sprintf(p_hMaterial->sMaterialType, "%s", szParams[0]);
      }

      if (strcmp(szAction,M_VMTActionAmbientColor)==0)
      {
        p_hMaterial->stAmbient.xR = (float)atof(szParams[0]);
        p_hMaterial->stAmbient.xG = (float)atof(szParams[1]);
        p_hMaterial->stAmbient.xB = (float)atof(szParams[2]);
        if (szParams[3]) 
		{	p_hMaterial->stAmbient.xA = (float)atof(szParams[3]);
			p_hMaterial->bExistAmbient = TRUE;
		}
      }

      if (strcmp(szAction,M_VMTActionBackface)==0)
      {
        sprintf(p_hMaterial->sBackface, "%s", szParams[0]);
      }

      if (strcmp(szAction,M_VMTActionDiffuseColor)==0)
      {
        p_hMaterial->stDiffuse.xR = (float)atof(szParams[0]);
        p_hMaterial->stDiffuse.xG = (float)atof(szParams[1]);
        p_hMaterial->stDiffuse.xB = (float)atof(szParams[2]);
        if (szParams[3]) 
		{   p_hMaterial->stDiffuse.xA = (float)atof(szParams[3]);
			p_hMaterial->bExistDiffuse = TRUE;
		}
      }

      if (strcmp(szAction,M_VMTActionSpecularColor)==0)
      {
        p_hMaterial->stSpecular.xR = (float)atof(szParams[0]);
        p_hMaterial->stSpecular.xG = (float)atof(szParams[1]);
        p_hMaterial->stSpecular.xB = (float)atof(szParams[2]);
        p_hMaterial->lSpecularExponent = (long)atoi(szParams[3]);
      }

      if (strcmp(szAction,M_VMTActionScroll)==0)
      {
        p_hMaterial->fScroll1[(long)atoi(szParams[0])] = (float)atof(szParams[1]);
        p_hMaterial->fScroll2[(long)atoi(szParams[0])] = (float)atof(szParams[2]);
        p_hMaterial->lNbScroll = (p_hMaterial->lNbScroll>(long)atoi(szParams[0])?p_hMaterial->lNbScroll+1:(long)atoi(szParams[0])+1);
      }

// multitextures
      if (strcmp(szAction,M_VMTActionMultiTexture)==0)
      {
		xString sFile, sAction, sIdent;
		SCR_tdst_Cxt_Values *p_stVal;
		char *sTexture;
		int i;

		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sName, szParams[1]);

		SCR_fn_v_RdL0_SplitSectionName(szParams[2], sFile, sAction, sIdent);

		if (!strstr(sFile, ".tex"))
			sprintf(sFile, "%s.mod", g_sFileIn);

		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sLinarMipMapping, "ON");
		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sPerspectiveCorrection, "ON");
		for (i=0; i<3; i++)
		  p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].fBlendToColor[i] = 0.0;
		p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].fBlendToAlpha = 0.0;

        SCR_fn_v_RdL0_ComputeSectionName(sAction, MLT_p_cGetFileNameWithoutPath(sFile), M_Texture, sIdent);
		if(SCR_fn_c_RdL0_IsSectionExists(sAction))
		{	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sAction, SCR_CDF_uw_Anl_Normal);
			sTexture = (char *)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
		}

		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sTexture, sIdent);
		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sLoad, sTexture);
		
		strcpy(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sOption, szParams[3]);
		
        p_hMaterial->lMultiTexture++;
	  }

      if (strcmp(szAction,M_VMTActionUVComputing)==0)
      {
		strcpy(p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].sName, szParams[1]);
		strcpy(p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].sObject, szParams[2]);
		
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iPosUSpeed = atoi(szParams[3]);
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iPosU = atoi(szParams[4]);
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iPosVSpeed = atoi(szParams[5]);
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iPosV = atoi(szParams[6]);

		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iAngleSpeed = atoi(szParams[7]);
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iAngleAlpha = atoi(szParams[8]);

		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iScaleU = atoi(szParams[9]);
		p_hMaterial->stUVComputing[p_hMaterial->lUVComputing].iScaleV = atoi(szParams[10]);

        p_hMaterial->lUVComputing++;
	  }

	  if (strcmp(szAction,M_VMTActionAddTexture)==0)
      {
        SCR_tdst_Cxt_Values *p_stVal;
        char *sTexture;

		p_stVal = SCR_fnp_st_RdL0_AnalyseSection(MLT_p_cGetFileNameWithoutPath(szParams[1]), SCR_CDF_uw_Anl_Normal);
		sTexture = (char*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

        strcpy(p_hMaterial->sTexture[p_hMaterial->lTexture], sTexture);
        if(szParams[2])
          p_hMaterial->fTexture1[p_hMaterial->lTexture] = (float)atof(szParams[2]);

        p_hMaterial->lTexture++;
      }
      
      if (strcmp(szAction,M_Texture)==0)
      {
        xString sDir;
        char *sTemp;

		MLT_vCopyTexturesFromRaw(szParams[0], sDir);
		MLT_vCopyTextureIfFound(szParams[0], sDir);

        sTemp = strstr(sDir, "Textures\\");
        if(sTemp == NULL)
        {
          sprintf(sDir, "");
        }
        else
        {
          sprintf(sDir, "%s\\", sTemp+9);
        }

        sprintf(p_hMaterial->sTexture[p_hMaterial->lTexture], "%s%s", sDir, szParams[0]);

        p_hMaterial->lTexture++;
      }
      break;

    case SCR_EA_Anl_EndSection:
      break;
        }
  return  SCR_ERV_Anl_NormalReturn;
}




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

  SCR_M_SvL0_SaveScriptFileHeader(p_stFile);
  sprintf(sComment, ".GMT (Game Material) : %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, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
}

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

  SCR_M_SvL0_SaveScriptFileHeader(p_stFile);
  sprintf(sComment, ".VMT (Visual Material) : %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, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
}

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

  SCR_M_SvL0_SaveScriptFileHeader(p_stFile);
  sprintf(sComment, ".TEX (Texture) : %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, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);

}




/****************************************************************************
*		Description:	save game material
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveGameMaterial (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstGameMaterial	*p_stGameMaterial;
  SCR_tdst_Cxt_Values	*p_stVal;
  MLT_tdstMaterial		*p_stMaterial;
  xString				sSection, sFile, sAction, sIdent, sIdent2;


  p_stMaterial = (MLT_tdstMaterial *)p_vPtr;
  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }
  else
    return;

  p_stMaterial->lSoundType = -1;

  if (!g_bIgnoreGmt)
  {
	SCR_fn_v_RdL0_SplitSectionName(p_stMaterial->sName, sFile, sAction, sIdent);
	sprintf(sFile, "%s.gmt", "temp");
	SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_GameMaterial, sIdent);
	if(SCR_fn_c_RdL0_IsSectionExists(sSection))
	{
      p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sSection, SCR_CDF_uw_Anl_Normal);
      p_stGameMaterial = (MLT_tdstGameMaterial *)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	  strcpy(p_stMaterial->sNameCMT, p_stGameMaterial->sNameCMT);
	  p_stMaterial->bMaxMaterial = p_stGameMaterial->bMaxMaterial;
	  p_stMaterial->lSoundType = p_stGameMaterial->lSoundType;
	}
  }

  SCR_fn_v_RdL0_SplitSectionName(p_stMaterial->sName, sFile, sAction, sIdent);
  sprintf(sFile, "Ed.gmt");
  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_GameMaterial, sIdent);
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(sSection), SCR_CC_C_Cfg_EOL);

  sprintf(sIdent2, "MAT_%s", sIdent);
  sprintf(sFile, "%s%s.vmt", g_sDirectorySave, g_sFileMaterial);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Material, sIdent2);
  SCR_M_SvL0_SaveEntry(p_stFile, M_GMTActionVisualMat, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);
  
  if (strcmp(p_stMaterial->sNameCMT, ""))
  {  //if the name of the collide mat is not null, save an entry in the .gmt file
	 SCR_M_SvL0_SaveEntry(p_stFile, M_GMTActionCollideMat, SCR_CC_C_Cfg_NoChar);
     strcpy(sSection, "ED.cmt^CollideMaterial:");
	 if (p_stMaterial->bMaxMaterial)
		 strcat(sSection, "MAX_");
	 strcat(sSection, p_stMaterial->sNameCMT);
     SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);
  }
  if (p_stMaterial->lSoundType != -1)
  {  //if the name of the collide mat is not null, save an entry in the .gmt file
	 SCR_M_SvL0_SaveEntry(p_stFile, M_GMTActionSoundMat, SCR_CC_C_Cfg_NoChar);
     SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "%d", p_stMaterial->lSoundType);
  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}



/****************************************************************************
*		Description:	save visual material
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveVisualMaterial(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  SCR_tdst_Cxt_Values	*p_stVal;
  MLT_tdstMaterial		*p_stMaterial;
  MLT_tdstMaterial		*p_stOldMaterial=NULL;
  xString				sFile, sAction, sIdent, sIdent2, sSection;
  long					i, l;

  p_stMaterial = (MLT_tdstMaterial *)p_vPtr;
  SCR_fn_v_RdL0_SplitSectionName(p_stMaterial->sName, sFile, sAction, sIdent);
  sprintf(sFile, "%s", p_stFile->a_szOpenFileName);
  sprintf(sIdent2, "MAT_%s", sIdent);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, sAction, sIdent2);

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
	  SCR_fn_v_SvL1_ToEndSection(p_stFile);

  }
  else
  {
    g_bPrintOutOK=FALSE;
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sSection, SCR_CDF_uw_Anl_Normal);
	p_stOldMaterial = (MLT_tdstMaterial*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	g_bPrintOutOK=TRUE;

    if (!g_bIgnoreTex) 
	{   p_stMaterial->lNbScroll = (p_stMaterial->lNbScroll>p_stOldMaterial->lNbScroll?p_stMaterial->lNbScroll:p_stOldMaterial->lNbScroll);
		for(i=0;i<p_stOldMaterial->lNbScroll;i++)
		{
			p_stMaterial->fScroll1[i] = p_stOldMaterial->fScroll1[i];
			p_stMaterial->fScroll2[i] = p_stOldMaterial->fScroll2[i];
		}
	}

    p_stMaterial->lTexture = (p_stMaterial->lTexture>p_stOldMaterial->lTexture ? p_stMaterial->lTexture : p_stOldMaterial->lTexture);
    for(l=1;l<p_stOldMaterial->lTexture;l++)
    {
      strcpy(p_stMaterial->sTexture[l], p_stOldMaterial->sTexture[l]);
    }
    for(l=0;l<p_stOldMaterial->lTexture;l++)
    {
      p_stMaterial->fTexture1[l] = p_stOldMaterial->fTexture1[l];
    }

    strcpy(p_stMaterial->sMaterialType, p_stOldMaterial->sMaterialType);
    if (!g_bIgnoreTex) 
	{   
		p_stMaterial->stAmbient = p_stOldMaterial->stAmbient;
		p_stMaterial->stDiffuse = p_stOldMaterial->stDiffuse;
		p_stMaterial->stSpecular = p_stOldMaterial->stSpecular;
		p_stMaterial->lSpecularExponent = p_stOldMaterial->lSpecularExponent;
		p_stMaterial->bExistDiffuse = p_stOldMaterial->bExistDiffuse;
		p_stMaterial->bExistAmbient = p_stOldMaterial->bExistAmbient;
		strcpy(p_stMaterial->sBackface, p_stOldMaterial->sBackface);

// multitextures
		for (i=0; i<p_stMaterial->lMultiTexture; i++)
		{	
			strcpy(p_stMaterial->stMultiTexture[i].sName, p_stOldMaterial->stMultiTexture[i].sName);
			strcpy(p_stMaterial->stMultiTexture[i].sTexture, p_stOldMaterial->stMultiTexture[i].sTexture);
			strcpy(p_stMaterial->stMultiTexture[i].sOption, p_stOldMaterial->stMultiTexture[i].sOption);
		}
		for (i=0; i<p_stMaterial->lUVComputing; i++)
		{	
			strcpy(p_stMaterial->stUVComputing[i].sName, p_stOldMaterial->stUVComputing[i].sName);
			strcpy(p_stMaterial->stUVComputing[i].sObject, p_stOldMaterial->stUVComputing[i].sObject);
			p_stMaterial->stUVComputing[i].iPosUSpeed = p_stOldMaterial->stUVComputing[i].iPosUSpeed;
			p_stMaterial->stUVComputing[i].iPosU = p_stOldMaterial->stUVComputing[i].iPosU;
			p_stMaterial->stUVComputing[i].iPosVSpeed = p_stOldMaterial->stUVComputing[i].iPosVSpeed;
			p_stMaterial->stUVComputing[i].iPosV = p_stOldMaterial->stUVComputing[i].iPosV;
			p_stMaterial->stUVComputing[i].iAngleSpeed = p_stOldMaterial->stUVComputing[i].iAngleSpeed;
			p_stMaterial->stUVComputing[i].iAngleAlpha = p_stOldMaterial->stUVComputing[i].iAngleAlpha;
			p_stMaterial->stUVComputing[i].iScaleU = p_stOldMaterial->stUVComputing[i].iScaleU;
			p_stMaterial->stUVComputing[i].iScaleV = p_stOldMaterial->stUVComputing[i].iScaleV;
		}
	}

    SCR_fn_v_SvL1_DeleteSection(p_stFile);
  }

  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(sSection), SCR_CC_C_Cfg_EOL);
  
  SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionType, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stMaterial->sMaterialType);
  
  SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionAmbientColor, SCR_CC_C_Cfg_NoChar);

  if (p_stMaterial->bExistAmbient)
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 5, "%.6f,%.6f,%.6f,%.6f",
      p_stMaterial->stAmbient.xR,
      p_stMaterial->stAmbient.xG,
      p_stMaterial->stAmbient.xB,
      p_stMaterial->stAmbient.xA);
  else
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%.6f,%.6f,%.6f",
      p_stMaterial->stAmbient.xR,
      p_stMaterial->stAmbient.xG,
      p_stMaterial->stAmbient.xB);
  
  SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionDiffuseColor, SCR_CC_C_Cfg_NoChar);

  if (p_stMaterial->bExistDiffuse)
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 5, "%.6f,%.6f,%.6f,%.6f",
      p_stMaterial->stDiffuse.xR,
      p_stMaterial->stDiffuse.xG,
      p_stMaterial->stDiffuse.xB,
      p_stMaterial->stDiffuse.xA);
  else
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%.6f,%.6f,%.6f",
      p_stMaterial->stDiffuse.xR,
      p_stMaterial->stDiffuse.xG,
      p_stMaterial->stDiffuse.xB);

  SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionSpecularColor, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 5, "%.6f,%.6f,%.6f,%d",
      p_stMaterial->stSpecular.xR,
      p_stMaterial->stSpecular.xG,
      p_stMaterial->stSpecular.xB,
      p_stMaterial->lSpecularExponent);

  if (strcmp(p_stMaterial->sBackface, ""))
  {	SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionBackface, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%s",
      p_stMaterial->sBackface);
  }

  for(i=0;i<p_stMaterial->lTexture;i++)
    if(strlen(p_stMaterial->sTexture[i]) != 0)
    {
      sprintf(sFile, "%s%s.tex", g_sDirectorySave, g_sFileIn);
      sprintf(sIdent, "TEX_%s", MLT_p_cEraseSpace(MLT_p_cGetFileNameWithoutExt(MLT_p_cGetFileNameWithoutPath(p_stMaterial->sTexture[i]))));
      SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Texture, sIdent);

      SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionAddTexture, SCR_CC_C_Cfg_NoChar);
      if(p_stMaterial->fTexture1[i])
        SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%s,%f", i, sSection, p_stMaterial->fTexture1[i]);
      else
        SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%s", i, sSection);
    }

  for(i=0;i<p_stMaterial->lNbScroll;i++)
  {
    SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionScroll, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%d,%.6f,%.6f", 
      i, 
      p_stMaterial->fScroll1[i], 
      p_stMaterial->fScroll2[i]);
  }
    
  
// multitextures
  for(i=0;i<p_stMaterial->lMultiTexture;i++)
  {   
	  sprintf(sFile, "%s%s.tex", g_sDirectorySave, g_sFileIn);
      sprintf(sIdent, "%s", p_stMaterial->stMultiTexture[i].sTexture);
      SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Texture, sIdent);

      SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionMultiTexture, SCR_CC_C_Cfg_NoChar);
      SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 5, "%d,%s,%s,%s", i, p_stMaterial->stMultiTexture[i].sName, sSection, p_stMaterial->stMultiTexture[i].sOption);
  }

  for(i=0;i<p_stMaterial->lUVComputing;i++)
  {   
	  SCR_M_SvL0_SaveEntry(p_stFile, M_VMTActionUVComputing, SCR_CC_C_Cfg_NoChar);
      SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 12, "%d,%s,%s,%d,%d,%d,%d,%d,%d,%d,%d", i, 
																		p_stMaterial->stUVComputing[i].sName, 
																		p_stMaterial->stUVComputing[i].sObject, 
																		p_stMaterial->stUVComputing[i].iPosUSpeed, 
																		p_stMaterial->stUVComputing[i].iPosU, 
																		p_stMaterial->stUVComputing[i].iPosVSpeed, 
																		p_stMaterial->stUVComputing[i].iPosV, 
																		p_stMaterial->stUVComputing[i].iAngleSpeed, 
																		p_stMaterial->stUVComputing[i].iAngleAlpha, 
																		p_stMaterial->stUVComputing[i].iScaleU, 
																		p_stMaterial->stUVComputing[i].iScaleV
																		);

  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}

  
/****************************************************************************
*		Description:	save texture 
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveTexture (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstMaterial *p_stMaterial;
  MLT_tdstMaterial *p_stOldMaterial=NULL;
  xString			sFile, sAction, sIdent2, sSection;
  BOOL				bChange=FALSE;
  BOOL				bChangeTiling=FALSE;
  int				i, l;

  p_stMaterial = (MLT_tdstMaterial *)p_vPtr;
  sprintf(sFile, "%s", p_stFile->a_szOpenFileName);

  sprintf(sAction, M_Texture);

// multitextures
  if (!p_stMaterial->lMultiTexture)
	sprintf(sIdent2, "TEX_%s", MLT_p_cEraseSpace(MLT_p_cGetFileNameWithoutExt(MLT_p_cGetFileNameWithoutPath(p_stMaterial->sTexture[p_stMaterial->lCurTexture]))));
  else
	sprintf(sIdent2, "%s", MLT_p_cEraseSpace(MLT_p_cGetFileNameWithoutExt(MLT_p_cGetFileNameWithoutPath(p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sTexture))));

  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, sAction, sIdent2);

  // init the material default parameters
  strcpy(p_stMaterial->sQuality[p_stMaterial->lCurTexture], "NORMAL");
  strcpy(p_stMaterial->sMipMapping, "ON");

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
	bChangeTiling = TRUE;
  }
  else
  {
	SCR_tdst_Cxt_Values *p_stVal;
	xString sName, sFile1, sAction1, sIdent1;
	SCR_fn_v_RdL0_SplitSectionName(p_stMaterial->sName, sFile1, sAction1, sIdent1);
	strcpy(sName, "MAT_");
	strcat(sName, sIdent1);
	strcpy(sIdent1, sName);
	SCR_fn_v_RdL0_ComputeSectionName(sName, sFile1, sAction1, sIdent1);

	if(SCR_fn_c_RdL0_IsSectionExists(sName))
	{
		p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sName, SCR_CDF_uw_Anl_Normal);
		p_stOldMaterial = (MLT_tdstMaterial *)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

		strcpy(p_stMaterial->sMipMapping, p_stOldMaterial->sMipMapping);
		strcpy(p_stMaterial->sTilingFirst, p_stOldMaterial->sTilingFirst);
		strcpy(p_stMaterial->sTilingSecond, p_stOldMaterial->sTilingSecond);

// multitextures
		if (!g_bIgnoreTex)
		{	strcpy(p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sLinarMipMapping, p_stOldMaterial->stMultiTexture[p_stMaterial->lCurTexture].sLinarMipMapping);
			strcpy(p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sPerspectiveCorrection, p_stOldMaterial->stMultiTexture[p_stMaterial->lCurTexture].sPerspectiveCorrection);
			for (i=0; i<3; i++)
				p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToColor[i] = p_stOldMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToColor[i];
			p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToAlpha = p_stOldMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToAlpha;
		}

		p_stMaterial->lTexture = (p_stMaterial->lTexture>p_stOldMaterial->lTexture ? p_stMaterial->lTexture : p_stOldMaterial->lTexture);
		for(l=0;l<p_stOldMaterial->lTexture;l++)
		{
			strcpy(p_stMaterial->sQuality[l], p_stOldMaterial->sQuality[l]);
			strcpy(p_stMaterial->sEnable[l], p_stOldMaterial->sEnable[l]);
			strcpy(p_stMaterial->sOn[l], p_stOldMaterial->sOn[l]);
			for (i=0; i<4; i++)
			{
				p_stMaterial->iVal[i][l]=p_stOldMaterial->iVal[i][l];
			}
			bChange = TRUE;
		}
	}
	else
		bChangeTiling = TRUE;

// multitextures
    if (!p_stMaterial->lMultiTexture)
		SCR_fn_v_SvL1_DeleteSection(p_stFile);
    else
	{
		//delete the section!!!
		if (!g_bIgnoreTex)
			return;
		SCR_fn_v_SvL1_DeleteSection(p_stFile);
	}
  }

  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(sSection), SCR_CC_C_Cfg_EOL);

// multitextures
  if (!p_stMaterial->lMultiTexture)
  {	
	if(strlen(p_stMaterial->sTexture[p_stMaterial->lCurTexture]))
	{
		SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionLoadTexture, SCR_CC_C_Cfg_NoChar);
		SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stMaterial->sTexture[p_stMaterial->lCurTexture]);
	}
  }
  else
  {
	SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionLoadTexture, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sLoad);
  }

  SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionTiling, SCR_CC_C_Cfg_NoChar);

  if (bChangeTiling || g_bIgnoreTex)
  {
     switch(MLT_vGetCyclingTextNameKey(p_stMaterial->sTexture[p_stMaterial->lCurTexture]))
	 {
		case MLT_C_lCyclingUV :
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "ON,ON");
			break;
		case MLT_C_lCyclingU :
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "ON,OFF");
			break;
		case MLT_C_lCyclingV :
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "OFF,ON");
			break;
		case MLT_C_lNoCycling :
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "OFF,OFF");
			break;
	 }
  }
  else 
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%s,%s", p_stMaterial->sTilingFirst, p_stMaterial->sTilingSecond);

  
  SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionMipMapping, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, p_stMaterial->sMipMapping);
  
  SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionChromakey, SCR_CC_C_Cfg_NoChar);

  // Enable parameter
  if (MLT_vGetNonZeroTextNameKey(p_stMaterial->sTexture[p_stMaterial->lCurTexture]))
	  strcpy(p_stMaterial->sEnable[p_stMaterial->lCurTexture], "ENABLE");
  else
	  strcpy(p_stMaterial->sEnable[p_stMaterial->lCurTexture], "DISABLE");

  if (!bChange)
  {	if(MLT_vGetNonZeroTextNameKey(p_stMaterial->sTexture[p_stMaterial->lCurTexture]))
		SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "ENABLE,FILTERING_ON,0,0,0,0");
	else
		SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "DISABLE,FILTERING_OFF,0,0,0,0");
  }
  else
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 7, "%s,%s,%d,%d,%d,%d", p_stMaterial->sEnable[p_stMaterial->lCurTexture],p_stMaterial->sOn[p_stMaterial->lCurTexture],p_stMaterial->iVal[0][p_stMaterial->lCurTexture],p_stMaterial->iVal[1][p_stMaterial->lCurTexture],p_stMaterial->iVal[2][p_stMaterial->lCurTexture],p_stMaterial->iVal[3][p_stMaterial->lCurTexture]);
  
  SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionQuality, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, p_stMaterial->sQuality[p_stMaterial->lCurTexture]);

  SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionPriority, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, "LOW");
  

// multitextures
  if (p_stMaterial->lMultiTexture)
  {	
	SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionLinarMipMapping, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sLinarMipMapping);

	SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionCorrection, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 1, p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].sPerspectiveCorrection);

	SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionBlendToColor, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 4, "%f,%f,%f", p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToColor[0], p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToColor[1], p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToColor[2]);

	SCR_M_SvL0_SaveEntry(p_stFile, M_TEXActionBlendToAlpha, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%f", p_stMaterial->stMultiTexture[p_stMaterial->lCurTexture].fBlendToAlpha);
  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  p_stMaterial->lCurTexture++;
}





/****************************************************************************
*		Description:	check texture name for cycling 
*
*		Parameters:		d_ucFileName	: texture name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
long MLT_vGetCyclingTextNameKey (char *d_ucFileName)
{
  long lLetterCount;
  unsigned char a4_ucDosExtend[4],a4_Extend[4];

  MLT_v2LastLetters( d_ucFileName, a4_ucDosExtend );
  
  for (lLetterCount = 0; lLetterCount < 256; lLetterCount ++)
  {
    if (d_ucFileName [lLetterCount] == 0) 
		break;
  }
  if (strcmp(a4_ucDosExtend,"NZ") == 0)  lLetterCount -=2;
  
  a4_Extend[0] = toupper (d_ucFileName [lLetterCount-7]);
  a4_Extend[1] = toupper (d_ucFileName [lLetterCount-6]);
  a4_Extend[2] = toupper (d_ucFileName [lLetterCount-5]);
  a4_Extend[3] = 0;
  if (strcmp(a4_Extend+1,"TX") == 0) 
	  return MLT_C_lCyclingU;
  if (strcmp(a4_Extend+1,"TY") == 0) 
	  return MLT_C_lCyclingV;
  if (strcmp(a4_Extend,"TXY") == 0) 
	  return MLT_C_lCyclingUV;

  return MLT_C_lNoCycling;
}


/****************************************************************************
*		Description:	check texture name for color key on 
*
*		Parameters:		d_ucFileName	: texture name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL MLT_vGetNonZeroTextNameKey (char *d_ucFileName)
{
  unsigned char a4_ucDosExtend[4];

  MLT_v2LastLetters( d_ucFileName, a4_ucDosExtend );
  if (strcmp(a4_ucDosExtend,"NZ") == 0)  
  {
    return TRUE;
  }

  return FALSE;
}


/****************************************************************************
*		Description:	get last 2 letters of the texture name 
*
*		Parameters:		d_ucFileName	: texture name
*						a4_Extend		: returns the last 2 letters
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_v2LastLetters (char *d_ucFileName , char *a4_Extend)
{
  long lLetterCount;

  for ( lLetterCount = 0 ; lLetterCount < 256 ; lLetterCount ++ )
  {
    if (d_ucFileName [lLetterCount] == 0) break;
  }
  a4_Extend[0] = toupper (d_ucFileName [lLetterCount-6]);
  a4_Extend[1] = toupper (d_ucFileName [lLetterCount-5]);
  a4_Extend[2] = 0;
}




