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

/**********************************************************************************/

long pBrain;
long pList;
long pModel;


void CPA_EdIR_Frame::m_fn_vLoadBrain(CPA_Actor *pclModel,CString csModelName)
{
	//Loads Intelligence
	CString csFileName=csModelName+g_c_csIntelligenceFileExtension;
	CString csFileNameQS=csModelName+g_c_csIntelligenceFileExtensionQS;
	CString	csSectionName=csFileName+'^'+g_c_csIntelligenceSectionName;
	CString	csSectionNameQS=csFileNameQS+'^'+g_c_csIntelligenceSectionName;
	BOOL		bQuickSaveFileExists=FALSE;
	BOOL		bLoadQuickSaveFile=FALSE;
	BOOL		bAsksQuestion=TRUE;
	BOOL		bDeleteQuick = FALSE;
	char Path[255], File[255];
	char *psz_Temp;
	CString	csBaseMsg="Would you like to load QuickSave for ";
	char *psz_Famil = fn_szGetFamiliesDataPath();

  pBrain=(long)(((EdActors_EditorActorModel *)pclModel->m_fn_pclGetEditorActor())->m_pclBrain);

	bQuickSaveFileExists=FALSE;
	bDeleteQuick=FALSE;
	if(QuickSaveNoAll == FALSE)
		bQuickSaveFileExists=!LDT_SearchFile(M_MAKECHAR(csFileNameQS), NULL);
	if(bQuickSaveFileExists)
	{
		CString	csMsg=csBaseMsg+csModelName+" model ?";

		if((QuickSaveDeleteAll == FALSE)&&(bAsksQuestion))
		{
			int iRes = QuickSaveDialog(csMsg);
			switch(iRes)
			{
			case IDYES:			bLoadQuickSaveFile = TRUE; break;
			case IDNO:			bLoadQuickSaveFile = FALSE; break;
			case IDNOALL:		bLoadQuickSaveFile = FALSE; QuickSaveNoAll = TRUE; break;
			case IDDELETE:		bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; break;
			case IDDELETEALL:	bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; QuickSaveDeleteAll = TRUE; break;
			}
		}
		bAsksQuestion=FALSE;

		if(bDeleteQuick || QuickSaveDeleteAll)
		{
			strcpy(Path, psz_Famil);
			strcat(Path, "\\");
			strcpy(File, (char *) (LPCSTR) csSectionNameQS);
			psz_Temp = strrchr(File, '^');
			if(psz_Temp)
			{
				*psz_Temp = '\0';
				strcat(Path, File);
				chmod(Path, _S_IWRITE);
				unlink(Path);
			}
			bLoadQuickSaveFile = FALSE;
		}
		else if (bLoadQuickSaveFile)
		{
			CPA_EdIR_Brain	*pclBrain=((EdActors_EditorActorModel *)pclModel->m_fn_pclGetEditorActor())->m_pclBrain;
			pclBrain->m_fn_vSetModified(TRUE);
			pclBrain->m_fn_vNotify();

			// for instances
			CPA_List<CPA_BaseObject> clInstancesList;
			g_pclAIInterface->GetMainWorld()->fn_lFindObjects(&clInstancesList,"",C_szActorInstanceTypeName,pclModel);
	
			POSITION	pos1=clInstancesList.GetHeadPosition();
			while(pos1!=NULL)
			{
				CPA_Actor				*pclInstance=(CPA_Actor *)(clInstancesList.GetNext(pos1));
				tdstEngineObject		*pstEngineObject=(tdstEngineObject *)pclInstance->GetStruct();
				MS_tdxHandleToBrain		hBrain=pstEngineObject->h_Brain;

				if(hBrain)
					AI_M_ucErrorFlag(M_pstGetMindOfBrain(hBrain))=1;
			} 
		}
	}

	if(bLoadQuickSaveFile)
		LDT_LoadSection(M_MAKECHAR(csSectionNameQS));
	else
		LDT_LoadSection(M_MAKECHAR(csSectionName));

	//Loads Reflex
	csFileName=csModelName+g_c_csReflexFileExtension;
	csFileNameQS=csModelName+g_c_csReflexFileExtensionQS;
	csSectionName=csFileName+'^'+g_c_csReflexSectionName;
	csSectionNameQS=csFileNameQS+'^'+g_c_csReflexSectionName;
		
	bQuickSaveFileExists=FALSE;
	if(QuickSaveNoAll == FALSE)
		bQuickSaveFileExists=!LDT_SearchFile(M_MAKECHAR(csFileNameQS), NULL);
	if(bQuickSaveFileExists)
	{
		CString	csMsg=csBaseMsg+csModelName+" model ?";

		if((QuickSaveDeleteAll == FALSE)&&(bAsksQuestion))
		{
			int iRes = QuickSaveDialog(csMsg);
			switch(iRes)
			{
			case IDYES:			bLoadQuickSaveFile = TRUE; break;
			case IDNO:			bLoadQuickSaveFile = FALSE; break;
			case IDNOALL:		bLoadQuickSaveFile = FALSE; QuickSaveNoAll = TRUE; break;
			case IDDELETE:		bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; break;
			case IDDELETEALL:	bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; QuickSaveDeleteAll = TRUE; break;
			}
		}
		bAsksQuestion=FALSE;

		if(bDeleteQuick || QuickSaveDeleteAll)
		{
			strcpy(Path, psz_Famil);
			strcat(Path, "\\");
			strcpy(File, (char *) (LPCSTR) csSectionNameQS);
			psz_Temp = strrchr(File, '^');
			if(psz_Temp)
			{
				*psz_Temp = '\0';
				strcat(Path, File);
				chmod(Path, _S_IWRITE);
				unlink(Path);
			}
			bLoadQuickSaveFile = FALSE;
		}
	}

	if(bLoadQuickSaveFile)
		LDT_LoadSection(M_MAKECHAR(csSectionNameQS));
	else
		LDT_LoadSection(M_MAKECHAR(csSectionName));

	//Loads Declaration
	csFileName=csModelName+g_c_csDeclarationFileExtension;
	csFileNameQS=csModelName+g_c_csDeclarationFileExtensionQS;
	csSectionName=csFileName+'^'+g_c_csDeclarationSectionName;
	csSectionNameQS=csFileNameQS+'^'+g_c_csDeclarationSectionName;
		
	//SCR_M_RdL0_SetContextLong(1,0,(long)(((EdActors_EditorActorModel *)pclModel->m_fn_pclGetEditorActor())->m_pclBrain));
	bQuickSaveFileExists=FALSE;
	if(QuickSaveNoAll == FALSE)
		bQuickSaveFileExists=!LDT_SearchFile(M_MAKECHAR(csFileNameQS), NULL);
	if(bQuickSaveFileExists)
	{
		CString	csMsg=csBaseMsg+csModelName+" model ?";

		if((QuickSaveDeleteAll == FALSE)&&(bAsksQuestion))
		{
			int iRes = QuickSaveDialog(csMsg);
			switch(iRes)
			{
			case IDYES:			bLoadQuickSaveFile = TRUE; break;
			case IDNO:			bLoadQuickSaveFile = FALSE; break;
			case IDNOALL:		bLoadQuickSaveFile = FALSE; QuickSaveNoAll = TRUE; break;
			case IDDELETE:		bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; break;
			case IDDELETEALL:	bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; QuickSaveDeleteAll = TRUE; break;
			}
		}
		bAsksQuestion=FALSE;

		if(bDeleteQuick || QuickSaveDeleteAll)
		{
			strcpy(Path, psz_Famil);
			strcat(Path, "\\");
			strcpy(File, (char *) (LPCSTR) csSectionNameQS);
			psz_Temp = strrchr(File, '^');
			if(psz_Temp)
			{
				*psz_Temp = '\0';
				strcat(Path, File);
				chmod(Path, _S_IWRITE);
				unlink(Path);
			}
			bLoadQuickSaveFile = FALSE;
		}
	}

	if(bLoadQuickSaveFile)
		LDT_LoadSection(M_MAKECHAR(csSectionNameQS));
	else
		LDT_LoadSection(M_MAKECHAR(csSectionName));

	//Loads Macros
	csFileName=csModelName+g_c_csMacroFileExtension;
	csFileNameQS=csModelName+g_c_csMacroFileExtensionQS;
	csSectionName=csFileName+'^'+g_c_csMacroSectionName;
	csSectionNameQS=csFileNameQS+'^'+g_c_csMacroSectionName;
		
	bQuickSaveFileExists=FALSE;
	if(QuickSaveNoAll == FALSE)
		bQuickSaveFileExists=!LDT_SearchFile(M_MAKECHAR(csFileNameQS), NULL);
	if(bQuickSaveFileExists)
	{
		CString	csMsg=csBaseMsg+csModelName+" model ?";

		if((QuickSaveDeleteAll == FALSE)&&(bAsksQuestion))
		{
			int iRes = QuickSaveDialog(csMsg);
			switch(iRes)
			{
			case IDYES:			bLoadQuickSaveFile = TRUE; break;
			case IDNO:			bLoadQuickSaveFile = FALSE; break;
			case IDNOALL:		bLoadQuickSaveFile = FALSE; QuickSaveNoAll = TRUE; break;
			case IDDELETE:		bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; break;
			case IDDELETEALL:	bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; QuickSaveDeleteAll = TRUE; break;
			}
		}
		bAsksQuestion=FALSE;

		if(bDeleteQuick || QuickSaveDeleteAll)
		{
			strcpy(Path, psz_Famil);
			strcat(Path, "\\");
			strcpy(File, (char *) (LPCSTR) csSectionNameQS);
			psz_Temp = strrchr(File, '^');
			if(psz_Temp)
			{
				*psz_Temp = '\0';
				strcat(Path, File);
				chmod(Path, _S_IWRITE);
				unlink(Path);
			}
			bLoadQuickSaveFile = FALSE;
		}
	}

	if(bLoadQuickSaveFile)
			LDT_LoadSection(M_MAKECHAR(csSectionNameQS));
	else
			LDT_LoadSection(M_MAKECHAR(csSectionName));

	//Loads Subrs
	csFileName=csModelName+g_c_csSubrFileExtension;
	csFileNameQS=csModelName+g_c_csSubrFileExtensionQS;
	csSectionName=csFileName+'^'+g_c_csSubrSectionName;
	csSectionNameQS=csFileNameQS+'^'+g_c_csSubrSectionName;
		
	bQuickSaveFileExists=FALSE;
	if(QuickSaveNoAll == FALSE)
		bQuickSaveFileExists=!LDT_SearchFile(M_MAKECHAR(csFileNameQS), NULL);
	if(bQuickSaveFileExists)
	{
		CString	csMsg=csBaseMsg+csModelName+" model ?";

		if((QuickSaveDeleteAll == FALSE)&&(bAsksQuestion))
		{
			int iRes = QuickSaveDialog(csMsg);
			switch(iRes)
			{
			case IDYES:			bLoadQuickSaveFile = TRUE; break;
			case IDNO:			bLoadQuickSaveFile = FALSE; break;
			case IDNOALL:		bLoadQuickSaveFile = FALSE; QuickSaveNoAll = TRUE; break;
			case IDDELETE:		bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; break;
			case IDDELETEALL:	bLoadQuickSaveFile = FALSE; bDeleteQuick = TRUE; QuickSaveDeleteAll = TRUE; break;
			}
		}
		bAsksQuestion=FALSE;

		if(bDeleteQuick || QuickSaveDeleteAll)
		{
			strcpy(Path, psz_Famil);
			strcat(Path, "\\");
			strcpy(File, (char *) (LPCSTR) csSectionNameQS);
			psz_Temp = strrchr(File, '^');
			if(psz_Temp)
			{
				*psz_Temp = '\0';
				strcat(Path, File);
				chmod(Path, _S_IWRITE);
				unlink(Path);
			}
			bLoadQuickSaveFile = FALSE;
		}
	}

	if(bLoadQuickSaveFile)
		LDT_LoadSection(M_MAKECHAR(csSectionNameQS));
	else
		LDT_LoadSection(M_MAKECHAR(csSectionName));

}

