#include "StdAfx.h"

#include "ACP_Base.h"
#include "TAC.h"
#include "TFa.h"
#include "ITF.h"

#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"
#include "GAM\MainChar.h"

#include "EDACDoc.hpp"
#include "EDACStrg.hpp"
#include "_AInterf.hpp"
#include "EDACGlob.hpp"
#include "EDACMISv.hpp"
#include "ErO.h"

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

#define C_SAVE_CURRENT_POS	2
#define C_SAVE_INITIAL_POS	0
#define C_SAVE_POS					C_SAVE_INITIAL_POS
//#define C_SAVE_POS					C_SAVE_CURRENT_POS

/////////////////////////////////////
// Motor Levels : Saving functions //
/////////////////////////////////////

//*************************************************************************
// Motor Level Saving Function
//*************************************************************************
void EdActors_MyDocument::m_fn_vSaveMotorLevel(SCR_tdst_File_Description *p_tdstFile,
																							 CString csSectionName,
																							 SCR_tde_Ntfy_Action _eAction)
{
	//Jumps to the end of the File, if Section does not exist
	if ( _eAction == SCR_EA_Ntfy_AddSection )
		SCR_fn_v_SvL1_ToEndSection(p_tdstFile);

	//Gets info on the file
	BOOL bLevelFile;
	CString csLevelName;
	CString csFileName = p_tdstFile->a_szFileName;
//	short wIndex = csFileName.ReverseFind('\\');
	CString csExtension(C_ScriptLevelSuffixe);
	CString csRealExtension	= csFileName.Right(csExtension.GetLength());
	if ( csRealExtension.CompareNoCase(csExtension) == 0 )
		bLevelFile = TRUE;
	else
		bLevelFile = FALSE;

	CString csCurrentLine;

	//Extracts Actors name from Section
	//(this is the name of the Actor which caused this Notification to be raised)
	CString csActorName = csSectionName;
	short wIndex = csActorName.ReverseFind(':');
	ERROR_ASSERT( wIndex != -1 );

	csActorName = csActorName.Right(csActorName.GetLength() - wIndex - 1);

	BOOL bMustSaveThisOne;
	BOOL bActorFound = FALSE;
	CPA_Actor *pclCurrentActor;
	HIE_tdxHandleToSuperObject h_MotorActorSO;
	
	CPA_BaseObjectList *pclConcernedList = ( bLevelFile ) ? &(g_pclInterface->m_clDocument.m_clInstancesList)
																												: &(g_pclInterface->m_clDocument.m_clAlwaysList);

	Position pos = pclConcernedList->GetHeadPosition();
	while ( ( pos != 0 ) && ( !bActorFound ) )
	{
		//Gets the Actor	 
		pclCurrentActor = (CPA_Actor *)pclConcernedList->GetNext(pos);

		h_MotorActorSO = pclCurrentActor->m_pub_fn_hGetMotorSuperObject();
		
		bMustSaveThisOne =		( HIE_fn_ulGetSuperObjectType(h_MotorActorSO) == HIE_C_ulActor )
											&&	(		( (bLevelFile) && (!pclCurrentActor->m_fn_bIsAnAlways()) )
													||	( (!bLevelFile) && (pclCurrentActor->m_fn_bIsAnAlways()) )
													);

		if	(	bMustSaveThisOne )
		{
			bActorFound = ( csActorName.Compare(pclCurrentActor->GetName()) == 0 );

			if ( bActorFound )											
			{
				CString csAction = bLevelFile ? C_SectionLevelCharacters : C_SectionLevelAlways;
				csCurrentLine.Format("%s:%s", csAction, 
																			pclCurrentActor->GetName());

				if /*( MC_fn_ucIsCharacterInMainCharacterList(h_MotorActorSO) ) */
					((*fn_p_ulStdGameGetCustomBitsInit(M_GetMSHandle(h_MotorActorSO,StandardGame)))&GAM_C_CustBitRayman)
				{
					SCR_M_SvL0_SaveBeginSection(p_tdstFile, M_MAKECHAR(csCurrentLine), SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Normal, 2, C_LevelPrincipalCharacter, C_LevelCharacterLauchingSounds);
				}
				else if ( h_MotorActorSO == g_stEngineStructure.h_StdCamCaracter )
				{
					SCR_M_SvL0_SaveBeginSection(p_tdstFile, M_MAKECHAR(csCurrentLine), SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Normal, 1, C_LevelStandardCamera);
				}
				else
				{
					SCR_M_SvL0_SaveBeginSection(p_tdstFile, M_MAKECHAR(csCurrentLine), SCR_CC_C_Cfg_EOL);
				}
				
				//*** Link character ***
				SCR_M_SvL0_SaveEntry(p_tdstFile, C_LevelLinkCharacters, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Normal, 1,
																		M_MAKECHAR(pclCurrentActor->GetReferencedSectionName()));
				
				if ( bLevelFile )
				{
// ANNECY MT - 19/11/98 {
					//*** In All SubMaps ??? ***
					if ( pclCurrentActor -> m_pub_fn_bIsInAllSubMaps() )
					{
						SCR_M_SvL0_SaveEntry(p_tdstFile, C_LevelCharacterIsInAllSubmap, SCR_CC_C_Cfg_NoChar);
						SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Normal, 1, "");
					}
// END ANNECY MT }
					//*** character's positions ***	(CURRENT Positions)
					SCR_M_SvL0_SaveEntry(p_tdstFile,C_LevelCharacterMatrixTranslation,SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Scanf,4,
						"%f,%f,%f",
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 0,0,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 0,0,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 0,0,2)
						);
					
					SCR_M_SvL0_SaveEntry(p_tdstFile,C_LevelCharacterMatrixRotation,SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Scanf,10,
						"%f,%f,%f,%f,%f,%f,%f,%f,%f",
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,0,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,0,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,0,2),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,1,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,1,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,1,2),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,2,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,2,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 1,2,2)
						);
					
					SCR_M_SvL0_SaveEntry(p_tdstFile,C_LevelCharacterMatrixScale,SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Scanf,10,
						"%f,%f,%f,%f,%f,%f,%f,%f,%f",
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,0,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,0,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,0,2),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,1,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,1,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,1,2),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,2,0),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,2,1),
						fn_v3dDataGetMatrixElement(M_GetMSHandle(h_MotorActorSO,3dData),C_SAVE_POS, 2,2,2)
						);
				}

				//*** Always Active ***
				if ( (!pclCurrentActor->m_fn_bIsAnAlways()) /*&& (!pclCurrentActor->m_fn_bIsTheWorldActor())*/ )
					if ( fn_bGetIfCharacterIsInAlwaysActiveCharacterList(h_MotorActorSO) )
					{
						SCR_M_SvL0_SaveEntry(p_tdstFile, C_LevelCharacterIsAlwaysActive, SCR_CC_C_Cfg_NoChar);
						SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Normal, 1, "");
					}

