// EdIRIRD.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRIRD.hpp"

#include "EdIRBvw.hpp"
#include "EdIRBtVw.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRBeha.hpp"
#include "EdIRFrm.hpp"
#include "EdIRBis.hpp"
#include "EdIRDVVw.hpp"
#include "EdIRDVLs.hpp"
#include "EdIRStrg.hpp"
#include "EdIR2Eng.hpp"
#include "EdIRGlob.hpp"
#include "EdIRCtl.hpp"
#include "EdIRDat.hpp"
#include "EdIRQuer.hpp"
#include "EdIRWaW.hpp"

//BEGIN ROMTEAM Cristi Petrescu 98-05-
#include "edirrvw.hpp"
//END ROMTEAM Cristi Petrescu 98-05-

#include "EdIRIAWd.hpp"

#include "ai_intf.hpp"

#include "Ctl.h"

#include "_Ainterf.hpp"

#include "EDACModl.hpp"

#include "TFA.h"

#include "DPT.h"

#include "incAI.h"

#include "x:\cpa\main\inc\_EditID.h"

#include "_Actors.hpp"

// BEGIN CPA2 Cristi Petrescu 98-03-
#include "EdIRMacL.hpp"
// END CPA2 Cristi Petrescu 98-03-

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// BEGIN CPA2 Cristi Petrescu 98-03-
extern CPA_EdIR_MacroList							g_clMacroList;
// END CPA2 Cristi Petrescu 98-03-

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_Brain
/////////////////////////////////////////////////////////////////////////////

static long	gs_lDummyData;

/****************************************************************************/
CPA_EdIR_Brain::CPA_EdIR_Brain(CPA_Actor *pclModel)
{
	m_pclModel									=pclModel;
	m_bHasBeenModified					=FALSE;
	m_bOldApplicationIsOK				=TRUE;
	m_bDsgVarControlsAreCreated	=FALSE;
	m_bMustReInitBehaviour			=FALSE;
	m_bAlreadyProcessed					=FALSE;
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_bMustBuildDebugInfo = FALSE;
	//END ROMTEAM Cristi Petrescu 98-05-
	//BEGIN ROMTEAM Cristi Petrescu 98-06-
	m_bShowPrivateVariable = FALSE;
	//END ROMTEAM Cristi Petrescu 98-06-
//ANNECY CB
	m_bFirstShow = TRUE;
	m_bCanQuickSave = TRUE;
//END ANNECY
}

/****************************************************************************/
CPA_EdIR_Brain::CPA_EdIR_Brain(CPA_Actor *pclModel,CPA_EdIR_Brain *pclBrain)
	:m_clListOfIntelligenceBehaviours(pclModel,&(pclBrain->m_clListOfIntelligenceBehaviours)),
	 m_clListOfReflexBehaviours(pclModel,&(pclBrain->m_clListOfReflexBehaviours)),
	 m_clListOfDeclarations(pclModel,&(pclBrain->m_clListOfDeclarations)),
	 // BEGIN CPA2 Cristi Petrescu 98-03-
	 m_clListOfMacros(pclModel,&(pclBrain->m_clListOfMacros)),
	 // END CPA2 Cristi Petrescu 98-03-
	 // BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	 m_clListOfSubrs(pclModel,&(pclBrain->m_clListOfSubrs))
	 // END ROMTEAM CPA2 Cristi Petrescu 98-04-
{
	m_pclModel									=pclModel;
	m_bHasBeenModified					=FALSE;
	m_bOldApplicationIsOK				=TRUE;
	m_bDsgVarControlsAreCreated	=FALSE;
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_bMustBuildDebugInfo = FALSE;
	//END ROMTEAM Cristi Petrescu 98-05-
	//BEGIN ROMTEAM Cristi Petrescu 98-06-
	m_bShowPrivateVariable = FALSE;
	//END ROMTEAM Cristi Petrescu 98-06-

	// BEGIN CPA2 Cristi Petrescu 98-03-
	// sets the macro list
	// cleanup the macro list
	// g_clMacroList.m_fn_vDestroyAndRemoveAllEntries();
	// END CPA2 Cristi Petrescu 98-03-

	//Renames all behaviour
	POSITION	pos=m_clListOfIntelligenceBehaviours.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfIntelligenceBehaviours.GetNext(pos);
		CString									csBehaviourName=pclMainBehaviour->GetName();

		m_fn_vReplaceBehaviourNames(csBehaviourName);

		pclMainBehaviour->fn_eRename(m_fn_csGetModelName()+"_"+csBehaviourName);
	}

	pos=m_clListOfReflexBehaviours.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfReflexBehaviours.GetNext(pos);
		CString									csBehaviourName=pclMainBehaviour->GetName();

		m_fn_vReplaceBehaviourNames(csBehaviourName);

		pclMainBehaviour->fn_eRename(m_fn_csGetModelName()+"_"+csBehaviourName);
	}

	// Notifies new IA
	m_fn_vNotify();

	m_fn_vSetModified(TRUE);
}

/****************************************************************************/
CPA_EdIR_Brain::~CPA_EdIR_Brain()
{
	m_clListOfIntelligenceBehaviours.m_fn_vDeleteAllMainBehaviours();
	m_clListOfReflexBehaviours.m_fn_vDeleteAllMainBehaviours();
	m_clListOfDeclarations.m_fn_vDeleteAllMainBehaviours();
	// BEGIN CPA2 Cristi Petrescu 98-03-
	m_clListOfMacros.m_fn_vDeleteAllMainBehaviours();
	// END CPA2 Cristi Petrescu 98-03-
	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	m_clListOfSubrs.m_fn_vDeleteAllMainBehaviours();
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vReplaceBehaviourNames(CString csBehaviourName)
{
	CString	csFamilyName=m_pclModel->m_fn_csGetFamilyName();

	POSITION	pos=m_clListOfIntelligenceBehaviours.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfIntelligenceBehaviours.GetNext(pos);
		CString									csBehaviourText=pclMainBehaviour->m_csText;

		pclMainBehaviour->m_csText=fn_csReplaceString(csBehaviourText,"\""+csBehaviourName+"\"","\""+m_fn_csGetModelName()+"_"+csBehaviourName+"\"");
	}

	pos=m_clListOfReflexBehaviours.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfReflexBehaviours.GetNext(pos);
		CString									csBehaviourText=pclMainBehaviour->m_csText;

		pclMainBehaviour->m_csText=fn_csReplaceString(csBehaviourText,"\""+csBehaviourName+"\"","\""+m_fn_csGetModelName()+"_"+csBehaviourName+"\"");
	}
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateBehavioursList(CPA_EdIR_DiagView *pclDiagView,tdeEditState tdeSate)
{
	CPA_EdIR_MainBehaviourList	*pclBehavioursList;
	BOOL						bDeclarationMode;

	switch(tdeSate)
	{
		case EditIntell:
			pclBehavioursList=&m_clListOfIntelligenceBehaviours;
			bDeclarationMode=FALSE;
			break;

		case EditReflex:
			pclBehavioursList=&m_clListOfReflexBehaviours;
			bDeclarationMode=FALSE;
			break;

		case EditDecl:
			pclBehavioursList=&m_clListOfDeclarations;
			bDeclarationMode=TRUE;
			break;

		// BEGIN CPA2 Cristi Petrescu 98-03-
		case EditMacro:
			pclBehavioursList=&m_clListOfMacros;
			bDeclarationMode=TRUE;
			break;
		// END CPA2 Cristi Petrescu 98-03-

		// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
		case EditSubr:
			pclBehavioursList=&m_clListOfSubrs;
			bDeclarationMode=FALSE;
			break;
		// END ROMTEAM CPA2 Cristi Petrescu 98-04-

		default:
			pclBehavioursList=NULL;
			break;
	}

	if(pclBehavioursList)
	{
		POSITION	pos=pclBehavioursList->GetHeadPosition();
//ANNECY CB
		pclDiagView->m_bSpecialCB = TRUE;
//END
		while(pos!=NULL)
			pclBehavioursList->GetNext(pos)->m_fn_vUpdateBehaviour(pclDiagView,bDeclarationMode);
//ANNECY CB
		pclDiagView->m_bSpecialCB = FALSE;
//END
	}
}

/****************************************************************************/
CString	CPA_EdIR_Brain::m_fn_csGetModelName()
{
	return m_pclModel->GetName();
}

