#include "Options/Specif/SubMapPs.h"
 
void GAM_fn_vCreateListOfLevel(void)
{
	unsigned long i;
	FIL_tdxHandleToFileNameListElement hFileNameListElement,hNextFileNameListElement;
	char szFileName[MAX_PATH];
	
	sprintf(szFileName,"%s/*",fn_szGetLevelsDataPath());
	g_stEngineStructure.hLevelNameList=FIL_fn_hSearchFile(szFileName,_A_SUBDIR);
	
	if (LST2_M_DynamicGetNumberOfElements(&g_stEngineStructure.hLevelNameList->hFileNameListAnchor))
	{
		LST2_M_DynamicForEachMovingElementOf(&g_stEngineStructure.hLevelNameList->hFileNameListAnchor,hFileNameListElement,hNextFileNameListElement,i)
		{
			sprintf(szFileName,"%s/%s/%s.%s",fn_szGetLevelsDataPath(),hFileNameListElement->szFileName,hFileNameListElement->szFileName,C_ScriptDescriptionSuffixe);
			if (!FIL_fn_bIsFileExist(szFileName))
			{
				FIL_fn_vDestroyOnElementInFileNameList(hFileNameListElement);
			}
		}
	}
	else
	{ /* try to find levels on CDROM*/
		
		sprintf(szFileName,"%c:\\%s/*",g_cCDROM,fn_szGetLevelsDataPath());
		
		g_stEngineStructure.hLevelNameList=FIL_fn_hSearchFile(szFileName,_A_SUBDIR);
		
		LST2_M_DynamicForEachMovingElementOf(&g_stEngineStructure.hLevelNameList->hFileNameListAnchor,hFileNameListElement,hNextFileNameListElement,i)
		{
			sprintf(szFileName,"%c:\\%s/%s/%s.%s",g_cCDROM,fn_szGetLevelsDataPath(),hFileNameListElement->szFileName,hFileNameListElement->szFileName,C_ScriptDescriptionSuffixe);
			if (!FIL_fn_bIsFileExist(szFileName))
			{
				FIL_fn_vDestroyOnElementInFileNameList(hFileNameListElement);
			}
		}
	}
/* ANNECY MT - 07/09/98 {*/
	g_stEngineStructure.lSubMapNumber = GAM_M_MakeSubMapNumber(255,255);
/* END ANNECY MT }*/
}
 
/*===================================================
Functions for thr choice of preferences
===================================================*/
tdstPreferences g_stPreferences;
void GAM_fn_vReadPreferences(void)
{
	char szTemp[_MAX_PATH];
	char szCurrentDir[_MAX_PATH];
	
	GetCurrentDirectory(_MAX_PATH, szCurrentDir);
	strcat(szCurrentDir,"\\StartPrg.ini");
	
	GetPrivateProfileString("Start","AskAtEachTime","0",szTemp,255,szCurrentDir);
	g_stPreferences.cAsk = (char) atoi(szTemp); 
	/* Get if you want to use binary data*/
	GetPrivateProfileString("Start","UseBinaryData","0",szTemp,_MAX_PATH,szCurrentDir);
	g_stPreferences.cBinary = (char) atoi(szTemp);
 
    /* Want to use binary dsc (game.dsb, level.dsb) instead of script dsc ?*/
    GetPrivateProfileString("Start","DscCreated","0",szTemp,_MAX_PATH,szCurrentDir);
	g_stPreferences.cBinaryDsc = (char) atoi(szTemp);
 
#ifdef _DEBUG
	GetPrivateProfileString("Start","GDDebugBin","",g_stPreferences.szBinaryDirectory,64,szCurrentDir);
#else
	GetPrivateProfileString("Start","GDReleaseBin","",g_stPreferences.szBinaryDirectory,64,szCurrentDir);
#endif /*_DEBUG*/
}
void GAM_fn_vSavePreferences(void)
{
	char szTemp[_MAX_PATH];
	char szCurrentDir[_MAX_PATH];
	
	GetCurrentDirectory(_MAX_PATH, szCurrentDir);
	strcat(szCurrentDir,"\\StartPrg.ini");
	
	sprintf(szTemp,"%d",g_stPreferences.cAsk);
	WritePrivateProfileString("Start","AskAtEachTime",szTemp,szCurrentDir);
	sprintf(szTemp,"%d",g_stPreferences.cBinary);
	WritePrivateProfileString("Start","UseBinaryData",szTemp,szCurrentDir);
	sprintf(szTemp,"%d",g_stPreferences.cBinaryDsc);
	WritePrivateProfileString("Start","DscCreated",szTemp,szCurrentDir);
}
 
