// **********************************************************************************
// * "Levels.c"																		*
// * Written by :	Mihaela Tancu													*
// **********************************************************************************

#include <windows.h>
#include <stdio.h> 
#include <string.h>
#include <direct.h>

#include "Levels.h"
#include "Anims.h"


MLT_tdstAlways **g_hAlwaysInFile;
long g_lAlwaysIndex;

MLT_tdstInsertCharacter **g_hInsertCharacterInFile;
long g_lInsertCharacterIndex;

MLT_tdstTextures **g_hTexturesInFile;
long g_lTexturesIndex;


/*
 =======================================================================================
  Name:			MLT_vInitSystem
  Description:	Init the system
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vInitSystem()
{
  Mmg_fn_vFirstInitMmgModule(1);
  SCR_fn_v_RdL0_Init();

  MLT_vInitALWLoad();
  MLT_vInitLVLLoad();
  MLT_vInitSTALoad();
  MLT_vInitTEXLoad();
}


/*
 =======================================================================================
  Name:			MLT_vExitSystem
  Description:	close and erase
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vExitSystem()
{
  SCR_fn_v_RdL0_Close();
  
  MLT_vEraseALWLib();
  MLT_vEraseLVLLib();
  MLT_vEraseSTALib();
  MLT_vEraseTEXLib();
}


/*
 =======================================================================================
  Name:			MLT_xLoadAlways
  Description:	Load section from script file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue MLT_xLoadAlways(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstAlways *p_stAlways;
  char *p_Char, *p_Char1;
  char szString[256];

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_stAlways=(MLT_tdstAlways*)malloc(sizeof(MLT_tdstAlways));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_stAlways);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_stAlways);
      sprintf(p_stAlways->sName,"%s",SCR_M_RdL0_GetCompleteSectionNameR(0));
      break;
                
    case SCR_EA_Anl_Entry:
      // Recuperation du LoadGeometric a remplir
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstAlways*,p_stAlways);

      // Loading of object's Always
      if (strcmp(szAction,M_LinkCharacter)==0)
      {
	      sprintf(p_stAlways->sLinkCharacter,"%s",szParams[0]);
		  p_Char = strstr(p_stAlways->sLinkCharacter, "Families");
		  strcpy(szString, p_Char+9);
		  p_Char1 = strchr(szString, '\\');
		  szString[strlen(szString) - strlen(p_Char1)]='\0';
		  strcpy(p_stAlways->sLinkCharacter, szString);
      } 
      break;

    case SCR_EA_Anl_EndSection:
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}



/*
 =======================================================================================
  Name:			GLI_xLoadALWCallback
  Description:	Load ALW file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue GLI_xLoadALWCallback(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstAlways **p_xAlwaysInFile;
  long xNbAlways;
  
  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_xAlwaysInFile = (MLT_tdstAlways**)malloc(sizeof(MLT_tdstAlways*));
    
      SCR_M_RdL0_SetContextLong(0,0,(long)p_xAlwaysInFile);
      SCR_M_RdL0_SetContextLong(0,1,(long)0);
      break;

    case SCR_EA_Anl_EndSubSection:
      if (strcmp(szParams[0],M_InsertAlways)==0)
      {
        MLT_tdstAlways *hAlways;

        SCR_M_RdL0_GetContextLong(0,0,MLT_tdstAlways**,p_xAlwaysInFile);
        SCR_M_RdL0_GetContextLong(0,1,long,xNbAlways);

        p_xAlwaysInFile = (MLT_tdstAlways**)realloc(p_xAlwaysInFile,(++xNbAlways)*sizeof(MLT_tdstAlways*));

        SCR_M_RdL0_GetSectionLong(1,0,MLT_tdstAlways*,hAlways);
        p_xAlwaysInFile[xNbAlways-1]=hAlways;

        SCR_M_RdL0_SetContextLong(0,0,(long)p_xAlwaysInFile);
        SCR_M_RdL0_SetContextLong(0,1,(long)xNbAlways);
      }
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstAlways**,p_xAlwaysInFile);
      SCR_M_RdL0_GetContextLong(0,1,long,xNbAlways);
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_xAlwaysInFile);
      SCR_M_RdL0_SetSectionLong(0,1,(long)xNbAlways);
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}

/*
 =======================================================================================
  Name:			MLT_vInitALWLoad
  Description:	init for loading an ALW file
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vInitALWLoad()
{
  // Init des scripts et lecture du fichier
  SCR_fn_v_RdL0_RegisterCallback(M_InsertAlways,MLT_xLoadAlways, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_FichierALW,GLI_xLoadALWCallback, SCR_CRC_c_RdL0_ForFile);
}


/*
 =======================================================================================
  Name:			MLT_vLoadALWFileInLib
  Description:	load ALW file in a lib
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vLoadALWFileInLib(char *szFileName)
{
  SCR_tdst_Cxt_Values *p_stVal;

  if(SCR_fn_c_RdL0_IsSectionExists(szFileName))
  {
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);

	g_hAlwaysInFile = (MLT_tdstAlways **)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	g_lAlwaysIndex = (long)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,1);
  }
}


/*
 =======================================================================================
  Name:			MLT_vEraseALWLib
  Description:	free all the object in the list
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vEraseALWLib()
{
  int i;

  if(g_hAlwaysInFile == NULL) return;

  for(i=0; i<g_lAlwaysIndex; i++)
  {
    if(g_hAlwaysInFile[i] == NULL) continue;
    free(g_hAlwaysInFile[i]);
  }
  g_lAlwaysIndex = 0;
  free(g_hAlwaysInFile);
  g_hAlwaysInFile = NULL;
}
//Mihaela Tancu - ALW



//Mihaela Tancu - LVL
/*
 =======================================================================================
  Name:			MLT_xLoadInsertCharacter
  Description:	load section in script file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue MLT_xLoadInsertCharacter(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstInsertCharacter *p_stInsertCharacter;
  char *p_Char, *p_Char1;
  char szString[256];

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_stInsertCharacter=(MLT_tdstInsertCharacter*)malloc(sizeof(MLT_tdstInsertCharacter));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_stInsertCharacter);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_stInsertCharacter);
      sprintf(p_stInsertCharacter->sName,"%s",SCR_M_RdL0_GetCompleteSectionNameR(0));
      break;
                
    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstInsertCharacter*,p_stInsertCharacter);

      // Loading of object's InsertCharacter
      if (strcmp(szAction,M_LinkCharacter)==0)
      {
	      sprintf(p_stInsertCharacter->sLinkCharacter,"%s",szParams[0]);
		  p_Char = strstr(p_stInsertCharacter->sLinkCharacter, "Families");
		  strcpy(szString, p_Char+9);
		  p_Char1 = strchr(szString, '\\');
		  szString[strlen(szString) - strlen(p_Char1)]='\0';
		  strcpy(p_stInsertCharacter->sLinkCharacter, szString);
      } 
      break;

    case SCR_EA_Anl_EndSection:
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


/*
 =======================================================================================
  Name:			GLI_xLoadLVLCallback
  Description:	load LVL file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue GLI_xLoadLVLCallback(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstInsertCharacter **p_xInsertCharacterInFile;
  long xNbInsertCharacter;
  
  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_xInsertCharacterInFile = (MLT_tdstInsertCharacter**)malloc(sizeof(MLT_tdstInsertCharacter*));
    
      SCR_M_RdL0_SetContextLong(0,0,(long)p_xInsertCharacterInFile);
      SCR_M_RdL0_SetContextLong(0,1,(long)0);
      break;

    case SCR_EA_Anl_EndSubSection:
      if (strcmp(szParams[0],M_InsertCharacter)==0)
      {
        MLT_tdstInsertCharacter *hInsertCharacter;

        SCR_M_RdL0_GetContextLong(0,0,MLT_tdstInsertCharacter**,p_xInsertCharacterInFile);
        SCR_M_RdL0_GetContextLong(0,1,long,xNbInsertCharacter);

        p_xInsertCharacterInFile = (MLT_tdstInsertCharacter**)realloc(p_xInsertCharacterInFile,(++xNbInsertCharacter)*sizeof(MLT_tdstInsertCharacter*));

        SCR_M_RdL0_GetSectionLong(1,0,MLT_tdstInsertCharacter*,hInsertCharacter);
        p_xInsertCharacterInFile[xNbInsertCharacter-1]=hInsertCharacter;

        SCR_M_RdL0_SetContextLong(0,0,(long)p_xInsertCharacterInFile);
        SCR_M_RdL0_SetContextLong(0,1,(long)xNbInsertCharacter);
      }
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstInsertCharacter**,p_xInsertCharacterInFile);
      SCR_M_RdL0_GetContextLong(0,1,long,xNbInsertCharacter);
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_xInsertCharacterInFile);
      SCR_M_RdL0_SetSectionLong(0,1,(long)xNbInsertCharacter);
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


/*
 =======================================================================================
  Name:			MLT_vInitLVLLoad
  Description:	init for loading an LVL file
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vInitLVLLoad()
{
  // Init des scripts et lecture du fichier
  SCR_fn_v_RdL0_RegisterCallback(M_InsertCharacter,MLT_xLoadInsertCharacter, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_FichierLVL,GLI_xLoadLVLCallback, SCR_CRC_c_RdL0_ForFile);
}


/*
 =======================================================================================
  Name:			MLT_vLoadLVLFileInLib
  Description:	loading an LVL file in lib
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vLoadLVLFileInLib(char *szFileName)
{
  SCR_tdst_Cxt_Values *p_stVal;

  if(SCR_fn_c_RdL0_IsSectionExists(szFileName))
  {
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);

	g_hInsertCharacterInFile = (MLT_tdstInsertCharacter **)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	g_lInsertCharacterIndex = (long)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,1);
  }
}


/*
 =======================================================================================
  Name:			MLT_vEraseLVLLib
  Description:	free all the object in the list
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vEraseLVLLib()
{
  int i;

  if(g_hInsertCharacterInFile == NULL) return;

  for(i=0; i<g_lInsertCharacterIndex; i++)
  {
    if(g_hInsertCharacterInFile[i] == NULL) continue;
    free(g_hInsertCharacterInFile[i]);
  }
  g_lInsertCharacterIndex = 0;
  free(g_hInsertCharacterInFile);
  g_hInsertCharacterInFile = NULL;
}
//Mihaela Tancu - LVL


//Mihaela Tancu - TEX
/*
 =======================================================================================
  Name:			MLT_xLoadTextures
  Description:	load section in script file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue MLT_xLoadTextures(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstTextures *p_stTextures;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_stTextures=(MLT_tdstTextures*)malloc(sizeof(MLT_tdstTextures));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_stTextures);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_stTextures);
      sprintf(p_stTextures->sName,"%s",SCR_M_RdL0_GetCompleteSectionNameR(0));
      break;
                
    case SCR_EA_Anl_Entry:
      // Recuperation du LoadGeometric a remplir
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstTextures*,p_stTextures);

      // Loading of object's Textures
      if (strcmp(szAction,M_LoadTexture)==0)
      {
		  sprintf(p_stTextures->sLoadTexture,"%s",szParams[0]);
      } 
      break;

    case SCR_EA_Anl_EndSection:
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}



/*
 =======================================================================================
  Name:			GLI_xLoadTEXCallback
  Description:	load TEX file
  Author:		Mihaela Tancu
 =======================================================================================
 */
