/*---------------------------------------------------------------------------*/
/* Strintel.cpp : Definition des fonctions utiles pour manipuler les structures*/
/*              de donnée du module d'intelligence.*/
/* auhor  : Olivier Didelot  08/08/96*/
/* modify : Olivier Didelot  21/10/96*/
/* .....???????????*/
/* modify : Olivier Couvreur 04/02/97  Lint 0 warnings excepted those specified at the end of AIMacros.h*/
/* modify : Olivier Couvreur 14/02/97  Creation of Deallocation functions*/
/* modify : Olivier Couvreur 18/02/97  CPA_EXPORT + new functions for editor*/
/* modify : Olivier Couvreur 05/03/97  Model can be gathered between several perso + Alloc and Free functions rewritten + Lint*/
/* modify : Olivier Couvreur 11/03/97  .dec can be gathered between several perso + Alloc and Free functions rewritten + Lint*/
/* modify : Olivier Couvreur 25/03/97  NbNode is not an unsigned char anymore + NoComportInit support + minor changes for editor*/
/* modify : Olivier Couvreur 29/04/97  Init Comport and .IAI support + DsgVar Perso*/
/*---------------------------------------------------------------------------*/
#include "AIUseCPA.h"
 
#include "specif/AIOption.h"
#include "AIMacros.h"
#include "AI_Mmg.h"
#include "AI_Erm.h"
 
#include "AI_Struc.h"
#include "AI_Proto.h"
#include "DsgMem.h"
#include "specif/ActTable.h"
#include "StrIntel.h"
 
#include "Intell.h"
#include "Convert.h"
 
#if defined(ACTIVE_AIDEBUG)   /*** DR ***/
#include "AID_Erm.h"
#include "specif/AID_Trac.h"
#endif /*ACTIVE_AIDEBUG*/
 
void fn_vOnAIErm_M_UpdateLastError(unsigned short FatalErrorNum,char *szMsg)
{
	Erm_M_UpdateLastError(AI, C_ucErmDefaultChannel, FatalErrorNum, C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, szMsg);
}
 
