/*---------------------------------------------------------------------------*/
/* Convert.c : Convert Id <-> string (mainly usefull for editor)*/
/* creation  : 17/01/1997 (Olivier Couvreur)*/
/* modify    : 29/01/1997 (Fabien Morales)   Vertex param*/
/* modify    : 31/01/1997 (Olivier Couvreur) lint verification*/
/* modify    : 12/02/1997 (Fabien Morales)   Module & DsgVarId param types*/
/* modify    : 19/02/1997 (Olivier Couvreur) types checking functions (for editor)*/
/* modify    : 26/02/1997 (Olivier Couvreur) new functions for editor type checking  */
/* modify    : 11/03/1997 (Olivier Couvreur) functions for editor transfered from several modules*/
/* modify    : 25/03/1997 (Olivier Couvreur) ACTIVE_EDITOR support*/
/* modify    : 21/05/1997 (Olivier Couvreur) HitPoints support*/
/* modify    : 27/05/1997 (Olivier Couvreur) ZDD + ZDE stuff*/
/* modify    : 27/06/1997 (Olivier Couvreur) Operators,Functions,Procedures,Conditions use functions pointers !*/
/*---------------------------------------------------------------------------*/
#include "AIUseCPA.h"
 
#include "specif/AIOption.h"
#include "AIMacros.h"	
#include "AI_Erm.h"
 
#include "GsParam.h"
 
/* so that exported functions are effectively exported !*/
#include "Convert.h"
 
#if defined(ACTIVE_EDITOR)
#include "specif/ActTable.h"
#endif /* ACTIVE_EDITOR*/
 
#if !defined(OPTIMIZED_COMMAND)
/**********************************************************************/
/* list of all allowed char for defining types*/
char szAutorizedTypeParam[]= \
  C_LONG_PARAM     \
  C_REAL_PARAM     \
  C_PERSO_PARAM    \
  C_VECTOR_PARAM   \
  C_MODULE_PARAM   \
  C_WAYPOINT_PARAM \
  C_LIST_PARAM     \
  C_BOOLEAN_PARAM  \
  C_ACTION_PARAM   \
  C_ZDE_PERSO_PARAM      \
  C_ZDD_PERSO_PARAM      \
  C_ZDM_PERSO_PARAM      \
  C_ZDE_MODULE_PARAM      \
  C_ZDD_MODULE_PARAM      \
  C_BUTTON_PARAM   \
  C_STRING_PARAM   \
  C_MAP_PARAM      \
  C_MASK_PARAM     \
  C_MASK_PARAM     \
  C_SURFACE_PARAM \
  C_LIPSYNCHRO_PARAM \
  C_OBJECTTABLE_PARAM \
  C_SECTOR_PARAM \
  C_ENV_PARAM \
  C_FAMILY_PARAM \
  C_COMPORT_PARAM \
  C_MY_REFLEX_COMPORT_PARAM \
  C_MY_INTELL_COMPORT_PARAM \
  C_REFLEX_COMPORT_PARAM    \
  C_INTELL_COMPORT_PARAM  \
  C_SOUNDEVENT_PARAM \
  C_ALWAYS_PARAM \
  C_POWER_PARAM \
  C_PERSO_PARAM_ONLY \
  C_FONT_PARAM \
  C_TEXT_PARAM \
  C_PARTICLEGENERATOR_PARAM \
  C_GMT_PARAM \
  C_MODEL_PARAM \
  C_ARRAY_PARAM \
  C_SUPEROBJECT_PARAM \
/*ANNECY CT 22/01/98 {*/
  C_LIGHT_PARAM \
/*ENANNECY CT}*/
/*ANNECY Bart 02/02/98 {*/
  C_CAPS_PARAM \
  C_GRAPH_PARAM \
/*ENDANNECY Bart }*/
  C_ZDR_PERSO_PARAM
;
 
/* return a pointer on a string containing all valid chars to define types*/
char *fn_szGetAutorizedTypeParam(void)
{
    return (&szAutorizedTypeParam[0]);
}
 