SCR_tde_Anl_ReturnValue GLI_xLoadTEXCallback(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstTextures **p_xTexturesInFile;
  long xNbTextures;
  
  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_xTexturesInFile = (MLT_tdstTextures**)malloc(sizeof(MLT_tdstTextures*));
    
      SCR_M_RdL0_SetContextLong(0,0,(long)p_xTexturesInFile);
      SCR_M_RdL0_SetContextLong(0,1,(long)0);
      break;

    case SCR_EA_Anl_EndSubSection:
      if (strcmp(szParams[0],M_Texture)==0)
      {
        MLT_tdstTextures *hTextures;

        SCR_M_RdL0_GetContextLong(0,0,MLT_tdstTextures**,p_xTexturesInFile);
        SCR_M_RdL0_GetContextLong(0,1,long,xNbTextures);

        p_xTexturesInFile = (MLT_tdstTextures**)realloc(p_xTexturesInFile,(++xNbTextures)*sizeof(MLT_tdstTextures*));

        SCR_M_RdL0_GetSectionLong(1,0,MLT_tdstTextures*,hTextures);
        p_xTexturesInFile[xNbTextures-1]=hTextures;

        SCR_M_RdL0_SetContextLong(0,0,(long)p_xTexturesInFile);
        SCR_M_RdL0_SetContextLong(0,1,(long)xNbTextures);
      }
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstTextures**,p_xTexturesInFile);
      SCR_M_RdL0_GetContextLong(0,1,long,xNbTextures);
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_xTexturesInFile);
      SCR_M_RdL0_SetSectionLong(0,1,(long)xNbTextures);
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


