#if defined(_AI_LIB_)
	#include "AIUseCPA.h"
	
	#include "specif/AIOption.h"
	#include "AIMacros.h"	
	#include "AI_Erm.h"
	
	#include "Convert.h"
	#include "EnumFunc.h"
 
	#if defined(OPTIMIZED_COMMAND) 
		#include "ProtFunc.h"
	#endif
#else
	#include <string.h>
	#include "specif/AIOption.h"
	
	#include "ConvCst.h"
	#include "EnumFunc.h"
	#include "ConvFunc.h"
#endif /* _AI_LIB_*/
 
#define C_MAX_SIZE_FUNCTION_EDITOR_NAME 50
#define C_MAX_SIZE_FUNCTION_SCRIPT_NAME 41
#define C_MAX_FUNCTION_NB_IN_PARAM 7 /*ANNECY BBB 4 -> 7 */
#define C_MAX_FUNCTION_NB_OUT_PARAM 1
 
typedef struct tdstFunctionEntry_
{
#if defined(ACTIVE_EDITOR)
    /* Function string used in editor*/
    char szFunctionEditorName[C_MAX_SIZE_FUNCTION_EDITOR_NAME+1]; 
    char szFunctionEditorName_En[C_MAX_SIZE_FUNCTION_EDITOR_NAME+1]; 
#endif /* ACTIVE_EDITOR*/
#if defined (AI_USE_SCRIPT)
    /* Function string used in script*/
    char szFunctionScriptName[C_MAX_SIZE_FUNCTION_SCRIPT_NAME+1];
#endif /* AI_USE_SCRIPT */
#if defined(_AI_LIB_)
    /* pointer on Function function*/
    tdp_fn_p_stFunctionFunctionPtr p_fn_p_stFunctionFunction;
#endif /* _AI_LIB_*/
#if defined (AI_USE_SCRIPT) || defined (U64_AIDEBUG)
	/* list of Function parameters (taken from szAutorizedTypeParam) in parameter*/
	char szFunctionTypeInParam[C_MAX_FUNCTION_NB_IN_PARAM+1];
    /* out parameter*/
	char szFunctionTypeOutParam[C_MAX_FUNCTION_NB_OUT_PARAM+1];
#endif /* AI_USE_SCRIPT */
	unsigned char bUltraFunctionFlag;
 
#ifdef PROFILE_IA
	unsigned long ulCallCtr;
	unsigned long ulTimeCtr;
	unsigned long ulOwnTimeCtr;
#endif /* PROFILE_IA*/
} tdstFunctionEntry;
 
#ifdef PROFILE_IA
extern unsigned long ulIndice;
extern unsigned long p_ulTime[255];
extern FILE *file;
#endif /* PROFILE_IA*/
 
 
 
tdstFunctionEntry a_stFunctionTable[eNbFunc]
#if defined(OPTIMIZED_COMMAND)
=
{
#ifdef U64_AIDEBUG
	/* function pointers in Function.c, in params, out param ,ultra-able*/
	#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { d,e,f,g },
	#include "DefFunc.h" 
}
#else /* U64_AIDEBUG*/
#if defined (AI_USE_SCRIPT)
/* Function initialisation*/
#if defined(_AI_LIB_)
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { b,english,c,d,e,f,g },
	#else
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { c,d,e,f,g },
	#endif /* ACTIVE_EDITOR*/
#else
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { b,english,c,e,f,g },
	#else
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { c,e,f,g },
	#endif /* ACTIVE_EDITOR*/
#endif
	#include "DefFunc.h" 
	#undef M_DEFINE_FUNCTION
}
#else /* AI_USE_SCRIPT */
/* Function initialisation*/
#if defined(_AI_LIB_)
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { b,english,d,g },
	#else
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { d,g },
	#endif /* ACTIVE_EDITOR*/
#else
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { b,english,g },
	#else
		#define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g) { g },
	#endif /* ACTIVE_EDITOR*/
#endif
	#include "DefFunc.h" 
	#undef M_DEFINE_FUNCTION
}
#endif /* AI_USE_SCRIPT */
#endif /* U64_AIDEBUG*/
#endif /* OPTIMIZED_COMMAND*/
;
 