/****************************************************************************/
CPA_Actor	*CPA_EdIR_Brain::m_fn_pclGetModel()
{
	return m_pclModel;
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vCreateInitControlsForBehaviour(AI_tdstAIModel *pstAIModel)
{
	CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
	CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

	CPA_List<CPA_BaseObject>	clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);
																											
	//For Intelligence
	if(m_clListOfIntelligenceBehaviours.GetCount()!=0)
	{
		tdstScriptAI	*pstScriptAIIntell=AI_M_p_stGetIntelligenceScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIIntell!=NULL)
		{
			//Adds entry in control (IA Behaviour Name)
			CTL_Editor_EnumDescriptor	*pclEnumList=new CTL_Editor_EnumDescriptor(g_c_csInitialIABehaviour,4);
			POSITION									pos=m_clListOfIntelligenceBehaviours.GetHeadPosition();
			long											lIndex=0;
			while(pos!=NULL)
			{
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfIntelligenceBehaviours.GetNext(pos);

				pclEnumList->m_fn_pclAddElement(pclMainBehaviour->GetName(),lIndex);

				lIndex++;
			}
			
			//Adds a control for initial intelligence
			CTL_Editor_EnumControl		*pclEnum;
			pclEnum=new CTL_Editor_EnumControl(READ_WRITE,pclListOfDesignerVariableControl,m_pclModel->GetName()+" "+g_c_csInitialIABehaviour, TRUE, /*Stefan Dumitrean 20-07(OAC buttons)*/1 ); //ANNECY BBB
			pclEnum->m_pub_fn_vSetControlCanBeDisplayedCallBack(fn_bMustDisplay);
			fn_vAddOwnerData(pclEnum,OAC_DATA_LEVEL_1,EDCAR_DATA_VISI_DUAL);
			pclListOfDesignerVariableControl->AddTail(pclEnum);

			pclDsgVarView->m_fn_vUpdateControls(pclListOfDesignerVariableControl);

			//Adds data for initial intelligence control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

			CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csInitialIABehaviour,pclListOfDesignerVariableType);
			fn_vAddOwnerData(pclData,m_pclModel->m_fn_pclGetEditorActor());
			pclData->m_pub_fn_vSetDataChangedCallBackFunction(fn_vNotifyInitialValue);
			pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

			//Sets initial value
			CString	csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
			if(!csValue.IsEmpty())
				M_GetScriptAINoComportInitDefault(pstScriptAIIntell)=atol(M_MAKECHAR(csValue));
			else
				M_GetScriptAINoComportInitDefault(pstScriptAIIntell)=0;

			pclData->m_fn_vSetMotorDataPtr(&(M_GetScriptAINoComportInitDefault(pstScriptAIIntell)));

			pclListOfDesignerVariableType->AddTail(pclData);
			pclEnum->m_fn_vSetEditedData(pclData);
			pclData->m_fn_vGetMotorData();

			//Adds data for initial intelligence control for the instances
			pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor								*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject				*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain			hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList			*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csInitialIABehaviour,pclListOfDesignerVariableType);
				fn_vAddOwnerData(pclData,pclInstance->m_fn_pclGetEditorActor());
				pclData->m_pub_fn_vSetDataChangedCallBackFunction(fn_vNotifyInitialValue);
				pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

				if(hBrain!=NULL)
				{
					AI_tdstMind							*pstMind=M_pstGetMindOfBrain(hBrain);

					//Sets initial value
					CString	csValue=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
					if(!csValue.IsEmpty())
						M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=atol(M_MAKECHAR(csValue));
					else
					{
						csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
						if(!csValue.IsEmpty())
							M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=atol(M_MAKECHAR(csValue));
						else
							M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=0;
					}

					pclData->m_fn_vSetMotorDataPtr(&(M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))));
				}
				else
				//For a new instance without hBrain
				{
					pclData->m_fn_vSetMotorDataPtr(&gs_lDummyData);
				}

				pclListOfDesignerVariableType->AddTail(pclData);
				pclEnum->m_fn_vSetEditedData(pclData);
				pclData->m_fn_vGetMotorData();
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't create control ("+g_c_csInitialIABehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}

	//For Reflex
	if(m_clListOfReflexBehaviours.GetCount()!=0)
	{
		tdstScriptAI	*pstScriptAIReflex=AI_M_p_stGetReflexScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIReflex!=NULL)
		{
			//Adds entry in control (Reflex Behaviour Name)
			CTL_Editor_EnumDescriptor	*pclEnumList=new CTL_Editor_EnumDescriptor(g_c_csInitialRfxBehaviour,4);
			POSITION									pos=m_clListOfReflexBehaviours.GetHeadPosition();
			long											lIndex=0;
			while(pos!=NULL)
			{
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfReflexBehaviours.GetNext(pos);

				pclEnumList->m_fn_pclAddElement(pclMainBehaviour->GetName(),lIndex);

				lIndex++;
			}
			
			//Adds a control for initial reflex
			CTL_Editor_EnumControl		*pclEnum;
			pclEnum=new CTL_Editor_EnumControl(READ_WRITE,pclListOfDesignerVariableControl,m_pclModel->GetName()+" "+g_c_csInitialRfxBehaviour, TRUE, /*Stefan Dumitrean 20-07(OAC buttons)*/1 ); //ANNECY BBB
			pclEnum->m_pub_fn_vSetControlCanBeDisplayedCallBack(fn_bMustDisplay);
			fn_vAddOwnerData(pclEnum,OAC_DATA_LEVEL_1,EDCAR_DATA_VISI_DUAL);
			pclListOfDesignerVariableControl->AddTail(pclEnum);

			pclDsgVarView->m_fn_vUpdateControls(pclListOfDesignerVariableControl);

			//Adds data for initial reflex control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

			CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csInitialRfxBehaviour,pclListOfDesignerVariableType);
			fn_vAddOwnerData(pclData,m_pclModel->m_fn_pclGetEditorActor());
			pclData->m_pub_fn_vSetDataChangedCallBackFunction(fn_vNotifyInitialValue);
			pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

			//Sets initial value
			CString				csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
			if(!csValue.IsEmpty())
				M_GetScriptAINoComportInitDefault(pstScriptAIReflex)=atol(M_MAKECHAR(csValue));
			else
				M_GetScriptAINoComportInitDefault(pstScriptAIReflex)=0;

			pclData->m_fn_vSetMotorDataPtr(&(M_GetScriptAINoComportInitDefault(pstScriptAIReflex)));

			pclListOfDesignerVariableType->AddTail(pclData);
			pclEnum->m_fn_vSetEditedData(pclData);
			pclData->m_fn_vGetMotorData();

			//Adds data for initial reflex control for the instances
			pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor								*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject				*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain			hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList			*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csInitialRfxBehaviour,pclListOfDesignerVariableType);
				fn_vAddOwnerData(pclData,pclInstance->m_fn_pclGetEditorActor());
				pclData->m_pub_fn_vSetDataChangedCallBackFunction(fn_vNotifyInitialValue);
				pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

				if(hBrain!=NULL)
				{
					AI_tdstMind							*pstMind=M_pstGetMindOfBrain(hBrain);

					//Sets initial value
					CString	csValue=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
					if(!csValue.IsEmpty())
						M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=atol(M_MAKECHAR(csValue));
					else
					{
						csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
						if(!csValue.IsEmpty())
							M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=atol(M_MAKECHAR(csValue));
						else
							M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=0;
					}

					pclData->m_fn_vSetMotorDataPtr(&(M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))));
				}
				else
				//For a new instance without hBrain
				{
					pclData->m_fn_vSetMotorDataPtr(&gs_lDummyData);
				}

				pclListOfDesignerVariableType->AddTail(pclData);
				pclEnum->m_fn_vSetEditedData(pclData);
				pclData->m_fn_vGetMotorData();
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't create control ("+g_c_csInitialRfxBehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateInitControlsForBehaviour(AI_tdstAIModel *pstAIModel)
{
	CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
	CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

	CPA_List<CPA_BaseObject>	clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);
																											
	//For Intelligence
	if(m_clListOfIntelligenceBehaviours.GetCount()!=0)
	{
		tdstScriptAI	*pstScriptAIIntell=AI_M_p_stGetIntelligenceScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIIntell!=NULL)
		{
			//Updates data for initial intelligence control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialIABehaviour);
			if(pclData!=NULL)
			{
				//Sets initial value
				CString	csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
				if(!csValue.IsEmpty())
					M_GetScriptAINoComportInitDefault(pstScriptAIIntell)=atol(M_MAKECHAR(csValue));
				else
					M_GetScriptAINoComportInitDefault(pstScriptAIIntell)=0;

				pclData->m_fn_vSetMotorDataPtr(&(M_GetScriptAINoComportInitDefault(pstScriptAIIntell)));
			}

			//Updates data for initial intelligence control for the instances
			POSITION	pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor								*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject				*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain			hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList			*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				if(hBrain!=NULL)
				{
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialIABehaviour);
					if(pclData!=NULL)
					{
						AI_tdstMind							*pstMind=M_pstGetMindOfBrain(hBrain);

						//Sets initial value
						CString	csValue=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
						if(!csValue.IsEmpty())
							M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=atol(M_MAKECHAR(csValue));
						else
						{
							csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialIABehaviour);
							if(!csValue.IsEmpty())
								M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=atol(M_MAKECHAR(csValue));
							else
								M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))=0;
						}

						pclData->m_fn_vSetMotorDataPtr(&(M_GetNoComportInit(AI_M_p_stGetIntelligence(pstMind))));
					}
				}
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't update control ("+g_c_csInitialIABehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}

	//For Reflex
	if(m_clListOfReflexBehaviours.GetCount()!=0)
	{
		tdstScriptAI	*pstScriptAIReflex=AI_M_p_stGetReflexScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIReflex!=NULL)
		{
			//Updates data for initial reflex control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialRfxBehaviour);
			if(pclData!=NULL)
			{
				//Sets initial value
				CString				csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
				if(!csValue.IsEmpty())
					M_GetScriptAINoComportInitDefault(pstScriptAIReflex)=atol(M_MAKECHAR(csValue));
				else
					M_GetScriptAINoComportInitDefault(pstScriptAIReflex)=0;

				pclData->m_fn_vSetMotorDataPtr(&(M_GetScriptAINoComportInitDefault(pstScriptAIReflex)));
			}

			//Updates data for initial reflex control for the instances
			POSITION	pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor								*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject				*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain			hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList			*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				if(hBrain!=NULL)
				{
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialRfxBehaviour);
					if(pclData!=NULL)
					{
						AI_tdstMind							*pstMind=M_pstGetMindOfBrain(hBrain);

						//Sets initial value
						CString	csValue=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
						if(!csValue.IsEmpty())
							M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=atol(M_MAKECHAR(csValue));
						else
						{
							csValue=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_csGetFirstValueOfADesignerVariable(g_c_csInitialRfxBehaviour);
							if(!csValue.IsEmpty())
								M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=atol(M_MAKECHAR(csValue));
							else
								M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))=0;
						}

						pclData->m_fn_vSetMotorDataPtr(&(M_GetNoComportInit(AI_M_p_stGetReflex(pstMind))));
					}
				}
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't update control ("+g_c_csInitialRfxBehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vCreateControlsForBehaviour()
{
	CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
	CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

	CPA_List<CPA_BaseObject>	clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);

	//For Intelligence
	if(!m_clListOfIntelligenceBehaviours.IsEmpty() && !clInstancesList.IsEmpty())
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
		AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
		AI_tdstAIModel			*pstAIModel=AI_M_p_stGetAIModel(pstMind);
		tdstScriptAI				*pstScriptAIIntell=AI_M_p_stGetIntelligenceScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIIntell!=NULL)
		{
			//Adds entry in control (IA Behaviour Name)
			CTL_Editor_EnumDescriptor	*pclEnumList=new CTL_Editor_EnumDescriptor(g_c_csCurrentIABehaviour,4);
			POSITION									pos=m_clListOfIntelligenceBehaviours.GetHeadPosition();
			unsigned long							ulIndex=0;
			while(pos!=NULL && ulIndex<M_GetScriptAINbComport(pstScriptAIIntell))
			{
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfIntelligenceBehaviours.GetNext(pos);

				pclEnumList->m_fn_pclAddElement(pclMainBehaviour->GetName(),
																				 (long)(M_GetScriptAIComportN(pstScriptAIIntell,ulIndex)));

				ulIndex++;
			}
			
			//Adds a control for current intelligence
			CTL_Editor_EnumControl		*pclEnum;
			pclEnum=new CTL_Editor_EnumControl(READ_ONLY,pclListOfDesignerVariableControl,m_pclModel->GetName()+" "+g_c_csCurrentIABehaviour, TRUE, /*Stefan Dumitrean 20-07(OAC buttons)*/2 ); //ANNECY BBB
			pclEnum->m_pub_fn_vSetControlCanBeDisplayedCallBack(fn_bMustDisplay);
			fn_vAddOwnerData(pclEnum,OAC_DATA_LEVEL_1,EDCAR_DATA_VISI_ONLY_INSTANCES);
			pclListOfDesignerVariableControl->AddTail(pclEnum);

			pclDsgVarView->m_fn_vUpdateControls(pclListOfDesignerVariableControl);

			//Adds a dummy data for current intelligence control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

			CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csCurrentIABehaviour,pclListOfDesignerVariableType);
			fn_vAddOwnerData(pclData,m_pclModel->m_fn_pclGetEditorActor());
			pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

			tdstIntelligence	*pstIntelligence=AI_M_p_stGetIntelligence(pstMind);
			pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstIntelligence)));
			pclListOfDesignerVariableType->AddTail(pclData);
			pclEnum->m_fn_vSetEditedData(pclData);
			pclData->m_fn_vGetMotorData();

			//Adds data for current intelligence control for the instances
			pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList	*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csCurrentIABehaviour,pclListOfDesignerVariableType);
				fn_vAddOwnerData(pclData,pclInstance->m_fn_pclGetEditorActor());
				pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

				if(hBrain!=NULL)
				{
					AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
					tdstIntelligence		*pstIntelligence=AI_M_p_stGetIntelligence(pstMind);

					pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstIntelligence)));
				}
				else
				//For a new instance without hBrain
				{
					pclData->m_fn_vSetMotorDataPtr(&gs_lDummyData);
			}

				pclListOfDesignerVariableType->AddTail(pclData);
				pclEnum->m_fn_vSetEditedData(pclData);
				pclData->m_fn_vGetMotorData();
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't create control ("+g_c_csCurrentIABehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}

	//For Reflex
	if(!m_clListOfReflexBehaviours.IsEmpty() && !clInstancesList.IsEmpty())
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
		AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
		AI_tdstAIModel			*pstAIModel=AI_M_p_stGetAIModel(pstMind);
		tdstScriptAI				*pstScriptAIReflex=AI_M_p_stGetReflexScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIReflex!=NULL)
		{
			//Adds entry in control (Reflex Behaviour Name)
			CTL_Editor_EnumDescriptor	*pclEnumList=new CTL_Editor_EnumDescriptor(g_c_csCurrentRfxBehaviour,4);
			POSITION									pos=m_clListOfReflexBehaviours.GetHeadPosition();
			unsigned long							ulIndex=0;
			while(pos!=NULL && ulIndex<M_GetScriptAINbComport(pstScriptAIReflex))
			{
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clListOfReflexBehaviours.GetNext(pos);

				pclEnumList->m_fn_pclAddElement(pclMainBehaviour->GetName(),
																				 (long)(M_GetScriptAIComportN(pstScriptAIReflex,ulIndex)));

				ulIndex++;
			}
			
			//Adds a control for current reflex
			CTL_Editor_EnumControl		*pclEnum;
			pclEnum=new CTL_Editor_EnumControl(READ_ONLY,pclListOfDesignerVariableControl,m_pclModel->GetName()+" "+g_c_csCurrentRfxBehaviour, TRUE, /*Stefan Dumitrean 20-07(OAC buttons)*/2 ); //ANNECY BBB
			pclEnum->m_pub_fn_vSetControlCanBeDisplayedCallBack(fn_bMustDisplay);
			fn_vAddOwnerData(pclEnum,OAC_DATA_LEVEL_1,EDCAR_DATA_VISI_ONLY_INSTANCES);
			pclListOfDesignerVariableControl->AddTail(pclEnum);

			pclDsgVarView->m_fn_vUpdateControls(pclListOfDesignerVariableControl);

			//Adds a dummy data for reflex init control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

			CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csCurrentRfxBehaviour,pclListOfDesignerVariableType);
			fn_vAddOwnerData(pclData,m_pclModel->m_fn_pclGetEditorActor());
			pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

			tdstIntelligence	*pstReflex=AI_M_p_stGetReflex(pstMind);
			pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstReflex)));
			pclListOfDesignerVariableType->AddTail(pclData);
			pclEnum->m_fn_vSetEditedData(pclData);
			pclData->m_fn_vGetMotorData();

			//Adds data for reflex init control for the instances
			pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList	*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				CTL_Editor_EnumData	*pclData=new CTL_Editor_EnumData(pclEnumList,g_c_csCurrentRfxBehaviour,pclListOfDesignerVariableType);
				fn_vAddOwnerData(pclData,pclInstance->m_fn_pclGetEditorActor());
				pclData->m_pub_fn_vSetDataChangedSpecialCallBackFunction(fn_vUpdateWatchData);																																						\

				if(hBrain!=NULL)
				{
					AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
					tdstIntelligence		*pstReflex=AI_M_p_stGetReflex(pstMind);

					pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstReflex)));
				}
				else
				//For a new instance without hBrain
				{
					pclData->m_fn_vSetMotorDataPtr(&gs_lDummyData);
				}

				pclListOfDesignerVariableType->AddTail(pclData);
				pclEnum->m_fn_vSetEditedData(pclData);
				pclData->m_fn_vGetMotorData();
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't create control ("+g_c_csCurrentRfxBehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateControlsForBehaviour()
{
	CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
	CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

	CPA_List<CPA_BaseObject>	clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);

	//For Intelligence
	if(!m_clListOfIntelligenceBehaviours.IsEmpty() && !clInstancesList.IsEmpty())
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
		AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
		AI_tdstAIModel			*pstAIModel=AI_M_p_stGetAIModel(pstMind);
		tdstScriptAI				*pstScriptAIIntell=AI_M_p_stGetIntelligenceScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIIntell!=NULL)
		{
			//Updates data for current intelligence control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csCurrentIABehaviour);
			if(pclData!=NULL)
			{
				tdstIntelligence	*pstIntelligence=AI_M_p_stGetIntelligence(pstMind);
				pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstIntelligence)));
			}

			//Updates data for current intelligence control for the instances
			POSITION	pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList	*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				if(hBrain!=NULL)
				{
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csCurrentIABehaviour);
					if(pclData!=NULL)
					{
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
						tdstIntelligence		*pstIntelligence=AI_M_p_stGetIntelligence(pstMind);

						pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstIntelligence)));
					}
				}
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't update control ("+g_c_csCurrentIABehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}

	//For Reflex
	if(!m_clListOfReflexBehaviours.IsEmpty() && !clInstancesList.IsEmpty())
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
		AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
		AI_tdstAIModel			*pstAIModel=AI_M_p_stGetAIModel(pstMind);
		tdstScriptAI				*pstScriptAIReflex=AI_M_p_stGetReflexScriptAIModel(pstAIModel);

		//Are editor data and engine data coherent ?
		if(pstScriptAIReflex!=NULL)
		{
			//Updates data for reflex init control for the model
			CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csCurrentRfxBehaviour);
			if(pclData!=NULL)
			{
				tdstIntelligence	*pstReflex=AI_M_p_stGetReflex(pstMind);
				pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstReflex)));
			}

			//Updates data for reflex init control for the instances
			POSITION	pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				CTL_Editor_DataList	*pclListOfDesignerVariableType;
				pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

				if(hBrain!=NULL)
				{
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csCurrentRfxBehaviour);
					if(pclData!=NULL)
					{
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
						tdstIntelligence		*pstReflex=AI_M_p_stGetReflex(pstMind);

						pclData->m_fn_vSetMotorDataPtr(&(M_GetCurrentComport(pstReflex)));
					}
				}
			}
		}
		//Editor data and engine data aren't coherent !!
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't update control ("+g_c_csCurrentRfxBehaviour+") : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
	}
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vRememberOldDsgVarValues()
{
	if(m_bOldApplicationIsOK)
	{
		//First part : empties list of old values
		//Model
		m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_vDestroyAndRemoveAllEntries();
		//Instances
		CPA_List<EdActors_EditorActorInstance>	*pclInstancesList=&(m_pclModel->m_fn_pclGetEditorModel()->m_clInstancesList);
		POSITION																pos=pclInstancesList->GetHeadPosition();
		while(pos!=NULL)
		{
			EdActors_EditorActorInstance	*pclInstance=pclInstancesList->GetNext(pos);
			pclInstance->m_pclListOfDesignerVariables->m_fn_vDestroyAndRemoveAllEntries();
		}

		//Second part : fills list of old values
		CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
		CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

		//Model
		CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
		pos=pclListOfDesignerVariableType->GetHeadPosition();

		if(pos == NULL)
		{
			m_fn_vCreateOrUpdateDsgVarControlsWithEngineValues();
			pos=pclListOfDesignerVariableType->GetHeadPosition();
		}

		while(pos!=NULL)
		{
			CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->GetNext(pos);

			//Remembers old values
			CString			csName;
			CString			csType;
			CStringList	cslValues;
			fn_vGetLowInfoAboutDsgVar(pclData,csName,csType,&cslValues);

			CPA_EdIR_DesignerVariable	*pclDsgVar;
			pclDsgVar=new CPA_EdIR_DesignerVariable(csName,csType,&cslValues);
			pclDsgVar->m_fn_vSetInWatchWindow(fn_bIsInWatch(pclData));
			m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
		}

		//Instances
		pos=pclInstancesList->GetHeadPosition();
		while(pos!=NULL)
		{
			EdActors_EditorActorInstance	*pclInstance=pclInstancesList->GetNext(pos);
			CTL_Editor_DataList						*pclListOfDesignerVariableType=pclInstance->m_pclListOfDesignerVariableType;

			//For new instance without old value
			CPA_Actor											*pclActor=pclInstance->m_pclActor;
			tdstEngineObject							*pstEngineObject=(tdstEngineObject *)pclActor->GetData();
			MS_tdxHandleToBrain						hBrain=pstEngineObject->h_Brain;

			if(hBrain!=NULL)
			{
				POSITION	p=pclListOfDesignerVariableType->GetHeadPosition();
				while(p!=NULL)
				{
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->GetNext(p);

					//Remembers old values
					CString			csName;
					CString			csType;
					CStringList	cslValues;
					fn_vGetLowInfoAboutDsgVar(pclData,csName,csType,&cslValues);

					CPA_EdIR_DesignerVariable	*pclDsgVar;
					pclDsgVar=new CPA_EdIR_DesignerVariable(csName,csType,&cslValues);
					pclDsgVar->m_fn_vSetInWatchWindow(fn_bIsInWatch(pclData));
					pclInstance->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
				}
			}
		}
	}
}

