// ai_intf.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#include "ai_intf.hpp"

#include "EdIRFrm.hpp"
#include "EdIRIRD.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRBeha.hpp"
#include "EdIRStrg.hpp"
#include "EdIRDVLs.hpp"
#include "EdIRDVVw.hpp"
#include "EdIRSave.hpp"
#include "EdIRWaW.hpp"

#include "OAc.h"
#include "EDACModl.hpp"

#include "WAW.h"

#include "x:\cpa\main\inc\_EditID.h"
// CPA2 ASilvescu 98-04-30
#include "edirwaw.hpp"
#include "edirdat.hpp"
// End CPA2 ASilvescu 98-04-30

//Stefan Dumitrean 19-06-98 ( choose font )
#include "EdIRRVw.hpp"
//End Stefan Dumitrean 19-06-98 ( choose font )

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

#include "x:\cpa\tempgrp\gam\locale\fonmem.h"
#include "x:\cpa\tempgrp\gam\locale\fonerm.h"

// infos
static CString g_csTIAName = "Artificial Intelligence";
static CString g_csTIAAuthor = "Xavier Billault";
static CString g_csTIAVersion = "V 5.0.6   31/03/99";
static CString g_csTIAFrenchHelpFile = "EdIRfr.hlp";
static CString g_csTIAEnglishHelpFile = "EdIR.hlp";

BOOL g_bTipFirstTime = TRUE;

/* 
 * the DLL global definition
 */
tdstDLLIdentity			g_stAIIdentity;
CPA_EdIR_Interface	*g_pclAIInterface;

#define C_AI_MenuID_SaveDimensions	105
//Stefan Dumitrean 18-06-98 ( choose font )
#define C_AI_MenuID_ChooseFont		106
//End Stefan Dumitrean 18-06-98
#define C_AI_MenuID_ReloadTexts		107
#define C_AI_MenuID_ReloadInit		108

CString g_sReloadMessage;

//////////////////////////////////////////////////////////////////
// class CPA_EdIR_Interface
//////////////////////////////////////////////////////////////////

/**********************************************************************************/
CPA_EdIR_Interface::CPA_EdIR_Interface()
{
	m_p_stDLLIdentity=&g_stAIIdentity;
	
	// Does your DLL can output in main game view ?
	m_stBaseDLLDefinition.bCanOutputIn3DView=FALSE;

	SetCurrent(FALSE);

    SetEditorInfo(g_csTIAName, g_csTIAAuthor, g_csTIAVersion, g_csTIAFrenchHelpFile, g_csTIAEnglishHelpFile);

	m_pclIRMainFrame=NULL;

	m_bIsOpened=FALSE;
	m_bIsProcessing = FALSE;

	//For Watch Window
	WAW_fn_hUserRegister(C_MODULE_NAME,fn_vCallBackForWatchWindow);
}

/*
 *=======================================================================================
 * ACP Messages Functions overload 
 *=======================================================================================
 */