void fn_vErrorOnAIAlloc()
{
	M_AIIfErrorUpdateFatalError(Mmg, E_uwAIFatalFailedAllocationMem);
}
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vErrorOnAIFree()
{
	M_AIIfErrorUpdateWarningError (Mmg, E_uwAIWarningCanNotFree, C_ucErmOpenInfoWindow);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
void fn_vReinitComport(tdstIntelligence *p_stIntelligence)
{
	(void) fn_ucChangeComportIntell(p_stIntelligence, (tdstComport *)M_GetNoComportInit(p_stIntelligence));	
	M_GetPrevComport(p_stIntelligence) = M_GetCurrentComport(p_stIntelligence);
}
 
unsigned char fn_ucChangeComportIntell(tdstIntelligence *p_stIntelligence, tdstComport *p_stComport)
{
#if defined(__DEBUG_AI__)
	long lComport=(p_stComport-M_GetComport(p_stIntelligence));
    if ( (lComport<0) || (lComport>=(long) M_GetNbComport(p_stIntelligence)) )
	{
		M_AIWarningErrorAndNeverStopForDebug(E_uwAIWarningComportOfAnotherPerso, C_ucErmOpenInfoWindow,M_GetNameComport(p_stComport));
#if defined(ACTIVE_EDITOR) && defined(ACTIVE_EXEC_CHECKING)
		fn_vSetStopAIEngineFlag();
#endif
		return(FALSE);
	}
	else
#endif
    {
		M_GetPrevComport(p_stIntelligence) = M_GetCurrentComport(p_stIntelligence);
		M_GetCurrentComport(p_stIntelligence) = p_stComport;
		fn_vInitActionTable(p_stIntelligence);
		/* Update new schedule.*/
		if (M_GetComportSchedule(p_stComport) == NULL)
			M_GetCurrentSchedule(p_stIntelligence) = NULL;
		else 
			M_GetCurrentSchedule(p_stIntelligence) = M_GetTreeComportSchedule(p_stComport);
		
#if defined(ACTIVE_AIDEBUG)   /*** DR ***/
		AIDebug_M_SetCurrentComport( fn_p_stGetCurrentMind(), p_stComport );
#endif /*ACTIVE_AIDEBUG*/
	}
	return(TRUE);
}
 
unsigned char fn_ucGetTreeNbParam(tdstNodeInterpret *p_stTree)
{
    unsigned char ucNbParam=0;
    unsigned char ucDepth  = M_DepthInterpret(p_stTree-1);
    
    while (    (M_DepthInterpret(p_stTree) > ucDepth) 
        && (!M_IsTheEndOfTree(p_stTree)))
    {
        if (M_DepthInterpret(p_stTree)==ucDepth+1)
        {
            ucNbParam++;
        }
        p_stTree++;
    }
    return(ucNbParam);
} 
 
#if defined(OPTIMIZE_NB_NODES)

unsigned short fn_uwGetNbNodeInterpret(struct tdstTreeInterpret_ *p_stTreeInterpret)
{
	tdstNodeInterpret *p_stTree,*p_stTreeInit;
	p_stTree=p_stTreeInit=M_GetNodeInterpret(p_stTreeInterpret);
	if (p_stTree==NULL) return(0);
	while(!M_IsTheEndOfTree(p_stTree)) p_stTree++;
	p_stTree++;
	return ((unsigned short) (p_stTree-p_stTreeInit) );
}

#endif /* OPTIMIZE_NB_NODES*/
 
/**************************************************************/
/* AI init functions used by allocations                      */
/**************************************************************/
 
//#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void fn_vInitMacro(tdstMacro *p_stMacro)
{
	M_GetMacroInitTree(p_stMacro) = NULL;
	M_GetMacroCurrentTree(p_stMacro) = M_GetMacroInitTree(p_stMacro);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vInitListOfMacro(tdstListOfMacro *p_stListOfMacro, unsigned char ucNbMacro)
{
	M_GetMacro(p_stListOfMacro) = NULL;
	M_GetNbMacro(p_stListOfMacro) = ucNbMacro;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
void fn_vInitComport(tdstComport *p_stComport)
{
    M_GetComportNbRules(p_stComport)=0;
    M_GetComportRules(p_stComport) = NULL;
    M_GetComportSchedule(p_stComport) = NULL;
}
 
void fn_vInitRule(tdstTreeInterpret *p_stRule)
{
	M_SetNbNodeInterpret(p_stRule,0);
    M_GetNodeInterpret(p_stRule)=NULL;
}
 
void fn_vInitNodeInterpret(tdstNodeInterpret *p_stNode, long lVal, 
                           tdeTypeInterpret eTypeInterpret, unsigned char ucDepth)
{
    M_ClearNodeInterpret(p_stNode);
    M_DepthInterpret(p_stNode)=ucDepth;
    M_lValueInterpret(p_stNode)=lVal;
	M_SetTypeInterpret(p_stNode,eTypeInterpret);
#if defined(OPTIMIZE_SWAP_PARAMETER)
   	M_NodeToSkip(p_stNode)=0;
#endif
 
#if defined( ACTIVE_AIDEBUG )
		M_SetBreakPoint(p_stNode,FALSE);
#endif /*ACTIVE_AIDEBUG*/
}
 
#ifdef ACTIVE_EDITOR
/**************************************************************/
/* AI desallocation functions :                               */
/**************************************************************/
char CFast_g_bNoNeedForFreeNodeContent = 0;
/*********************************************************************************************/
void fn_vFreeNodeContent(tdstNodeInterpret *p_stNode)
{
	/* is there node ?*/
	if (p_stNode!=NULL)
	{
		if (M_GetTypeInterpret(p_stNode) == E_ti_String)
		{
			if (M_szStringInterpret(p_stNode) != NULL)
			{
				M_AIFree(M_szStringInterpret(p_stNode));
			}
		}
		else if (M_GetTypeInterpret(p_stNode) == E_ti_ConstantVector)
		{
			if (M_p_stVectorInterpret(p_stNode) != NULL)
			{
				M_AIFree(M_p_stVectorInterpret(p_stNode));
			}
		}
		fn_vInitNodeInterpret(p_stNode,0,(tdeTypeInterpret) 0,0);
	}
}
 
/* free an array of nodes */
void fn_vFreeNodeInterpretAll(tdstNodeInterpret **p_p_stNode,unsigned short uwNbNodes)
{
	/* is there node ?*/
	if (*p_p_stNode!=NULL)
	{
		unsigned short i;
		/* free nodes content*/
    if ( !CFast_g_bNoNeedForFreeNodeContent ) {
		  for (i=0;i<uwNbNodes;i++)
		  {
			  fn_vFreeNodeContent(&((*p_p_stNode)[i]));
		  }
    }		
		/* free node itself*/
		M_AIFree(*p_p_stNode);
		*p_p_stNode=NULL;
	}
}
 
 
/*********************************************************************************************/
/* free rule content*/
void fn_vFreeRuleContent(tdstTreeInterpret *p_stRules)
{    
	/* is there rule ?*/
	if (p_stRules!=NULL)
	{
		/* free nodes*/
		fn_vFreeNodeInterpretAll(&M_GetNodeInterpret(p_stRules),M_GetNbNodeInterpret(p_stRules));
		fn_vInitRule(p_stRules);
	}
}
 
/* free a rule and all its nodes*/
void fn_vFreeRuleAll(tdstTreeInterpret **p_p_stRules,unsigned char ucNbRules)
{
	/* is there rule ?*/
	if (*p_p_stRules!=NULL)
	{
		unsigned char i;
		/* free rule content (nodes)*/
		for(i=0;i<ucNbRules;i++)
		{
			fn_vFreeRuleContent(&((*p_p_stRules)[i]));
		}
		
		/* free rule itself*/
		M_AIFree(*p_p_stRules);
		*p_p_stRules=NULL;  
	}
}
 
/*********************************************************************************************/
/* free schedule content*/
void fn_vFreeScheduleContent(tdstTreeInterpret *p_stSchedule)
{    
	/* is there schedule ?*/
	if (p_stSchedule!=NULL)
	{
		/* free nodes*/
		fn_vFreeNodeInterpretAll(&M_GetNodeInterpret(p_stSchedule),M_GetNbNodeInterpret(p_stSchedule));
		fn_vInitRule(p_stSchedule);
	}
}
 
void fn_vFreeScheduleAll(tdstTreeInterpret **p_p_stSchedule)
{
	/* is there schedule ?*/
	if (*p_p_stSchedule!=NULL)
	{
		/* free Schedule content (nodes)*/
		fn_vFreeScheduleContent(*p_p_stSchedule); 
		
		/* free schedule itself*/
		M_AIFree(*p_p_stSchedule);
		*p_p_stSchedule=NULL;
	}
}
 
/*********************************************************************************************/
/* free a comport content, all its rules, and its schedule*/
void fn_vFreeComportContent(tdstComport *p_stComport)
{
	if (p_stComport!=NULL)
	{
		/* free rules */
		fn_vFreeRuleAll(&M_GetComportRules(p_stComport),M_GetComportNbRules(p_stComport));
		
		/* free schedule */
		fn_vFreeScheduleAll(&M_GetComportSchedule(p_stComport));
		
		/* clear */
		fn_vInitComport(p_stComport);
	}
}
 
void fn_vFreeComportAll(tdstComport **p_p_stComport,unsigned long ulNbComport)
{
	/* is there comport ?*/
	if (*p_p_stComport!=NULL)
	{
		unsigned long i;
		/* free comport content*/
		for (i=0;i<ulNbComport;i++)
		{
			fn_vFreeComportContent(&((*p_p_stComport)[i]));
		}
		
		/* free comport itself*/
		M_AIFree(*p_p_stComport); 
		*p_p_stComport=NULL;
	}
}
/*********************************************************************************************/
void fn_vFreeSubroutineContent ( tdstMacro * p_stSubroutine )
{
	if ( p_stSubroutine != NULL )
	{
		fn_vFreeRuleAll ( & M_GetMacroInitTree ( p_stSubroutine ) , 1 ) ;
	}
}
 
/* free a list of Subroutines */
void fn_vFreeSubroutinesAll(tdstListOfMacro **p_stListSubroutines)
{
	if ( * p_stListSubroutines != NULL )
	{
		int i ;
		/* free the subroutines */
		for ( i = 0 ; i < M_GetNbMacro ( ( * p_stListSubroutines ) ) ; i ++ )
			fn_vFreeSubroutineContent ( M_GetMacroN ( * p_stListSubroutines , i) ) ;
		/* and free the list itself */
		M_AIFree ( * p_stListSubroutines ) ; 
		* p_stListSubroutines = NULL ;
	}
}

/*********************************************************************************************/
 
void fn_vFreeScriptAIContent(tdstScriptAI *p_stScriptAI)
{
	/* is there ScriptAI ?*/
	if (p_stScriptAI!=NULL)
	{
		fn_vFreeComportAll(&M_GetScriptAIComport(p_stScriptAI),M_GetScriptAINbComport(p_stScriptAI));
	}
}
 
/* free a ScriptAI and all its comport*/
void fn_vFreeScriptAIAll(tdstScriptAI **p_p_stScriptAI)
{
	/* is there ScriptAI ?*/
	if (*p_p_stScriptAI!=NULL)
	{
		/* free ScriptAI content (comport)*/
		fn_vFreeScriptAIContent(*p_p_stScriptAI);
		
		/* free ScriptAI itself*/
		M_AIFree(*p_p_stScriptAI); 
		*p_p_stScriptAI=NULL;
	}
}
/*********************************************************************************************/
/* free model content*/
void fn_vFreeAIModelContent(AI_tdstAIModel *p_stAIModel)
{
    /* is there Model ?*/
	if (p_stAIModel!=NULL)
	{
		/* free the intelligence */
		fn_vFreeScriptAIAll(&AI_M_p_stGetIntelligenceScriptAIModel(p_stAIModel));
		/* free the reflexes */
		fn_vFreeScriptAIAll(&AI_M_p_stGetReflexScriptAIModel(p_stAIModel));
		/* free the subroutines */
		fn_vFreeSubroutinesAll(&AI_M_p_stGetListOfMacroAIModel(p_stAIModel)) ;
	}
}
 
/* free a model and its 2  ScriptAI*/
void fn_vFreeAIModelAll(AI_tdstAIModel **p_p_stAIModel)
{
	/* is there model ?*/
	if (*p_p_stAIModel!=NULL)
	{
		/* free ScriptAI content (comport)*/
		fn_vFreeAIModelContent(*p_p_stAIModel);
		
    /* CFast, is there a RefTree*/
    if ((*p_p_stAIModel)->CFast_dst_RefTreeArray != NULL) {
      fn_vFreeNodeInterpretAll(&(*p_p_stAIModel)->CFast_dst_RefTreeArray, (*p_p_stAIModel)->uwNbNode) ;
    }
    /* CFast, is there a ParamTree*/
    if ((*p_p_stAIModel)->CFast_dst_ParamArray != NULL) {
      CFast_g_bNoNeedForFreeNodeContent = 1 ;
      fn_vFreeNodeInterpretAll(&(*p_p_stAIModel)->CFast_dst_ParamArray, (*p_p_stAIModel)->uwNbParamNode) ;
      CFast_g_bNoNeedForFreeNodeContent = 0 ;
    }
 
		/* free ScriptAI itself*/
		M_AIFree(*p_p_stAIModel); 
		*p_p_stAIModel=NULL;
	}
}
/*********************************************************************************************/
/* free action table entry content*/
void fn_vFreeActionTableEntryContent(tdstActionTableEntry *p_stActionTableEntry)
{  
	/* are there actiontable entries ?*/
	if (p_stActionTableEntry!=NULL)
	{
		/* nothing to do   */
	}
}
 
void fn_vFreeActionTableEntryAll(tdstActionTableEntry **p_p_stActionTableEntry,unsigned ucActionTableNbEntry)
{
    /* is there comport ?*/
	if (*p_p_stActionTableEntry!=NULL)
	{
		unsigned char i;
		/* free comport content*/
		for (i=0;i<ucActionTableNbEntry;i++)
		{
			fn_vFreeActionTableEntryContent(&((*p_p_stActionTableEntry)[i]));
		}
		
		/* free comport itself*/
		M_AIFree(*p_p_stActionTableEntry); 
		*p_p_stActionTableEntry=NULL;
	}
}
 
/***************************************************************************/
void fn_vFreeActionTableContent(tdstActionTable *p_stActionTable)
{
	/* is there action table ?*/
	if (p_stActionTable!=NULL)
	{
		fn_vFreeActionTableEntryAll(&M_TableEntry(p_stActionTable),M_TableNbEntries(p_stActionTable));
	}
}
 
/* free action table from intelligence*/
void fn_vFreeActionTableAll(tdstActionTable **p_p_stActionTable)
{
	/* is there action table ?*/
	if (*p_p_stActionTable!=NULL)
	{
		/* free action table content*/
		fn_vFreeActionTableContent(*p_p_stActionTable);
		
		/* free action table itself*/
		M_AIFree(*p_p_stActionTable);
		*p_p_stActionTable=NULL;
	}
}
/*****************************************************************************/
/* free intelligence content*/
void fn_vFreeIntelligenceContent(tdstIntelligence *p_stIntelligence)
{
    if (p_stIntelligence!=NULL)
    {
		fn_vFreeActionTableAll(&M_ActionTable(p_stIntelligence));
		M_GetCurrentSchedule(p_stIntelligence)=NULL;
		M_GetCurrentComport(p_stIntelligence)=NULL;
		M_GetPrevComport(p_stIntelligence)=NULL;
		p_stIntelligence->p_p_stScriptAI=NULL;
    }
}
 
/* free intelligence*/
void fn_vFreeIntelligenceAll(tdstIntelligence **p_p_stIntelligence)
{
	/* is there intelligence ?*/
	if (*p_p_stIntelligence!=NULL)
	{
		fn_vFreeIntelligenceContent(*p_p_stIntelligence);
		
		/* free intelligence itself*/
		M_AIFree(*p_p_stIntelligence);
		*p_p_stIntelligence=NULL;
	}
}
 
/* free reflex*/
void fn_vFreeReflexAll(tdstIntelligence **p_p_stReflex)
{
	fn_vFreeIntelligenceAll(p_p_stReflex);
}
 
/********************************************************************************************/
/* free mind content*/
void fn_vFreeMindContent(AI_tdstMind *p_stMind)
{
	if (p_stMind!=NULL)
	{
		/* before model*/
		/* free designer variable from mind*/
		fn_vFreeDesignerVariable(p_stMind);
		
		/* free AI model*/
		/* fn_vFreeAIModelAll(&AI_M_p_stGetAIModel(p_stMind));*/
		
		/* free intelligence from mind*/
		fn_vFreeIntelligenceAll(&AI_M_p_stGetIntelligence(p_stMind));
		
		/* free reflex from mind*/
		fn_vFreeReflexAll(&AI_M_p_stGetReflex(p_stMind));
	}
}
 
/* free mind all*/
void fn_vFreeMindAll(AI_tdstMind **p_p_stMind)
{
	if (*p_p_stMind!=NULL)
	{
		/* free mind content*/
		fn_vFreeMindContent(*p_p_stMind);
		
		/* free mind itself*/
		M_AIFree(*p_p_stMind);
		*p_p_stMind=NULL;
	}
}
#endif /* ACTIVE_EDITOR*/
 
/**************************************************************/
/* AI allocation functions :                                  */
/**************************************************************/
 
/**************************************************************/
/* AI allocation functions for macros :                       */
/**************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdstListOfMacro *fn_p_stAllocListOfMacro(unsigned char ucNbMacro)
{
    tdstListOfMacro *p_stListOfMacro=NULL;
/*XB980430*/
/*	tdstMacro *p_stMacro=NULL;*/
/*End XB*/
    
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeMacro , NULL);
	M_AIAlloc(p_stListOfMacro, tdstListOfMacro *, sizeof(tdstListOfMacro));
	fn_vInitListOfMacro(p_stListOfMacro, ucNbMacro);
 
    if (ucNbMacro!=0)
    {
		unsigned long i;
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeMacro , NULL);
		M_AIAlloc(M_GetMacro(p_stListOfMacro), tdstMacro *, ucNbMacro*sizeof(tdstMacro));
		
		for (i=0;i<ucNbMacro;i++)
		{
			fn_vInitMacro(M_GetMacroN(p_stListOfMacro, i));
		}
    }
    return(p_stListOfMacro);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vAllocListOfMacro(struct AI_tdstMind_ *p_stMind, unsigned char ucNbMacro)
{
	AI_M_SetListOfMacroAIModel(AI_M_p_stGetAIModel(p_stMind), fn_p_stAllocListOfMacro(ucNbMacro));
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vAllocNodeMacro(tdstMacro *p_stMacro, unsigned short uwNbNode)
{
    MMG_fn_vBeginMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeNodeMacro , NULL);
 
	M_AIAlloc(M_GetMacroInitTree(p_stMacro), tdstTreeInterpret *, sizeof(tdstTreeInterpret));
	M_GetNodeInterpret(M_GetMacroInitTree(p_stMacro)) = fn_p_stAllocNodeInterpret(uwNbNode);;
	M_SetNbNodeInterpret(M_GetMacroInitTree(p_stMacro),uwNbNode);
 
	M_AIAlloc(M_GetMacroCurrentTree(p_stMacro), tdstTreeInterpret *, sizeof(tdstTreeInterpret))
	M_GetNodeInterpret(M_GetMacroCurrentTree(p_stMacro)) = M_GetNodeInterpret(M_GetMacroInitTree(p_stMacro));
 
    MMG_fn_vEndMemoryInfo ();
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
tdstComport *fn_p_stAllocComport(unsigned long ulNbComport)
{
    tdstComport *p_stComport=NULL;
    
    if (ulNbComport!=0)
    {
		unsigned long i;
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeComport , NULL);
		M_AIAlloc(p_stComport,tdstComport *,ulNbComport*sizeof(tdstComport));
		
		for (i=0;i<ulNbComport;i++)
		{
			fn_vInitComport(&(p_stComport[i]));
//XB 05/08/99
#ifndef D_THROW_CFAST_AI
//End XB
			p_stComport[i].CFast_lMyIndex = i;
//XB 05/08/99
#endif /* D_THROW_CFAST_AI */
//End XB
		}
    }
    return(p_stComport);
}
 
tdstTreeInterpret *fn_p_stAllocRule(unsigned char ucNbRules)
{
	tdstTreeInterpret *p_stRules=NULL;
	if (ucNbRules!=0)
	{
		unsigned char i;
		
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeRule , NULL);
		M_AIAlloc(p_stRules, tdstTreeInterpret *,ucNbRules*sizeof(tdstTreeInterpret));
		for (i=0;i<ucNbRules;i++)
		{
			fn_vInitRule(&(p_stRules[i]));
		}
	}
	return(p_stRules);
}
 