#if ! defined(OPTIMIZED_COMMAND)
void fn_vSetFunctionUltraOperator(enum tdeFuncId_ eFuncId,unsigned char bFlag)
{
  a_stFunctionTable[eFuncId].bUltraFunctionFlag=bFlag;
};
#endif /* OPTIMIZED_COMMAND*/
 
unsigned char fn_ucGetFunctionUltraOperator(enum tdeFuncId_ eFuncId)
{
  return(a_stFunctionTable[eFuncId].bUltraFunctionFlag);
}
 
#if defined(_AI_LIB_)
tdp_fn_p_stFunctionFunctionPtr fn_p_fn_pGetFunctionFunctionPtr(enum tdeFuncId_ eFuncId)
{
  return(a_stFunctionTable[eFuncId].p_fn_p_stFunctionFunction);
}
 
 
#ifdef PROFILE_IA
void fn_vFuncSaveTime(unsigned long ulTime, enum tdeFuncId_ eFuncId)
{
	/* if level != 0, then stop time count of last function*/
	if (ulIndice != 0)
	{
		p_ulTime[ulIndice - 1] = (ulTime - p_ulTime[ulIndice - 1]);
	}
	/* set time for this new function and add indice*/
	p_ulTime[ulIndice++] = ulTime;
}
 
 
void fn_vFuncComputeTime(unsigned long ulCurrentTime, unsigned long ulTime, enum tdeFuncId_ eFuncId)
{
	/* compute total time for this function*/
	a_stFunctionTable[eFuncId].ulCallCtr++;
	a_stFunctionTable[eFuncId].ulTimeCtr += (ulCurrentTime - ulTime)/5;
	/* compute own time for this function*/
	ulIndice--;
	/* compute the time for this function*/
	a_stFunctionTable[eFuncId].ulOwnTimeCtr += ((ulCurrentTime  - p_ulTime[ulIndice]) / 5);
	p_ulTime[ulIndice] = 0;
	if (ulIndice != 0)
	{
		/* level != 0, then restart previous function time*/
		p_ulTime[ulIndice-1] = ulCurrentTime - p_ulTime[ulIndice-1];
	}
}
 
 
void dumpFunc()
{
	int i;
	char string[250];
	
	strcpy(string,"x:\\cpa\\");
	strcat(string,fn_p_szGetLevelName());
	strcat(string,".txt");
 
	file = fopen(string,"a+");
	if (file == NULL)
		return;
	
	for (i=0;i<(long)eNbFunc;i++)
	{
		if (a_stFunctionTable[i].ulCallCtr)
		{
			fprintf(file,"%s	%i	%i	%i	%i	%i\n",
				a_stFunctionTable[i].szFunctionScriptName,
				a_stFunctionTable[i].ulCallCtr,
				a_stFunctionTable[i].ulTimeCtr,
				a_stFunctionTable[i].ulOwnTimeCtr,
				a_stFunctionTable[i].ulTimeCtr / a_stFunctionTable[i].ulCallCtr,
				a_stFunctionTable[i].ulOwnTimeCtr / a_stFunctionTable[i].ulCallCtr);
		}
		else
		{
			fprintf(file,"%s	%i	%i	%i	%i	%i\n",
				a_stFunctionTable[i].szFunctionScriptName,
				0,
				0,
				0,
				0,
				0);
		}
 
	}
	fclose(file);
}
 
 
void clearFunc()
{
	int i;
	for (i=0;i<(long)eNbFunc;i++)
	{
		a_stFunctionTable[i].ulCallCtr = 0;
		a_stFunctionTable[i].ulTimeCtr = 0;
		a_stFunctionTable[i].ulOwnTimeCtr = 0;
	}
}
#endif /* PROFILE_IA*/
 
 
#endif /* _AI_LIB_*/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
enum tdeFuncId_ fn_eGetNbFunc()
{
  return(eNbFunc);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#if defined (AI_USE_SCRIPT) || defined (U64_AIDEBUG)
char *szGetFunctionTypeInParamFromId(enum tdeFuncId_ eFuncId)
{
    return(a_stFunctionTable[eFuncId].szFunctionTypeInParam);
}
#endif /* AI_USE_SCRIPT */
 
#if defined(__DEBUG_AI__) || defined(ACTIVE_EDITOR)
char *szFindFunctionScriptNameFromId(enum tdeFuncId_ eFuncId)
{
    return(a_stFunctionTable[eFuncId].szFunctionScriptName);
}
#endif /* __DEBUG_AI__ ACTIVE_EDITOR*/
 
#if defined(ACTIVE_EDITOR) || defined (U64_AIDEBUG)
char *szGetFunctionTypeOutParamFromId(enum tdeFuncId_ eFuncId)
{
    return(a_stFunctionTable[eFuncId].szFunctionTypeOutParam);
}
#endif /* ACTIVE_EDITOR */
 
#if defined(ACTIVE_EDITOR) 
char *szFindFunctionEditorNameFromId(enum tdeFuncId_ eFuncId)
{
    return(a_stFunctionTable[eFuncId].szFunctionEditorName);
}
 
enum tdeFuncId_ fn_eFindFunctionIdFromEditorName(char *szFunction)
{
    short wFunctionEntry;
    for(wFunctionEntry=0;wFunctionEntry<fn_eGetNbFunc();wFunctionEntry++)
    {
        if (!stricmp(a_stFunctionTable[wFunctionEntry].szFunctionEditorName,szFunction))
        {
			return((enum tdeFuncId_) wFunctionEntry);
        }
    }
    return(fn_eGetNbFunc());
}
#endif /* ACTIVE_EDITOR*/
 
#if defined (AI_USE_SCRIPT)
enum tdeFuncId_ fn_eFindFunctionIdFromScriptName(char *szFunction)
{
    short wFunctionEntry;
    for(wFunctionEntry=0;wFunctionEntry<fn_eGetNbFunc();wFunctionEntry++)
    {
        if (!stricmp(a_stFunctionTable[wFunctionEntry].szFunctionScriptName,szFunction))
        {
			return((enum tdeFuncId_) wFunctionEntry);
        }
    }
    return(fn_eGetNbFunc());
}
#endif /* AI_USE_SCRIPT */
 
#if !defined(OPTIMIZED_COMMAND)
#if defined(ACTIVE_EDITOR)
void fn_vDefineFunctionEntry(enum tdeFuncId_ eFuncId,char *szFunctionEditorName,char *szFunctionEditorName_En,char *szFunctionScriptName,tdp_fn_p_stFunctionFunctionPtr p_fn_p_stFunctionFunction,char *szFunctionTypeInParam,char *szFunctionTypeOutParam)				
#else
void fn_vDefineFunctionEntry(enum tdeFuncId_ eFuncId,char *szFunctionScriptName,tdp_fn_p_stFunctionFunctionPtr p_fn_p_stFunctionFunction,char *szFunctionTypeInParam,char *szFunctionTypeOutParam)				
#endif /* ACTIVE_EDITOR*/
{
    if (
        (eFuncId<fn_eGetNbFunc())
#if defined(ACTIVE_EDITOR)
        &&(strlen(szFunctionEditorName)<=C_MAX_SIZE_FUNCTION_EDITOR_NAME)
#endif /* ACTIVE_EDITOR*/
        &&(strlen(szFunctionScriptName)<=C_MAX_SIZE_FUNCTION_SCRIPT_NAME)
        &&(strlen(szFunctionTypeInParam)<=C_MAX_FUNCTION_NB_IN_PARAM)
        &&(fn_ucCheckTypeParam(szFunctionTypeInParam))
        &&(strlen(szFunctionTypeOutParam)<=C_MAX_FUNCTION_NB_OUT_PARAM)
        &&(fn_ucCheckTypeParam(szFunctionTypeOutParam))
        )
    {
#if defined(ACTIVE_EDITOR)
        strcpy(a_stFunctionTable[eFuncId].szFunctionEditorName,szFunctionEditorName);
#endif /* ACTIVE_EDITOR*/
        strcpy(a_stFunctionTable[eFuncId].szFunctionScriptName,szFunctionScriptName);
        a_stFunctionTable[eFuncId].p_fn_p_stFunctionFunction=p_fn_p_stFunctionFunction;
        strcpy(a_stFunctionTable[eFuncId].szFunctionTypeInParam,szFunctionTypeInParam);
        strcpy(a_stFunctionTable[eFuncId].szFunctionTypeOutParam,szFunctionTypeOutParam);
    }
    else
    {
       char str[C_MAX_LENGTH_STRING];
  	   sprintf(str,"Error while defining %s function in %s\n",szFunctionScriptName,__FILE__);
       M_AIFatalErrorMsg(E_uwAIFatalNotValidFunction,str);
    }
}
#endif /* OPTIMIZED_COMMAND*/
 
#if !defined(OPTIMIZED_COMMAND)
#if defined(__DEBUG_AI__)
void fn_vInitFunctionTable(tdp_fn_p_stFunctionFunctionPtr fn_p_stDefaultFunctionFunctionPointer)
{
  short wFunctionEntry;
  for(wFunctionEntry=0;wFunctionEntry<fn_eGetNbFunc();wFunctionEntry++)
  {
	fn_vDefineFunctionEntry(M_FUNCTION_ENTRY((enum tdeFuncId_) wFunctionEntry,"","","",fn_p_stDefaultFunctionFunctionPointer,USE_NO_PARAM,USE_NO_PARAM));
	/* ULTRA OPERATOR USAGE : FALSE AS A DEFAULT*/
    fn_vSetFunctionUltraOperator((enum tdeFuncId_) wFunctionEntry,NOT_ULTRA_ABLE);
  }
}
 
void fn_vCheckFunctionTable(tdp_fn_p_stFunctionFunctionPtr fn_p_stDefaultFunctionFunctionPointer)
{
  short wFunctionEntry;
  char str[C_MAX_LENGTH_STRING];
  size_t lLen;
  size_t lMaxLengthScriptName=0;
#if defined(ACTIVE_EDITOR)
  size_t lMaxLengthEditorName=0;
#endif /* ACTIVE_EDITOR*/
  size_t lMaxLengthInParam=0;
  size_t lMaxLengthOutParam=0;
  for(wFunctionEntry=0;wFunctionEntry<fn_eGetNbFunc();wFunctionEntry++)
  {
     if (fn_p_fn_pGetFunctionFunctionPtr((enum tdeFuncId_) wFunctionEntry)==fn_p_stDefaultFunctionFunctionPointer)
	 {
  	   sprintf(str,"Error while defining %s function in %s\n",a_stFunctionTable[wFunctionEntry].szFunctionScriptName,__FILE__);
	   M_AIFatalErrorMsg(E_uwAIFatalNotValidFunction,str);
	 }
	 if ((lLen=strlen(a_stFunctionTable[wFunctionEntry].szFunctionScriptName))>lMaxLengthScriptName)
	 {
	   lMaxLengthScriptName=lLen;
	 }
#if defined(ACTIVE_EDITOR)
	 if ((lLen=strlen(a_stFunctionTable[wFunctionEntry].szFunctionEditorName))>lMaxLengthEditorName)
	 {
	   lMaxLengthEditorName=lLen;
	 }
#endif /* ACTIVE_EDITOR*/
	 if ((lLen=strlen(a_stFunctionTable[wFunctionEntry].szFunctionTypeInParam))>lMaxLengthInParam)
	 {
        lMaxLengthInParam=lLen;
	 }
	 if ((lLen=strlen(a_stFunctionTable[wFunctionEntry].szFunctionTypeOutParam))>lMaxLengthOutParam)
	 {
        lMaxLengthOutParam=lLen;
	 }
  }	
#if !defined(U64)
  {
	if (lMaxLengthScriptName<C_MAX_SIZE_FUNCTION_SCRIPT_NAME)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_SIZE_FUNCTION_SCRIPT_NAME in %s to %d\n",__FILE__,lMaxLengthScriptName);
		OutputDebugString(str);
	}
#if defined(ACTIVE_EDITOR)
	if (lMaxLengthEditorName<C_MAX_SIZE_FUNCTION_EDITOR_NAME)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_SIZE_FUNCTION_EDITOR_NAME in %s to %d\n",__FILE__,lMaxLengthEditorName);
		OutputDebugString(str);
	}
#endif /* ACTIVE_EDITOR*/
	if (lMaxLengthInParam<C_MAX_FUNCTION_NB_IN_PARAM)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_FUNCTION_NB_IN_PARAM in %s to %d\n",__FILE__,lMaxLengthInParam);
		OutputDebugString(str);
	}
	if (lMaxLengthOutParam<C_MAX_FUNCTION_NB_OUT_PARAM)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_FUNCTION_NB_OUT_PARAM in %s to %d\n",__FILE__,lMaxLengthOutParam);
		OutputDebugString(str);
	}
  }
#endif /* U64*/
}
#endif /* __DEBUG_AI__*/
 
#endif /* OPTIMIZED_COMMAND*/