/*************************************************************************/
void CPA_EdIR_Brain::m_fn_vDeleteOldDsgVarControl()
{
	CTL_Editor_ControlList					*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
	CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);

	//Deletes old datas
	//Model
	CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
	POSITION						pos=pclListOfDesignerVariableType->GetHeadPosition();
	while(pos!=NULL)
	{
		//Deletes data
		fn_vDelete(pclListOfDesignerVariableType->GetNext(pos));
	}
	pclListOfDesignerVariableType->RemoveAll();

	//Instances
	CPA_List<EdActors_EditorActorInstance>	*pclInstancesList=&(m_pclModel->m_fn_pclGetEditorModel()->m_clInstancesList);
	pos=pclInstancesList->GetHeadPosition();
	while(pos!=NULL)
	{
		EdActors_EditorActorInstance	*pclInstance=pclInstancesList->GetNext(pos);
		CTL_Editor_DataList						*pclListOfDesignerVariableType=pclInstance->m_pclListOfDesignerVariableType;

		POSITION	p=pclListOfDesignerVariableType->GetHeadPosition();
		while(p!=NULL)
		{
			//Deletes data
			fn_vDelete(pclListOfDesignerVariableType->GetNext(p));
		}
		pclListOfDesignerVariableType->RemoveAll();
	}

	//Deletes old controls
	pclDsgVarView->m_fn_vAModelHasBeenRemoved(pclListOfDesignerVariableControl);

	pos=pclListOfDesignerVariableControl->GetHeadPosition();
	while(pos!=NULL)
		delete pclListOfDesignerVariableControl->GetNext(pos);
	pclListOfDesignerVariableControl->RemoveAll();
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateDatasWithOldValues()
{
	CPA_List<CPA_BaseObject> clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);

	//Updates Data with old values for models
	POSITION	pos=clInstancesList.GetHeadPosition();
	if(pos!=NULL)
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)(clInstancesList.GetAt(pos));
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

		if(hBrain!=NULL)
		{
			AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

			if(pstMind!=NULL)
			{
				if(M_GetDsgMem(pstMind)!=NULL)
				{
					CTL_Editor_DataList		*pclListOfDesignerVariableType;
					pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

					POSITION			p=pclListOfDesignerVariableType->GetHeadPosition();
					unsigned long	ulIndexOfSaveTypeDsgVar=0;
					unsigned long	ulIndexOfInitTypeDsgVar=0;
					unsigned long	ulIndexOfDsgVarInit=0;
					while(p!=NULL)
					{
						CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->GetNext(p);

						//Gets address of the engine data
						//Only for Save Type and Init Type and initial value for array
						if(pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarIndexText)==-1) //exclude index
						{
							void	*pvEngineData=NULL;
							if((fn_pvGetBaseAddress(pclData)!=NULL) && //is it an array ?
								 (pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarInitText)!=-1) && //is it an initial value ?
								 (ulIndexOfDsgVarInit<M_GetNbDsgVar(pstMind)))
							{
								pvEngineData=M_GetDsgVarAddrDefault(pstMind,ulIndexOfDsgVarInit);
								ulIndexOfDsgVarInit++;
							}
							else
							if((pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarSaveTypeText)!=-1) &&
								 (ulIndexOfSaveTypeDsgVar<M_GetNbDsgVar(pstMind)))
							{
								pvEngineData=&M_GetDsgVarSaveType(pstMind,ulIndexOfSaveTypeDsgVar);
								ulIndexOfSaveTypeDsgVar++;
							}
							else
							if((pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarInitTypeText)!=-1) &&
								 (ulIndexOfInitTypeDsgVar<M_GetNbDsgVar(pstMind)))
							{
								pvEngineData=&M_GetDsgVarInitType(pstMind,ulIndexOfInitTypeDsgVar);
								ulIndexOfInitTypeDsgVar++;
							}

							//Gets old value
							CStringList	*pcslValues=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_pcslGetValuesOfADesignerVariable(pclData->m_pub_fn_csGetDataName());

							//Sets old value into the dsg var
							if((pcslValues!=NULL) &&
								 (pvEngineData!=NULL))
								fn_vSetValueInDsgVar(pclData,pcslValues,pvEngineData);
						}
					}
				}
			}
		}
	}

	//Updates Data with old values for instances
	pos=clInstancesList.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_Actor						*pclInstance=(CPA_Actor *)(clInstancesList.GetNext(pos));
		tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
		MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

		if(hBrain!=NULL)
		{
			AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

			if(pstMind!=NULL)
			{
				if(M_GetDsgMem(pstMind)!=NULL)
				{
					CTL_Editor_DataList		*pclListOfDesignerVariableType;
					pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;

					POSITION			p=pclListOfDesignerVariableType->GetHeadPosition();
					unsigned long	ulIndexOfDsgVar=0;
					unsigned long	ulIndexOfDsgVarInit=0;
					while(p!=NULL)
					{
						CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->GetNext(p);

						//Gets address of the engine data
						//Only for current and initial value
						if(pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarIndexText)==-1)
						{
							void	*pvEngineData=NULL;
							if((pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarInitText)!=-1) &&
								 (ulIndexOfDsgVarInit<M_GetNbDsgVar(pstMind)))
							{
								pvEngineData=M_GetDsgVarAddrInit(pstMind,ulIndexOfDsgVarInit);
								ulIndexOfDsgVarInit++;
							}
							else
							if((pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarSaveTypeText)==-1) &&
								 (pclData->m_pub_fn_csGetDataName().Find(g_c_csDsgVarInitTypeText)==-1) &&
								 (ulIndexOfDsgVar<M_GetNbDsgVar(pstMind)))
							{
								pvEngineData=M_GetDsgVarAddr(pstMind,ulIndexOfDsgVar);
								ulIndexOfDsgVar++;
							}

							//Gets old value
							CStringList	*pcslValues=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_pcslGetValuesOfADesignerVariable(pclData->m_pub_fn_csGetDataName());

							//Sets old value into the dsg var
							if((pcslValues!=NULL) &&
								 (pvEngineData!=NULL))
								fn_vSetValueInDsgVar(pclData,pcslValues,pvEngineData);

							//Sets the dsg var in watch window if needed
							if(pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->m_fn_bDesignerVariableIsInWatchWindow(pclData->m_pub_fn_csGetDataName()))
							{
								fn_vAddValueInWatch(pclData);
								fn_vSetInWatch(pclData,TRUE);
							}
						}
					}
				}
			}
		}
	}
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vCreateOrUpdateDsgVarControlsWithEngineValues()
{
	if(m_bDsgVarControlsAreCreated==FALSE)
	{
//ANNECY CB
		CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);
		pclDsgVarView->m_bCanUpdateControls = FALSE;
//END
		if(m_bOldApplicationIsOK==TRUE)
		{
			AfxGetApp()->DoWaitCursor(1);
			SetCapture(g_pclAIInterface->m_pclIRMainFrame->m_hWnd);

			CString	csFamilyName=(m_pclModel->m_pub_fn_pclGetFamily()==NULL) ? "" : m_pclModel->m_pub_fn_pclGetFamily()->GetName();
			CString	csModelName=m_pclModel->GetName();

			CString	csMsg;
			CString	csMsgBase="Creating controls for "+csFamilyName+"@"+csModelName+" ... ";
			fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);

			//Creates dsg var controls
			if(!m_clListOfDeclarations.IsEmpty())
			{
				g_pclCurrentModel=m_pclModel;

				//Gets current state
				CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;
				tdeEditState		tdeOldState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_tdeGetEditState();

				pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl, FALSE);

				//Gets declaration text without any comments
				CString	csIn;

				//Gets Designer Variable MainBehaviour (first element in the list)
				CPA_EdIR_MainBehaviour *pclMainBehaviour=m_clListOfDeclarations.GetHead();

				//Gets text declaration
				if(pclMainBehaviour->m_pclBehaviour!=NULL)
					csIn=pclMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
				else
					csIn=pclMainBehaviour->m_csText;

				csIn=fn_csGetCodeWithoutComments(csIn);

				//Checks dsg var declarations
//ANNECY CB
//				if(m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText))
//END ANNECY
				{
					//Allocates and fills initial buffer for instances if needed
					CPA_List<CPA_BaseObject> clInstancesList;
					g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);
					POSITION	pos=clInstancesList.GetHeadPosition();
					while(pos!=NULL)
					{
						CPA_Actor						*pclInstance=(CPA_Actor *)(clInstancesList.GetNext(pos));
						tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

						if(hBrain!=NULL)
						{
							AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

							if((pstMind!=NULL) &&
								 (AI_M_p_stGetDsgVar(pstMind)!=NULL) &&
								 (M_GetDsgMemBufferInit(pstMind)==NULL))
							{
								M_GetDsgMemBufferInit(pstMind)=fn_p_cAllocDsgMemBuffer(M_GetDsgVarSize(pstMind));
								// copy it from the model 
								memcpy(M_GetDsgMemBufferInit(pstMind),M_GetDsgMemDefaultInit(pstMind),M_GetDsgVarSize(pstMind));
							}
						}
					}

					//Creates controls and datas
					long		lCurrentIndex=0;
					CString	csWord=fn_csGetNextWord(csIn,lCurrentIndex);
					long		lNoDsgVar=0;
					while(!csWord.IsEmpty())
					{
						//BEGIN ROMTEAM Cristi Petrescu 98-06-
						// Check for qualifier
						BOOL bIsPrivate = FALSE;
						if (fn_bIsDsgVarQualifier (csWord))
						{
							if (!csWord.CompareNoCase("private"))
								bIsPrivate = TRUE;
							csWord=fn_csGetNextWord(csIn,lCurrentIndex);
						}
						//END ROMTEAM Cristi Petrescu 98-06-

						//Gets Type
						tdeDsgVarTypeId	eDsgVarTypeId=(tdeDsgVarTypeId)fn_lGetDsgVarTypeIdFromEditorName(csWord);
						CString					csDsgVarType=szFindDsgVarTypeScriptNameFromId(eDsgVarTypeId);

						//Gets Name
						CString csDsgVarName=fn_csGetNextWord(csIn,lCurrentIndex);

						//Gets Init Value
						CString csDsgVarValue=fn_csGetNextWord(csIn,lCurrentIndex);

						//Creates a dsg var control
						//BEGIN ROMTEAM Cristi Petrescu 98-06-
						fn_vCreateDsgVarControl(csDsgVarType,csDsgVarName,lNoDsgVar,bIsPrivate);
						//END ROMTEAM Cristi Petrescu 98-06-

						if(eDsgVarTypeId==eDsgVarType_Vector)
						{
							//Skips x,y,z components
							fn_csGetNextWord(csIn,lCurrentIndex);
							fn_csGetNextWord(csIn,lCurrentIndex);
							fn_csGetNextWord(csIn,lCurrentIndex);
						}

						//Skips Type
						csWord=fn_csGetNextWord(csIn,lCurrentIndex);

						lNoDsgVar++;
					}

					//Sets current state
					pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(tdeOldState, FALSE);
				}
