// EdIRSave.cpp : implementation of the CEdIAFrame class
//

#include "stdafx.h"
// Shaitan FastC {
#define D_Brain_StructureDefine
//End Shaitan FastC }

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRSave.hpp"

#include "EdIRStrg.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRBELs.hpp"
#include "EdIRBeha.hpp"
#include "EdIRIRD.hpp"
#include "EdIRDVLs.hpp"
#include "EdIRFrm.hpp"
#include "EdIRBtVw.hpp"
#include "EdIR2Eng.hpp"
#include "EdIRBis.hpp"
#include "EdIRGlob.hpp"
#include "EdIRCsLs.hpp"

#include "EdIRIAWd.hpp"

#include "Ctl.h"

#include "ai_intf.hpp"

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

#include "_Ainterf.hpp"
#include "EDACDoc.hpp"
#include "EDACModl.hpp"

#include "TFA.h"
#include "incAI.h"

#include "_Actors.hpp"

#include "DPT.h"

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


/////////////////////////////////
// TEST DEFINE
// DO NOT DEFINE THEM
// DO NOT DELETE THEM
/////////////////////////////////
//#define _SAVE_DEFAULT_IN_COMMENT_
//#define _SHOW_WARNING_
/////////////////////////////////
/////////////////////////////////


#define	M_SAVEENTRY1PARM(a,b)						SCR_M_SvL0_SaveEntry(p_tdstFile,								\
																															M_MAKECHAR(a),						\
																															SCR_CC_C_Cfg_NoChar);			\
																				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,			\
																															SCR_EF_SvL0_Normal,				\
																															1,												\
																															M_MAKECHAR(b));

#define	M_SAVEENTRY2PARMS(a,b,c)				SCR_M_SvL0_SaveEntry(p_tdstFile,								\
																															M_MAKECHAR(a),						\
																															SCR_CC_C_Cfg_NoChar);			\
																				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,			\
																															SCR_EF_SvL0_Normal,				\
																															2,												\
																															M_MAKECHAR(b),						\
																															M_MAKECHAR(c));

#define	M_SAVEENTRY3PARMS(a,b,c,d)			SCR_M_SvL0_SaveEntry(p_tdstFile,								\
																															M_MAKECHAR(a),						\
																															SCR_CC_C_Cfg_NoChar);			\
																				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,			\
																															SCR_EF_SvL0_Normal,				\
																															3,												\
																															M_MAKECHAR(b),						\
																															M_MAKECHAR(c),						\
																															M_MAKECHAR(d));

#define	M_SAVEENTRY4PARMS(a,b,c,d,e)		SCR_M_SvL0_SaveEntry(p_tdstFile,								\
																															M_MAKECHAR(a),						\
																															SCR_CC_C_Cfg_NoChar);			\
																				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,			\
																															SCR_EF_SvL0_Normal,				\
																															4,												\
																															M_MAKECHAR(b),						\
																															M_MAKECHAR(c),						\
																															M_MAKECHAR(d),						\
																															M_MAKECHAR(e));

#define	M_SAVEENTRY5PARMS(a,b,c,d,e,f)	SCR_M_SvL0_SaveEntry(p_tdstFile,								\
																															M_MAKECHAR(a),						\
																															SCR_CC_C_Cfg_NoChar);			\
																				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,			\
																															SCR_EF_SvL0_Normal,				\
																															5,												\
																															M_MAKECHAR(b),						\
																															M_MAKECHAR(c),						\
																															M_MAKECHAR(d),						\
																															M_MAKECHAR(e),						\
																															M_MAKECHAR(f));

/**********************************************************************************/
#if defined(_SAVE_DEFAULT_IN_COMMENT_)  // {

static char gs_szComment[255];

#define M_SAVECOMMENT1PARM(a,b)\
    sprintf (gs_szComment,"%s(%s)",M_MAKECHAR(a),M_MAKECHAR(b));\
    SCR_M_SvL0_SaveComment(p_tdstFile,gs_szComment);

#define M_SAVECOMMENT2PARMS(a,b,c)\
    sprintf (gs_szComment,"%s(%s,%s)",M_MAKECHAR(a),M_MAKECHAR(b),M_MAKECHAR(c));\
    SCR_M_SvL0_SaveComment(p_tdstFile,gs_szComment);

#define M_SAVECOMMENT3PARMS(a,b,c,d)\
    sprintf (gs_szComment,"%s(%s,%s,%s)",M_MAKECHAR(a),M_MAKECHAR(b),M_MAKECHAR(c),M_MAKECHAR(d));\
    SCR_M_SvL0_SaveComment(p_tdstFile,gs_szComment);

#define M_SAVECOMMENT4PARMS(a,b,c,d,e)\
    sprintf (gs_szComment,"%s(%s,%s,%s,%s)",M_MAKECHAR(a),M_MAKECHAR(b),M_MAKECHAR(c),M_MAKECHAR(d),M_MAKECHAR(e));\
    SCR_M_SvL0_SaveComment(p_tdstFile,gs_szComment);

#define M_SAVECOMMENT5PARMS(a,b,c,d,e,f)\
    sprintf (gs_szComment,"%s(%s,%s,%s,%s,%s)",M_MAKECHAR(a),M_MAKECHAR(b),M_MAKECHAR(c),M_MAKECHAR(d),M_MAKECHAR(e),M_MAKECHAR(f));\
    SCR_M_SvL0_SaveComment(p_tdstFile,gs_szComment);

#endif                                  // _SAVE_DEFAULT_IN_COMMENT_ }
/**********************************************************************************/
void fn_vSaveInfos(SCR_tdst_File_Description *p_tdstFile)
{
	//Saves version number
	SCR_M_SvL0_SaveComment(p_tdstFile,C_VERSION);
	SCR_M_SvL0_SaveBlankLine(p_tdstFile);

	//Computes date
	SYSTEMTIME stSystemTime;
	::GetLocalTime(&stSystemTime);
	CTime cTime(stSystemTime);
	CString csDate=cTime.Format("%A,%B,%d,%Y,%Hh%Mm");

	CString csLine="Save date:"+csDate;
	SCR_M_SvL0_SaveComment(p_tdstFile,M_MAKECHAR(csLine));
	SCR_M_SvL0_SaveBlankLine(p_tdstFile);
}