/**********************************************************************************/

int CPA_EdIR_Frame::m_fn_iLoadIntelligence( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

	CPA_EdIR_Brain	*p_clBrain=(CPA_EdIR_Brain	*)pBrain;

	pList=long(&(p_clBrain->m_clListOfIntelligenceBehaviours));
	pModel=long(p_clBrain->m_fn_pclGetModel());

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_BeginSection: /* a subsection */
        LDT_LoadSection( NULL );
    }
  }

  return 0;
}

/**********************************************************************************/

int CPA_EdIR_Frame::m_fn_iLoadReflex( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

	CPA_EdIR_Brain	*p_clBrain=(CPA_EdIR_Brain	*)pBrain;

	pList=long(&(p_clBrain->m_clListOfReflexBehaviours));
	pModel=long(p_clBrain->m_fn_pclGetModel());

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_BeginSection: /* a subsection */
        LDT_LoadSection( NULL );
    }
  }

  return 0;
}

/**********************************************************************************/

int CPA_EdIR_Frame::m_fn_iLoadDeclaration( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

	CPA_EdIR_Brain	*p_clBrain=(CPA_EdIR_Brain	*)pBrain;

	pList=long(&(p_clBrain->m_clListOfDeclarations));
	pModel=long(p_clBrain->m_fn_pclGetModel());

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_BeginSection: /* a subsection */
        LDT_LoadSection( NULL );
    }
  }

  return 0;
}