tdstNodeInterpret *fn_p_stAllocNodeInterpret(unsigned short uwNbNode)
{
    tdstNodeInterpret *p_stNodeInterpret=NULL;
	
    if (uwNbNode!=0)
    {
        unsigned short i;
		
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeNodeInterpret , NULL);
        M_AIAlloc(p_stNodeInterpret, tdstNodeInterpret *, uwNbNode*sizeof(tdstNodeInterpret));
		
        for (i=0;i<uwNbNode;i++)
		{
			fn_vInitNodeInterpret(&(p_stNodeInterpret[i]), 0, (tdeTypeInterpret)0, 0);
		}
    }
    return(p_stNodeInterpret);
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdstTreeInterpret *fn_p_stAllocSchedule(unsigned short uwNbNode)
{
	tdstTreeInterpret *p_stSchedule=NULL;
	
	/* allocate 1 rule*/
	p_stSchedule=fn_p_stAllocRule(1);
	
	/* allocate node*/
	M_GetNodeInterpret(p_stSchedule)=fn_p_stAllocNodeInterpret(uwNbNode);
	M_SetNbNodeInterpret(p_stSchedule,uwNbNode);
	return(p_stSchedule);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/* XB 20/05/1999 */
#ifndef D_THROW_COMPLEX_ACTION_TABLE
tdstActionTableEntry *fn_p_stAllocActionTableEntry(unsigned char ucActionTableNbEntry)
{
	tdstActionTableEntry *p_stActionTableEntry=NULL;
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeActionTableEntry , NULL);
	M_AIAlloc(p_stActionTableEntry,tdstActionTableEntry *,ucActionTableNbEntry*sizeof(tdstActionTableEntry));   
	return(p_stActionTableEntry);
}
#endif /* D_THROW_COMPLEX_ACTION_TABLE */
/* End XB 20/05/1999 */
 