/*
 =======================================================================================
  Name:			MLT_vInitTEXLoad
  Description:	init for loading an TEX file
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vInitTEXLoad()
{
  // Init des scripts et lecture du fichier
  SCR_fn_v_RdL0_RegisterCallback(M_Texture,MLT_xLoadTextures, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_FichierTEX,GLI_xLoadTEXCallback, SCR_CRC_c_RdL0_ForFile);
}


/*
 =======================================================================================
  Name:			MLT_vLoadTEXFileInLib
  Description:	loading an TEX file in lib
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vLoadTEXFileInLib(char *szFileName)
{
  SCR_tdst_Cxt_Values *p_stVal;

  if(SCR_fn_c_RdL0_IsSectionExists(szFileName))
  {
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);

	g_hTexturesInFile = (MLT_tdstTextures **)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	g_lTexturesIndex = (long)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,1);
  }
}



/*
 =======================================================================================
  Name:			MLT_vEraseTEXLib
  Description:	free all the object in the list
  Author:		Mihaela Tancu
 =======================================================================================
 */
void MLT_vEraseTEXLib()
{
  int i;

  if(g_hTexturesInFile == NULL) return;

  for(i=0; i<g_lTexturesIndex; i++)
  {
    if(g_hTexturesInFile[i] == NULL) continue;
    free(g_hTexturesInFile[i]);
  }
  g_lTexturesIndex = 0;
  free(g_hTexturesInFile);
  g_hTexturesInFile = NULL;
}
//Mihaela Tancu - TEX


/*
 =======================================================================================
  Name:			MLT_p_cGetFileNameWithoutExt
  Description:	return the file name whitout extension
  Author:		Mihaela Tancu
 =======================================================================================
 */
char *MLT_p_cGetFileNameWithoutExt(char *_sFileName)
{
  char *p_char;
  static xString sFileName;

  sprintf(sFileName, "%s", _sFileName);
  p_char = sFileName;
  if ((p_char=strstr(sFileName, "."))!=NULL)
	*p_char = 0;
  return sFileName;
}


/*
 =======================================================================================
  Name:			MLT_p_cGetFileNameWithoutPath
  Description:	return the file name whitout path
  Author:		Mihaela Tancu
 =======================================================================================
 */
char *MLT_p_cGetFileNameWithoutPath(char *sFileName)
{
	char *p_char;

	p_char = sFileName + strlen(sFileName) - 1;
	if (((p_char = strrchr( sFileName, '/')) != NULL) || ((p_char = strrchr( sFileName, '\\')) != NULL))
		p_char++;
	else
		p_char = sFileName;
	return p_char;
}