/**********************************************************************************/
void CPA_EdIR_MainBehaviourList::m_fn_vSaveBehaviourList(SCR_tdst_File_Description *p_tdstFile,
																												 CString csFamilyName,
																												 CString csModelName,
																												 CString csSectionName)
{
	//Computes file name for text
	CString	csRulesFileName=csFamilyName+"\\"+csModelName+"\\"+csModelName;
	if(csSectionName==g_c_csIntelligenceSectionName)
		csRulesFileName+=g_c_csIntelligenceTextFileExtension;
	else
	if(csSectionName==g_c_csReflexSectionName)
		csRulesFileName+=g_c_csReflexTextFileExtension;
	else
	if(csSectionName==g_c_csDeclarationSectionName)
		csRulesFileName+=g_c_csDeclarationTextFileExtension;

	POSITION	pos=GetHeadPosition();
	long			lOffset=0;
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour	*p_clMainBehaviour=GetNext(pos);

		if(p_clMainBehaviour->m_pclBehaviour)
		{
			p_clMainBehaviour->m_csText=p_clMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
			p_clMainBehaviour->m_crPosition=p_clMainBehaviour->m_pclBehaviour->m_fn_crGetBoundingBox();
		}

		SCR_M_SvL0_SaveBeginSection(p_tdstFile,
																M_MAKECHAR(g_c_csBehaviourSectionName+":"),
																SCR_CC_C_Cfg_EOL);

		//Writes Language
		M_SAVEENTRY1PARM(g_c_csBehaviourLanguage,g_pclAIInterface->M_GetMainWorld()->GetInterface()->fn_csGetCurrentLanguage());

		//Writes name
		CString		csName=p_clMainBehaviour->GetName();

		M_SAVEENTRY1PARM(g_c_csBehaviourName,csName);

		//Writes bounding box
		CRect		crBoundingBox=p_clMainBehaviour->m_crPosition;
		CString	csPosLeft;
		CString	csPosTop;
		CString	csPosRight;
		CString	csPosBottom;
		csPosLeft.Format("%ld",crBoundingBox.left);
		csPosTop.Format("%ld",crBoundingBox.top);
		csPosRight.Format("%ld",crBoundingBox.right);
		csPosBottom.Format("%ld",crBoundingBox.bottom);

		M_SAVEENTRY4PARMS(g_c_csBehaviourBoundingBox,
											csPosLeft,
											csPosTop,
											csPosRight,
											csPosBottom);

		//Writes infos about text
		CString		csRules=p_clMainBehaviour->m_csText;

		csRules=fn_csReplaceChar(csRules,'\"','\'');

		long		lIndex=0;
		CString	csLine;
		//BOOL		bMore=fn_bGetLine(csRules,lLineNumber,csLine);
		BOOL		bMore=fn_bGetNextLine(csRules,lIndex,csLine);
		while(bMore)
		{
			M_SAVEENTRY1PARM(g_c_csBehaviourText,csLine);

			//lLineNumber++;
			//bMore=fn_bGetLine(csRules,lLineNumber,csLine);
			bMore=fn_bGetNextLine(csRules,lIndex,csLine);
		}

/*		CString	csOffset;
		csOffset.Format("%ld",lOffset);

		long		lTextLen=csRules.GetLength();
		CString	csTextLen;
		csTextLen.Format("%ld",lTextLen);

		M_SAVEENTRY3PARMS(g_c_csBehaviourText,
											csRulesFileName,
											csOffset,
											csTextLen);

		lOffset+=lTextLen;*/

		SCRIPT_vSaveEndSection(p_tdstFile);
	}
}

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEditorAIDeclaration(SCR_tdst_File_Description	*p_tdstFile,
																									 char												*p_szSectionName,
																									 void												*p_vData,
																									 SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Editor)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csDeclarationSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	pclBrain->m_clListOfDeclarations.m_fn_vSaveBehaviourList(p_tdstFile,
																													 pclBrain->m_pclModel->m_pub_fn_pclGetFamily()->GetName(),
																													 pclBrain->m_fn_csGetModelName(),
																													 g_c_csDeclarationSectionName);

	SCRIPT_vSaveEndSection(p_tdstFile);

	/* CHRISTOPHE QUICK SAVE MODIFICATIONS { */
	//Deletes quick save file
	CString	csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString	csFileName=csPathName+csModelName+g_c_csDeclarationFileExtensionQS;
	DeleteFile(csFileName);
	/* } END CHRISTOPHE QUICK SAVE MODIFICATIONS */

}

// BEGIN CPA2 Cristi Petrescu 98-03-
/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEditorAIMacros(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Editor)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csMacroSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	pclBrain->m_clListOfMacros.m_fn_vSaveBehaviourList(p_tdstFile,
																															 pclBrain->m_pclModel->m_pub_fn_pclGetFamily()->GetName(),
																															 pclBrain->m_fn_csGetModelName(),
																															 g_c_csMacroSectionName);

	SCRIPT_vSaveEndSection(p_tdstFile);

	//Deletes quick save file
	CString	csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString	csFileName=csPathName+csModelName+g_c_csMacroFileExtensionQS;
	DeleteFile(csFileName);
}
// END CPA2 Cristi Petrescu 98-03-

// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEditorAISubrs(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Editor)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csSubrSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	pclBrain->m_clListOfSubrs.m_fn_vSaveBehaviourList(p_tdstFile,
																															 pclBrain->m_pclModel->m_pub_fn_pclGetFamily()->GetName(),
																															 pclBrain->m_fn_csGetModelName(),
																															 g_c_csSubrSectionName);

	SCRIPT_vSaveEndSection(p_tdstFile);

	//Deletes quick save file
	CString	csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString	csFileName=csPathName+csModelName+g_c_csSubrFileExtensionQS;
	DeleteFile(csFileName);
}
// END ROMTEAM CPA2 Cristi Petrescu 98-04-

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEditorAIRules(SCR_tdst_File_Description	*p_tdstFile,
																						 char												*p_szSectionName,
																						 void												*p_vData,
																						 SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Editor)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csIntelligenceSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	pclBrain->m_clListOfIntelligenceBehaviours.m_fn_vSaveBehaviourList(p_tdstFile,
																																		 pclBrain->m_pclModel->m_pub_fn_pclGetFamily()->GetName(),
																																		 pclBrain->m_fn_csGetModelName(),
																																		 g_c_csIntelligenceSectionName);
	SCRIPT_vSaveEndSection(p_tdstFile);

	/* CHRISTOPHE QUICK SAVE MODIFICATIONS { */
	//Deletes quick save file
	CString	csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString	csFileName=csPathName+csModelName+g_c_csIntelligenceFileExtensionQS;
	DeleteFile(csFileName);
	/* } END CHRISTOPHE QUICK SAVE MODIFICATIONS */
}

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEditorAIReflexes(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Editor)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csReflexSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	pclBrain->m_clListOfReflexBehaviours.m_fn_vSaveBehaviourList(p_tdstFile,
																															 pclBrain->m_pclModel->m_pub_fn_pclGetFamily()->GetName(),
																															 pclBrain->m_fn_csGetModelName(),
																															 g_c_csReflexSectionName);

	SCRIPT_vSaveEndSection(p_tdstFile);

	/* CHRISTOPHE QUICK SAVE MODIFICATIONS { */
	//Deletes quick save file
	CString	csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString	csFileName=csPathName+csModelName+g_c_csReflexFileExtensionQS;
	DeleteFile(csFileName);
	/* } END CHRISTOPHE QUICK SAVE MODIFICATIONS */
}
/* CHRISTOPHE QUICK SAVE MODIFICATIONS  { */
/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vQuickSave()
{
	SCR_tdst_Ntfy_Description	stNotify;
	CString										csFamilyName=m_fn_pclGetModel()->m_pub_fn_pclGetFamily()->GetName();
	CString										csModelName=m_fn_pclGetModel()->GetName();
	CString										csPathName=CString(fn_szGetFamiliesDataPath())+"\\"+csFamilyName+"\\"+csModelName+"\\";
	CString										csFileName=csPathName+csModelName+g_c_csReflexFileExtension;

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

	//Declaration
	csFileName=csPathName+csModelName+g_c_csDeclarationFileExtensionQS;
	strcpy(stNotify.a_szSectionName,M_MAKECHAR(csFileName));
	stNotify.pfn_vCallback=CPA_EdIR_Brain::m_fn_vSaveEditorAIDeclaration;
	stNotify.p_vData=this;
	stNotify.eAction=SCR_EA_Ntfy_RebuildSection;
	fn_v_SvL1_UpdateOneNotify(&stNotify,SCR_EA_Ntfy_RebuildSection);
	CString	csMsg="Quick save OK";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	//Rules
	csFileName=csPathName+csModelName+g_c_csIntelligenceFileExtensionQS;
	strcpy(stNotify.a_szSectionName,M_MAKECHAR(csFileName));
	stNotify.pfn_vCallback=CPA_EdIR_Brain::m_fn_vSaveEditorAIRules;
	stNotify.p_vData=this;
	stNotify.eAction=SCR_EA_Ntfy_RebuildSection;
	fn_v_SvL1_UpdateOneNotify(&stNotify,SCR_EA_Ntfy_RebuildSection);
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	//Reflex
	csFileName=csPathName+csModelName+g_c_csReflexFileExtensionQS;
	strcpy(stNotify.a_szSectionName,M_MAKECHAR(csFileName));
	stNotify.pfn_vCallback=CPA_EdIR_Brain::m_fn_vSaveEditorAIReflexes;
	stNotify.p_vData=this;
	stNotify.eAction=SCR_EA_Ntfy_RebuildSection;
	fn_v_SvL1_UpdateOneNotify(&stNotify,SCR_EA_Ntfy_RebuildSection);
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	// BEGIN CPA2 Cristi Petrescu 98-03-
	//Macro
	csFileName=csPathName+csModelName+g_c_csMacroFileExtensionQS;
	strcpy(stNotify.a_szSectionName,M_MAKECHAR(csFileName));
	stNotify.pfn_vCallback=CPA_EdIR_Brain::m_fn_vSaveEditorAIMacros;
	stNotify.p_vData=this;
	stNotify.eAction=SCR_EA_Ntfy_RebuildSection;
	fn_v_SvL1_UpdateOneNotify(&stNotify,SCR_EA_Ntfy_RebuildSection);
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
	// END CPA2 Cristi Petrescu 98-03-

	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	//Subr
	csFileName=csPathName+csModelName+g_c_csSubrFileExtensionQS;
	strcpy(stNotify.a_szSectionName,M_MAKECHAR(csFileName));
	stNotify.pfn_vCallback=CPA_EdIR_Brain::m_fn_vSaveEditorAISubrs;
	stNotify.p_vData=this;
	stNotify.eAction=SCR_EA_Ntfy_RebuildSection;
	fn_v_SvL1_UpdateOneNotify(&stNotify,SCR_EA_Ntfy_RebuildSection);
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-

	::ReleaseCapture();
	AfxGetApp()->DoWaitCursor(-1);
}
/* } END CHRISTOPHE QUICK SAVE MODIFICATIONS */