extern unsigned char g_ucUseDefaultActionReturn;
extern unsigned char g_ucNewActionReturn;
 
tdstActionTable *fn_p_stAllocActionTable(unsigned char ucActionTableNbEntry)
{
	tdstActionTableEntry *p_stEntry;
	tdstActionTable *p_stActionTable=NULL;
/* XB 20/05/1999 */
#ifndef D_THROW_COMPLEX_ACTION_TABLE
	unsigned char ucNEntry;
#endif /* D_THROW_COMPLEX_ACTION_TABLE */
/* End XB 20/05/1999 */
 
/* XB 21/05/1999 */
#ifdef D_THROW_COMPLEX_ACTION_TABLE
	if(ucActionTableNbEntry!=0)
	{
#endif /* D_THROW_COMPLEX_ACTION_TABLE */
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeActionTable , NULL);
	M_AIAlloc(p_stActionTable,tdstActionTable *,sizeof(tdstActionTable));   
#ifdef D_THROW_COMPLEX_ACTION_TABLE
	}
	else
	{
		return NULL;
	}
#endif /* D_THROW_COMPLEX_ACTION_TABLE */
/* End XB 21/05/1999 */

/* XB 20/05/1999 */
#ifndef D_THROW_COMPLEX_ACTION_TABLE

	M_TableNbEntries(p_stActionTable)=ucActionTableNbEntry;
	M_TableEntry(p_stActionTable)=fn_p_stAllocActionTableEntry(ucActionTableNbEntry);
	/*//*/