/* check if a string contains only valid chars*/
unsigned char fn_ucCheckTypeParam(char *szTypeParam)
{
    unsigned char Ok=0;
    
    if (strlen(szTypeParam)<=C_MAX_NB_PARAM)
    {
        unsigned char i;
        Ok=1;
        for (i=0;((i<strlen(szTypeParam)) && (Ok==1));i++)
        {
			Ok=(unsigned char) (strchr(fn_szGetAutorizedTypeParam(),toupper(szTypeParam[i]))!=NULL);
        }
    }
 
    if (!Ok)
    {
        M_AIFatalError(E_uwAIFatalWrongParamAction);
    }
    return(Ok);
}
#endif /* !OPTIMIZED_COMMAND*/
 
 
/**********************************************************************/
 
 
#if defined(ACTIVE_EDITOR)
/* check if two types are compatible*/
/* c1=c2*/
/* c1=toupper(c2)*/
/* l|l*/
/* L|l,L*/
/* x|x,l*/
/* X|l,L,x,X*/
/* Perso_Only=Perso*/
/* My_XX=XX (where XX is rul or rfx)*/
ACP_tdxBool fn_bIsCompatibleType(char c1,char c2)
{
 if ((c1==c2) || (c1==(char) toupper(c2)) )
 {
    return(TRUE);
 }
 else if 
	 (
	   (toupper(c1)==STRING1_TO_CHAR(C_REAL_PARAM)) &&
	   (toupper(c2)==STRING1_TO_CHAR(C_LONG_PARAM))
	 )
 {
	 if( 
		 (c1==STRING1_TO_CHAR(C_REAL_PARAM)) || 
		 (c2==STRING1_TO_CHAR(C_ADDR_LONG_PARAM))
	   )
	 {
		 return TRUE;
	 }
 }
 else if 
	 (
	   (c1==STRING1_TO_CHAR(C_PERSO_PARAM_ONLY)) &&
	   (c2==STRING1_TO_CHAR(C_PERSO_PARAM))
	 )
 {
	 return TRUE;
 }
 else if 
	 (
	  (c1==STRING1_TO_CHAR(C_REFLEX_COMPORT_PARAM)) &&
	  (c2==STRING1_TO_CHAR(C_MY_REFLEX_COMPORT_PARAM))
	 )
 {
	 return TRUE;
 }
 else if 
	 (
	  (c1==STRING1_TO_CHAR(C_INTELL_COMPORT_PARAM)) &&
	  (c2==STRING1_TO_CHAR(C_MY_INTELL_COMPORT_PARAM))
	 )
 {
	 return TRUE;
 }
 else if 
	 ( 
	   (toupper(c2)==STRING1_TO_CHAR(C_COMPORT_PARAM)) &&
		 (
				(c1==STRING1_TO_CHAR(C_INTELL_COMPORT_PARAM)) ||
				(c1==STRING1_TO_CHAR(C_MY_INTELL_COMPORT_PARAM)) ||
				(c1==STRING1_TO_CHAR(C_REFLEX_COMPORT_PARAM)) ||
				(c1==STRING1_TO_CHAR(C_MY_REFLEX_COMPORT_PARAM)) 
		 )
	 )
	{
		return(TRUE);
	}
  else if
  (
	   (toupper(c1)==STRING1_TO_CHAR(C_STRING_PARAM)) &&
	   (toupper(c2)==STRING1_TO_CHAR(C_TEXT_PARAM))
  )
  {
    return TRUE;
  }
  else if
  (
	   (toupper(c1)==STRING1_TO_CHAR(C_TEXT_PARAM)) &&
	   (toupper(c2)==STRING1_TO_CHAR(C_STRING_PARAM))
  )
  {
    return TRUE;
  }
 
 return(FALSE);
}
 
/* check if two types are compatible for affectation*/
ACP_tdxBool fn_bIsCompatibleTypeForAffect(char c1,char c2)
{
	if (c1==tolower(c2))
	{
		return TRUE;
	}
	else if 
		(
		    (c1==STRING1_TO_CHAR(C_ADDR_REAL_PARAM))
		  &&(toupper(c2)==STRING1_TO_CHAR(C_LONG_PARAM))
		)
	{
		return TRUE;
	}
 
	return FALSE;
}
 
 
/* check if two string type are compatible*/
ACP_tdxBool fn_bIsCompatibleStringType(char *szType1,char *szType2)
{
  size_t l=strlen(szType1);
 
  if (l!=strlen(szType2)) 
  {
     return(FALSE);
  }
  else
  {
    size_t i;
    for (i=0;i<l;i++)
    {
      if (!fn_bIsCompatibleType(szType1[i],szType2[i]))
        return(FALSE);  
    }
  }
  return(TRUE);
}
 