/**********************************************************************************/

int CPA_EdIR_Frame::m_fn_iLoadMacro( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

	CPA_EdIR_Brain	*p_clBrain=(CPA_EdIR_Brain	*)pBrain;

	pList=long(&(p_clBrain->m_clListOfMacros));
	pModel=long(p_clBrain->m_fn_pclGetModel());

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_BeginSection: /* a subsection */
        LDT_LoadSection( NULL );
    }
  }

  return 0;
}

/**********************************************************************************/

int CPA_EdIR_Frame::m_fn_iLoadSubr( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;

	CPA_EdIR_Brain	*p_clBrain=(CPA_EdIR_Brain	*)pBrain;

	pList=long(&(p_clBrain->m_clListOfSubrs));
	pModel=long(p_clBrain->m_fn_pclGetModel());

  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_BeginSection: /* a subsection */
        LDT_LoadSection( NULL );
    }
  }

  return 0;
}

/**********************************************************************************/


int CPA_EdIR_Frame::m_fn_iLoadMainBehaviour( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  CPA_EdIR_MainBehaviourList	*p_clMainBehaviourList=(CPA_EdIR_MainBehaviourList	*)pList;

	static CPA_EdIR_MainBehaviour	*s_pclMainBehaviour;
	static CString								s_csLanguage;

	CPA_Actor *pclModel=(CPA_Actor*)pModel;

	s_pclMainBehaviour=new CPA_EdIR_MainBehaviour(pclModel);

	s_csLanguage="French";


  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szAction=LDT_szGetEntryName();

		    if(strcmpi(szAction,g_c_csBehaviourLanguage)==0)
		    {
			    s_csLanguage=LDT_szGetParam(1);
		    }
		    else
		    if(strcmpi(szAction,g_c_csBehaviourName)==0)
		    {
			    if(s_pclMainBehaviour->fn_eRename(CString(LDT_szGetParam(1)))!=E_mc_None)
				    s_pclMainBehaviour->SetDefaultUniqueName();
		    }
		    else
		    if(strcmpi(szAction,g_c_csBehaviourBoundingBox)==0)
		    {
			    s_pclMainBehaviour->m_crPosition.left=atol(LDT_szGetParam(1));
			    s_pclMainBehaviour->m_crPosition.top=atol(LDT_szGetParam(2));
			    s_pclMainBehaviour->m_crPosition.right=atol(LDT_szGetParam(3));
			    s_pclMainBehaviour->m_crPosition.bottom=atol(LDT_szGetParam(4));
		    }
		    else
		    if(strcmpi(szAction,g_c_csBehaviourText)==0)
		    {
			    //Old file format
			    if(LDT_iGetNbParams()==4)
			    {
				    CFile	cfFile;
				    CString	csFileName;
				    
				    csFileName=fn_szGetFamiliesDataPath()+CString("\\")+
									     CString(LDT_szGetParam(1));

				    if(cfFile.Open(csFileName,CFile::modeRead))
				    {
					    long	lOffset=atol(LDT_szGetParam(1));
					    long	lLen=atol(LDT_szGetParam(2));
					    char	*p_cBuf=new char[lLen+1];

					    cfFile.Seek(lOffset,CFile::begin);
					    cfFile.Read(p_cBuf,lLen);
					    p_cBuf[lLen]=0;

					    s_pclMainBehaviour->m_csText=p_cBuf;

					    delete[] p_cBuf;

					    cfFile.Close();
				    }
			    }
			    //New file format
			    else
			    {
				    CString	csLine=CString(LDT_szGetParam(1));
				    csLine=fn_csReplaceChar(csLine,'\'','\"');
				    s_pclMainBehaviour->m_csText+=csLine+"\xD\xA";
			    }
		    
        }
      }
  	}
  }

	//Deletes the last "\xD\xA" sequence
	if(s_pclMainBehaviour->m_csText.Right(2).CompareNoCase("\xD\xA")==0)
		s_pclMainBehaviour->m_csText=s_pclMainBehaviour->m_csText.Left(s_pclMainBehaviour->m_csText.GetLength()-2);

	//Translates text if needed
	CString	csCurrentLanguage=g_pclAIInterface->M_GetMainWorld()->GetInterface()->fn_csGetCurrentLanguage();
	if(s_csLanguage!=csCurrentLanguage)
	{
		s_pclMainBehaviour->m_csText=fn_csFastTranslateText(s_pclMainBehaviour->m_csText,s_csLanguage,csCurrentLanguage);
	}

	p_clMainBehaviourList->AddTail(s_pclMainBehaviour);

  LDT_SetVolatileSubsection( pLink );

  return 0;
}