/* ANNECY MT - 17/09/98 { initializes the table*/
    p_stActionTable->ucNbEntriesUsed=0;
	p_stActionTable->ucCurrentEntry=0;
	/**/
    for (ucNEntry=0,p_stEntry=M_TableEntry(p_stActionTable);ucNEntry<ucActionTableNbEntry;ucNEntry++,p_stEntry++)
    {
		p_stEntry -> bUseDefaultActionReturn	= g_ucUseDefaultActionReturn;
		p_stEntry -> ucNewActionReturn			= g_ucNewActionReturn;
    }
/* END ANNECY MT }*/
	/*//*/

#else /* D_THROW_COMPLEX_ACTION_TABLE */

	p_stEntry = &(p_stActionTable->stEntry);

	p_stEntry->bUseDefaultActionReturn=g_ucUseDefaultActionReturn;
	p_stEntry->ucNewActionReturn=g_ucNewActionReturn;

#endif /* D_THROW_COMPLEX_ACTION_TABLE */
/* End XB 20/05/1999 */

	return(p_stActionTable);
}
 
AI_tdstAIModel *fn_p_stAllocAIModel()
{
  AI_tdstAIModel *p_stAIModel=NULL;
  MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeModel , NULL);
  M_AIAlloc(p_stAIModel, AI_tdstAIModel *, sizeof(AI_tdstAIModel));
  AI_M_SetIntelligenceScriptAIModel(p_stAIModel, NULL);
  AI_M_SetReflexScriptAIModel(p_stAIModel, NULL);
  AI_M_SetDsgVarAIModel(p_stAIModel,NULL);