//ANNECY Shaitan SPOFlags 19/02/98
				//*** SPO Flags ***
				unsigned long ulFlags = HIE_fn_SO_ulGetFlags(pclCurrentActor->m_pub_fn_pclGetSuperObject()->GetStruct());
				SCR_M_SvL0_SaveEntry(p_tdstFile,C_LevelCharacterFlags,SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Scanf,1, "%d", ulFlags);
//ENDANNECY Shaitan SPOFlags

//ANNECY Shaitan Transparency (26/03/98) {
				//*** Transparency ***
				float fTransparency = HIE_fn_fGetSuperObjectTransparenceLevel(pclCurrentActor->m_pub_fn_pclGetSuperObject()->GetStruct());
				SCR_M_SvL0_SaveEntry(p_tdstFile,C_LevelCharacterTransparency,SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_tdstFile, SCR_EF_SvL0_Scanf,1, "%f", fTransparency);
//ENDANNECY Shaitan Transparency }

					SCR_M_SvL0_SaveEndSection(p_tdstFile, SCR_CC_C_Cfg_EOL);
			}
		}
	}

	EDACTORS_fn_vGiveProgressInfo("Motor Level saved by Actors Editor ...", -1);
}


////////////////////////////////////////
// Motor Instances : Saving functions //
////////////////////////////////////////

