#if defined(_AI_LIB_)
	#include "AIUseCPA.h"
	
	#include "specif/AIOption.h"
	#include "AIMacros.h"	
	#include "AI_Erm.h"
	
	#include "Convert.h"
	#include "EnumCond.h"
 
	#if defined(OPTIMIZED_COMMAND) 
		#include "ProtCond.h"
	#endif
#else
	#include <string.h>
	#include "specif/AIOption.h"
	
	#include "ConvCst.h"
	#include "EnumCond.h"
	#include "ConvCond.h"
#endif /* _AI_LIB_*/
 
#define C_MAX_SIZE_CONDITION_EDITOR_NAME 50
#define C_MAX_SIZE_CONDITION_SCRIPT_NAME 40
#define C_MAX_CONDITION_NB_PARAM 4
 
typedef struct tdstConditionEntry_
{
#if defined(ACTIVE_EDITOR)
    /* Condition string used in editor*/
    char szConditionEditorName[C_MAX_SIZE_CONDITION_EDITOR_NAME+1]; 
    char szConditionEditorName_En[C_MAX_SIZE_CONDITION_EDITOR_NAME+1]; 
#endif /* ACTIVE_EDITOR*/
#if defined (AI_USE_SCRIPT)
    /* Condition string used in script*/
    char szConditionScriptName[C_MAX_SIZE_CONDITION_SCRIPT_NAME+1];
#endif /* AI_USE_SCRIPT */
#if defined(_AI_LIB_)
    /* pointer on Condition function*/
    tdp_fn_p_stConditionFunctionPtr p_fn_p_stConditionFunction;
#endif /* _AI_LIB_*/
#if defined (AI_USE_SCRIPT) || defined (U64_AIDEBUG)
	/* list of Condition parameters (taken from szAutorizedTypeParam) in parameter*/
	char szConditionTypeInParam[C_MAX_CONDITION_NB_PARAM+1];
#endif /* AI_USE_SCRIPT */
 
	unsigned char bUltraCondtionFlag;
 
#ifdef PROFILE_IA
	unsigned long ulCallCtr;
	unsigned long ulTimeCtr;
	unsigned long ulOwnTimeCtr;
#endif /* PROFILE_IA*/
} tdstConditionEntry;
 
#ifdef PROFILE_IA
extern unsigned long ulIndice;
extern unsigned long p_ulTime[255];
extern FILE *file;
#endif /* PROFILE_IA*/
 
tdstConditionEntry a_stConditionTable[eNbCond]
#if defined(OPTIMIZED_COMMAND)
=
{
	
#ifdef U64_AIDEBUG
	/* function pointers in DefCond.c, in params,ultra-able*/
	#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { d,e,f },
	#include "DefCond.h" 
}
#else /* U64_AIDEBUG*/
/* Conditions initialisation*/
#if defined (AI_USE_SCRIPT)
#if defined(_AI_LIB_)
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { b,english,c,d,e,f },
	#else
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { c,d,e,f },
	#endif /* ACTIVE_EDITOR*/
#else    
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { b,english,c,e,f },
	#else
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { c,e,f },
	#endif /* ACTIVE_EDITOR*/
#endif
	#include "DefCond.h" 
	#undef M_DEFINE_CONDITION
}
#else /* AI_USE_SCRIPT */
#if defined(_AI_LIB_)
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { b,english,d,f },
	#else
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { d,f },
	#endif /* ACTIVE_EDITOR*/
#else    
	#if defined(ACTIVE_EDITOR)
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { b,english,f },
	#else
		#define M_DEFINE_CONDITION(a,b,english,c,d,e,f) { f },
	#endif /* ACTIVE_EDITOR*/
#endif
	#include "DefCond.h" 
	#undef M_DEFINE_CONDITION
}
#endif /* AI_USE_SCRIPT */
#endif /* U64_AIDEBUG*/
#endif /* OPTIMIZED_COMMAND*/
;
 
#if ! defined(OPTIMIZED_COMMAND)
void fn_vSetConditionUltraOperator(enum tdeCondId_ eCondId,unsigned char bFlag)
{
  a_stConditionTable[eCondId].bUltraCondtionFlag=bFlag;
}
#endif /* OPTIMIZED_COMMAND*/
 
unsigned char fn_ucGetConditionUltraOperator(enum tdeCondId_ eCondId)
{
  return(a_stConditionTable[eCondId].bUltraCondtionFlag);
}
 