//ANNECY CB
//				else
//				{
//					AfxMessageBox("dsg var declaration is not good !!!\nCan't create windows controls for the dsg var");
//				}
//END ANNECY
			}

			//Fills initial value for behaviour
			//Model
			CPA_EdIR_DesignerVariable	*pclDsgVar;
			CPA_List<CPA_BaseObject>	clInstancesList;
			g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);
			if(!clInstancesList.IsEmpty())
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				if(hBrain!=NULL)
				{
					AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

					if(pstMind!=NULL)
					{
						AI_tdstAIModel	*p_stAIModel=AI_M_p_stGetAIModel(pstMind);

						if(p_stAIModel!=NULL)
						{
							//Sets initial behaviour for intelligence
							tdstScriptAI	*pstIAScriptIA=AI_M_p_stGetIntelligenceScriptAIModel(p_stAIModel);
							if(pstIAScriptIA!=NULL)
							{
								long				lInitialIABehaviourIndex=M_GetScriptAINoComportInitDefault(pstIAScriptIA);
								CString			csInitialIABehaviour;
								csInitialIABehaviour.Format("%ld",lInitialIABehaviourIndex);
								CStringList	cslValues;
								cslValues.AddTail(csInitialIABehaviour);
								pclDsgVar=new CPA_EdIR_DesignerVariable(g_c_csInitialIABehaviour,g_c_csEnum,&cslValues);
								m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
							}

							//Sets initial behaviour for reflex
							tdstScriptAI	*pstRfxScriptIA=AI_M_p_stGetReflexScriptAIModel(p_stAIModel);
							if(pstRfxScriptIA!=NULL)
							{
								long				lInitialRfxBehaviourIndex=M_GetScriptAINoComportInitDefault(pstRfxScriptIA);
								CString			csInitialRfxBehaviour;
								csInitialRfxBehaviour.Format("%ld",lInitialRfxBehaviourIndex);
								CStringList	cslValues;
								cslValues.AddTail(csInitialRfxBehaviour);
								pclDsgVar=new CPA_EdIR_DesignerVariable(g_c_csInitialRfxBehaviour,g_c_csEnum,&cslValues);
								m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
							}
						}
					}
				}
			}

			//Instances
			POSITION	pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				if(hBrain!=NULL)
				{
					AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

					if(pstMind!=NULL)
					{
						//Sets initial behaviour for intelligence
						tdstIntelligence	*pstIAIntelligence=AI_M_p_stGetIntelligence(pstMind);
						if(pstIAIntelligence!=NULL)
						{
							long				lInitialIABehaviourIndex=M_GetNoComportInit(pstIAIntelligence);
							CString			csInitialIABehaviour;
							csInitialIABehaviour.Format("%ld",lInitialIABehaviourIndex);
							CStringList	cslValues;
							cslValues.AddTail(csInitialIABehaviour);
							pclDsgVar=new CPA_EdIR_DesignerVariable(g_c_csInitialIABehaviour,g_c_csEnum,&cslValues);
							pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
						}

						//Sets initial behaviour fr reflex
						tdstIntelligence	*pstRfxIntelligence=AI_M_p_stGetReflex(pstMind);
						if(pstRfxIntelligence!=NULL)
						{
							long				lInitialRfxBehaviourIndex=M_GetNoComportInit(pstRfxIntelligence);
							CString			csInitialRfxBehaviour;
							csInitialRfxBehaviour.Format("%ld",lInitialRfxBehaviourIndex);
							CStringList	cslValues;
							cslValues.AddTail(csInitialRfxBehaviour);
							pclDsgVar=new CPA_EdIR_DesignerVariable(g_c_csInitialRfxBehaviour,g_c_csEnum,&cslValues);
							pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariables->AddTail(pclDsgVar);
						}
					}
				}
			}

			//Creates controls for initial and current behaviour
			if(!clInstancesList.IsEmpty())
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

				if(hBrain!=NULL)
				{
					AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

					if(pstMind!=NULL)
					{
						AI_tdstAIModel	*p_stAIModel=AI_M_p_stGetAIModel(pstMind);

						if(p_stAIModel!=NULL)
						{
							//Creates controls for current behaviour
							m_fn_vCreateInitControlsForBehaviour(p_stAIModel);

							//Creates controls for current behaviour
							m_fn_vCreateControlsForBehaviour();
						}
					}
				}
			}

			csMsg=csMsgBase+"OK";
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

//ANNECY CB
			AfxGetApp()->DoWaitCursor(-1);
			ReleaseCapture();
//			AfxGetApp()->DoWaitCursor(0);
//END ANNECY
			m_bDsgVarControlsAreCreated=(!clInstancesList.IsEmpty());
		}
		else
		{
			CPA_Actor	*pclActor=m_fn_pclGetModel();
			CString		csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
			CString		csModelName=pclActor->GetName();
			CString		csMsg="Can't create windows controls : check IA of "+csFamilyName+"@"+csModelName;
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
		}
//ANNECY CB
		pclDsgVarView->m_bCanUpdateControls = TRUE;
		CTL_Editor_ControlList	*pclListOfDesignerVariableControl=&(((EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor())->m_clListOfDesignerVariableControl);
		pclDsgVarView->m_fn_vUpdateControls(pclListOfDesignerVariableControl);
		CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
//END
	}
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bCheckIA()
{
	//TRUE if no modification has been done
	if(m_fn_bHasBeenModified()==FALSE)
		return TRUE;

	AfxGetApp()->DoWaitCursor(1);
	SetCapture(g_pclAIInterface->m_pclIRMainFrame->m_hWnd);

	CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;
	BOOL						bOldButtonsState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_bGetButtonsSate();
	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vEnableButtons(FALSE);

	CString	csFamilyName=(m_pclModel->m_pub_fn_pclGetFamily()==NULL) ? "" : m_pclModel->m_pub_fn_pclGetFamily()->GetName();
	CString	csModelName=m_pclModel->GetName();

	CString	csMsg;
	CString	csMsgBase="Checking IA of "+csFamilyName+"@"+csModelName+" ... ";
	fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);

	BOOL	bNoError;

	//Gets current state
	tdeEditState		tdeOldState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_tdeGetEditState();

	g_pclCurrentModel=m_pclModel;

	//Gets current brain
	CPA_EdIR_Brain	*pclOldBrain=pclIRMainFrame->m_fn_pclUpdateBrain(this,FALSE,FALSE);

	csMsg=csMsgBase+"(Declaration)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl, FALSE);
	bNoError=m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText);
	// BEGIN CPA2 Cristi Petrescu 98-03-
	if(bNoError)
	{
		csMsg=csMsgBase+"(Macro)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

		pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);
		bNoError=m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text);
	}
	// END CPA2 Cristi Petrescu 98-03-
	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if(bNoError)
	{
		csMsg=csMsgBase+"(Subr)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

		pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr);
		bNoError=m_clListOfSubrs.m_fn_bCheckIA(SubrText);
	}
	//END ROMTEAM Cristi Petrescu 98-04-
	if(bNoError)
	{
		csMsg=csMsgBase+"(Reflex)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

		pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditReflex, FALSE);
		bNoError=m_clListOfReflexBehaviours.m_fn_bCheckIA(ReflexText);
	}
	if(bNoError)
	{
		csMsg=csMsgBase+"(Intelligence)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

		pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditIntell, FALSE);
		bNoError=m_clListOfIntelligenceBehaviours.m_fn_bCheckIA(IntelligenceText);

		if(bNoError)
		{
			//Sets current state
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(tdeOldState, FALSE);

			//Sets current brain
			pclIRMainFrame->m_fn_pclUpdateBrain(pclOldBrain,FALSE,FALSE);
		}
	}

	if(bNoError)
	{
		//csMsg=csMsgBase+"OK (Good Work !!)";
		csMsg=csMsgBase+"OK (ça vaaaaa 3lik !!)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
	}
	else
	{
		csMsg=csMsgBase+"KO :-( (Bad Boy)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
	}

	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vEnableButtons(bOldButtonsState);

//ANNECY CB
	AfxGetApp()->DoWaitCursor(-1);
	ReleaseCapture();