/**********************************************************************************/
void CPA_EdIR_Interface::fn_vJustAfterRegistered()
{
	m_csBrainHelpFileNameAndPath= M_GetMainApp()->m_csEditorDataPath 
															+ CString("Objects\\Actors\\Help\\EdIRfr.hlp");

	CString a_csTypes[] =
	{
		CString(C_szIABehaviourTypeName)
	};

	fn_vRegisterObjectsType(a_csTypes, 1);

	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csIntelligenceSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCallBackLoadIntelligence,
																	SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csReflexSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCallBackLoadReflex,
																	SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csDeclarationSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCallBackLoadDeclaration,
																	SCR_CRC_c_RdL0_ForSection);
	// BEGIN CPA2 Cristi Petrescu 98-03-
	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csMacroSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCallBackLoadMacro,
																	SCR_CRC_c_RdL0_ForSection);
	// END CPA2 Cristi Petrescu 98-03-
	// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csSubrSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCallBackLoadSubr,
																	SCR_CRC_c_RdL0_ForSection);
	// END ROMTEAM CPA2 Cristi Petrescu 98-04-
	SCR_fn_v_RdL0_RegisterCallback(	M_MAKECHAR(g_c_csBehaviourSectionName),
																	CPA_EdIR_Frame::m_fn_tdeCreateMainBehaviour,
																	SCR_CRC_c_RdL0_ForSection);
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vConstruct()
{
	//Creates the main frame (IR)
	if ( m_pclIRMainFrame == NULL )
	{
		m_pclIRMainFrame = new CPA_EdIR_Frame;

		m_pclIRMainFrame->m_fn_vCreateEditor();
	}
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vLevelChanges()
{
	if(m_pclIRMainFrame!=NULL)
		m_pclIRMainFrame->m_fn_vLevelChanges();
}

//**************************************************************************************
BOOL CPA_EdIR_Interface::fn_bAcceptToRunEngine()
{
	if (m_bIsProcessing)
		return FALSE;

	if(m_pclIRMainFrame!=NULL)
		return m_pclIRMainFrame->m_fn_bAcceptToRunEngine();
	else
		return TRUE;
}

//**************************************************************************************
BOOL CPA_EdIR_Interface::fn_bAcceptToSave()
{
	if(m_pclIRMainFrame!=NULL)
		return m_pclIRMainFrame->m_fn_bAcceptToSave();
	else
		return TRUE;
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vBeforeSaveAll()
{
	m_bIsSaving = TRUE;		
			
	if(m_pclIRMainFrame!=NULL)
		m_pclIRMainFrame->m_fn_vBeforeSaveAll();
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vAfterSaveAll()
{
	m_bIsSaving = FALSE;		

	if(m_pclIRMainFrame!=NULL)
		m_pclIRMainFrame->m_fn_vAfterSaveAll();
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vBeforeEngine()
{
	if(m_pclIRMainFrame!=NULL)
		m_pclIRMainFrame->m_fn_vBeforeEngine();

	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if(m_bIsOpened && m_pclIRMainFrame!=NULL)
	{
		g_oFrameGest.mfn_vDisactivateWindow(m_pclIRMainFrame);
		g_oFrameGest.mfn_vActivateWindow(GetInterface()->GetModelFrame());
	}

	g_oFrameGest.mfn_vSetRefresh(TRUE);
//	m_fn_vHideEditor();
}

//**************************************************************************************
void CPA_EdIR_Interface::fn_vBeforeEditor()
{
	if(m_pclIRMainFrame!=NULL)
		m_pclIRMainFrame->m_fn_vBeforeEditor();
}

//**************************************************************************************
BOOL CPA_EdIR_Interface::fn_bOnInvalidateLink(CPA_BaseObject *pOwner, CPA_BaseObject *pReferencedObject, BOOL bDestroyed)
{
	if(pOwner->GetType()==C_szIABehaviourTypeName)
	{
		CPA_EdIR_MainBehaviour *pclMainBehaviour=(CPA_EdIR_MainBehaviour *)pOwner;

		if(pclMainBehaviour->m_pclBehaviour!=NULL)
		{
			pclMainBehaviour->m_pclBehaviour->m_fn_vSetModified(TRUE);
		}
	}

	if(pOwner->GetType()==C_szActorModelTypeName)
	{
		CPA_Actor	*pclActor=(CPA_Actor *)pOwner;

		EdActors_EditorActorModel	*pclModelOfNewInstance=(EdActors_EditorActorModel *)(pclActor->m_fn_pclGetEditorActor());
		if(pclModelOfNewInstance->m_pclBrain!=NULL)
		{
			//Save IA
			pclModelOfNewInstance->m_pclBrain->m_fn_vSetModified(TRUE);
		}
	}

	return FALSE;
}

//***********************************************************************
BOOL CPA_EdIR_Interface::fn_bCanActivateEditor(CPA_List<CPA_BaseObject> *)
{
	return TRUE;
}

//***********************************************************************
void CPA_EdIR_Interface::fn_vOnActivateEditor(CPA_List<CPA_BaseObject> *,BOOL bBackActivated)
{
	m_fn_vShowEditor();
}

//***********************************************************************
BOOL CPA_EdIR_Interface::fn_bCanCloseEditor(void)
{
	return TRUE;
}

//***********************************************************************
void CPA_EdIR_Interface::fn_vOnCloseEditor(void)
{
}

//**************************************************************************************
BOOL CPA_EdIR_Interface::fn_bDefineSubMenu(EDT_SubMenu *p_oEDTSubMenu)
{
	if (p_oEDTSubMenu->GetSubMenuType() == C_SubMenuTool)
	{
		// add entry to reload texts
		p_oEDTSubMenu->AddAnEntry(this, "Reload Texts", C_AI_MenuID_ReloadTexts);

		// add entry to reload initial values
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CPA_Actor	*pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
		if (pclSelectedActor) 
			p_oEDTSubMenu->AddAnEntry(this, "Reload Initial Values ...", C_AI_MenuID_ReloadInit);
	}
/*
	if(p_oEDTSubMenu->GetSubMenuType()==C_SubMenuPreference)
	{
		if(m_bIsOpened)
			p_oEDTSubMenu->AddAnEntry(this,"Save dimensions (AI)",C_AI_MenuID_SaveDimensions,FALSE);
	}
*/
	//Stefan Dumitrean 18-06-98 ( choose font )
/*
	if( p_oEDTSubMenu->GetSubMenuType() == C_SubMenuTool )
		p_oEDTSubMenu->AddAnEntry( this, "Choose AI editor font...", C_AI_MenuID_ChooseFont );
*/
//End Stefan Dumitrean 18-06-98 ( choose font )

	return TRUE;
}


//**************************************************************************************
void CPA_EdIR_Interface::_OnSubMenuCommand(EDT_SubMenu *p_oEDTSubMenu,UINT uiMsgID)
{
	char szSection[256];
	char szText[256];
	
	switch(uiMsgID)
	{
		case C_AI_MenuID_ReloadTexts:
			// status bar
			M_GetMainWnd()->UpdateStatus("Reloading Text... reinit fonts", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			// close corresponding LinkTable
			SCR_fn_v_Link_CloseTable(FON_fn_p_stGetLinkTable());
			// reinit corresponding memory
			FON_fn_vReinitFONMemory();
			// init corresponding structures
			FON_fn_vReInitFonts();
			// status bar
			M_GetMainWnd()->UpdateStatus("Reloading Text... reinit scripts", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			// delete open sections for FON
			SCR_fn_v_RdL0_DeleteOpenSection(FON_C_SectionAddNewStrings, SCR_CDR_c_RdL0_Contains);
			SCR_fn_v_RdL0_DeleteOpenSection(FON_C_SectionNewString, SCR_CDR_c_RdL0_Contains);
			SCR_fn_v_RdL0_DeleteOpenSection(FON_C_SectionNewUpperString, SCR_CDR_c_RdL0_Contains);
			SCR_fn_v_RdL0_DeleteOpenSection(FON_C_SectionNewStringLength, SCR_CDR_c_RdL0_Contains);
			// delete open sections for DSC
			SCR_fn_v_RdL0_DeleteOpenSection(C_EntryAddStringsFile, SCR_CDR_c_RdL0_Contains);
			SCR_fn_v_RdL0_DeleteOpenSection(C_AddLanguage, SCR_CDR_c_RdL0_Contains);
			SCR_fn_v_RdL0_DeleteOpenSection(".txt", SCR_CDR_c_RdL0_Contains);
			// status bar
			M_GetMainWnd()->UpdateStatus("Reloading Text... load text sections", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			// reload corresponding entries
			SCR_fn_v_RdL0_ComputeSectionName(szSection, C_GameScriptFile, C_SectionAddStringsFiles, "");
			SCR_fnp_st_RdL0_AnalyseSection(szSection, SCR_CDF_uw_Anl_ForceAnalyse);
			// status bar
			M_GetMainWnd()->UpdateStatus("Reloading Text : OK.", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			break;

		case C_AI_MenuID_ReloadInit:
			{
			// get selected actor
			CPA_DLLBase	*pclActorDLL		= (CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
			CPA_Actor	*pclSelectedActor	= (CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
			if (!pclSelectedActor)
				break;
			// get corresponding model
			CPA_Actor *pModel = pclSelectedActor->m_fn_bIsAModel() ? pclSelectedActor : pclSelectedActor->m_fn_pclGetModel();
			// status bar
			sprintf(szText, "Reloading Initial Values for instances of model %s", pModel->GetName());
			M_GetMainWnd()->UpdateStatus("", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			// change the callback for the section init
			SCR_tdst_Anl_Callback *pCallback = SCR_fnp_st_RdL0_GetRegisterCallback(C_InitVariables_String, SCR_CRC_c_RdL0_ForSection);
			SCR_tdpfn_Anl_Callback pPreviousCallback = pCallback->pfn_eCallback;										/* Address of function */
			SCR_fn_v_RdL0_DeleteRegisterCallback(C_InitVariables_String, SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Match);
			SCR_fn_v_RdL0_RegisterCallback(C_InitVariables_String, m_fn_tdeCallBackReloadInitValues, SCR_CRC_c_RdL0_ForSection);
			// get all instances of the model
			CPA_List<CPA_BaseObject> clInstancesList;
			g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList, "", C_szActorInstanceTypeName, pModel);
			POSITION	pos=clInstancesList.GetHeadPosition();
			while (pos!=NULL)
			{
				CPA_Actor *pclInstance=(CPA_Actor *)(clInstancesList.GetNext(pos));
				char szBrainSection[256];
				char szInitSection[256];

				g_sReloadMessage = "";
				
				// compute section name				
				SCR_fn_v_RdL0_ComputeSectionName(szBrainSection, (char *)(LPCSTR) pclInstance->GetReferencedSectionName(), "Brain" ,"");
				SCR_fn_v_RdL0_ComputeSectionName(szInitSection, szBrainSection, C_InitVariables_String ,"");
				// analyse section with the new callback
				if (SCR_fn_c_RdL0_IsSectionExists(szInitSection))
				{
					SCR_M_RdL0_SetContextLong(1,0,(long)pclInstance);
					SCR_fnp_st_RdL0_AnalyseSection(szInitSection, SCR_CDF_uw_Anl_ForceAnalyse);
				}

				if (g_sReloadMessage != "")
				{
					MessageBox(NULL, "The following variables had a specific value and were reloaded :\n" + g_sReloadMessage,
							   "Reload values for instance " + pclInstance->GetName(), IDOK);
				}
				else
				{
					MessageBox(NULL, "The instance had no specific values for its designer variables",
							   "Reload values for instance " + pclInstance->GetName(), IDOK);
				}
			}
			// update display
			pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_DisplayActor,(long)NULL);
			pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_DisplayActor,(long)pclSelectedActor);
			// restore the callback for the section init
			SCR_fn_v_RdL0_DeleteRegisterCallback(C_InitVariables_String, SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Match);
			SCR_fn_v_RdL0_RegisterCallback(C_InitVariables_String, pPreviousCallback, SCR_CRC_c_RdL0_ForSection);
			// status bar
			M_GetMainWnd()->UpdateStatus("Reloading Initial Values : OK.", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			}
			break;
/*
		case C_AI_MenuID_SaveDimensions:
			if(m_bIsOpened)
				m_clDocument.m_pub_fn_bSavePreferences();
			break;
*/
//Stefan Dmmumitrean 18-06-98 ( choose font )
/*
		case C_AI_MenuID_ChooseFont:
			if( m_pclIRMainFrame && m_pclIRMainFrame->m_fn_pclGetEdRulesView() )
				(m_pclIRMainFrame->m_fn_pclGetEdRulesView())->m_fn_vChooseFont();
			break;
*/
//End Stefan Dumitrean 18-06-98 ( choose font )

		default :
			break;

	}
}

//***********************************************************************
void CPA_EdIR_Interface::m_fn_vShowEditor()
{
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if(/*!m_bIsOpened && */m_pclIRMainFrame!=NULL)
	{
		g_oFrameGest.mfn_vDisactivateWindow(GetInterface()->GetModelFrame());
		g_oFrameGest.mfn_vActivateWindow(m_pclIRMainFrame);

		m_bIsOpened=TRUE;
	}

	g_oFrameGest.mfn_vSetRefresh(TRUE);

// Shaitan Correction
	CPA_EdIR_Brain *p_clCurrentBrain = m_pclIRMainFrame->m_pclBrain;
//End Shaitan Correction
	
	// CPA2 ASilvescu 98-05-08
// Load watch variables
	CPA_List<CPA_BaseObject> clInstancesList;
	GetInterface()->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,NO_CRITERIA/*pclModel*/);
	POSITION pos = clInstancesList.GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_Actor	*pclInstance = (CPA_Actor *)(clInstancesList.GetNext(pos));
		EdActors_EditorActor *edActor = pclInstance->m_fn_pclGetEditorActor();
		CString name = edActor->m_pclActor->GetName();
		CString iniFileName = M_GetMainApp()->m_csEditorDataPath;
		iniFileName += "watch.dat";
		char var[16384], *c;
		GetPrivateProfileSection(name, var, sizeof(var), iniFileName);
		for(c=var; *c; c+=lstrlen(c)+1)
		{
			CTL_Editor_Data *data = NULL;
			char *d = strchr(c, '=');
			if(d)
			{
				*d++ = 0;
				if(!lstrcmpi(d, "Designer Variables"))
				{
					m_pclIRMainFrame->m_fn_vShowBrain(((EdActors_EditorActorModel *)pclInstance->m_fn_pclGetModel()->m_fn_pclGetEditorActor())->m_pclBrain);
					CTL_Editor_DataList	*datalist = edActor->m_pclListOfDesignerVariableType;
					data = datalist->m_fn_pclGetDataFromName(CString(c));
					if(data)
					{
						fn_vAddValueInWatch(data);
						fn_vSetInWatch(data,TRUE);
					}
				}
			}
			c = d;
		}
	}
// End CPA2 ASilvescu 98-05-08

// Shaitan Correction
	if (p_clCurrentBrain)
		m_pclIRMainFrame->m_fn_vShowBrain(p_clCurrentBrain);
//End Shaitan Correction

	// BEGIN ROMTEAM Cristi Petrescu 98-08-
	//Enable windows
	((CWnd *) m_pclIRMainFrame -> m_fn_pclGetDiagrammeView ()) -> EnableWindow (TRUE);
	((CWnd *) m_pclIRMainFrame -> m_fn_pclGetButtonView ()) -> EnableWindow (TRUE);
	// END ROMTEAM Cristi Petrescu 98-08-
}

//***********************************************************************
void CPA_EdIR_Interface::m_fn_vHideEditor()
{						  
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if(m_bIsOpened && m_pclIRMainFrame!=NULL)
	{
		g_oFrameGest.mfn_vDisactivateWindow(m_pclIRMainFrame);
		g_oFrameGest.mfn_vActivateWindow(GetInterface()->GetModelFrame());
		m_bIsOpened=FALSE;
	}

	g_oFrameGest.mfn_vSetRefresh(TRUE);
}

/*
 *=======================================================================================
 * tool dll functions overload
 *=======================================================================================
 */

/**********************************************************************************/
long CPA_EdIR_Interface::OnQueryAction(CPA_EditorBase *p_oSender,WPARAM wParam,LPARAM lParam)
{
	long	lResult=0;

	switch(wParam)
	{
		case C_uiAI_GetNewDesignerVariableList:
			{
				lResult=(long) new CPA_EdIR_DesignerVariableList;
			}
			break;

		case C_uiAI_DeleteDesignerVariableList:
			{
				delete (CPA_EdIR_DesignerVariableList *)lParam;

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_UpdateDisplayWithValues:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)pstCom->pvThis;
				CTL_Editor_DataList							*pclDataList=(CTL_Editor_DataList *)pstCom->pvData;

				pclDesignerVariableView->m_fn_vUpdateDisplayWithValues(pclDataList);

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_UpdateDisplay:
			{
				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)lParam;

				pclDesignerVariableView->m_fn_vUpdateDisplay();

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_HideVariables:
			{
				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)lParam;

				pclDesignerVariableView->mfn_vHideVariables(TRUE);

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_ShowVariables:
			{
				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)lParam;

				pclDesignerVariableView->mfn_vHideVariables(FALSE);

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_UpdateControls:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)pstCom->pvThis;
				CTL_Editor_ControlList					*pclControlList=(CTL_Editor_ControlList *)pstCom->pvData;

				pclDesignerVariableView->m_fn_vUpdateControls(pclControlList);

				lResult=1;
			}
			break;

		case C_uiAI_DesignerVariableView_GetOldHeight:
			{
				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)lParam;

				lResult=pclDesignerVariableView->m_fn_ulGetOldHeight();
			}
			break;

		case C_uiAI_DesignerVariableView_SetOldHeight:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)pstCom->pvThis;
				unsigned long										ulOldHeight=pstCom->lData;

				pclDesignerVariableView->m_fn_vSetOldHeight(ulOldHeight);

				lResult=1;
			}
		break;

		case C_uiAI_DesignerVariableView_GetRuntimeClass:
			{
				lResult=(long)RUNTIME_CLASS(CPA_EdIR_DesignerVariablesView);
			}
			break;

		case C_uiAI_GetNewIA:
			{
				lResult=(long) new CPA_EdIR_Brain((CPA_Actor *)lParam);
			}
			break;

		case C_uiAI_GetNewCopyOfIA:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)pstCom->pvThis;
				CPA_Actor				*pclModel=(CPA_Actor *)pstCom->pvData;

				lResult=(long) new CPA_EdIR_Brain(pclModel,pclBrain);
			}
			break;

		case C_uiAI_DeleteIA:
			{
				delete (CPA_EdIR_Brain *)lParam;

				lResult=1;
			}
			break;

		case C_uiAI_ShowIA:
			{
				m_pclIRMainFrame->m_fn_vShowBrain((CPA_EdIR_Brain *)lParam);

				lResult=1;
			}
			break;

		case C_uiAI_LoadIA:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor	*pclModel=(CPA_Actor *)pstCom->pvThis;
				CString		*pcsName=(CString *)pstCom->pvData;

				m_pclIRMainFrame->m_fn_vLoadBrain(pclModel,*pcsName);

				lResult=1;
			}
			break;

// Shaitan => state list in the level
		case C_uiAI_ProcessModifiedIA:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor	*pclModel=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor	*pclInstance=(CPA_Actor *)pstCom->pvData;

				EdActors_EditorActorModel	*pclModelOfNewInstance=(EdActors_EditorActorModel *)(pclModel->m_fn_pclGetEditorActor());
				if (pclModelOfNewInstance->m_pclBrain &&
					(!pclModelOfNewInstance->m_pclBrain->m_fn_bIAIsAlreadyProcessed() ||
					 pclModelOfNewInstance->m_pclBrain->m_bHasBeenModified))
				{
					if(pclModelOfNewInstance->m_pclBrain->m_fn_bProcessIA(FALSE,pclInstance))
					{
						lResult=1;
					}
					else
					{
						lResult=0;
					}
				}
				else
				{
					lResult=0;
				}
			}
			break;