/****************************************************************************/
BOOL CPA_EdIR_MainBehaviourList::m_fn_bSaveIA(SCR_tdst_File_Description *p_tdstFile,AI_tdstMind *pstMind,tdeTypeText eTypeText)
{
	POSITION	pos=GetHeadPosition();
	BOOL			bNoError=TRUE;
	long			lCurrentBehaviour=0;
	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	CString	csCurrentBehaviour;
	//END ROMTEAM Cristi Petrescu 98-04-

	while(pos!=NULL && bNoError)
	{
		CPA_EdIR_Behaviour *pclBehaviour=GetNext(pos)->m_pclBehaviour;

		//BEGIN ROMTEAM Cristi Petrescu 98-04-
		if (eTypeText == SubrText)
		{
			csCurrentBehaviour.Format("%ld",lCurrentBehaviour);
		}
		//END ROMTEAM Cristi Petrescu 98-04-
		else if(!pclBehaviour->m_bDeclaration && eTypeText != Macro_2_Text && eTypeText != CRRGeneration)
		{
			//BEGIN ROMTEAM Cristi Petrescu 98-04-
			//CString	csCurrentBehaviour;
			//END ROMTEAM Cristi Petrescu 98-04-
			csCurrentBehaviour.Format("%ld",lCurrentBehaviour);

			long		lNbRules=fn_lGetNbRules(pclBehaviour->m_fn_csGetRules());
			CString	csNbRules;
			csNbRules.Format("%ld",lNbRules);

			SCR_M_SvL0_SaveBeginSection(p_tdstFile,
																	M_MAKECHAR(g_c_csBehavSectionName+":"+pclBehaviour->m_fn_csGetName()),
																	SCR_CC_C_Cfg_NoChar);
			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																			SCR_EF_SvL0_Normal,
																			2,
																			M_MAKECHAR(csCurrentBehaviour),
																			M_MAKECHAR(csNbRules));
		}

		//BEGIN ROMTEAM Cristi Petrescu 98-04-
		bNoError=pclBehaviour->m_fn_bSaveIA(p_tdstFile,pstMind,eTypeText,csCurrentBehaviour);
		//END ROMTEAM Cristi Petrescu 98-04-

		//BEGIN ROMTEAM Cristi Petrescu 98-04-
		if(!pclBehaviour->m_bDeclaration && eTypeText != Macro_2_Text && eTypeText != SubrText && eTypeText != CRRGeneration)
			//END ROMTEAM Cristi Petrescu 98-04-
		{
			SCRIPT_vSaveEndSection(p_tdstFile);
		}

		lCurrentBehaviour++;
	}

	return bNoError;
}

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAI(SCR_tdst_File_Description	*p_tdstFile,
																				char											*p_szSectionName,
																				void											*p_vData,
																				SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();
  long            CFast_bIsC ;

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Engine)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csAISectionName+":"),
															SCR_CC_C_Cfg_EOL);

	M_SAVEENTRY1PARM(g_c_csAIDecSectionName,
									 csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAIDecFileExtension);
	M_SAVEENTRY1PARM(g_c_csAIRulSectionName,
									 csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAIRulFileExtension);
	M_SAVEENTRY1PARM(g_c_csAIRfxSectionName,
									 csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAIRfxFileExtension);
	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if (! pclBrain -> m_clListOfSubrs . IsEmpty ())
	{
		M_SAVEENTRY1PARM(g_c_csAISubrSectionName,
									 csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAISubrFileExtension);
	}
	//END ROMTEAM Cristi Petrescu 98-04-

// Shaitan FastC {
  CFast_bIsC = pclActor->GetStruct()->h_Brain->p_stMind->p_stAIModel->CFast_bIsC ;

  if ( CFast_bIsC ) {
	  M_SAVEENTRY1PARM(g_c_csAICrrSectionName,
									   csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAICrrFileExtension);
	  M_SAVEENTRY1PARM( g_c_csAINodeSectionName,
									    csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAINodeFileExtension );
  }

	CString csFast_IsC;
	CString csFast_FunctionIndex;

	csFast_IsC.Format("%ld", CFast_bIsC);
	M_SAVEENTRY1PARM(g_c_csAIFastIsC_SectionName, csFast_IsC);
	csFast_FunctionIndex.Format("%ld", pclActor->GetStruct()->h_Brain->p_stMind->p_stAIModel->CFast_lFunction);
	M_SAVEENTRY1PARM(g_c_csAIFastFId_SectionName, csFast_FunctionIndex);
//End Shaitan FastC }

	SCRIPT_vSaveEndSection(p_tdstFile);
}