ACP_tdxBool fn_bIsCompatibleStringTypeForAffect(char *szType1,char *szType2)
{
  size_t l=strlen(szType1);
 
  if (l!=strlen(szType2)) 
  {
     return(FALSE);
  }
  else
  {
    size_t i;
    for (i=0;i<l;i++)
    {
      if (!fn_bIsCompatibleTypeForAffect(szType1[i],szType2[i]))
        return(FALSE);  
    }
  }
  return(TRUE);
}
 
/*/
// get compatible type (c1 left, c2 right)
char fn_cGetCompatibleType(char c1,char c2)
{
  if (c1==c2)
  {
    return(c1);
  }
  else
  {
    if(
        (c1==(char) (*((char *) C_REAL_PARAM)) )
      ||(c2==(char) (*((char *) C_REAL_PARAM)) )
      )
    {
        return( (char) (*((char *) C_REAL_PARAM)) );
    }
    return(c1);
  }
}
 
// idem for a string
#define C_MAX_SIZE_INFOPARAM_NAME 50
char g_szStringType[C_MAX_SIZE_INFOPARAM_NAME+1];
char *fn_szGetCompatibleStringType(char *szType1,char *szType2)
{
  size_t l=strlen(szType1);
  g_szStringType[0]=0;
  g_szStringType[l]=0;
  if (l==strlen(szType2) )
  {
    size_t i;
    for (i=0;i<l;i++)
    {
      g_szStringType[i]=fn_cGetCompatibleType(szType1[i],szType2[i]);
    }
  }
  return(&g_szStringType[0]);
}
*/
#endif /* ACTIVE_EDITOR*/
 
/**********************************************************************/
#if defined(ACTIVE_EDITOR)
unsigned char fn_ucGetDefaultActionTableNbEntries()
{
  return(DEFAULT_ACTION_TABLE_NB_ENTRIES);
}
 
unsigned char fn_ucGetMinActionTableNbEntries()
{
  return(MIN_ACTION_TABLE_NB_ENTRIES);
}
 
unsigned char fn_ucGetMaxActionTableNbEntries()
{
  return(MAX_ACTION_TABLE_NB_ENTRIES);
}
#endif /* ACTIVE_EDITOR*/
 