#ifndef U64
  AI_M_SetListOfMacroAIModel(p_stAIModel,NULL);
#endif /* U64 */
  AI_M_SetSecondPassDone(p_stAIModel,0); 
  
//XB 05/08/99
#ifndef D_THROW_CFAST_AI
//End XB
  p_stAIModel->CFast_dst_RefTreeArray = NULL; /* BART ?*/
  p_stAIModel->uwNbNode = 0; /* BART ?*/
  p_stAIModel->CFast_dst_ParamArray = NULL ;
  p_stAIModel->uwNbParamNode = 0 ;
  p_stAIModel->CFast_lFunction = 0;
//XB 05/08/99
#endif /* D_THROW_CFAST_AI */
//End XB

  return(p_stAIModel);
}
 
tdstScriptAI *fn_p_stAllocScriptAI(unsigned char ucActionTableNbEntry)
{
	tdstScriptAI *p_stScriptAI=NULL;
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeScriptAI , NULL);
	M_AIAlloc(p_stScriptAI, tdstScriptAI *, sizeof(tdstScriptAI));
	M_GetScriptAINbComport(p_stScriptAI)=0;
	M_GetScriptAIComport(p_stScriptAI)=NULL;
	M_GetScriptAIActionTableNbEntry(p_stScriptAI)=ucActionTableNbEntry;
	M_GetScriptAINoComportInitDefault(p_stScriptAI)=0;
	return(p_stScriptAI);
}
 