/**********************************************************************************/
void fn_vSaveSaveType(SCR_tdst_File_Description *p_tdstFile,CPA_Actor *pclModel,CPA_EdIR_MainBehaviourList *pclDeclarationsList)
{
	if(!pclDeclarationsList->IsEmpty())
	{
		//Gets Designer Variable MainBehaviour (first element in the list)
		CPA_EdIR_MainBehaviour *pclMainBehaviour=pclDeclarationsList->GetHead();

		//Gets declaration text without any comments
		CString	csIn;

		if(pclMainBehaviour->m_pclBehaviour!=NULL)
			csIn=pclMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
		else
			csIn=pclMainBehaviour->m_csText;

		csIn=fn_csGetCodeWithoutComments(csIn);

		//Saves "save type" for DsgVar
		//Analyses dsg var declarations
		if(pclDeclarationsList->m_fn_bCheckIA(DsgVarText))
		{
			//Saves entries
			EdActors_EditorActor	*pclEditorActor=pclModel->m_fn_pclGetEditorActor();
			CTL_Editor_DataList		*pclDsgVarListType=pclEditorActor->m_pclListOfDesignerVariableType;

			long		lCurrentIndex=0;
			//ROMTEAM Cristi Petrescu THIS STINKS!!! You should use the PARSER to analyze your code
			//ROMTEAM Cristi Petrescu This way you cannot use #define in this section
			//ROMTEAM Cristi Petrescu You may redefine the fn_csGetNextWord function to ignore #define
			//ROMTEAM Cristi Petrescu or write fn_csGetCodeWithoutCommentsAndDefines, but your #define will VANISH
			//ROMTEAM Cristi Petrescu So, you are in troubles...
			CString	csWord=fn_csGetNextWord(csIn,lCurrentIndex);
			long		lNoDsgVar=0;
			while(!csWord.IsEmpty())
			{
				// checks for "Private" variables
				if (!csWord.CompareNoCase("Private"))
					csWord = fn_csGetNextWord(csIn,lCurrentIndex);

				//Gets Type
				tdeDsgVarTypeId	eDsgVarTypeId=(tdeDsgVarTypeId)fn_lGetDsgVarTypeIdFromEditorName(csWord);

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

				//Computes Index
				CString	csDsgVarIndex;
				csDsgVarIndex.Format("%ld",lNoDsgVar);

				//Skips Initial Value
				CString	csInitValue=fn_csGetNextWord(csIn,lCurrentIndex);

				//Saves only if different from "None"
				CTL_Editor_Data	*pclBaseData=pclDsgVarListType->m_fn_pclGetDataFromName(csDsgVarName+g_c_csDsgVarSaveTypeText);
				if(pclBaseData!=NULL)
				{
					ASSERT(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM);

					if(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM)
					{
						CTL_Editor_EnumData	*pclDataEnum=(CTL_Editor_EnumData *)pclBaseData;

						if(pclDataEnum->m_pclCurrentValue!=NULL)
						{
// Shaitan => Check Save type		
							EdActors_EditorActorModel	*pModel		= (EdActors_EditorActorModel *)(pclModel->m_fn_pclGetEditorActor());
							POSITION					pos			= pModel->m_clInstancesList.GetHeadPosition();
							CString						csMessage;
							BOOL						bAlways		= FALSE;
							BOOL						bCommon		= TRUE;
							while (pos)
							{
								EdActors_EditorActorInstance *pclInstance = pModel->m_clInstancesList.GetNext(pos);
								// an always must have no save type
								if (pclInstance->m_pclActor->m_fn_bIsAnAlways())
									bAlways = TRUE;
								// a level actor can have only "level" or "non" save type
								else if (!pclInstance->m_pclActor->m_pub_fn_bIsCommon())
									bCommon = FALSE;
							}

							if (bAlways)
							{
								if (pclDataEnum->m_pclCurrentValue->m_pub_fn_lGetValue() != 0)
								{
									csMessage.Format("The always model %s was using a wrong Save Type.\nIt was changed to the default one", pclModel->GetName());
									MessageBox(NULL,  csMessage, "WARNING SAVE TYPE", MB_ICONEXCLAMATION);
									pclDataEnum->m_pclCurrentValue = pclDataEnum->m_fn_pclGetEnumDescriptor()->GetHead();
									pclDataEnum->m_fn_vUpdateMotorData();
								}

							}
							// a level actor can have only "level" or "non" save type
							else if (!bCommon)
							{
								if (pclDataEnum->m_pclCurrentValue->m_pub_fn_lGetValue() == SAI_ePlayerSaveCurrentValue)
								{
									csMessage.Format("The model %s is not in fix, and uses the Save Type 'Player'.\nAre you sure you want to keep this value ?", pclModel->GetName());
									int iRes = MessageBox(NULL,  csMessage, "WARNING SAVE TYPE", MB_ICONEXCLAMATION | MB_YESNO );
									if (iRes == IDNO)
									{
										pclDataEnum->m_pclCurrentValue = pclDataEnum->m_fn_pclGetEnumDescriptor()->GetHead();
										pclDataEnum->m_fn_vUpdateMotorData();
									}
								}
							}
// End Shaitan => Check Save type
							
							long		lSaveTypeValue=pclDataEnum->m_pclCurrentValue->m_pub_fn_lGetValue();
							CString	csSaveTypeValue=fn_szFindScriptNameFromSaveType((enum SAI_tdeFlags_)lSaveTypeValue);

							if(lSaveTypeValue!=fn_eFindSaveTypeFromSaveTypeNumber(fn_ucDefaultSaveTypeNumber()))
							{
								M_SAVEENTRY2PARMS(g_c_csDecSaveTypeEntryName,
																	csDsgVarIndex,
																	csSaveTypeValue);
							}
#if defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
                            else
                            {
                                M_SAVECOMMENT2PARMS(g_c_csDecSaveTypeEntryName,
																	csDsgVarIndex,
																	csSaveTypeValue);
                            }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
						}
					}
				}

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

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

				lNoDsgVar++;
			}
		}
	}
}