/**********************************************************************/
#if defined(ACTIVE_EDITOR)
/*
typedef struct tdstTypeConversionTableEntry_
{
  enum tdeVariableType_ eType;
  char szStringType[2];
} tdstTypeConversionTableEntry;
 
static tdstTypeConversionTableEntry a_stTypeConversiontable[]=
{
    {E_vt_None,             C_NO_PARAM},
    {E_vt_Boolean,          C_ADDR_BOOLEAN_PARAM},
    {E_vt__128To127,        C_ADDR_LONG_PARAM},
    {E_vt_0To255,           C_ADDR_LONG_PARAM},
    {E_vt__32768To32767,    C_ADDR_LONG_PARAM},
    {E_vt_0To65535,         C_ADDR_LONG_PARAM},
    {E_vt_Integer,          C_ADDR_LONG_PARAM},
    {E_vt_PositiveInteger,  C_ADDR_LONG_PARAM},
    {E_vt_Float,            C_ADDR_REAL_PARAM},
    {E_vt_WayPoint,         C_ADDR_WAYPOINT_PARAM},
    {E_vt_Way,              C_ADDR_LONG_PARAM},        
    {E_vt_Perso,            C_ADDR_PERSO_PARAM},
    {E_vt_List,             C_ADDR_LIST_PARAM},
    //{E_vt_1bit,             C_ADDR_LONG_PARAM},
    //{E_vt_2bit,             C_ADDR_LONG_PARAM},
    //{E_vt_3bit,             C_ADDR_LONG_PARAM},
    //{E_vt_4bit,             C_ADDR_LONG_PARAM}, 
    //{E_vt_5bit,             C_ADDR_LONG_PARAM},
    //{E_vt_6bit,             C_ADDR_LONG_PARAM},
    //{E_vt_7bit,             C_ADDR_LONG_PARAM},
    {E_vt_MetaActionReturn, C_ADDR_LONG_PARAM},
    {E_vt_Vector,           C_ADDR_VECTOR_PARAM},
    {E_vt_Module,           C_ADDR_LONG_PARAM},
    {E_vt_DsgVarId,         C_ADDR_LONG_PARAM},
    {E_vt_Button,           C_BUTTON_PARAM},
	{E_vt_Mask,             C_MASK_PARAM},
	{E_vt_Text,				C_ADDR_TEXT_PARAM},
	{E_vt_GameMaterial,		C_ADDR_GMT_PARAM},
	{E_vt_Action,			C_ADDR_ACTION_PARAM},
	//{E_vt_Comport,			C_ADDR_COMPORT_PARAM}
	{E_vt_Array,			C_ADDR_ARRAY_PARAM}
 
};
 
 
int fn_TypeConversionSize()
{
	return(sizeof(a_stTypeConversiontable)/sizeof(a_stTypeConversiontable[0]));
}
 
char *fn_szFindStringTypeFromVariableType(tdeVariableType eType)
{
  int i;
  for(i=0;i<fn_TypeConversionSize();i++)
  {
    if (a_stTypeConversiontable[i].eType==eType)
    {
      return(a_stTypeConversiontable[i].szStringType);
    }
  }
  return(USE_NO_PARAM);
}
*/
 
ACP_tdxBool IsADDRType(char cType) 
{
  return((ACP_tdxBool) islower((int) cType) );
}
 
char fn_cConvertToADDRType(char cType)
{
  return((char) tolower((int) cType) );
}
 
char fn_cConvertFromADDRType(char cType)
{
  return((char) toupper((int) cType) );
}
 
#endif /* ACTIVE_EDITOR*/
 
/**********************************************************************************************************************/
/* for multilanguage support : old aiinfo.c */
#if defined(ACTIVE_EDITOR) 
 
/* C'est pour virer Proc,Cond,Action etc...*/
static char *fn_szConvertString(char *str)
{
  char *ptr;
  if ((ptr=strchr(str,'_'))!=NULL)
  {
	ptr++;
  }
  else
  {
    ptr=str;
  }
  return(ptr);
}
 
/* C'est pour Xavier qui veut que les noms des conditions <> <= >= etc soit les mêmes en anglais et en francais !!!*/
static char *fn_szConvertConditionString(char *str1,char *str2)
{
	if (!isalpha(str2[0]))
	{
		return(fn_szConvertString(str2));
	}
	return(fn_szConvertString(str1));
}
 
/* C'est pour Xavier qui veut qu'il y ait bijection entre les mots clés et les types des variables designers*/
/* C'est pour Xavier qui veut que les noms des types des variables commencent par une lettre !!!*/
static char TempChar[200];
static char *fn_szConvertDsgVarString(char *str1,char *str2)
{
	if (!strnicmp(str2,"Type",4))
	{
		sprintf(&TempChar[0],"%sType",str1);
		return(fn_szConvertString(&TempChar[0]));
	}
	else if ((isdigit(str1[0])) || (str1[0]=='_') )
	{
		sprintf(&TempChar[0],"Integer%s",str1);
		return(&TempChar[0]);	
	}
	return(str1);
}
 