//*************************************************************************
// Motor Instances Saving Function
//*************************************************************************
void EdActors_MyDocument::m_fn_vSaveMotorInstance(SCR_tdst_File_Description *p_tdstFile,
																									CPA_Actor *p_clActor)
{
  //Saves Scripts header
	SCR_M_SvL0_SaveScriptFileHeader(p_tdstFile);
	
	//Actors Editor header
	CString csNextAndPreviousLine = "+++++++++++++++++++++++++++++++++++++++++++";
	CString csCurrentLine =					"+              Actors Editor              +";
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csNextAndPreviousLine));
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csCurrentLine));
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csNextAndPreviousLine));
 	csCurrentLine =					"+   Version " + g_pclInterface->m_fn_csGetEditorVersion() + "   +";
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csCurrentLine));
	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 ");
  
	csCurrentLine =	" Save date : " + csDate;
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csCurrentLine));
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csNextAndPreviousLine));
	SCR_M_SvL0_SaveBlankLine(p_tdstFile);

	csCurrentLine.Format("########### Actor %s ###########", p_clActor->GetName());
	csNextAndPreviousLine = "######################";
	while ( csNextAndPreviousLine.GetLength() < csCurrentLine.GetLength() )
		csNextAndPreviousLine += '#';

	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csNextAndPreviousLine));
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csCurrentLine));
	SCR_M_SvL0_SaveComment(p_tdstFile, M_MAKECHAR(csNextAndPreviousLine));
	SCR_M_SvL0_SaveBlankLine(p_tdstFile);

	//Begins section
	csCurrentLine = CString(C_SectionOneCharacters) + ':' + p_clActor->GetName();
	SCR_M_SvL0_SaveBeginSection(p_tdstFile,
															M_MAKECHAR(csCurrentLine),
															SCR_CC_C_Cfg_EOL);

	//Saves all MS
	m_fn_vSaveAllMSOfMotorInstance(p_tdstFile, p_clActor);

	//End section
	SCR_M_SvL0_SaveEndSection(p_tdstFile, SCR_CC_C_Cfg_EOL);
}	 

//*************************************************************************
void EdActors_MyDocument::m_fn_vSaveAllMSOfMotorInstance(	SCR_tdst_File_Description *p_tdstFile,
																													CPA_Actor *p_clActor)
{
	struct tdstEngineObject_ *p_tdstObject = (struct tdstEngineObject_ *)p_clActor->GetData();

	//Saves all MS
	///////////////////////////////////////
	// BE CAREFUL : Order is important ! //
	///////////////////////////////////////
	ED_ACTORS_fn_v3dDataSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vStdGameSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vBrainSaveMiniStructure(p_clActor,p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vDynamSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vCineinfoSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vCollSetSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vSectInfoSaveMiniStructure(p_tdstFile, p_tdstObject);
//ANNECY BBB 20/10/97 {
  ED_ACTORS_fn_vAnimEffectSaveMiniStructure(p_tdstFile, p_tdstObject);
//ENDANNECY BBB 20/10/97 }
	ED_ACTORS_fn_vMicroSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vMSWaySaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vLightSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vPrtSourceSaveMiniStructure(p_tdstFile, p_tdstObject);
	ED_ACTORS_fn_vSoundSaveMiniStructure(p_tdstFile, p_tdstObject);
// CHINA QZY (MT) 26/02/98 {
	ED_ACTORS_fn_vMagnetSaveMiniStructure(p_tdstFile, p_tdstObject);
// CHINA QZY }
}