tdstIntelligence *fn_p_stAllocIntelligence(tdstScriptAI **p_p_stScriptAI,unsigned char ucActionTableNbEntry)
{
	tdstIntelligence *p_stIntelligence=NULL;
	
	if (M_GetScriptAINbComport(*p_p_stScriptAI)!=0)
	{
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeIntelligence , NULL);
		M_AIAlloc(p_stIntelligence, tdstIntelligence *, sizeof(tdstIntelligence));
		
		/* link intelligence to its model*/
		p_stIntelligence->p_p_stScriptAI=p_p_stScriptAI; /* faire macro*/
		
		/* initialise with default comport*/
		M_GetNoComportInit(p_stIntelligence)=M_GetScriptAINoComportInitDefault(*p_p_stScriptAI);
		
		M_ActionTable(p_stIntelligence)=fn_p_stAllocActionTable(ucActionTableNbEntry);
/* ANNECY MT - 18/09/98 { fn_p_stAllocActionTable() initializes the table now*/
		/*fn_vInitActionTable(p_stIntelligence);*/
/* END ANNECY MT }*/
	}
	
	return(p_stIntelligence);
}
 
 
 
#ifdef ACTIVE_EDITOR
/*********************/
/* DESIGNER VARAIBLE */
/*********************/
 
void fn_vFreeDsgVarInfoContent(tdstDsgVarInfo *p_stDsgVarInfo)
{
	if (p_stDsgVarInfo!=NULL)
	{
		p_stDsgVarInfo->ulOffsetInDsgMem=0;
		p_stDsgVarInfo->eDsgVarTypeId=eNbDsgVarType;
	}
}
 
void fn_vFreeDsgVarInfoAll(tdstDsgVarInfo **p_p_stDsgVarInfo,unsigned char ucNbDsgVar)
{
	if (*p_p_stDsgVarInfo!=NULL)
	{
		unsigned char i; 
		/* free DsgVarContent*/
		for(i=0;i<ucNbDsgVar;i++)
		{
			fn_vFreeDsgVarInfoContent(&((*p_p_stDsgVarInfo)[i]));
		}
		
		/* free DsgVar itself*/
		M_AIFree(*p_p_stDsgVarInfo);
		*p_p_stDsgVarInfo=NULL;
	}
}
 
void fn_vFreeDsgVarContent(tdstDsgVar *p_stDsgVar)
{
	if (p_stDsgVar!=NULL)
	{
		fn_vFreeDsgMemBuffer(&(p_stDsgVar->p_cDsgMemDefaultInit));
		fn_vFreeDsgVarInfoAll(&(p_stDsgVar->p_stDsgVarInfo),p_stDsgVar->ucNbDsgVar);
		p_stDsgVar->ulSize=0;
		p_stDsgVar->ucNbDsgVar=0;
	}
}
 
void fn_vFreeDsgVarAll(tdstDsgVar **p_p_stDsgVar)
{
	if (*p_p_stDsgVar!=NULL)
	{
		fn_vFreeDsgVarContent(*p_p_stDsgVar);
		
		M_AIFree(*p_p_stDsgVar);
		*p_p_stDsgVar=NULL;
	}
}
 
void fn_vFreeDsgMemBuffer(char **p_p_cDsgMemBuffer)
{
	if (*p_p_cDsgMemBuffer!=NULL)
	{
		M_AIFree(*p_p_cDsgMemBuffer);
		*p_p_cDsgMemBuffer=NULL;
	}
}
 
void fn_vFreeDsgMemContent(tdstDsgMem *p_stDsgMem)
{
	if (p_stDsgMem!=NULL)
	{
		fn_vFreeDsgMemBuffer(&(p_stDsgMem->p_cDsgMemBufferInit));
		fn_vFreeDsgMemBuffer(&(p_stDsgMem->p_cDsgMemBuffer));
	}
}
 
void fn_vFreeDsgMemAll(tdstDsgMem **p_p_stDsgMem)
{
	if (*p_p_stDsgMem!=NULL)
	{
		fn_vFreeDsgMemContent(*p_p_stDsgMem);
		
		M_AIFree(*p_p_stDsgMem);
		*p_p_stDsgMem=NULL;
	}
}
 
void fn_vFreeDesignerVariable(AI_tdstMind *p_stMind)
{
	if (M_GetDsgMem(p_stMind)!=NULL)
	{
		if(M_GetDsgVar(p_stMind)!=NULL)
		{
			fn_vFreeDsgVarAll(&M_GetDsgVar(p_stMind));
		}
		
		fn_vFreeDsgMemAll(&M_GetDsgMem(p_stMind));
	}
}
 
#endif /* ACTIVE_EDITOR*/
 