void fn_vWriteAILangue()
{
	FILE *fp;
 
  if ((fp=fopen(AI_MULTI_LANGUAGE_SCRIPT,"wt"))!=NULL)
	{
		#define  M_BAR "------------------------------------------------------------------"
 
		#define  M_WRITE_ALL_COMMAND_START(title,nb)	fprintf(fp,";%s\n;%s\n;%s\n{%s:(%d)\n",M_BAR,title,M_BAR,title,nb);
		#define  M_WRITE_ALL_COMMAND_END()  			fprintf(fp,"}\n\n");
 
		#define M_WRITE_ONE_COMMAND(title,key,fr,gb) \
			fprintf(fp,"\t{%s:%s\n",title,key); \
			fprintf(fp,"\t\t%s(%s)\n",AI_FRENCH,fr); \
			fprintf(fp,"\t\t%s(%s)\n",AI_ENGLISH,gb); \
			fprintf(fp,"\t}\n\n");
 
		/* MetaActions*/
		#define M_WRITE_ALL_METAACTIONS(nb)			 M_WRITE_ALL_COMMAND_START(AI_ALL_METAACTIONS,nb)	
		#define M_WRITE_ONE_METAACTION(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_METAACTION,key,fr,gb)
		
		M_WRITE_ALL_METAACTIONS(fn_eGetNbMetaAction())
		/*#define M_DEFINE_METAACTION(a,b,c,d,e,f)	 M_WRITE_ONE_METAACTION(c,b,fn_szConvertString(c))*/
        #define M_DEFINE_METAACTION(a,b,english,c,d,e,f)	 M_WRITE_ONE_METAACTION(c,b,english)
		#include "DefAct.h"
		#undef M_DEFINE_METAACTION
		M_WRITE_ALL_COMMAND_END()
 
		/* Procedures*/
		#define M_WRITE_ALL_PROCEDURES(nb)			 M_WRITE_ALL_COMMAND_START(AI_ALL_PROCEDURES,nb)	
		#define M_WRITE_ONE_PROCEDURE(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_PROCEDURE,key,fr,gb)
 
		M_WRITE_ALL_PROCEDURES(fn_eGetNbProcedure())
		/*#define M_DEFINE_PROCEDURE(a,b,c,d,e,f)		 M_WRITE_ONE_PROCEDURE(c,b,fn_szConvertString(c))*/
        #define M_DEFINE_PROCEDURE(a,b,english,c,d,e,f)		 M_WRITE_ONE_PROCEDURE(c,b,english)
		#include "DefProc.h"
		#undef M_DEFINE_PROCEDURE
		M_WRITE_ALL_COMMAND_END()
 
		/* Functions*/
		#define M_WRITE_ALL_FUNCTIONS(nb)			 M_WRITE_ALL_COMMAND_START(AI_ALL_FUNCTIONS,nb)	
		#define M_WRITE_ONE_FUNCTION(key,fr,gb)	     M_WRITE_ONE_COMMAND(AI_ONE_FUNCTION,key,fr,gb)
 
		M_WRITE_ALL_FUNCTIONS(fn_eGetNbFunc())
		/*#define M_DEFINE_FUNCTION(a,b,c,d,e,f,g)	 M_WRITE_ONE_FUNCTION(c,b,fn_szConvertString(c))*/
        #define M_DEFINE_FUNCTION(a,b,english,c,d,e,f,g)	 M_WRITE_ONE_FUNCTION(c,b,english)
		#include "DefFunc.h"
		#undef M_DEFINE_FUNCTION
		M_WRITE_ALL_COMMAND_END()
 
		/* Conditions*/
		#define M_WRITE_ALL_CONDITIONS(nb)			 M_WRITE_ALL_COMMAND_START(AI_ALL_CONDITIONS,nb)	
		#define M_WRITE_ONE_CONDITION(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_CONDITION,key,fr,gb)
 
		M_WRITE_ALL_CONDITIONS(fn_eGetNbCond())
		/*#define M_DEFINE_CONDITION(a,b,c,d,e,f)		 M_WRITE_ONE_CONDITION(c,b,fn_szConvertConditionString(c,b))*/
        #define M_DEFINE_CONDITION(a,b,english,c,d,e,f)		 M_WRITE_ONE_CONDITION(c,b,english)
		#include "DefCond.h"
		#undef M_DEFINE_CONDITION
		M_WRITE_ALL_COMMAND_END()
 
		/* KeyWords*/
		#define M_WRITE_ALL_KEYWORDS(nb)			 M_WRITE_ALL_COMMAND_START(AI_ALL_KEYWORDS,nb)	
		#define M_WRITE_ONE_KEYWORD(key,fr,gb)	     M_WRITE_ONE_COMMAND(AI_ONE_KEYWORD,key,fr,gb)
 
		M_WRITE_ALL_KEYWORDS(fn_eGetNbKeyWord())
		/*#define M_DEFINE_KEYWORD(a,b,c,d,e,f)		 M_WRITE_ONE_KEYWORD(c,b,fn_szConvertString(c))*/
        #define M_DEFINE_KEYWORD(a,b,english,c,d,e,f)		 M_WRITE_ONE_KEYWORD(c,b,english)
		#include "DefKey.h"
		#undef M_DEFINE_KEYWORD
		M_WRITE_ALL_COMMAND_END()
 
		/* Fields*/
		#define M_WRITE_ALL_FIELDS(nb)				 M_WRITE_ALL_COMMAND_START(AI_ALL_FIELDS,nb)	
		#define M_WRITE_ONE_FIELD(key,fr,gb)		 M_WRITE_ONE_COMMAND(AI_ONE_FIELD,key,fr,gb)
 
		M_WRITE_ALL_FIELDS(fn_eGetNbField())
		/*#define M_DEFINE_FIELD(a,b,c,d,e,f)			 M_WRITE_ONE_FIELD(c,b,fn_szConvertString(c))*/
        #define M_DEFINE_FIELD(a,b,english,c,d,e,f)			 M_WRITE_ONE_FIELD(c,b,english)
		#include "DefFild.h"
		#undef M_DEFINE_FIELD
		M_WRITE_ALL_COMMAND_END()
 
		/* Designer variables*/
		#define M_WRITE_ALL_DSGVAR_TYPES(nb)         M_WRITE_ALL_COMMAND_START(AI_ALL_DSGVAR_TYPES,nb)	
		#define M_WRITE_ONE_DSGVAR_TYPE(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_DSGVAR_TYPE,key,fr,gb)
 
		M_WRITE_ALL_DSGVAR_TYPES(fn_eGetNbDsgVarType())
		#define M_DEFINE_DSGVAR_TYPE(a,b,c,d,e,f,g,h,i,j)    M_WRITE_ONE_DSGVAR_TYPE(c,b,fn_szConvertDsgVarString(c,b))
		#include "DefVar.h"
		#undef M_DEFINE_DSGVAR_TYPE
		M_WRITE_ALL_COMMAND_END()
 
		/* SAI Type*/
		#define M_WRITE_ALL_DSGVAR_SAVE_TYPES(nb)        M_WRITE_ALL_COMMAND_START(AI_ALL_DSGVAR_SAVE_TYPES,nb)	
		#define M_WRITE_ONE_DSGVAR_SAVE_TYPE(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_DSGVAR_SAVE_TYPE,key,fr,gb)
 
		M_WRITE_ALL_DSGVAR_SAVE_TYPES(fn_ucGetSaveTypeNumber())
		#define M_DEFINE_DSGVAR_SAVE_TYPE(a,b,c)		 M_WRITE_ONE_DSGVAR_SAVE_TYPE(b,a,b)
		#include "DefSAI.h"
		#undef M_DEFINE_DSGVAR_SAVE_TYPE
		M_WRITE_ALL_COMMAND_END()
 
		/* OTI Type*/
		#define M_WRITE_ALL_DSGVAR_INIT_TYPES(nb)        M_WRITE_ALL_COMMAND_START(AI_ALL_DSGVAR_INIT_TYPES,nb)	
		#define M_WRITE_ONE_DSGVAR_INIT_TYPE(key,fr,gb)	 M_WRITE_ONE_COMMAND(AI_ONE_DSGVAR_INIT_TYPE,key,fr,gb)
 
		M_WRITE_ALL_DSGVAR_INIT_TYPES(fn_ucGetInitTypeNumber())
		#define M_DEFINE_DSGVAR_INIT_TYPE(a,b,c)		 M_WRITE_ONE_DSGVAR_INIT_TYPE(b,a,b)
		#include "DefOTI.h"
		#undef M_DEFINE_DSGVAR_INIT_TYPE
		M_WRITE_ALL_COMMAND_END()
 
		fclose(fp);
	}
}
#endif /* ACTIVE_EDITOR*/