//	AfxGetApp()->DoWaitCursor(0);
//END ANNECY
	return bNoError;
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bProcessIA(BOOL bProcessAll,CPA_Actor *pclActor)
{
	AfxGetApp()->DoWaitCursor(1);
	SetCapture(g_pclAIInterface->m_pclIRMainFrame->m_hWnd);
	g_pclAIInterface->m_bIsProcessing = TRUE;

	CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;
	BOOL						bOldButtonsState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_bGetButtonsSate();
	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vEnableButtons(FALSE);

	CString	csFamilyName=(m_pclModel->m_pub_fn_pclGetFamily()==NULL) ? "" : m_pclModel->m_pub_fn_pclGetFamily()->GetName();
	CString	csModelName=m_pclModel->GetName();

	CString	csMsg;
	CString	csMsgBase="Processing IA of "+csFamilyName+"@"+csModelName+" ... ";
	fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);

	//Applies IA
	BOOL	bNoError=TRUE;

	//For Debugger
	CList<BOOL,BOOL>	clDebugIsEnableList;

	//Gets current state
	tdeEditState		tdeOldState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_tdeGetEditState();

	//Gets current brain
	//Do not create dsg var when a new instance is inserted
	CPA_EdIR_Brain	*pclOldBrain=pclIRMainFrame->m_fn_pclUpdateBrain(this,(pclActor==NULL),FALSE);

	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	// safety, safety
	if (bProcessAll)
	{
		pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (EditIntell);
		pclIRMainFrame -> m_fn_vUpdateBehaviours(FALSE);
		pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (EditReflex);
		pclIRMainFrame -> m_fn_vUpdateBehaviours(FALSE);
		pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (EditDecl);
		pclIRMainFrame -> m_fn_vUpdateBehaviours(FALSE);
		pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (EditMacro);
		pclIRMainFrame -> m_fn_vUpdateBehaviours(FALSE);
		pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (EditSubr);
		pclIRMainFrame -> m_fn_vUpdateBehaviours(FALSE);	
	}

	pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_vSetEditState (tdeOldState);
	// END ROMTEAM Cristi Petrescu 98-06-

	g_pclCurrentModel=m_pclModel;

	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl, FALSE);

	//Translates declaration
	csMsg=csMsgBase+"(Declaration)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	//Dsg Var declaration has changed (dsg var declaration is the first element of the list)?
	BOOL	bDeclDsgVarMustBeProcessed=(m_clListOfDeclarations.GetHead()->m_pclBehaviour->m_fn_bIntermediateCodeListIsGood()==FALSE);

	//Remembers old dsg var values end removes it
	if(m_bOldApplicationIsOK)
		m_fn_vRememberOldDsgVarValues();

	//	if(bProcessAll || bDeclDsgVarMustBeProcessed)
	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	// somebody eliminated the test, so keep it so
	//{
		bNoError=m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText, TRUE);

		// process ALL macros
		if (bNoError)
		{
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro, FALSE);
			bNoError=m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text, TRUE);
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(tdeOldState, FALSE);
		}
	//}
	// END ROMTEAM Cristi Petrescu 98-06-	

	if(bNoError)
	{
		//Remembers old dsg var values end removes it
		m_fn_vRememberOldDsgVarValues();
		if(bProcessAll || bDeclDsgVarMustBeProcessed)
			m_fn_vDeleteOldDsgVarControl();

		//Removes debug info
		if(m_bOldApplicationIsOK)
		{
			m_clDebugInfoList.m_fn_vDestroyAndRemoveAllEntries();
		}

		//Removes ia MS for all instances
		AI_tdstAIModel						*p_stAIModel=NULL;
		CPA_List<CPA_BaseObject>	clInstancesList;
		g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);
		POSITION	pos=clInstancesList.GetHeadPosition();
		while(pos!=NULL)
		{
			CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
			tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
			MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

			if(hBrain!=NULL)
			{
				AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

				// CG PB DEBUGGER 23/06/98
				#ifdef ACTIVE_AIDEBUG
					//For Debugger
					clDebugIsEnableList.AddTail(AIDebug_fn_bGetEnable(pstMind));
				#endif

				//Gets Model in order to free it after
				if(p_stAIModel==NULL)
					p_stAIModel=AI_M_p_stGetAIModel(pstMind);

				//Removes ia MS
				fn_vFreeMindAll(&pstMind);
				fn_vBrainFree(pstEngineObject);
			}
		}

		//Frees Model
		fn_vFreeAIModelAll(&p_stAIModel);

	//	bNoError=m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText, TRUE);

		//Alloc IA
		long	lNbReflexComport=m_clListOfReflexBehaviours.GetCount();
		long	lNbIntellComport=m_clListOfIntelligenceBehaviours.GetCount();
		long	lNbDesignerVariables=g_clDsgVarList.GetCount();
	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	long	lNbSubrs = m_clListOfSubrs . GetCount ();
	//END ROMTEAM Cristi Petrescu 98-04-

		if(lNbReflexComport!=0 || lNbIntellComport!=0 || lNbDesignerVariables!=0 || lNbSubrs!=0)
		{
			//Alloc one AI Model
			p_stAIModel=fn_p_stAllocAIModel();

			if(lNbReflexComport!=0)
			{
				//Alloc Reflex Script IA
				AI_M_p_stGetReflexScriptAIModel(p_stAIModel)=fn_p_stAllocScriptAI(fn_ucGetDefaultActionTableNbEntries());
			}

			if(lNbIntellComport!=0)
			{
				//Alloc Intel Script IA
				AI_M_p_stGetIntelligenceScriptAIModel(p_stAIModel)=fn_p_stAllocScriptAI(fn_ucGetDefaultActionTableNbEntries());
			}

			if(lNbDesignerVariables!=0)
			{
				//Alloc Dsg Var
				//A CHANGER passer par une macro
				unsigned long	ulSizeOfAllDsgVar=g_clDsgVarList.m_fn_lGetSizeOfAllDesignerVariables();
				if(ulSizeOfAllDsgVar!=0)
					p_stAIModel->p_stDsgVar=fn_p_stAllocDsgVar(ulSizeOfAllDsgVar,(unsigned char)lNbDesignerVariables);
				else
					p_stAIModel->p_stDsgVar=NULL;
			}

//SHAITAN
      m_pclModel->mfn_vUpdateAIModel(p_stAIModel);
//SHAITAN

			//Alloc ia MS for all instances
			pos=clInstancesList.GetHeadPosition();
			POSITION	p=clDebugIsEnableList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor					*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject	*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();

				//Alloc ia MS and Mind structure
				fn_vBrainAlloc(pstEngineObject);

				//Sets AI Model pointer
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
				AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);

				AI_M_p_stGetAIModel(pstMind)=p_stAIModel;

        //SHAITAN
        pclInstance->mfn_vUpdateAIModel(p_stAIModel);
        //SHAITAN

				// CG PB DEBUGGER 23/06/98
				#ifdef ACTIVE_AIDEBUG
					//For Debugger
					if(p!=NULL)
						AIDebug_fn_vSetEnable(pstMind,clDebugIsEnableList.GetNext(p));
				#endif

				//Sets AI character name
				char *szCharacterName=fn_szFindPersonalTypeNameOfPersonalType(M_ObjectGetPersonalType(pstEngineObject));
				if(szCharacterName!=NULL)
					AI_M_SetPersoName(pstMind,szCharacterName);

				//Alloc Dsg Mem
				unsigned long	ulSizeOfAllDsgVar=g_clDsgVarList.m_fn_lGetSizeOfAllDesignerVariables();
				if(ulSizeOfAllDsgVar!=0)
				{
					AI_M_p_stGetDsgMem(pstMind)=fn_p_stAllocDsgMem(&AI_M_p_stGetDsgVar(pstMind),ulSizeOfAllDsgVar);
				  M_GetDsgMemBufferInit(pstMind)=fn_p_cAllocDsgMemBuffer(ulSizeOfAllDsgVar);
				}
				else
					AI_M_p_stGetDsgMem(pstMind)=NULL;
			}
		}
		else
		{
			//Sets h_Brain to null
			pos=clInstancesList.GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_Actor					*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
				tdstEngineObject	*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();

				pstEngineObject->h_Brain=NULL;
			}
		}

		//Translates declaration
		if(lNbDesignerVariables!=0)
		{
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl, FALSE);

			//For model
			if(!m_pclModel->m_fn_pclGetEditorModel()->m_clInstancesList.IsEmpty())
			{
				CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetHead();
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
				MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
				AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);

				bNoError=m_clListOfDeclarations.m_fn_bProcessIA(bProcessAll|bDeclDsgVarMustBeProcessed,TRUE,pstMind,NULL,DsgVarText,&m_clDebugInfoList);
			}

			if(bNoError)
			{
				//For instances
				pos=clInstancesList.GetHeadPosition();
				while(pos!=NULL)
				{
					CPA_Actor					*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
					tdstEngineObject	*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();

					if(bNoError)
					{
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);

					  memcpy(M_GetDsgMemBufferInit(pstMind),M_GetDsgMemDefaultInit(pstMind),M_GetDsgVarSize(pstMind));
					}
				}
			}
		}

	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if (bNoError)
	{
		//Translates (acknowledge) macros
		pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro, FALSE);
		// this should be same as for declarations
		// since i don't wanna change macro behaviour

		// For model
		// translates (ack) macros anyway, don't need instances for that...
		if(! m_pclModel->m_fn_pclGetEditorModel () -> m_clInstancesList.IsEmpty ())
		{
			CPA_Actor				*pclInstance = (CPA_Actor *) clInstancesList . GetHead ();
			tdstEngineObject		*pstEngineObject = (tdstEngineObject *) pclInstance -> GetData ();
			MS_tdxHandleToBrain		hBrain = pstEngineObject -> h_Brain;
      if ( hBrain != NULL ) {
		    AI_tdstMind				*pstMind = M_pstGetMindOfBrain (hBrain);

		    bNoError=m_clListOfMacros.m_fn_bProcessIA(bProcessAll,TRUE,pstMind,NULL,Macro_2_Text,&m_clDebugInfoList);
      }
		}
	}

	if (bNoError)
	{
		csMsg=csMsgBase+"(Subr)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
	
		if(lNbSubrs != 0)
		{
			tdstListOfMacro	*pstListOfSubrs;

			if(bNoError)
			{
				pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr, FALSE);

				//Alloc list of subrs
				pstListOfSubrs = fn_p_stAllocListOfMacro ((unsigned char) lNbSubrs);

				//Links subrs with model
				AI_M_SetListOfMacroAIModel (p_stAIModel, pstListOfSubrs);

				//Generates subrs data
				bNoError = m_clListOfSubrs . m_fn_bProcessIA (bProcessAll, 
															  FALSE, 
															  NULL,
															  (tdstComport *) pstListOfSubrs,
															  SubrText,
															  &m_clDebugInfoList);
			}
		}
	}
	//END ROMTEAM Cristi Petrescu 98-04-

	if(bNoError)
		{
			//Translates reflex
			csMsg=csMsgBase+"(Reflex)";
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

			if(lNbReflexComport!=0)
			{
				tdstComport	*pstReflexComport;

				if(bNoError)
				{
					pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditReflex, FALSE);

					lNbReflexComport=m_clListOfReflexBehaviours.GetCount();
					if(lNbReflexComport!=0)
					{
						//Alloc behaviours of reflex
						pstReflexComport=fn_p_stAllocComport((unsigned char)lNbReflexComport);

						//Links reflex with model
						M_GetScriptAIComport(AI_M_p_stGetReflexScriptAIModel(p_stAIModel))=pstReflexComport;

						//Generates reflex data
						bNoError=m_clListOfReflexBehaviours.m_fn_bProcessIA(bProcessAll,FALSE,NULL,pstReflexComport,ReflexText,&m_clDebugInfoList);
					}
				}

				//Links reflex with reflex script and alloc/init actions table for all instances
				pos=clInstancesList.GetHeadPosition();
				while(pos!=NULL && bNoError)
				{
					CPA_Actor					*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
					tdstEngineObject	*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();

					if(lNbReflexComport!=0 && bNoError)
					{
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
						AI_tdstAIModel			*pstModel=AI_M_p_stGetAIModel(pstMind);
						tdstScriptAI				**ppstReflex=&(AI_M_p_stGetReflexScriptAIModel(pstModel));

						//Gets reflex pointer
						tdstScriptAI	*pstReflex=AI_M_p_stGetReflexScriptAIModel(pstModel);

						//Sets Reflex ScriptAI pointer
						M_GetScriptAIComport(pstReflex)=pstReflexComport;
						M_GetScriptAINbComport(pstReflex)=(unsigned char)lNbReflexComport;

						//Links reflex with reflex script
						AI_M_SetReflex(
													 pstMind,
													 fn_p_stAllocIntelligence(
																										ppstReflex,
																										M_GetScriptAIActionTableNbEntry(*ppstReflex)
																									 )
													);
					}
				}
			}
		}

		//Translates intelligence
		if(bNoError)
		{
			csMsg=csMsgBase+"(Intelligence)";
			fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

			if(lNbIntellComport!=0)
			{
				tdstComport	*pstIntellComport;

				if(bNoError)
				{
					pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditIntell, FALSE);

					lNbIntellComport=m_clListOfIntelligenceBehaviours.GetCount();
					if(lNbIntellComport!=0)
					{
						//Alloc behaviours of intelligence
						pstIntellComport=fn_p_stAllocComport((unsigned char)lNbIntellComport);

						//Links intelligence with model
						M_GetScriptAIComport(AI_M_p_stGetIntelligenceScriptAIModel(p_stAIModel))=pstIntellComport;

						//Generates intelligence data
						bNoError=m_clListOfIntelligenceBehaviours.m_fn_bProcessIA(bProcessAll,FALSE,NULL,pstIntellComport,IntelligenceText,&m_clDebugInfoList);
					}
				}

				//Links intelligence with intelligence script and alloc/init actions table for all instances
				pos=clInstancesList.GetHeadPosition();
				while(pos!=NULL && bNoError)
				{
					CPA_Actor					*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
					tdstEngineObject	*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();

					if(lNbIntellComport!=0 && bNoError)
					{
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
						AI_tdstAIModel			*pstModel=AI_M_p_stGetAIModel(pstMind);
						tdstScriptAI				**ppstIntelligence=&(AI_M_p_stGetIntelligenceScriptAIModel(pstModel));

						//Gets intelligence pointer
						tdstScriptAI	*pstIntelligence=AI_M_p_stGetIntelligenceScriptAIModel(pstModel);
					
						//Sets instellience ScriptAI pointer
						M_GetScriptAIComport(pstIntelligence)=pstIntellComport;
						M_GetScriptAINbComport(pstIntelligence)=(unsigned char)lNbIntellComport;

						//Links intelligence with intelligence script
						AI_M_SetIntelligence(
																 pstMind,
																 fn_p_stAllocIntelligence(
																													ppstIntelligence,
																													M_GetScriptAIActionTableNbEntry(*ppstIntelligence)
																												 )
																);
					}
				}
			}
		}

		if(bNoError)
		{
			m_fn_vSetModified(FALSE);

			//For Reinit comport in "CPA_EdIR_Frame::m_fn_vBeforeEngine"
			m_fn_vSetReInitBehaviourFlag(TRUE);

			if((lNbReflexComport!=0) ||
				 (lNbIntellComport!=0) ||
				 ((lNbDesignerVariables!=0) && (!clInstancesList.IsEmpty())))
			{
				if(bProcessAll || bDeclDsgVarMustBeProcessed)
				{
					m_fn_vCreateInitControlsForBehaviour(p_stAIModel);
					m_fn_vCreateControlsForBehaviour();
				}
				else
				{
// CPA2 ASilvescu 98-05-28
//						m_fn_vUpdateInitControlsForBehaviour(p_stAIModel);
//						m_fn_vUpdateControlsForBehaviour();
					CTL_Editor_DataList	*pclListOfDesignerVariableType=m_pclModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
					CTL_Editor_Data	*pclData=pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialIABehaviour);
					if(pclData==NULL)
					{
						m_fn_vCreateInitControlsForBehaviour(p_stAIModel);
						m_fn_vCreateControlsForBehaviour();
					}
					else
					{
						m_fn_vUpdateInitControlsForBehaviour(p_stAIModel);
						m_fn_vUpdateControlsForBehaviour();
					}
// End CPA2 ASilvescu 98-05-28
				}

				if(lNbDesignerVariables!=0)
				{
					//Init current dsg var values with init dsg var values (For all instances)
					pos=clInstancesList.GetHeadPosition();
					while(pos!=NULL)
					{
						CPA_Actor						*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);
						tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetData();
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
						AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);

// ANNECY MT - 11/09/98 {
						fn_vSecondPassOfDsgMem(pstMind,FALSE,OTI_MapLoaded/* ??? */);
// END ANNECY MT }
						AI_M_SetSecondPassDone(AI_M_p_stGetAIModel(pstMind),1);

						//Update data in control
						CTL_Editor_DataList							*pclListOfDesignerVariableType=pclInstance->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
						CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
						CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);
						pclDsgVarView->m_fn_vUpdateValues(pclListOfDesignerVariableType);
					}
				}

				m_fn_vUpdateDatasWithOldValues();

				//Updates designer variable view
				CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
				CPA_EdIR_DesignerVariablesView	*pclDsgVarView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);
				EdActors_EditorActor						*pclSelectedActor;

				if(pclActor!=NULL)
				{
					pclSelectedActor=pclActor->m_fn_pclGetEditorActor();
					CTL_Editor_DataList	*pclListOfDesignerVariableType=pclSelectedActor->m_pclListOfDesignerVariableType;
					pclDsgVarView->m_fn_vUpdateDisplayWithValues(pclListOfDesignerVariableType);
				}
			}
			//Sets current state
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(tdeOldState, FALSE);

			//Sets current brain
			pclIRMainFrame->m_fn_pclUpdateBrain(pclOldBrain,TRUE,FALSE);

			if(!m_bDsgVarControlsAreCreated)
				m_bDsgVarControlsAreCreated=(!clInstancesList.IsEmpty());
		}
	}

	m_bOldApplicationIsOK=bNoError;
	if(bNoError)
	{
		//csMsg=csMsgBase+"OK (Good Work !!)";
		csMsg=csMsgBase+"OK (ça vaaaaa 3lik !!)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

		m_bAlreadyProcessed=TRUE;
	}
	else
	{
		csMsg=csMsgBase+"KO :-( (Bad Boy)";
		fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
	}

	pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vEnableButtons(bOldButtonsState);

//ANNECY CB
	AfxGetApp()->DoWaitCursor(-1);
	g_pclAIInterface->m_bIsProcessing = FALSE;

	ReleaseCapture();
//	AfxGetApp()->DoWaitCursor(0);
//END ANNECY
	return bNoError;
}