// End Shaitan => state list in the level

// Shaitan => optimisation lists in the level
		case C_uiAI_UpdateAndProcessIA:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor	*pclModel=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor	*pclInstance=(CPA_Actor *)pstCom->pvData;

				EdActors_EditorActorModel	*pclModelOfNewInstance=(EdActors_EditorActorModel *)(pclModel->m_fn_pclGetEditorActor());
				if(pclModelOfNewInstance->m_pclBrain!=NULL)
				{
					pclModelOfNewInstance->m_pclBrain->m_fn_vSetModified(TRUE);
					if(pclModelOfNewInstance->m_pclBrain->m_fn_bProcessIA(TRUE,pclInstance))
					{
						lResult=1;
					}
					else
					{
						lResult=0;
					}
				}
				else
				{
					lResult=0;
				}
			}
			break;
// End Shaitan => optimisation lists in the level

		case C_uiAI_ProcessIA:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor	*pclModel=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor	*pclInstance=(CPA_Actor *)pstCom->pvData;

				EdActors_EditorActorModel	*pclModelOfNewInstance=(EdActors_EditorActorModel *)(pclModel->m_fn_pclGetEditorActor());
				if(pclModelOfNewInstance->m_pclBrain!=NULL)
				{
					if(pclModelOfNewInstance->m_pclBrain->m_fn_bProcessIA(TRUE,pclInstance))
					{
						lResult=1;
					}
					else
					{
						lResult=0;
					}
				}
				else
				{
					lResult=0;
				}
			}
			break;

		case C_uiAI_SaveIA:
			{
				CPA_Actor	*pclActor=(CPA_Actor *)lParam;

				EdActors_EditorActorModel	*pclModelOfNewInstance=(EdActors_EditorActorModel *)(pclActor->m_fn_pclGetEditorActor());
				if(pclModelOfNewInstance->m_pclBrain!=NULL)
				{
					pclModelOfNewInstance->m_pclBrain->m_fn_vSetModified(TRUE);
				}

				lResult=1;
			}
			break;

		case C_uiAI_UpdateIA:
			{
				OnQueryAction(g_pclAIInterface,C_uiAI_ProcessIA,lParam);
				OnQueryAction(g_pclAIInterface,C_uiAI_SaveIA,lParam);
			}
			break;

		case C_uiAI_SaveInitValuesInBrainMS:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				SCR_tdst_File_Description	*pstFile=(SCR_tdst_File_Description *)pstCom->pvData;

				EdIR_fn_vSaveInitValuesInBrainMS(pclInstance,pstFile);

				lResult=1;
			}
			break;

		case C_uiAI_MustSaveAIEntryInModel:
			{
				CPA_EdIR_Brain	*pclBrain=(CPA_EdIR_Brain *)lParam;

				CPA_EdIR_MainBehaviour	*pclDsgVarBehaviour=NULL;
				if(!pclBrain->m_clListOfDeclarations.IsEmpty())
					pclDsgVarBehaviour=pclBrain->m_clListOfDeclarations.GetHead();

				if(
						(!pclBrain->m_clListOfIntelligenceBehaviours.IsEmpty())
						||
						(!pclBrain->m_clListOfReflexBehaviours.IsEmpty())
						// BEGIN CPA2 Cristi Petrescu 98-03-
						||
						(!pclBrain->m_clListOfMacros.IsEmpty())
						// END CPA2 Cristi Petrescu 98-03-
						// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
						||
						(!pclBrain->m_clListOfMacros.IsEmpty())
						// END ROMTEAM CPA2 Cristi Petrescu 98-04-
						||
						(
							(pclDsgVarBehaviour!=NULL)
							&&
							(
								(!pclDsgVarBehaviour->m_csText.IsEmpty())
								||
								(
									(pclDsgVarBehaviour->m_pclBehaviour!=NULL)
									&&
									(!pclDsgVarBehaviour->m_pclBehaviour->m_fn_csGetRules().IsEmpty())
								)
							)
						)
					)
				{
					lResult=(long)TRUE;
				}
				else
				{
					lResult=(long)FALSE;
				}
			}
			break;

		case C_uiAI_GetDsgVarName:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				long											lDsgVarIndex=pstCom->lData;

				pstCom->csData=pclActorModel->m_pclBrain->m_fn_csGetDsgVarName(lDsgVarIndex);
			}
			break;

		case C_uiAI_ShowBehaviour:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				struct tdstNodeInterpret_	*pstNode=(struct tdstNodeInterpret_	*)pstCom->pvData;

				pclActorModel->m_pclBrain->m_fn_vShowBehaviour(pstNode);
			}
			break;

		case C_uiAI_ShowEditor:
		{
			CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
			CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);
			m_fn_vShowEditor();
			pclDesignerVariableView->m_fn_vUpdateDisplay();

			// display Tip of the Day
			GetInterface()->fn_vDisplayTipOfDay("Intelligence", "TIA", g_bTipFirstTime);
			g_bTipFirstTime = FALSE;
			break;
		}

		case C_uiAI_HideEditor:
		{
			CPA_DLLBase											*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
			CPA_EdIR_DesignerVariablesView	*pclDesignerVariableView=(CPA_EdIR_DesignerVariablesView *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetDesignerVariablesView,0);
			m_fn_vHideEditor();
			pclDesignerVariableView->m_fn_vUpdateDisplay();
			break;
		}

		case C_uiAI_HasLostFocus:
			m_bIsOpened=FALSE;
			break;

		//BEGIN ROMTEAM Cristi Petrescu 98-05-
		case C_uiAI_MustBuildDebugInfo:
			{
				tdstActorsIACom *pstCom = (tdstActorsIACom *) lParam;
			
				CPA_Actor *pclInstance = (CPA_Actor *) pstCom -> pvThis;
				CPA_Actor *pclModel = pclInstance -> m_fn_pclGetModel ();
				EdActors_EditorActorModel *pclActorModel = pclModel -> m_fn_pclGetEditorModel ();
				BOOL bBuild = *((BOOL *) (pstCom -> pvData));

				pclActorModel -> m_pclBrain -> m_fn_vMustBuildDebugInfo (bBuild);
			}
			break;

		case C_uiAI_GetBehaviourName:
			{
				tdstActorsIACom *pstCom = (tdstActorsIACom *) lParam;
			
				CPA_Actor *pclInstance = (CPA_Actor *) pstCom -> pvThis;
				CPA_Actor *pclModel = pclInstance -> m_fn_pclGetModel ();
				EdActors_EditorActorModel *pclActorModel = pclModel -> m_fn_pclGetEditorModel ();
				struct tdstNodeInterpret_ *pstNode=(struct tdstNodeInterpret_	*)pstCom->pvData;

				pstCom -> csData = pclActorModel -> m_pclBrain -> m_fn_csGetBehaviourName (pstNode);
			}
			break;
		/*
		case C_uiAI_StartExecutedCodeDisplay:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				CString csBehaviourName = *((CString *) pstCom->pvData);

				pclActorModel->m_pclBrain->m_fn_vStartExecutedCodeDisplay (csBehaviourName);
			}
			break;

		case C_uiAI_StopExecutedCodeDisplay:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				CString csBehaviourName = *((CString *) pstCom->pvData);

				pclActorModel->m_pclBrain->m_fn_vStopExecutedCodeDisplay (csBehaviourName);
			}
			break;
		*/
		case C_uiAI_ClearExecutedNodes:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				CString csBehaviourName = *((CString *) pstCom->pvData);

				pclActorModel->m_pclBrain->m_fn_vClearExecutedNodes (csBehaviourName);
			}
			break;

		case C_uiAI_SetOneExecutedNode:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				struct tdstNodeInterpret_	*pstNode=(struct tdstNodeInterpret_	*)pstCom->pvData;

				pclActorModel->m_pclBrain->m_fn_vSetOneExecutedNode (pstNode);
			}
			break;

		case C_uiAI_ShowBehaviourByEngineNode:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				struct tdstNodeInterpret_	*pstNode=(struct tdstNodeInterpret_	*)pstCom->pvData;

				// BEGIN ROMTEAM Cristi Petrescu 98-06-
				m_pclIRMainFrame -> m_pclDebugBrain = pclActorModel->m_pclBrain;
				// END ROMTEAM Cristi Petrescu 98-06-

				// BEGIN ROMTEAM Cristi Petrescu 98-08-
				//Disable windows
				((CWnd *) m_pclIRMainFrame -> m_fn_pclGetDiagrammeView ()) -> EnableWindow (FALSE);
				((CWnd *) m_pclIRMainFrame -> m_fn_pclGetButtonView ()) -> EnableWindow (FALSE);
				// END ROMTEAM Cristi Petrescu 98-08-

				pclActorModel->m_pclBrain->m_fn_vShowBehaviour (pstNode);
			}
			break;

		case C_uiAI_ShowBehaviourByName:
			{
				tdstActorsIACom *pstCom=(tdstActorsIACom *)lParam;

				CPA_Actor									*pclInstance=(CPA_Actor *)pstCom->pvThis;
				CPA_Actor									*pclModel=pclInstance->m_fn_pclGetModel();
				EdActors_EditorActorModel *pclActorModel=pclModel->m_fn_pclGetEditorModel();
				CString csBehaviourName = *((CString *) pstCom->pvData);

				// BEGIN ROMTEAM Cristi Petrescu 98-06-
				m_pclIRMainFrame -> m_pclDebugBrain = pclActorModel->m_pclBrain;
				// END ROMTEAM Cristi Petrescu 98-06-

				// BEGIN ROMTEAM Cristi Petrescu 98-08-
				//Disable windows
				((CWnd *) m_pclIRMainFrame -> m_fn_pclGetDiagrammeView ()) -> EnableWindow (FALSE);
				((CWnd *) m_pclIRMainFrame -> m_fn_pclGetButtonView ()) -> EnableWindow (FALSE);
				// END ROMTEAM Cristi Petrescu 98-08-

				pclActorModel->m_pclBrain->m_fn_vShowBehaviour (csBehaviourName);
			}
			break;

		case C_uiAI_ShowFlags:
			{
				switch (lParam)
				{
				case E_MarkExecutedLines:
					if (m_pclIRMainFrame )
					m_pclIRMainFrame -> m_fn_vMarkExecutedLines ();
					break;
				case E_ShowTestsAndActions:
					m_pclIRMainFrame -> m_fn_vShowTestsAndActions ();
					break;
				case E_ShowActions:
					m_pclIRMainFrame -> m_fn_vShowActions ();
					break;
				}
				if (m_pclIRMainFrame )
				m_pclIRMainFrame -> m_pclBrain -> m_fn_vShowBehaviour (NULL);
			}
			break;

		// BEGIN ROMTEAM Cristi Petrescu 98-08-
		case C_uiAI_GetVariableIndex:
			{
				CString csVariableName = * ((CString *) lParam);
				lResult = m_pclIRMainFrame -> m_pclDebugBrain -> m_fn_lGetVariableIndex (csVariableName);
			}
			break;
		// END ROMTEAM Cristi Petrescu 98-08-

		case C_uiAI_AddVariableInWatch:
			{
				CTL_Editor_Data *pclData = (CTL_Editor_Data *) lParam;

				fn_vAddValueInWatch(pclData);
				fn_vSetInWatch(pclData,TRUE);
			}
			break;
	
		default:
			break;
	}

	return lResult;
}