/**********************************************************************************/
void fn_vSaveInitType(SCR_tdst_File_Description *p_tdstFile,CPA_Actor *pclModel,CPA_EdIR_MainBehaviourList *pclDeclarationsList)
{
	if(!pclDeclarationsList->IsEmpty())
	{
		//Gets Designer Variable MainBehaviour (first element in the list)
		CPA_EdIR_MainBehaviour *pclMainBehaviour=pclDeclarationsList->GetHead();

		//Gets declaration text without any comments
		CString	csIn;

		if(pclMainBehaviour->m_pclBehaviour!=NULL)
			csIn=pclMainBehaviour->m_pclBehaviour->m_fn_csGetRules();
		else
			csIn=pclMainBehaviour->m_csText;

		csIn=fn_csGetCodeWithoutComments(csIn);

		//Saves "init type" for DsgVar
		//Analyses dsg var declarations
		if(pclDeclarationsList->m_fn_bCheckIA(DsgVarText))
		{
			//Saves entries
			EdActors_EditorActor	*pclEditorActor=pclModel->m_fn_pclGetEditorActor();
			CTL_Editor_DataList		*pclDsgVarListType=pclEditorActor->m_pclListOfDesignerVariableType;

			long		lCurrentIndex=0;
			//ROMTEAM Cristi Petrescu THIS STINKS!!! You should use the PARSER to analyze your code
			//ROMTEAM Cristi Petrescu This way you cannot use #define in this section
			//ROMTEAM Cristi Petrescu You may redefine the fn_csGetNextWord function to ignore #define
			//ROMTEAM Cristi Petrescu or write fn_csGetCodeWithoutCommentsAndDefines, but your #define will VANISH
			//ROMTEAM Cristi Petrescu So, you are in troubles...
			CString	csWord=fn_csGetNextWord(csIn,lCurrentIndex);
			long		lNoDsgVar=0;
			while(!csWord.IsEmpty())
			{
				// checks for "Private" variables
				if (!csWord.CompareNoCase("Private"))
					csWord = fn_csGetNextWord(csIn,lCurrentIndex);

				//Gets Type
				tdeDsgVarTypeId	eDsgVarTypeId=(tdeDsgVarTypeId)fn_lGetDsgVarTypeIdFromEditorName(csWord);

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

				//Computes Index
				CString	csDsgVarIndex;
				csDsgVarIndex.Format("%ld",lNoDsgVar);

				//Skips Initial Value
				CString	csInitValue=fn_csGetNextWord(csIn,lCurrentIndex);

				//Saves only if different from "None"
				CTL_Editor_Data	*pclBaseData=pclDsgVarListType->m_fn_pclGetDataFromName(csDsgVarName+g_c_csDsgVarInitTypeText);
				if(pclBaseData!=NULL)
				{
					ASSERT(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM);

					if(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM)
					{
						CTL_Editor_EnumData	*pclDataEnum=(CTL_Editor_EnumData *)pclBaseData;

						if(pclDataEnum->m_pclCurrentValue!=NULL)
						{
							long		lInitTypeValue=pclDataEnum->m_pclCurrentValue->m_pub_fn_lGetValue();
							CString	csInitTypeValue=fn_szFindScriptNameFromInitType((enum tdeObjectTreeInit_)lInitTypeValue);

							if(lInitTypeValue!=fn_eFindInitTypeFromInitTypeNumber(fn_ucDefaultInitTypeNumber()))
							{
								M_SAVEENTRY2PARMS(g_c_csDecInitTypeEntryName,
																	csDsgVarIndex,
																	csInitTypeValue);
							}
#if defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
                            else
                            {
                                M_SAVECOMMENT2PARMS(g_c_csDecInitTypeEntryName,
																	csDsgVarIndex,
																	csInitTypeValue);

                            }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
						}
					}
				}

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

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

				lNoDsgVar++;
			}
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAIDeclaration(SCR_tdst_File_Description	*p_tdstFile,
																									 char												*p_szSectionName,
																									 void												*p_vData,
																									 SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;
	g_pclCurrentModel=pclBrain->m_pclModel;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Engine)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

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

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

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

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csDecSectionName+":"),
															SCR_CC_C_Cfg_NoChar);

	//Analyses dsg var declarations
	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if(pclBrain->m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText) &&
		 pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text))
	//END ROMTEAM Cristi Petrescu 98-04-
	{
		CString				csSizeOfAllDsgVar;
		csSizeOfAllDsgVar.Format("%ld",g_clDsgVarList.m_fn_lGetSizeOfAllDesignerVariables());
		CString				csNbDsgVar;
		csNbDsgVar.Format("%ld",g_clDsgVarList.GetCount());
		SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																		SCR_EF_SvL0_Normal,
																		2,
																		M_MAKECHAR(csSizeOfAllDsgVar),
																		M_MAKECHAR(csNbDsgVar));

		AI_tdstMind	*pstMind=NULL;
		CPA_List<CPA_BaseObject> clInstancesList;
		g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,pclActor);
		if(!clInstancesList.IsEmpty())
		{
			CPA_Actor						*pclInstance=(CPA_Actor *)(clInstancesList.GetHead());
			tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
			MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;

			if(hBrain!=NULL)
				pstMind=M_pstGetMindOfBrain(hBrain);
		}

		if(pclBrain->m_clListOfDeclarations.m_fn_bSaveIA(p_tdstFile,pstMind,DsgVarText))
		{
			fn_vSaveSaveType(p_tdstFile,g_pclCurrentModel,&(pclBrain->m_clListOfDeclarations));
			//BEGIN ROMTEAM Cristi Petrescu 98-04-
			pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text);
			//END ROMTEAM Cristi Petrescu 98-04-
			fn_vSaveInitType(p_tdstFile,g_pclCurrentModel,&(pclBrain->m_clListOfDeclarations));
			//BEGIN ROMTEAM Cristi Petrescu 98-04-
			pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text);
			//END ROMTEAM Cristi Petrescu 98-04-

			SCRIPT_vSaveEndSection(p_tdstFile);

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

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

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAIRules(SCR_tdst_File_Description	*p_tdstFile,
																						 char												*p_szSectionName,
																						 void												*p_vData,
																						 SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;
	g_pclCurrentModel=pclBrain->m_pclModel;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Engine)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

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

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

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

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csRulSectionName+":"),
															SCR_CC_C_Cfg_NoChar);

	CString	csNbBehaviours;
	csNbBehaviours.Format("%ld",pclBrain->m_clListOfIntelligenceBehaviours.GetCount());
	CString	csNbEntriesInActionTable;
	csNbEntriesInActionTable.Format("%u",fn_ucGetDefaultActionTableNbEntries());

	if(!pclBrain->m_clListOfIntelligenceBehaviours.IsEmpty())
	{
		/* Saves initial behaviour */
		CTL_Editor_Data *pclBaseData=g_pclCurrentModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialIABehaviour);

		if((pclBaseData!=NULL) &&
			 (!pclBaseData->m_fn_pcslFormatDataValueString()->IsEmpty()))
		{
			CString	csInitialIABehaviour=pclBaseData->m_fn_pcslFormatDataValueString()->GetHead();

			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																			SCR_EF_SvL0_Normal,
																			3,
																			M_MAKECHAR(csNbBehaviours),
																			M_MAKECHAR(csNbEntriesInActionTable),
																			M_MAKECHAR(csInitialIABehaviour));
		}
		else
		{
			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																			SCR_EF_SvL0_Normal,
																			2,
																			M_MAKECHAR(csNbBehaviours),
																			M_MAKECHAR(csNbEntriesInActionTable));
		}
	}
	else
	{
		SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,SCR_EF_SvL0_Normal,2,"0","0");
	}

	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if(pclBrain->m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText) &&
		 pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text) &&
		 pclBrain->m_clListOfIntelligenceBehaviours.m_fn_bSaveIA(p_tdstFile,NULL,IntelligenceText))
	//END ROMTEAM Cristi Petrescu 98-04-
	{
		SCRIPT_vSaveEndSection(p_tdstFile);

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

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

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAIReflexes(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;
	g_pclCurrentModel=pclBrain->m_pclModel;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Engine)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

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

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

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

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csRfxSectionName+":"),
															SCR_CC_C_Cfg_NoChar);

	CString	csNbBehaviours;
	csNbBehaviours.Format("%ld",pclBrain->m_clListOfReflexBehaviours.GetCount());
	CString	csNbEntriesInActionTable;
	csNbEntriesInActionTable.Format("%u",fn_ucGetDefaultActionTableNbEntries());

	if(!pclBrain->m_clListOfReflexBehaviours.IsEmpty())
	{
		/* Saves initial behaviour */
		CTL_Editor_Data *pclBaseData=g_pclCurrentModel->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType->m_fn_pclGetDataFromName(g_c_csInitialRfxBehaviour);

		if((pclBaseData!=NULL) &&
			 (!pclBaseData->m_fn_pcslFormatDataValueString()->IsEmpty()))
		{
			CString	csInitialRfxBehaviour=pclBaseData->m_fn_pcslFormatDataValueString()->GetHead();

			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																			SCR_EF_SvL0_Normal,
																			3,
																			M_MAKECHAR(csNbBehaviours),
																			M_MAKECHAR(csNbEntriesInActionTable),
																			M_MAKECHAR(csInitialRfxBehaviour));
		}
		else
		{
			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
																			SCR_EF_SvL0_Normal,
																			2,
																			M_MAKECHAR(csNbBehaviours),
																			M_MAKECHAR(csNbEntriesInActionTable));
		}
	}
	else
	{
		SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,SCR_EF_SvL0_Normal,2,"0","0");
	}

	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if(pclBrain->m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText) &&
		 pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text) &&
		 pclBrain->m_clListOfReflexBehaviours.m_fn_bSaveIA(p_tdstFile,NULL,ReflexText))
	//END ROMTEAM Cristi Petrescu 98-04-
	{
		SCRIPT_vSaveEndSection(p_tdstFile);

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

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

//BEGIN ROMTEAM Cristi Petrescu 98-04-
/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAISubrs(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)p_vData;
	g_pclCurrentModel=pclBrain->m_pclModel;

	CPA_Actor				*pclActor=pclBrain->m_fn_pclGetModel();
	CString					csFamilyName=pclActor->m_pub_fn_pclGetFamily()->GetName();
	CString					csModelName=pclActor->GetName();

	CString	csMsg="Saving IA of "+csFamilyName+"@"+csModelName+" (Engine)";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);

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

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

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

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csEngSubrSectionName+":"),
															SCR_CC_C_Cfg_NoChar);

	CString	csNbBehaviours;
	csNbBehaviours.Format("%ld",pclBrain->m_clListOfSubrs.GetCount());

	if(!pclBrain->m_clListOfSubrs.IsEmpty())
	{
		SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
										SCR_EF_SvL0_Normal,
										1,
										M_MAKECHAR(csNbBehaviours));
	}
	else
	{
		SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,SCR_EF_SvL0_Normal,1,"0");
	}

	//BEGIN ROMTEAM Cristi Petrescu 98-04-
	if(pclBrain->m_clListOfDeclarations.m_fn_bCheckIA(DsgVarText) &&
		 pclBrain->m_clListOfMacros.m_fn_bCheckIA(Macro_2_Text) &&
		 pclBrain->m_clListOfSubrs.m_fn_bSaveIA(p_tdstFile,NULL,SubrText))
	 //END ROMTEAM Cristi Petrescu 98-04-
	{
		SCRIPT_vSaveEndSection(p_tdstFile);

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

		//Sets current brain
		pclIRMainFrame->m_fn_pclUpdateBrain(pclOldBrain,TRUE,FALSE);
	}
}
//END ROMTEAM Cristi Petrescu 98-04-

/**********************************************************************************/
void CPA_EdIR_Brain::m_fn_vSaveEngineAIDefines(SCR_tdst_File_Description	*p_tdstFile,
																								char											*p_szSectionName,
																								void											*p_vData,
																								SCR_tde_Ntfy_Action				eAction)
{
	CPA_EdIR_ConstantList	*pclConstantList=(CPA_EdIR_ConstantList *)p_vData;

	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);

	fn_vSaveInfos(p_tdstFile);

	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(g_c_csAIDefineSectionName+":"),
															SCR_CC_C_Cfg_EOL);

	POSITION	pos=pclConstantList->GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_Constant	*pclConstant=pclConstantList->GetNext(pos);

		CString	csType=pclConstant->m_fn_csGetConstantType();
		if(csType==g_c_csInteger)
			csType=g_c_csAIDefineConstantRef;
		else
		if(csType==g_c_csDecimal)
			csType=g_c_csAIDefineRealRef;
		else
		{
			AfxMessageBox("CPA_EdIR_Brain::m_fn_vSaveEngineAIDefines : unknown constant type ("+csType+")");

			ASSERT(FALSE);
		}

		CString	csName=pclConstant->m_fn_csGetConstantName();
		CString	csValue=pclConstant->m_fn_csGetConstantValue();

		M_SAVEENTRY2PARMS(csType,csName,csValue);
	}

	SCRIPT_vSaveEndSection(p_tdstFile);
}