/****************************************************************************/
void *CPA_EdIR_Brain::m_fn_pvGetStateHandle(CString csActionName)
{
	CPA_List<CPA_Action> *pclActionList=&(m_pclModel->m_pub_fn_pclGetFamily()->m_oListOfActions);
//ANNECY CB
	LPCSTR	lpcs = (LPCSTR) csActionName;
//END
	POSITION	pos=pclActionList->GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_Action	*pclAction=pclActionList->GetNext(pos);

//ANNECY CB
		if(!strcmpi(lpcs, pclAction->GetName()))
//		if(csActionName.CompareNoCase(CString(pclAction->GetName()))==0)
//END
		{
			if(pclAction->mfn_p_oGetDefaultState()!=NULL)
			{
				return pclAction->mfn_p_oGetDefaultState()->GetData();
			}
			else
			{
				AfxMessageBox("The action \""+pclAction->GetName()+"\" has no Initial State !!!!");

				return NULL;
			}
		}
	}

	return NULL;
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vGetActionList(CStringList *p_cslActionList)
{
	CPA_List<CPA_Action> *pclActionList=&(m_pclModel->m_pub_fn_pclGetFamily()->m_oListOfActions);

	POSITION	pos=pclActionList->GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_Action	*pclAction=pclActionList->GetNext(pos);

		p_cslActionList->AddTail(CString(pclAction->GetName()));
	}
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bOldApplicationIsOK()
{
	return m_bOldApplicationIsOK;
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bReInitBehaviourIsNeeded()
{
	return m_bMustReInitBehaviour;
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vSetReInitBehaviourFlag(BOOL bMustReInitBehaviour)
{
	m_bMustReInitBehaviour=bMustReInitBehaviour;
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bHasBeenModified()
{
	BOOL	bHasBeenModified=FALSE;

	bHasBeenModified=bHasBeenModified || m_clListOfIntelligenceBehaviours.m_fn_bHasBeenModified();
	bHasBeenModified=bHasBeenModified || m_clListOfReflexBehaviours.m_fn_bHasBeenModified();
	bHasBeenModified=bHasBeenModified || m_clListOfDeclarations.m_fn_bHasBeenModified();
	// BEGIN CPA2 Cristi Petrescu 98-03-
	bHasBeenModified=bHasBeenModified || m_clListOfMacros.m_fn_bHasBeenModified();
	// END CPA2 Cristi Petrescu 98-03-
	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	bHasBeenModified=bHasBeenModified || m_clListOfSubrs.m_fn_bHasBeenModified();
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-

	return (m_bHasBeenModified||bHasBeenModified);
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vSetModified(BOOL bIsModified)
{
	m_clListOfIntelligenceBehaviours.m_fn_vSetModified(bIsModified);
	m_clListOfReflexBehaviours.m_fn_vSetModified(bIsModified);
	m_clListOfDeclarations.m_fn_vSetModified(bIsModified);
	// BEGIN CPA2 Cristi Petrescu 98-03-
	m_clListOfMacros.m_fn_vSetModified(bIsModified);
	// END CPA2 Cristi Petrescu 98-03-
	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	m_clListOfSubrs.m_fn_vSetModified(bIsModified);
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-
	m_bHasBeenModified=bIsModified;
}

/****************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bIAIsAlreadyProcessed()
{
	return m_bAlreadyProcessed;
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vNotify()
{
	//Notifications
	CString					csFamilyName=m_fn_pclGetModel()->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=m_fn_pclGetModel()->GetName();
	CString					csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString					csEngineSectionName;
	CString					csEditorSectionName;

	//Notifies Engine script (.car) for all instances
	CPA_List<CPA_BaseObject> clInstancesList;
	g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_fn_pclGetModel());
	POSITION	pos=clInstancesList.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_Actor	*pclInstance=(CPA_Actor *)clInstancesList.GetNext(pos);

		pclInstance->fn_vNotifySave();
	}

	//Notifies Editor model script
	m_fn_pclGetModel()->fn_vNotifySave();

	//Notifies Engine AI script
	csEngineSectionName=csPathName+csModelName+g_c_csAIFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEngineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAI,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//Notifies Engine AI declaration script
	csEngineSectionName=csPathName+csModelName+g_c_csAIDecFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEngineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAIDeclaration,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//Notifies Engine AI rule script
	csEngineSectionName=csPathName+csModelName+g_c_csAIRulFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEngineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAIRules,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//Notifies Engine AI reflex script
	csEngineSectionName=csPathName+csModelName+g_c_csAIRfxFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEngineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAIReflexes,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	//Notifies Engine AI reflex script
	csEngineSectionName=csPathName+csModelName+g_c_csAISubrFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEngineSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEngineAISubrs,
															 this,
															 SCR_EA_Ntfy_RebuildSection);
	//END ROMTEAM Cristi Petrescu 98-04-

	//Notifies Editor AI declaration script
	csEditorSectionName=csPathName+csModelName+g_c_csDeclarationFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEditorSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEditorAIDeclaration,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	// BEGIN CPA2 Cristi Petrescu 98-03-
	//Notifies Editor AI macro script
	csEditorSectionName=csPathName+csModelName+g_c_csMacroFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEditorSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEditorAIMacros,
															 this,
															 SCR_EA_Ntfy_RebuildSection);
	// END CPA2 Cristi Petrescu 98-03-
	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	//Notifies Editor AI Subr script
	csEditorSectionName=csPathName+csModelName+g_c_csSubrFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEditorSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEditorAISubrs,
															 this,
															 SCR_EA_Ntfy_RebuildSection);
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-
	//Notifies Editor AI rule script
	csEditorSectionName=csPathName+csModelName+g_c_csIntelligenceFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEditorSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEditorAIRules,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//Notifies Editor AI reflex script
	csEditorSectionName=csPathName+csModelName+g_c_csReflexFileExtension;
	SCR_fn_v_SvL1_RegisterNotify(M_MAKECHAR(csEditorSectionName),
															 CPA_EdIR_Brain::m_fn_vSaveEditorAIReflexes,
															 this,
															 SCR_EA_Ntfy_RebuildSection);

	//Adds this brain into the list of modified brain
	CPA_EdIR_Frame	*pclMainFrame=g_pclAIInterface->m_pclIRMainFrame;
	pclMainFrame->m_fn_vAddOneModifiedBrain(this);
}

/****************************************************************************/
CString CPA_EdIR_Brain::m_fn_csGetDsgVarName(long lDsgVarIndex)
{
	CString	csIn;

	if(!m_clListOfDeclarations.IsEmpty())
	{
		//Gets Designer Variable MainBehaviour (first element in the list)
		CPA_EdIR_MainBehaviour *pclMainBehaviour=m_clListOfDeclarations.GetHead();

		//Gets text declaraition
		if(pclMainBehaviour->m_pclBehaviour!=NULL)
			csIn=pclMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
		else
			csIn=pclMainBehaviour->m_csText;

		csIn=fn_csGetCodeWithoutComments(csIn);

		//Looks for dsg var in this text
		long		lCurrentIndex=0;
		long		lCurrentDsgVarIndex=0;
		CString	csWord=fn_csGetNextWord(csIn,lCurrentIndex);
		CString	csName;
		CString	csType=csWord;
		while(!csWord.IsEmpty() && lCurrentDsgVarIndex<=lDsgVarIndex)
		{
			//Gets Type
			tdeDsgVarTypeId	eDsgVarTypeId=(tdeDsgVarTypeId)fn_lGetDsgVarTypeIdFromEditorName(csWord);
			csType=csWord;

			//Gets Name
			csName=fn_csGetNextWord(csIn,lCurrentIndex);

			//Skips Value
			fn_csGetNextWord(csIn,lCurrentIndex);

			if(eDsgVarTypeId==eDsgVarType_Vector)
			{
				//Skips x,y,z components
				fn_csGetNextWord(csIn,lCurrentIndex);
				fn_csGetNextWord(csIn,lCurrentIndex);
				fn_csGetNextWord(csIn,lCurrentIndex);
			}

			//Gets next word
			csWord=fn_csGetNextWord(csIn,lCurrentIndex);

			lCurrentDsgVarIndex++;
		}

		if(lCurrentDsgVarIndex==lDsgVarIndex+1)
			return csName+" <"+csType+">";
		else
			return "";
	}
	else
		return "";
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vShowBehaviour(tdstNodeInterpret *pstNode)
{
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	CPA_EdIR_Behaviour *pclBehaviour;
	CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;

	if (pstNode)
	{
		CPA_EdIR_DebugInfo *pclDebugInfo = m_clDebugInfoList . m_fn_pclGetDebugInfo (pstNode);
	//END ROMTEAM Cristi Petrescu 98-05-
	CString csBehaviourName= pclDebugInfo -> m_fn_csGetBehaviourName ();
	long lMacroIndex = pclDebugInfo -> m_fn_lGetMacroIndex ();
	CPA_EdIR_MainBehaviourList	*pclBehaviourList=NULL;
	if (lMacroIndex >= 0)
	{
		csBehaviourName = pclDebugInfo -> m_fn_csGetMacroName ();
		pclBehaviourList = & m_clListOfMacros;
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=pclBehaviourList->m_fn_pclGetMainBehaviour(csBehaviourName);
		if (! pclMainBehaviour)// do not overdo
			//|| pclMainBehaviour -> m_pclBehaviour -> m_bOldMacro)
		{
			csBehaviourName= pclDebugInfo -> m_fn_csGetBehaviourName ();
			lMacroIndex = -1;
		}
	}
	
	if (lMacroIndex < 0)
	{
		if(m_clListOfIntelligenceBehaviours.m_fn_bIsInList(csBehaviourName))
		{
			pclBehaviourList=&m_clListOfIntelligenceBehaviours;
		}
		else
		if(m_clListOfReflexBehaviours.m_fn_bIsInList(csBehaviourName))
		{
			pclBehaviourList=&m_clListOfReflexBehaviours;
		}
	}

	if(pclBehaviourList!=NULL)
	{
		CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;

		//Shows current actor
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_DisplayActor,(long)m_pclModel);

		//Shows intelligence editor
		g_pclAIInterface->m_fn_vShowEditor();

		//Sets current state
		if(pclBehaviourList==&m_clListOfIntelligenceBehaviours)
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditIntell);
		else
			if (pclBehaviourList == & m_clListOfReflexBehaviours)
				pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditReflex);
			else
				pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);

		//Shows the good behaviour
		
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=pclBehaviourList->m_fn_pclGetMainBehaviour(csBehaviourName);
			//BEGIN ROMTEAM Cristi Petrescu 98-05-
			pclBehaviour=pclMainBehaviour->m_pclBehaviour;
			//END ROMTEAM Cristi Petrescu 98-05-
			ASSERT(pclBehaviour!=NULL);
			pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_vOldActiveBehaviourHasChanged(pclBehaviour);

			//BEGIN ROMTEAM Cristi Petrescu 98-05-
			// mark the executed lines
			pclIRMainFrame -> m_fn_pclGetEdRulesView () -> m_fn_vSetDebugInfoList (& pclMainBehaviour -> m_clDebugInfoList);
			//END ROMTEAM Cristi Petrescu 98-05-

			//Shows the good line
			//BEGIN ROMTEAM Cristi Petrescu 98-05-
			long	lLineNumber = pclDebugInfo -> m_fn_lGetLineNumber ();
			if (lMacroIndex < 0)
			{
				pclBehaviour -> m_fn_vSetCursorPosByLine (lLineNumber);
				pclIRMainFrame -> m_fn_pclGetEdRulesView () -> m_lExecutedLine = pclDebugInfo -> m_fn_lGetIndex ();
			}
			else
			{
				//pclBehaviour -> m_fn_vSetCursorPosByIndex (lMacroIndex);
				pclIRMainFrame -> m_fn_pclGetEdRulesView () -> m_lExecutedLine = 0;
			}
			//END ROMTEAM Cristi Petrescu 98-05-
		}
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	}
	else
	{
		//the old behaviour
		pclBehaviour = pclIRMainFrame -> m_fn_pclGetDiagrammeView () -> m_fn_pclGetActiveBehaviour ();	
	}

	if (pclBehaviour)
	{
		// hide lines if necessary
		pclBehaviour -> m_fn_vHideNonExecutedLines ();

		// redraw marks
		pclIRMainFrame -> m_fn_pclGetEdRulesView () -> InvalidateRect (NULL);
	}
	//END ROMTEAM Cristi Petrescu 98-05-
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vShowBehaviour(CString csBehaviourName)
{
	CPA_EdIR_MainBehaviourList	*pclBehaviourList=NULL;
	if(m_clListOfIntelligenceBehaviours.m_fn_bIsInList(csBehaviourName))
	{
		pclBehaviourList=&m_clListOfIntelligenceBehaviours;
	}
	else
	if(m_clListOfReflexBehaviours.m_fn_bIsInList(csBehaviourName))
	{
		pclBehaviourList=&m_clListOfReflexBehaviours;
	}

	if(pclBehaviourList!=NULL)
	{
		CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;

		//Shows current actor
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_DisplayActor,(long)m_pclModel);

		//Shows intelligence editor
		g_pclAIInterface->m_fn_vShowEditor();

		//Sets current state
		if(pclBehaviourList==&m_clListOfIntelligenceBehaviours)
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditIntell);
		else
			pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditReflex);

		//Shows the good behaviour
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=pclBehaviourList->m_fn_pclGetMainBehaviour(csBehaviourName);
		CPA_EdIR_Behaviour			*pclBehaviour=pclMainBehaviour->m_pclBehaviour;
		ASSERT(pclBehaviour!=NULL);
		pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_vOldActiveBehaviourHasChanged(pclBehaviour);

		// mark the executed lines
		pclIRMainFrame -> m_fn_pclGetEdRulesView () -> m_fn_vSetDebugInfoList (& pclMainBehaviour -> m_clDebugInfoList);

		/*
		//Shows the good line
		long	lIndex=m_clDebugInfoList.m_fn_lGetIndex(pstNode);
		pclBehaviour->m_fn_vSetCursorPos(lIndex);
		*/

		// hide lines if necessary
		pclBehaviour -> m_fn_vHideNonExecutedLines ();

		pclIRMainFrame -> m_fn_pclGetEdRulesView () -> m_lExecutedLine = -1;

		// redraw marks
		pclIRMainFrame -> m_fn_pclGetEdRulesView () -> InvalidateRect (NULL);
	}
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vMustBuildDebugInfo (BOOL bBuild)
{
	m_bMustBuildDebugInfo = bBuild;
}