/**********************************************************************************/
SCR_tde_Anl_ReturnValue CPA_EdIR_Interface::m_fn_tdeCallBackReloadInitValues(SCR_tdst_File_Description *pfFile,  char *szAction, char *szPars[], SCR_tde_Anl_Action eType)
{
	tdstEngineObject	*pstEngineObject;
	AI_tdstMind			*p_stMind;
	CPA_Actor			*pclInstance;

	if (eType==SCR_EA_Anl_BeginSection)
	{
		SCR_M_RdL0_GetContextLong(0,0,CPA_Actor *,pclInstance);
		SCR_M_RdL0_SetSectionLong(0, 0, (long) pclInstance);
	}

	if (eType == SCR_EA_Anl_Entry)
	{
		long							lNumDsgVar;
		CString							csDsgVarType = szAction;
		CString							csDsgVarName = szPars[1];
		CString							csDsgVarValue = szPars[2];

		SCR_M_RdL0_GetSectionLong(0, 0, CPA_Actor *,  pclInstance);
		
		pstEngineObject = (tdstEngineObject *)pclInstance->GetStruct();
		p_stMind = (pstEngineObject->h_Brain) ? M_pstGetMindOfBrain(pstEngineObject->h_Brain) : NULL;

		if (p_stMind)
		{
			char cElement=(szPars[1][0]=='@');
			char *szName=&szPars[1][cElement];

			// get corresponding variable
			for (lNumDsgVar = 0; lNumDsgVar < M_GetNbDsgVar(p_stMind); lNumDsgVar++)
			{
				if (!stricmp(M_GetDsgVarName(p_stMind, lNumDsgVar), szName))
					break;
			}

			// if variable was found
			if (lNumDsgVar < M_GetNbDsgVar(p_stMind))
			{
				tdeDsgVarTypeId eDsgVarTypeId=fn_eFindDsgVarTypeIdFromScriptName(szAction);

				// element of array
				if (cElement)
				{
					unsigned char ucIndex= (unsigned char) atoi(szPars[0]);
					
					// check the type
					if (fn_GetDsgVarType(M_GetDsgVarType(p_stMind,lNumDsgVar)) == E_vt_Array)
					{
						// check the element
						tdstArray *p_stArray =(tdstArray*) M_GetDsgVarAddrInit(p_stMind,lNumDsgVar);
						if (eDsgVarTypeId ==M_ARRAY_TYPE(p_stArray))
						{
							// check index
							if (ucIndex<M_ARRAY_SIZE(p_stArray))
							{
								fn_vInitDsgVarArrayInit(p_stMind,lNumDsgVar,ucIndex,eDsgVarTypeId,szPars);

								CString csVariable;
								csVariable.Format("\n array %s : element n°%ld", szName, ucIndex);
								g_sReloadMessage += csVariable;
							}
						}
					}
				}
				// single value
				else
				{
					// check if thi is the right type
					if (eDsgVarTypeId == M_GetDsgVarType(p_stMind,lNumDsgVar))
					{
						// set new engine value for this variable
						fn_vInitDsgVarInit(p_stMind,lNumDsgVar,eDsgVarTypeId,szPars);

						CString csVariable;
						csVariable.Format("\n variable %s", szName);
						g_sReloadMessage += csVariable;
					}
				}
			}
		}
	}

	return SCR_ERV_Anl_NormalReturn;
}
// END CPA2 Cristi Petrescu 98-03-