/**********************************************************************************/
void EdIR_fn_vSaveInitialBehaviourInBrainMS(CPA_Actor *pclActor,SCR_tdst_File_Description *p_tdstFile)
{
	tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclActor->GetData();
	MS_tdxHandleToBrain		hBrain			=pstEngineObject->h_Brain;
	AI_tdstMind				*pstMind		=M_pstGetMindOfBrain(hBrain);

	//Saves initial behaviour (rule)
	if(pstMind!=NULL)
	{
		EdActors_EditorActor		*pclEditorActor		=pclActor->m_fn_pclGetEditorActor();
		CPA_Actor					*pclModel			=pclActor->m_fn_pclGetModel();
		EdActors_EditorActorModel	*pclEditorModel		=(EdActors_EditorActorModel *)pclModel->m_fn_pclGetEditorActor();
		CTL_Editor_DataList			*pclDsgVarListType	=pclEditorActor->m_pclListOfDesignerVariableType;
		CTL_Editor_Data				*pclBaseData		=pclDsgVarListType->m_fn_pclGetDataFromName(g_c_csInitialIABehaviour);

		if(!pclEditorModel->m_pclBrain->m_clListOfIntelligenceBehaviours.IsEmpty())
		{
			if( (pclBaseData!=NULL) &&
				(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM)  )
			{
				CString		csInitialBehaviourName	=pclBaseData->m_fn_pcslFormatDataValueString()->GetHead();
				CString		csFamilyName			=pclModel->m_pub_fn_pclGetFamily()->GetName();
				CString		csModelName				=pclModel->GetName();
				CString		csPath					=csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAIRulFileExtension+"^"+g_c_csRulSectionName;

				SCR_M_SvL0_SaveBeginSection(p_tdstFile,
					M_MAKECHAR(g_c_csCarRulSectionName+":"),
					SCR_CC_C_Cfg_NoChar);

				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
					SCR_EF_SvL0_Normal,
					1,
					M_MAKECHAR(csPath+"^"+g_c_csBehavSectionName+":"+csInitialBehaviourName));

				SCRIPT_vSaveEndSection(p_tdstFile);
			}
#if defined(_SHOW_WARNING_) // {
			else
			{
				if (AfxMessageBox("WARNING : EdIR_fn_vSaveInitialBehaviourInBrainMS : no initial comportement saved\nDo you want to debug ?",MB_YESNO) == IDYES)
					ASSERT(FALSE);
			}
#endif // _SHOW_WARNING_ }
		}
#if defined(_SAVE_DEFAULT_IN_COMMENT_) // {
		else
		{
			SCR_M_SvL0_SaveComment(p_tdstFile,"No Intelligence behaviour in model");
		}
#endif // _SAVE_DEFAULT_IN_COMMENT_ }
	}

	//Saves initial behaviour (reflex)
	if(pstMind!=NULL)
	{
		EdActors_EditorActor		*pclEditorActor		=pclActor->m_fn_pclGetEditorActor();
		CPA_Actor					*pclModel			=pclActor->m_fn_pclGetModel();
		EdActors_EditorActorModel	*pclEditorModel		=(EdActors_EditorActorModel *)pclModel->m_fn_pclGetEditorActor();
		CTL_Editor_DataList			*pclDsgVarListType	=pclEditorActor->m_pclListOfDesignerVariableType;
		CTL_Editor_Data				*pclBaseData		=pclDsgVarListType->m_fn_pclGetDataFromName(g_c_csInitialRfxBehaviour);

		if(!pclEditorModel->m_pclBrain->m_clListOfReflexBehaviours.IsEmpty())
		{
			if( (pclBaseData!=NULL) &&
				(pclBaseData->m_pub_fn_tdeGetDataType()==CTL_DATA_TYPE__ENUM)  )
			{
				CString		csInitialBehaviourName	=pclBaseData->m_fn_pcslFormatDataValueString()->GetHead();
				CString		csFamilyName			=pclModel->m_pub_fn_pclGetFamily()->GetName();
				CString		csModelName				=pclModel->GetName();
				CString		csPath					=csFamilyName+"\\"+csModelName+"\\"+csModelName+g_c_csAIRfxFileExtension+"^"+g_c_csRfxSectionName;

				SCR_M_SvL0_SaveBeginSection(p_tdstFile,
					M_MAKECHAR(g_c_csCarRfxVarSectionName+":"),
					SCR_CC_C_Cfg_NoChar);

				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
					SCR_EF_SvL0_Normal,
					1,
					M_MAKECHAR(csPath+"^"+g_c_csBehavSectionName+":"+csInitialBehaviourName));
				SCRIPT_vSaveEndSection(p_tdstFile);
			}
#if defined(_SHOW_WARNING_) // {
			else
			{
				if(AfxMessageBox("WARNING : EdIR_fn_vSaveInitialBehaviourInBrainMS : no initial reflex saved\nDo you want to debug ?",MB_YESNO)==IDYES)
					ASSERT(FALSE);
			}
#endif // _SHOW_WARNING_ }
		}
#if defined(_SAVE_DEFAULT_IN_COMMENT_) // {
		else
		{
			SCR_M_SvL0_SaveComment(p_tdstFile,"No Reflex behaviour in model");
		}
#endif // _SAVE_DEFAULT_IN_COMMENT_ }
	}
}