/****************************************************************************/
CString CPA_EdIR_Brain::m_fn_csGetBehaviourName(tdstNodeInterpret *pstNode)
{
	return m_clDebugInfoList . m_fn_csGetBehaviourName (pstNode);
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vClearExecutedNodes (CString csBehaviourName)
{
	CPA_EdIR_MainBehaviour *pclMainBehaviour = m_clListOfIntelligenceBehaviours . m_fn_pclGetMainBehaviour (csBehaviourName);
	if (! pclMainBehaviour)
	{
		pclMainBehaviour = m_clListOfReflexBehaviours . m_fn_pclGetMainBehaviour (csBehaviourName);
	}
	if (pclMainBehaviour)
	{
		pclMainBehaviour -> m_fn_vClearExecutedNodes ();
	}
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vSetOneExecutedNode (tdstNodeInterpret *pstNode)
{
	CPA_EdIR_DebugInfo *pclDebugInfo = m_clDebugInfoList . m_fn_pclGetDebugInfo (pstNode);

	if (pclDebugInfo)
	{
		pclDebugInfo -> m_fn_vSetExecuted (TRUE);

		// and, as we do this it means that the Behaviour should be redisplayed...
		CString csBehaviourName = pclDebugInfo -> m_fn_csGetBehaviourName ();

		CPA_EdIR_MainBehaviour *pclMainBehaviour;

		pclMainBehaviour = m_clListOfIntelligenceBehaviours . m_fn_pclGetMainBehaviour (csBehaviourName);
		if (! pclMainBehaviour)
		{
			pclMainBehaviour = m_clListOfReflexBehaviours . m_fn_pclGetMainBehaviour (csBehaviourName);
		}
		if (pclMainBehaviour)
		{
			pclMainBehaviour -> m_pclBehaviour -> m_bFirstActivate = TRUE;
		}
	}
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateDebugInfo (void)
{
	// this is called to build the debug info for TID usage
	if (m_bMustBuildDebugInfo && m_clDebugInfoList . IsEmpty ())
	{
		//force full process
		m_fn_bProcessIA (TRUE);
	}

	// anyway, clean up the executed flag
	POSITION pos = m_clDebugInfoList . GetHeadPosition ();
	CPA_EdIR_DebugInfo *pclDebugInfo;
	while (pos)
	{
		pclDebugInfo = m_clDebugInfoList . GetNext (pos);

		pclDebugInfo -> m_fn_vSetExecuted (FALSE);
	}
}
//END ROMTEAM Cristi Petrescu 98-05-

// BEGIN ROMTEAM Cristi Petrescu 98-06-
//****************************************************************************
long CPA_EdIR_Brain::m_fn_lGetVariableIndex (CString csVariableName)
{
	CString	csIn;
	csVariableName . MakeLower ();

	if (! m_clListOfDeclarations . IsEmpty ())
	{
		//Gets Designer Variable MainBehaviour (first element in the list)
		CPA_EdIR_MainBehaviour * pclMainBehaviour = m_clListOfDeclarations . GetHead ();

		//Gets text declaraition
		if (pclMainBehaviour -> m_pclBehaviour != NULL)
			csIn = pclMainBehaviour -> m_pclBehaviour -> m_fn_csGetRules ();
		else
			csIn = pclMainBehaviour ->  m_csText;

		csIn = fn_csGetCodeWithoutComments (csIn);

		//Looks for dsg var in this text
		long		lCurrentIndex = 0;
		long		lCurrentDsgVarIndex = 0;
		CString	csWord = fn_csGetNextWord (csIn, lCurrentIndex);
		CString	csName;
		CString	csType = csWord;
		while(! csWord . IsEmpty ())
		{
			//Gets Type
			tdeDsgVarTypeId	eDsgVarTypeId = (tdeDsgVarTypeId) fn_lGetDsgVarTypeIdFromEditorName (csWord);
			csType = csWord;

			//Gets Name
			csName = fn_csGetNextWord (csIn, lCurrentIndex);
			csName . MakeLower ();

			if (csName == csVariableName)
				return lCurrentDsgVarIndex;

			//Skips Value
			fn_csGetNextWord (csIn,lCurrentIndex);

			if( eDsgVarTypeId == eDsgVarType_Vector)
			{
				//Skips x,y,z components
				fn_csGetNextWord (csIn, lCurrentIndex);
				fn_csGetNextWord (csIn, lCurrentIndex);
				fn_csGetNextWord (csIn, lCurrentIndex);
			}

			//Gets next word
			csWord = fn_csGetNextWord (csIn, lCurrentIndex);

			lCurrentDsgVarIndex++;
		}
	}
	
	return -1;
}
// END ROMTEAM Cristi Petrescu 98-06-

/****************************************************************************/
CPA_EdIR_BreakPointList	*CPA_EdIR_Brain::m_fn_pclGetBreakPointList()
{
	return &m_clBreakPointList;
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vSetOrRemoveOneBreakPoint(long lIndex,CString csBehaviourName)
{
	if(m_clBreakPointList.m_fn_bIsInList(lIndex,csBehaviourName))
		m_clBreakPointList.m_fn_vRemoveOneBreakPoint(lIndex,csBehaviourName);
	else
		m_clBreakPointList.m_fn_vSetOneBreakPoint(lIndex,csBehaviourName);
}

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vSetEngineBreakPoints(BOOL bSetBreakPoint)
{
	if(bSetBreakPoint==FALSE)
	{
		//Removes BreakPoint
		POSITION	pos=m_clBreakPointList.GetHeadPosition();
		while(pos!=NULL)
		{
			tdstNodeInterpret *pstNode=m_clBreakPointList.GetNext(pos)->m_fn_pstGetNode();

				// CG PB DEBUGGER 23/06/98
				#ifdef ACTIVE_AIDEBUG
					if(pstNode!=NULL)
						M_SetBreakPoint(pstNode,FALSE);
				#endif
		}
	}
	else
	{
		CPA_EdIR_BreakPointList	clBreakPointListToRemove;

		//Sets BreakPoint
		POSITION	pos=m_clBreakPointList.GetHeadPosition();
		while(pos!=NULL)
		{
			CPA_EdIR_BreakPoint	*pclBreakPoint=m_clBreakPointList.GetNext(pos);
			CString							csBehaviourName=pclBreakPoint->m_fn_csGetBehaviourName();
			long								lIndex=pclBreakPoint->m_fn_lGetIndex();

			tdstNodeInterpret *pstNode=m_clDebugInfoList.m_fn_pstGetNode(lIndex,csBehaviourName);

			if(pstNode!=NULL)
			{
				long												lBehaviourIndex=-1;
				CPA_EdIR_MainBehaviourList	*pclBehaviourList=NULL;
				if(m_clListOfIntelligenceBehaviours.m_fn_bIsInList(csBehaviourName))
				{
					pclBehaviourList=&m_clListOfIntelligenceBehaviours;
				}
				else
				if(m_clListOfReflexBehaviours.m_fn_bIsInList(csBehaviourName))
				{
					pclBehaviourList=&m_clListOfReflexBehaviours;
				}

				if(pclBehaviourList!=NULL)
					lBehaviourIndex=pclBehaviourList->m_fn_lGetIndexOfABehaviour(csBehaviourName);

				if(lBehaviourIndex!=-1)
				{
					CPA_List<CPA_BaseObject> clInstancesList;
					g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,m_pclModel);

					POSITION	pos=clInstancesList.GetHeadPosition();
					if(pos!=NULL)
					{
						CPA_Actor						*pclInstance=(CPA_Actor *)(clInstancesList.GetAt(pos));
						tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
						MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

						if(hBrain!=NULL)
						{
							AI_tdstMind	*pstMind=M_pstGetMindOfBrain(hBrain);

							if(pstMind!=NULL)
							{
								AI_tdstAIModel *pstAIModel=AI_M_p_stGetAIModel(pstMind);

								if(pstAIModel!=NULL)
								{
									tdstScriptAI	*pstScriptAI;
									if(pclBehaviourList==&m_clListOfIntelligenceBehaviours)
										pstScriptAI=AI_M_p_stGetIntelligenceScriptAIModel(pstAIModel);
									else
										pstScriptAI=AI_M_p_stGetReflexScriptAIModel(pstAIModel);


								// CG PB DEBUGGER 23/06/98 {
								#ifdef ACTIVE_AIDEBUG
									if(pstScriptAI!=NULL)
									{
										tdstNodeInterpret	*pstNewNode=fn_pstSetValidBreakPoint(pstNode,pstScriptAI,lBehaviourIndex);

										//Update breakpoint
										CPA_EdIR_MainBehaviour	*pclMainBehaviour=pclBehaviourList->m_fn_pclGetMainBehaviour(csBehaviourName);
										CPA_EdIR_Behaviour			*pclBehaviour=pclMainBehaviour->m_pclBehaviour;
										long										lIndex=m_clDebugInfoList.m_fn_lGetIndex(pstNewNode);
										lIndex=pclBehaviour->m_fn_lGetStartLineIndex(lIndex);

										pclBreakPoint->m_fn_vSetIndex(lIndex);
										pclBreakPoint->m_fn_vSetNode(pstNewNode);

										if(m_clBreakPointList.m_fn_bExistMoreThanOnce(pclBreakPoint))
											clBreakPointListToRemove.AddTail(pclBreakPoint);
									}
								#endif // } CG PB DEBUGGER 23/06/98
								}
							}
						}
					}
				}
			}
		}

		//Removes break point (multiple break point on the same location)
		pos=clBreakPointListToRemove.GetHeadPosition();
		while(pos!=NULL)
		{
			CPA_EdIR_BreakPoint	*pclBreakPoint=clBreakPointListToRemove.GetNext(pos);

			m_clBreakPointList.m_fn_vRemoveOneBreakPoint(pclBreakPoint->m_fn_lGetIndex(),pclBreakPoint->m_fn_csGetBehaviourName());
		}
		clBreakPointListToRemove.RemoveAll();
	}
}

/****************************************************************************/
CPA_EdIR_DebugInfoList *CPA_EdIR_Brain::m_fn_pclGetDebugInfoList(void) {
  return ( &m_clDebugInfoList ) ;
}


// Shaitan => Macros to subroutines

/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vChangeAllEditorMacrosToSubroutines (void)
{
	CPA_EdIR_MainBehaviour	*pMacroMainBehaviour;
	CPA_EdIR_MainBehaviour	*pSubrMainBehaviour;
	CStringList				csListOfMacros;
	CStringList				csListOfSubrs;
	POSITION				pos;
	CRect					crRect(10, 10, 100, 40);
	long					lNbSubroutines;

	// first update macros lists
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	
	// get initial rectangle
	if (!m_clListOfSubrs.IsEmpty()) 
	{
		pSubrMainBehaviour = m_clListOfSubrs.GetTail();
		crRect = pSubrMainBehaviour->m_pclBehaviour->m_fn_crGetBoundingBox();
		crRect.OffsetRect(0,50);
	}

	// first convert the macro list
	pMacroMainBehaviour = m_clListOfDeclarations.GetTail();
	lNbSubroutines = m_fn_lCreateEditorSubroutinesFromMacro(pMacroMainBehaviour, crRect, &csListOfMacros, &csListOfSubrs);
	crRect.OffsetRect(0, 50*lNbSubroutines);
	
	// then convert all the macro behaviours
	pos = m_clListOfMacros.GetHeadPosition();
	while (pos)
	{
		// get the editor text of the macro behaviour
		pMacroMainBehaviour = m_clListOfMacros.GetNext(pos);
		lNbSubroutines = m_fn_lCreateEditorSubroutinesFromMacro(pMacroMainBehaviour, crRect, &csListOfMacros, &csListOfSubrs);
		crRect.OffsetRect(0, 50*lNbSubroutines);
	}

	// replace macros calls with subroutines calls
	m_fn_vUpdateEditorMacroCallsInEditorAI(&csListOfMacros, &csListOfSubrs);

	// remove the macros
	m_fn_vRemoveAllMacrosFromEditorAI();

	// update the display of the subroutines
	m_fn_vUpdateEditorSubroutines();
}


/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vRemoveAllMacrosFromEditorAI (void)
{
	CPA_EdIR_MainBehaviour	*pMacroMainBehaviour;

	// remove all macros of the list
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	pMacroMainBehaviour = m_clListOfDeclarations.GetTail();
	pMacroMainBehaviour->m_pclBehaviour->m_fn_vSetRules("");
	pMacroMainBehaviour->m_csText = "";
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);

	// remove all macro behaviours
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_bDeleteAllBehavioursFromList();
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
}


/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateEditorSubroutines (void)
{
	CPA_EdIR_MainBehaviour	*pSubrMainBehaviour;
	POSITION				pos;

	// update subroutines panel
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);

	// update each subroutine
	pos = m_clListOfSubrs.GetHeadPosition();
	while (pos)
	{
		// update the text of the subroutine
		pSubrMainBehaviour = m_clListOfMacros.GetNext(pos);
		pSubrMainBehaviour->m_pclBehaviour->m_clEditCtrl.m_fn_vColorText(0, pSubrMainBehaviour->m_pclBehaviour->m_clEditCtrl.GetTextLength());
	}
}


/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vChangeOneEditorMacroToSubroutines (CPA_EdIR_MainBehaviour *pMacroMainBehaviour)
{
	CPA_EdIR_MainBehaviour	*pSubrMainBehaviour;
	CStringList				csListOfMacros;
	CStringList				csListOfSubrs;
	CRect					crRect(10, 10, 100, 40);

	// first update macros lists
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	
	// get initial rectangle
	if (!m_clListOfSubrs.IsEmpty()) 
	{
		pSubrMainBehaviour = m_clListOfSubrs.GetTail();
		crRect = pSubrMainBehaviour->m_pclBehaviour->m_fn_crGetBoundingBox();
		crRect.OffsetRect(0,50);
	}

	m_fn_lCreateEditorSubroutinesFromMacro(pMacroMainBehaviour, crRect, &csListOfMacros, &csListOfSubrs);

	// replace macros calls with subroutines calls
	m_fn_vUpdateEditorMacroCallsInEditorAI(&csListOfMacros, &csListOfSubrs);

	// remove the macros
	m_fn_vRemoveOneMacroFromEditorAI(pMacroMainBehaviour);

	// update subroutines panel
	m_fn_vUpdateEditorSubroutines();
}


/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vRemoveOneMacroFromEditorAI (CPA_EdIR_MainBehaviour *pMacroMainBehaviour)
{
	CPA_EdIR_MainBehaviour *pListMainBehaviour;
	
	// check if the behaviour is the macro list
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditDecl);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	pListMainBehaviour = m_clListOfDeclarations.GetTail();
	if (pListMainBehaviour == pMacroMainBehaviour)
	{
		pMacroMainBehaviour->m_pclBehaviour->m_fn_vSetRules("");
		pMacroMainBehaviour->m_csText = "";
		g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	}
	// otherwise remove macro behaviou
	else
	{
		// remove all macro behaviours
		g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditMacro);
		g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
		g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_bDeleteBehaviourFromList(pMacroMainBehaviour);
		g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	}
}