/*===================================================
Functions for the choice of the gamedata directory
===================================================*/
void GAM_fn_vGetDataDirectoryInStartPrgIni(void)
{
#ifdef ACTIVE_EDITOR
	strcpy(g_a64_cGameDataDirectory,"GameData");
#else
	char szTemp[255];
	char cUseBinaryData=0;
	char cAskAtEachTime=0;
	char cBinIsOk=0;
	char szCurrentDir[_MAX_PATH];
	
	if (g_cMemMode < 2) 
		/* Test if we are in a data binarisation pass*/
	{
		GetCurrentDirectory(_MAX_PATH, szCurrentDir);
		strcat(szCurrentDir,"\\StartPrg.ini");
		
		if (_access("StartPrg.ini", 0) != 0)
		{
			strcpy(g_a64_cGameDataDirectory,"GameData");
			return;
		}
#ifdef _DEBUG
		GetPrivateProfileString("Start","BinDebugIsOk","0",szTemp,255,szCurrentDir);
#else
		GetPrivateProfileString("Start","BinReleaseIsOk","0",szTemp,255,szCurrentDir);
#endif /*_DEBUG*/
		cBinIsOk = (char) atoi(szTemp);

/* ANNECY OA - 20/07/99 { */
		GetPrivateProfileString("Start", "CDDrive", "", szTemp, 255, szCurrentDir);
		g_cCDROM = *szTemp;
/* END ANNECY OA } */
		
		if (cBinIsOk) /* Data are binarized*/
		{
			GAM_fn_vReadPreferences();
			
			if (g_stPreferences.cAsk)
			{
				fn_vChoicePreferences();
			}
			if (g_stPreferences.cBinary)
			{ 
				g_cMemMode  = 1;
                SNA_fn_vSetLoadType( SNA_LOAD_SNAPSHOT );
				strcpy(g_a64_cGameDataDirectory,g_stPreferences.szBinaryDirectory);	
			}
			else
			{
                SNA_fn_vSetLoadType( SNA_LOAD_SCRIPT );
				strcpy(g_a64_cGameDataDirectory,"GameData");
			}
 
            /* Test if the load/save dsc has already been set  (by command line)*/
            if( SNA_fn_ucGetLoadDscType() == SNA_C_LoadScriptDsc )
            {
                /* If not, set it from user preference*/
                if( g_stPreferences.cBinaryDsc )
                {
                    SNA_fn_vSetLoadDscType( SNA_C_LoadBinaryDsc );
                }
            }
 
		}
		else
		{
            SNA_fn_vSetLoadType( SNA_LOAD_SCRIPT );			
			strcpy(g_a64_cGameDataDirectory,"GameData");
		}

		GetPrivateProfileString("Start","UsesBigFile","No",szTemp,255,szCurrentDir);
		SNA_fn_vSetBigFile( ( unsigned char ) strcmp(szTemp , "No") );
	}
	else
	{
		/* pass of binarization*/
        SNA_fn_vSetLoadType( SNA_LOAD_SCRIPT );
		SNA_fn_vSetBigFile( FALSE );
		strcpy(g_a64_cGameDataDirectory,"GameData");
	}
#endif /* ACTIVE_EDITOR*/
}
/*===================================================
Functions for the choice of the gamedata directory
===================================================*/
#if !defined(RETAIL)
ACP_tdxBool  TEST_g_bAutomaticTest;
char         TEST_g_cChangeMapWithRestart;
char         TEST_g_cAllMaps;
char         TEST_g_cWriteLogFile;
long         TEST_g_lWaitBeforeChangeLevel;
long         TEST_g_lWaitMin;	
long         TEST_g_lWaitMax;	
long         TEST_g_lNbChangeMapBeforeExit;
long         TEST_g_lAnimStack;
long         TEST_g_lNbMapInSequence;
char         TEST_g_a_szMapSequence[200][50];
void GAM_fn_vReadFileTestIni()
{
#ifdef ACTIVE_EDITOR
	TEST_g_bAutomaticTest = FALSE;
#else
	char szTemp[256];
	char szCurrentDir[_MAX_PATH];
	
	GetCurrentDirectory(_MAX_PATH, szCurrentDir);
	strcat(szCurrentDir,"\\Test.ini");
		
	if (_access(szCurrentDir, 04) != 0)
	{
		TEST_g_bAutomaticTest = FALSE;
		return;
	}

	/* As soon as there is a test.ini file, we activate autotest.
	 * even if the file is empty (default values will apply) */
	TEST_g_bAutomaticTest=TRUE;

	/* Default value is ALLMAPSONLY */
	TEST_g_cAllMaps = 1;
	GetPrivateProfileString("TEST","MODE","ALLMAPSONLY",szTemp,255,szCurrentDir);
	if (strcmp(szTemp, "RANDOM") == 0)
	{
		TEST_g_cAllMaps = 0;
	}
	else if (strcmp(szTemp, "ALLMAPSONLY") == 0)
	{
		TEST_g_cAllMaps = 1;
	}
	else if (strcmp(szTemp, "SEQUENCE") == 0)
	{
		TEST_g_cAllMaps = 2;
	}
	else if (strcmp(szTemp, "RESTARTONLY") == 0)
	{
		TEST_g_cAllMaps = 3;
	}

	GetPrivateProfileString("TEST","WITHRESTART","0",szTemp,255,szCurrentDir);
	TEST_g_cChangeMapWithRestart = (char) atoi(szTemp);

	GetPrivateProfileString("TEST","STAT","1",szTemp,255,szCurrentDir);
	TEST_g_cWriteLogFile = (char) atoi(szTemp);

	if( TEST_g_cWriteLogFile )
	{
		DeleteFile("Test.log" );
	}

	GetPrivateProfileString("TEST","WAITMIN","60",szTemp,255,szCurrentDir);
	TEST_g_lWaitMin = (long) atoi(szTemp);

	GetPrivateProfileString("TEST","WAITMAX","200",szTemp,255,szCurrentDir);
	TEST_g_lWaitMax= (long) atoi(szTemp);

	GetPrivateProfileString("TEST","ANIMSTACK","0",szTemp,255,szCurrentDir);
	TEST_g_lAnimStack= (long) atoi(szTemp);

	TEST_g_lWaitBeforeChangeLevel =  TEST_g_lWaitMin;

	GetPrivateProfileString("TEST","NBCHANGEMAP","-1",szTemp,255,szCurrentDir);
	TEST_g_lNbChangeMapBeforeExit= (long) atoi(szTemp);

	{ /* Read entries SEQUENCE1=... to SEQUENCE10=... */
		int i, iMapNameLength;
		char szKeyName[50];
		char szMapList[500];
		char *p_szMap;

		TEST_g_lNbMapInSequence = 0;

		for( i=1; i<=10; i++ )
		{
			/* Get one sequence */
			sprintf( szKeyName, "SEQUENCE%i", i );
			GetPrivateProfileString("TEST", szKeyName,"", szMapList, 500, szCurrentDir);

			/* Get first map of sequence */
			p_szMap = strtok( szMapList, " \t" );

			while( p_szMap != NULL )
			{
				/* Copy its name to array of map */
				iMapNameLength = strlen( p_szMap );
				if( iMapNameLength > 2 && iMapNameLength < _MAX_NAME_LEVEL )
					strcpy( TEST_g_a_szMapSequence[TEST_g_lNbMapInSequence++], p_szMap );

				/* Get next map of sequence */
				p_szMap = strtok( NULL, " \t" );
			}
		}
	}
	
#endif /* ACTIVE_EDITOR*/
}
#endif
 
 
/*************************************************************************/
void GAM_fn_vSetSlotName(short _wSlotdId, char *_szSlotName)
{
  /*if we could rename the data file*/
  if ( fn_bRenameSaveGameSlot(g_stGameOptions.a_szSlotNames[_wSlotdId], _szSlotName) )
  {
    /* change the name in the slot database as well*/
/*  	strcpy(g_stGameOptions.a_szSlotNames[_wSlotdId], _szSlotName);*/
	memcpy(	g_stGameOptions.a_szSlotNames[_wSlotdId],  _szSlotName, C_ucSaveGameSlotNameLength );
  }
}
 
/*************************************************************************/
char *GAM_fn_szGetSlotName(short _wSlotId)
{
	return g_stGameOptions.a_szSlotNames[_wSlotId];
}