/**********************************************************************************/
CString EdIR_fn_csGetInitialValue(void *pvInitEngineValue,tdeDsgVarTypeId eDsgVarTypeId)
{
	CString					csInitDsgVarValue;
	tdstGetSetParam	stGetSetParam;

	switch(eDsgVarTypeId)
	{
		case eDsgVarType_Boolean:
		case eDsgVarType__128To127:
		case eDsgVarType_0To255:
		case eDsgVarType__32768To32767:
		case eDsgVarType_0To65535:
		case eDsgVarType_Integer:
		case eDsgVarType_PositiveInteger:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);
				csInitDsgVarValue.Format("%ld",M_GetSetParam_lValue(&stGetSetParam));
			}
			break;

		case eDsgVarType_Float:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);
				csInitDsgVarValue.Format("%f",M_GetSetParam_xValue(&stGetSetParam));
			}
			break;

		case eDsgVarType_Caps:
			{
        char sz255Temp[255];
				fn_ucGetDsgVarValue(pvInitEngineValue, 0, eDsgVarTypeId, &stGetSetParam);
				ltoa( M_GetSetParam_CapsValue(&stGetSetParam), sz255Temp, 2);
        csInitDsgVarValue = sz255Temp;
			}
			break;

		case eDsgVarType_Graph:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				WP_tdHandleOfGraph lHandle = (WP_tdHandleOfGraph) M_GetSetParam_GraphValue(&stGetSetParam);
				CString	csReferencedSectionName;
				if(lHandle!=NULL)
				{
					csReferencedSectionName = lHandle->m_szNameOfGraph;
				}
				else
					csReferencedSectionName=szFindKeyWordScriptNameFromId(eKeyWord_NoGraph);

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;

		case eDsgVarType_WayPoint:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long		lHandle=(long)M_GetSetParam_WayPointValue(&stGetSetParam);
				CString	csReferencedSectionName;
				if(lHandle!=NULL)
				{
					CPA_SaveObject	*pclObject=fn_pclGetReachableObject(lHandle,C_szWayPointTypeName);
					csReferencedSectionName=pclObject->GetReferencedSectionName();
				}
				else
					csReferencedSectionName=szFindKeyWordScriptNameFromId(eKeyWord_Nowhere);

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;

		case eDsgVarType_Perso:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long		lHandle=M_GetSetParam_lValue(&stGetSetParam);
				CString	csReferencedSectionName;
				if(lHandle!=NULL)
				{
					CPA_SaveObject	*pclObject=fn_pclGetReachableObject(lHandle,C_szActorInstanceTypeName);
					csReferencedSectionName=pclObject->GetReferencedSectionName();
				}
				else
					csReferencedSectionName=szFindKeyWordScriptNameFromId(eKeyWord_Nobody);

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;

		case eDsgVarType_SuperObject:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long		lHandle=M_GetSetParam_lValue(&stGetSetParam);
				CString	csReferencedSectionName;
				if(lHandle!=NULL)
				{
					CPA_SaveObject	*pclObject=fn_pclGetReachableObject(lHandle,C_szSuperObjectTypeName);
					csReferencedSectionName=pclObject->GetReferencedSectionName();
				}
				else
					csReferencedSectionName=szFindKeyWordScriptNameFromId(eKeyWord_NoSuperObject);

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;

		case eDsgVarType_List:
			{
				ASSERT(FALSE);
			}
			break;

		case eDsgVarType_Vector:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				MTH3D_tdstVector	*pstVector=&(M_GetSetParam_stVertexValue(&stGetSetParam));
				csInitDsgVarValue.Format("%f,%f,%f",
																 MTH3D_M_xGetXofVector(pstVector),
																 MTH3D_M_xGetYofVector(pstVector),
																 MTH3D_M_xGetZofVector(pstVector));
			}
			break;

		case eDsgVarType_Comport:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long	lHandle=(long)M_GetSetParam_pComport(&stGetSetParam);

				csInitDsgVarValue=M_GetNameComport((struct tdstComport_ *)lHandle);
			}
			break;

		case eDsgVarType_Action:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long						lHandle=(long)M_GetSetParam_ActionValue(&stGetSetParam);
				CPA_SaveObject	*pclObject=fn_pclGetReachableObject(lHandle,C_szStateTypeName);
				CString					csReferencedSectionName=pclObject->GetReferencedSectionName();

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;

    case eDsgVarType_Text:
      {
        fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

        FON_tdxHandleOfText hText = M_GetSetParam_hText(&stGetSetParam);

        csInitDsgVarValue = FON_fn_szGetNameOfTextHandle(hText);
      }
      break;

		case eDsgVarType_GameMaterial:
			{
				fn_ucGetDsgVarValue(pvInitEngineValue,0,eDsgVarTypeId,&stGetSetParam);

				long						lHandle=(long)M_GetSetParam_hGameMaterial(&stGetSetParam);
				CPA_SaveObject	*pclObject=fn_pclGetReachableObject(lHandle,C_szGameMaterialTypeName);
				CString					csReferencedSectionName=pclObject->GetReferencedSectionName();

				csInitDsgVarValue=csReferencedSectionName;
			}
			break;
	}

	return csInitDsgVarValue;
}

// BEGIN ROMTEAM Cristi Petrescu 99-01- debug
#include "EdIRDV.hpp"
#include "EdIRDVls.hpp"

/**********************************************************************************/
void EdIR_fn_vSaveInitValue(CPA_Actor *pclActor,long lNoDsgVar,SCR_tdst_File_Description *p_tdstFile)
{
	tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclActor->GetData();
	MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
	AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);
// END ROMTEAM Cristi Petrescu 99-01- debug
	//Gets Type
	tdeDsgVarTypeId	eDsgVarTypeId=M_GetDsgVarType(pstMind,lNoDsgVar);
	tdeVariableType	eVariableType=fn_GetDsgVarType(eDsgVarTypeId);
	CString					csDsgVarType=szFindDsgVarTypeScriptNameFromId(eDsgVarTypeId);

	if(  ( eDsgVarTypeId == eDsgVarType_List ) )
	{
		// no value for this type of variable
		return;
	}

	//Saves only if different from model
	void	*pvAddrInit=M_GetDsgVarAddrInit(pstMind,lNoDsgVar);
	void	*pvAddrDefault=M_GetDsgVarAddrDefault(pstMind,lNoDsgVar);

// BEGIN ROMTEAM Cristi Petrescu 99-01- debug
#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
	long lNbElt=0;//, lModelNbElt = 0;
	BOOL bNeedToSave = FALSE;
	if(eVariableType==E_vt_Array)
	{
		tdstArray	*pstArray=(tdstArray *)pvAddrInit;
		lNbElt=M_ARRAY_SIZE(pstArray);

		// extremely ineficient, but is the best I can do
		//CPA_EdIR_Brain *pclBrain = ((EdActors_EditorActorModel *) pclActor -> m_fn_pclGetEditorActor ()) -> m_pclBrain;
		//CPA_Actor *pclModel = pclBrain -> m_fn_pclGetModel ();
		//pclBrain = ((EdActors_EditorActorModel *) pclModel -> m_fn_pclGetEditorActor ()) -> m_pclBrain;
		// ...

		// this is better. does it work?
		//CPA_EdIR_DesignerVariable *pclDsgVar = g_clDsgVarList -> GetAt (pos);
		//if (! pclDsgVar)
		//	cModelNbElt = 0;
		//else
		//	cModelNbElt = pclDsgVar -> m_fn_csGetFirstDesignerVariableValue ();

		
		//CPA_Actor *pclModel = pclActor -> m_fn_pclGetModel ();
		//CPA_EdIR_Brain *pclBrain = ((EdActors_EditorActorModel *) pclModel -> m_fn_pclGetEditorActor ()) -> m_pclBrain;			
		//CPA_EdIR_MainBehaviour *pclVariableDeclarations = pclBrain -> m_clListOfDeclarations . m_fn_pclGetMainBehaviour (g_c_csDsgVarName);
		//if (! pclVariableDeclarations -> m_fn_bHasBeenModified ())
		//	lModelNbElt = lNbElt; // use engine data
		//else
		//{	// use editor data
		//	CString csModelNbElt = g_clDsgVarList . m_fn_csGetFirstValueOfADesignerVariable (lNoDsgVar);
		//	lModelNbElt = atoi (csModelNbElt);
		//}
			

		//if (lNbElt != lModelNbElt)
		//{
		//	bNeedToSave = TRUE;
		//}
		//else
		//{
			unsigned long	ulSize=fn_ulSizeOfDsgVar(M_GetDsgVarType(pstMind,lNoDsgVar),lNbElt);
			if(memcmp(pvAddrInit,pvAddrDefault,ulSize)!=0)
				bNeedToSave = TRUE;
		//}
	}
	else
	{
		unsigned long	ulSize=fn_ulSizeOfDsgVar(M_GetDsgVarType(pstMind,lNoDsgVar),0);
		if(memcmp(pvAddrInit,pvAddrDefault,ulSize)!=0)
			bNeedToSave = TRUE;
	}
	if (bNeedToSave)
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
// END ROMTEAM Cristi Petrescu 99-01- debug
	{
		//Gets Name
		CString csDsgVarName=M_GetDsgVarName(pstMind,lNoDsgVar);

		//Computes Index
		CString	csDsgVarIndex;
		csDsgVarIndex.Format("%ld",lNoDsgVar);

		//Gets Init Value
		void	*pvInitEngineValue=M_GetDsgVarAddrInit(pstMind,lNoDsgVar);

		if(eVariableType==E_vt_Array)
		{
			tdstArray				*pstModelArray=(tdstArray *)pvAddrDefault;
			unsigned long			ulEntrySize=fn_ulSizeOfDsgVar(M_ARRAY_TYPE(pstModelArray),0);
			tdstArray				*pstArray=(tdstArray *)pvInitEngineValue;
			long					lNbElt=M_ARRAY_SIZE(pstArray);

			//Saves Declaration
			CString csNbElt;
// BEGIN ROMTEAM Cristi Petrescu 99-01- debug
			csNbElt.Format("%ld",lNbElt);
			//csNbElt.Format("%ld",lModelNbElt);
			M_SAVEENTRY3PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csNbElt);

			//Saves Values
			SCR_g_ui_SvL0_IndentationLevel++;

			CString	csDsgVarSubType=szFindDsgVarTypeScriptNameFromId(M_ARRAY_TYPE(pstArray));
			for(long lIndex=0;lIndex < lNbElt; lIndex ++)//, lIndex < lModelNbElt;lIndex++)
// END ROMTEAM Cristi Petrescu 99-01- debug
			{
				//Saves only if different from model
#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
				if(memcmp(M_ARRAY_ELEMENT(pstArray,lIndex),M_ARRAY_ELEMENT(pstModelArray,lIndex),ulEntrySize)!=0)
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
				{
					CString	csIndex;
					csIndex.Format("%ld",lIndex);

					CString	csInitDsgVarValue=EdIR_fn_csGetInitialValue(M_ARRAY_ELEMENT(pstArray,lIndex),M_ARRAY_TYPE(pstArray));

					if((eDsgVarTypeId==eDsgVarType_Vector)||(eDsgVarTypeId==eDsgVarType_VectorArray))
					{
						long		lTmpIndex=0;
						CString	csX=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);
						CString	csY=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);
						CString	csZ=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);

#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
						M_SAVEENTRY5PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csX,csY,csZ);