/**********************************************************************************/
long CPA_EdIR_Brain::m_fn_lCreateEditorSubroutinesFromMacro (CPA_EdIR_MainBehaviour	*pMacroMainBehaviour, CRect crTitleRect, CStringList *pListOfMacros, CStringList *pListOfSubrs)
{
	CPA_EdIR_MainBehaviour	*pSubrMainBehaviour;
	CPA_EdIR_Behaviour		*pSubrBehaviour;
	CStringList				csListOfDefines;
	CString					csRules, csLowRules, csMacro;
	CString					csMacroName, csMacroText;
	CString					csSubrName, csSubrText;
	CString					csCommentLines;
	long					lNbSubroutines = 0;
	int						iMacroIndex, iNameIndex;
	int						iSubrIndex;

	// first register the local defines
	m_fn_vRegisterEditorDefinesForMacro(pMacroMainBehaviour, &csListOfDefines);

	// for the search we need a lower-case copy
	csRules = pMacroMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
	csLowRules = csRules;
	csLowRules.MakeLower();

	// get the first macro in the text
	iMacroIndex = m_fn_iGetFirstValidOccurence(csLowRules, "macro");
	// get the first comment lines
	if ((iMacroIndex != -1)&&(iMacroIndex!=0))
		csCommentLines = m_fn_csExtractCommentLinesForNextMacro("\n"+csRules.Left(iMacroIndex));

	// if there is no macro in the text, no need to continue
	while (iMacroIndex != -1)
	{
		// get macro text
		csMacro = csRules.Mid(iMacroIndex);

		// skip the current macro title
		csRules = csRules.Mid(iMacroIndex + 5);
		csLowRules = csLowRules.Mid(iMacroIndex + 5);
		// find the next one
		iMacroIndex = m_fn_iGetFirstValidOccurence(csLowRules, "macro");

		// cut the macro text and go to the next one
		if (iMacroIndex != -1)
		{
			csMacro = csMacro.Left(iMacroIndex + 5);
			csRules = csRules.Mid(iMacroIndex);
			csLowRules = csLowRules.Mid(iMacroIndex);
			iMacroIndex = 0;
		}

		// get name of macro
		iNameIndex = csMacro.Find(":");
		csMacroName = csMacro.Mid(5, iNameIndex-5);
		csMacroName.TrimLeft();
		csMacroName.TrimRight();
		// get text of macro
		csMacroText = csMacro.Mid(iNameIndex+1);

		// build subroutine name
		csSubrName = g_pclAIInterface->GetInterface()->GetUserPrefix() + csMacroName;
		iSubrIndex = 0;
		while (pMacroMainBehaviour->fn_eCheckUnicity(csSubrName) != E_mc_None)
			csSubrName.Format("%s%s_SB%ld", g_pclAIInterface->GetInterface()->GetUserPrefix(), csMacroName, iSubrIndex++);

		// build subroutine text
		csSubrText = m_fn_csBuildDefineListForMacro(&csListOfDefines, csMacroText);
		csSubrText += csCommentLines;
		csSubrText += "subroutine :" + csMacroText;

		// get comment lines for the next macro
		csCommentLines = m_fn_csExtractCommentLinesForNextMacro(csSubrText);
		if (csCommentLines.GetLength())
			csSubrText = csSubrText.Left(csSubrText.GetLength()-csCommentLines.GetLength());

		// create subroutine behaviour
		pSubrBehaviour = g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_pclAddOneBehaviour(csSubrName,crTitleRect,csSubrText,FALSE,0);
		pSubrMainBehaviour = new CPA_EdIR_MainBehaviour(m_pclModel,csSubrName,crTitleRect,csSubrText,pSubrBehaviour);
		pSubrBehaviour->m_fn_vSetMainBehaviour(pSubrMainBehaviour);
		g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetDiagrammeView()->m_fn_vAddOneBehaviourInList(pSubrMainBehaviour);
		m_clListOfSubrs.m_fn_vSetModified(TRUE);
		crTitleRect.OffsetRect(0,50);
		lNbSubroutines++;

		// update lists
		pListOfMacros->AddTail(csMacroName);
		pListOfSubrs->AddTail(csSubrName);
	}

	return lNbSubroutines;
}


/****************************************************************************/
void CPA_EdIR_Brain::m_fn_vUpdateEditorMacroCallsInEditorAI (CStringList *pListOfMacros, CStringList *pListOfSubrs)
{
	CPA_EdIR_MainBehaviour	*pMainBehaviour;
	POSITION				pos, posMacro, posSubr;

	// update all the behaviours
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditIntell);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	pos = m_clListOfIntelligenceBehaviours.GetHeadPosition();
	while (pos)
	{
		// get the editor text of the macro behaviour
		pMainBehaviour = m_clListOfIntelligenceBehaviours.GetNext(pos);
		posMacro = pListOfMacros->GetHeadPosition();
		posSubr = pListOfSubrs->GetHeadPosition();
		while (posMacro && posSubr)
			m_fn_vReplaceInEditorBehaviour(pMainBehaviour, pListOfMacros->GetNext(posMacro), pListOfSubrs->GetNext(posSubr));
	}
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);

	// update all the reflex
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditReflex);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	pos = m_clListOfReflexBehaviours.GetHeadPosition();
	while (pos)
	{
		// get the editor text of the macro behaviour
		pMainBehaviour = m_clListOfReflexBehaviours.GetNext(pos);
		posMacro = pListOfMacros->GetHeadPosition();
		posSubr = pListOfSubrs->GetHeadPosition();
		while (posMacro && posSubr)
			m_fn_vReplaceInEditorBehaviour(pMainBehaviour, pListOfMacros->GetNext(posMacro), pListOfSubrs->GetNext(posSubr));
	}
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);

	// update all the subroutines
	g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vSetEditState(EditSubr);
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
	pos = m_clListOfSubrs.GetHeadPosition();
	while (pos)
	{
		// get the editor text of the macro behaviour
		pMainBehaviour = m_clListOfSubrs.GetNext(pos);
		posMacro = pListOfMacros->GetHeadPosition();
		posSubr = pListOfSubrs->GetHeadPosition();
		while (posMacro && posSubr)
			m_fn_vReplaceInEditorBehaviour(pMainBehaviour, pListOfMacros->GetNext(posMacro), pListOfSubrs->GetNext(posSubr));
	}
	g_pclAIInterface->m_pclIRMainFrame->m_fn_vUpdateBehaviours(FALSE);
}


/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vReplaceInEditorBehaviour (CPA_EdIR_MainBehaviour	*pMainBehaviour, CString csMacroName, CString csSubrName)
{
	CString					csRules, csResultRules;
	CString					csLowRules, csLowName;
	int						iMacroIndex, iEndMacroIndex;

	csRules = pMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
	// for the search we need a lower-case copy
	csLowRules = csRules;
	csLowRules.MakeLower();
	csLowName = csMacroName;
	csLowName.MakeLower();
	// get the first macro in the text
	iMacroIndex = csLowRules.Find(csLowName);
	// if there is no macro in the text, no need to continue
	while (iMacroIndex != -1)
	{
		// copy the previous lines of the rules
		csResultRules += csRules.Left(iMacroIndex);

		// check if the found name is really the macro one
		iEndMacroIndex = iMacroIndex + csMacroName.GetLength();
		
		// add the correpsonding word to the result
		csResultRules += m_fn_bIsIsolatedName(iMacroIndex, iEndMacroIndex, csRules) ? csSubrName : csMacroName;

		// skip the word and get the next one
		csRules = csRules.Mid(iMacroIndex+csMacroName.GetLength());
		csLowRules = csLowRules.Mid(iMacroIndex+csMacroName.GetLength());
		iMacroIndex = csLowRules.Find(csLowName);
	}
	csResultRules += csRules;

	// replace the text of the behaviour
	pMainBehaviour->m_pclBehaviour->m_fn_vSetRules(csResultRules);
	pMainBehaviour->m_csText = csResultRules;
}


/**********************************************************************************/
CString CPA_EdIR_Brain::m_fn_csBuildDefineListForMacro (CStringList *pListOfDefines, CString csMacroText)
{
	POSITION	pos;
	CString		csDefineText, csLowMacro;
	CString		csDefine, csLowDefine;
	int			iDefineIndex, iWordIndex;

	// for search we need a lower case text
	csLowMacro = csMacroText;
	csLowMacro.MakeLower();

	// find the defines used in the macro
	pos = pListOfDefines->GetHeadPosition();
	while (pos)
	{
		// get define text
		csDefine = pListOfDefines->GetNext(pos);
		csLowDefine = csDefine;
		csLowDefine.MakeLower();
		// get define title;
		csLowDefine.TrimLeft();
		iWordIndex = csLowDefine.FindOneOf(" \t");
		csLowDefine = csLowDefine.Left(iWordIndex);
		// check if it is used in the macro
		iDefineIndex = m_fn_iGetFirstValidOccurence(csLowMacro, csLowDefine);
		if (iDefineIndex != -1)
			csDefineText += "#define " + csDefine + "\n";
	}

	// add a line to separate from subroutine
	if (!csDefineText.IsEmpty())
		csDefineText += "\n";

	return csDefineText;
}


/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vRegisterEditorDefinesForMacro (CPA_EdIR_MainBehaviour *pMacroMainBehaviour, CStringList *pListOfDefines)
{
	CString					csRules, csLowRules, csDefine;
	int						iDefineIndex, iNewLineIndex;

	csRules = pMacroMainBehaviour->m_pclBehaviour->m_fn_csGetRules();

	// for the search we need a lower-case copy
	csLowRules = csRules;
	csLowRules.MakeLower();
	// get the first macro in the text
	iDefineIndex = m_fn_iGetFirstValidOccurence(csLowRules, "#define");
	// if there is no define in the text, no need to continue
	while (iDefineIndex != -1)
	{
		csDefine = csRules.Mid(iDefineIndex + 7);
		
		// register define text
		iNewLineIndex = csDefine.Find('\n');
		pListOfDefines->AddTail(csDefine.Left(iNewLineIndex));

		// skip this define and go to the next one
		csRules = csRules.Mid(iDefineIndex + 7);
		csLowRules = csLowRules.Mid(iDefineIndex + 7);
		iDefineIndex = m_fn_iGetFirstValidOccurence(csLowRules, "#define");
	}
}


/**********************************************************************************/
CString CPA_EdIR_Brain::m_fn_csExtractCommentLinesForNextMacro (CString csMacroText)
{
	CString csCommentLines;
	BOOL	bCommentText = TRUE;
	int		iNewLineIndex;

	// get last line of the macro
	iNewLineIndex = csMacroText.ReverseFind('\n');

	// check the last lines of the macro
	while ((iNewLineIndex != -1) && bCommentText)
	{
		// check if this line is emtpy or commented
		bCommentText = ((iNewLineIndex == csMacroText.GetLength() - 1)	||
						(csMacroText[iNewLineIndex+1] == ';')			|| 
						(csMacroText[iNewLineIndex+1] == '\n'));

		// in this case add it to the comment 
		if (bCommentText)
			csCommentLines = csMacroText.Mid(iNewLineIndex) + csCommentLines;
		// prepare next line to check
		csMacroText = csMacroText.Left(iNewLineIndex);
		iNewLineIndex = csMacroText.GetLength() ? csMacroText.ReverseFind('\n') : -1;
	}

	// remove the first empty lines
	csCommentLines.TrimLeft();

	return csCommentLines;
}


/**********************************************************************************/
int CPA_EdIR_Brain::m_fn_iGetFirstValidOccurence (CString csLowRules, CString csLowSearched)
{
	int iIndex, iLength, iRealIndex;
	
	// get the first macro in the text
	iIndex = csLowRules.Find(csLowSearched);
	iLength = csLowSearched.GetLength();
	iRealIndex = 0;

	// get define start
	while ((iIndex != -1) && 
		   (!m_fn_bIsIsolatedName(iIndex, iIndex+ iLength, csLowRules) || 
		     m_fn_bIsInCommentedLine(iIndex, csLowRules)))
	{
		csLowRules = csLowRules.Mid(iIndex + iLength);
		iRealIndex += iIndex + iLength;
		iIndex = csLowRules.Find(csLowSearched);
	}

	return ((iIndex == -1) ? -1 : iRealIndex + iIndex);
}


/**********************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bIsIsolatedName (int iStartIndex, int iEndIndex, CString csRule)
{
	BOOL bBlankLeft, bBlankRight;

	bBlankLeft = (iStartIndex == 0) || (!isalnum(csRule[iStartIndex-1]) && (csRule[iStartIndex-1] != '_'));
	bBlankRight = (iEndIndex == csRule.GetLength()) || (!isalnum(csRule[iEndIndex]) && (csRule[iEndIndex] != '_'));

	return (bBlankLeft && bBlankRight);
}


/**********************************************************************************/
BOOL CPA_EdIR_Brain::m_fn_bIsInCommentedLine (int iStartIndex, CString csRule)
{
	CString csLeftRule;
	int		iCommentIndex;
	int		iNewLineIndex;

	// get the left part of the rule
	csLeftRule = csRule.Left(iStartIndex);
	
	// get the last occurence of the comment symbol
	iCommentIndex = csLeftRule.ReverseFind(';');
	// get the last occurence of the new line symbol
	iNewLineIndex = csLeftRule.ReverseFind('\n');

	return ((iCommentIndex > iNewLineIndex) || ((iCommentIndex != -1) && (iNewLineIndex == -1)));
}

// End Shaitan => Macros to subroutines


#endif //D_ED_IR_ACTIVE
	