#if defined(_AI_LIB_)
tdp_fn_p_stConditionFunctionPtr fn_p_fn_pGetConditionFunctionPtr(enum tdeCondId_ eCondId)
{
  return(a_stConditionTable[eCondId].p_fn_p_stConditionFunction);
}
 
 
#ifdef PROFILE_IA
void fn_vCondSaveTime(unsigned long ulTime, enum tdeCondId_ eCondId)
{
	/* 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_vCondComputeTime(unsigned long ulCurrentTime, unsigned long ulTime, enum tdeCondId_ eCondId)
{
	/* compute total time for this function*/
	a_stConditionTable[eCondId].ulCallCtr++;
	a_stConditionTable[eCondId].ulTimeCtr += (ulCurrentTime - ulTime)/5;
	/* compute own time for this function*/
	ulIndice--;
	/* compute the time for this function*/
	a_stConditionTable[eCondId].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 dumpCond()
{
	int i;
	char string[250];
	
	strcpy(string,"x:\\cpa\\");
	strcat(string,fn_p_szGetLevelName());
	strcat(string,".txt");
 
	file = fopen(string,"w");
	if (file == NULL)
		return;
 
	fprintf(file,"name	Count	Time	Own Time	Rel Time	Own Rel Time\n");
	for (i=0;i<(long)eNbCond;i++)
	{
		if (a_stConditionTable[i].ulCallCtr)
		{
			fprintf(file,"%s	%i	%i	%i	%i	%i\n",
				a_stConditionTable[i].szConditionScriptName,
				a_stConditionTable[i].ulCallCtr,
				a_stConditionTable[i].ulTimeCtr,
				a_stConditionTable[i].ulOwnTimeCtr,
				a_stConditionTable[i].ulTimeCtr / a_stConditionTable[i].ulCallCtr,
				a_stConditionTable[i].ulOwnTimeCtr / a_stConditionTable[i].ulCallCtr);
		}
		else
		{
			fprintf(file,"%s	%i	%i	%i	%i	%i\n",
				a_stConditionTable[i].szConditionScriptName,
				0,
				0,
				0,
				0,
				0);
		}
 
	}
	fclose(file);
}
 
void clearCond()
{
	int i;
	for (i=0;i<(long)eNbCond;i++)
	{
		a_stConditionTable[i].ulCallCtr = 0;
		a_stConditionTable[i].ulTimeCtr = 0;
		a_stConditionTable[i].ulOwnTimeCtr = 0;
	}
	ulIndice = 0;
}
#endif /* PROFILE_IA*/
 
 
#endif /* _AI_LIB_*/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
enum tdeCondId_ fn_eGetNbCond()
{
  return(eNbCond);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#if defined(__DEBUG_AI__) || defined (U64_AIDEBUG)
char *szGetConditionTypeInParamFromId(enum tdeCondId_ eCondId)
{
    return(a_stConditionTable[eCondId].szConditionTypeInParam);
}
#endif /* __DEBUG_AI__*/
 
#if defined(ACTIVE_EDITOR)
char *szGetConditionTypeOutParamFromId(enum tdeCondId_ eCondId)
{
    eCondId=eCondId;
    return(USE_BOOLEAN_PARAM);
}
 
enum tdeCondId_ fn_eFindConditionIdFromEditorName(char *szCondition)
{
    short wConditionEntry;
    for(wConditionEntry=0;wConditionEntry<fn_eGetNbCond();wConditionEntry++)
    {
        if (!stricmp(a_stConditionTable[wConditionEntry].szConditionEditorName,szCondition))
        {
			return((enum tdeCondId_) wConditionEntry);
        }
    }
    return(fn_eGetNbCond());
}
 
char *szFindConditionEditorNameFromId(enum tdeCondId_ eCondId)
{
    return(a_stConditionTable[eCondId].szConditionEditorName);
}
#endif /* ACTIVE_EDITOR*/
 
#if defined (AI_USE_SCRIPT)
char *szFindConditionScriptNameFromId(enum tdeCondId_ eCondId)
{
    return(a_stConditionTable[eCondId].szConditionScriptName);
}
 
enum tdeCondId_ fn_eFindConditionIdFromScriptName(char *szCondition)
{
    short wConditionEntry;
    for(wConditionEntry=0;wConditionEntry<fn_eGetNbCond();wConditionEntry++)
    {
        if (!stricmp(a_stConditionTable[wConditionEntry].szConditionScriptName,szCondition))
        {
			return((enum tdeCondId_) wConditionEntry);
        }
    }
    return(fn_eGetNbCond());
}
#endif /* AI_USE_SCRIPT */
 
#if !defined(OPTIMIZED_COMMAND)
#if defined(ACTIVE_EDITOR)
void fn_vDefineConditionEntry(enum tdeCondId_ eCondId,char *szConditionEditorName,char *szConditionEditorName_En,char *szConditionScriptName,tdp_fn_p_stConditionFunctionPtr p_fn_p_stConditionFunction,char *szConditionTypeInParam)				
#else
void fn_vDefineConditionEntry(enum tdeCondId_ eCondId,char *szConditionScriptName,tdp_fn_p_stConditionFunctionPtr p_fn_p_stConditionFunction,char *szConditionTypeInParam)				
#endif /* ACTIVE_EDITOR*/
{
    if (
        (eCondId<fn_eGetNbCond())
#if defined(ACTIVE_EDITOR)
        &&(strlen(szConditionEditorName)<=C_MAX_SIZE_CONDITION_EDITOR_NAME)
#endif /* ACTIVE_EDITOR*/
        &&(strlen(szConditionScriptName)<=C_MAX_SIZE_CONDITION_SCRIPT_NAME)
   		&&(strlen(szConditionTypeInParam)<=C_MAX_CONDITION_NB_PARAM)
        &&(fn_ucCheckTypeParam(szConditionTypeInParam))
        )
    {
#if defined(ACTIVE_EDITOR)
        strcpy(a_stConditionTable[eCondId].szConditionEditorName,szConditionEditorName);
#endif /* ACTIVE_EDITOR*/
        strcpy(a_stConditionTable[eCondId].szConditionScriptName,szConditionScriptName);
        a_stConditionTable[eCondId].p_fn_p_stConditionFunction=p_fn_p_stConditionFunction;
        strcpy(a_stConditionTable[eCondId].szConditionTypeInParam,szConditionTypeInParam);
    }
    else
    {
		char str[C_MAX_LENGTH_STRING];
		sprintf(str,"Error while defining %s condition in %s\n",szConditionScriptName,__FILE__);
        M_AIFatalErrorMsg(E_uwAIFatalNotValidCondition,str);
    }
}
#endif /* OPTIMIZED_COMMAND*/
 
#if !defined(OPTIMIZED_COMMAND)
#if defined(__DEBUG_AI__)
void fn_vInitConditionTable(tdp_fn_p_stConditionFunctionPtr p_fn_p_stDefaultConditionFunctionPointer)
{
  short wConditionEntry;
  for(wConditionEntry=0;wConditionEntry<fn_eGetNbCond();wConditionEntry++)
  {
	fn_vDefineConditionEntry(M_CONDITION_ENTRY((enum tdeCondId_) wConditionEntry,"","","",p_fn_p_stDefaultConditionFunctionPointer,USE_NO_PARAM));
	/* ULTRA OPERATOR USAGE : FALSE AS A DEFAULT*/
    fn_vSetConditionUltraOperator((enum tdeCondId_) wConditionEntry,NOT_ULTRA_ABLE);
  }
}
 
void fn_vCheckConditionTable(tdp_fn_p_stConditionFunctionPtr p_fn_p_stDefaultConditionFunctionPointer)
{
  short wConditionEntry;
  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;
  for(wConditionEntry=0;wConditionEntry<fn_eGetNbCond();wConditionEntry++)
  {
     if (fn_p_fn_pGetConditionFunctionPtr((enum tdeCondId_) wConditionEntry)==p_fn_p_stDefaultConditionFunctionPointer)
	 {
	   sprintf(str,"Error while defining %s condition in %s\n",a_stConditionTable[wConditionEntry].szConditionScriptName,__FILE__);
 	   M_AIFatalErrorMsg(E_uwAIFatalNotValidCondition,str);
	 }
	 if ((lLen=strlen(a_stConditionTable[wConditionEntry].szConditionScriptName))>lMaxLengthScriptName)
	 {
	   lMaxLengthScriptName=lLen;
	 }
#if defined(ACTIVE_EDITOR)
	 if ((lLen=strlen(a_stConditionTable[wConditionEntry].szConditionEditorName))>lMaxLengthEditorName)
	 {
	   lMaxLengthEditorName=lLen;
	 }
#endif /* ACTIVE_EDITOR*/
	 if ((lLen=strlen(a_stConditionTable[wConditionEntry].szConditionTypeInParam))>lMaxLengthInParam)
	 {
        lMaxLengthInParam=lLen;
	 }
  }	
#if !defined(U64)
  {
	if (lMaxLengthScriptName<C_MAX_SIZE_CONDITION_SCRIPT_NAME)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_SIZE_CONDITION_SCRIPT_NAME in %s to %d\n",__FILE__,lMaxLengthScriptName);
		OutputDebugString(str);
	}
#if defined(ACTIVE_EDITOR)
	if (lMaxLengthEditorName<C_MAX_SIZE_CONDITION_EDITOR_NAME)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_SIZE_CONDITION_EDITOR_NAME in %s to %d\n",__FILE__,lMaxLengthEditorName);
		OutputDebugString(str);
	}
#endif /* ACTIVE_EDITOR*/
	if (lMaxLengthInParam<C_MAX_CONDITION_NB_PARAM)
	{
		sprintf(str,"*** AI : You should reduce C_MAX_CONDITION_NB_PARAM in %s to %d\n",__FILE__,lMaxLengthInParam);
		OutputDebugString(str);
	}
  }
#endif /* U64*/
}
#endif /* __DEBUG_AI__*/
 
#endif /* OPTIMIZED_COMMAND*/