#else /* } { */
                        if(memcmp(M_ARRAY_ELEMENT(pstArray,lIndex),M_ARRAY_ELEMENT(pstModelArray,lIndex),ulEntrySize)!=0)
                        {
                            M_SAVEENTRY5PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csX,csY,csZ);
                        }
                        else
                        {
                            M_SAVECOMMENT5PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csX,csY,csZ);
                        }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
					}
					else
					{
#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
						M_SAVEENTRY3PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csInitDsgVarValue);
#else /* } { */
                        if(memcmp(M_ARRAY_ELEMENT(pstArray,lIndex),M_ARRAY_ELEMENT(pstModelArray,lIndex),ulEntrySize)!=0)
                        {
                            M_SAVEENTRY3PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csInitDsgVarValue);
                        }
                        else
                        {
                            M_SAVECOMMENT3PARMS(csDsgVarSubType,csIndex,"\"@"+csDsgVarName+"\"",csInitDsgVarValue);
                        }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
					}
				}
			}

			SCR_g_ui_SvL0_IndentationLevel--;
		}
		else
		{
			CString	csInitDsgVarValue=EdIR_fn_csGetInitialValue(pvInitEngineValue,eDsgVarTypeId);

			if(eDsgVarTypeId==eDsgVarType_Vector)
			{
				long		lTmpIndex=0;
				CString	csX=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);
				CString	csY=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);
				CString	csZ=fn_csGetNextWord(csInitDsgVarValue,lTmpIndex);

#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
				M_SAVEENTRY5PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csX,csY,csZ);
#else
                if(memcmp(pvAddrInit,pvAddrDefault,ulSize)!=0)
                {
                    M_SAVEENTRY5PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csX,csY,csZ);
                }
                else
                {
                    M_SAVECOMMENT5PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csX,csY,csZ);
                }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
			}
			else
			{
#if !defined(_SAVE_DEFAULT_IN_COMMENT_)  // {
				M_SAVEENTRY3PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csInitDsgVarValue);
#else
                if(memcmp(pvAddrInit,pvAddrDefault,ulSize)!=0)
                {
                    M_SAVEENTRY3PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csInitDsgVarValue);
                }
                else
                {
                    M_SAVECOMMENT3PARMS(csDsgVarType,csDsgVarIndex,csDsgVarName,csInitDsgVarValue);
                }
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
			}
		}
	}
}

/**********************************************************************************/
void EdIR_fn_vSaveInitValuesInBrainMS(CPA_Actor *pclActor,SCR_tdst_File_Description *p_tdstFile)
{
	tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclActor->GetData();
	MS_tdxHandleToBrain	hBrain=pstEngineObject->h_Brain;
	AI_tdstMind					*pstMind=M_pstGetMindOfBrain(hBrain);

	//Saves initial behaviour
	EdIR_fn_vSaveInitialBehaviourInBrainMS(pclActor,p_tdstFile);

	//Saves init value for designer variable
	if(pstMind!=NULL)
	{
		if((AI_M_p_stGetDsgVar(pstMind)!=NULL) &&
			(M_GetDsgMemBufferInit(pstMind)!=NULL) &&
			(M_GetDsgMemDefaultInit(pstMind)!=NULL)
#if !defined (_SAVE_DEFAULT_IN_COMMENT_)  // {
			&&
			(memcmp(M_GetDsgMemBufferInit(pstMind),M_GetDsgMemDefaultInit(pstMind),M_GetDsgVarSize(pstMind))!=0)
#endif /* _SAVE_DEFAULT_IN_COMMENT_ } */
			)
		{
			//Saves Init value
			CString				csSizeOfAllDsgVar;
// BEGIN ROMTEAM Cristi Petrescu 99-01- debug
			long lSize;
			//CPA_Actor *pclModel = pclActor -> m_fn_pclGetModel ();
			//CPA_EdIR_Brain *pclBrain = ((EdActors_EditorActorModel *) pclModel -> m_fn_pclGetEditorActor ()) -> m_pclBrain;			
			//CPA_EdIR_MainBehaviour *pclVariableDeclarations = pclBrain -> m_clListOfDeclarations . m_fn_pclGetMainBehaviour (g_c_csDsgVarName);
			//if (pclVariableDeclarations -> m_fn_bHasBeenModified ())
			//{
				// safety :-(
			//	lSize = g_clDsgVarList.m_fn_lGetSizeOfAllDesignerVariables();
			//}
			//else
				lSize = M_GetDsgVarSize(pstMind);

			csSizeOfAllDsgVar.Format("%ld",lSize);
			//csSizeOfAllDsgVar.Format("%ld",M_GetDsgVarSize(pstMind));
// END ROMTEAM Cristi Petrescu 99-01- debug
			CString				csNbDsgVar;
			csNbDsgVar.Format("%ld",M_GetNbDsgVar(pstMind));

			//Saves special sub section
			SCR_M_SvL0_SaveBeginSection(p_tdstFile,
																M_MAKECHAR(g_c_csCarDecSectionName+":"),
																SCR_CC_C_Cfg_NoChar);

			SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile,
				SCR_EF_SvL0_Normal,
				2,
				M_MAKECHAR(csSizeOfAllDsgVar),
				M_MAKECHAR(csNbDsgVar));

			//Saves entries in this sub section
			for(long lNoDsgVar=0;lNoDsgVar<M_GetNbDsgVar(pstMind);lNoDsgVar++)
			{
				//Saves only if different from model
// BEGIN ROMTEAM Cristi Petrescu 99-01- debug
				EdIR_fn_vSaveInitValue(pclActor,lNoDsgVar,p_tdstFile);
// END ROMTEAM Cristi Petrescu 99-01- debug
			}

			SCRIPT_vSaveEndSection(p_tdstFile);
		}
		else
		{
#if defined (_SHOW_WARNING_) // {
            if(AI_M_p_stGetDsgVar(pstMind)==NULL)
			{
				if(AfxMessageBox("No Variable in Model\nDo you want to debug ???",MB_YESNO) == IDYES)		ASSERT(FALSE);
			}
            if(M_GetDsgMemBufferInit(pstMind)==NULL)
			{
				if(AfxMessageBox("No Init Value in Model\nDo you want to debug ???",MB_YESNO) == IDYES)		ASSERT(FALSE);
			}
            if(M_GetDsgMemDefaultInit(pstMind)==NULL)
			{
				if(AfxMessageBox("No Default Value in Model\nDo you want to debug ???",MB_YESNO) == IDYES)		ASSERT(FALSE);
			}
#endif // _SHOW_WARNING_ }

#if defined (_SAVE_DEFAULT_IN_COMMENT_) // {
			if(AI_M_p_stGetDsgVar(pstMind)==NULL)				SCR_M_SvL0_SaveComment(p_tdstFile,"No Variable in Model");
            if(M_GetDsgMemBufferInit(pstMind)==NULL)            SCR_M_SvL0_SaveComment(p_tdstFile,"No Init Value in Model");
            if(M_GetDsgMemDefaultInit(pstMind)==NULL)           SCR_M_SvL0_SaveComment(p_tdstFile,"No Default Value in Model");
#endif // _SAVE_DEFAULT_IN_COMMENT_ }
		}
	}
}

#endif //D_ED_IR_ACTIVE