tdstDsgVarInfo *fn_p_stAllocDsgVarInfo(unsigned char ucNbDsgVar)
{
	unsigned char i;
	tdstDsgVarInfo *p_stDsgVarInfo=NULL;
	
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeDsgVarInfo , NULL);
	M_AIAlloc(p_stDsgVarInfo,tdstDsgVarInfo *,ucNbDsgVar*sizeof(tdstDsgVarInfo));   
	for (i=0;i<ucNbDsgVar;i++)
	{
		p_stDsgVarInfo[i].ulOffsetInDsgMem=0;
		p_stDsgVarInfo[i].eDsgVarTypeId=eNbDsgVarType;
		p_stDsgVarInfo[i].eSaveType=fn_eFindSaveTypeFromSaveTypeNumber(fn_ucDefaultSaveTypeNumber());
		p_stDsgVarInfo[i].eInitType=fn_eFindInitTypeFromInitTypeNumber(fn_ucDefaultInitTypeNumber());
	}
	return(p_stDsgVarInfo);
}
 
tdstDsgVar *fn_p_stAllocDsgVar(unsigned long ulSize,unsigned char ucNbDsgVar)
{
	tdstDsgVar *p_stDsgVar=NULL;
	
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeDsgVar , NULL);
	M_AIAlloc(p_stDsgVar,tdstDsgVar *,sizeof(tdstDsgVar));   
	p_stDsgVar->p_cDsgMemDefaultInit=fn_p_cAllocDsgMemBuffer(ulSize);
	p_stDsgVar->p_stDsgVarInfo=fn_p_stAllocDsgVarInfo(ucNbDsgVar);
	p_stDsgVar->ulSize=ulSize;
	p_stDsgVar->ucNbDsgVar=ucNbDsgVar;
	return(p_stDsgVar);
}
 
 
char *fn_p_cAllocDsgMemBuffer(unsigned long ulSize)
{
	char *p_cDsgMemBuffer=NULL;
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeDsgMemBuffer , NULL);
	M_AIAlloc(p_cDsgMemBuffer,char *,ulSize*sizeof(char));   
/*	memset(p_cDsgMemBuffer,0,ulSize); AR9904 Already done in the alloc function */
	return(p_cDsgMemBuffer);
}
 
tdstDsgMem *fn_p_stAllocDsgMem(tdstDsgVar **p_p_stDsgVar,unsigned long ulSize)
{
	tdstDsgMem *p_stDsgMem=NULL;
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeDsgMem , NULL);
	M_AIAlloc(p_stDsgMem,tdstDsgMem*,sizeof(tdstDsgMem));   
	
	/* link DsgMem to its model : dont forget it*/
	p_stDsgMem->p_p_stDsgVar=p_p_stDsgVar;
	
	/* no specific values by default */
	p_stDsgMem->p_cDsgMemBufferInit=NULL; 
	
	/* zone which stores current DsgVarValues : Set during 2nd pass*/
	p_stDsgMem->p_cDsgMemBuffer=fn_p_cAllocDsgMemBuffer(ulSize);
	return(p_stDsgMem);
}
 
void fn_vAllocVariableDesigner(AI_tdstMind *p_stMind,unsigned long ulSizeDsgMem,unsigned char ucNbDsgVar)
{
    if ( (ulSizeDsgMem!=0) && (ucNbDsgVar!=0) )
    {
		/* note : instruction order is important*/
		/* obliged to use these macros instead of those defined in dsgmem.h*/
		/* because at this moment p_p_stDsgVar in tdstDsgMem (in tdstMind) is not allocated yet*/
		AI_M_p_stGetDsgVar(p_stMind)=fn_p_stAllocDsgVar(ulSizeDsgMem,ucNbDsgVar);
		
		AI_M_p_stGetDsgMem(p_stMind)=fn_p_stAllocDsgMem(&AI_M_p_stGetDsgVar(p_stMind),ulSizeDsgMem);
    }
    else
    {
		AI_M_p_stGetDsgVar(p_stMind)=NULL;
		AI_M_p_stGetDsgMem(p_stMind)=NULL;
    }
}
 
void fn_vCreateNodeString(tdstNodeInterpret *p_stNode, char *szString)
{
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeNodeString , NULL);
	/* don't forget +1 because of the terminal zero*/
	M_AIAlloc(M_szStringInterpret(p_stNode), char *, (strlen(szString)+1)*sizeof(char));
    strcpy(M_szStringInterpret(p_stNode), szString);
	M_SetTypeInterpret(p_stNode,E_ti_String);
}
 
void fn_vCreateNodeVector(struct tdstNodeInterpret_ *p_stNode)
{
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeNodeVector , NULL);
	M_AIAlloc(M_p_stVectorInterpret(p_stNode), MTH3D_tdstVector *, sizeof(MTH3D_tdstVector));
	MTH3D_M_vNullVector(M_p_stVectorInterpret(p_stNode));
	M_SetTypeInterpret(p_stNode,E_ti_ConstantVector);
}
