/*=========================================================================
* Objinit.c :       Initialize all object's models.
* This is a part of the Game project.
*
* Version                      1.0
* Creation date        09/08/96
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1996
*=======================================================================*/
#include <cpa_std.h>
#define D_State_Define
#include "ToolsCPA.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/Objects.h"
#include "Structur/EngMode.h"
#include "Structur/ErrGame.h"
#include "Structur/MemGame.h"
#include "Structur/StdObjSt.h"
 
#include "Always.h"
#include "Basic.h"
#include "Family.h"
#include "GameEng.h"
#include "InitEng.h"
#include "MainChar.h"
#include "Micros.h"
#include "ObjInit.h"
#include "PlayAnim/PlayAnim.h"
#include "ObjType.h"
#include "ZeMem.h"
 
#include "Toolcam.h"
#include "Structur/state.h"
#ifdef GAM_USE_SNA
#include "sna\snaglob.h"
#endif /* GAM_USE_SNA */
 
#if defined (ACTIVE_EDITOR) || defined (RETAIL)
#define ENGassert(param)
#else
#define ENGassert(param) assert(param)
#endif
 
/*-----------------------------------------------------------------------------
 *  Description :   Free a world hierarchy
 *-----------------------------------------------------------------------------
 *  Input :         Super Object
 *  Output :        Void
 *-----------------------------------------------------------------------------
 *  Creation date : 12/11/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
extern ACP_tdxBool  fn_bIsInFix(struct tdstEngineObject_ *p_stObject);
 
 
void fn_vDestroyAllHierarchy(HIE_tdxHandleToSuperObject p_stSuperObject)
{
	HIE_tdxHandleToSuperObject p_stChild,p_stNextChild;
	long i;
 
	HIE_M_ForEachMovingChildOf(p_stSuperObject,p_stChild,p_stNextChild,i)
	{
		fn_vDestroyAllHierarchy(p_stChild);
	}
 
	HIE_fn_vIsolate(p_stSuperObject);
 
#ifdef U64
	if ( (HIE_fn_ulGetSuperObjectType(p_stSuperObject) == HIE_C_ulActor) &&
		 !fn_ucIsAnAlwaysObject(M_GetEngineObject(p_stSuperObject)) )
	{
		/* desinit*/
		fn_vDesinitOneObject(M_GetEngineObject(p_stSuperObject),OTI_MapLoaded);
	}
 
#else
#if defined(GAM_USE_SNA)
	switch(HIE_fn_ulGetSuperObjectType(p_stSuperObject))
	{
	case HIE_C_ulActor:
		if ( !fn_ucIsAnAlwaysObject(M_GetEngineObject(p_stSuperObject)) )
		{
			/* We desinit the object*/
			/*  - if it is in the fix because fix objects must not be deleted*/
			/*  - if we load snapshot because in this case there is no need to delete it*/
			if (   fn_bIsInFix(M_GetEngineObject(p_stSuperObject)) 
				  || SNA_fn_ucGetLoadType() == SNA_LOAD_SNAPSHOT)
			{
				/* desinit*/
				fn_vDesinitOneObject(M_GetEngineObject(p_stSuperObject),OTI_MapLoaded);
			}
			else
			{
				fn_p_stFreeEngineSuperObject(p_stSuperObject);
			}
 
		}
		break;
	}
#endif /* GAM_USE_SNA */
 
#endif /* U64*/
}
 
/*-----------------------------------------------------------------------------
 *  Description :
 *-----------------------------------------------------------------------------
 *  Input :
 *  Output :
 *-----------------------------------------------------------------------------
 *  Creation date : 25/06/98            Author : Marc
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vDestroyAllTheWorld()
{
	fn_vDestroyAllHierarchy(gp_stDynamicWorld);
	fn_vDestroyAllHierarchy(gp_stInactiveDynamicWorld);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Free a Character's SuperObject
 *-----------------------------------------------------------------------------
 *  Input :         Pointer to the super object
 *  Output :        Pointer to the super object (NULL if OK)
 *-----------------------------------------------------------------------------
 *  Creation date : 15/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
#ifndef _FIRE_DEADCODE_U64_
HIE_tdxHandleToSuperObject fn_p_stFreeEngineSuperObject(HIE_tdxHandleToSuperObject p_stSuperObject)
{
	tdstEngineObject *p_stEngineObject;
	long lSuperObjectType;
	
	p_stEngineObject = (struct tdstEngineObject_ *)HIE_fn_hGetSuperObjectObject(p_stSuperObject);
	lSuperObjectType = HIE_fn_ulGetSuperObjectType(p_stSuperObject);
	
	if (lSuperObjectType==HIE_C_ulActor)
	{
		fn_p_stFreeEngineObject(p_stEngineObject);
		HIE_fn_vDestroySuperObject(p_stSuperObject);
	}
	else
		M_GameWarningError(E_uwGameFreeEngineSuperObjectNoCharacter);
	
	return(p_stSuperObject);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*-----------------------------------------------------------------------------
 *  Description :   Free a engine object
 *-----------------------------------------------------------------------------
 *  Input :         Pointer to the object
 *  Output :        Pointer of the object (NULL if succes)
 *-----------------------------------------------------------------------------
 *  Creation date : 09/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
struct tdstEngineObject_ *fn_p_stFreeEngineObject(struct tdstEngineObject_ *p_stTempObject)
{
	long i;
	
	for(i=(sizeof(tdstEngineObject)/sizeof(void*))-1;i>=0;i--)
	{
		if (M_VoidMiniStructurePointer(p_stTempObject,i)!=NULL)
/*	M_ForAllMiniStructureExceptNULL(p_stTempObject,i)*/
		{
			g_a_stStructureHandleFunction[i].p_fn_vFreeFunction(p_stTempObject);
		}
	}
	
	MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeEngineObject , 0 );
	M_GameFreeInHLM(p_stTempObject);
	
	return(p_stTempObject);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 

/*-----------------------------------------------------------------------------
 *  Description :   Initialisation of an object
 *-----------------------------------------------------------------------------
 *  Input :         object structure pointer
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 28/02/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vInitOneObject(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjIniType)
{
	long i;
	
	if (p_stObject->h_3dData==NULL||p_stObject->h_StandardGame==NULL)
		M_GameFatalError(E_uwGameInitDesinitOneObjectNullPointer);
	
	if (M_ObjectGetFamilyType(p_stObject)==C_InvalidObjectType)
		M_GameInformationError(E_uwGameInitOneObjectInvalidObject);

	M_ForAllMiniStructureExceptNULL(p_stObject,i)
	{
		g_a_stStructureHandleFunction[i].p_fn_vInitFunction(p_stObject,eObjIniType);
	}
	if (eObjIniType==OTI_MapLoaded)
		fn_vAllSecondPassAreDone(p_stObject->h_StandardGame);
	
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Desinitialisation of an object
 *-----------------------------------------------------------------------------
 *  Input :         object structure pointer
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 28/02/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vDesinitOneObject(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjIniType)
{
	long i;
	
	if (p_stObject->h_3dData==NULL||p_stObject->h_StandardGame==NULL)
		M_GameFatalError(E_uwGameInitDesinitOneObjectNullPointer);
	
	if (M_ObjectGetFamilyType(p_stObject)==C_InvalidObjectType)
		M_GameInformationError(E_uwGameInitOneObjectInvalidObject);
	
	M_ForAllMiniStructureExceptNULL(p_stObject,i)
	{
		g_a_stStructureHandleFunction[i].p_fn_vDesinitFunction(p_stObject,eObjIniType);
	}	
}
 
void fn_vForceActiveAnObject(struct tdstEngineObject_ *p_stObject)
{
	struct MTH3D_tdstVector_ hPosition;
	POS_tdstCompletePosition *hMatrix;
	HIE_tdxHandleToSuperObject hFather;
 
	hFather = HIE_fn_hGetSuperObjectFather( M_GetSuperObject(p_stObject) );
	if( hFather == gp_stInactiveDynamicWorld )
	{
		if( fn_ucStandardGameGetPlatFormType(M_GetMSHandle(M_GetSuperObject(p_stObject),StandardGame))!=0 )
		{
			HIE_fn_vSuperObjectAddHead(gp_stDynamicWorld,M_GetSuperObject(p_stObject));
		}
		else
		{
			HIE_fn_vSuperObjectAddTail(gp_stDynamicWorld,M_GetSuperObject(p_stObject));
		}
	}
 
	M_CheckPointer(HIE_fn_hGetSuperObjectFather(M_GetSuperObject(p_stObject)));
	
	hMatrix = HIE_fn_hGetSuperObjectGlobalMatrix( HIE_fn_hGetSuperObjectFather( M_GetSuperObject(p_stObject) ) );
	POS_fn_vGetTranslationVector( HIE_fn_hGetSuperObjectMatrix(M_GetSuperObject(p_stObject)) ,&hPosition);
	POS_fn_vMulMatrixVertex( &hPosition, hMatrix, &hPosition );
	fn_vSectInfoInitForSingleCharacter(M_GetSuperObject(p_stObject),&hPosition);
	fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
	M_ActivateObject(p_stObject);
	/*  if (fn_ul3dDataGetTimeDelay(p_stObject->h_3dData))*/
	{	  /*fn_v3dDataSetStartTime(p_stObject->h_3dData,g_stEngineStructure.stEngineTimer.ulCurrentTimerCount-fn_ul3dDataGetTimeDelay(p_stObject->h_3dData)+fn_ul3dDataGetStartTime(p_stObject->h_3dData));*/
		fn_v3dDataSetStartTime(p_stObject->h_3dData,g_stEngineStructure.stEngineTimer.ulCurrentTimerCount);
		fn_v3dDataSetTimePreviousFrame(p_stObject->h_3dData,g_stEngineStructure.stEngineTimer.ulCurrentTimerCount);
	}
	/* reactive an object, so create its element3d array if necessary*/
	if (fn_p_st3dDataGetCurrentFrame(p_stObject->h_3dData)->p_stArrayOfElts3d == NULL)
	{
		tdstAnim3d  * p_stCurrentAnim;
 
		ENGassert(fn_h3dDataGetCurrentState(p_stObject->h_3dData) != NULL);
		if (fn_h3dDataGetCurrentState(p_stObject->h_3dData))
		{
			p_stCurrentAnim = fn_h3dDataGetCurrentState(p_stObject->h_3dData)->p_stAnim;
			if (p_stCurrentAnim)
			{
/* ANNECY MT - 22/09/98 {*/
                /*MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeAnimation , p_stObject->h_3dData->h_Family);*/
				/*fn_p_st3dDataGetCurrentFrame(p_stObject->h_3dData)->p_stArrayOfElts3d = (tdstElement3d*)TMP_M_p_Malloc(sizeof(tdstElement3d) * p_stCurrentAnim->ucMaxNumberOfElements);*/
				fn_p_st3dDataGetCurrentFrame(p_stObject->h_3dData)->p_stAnim = NULL;
				/*memset(fn_p_st3dDataGetCurrentFrame(p_stObject->h_3dData)->p_stArrayOfElts3d,0,sizeof(tdstElement3d) * p_stCurrentAnim->ucMaxNumberOfElements);*/
				fn_vAllocArrayOfElts3d (p_stObject -> h_3dData , p_stCurrentAnim->ucMaxNumberOfElements);
/* END ANNECY MT }*/
			}
		}
		/* reinit anim reference to force interpolation to re-create frame*/
		fn_p_st3dDataGetCurrentFrame(p_stObject->h_3dData)->p_stAnim = NULL;
	}
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vActiveAnObject(struct tdstEngineObject_ *p_stObject)
{
	if (!fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame))
	{
		fn_vForceActiveAnObject(p_stObject);
	}
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
void fn_vDesactiveAnObject(struct tdstEngineObject_ *p_stObject)
{
	PLA_fn_vDesInitAllChildOfCharacter(M_GetSuperObject(p_stObject),p_stObject);
	SND_fn_vKillObjectSound2((long)M_GetSuperObject(p_stObject),g_lSoundObjectTypeAnim);
 
	if( !fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame) && M_ObjectIsActivable(p_stObject) )
	{
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
 
		M_UnactivateObject(p_stObject);
	}
}
 
void fn_vKillEngineObjectNotAlways(struct tdstEngineObject_ *p_stObject)
{
	PLA_fn_vDesInitAllChildOfCharacter(M_GetSuperObject(p_stObject),p_stObject);
	/*
	switch(fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame))
	{
	case OI_WhenPlayerGoOutOfActionZone:
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
		break;
	case OI_Always:
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
		fn_vInitOneObject(p_stObject,OTI_Always);
		break;
	case OI_WhenPlayerIsDead:
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
		break;
	case OI_NeverBackWhenTaken:
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,1);
		break;
	case OI_WhenMapJustLoaded:
	case OI_WhenSavedGameJustLoaded:
	case OI_WhenGeneratorIsDesactivated:
		fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
		break;
	}
	*/
	if(!fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame))
	{
		/* dead*/
		fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,1);
		/* definitively ????*/
		if (fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_NeverBackWhenTaken)
			fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
		else
			fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);		
		/* reinit ???*/
		if (fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_Always)
			fn_vInitOneObject(p_stObject,OTI_Always);
		else
			M_UnactivateObject(p_stObject);
	}
 
	/*kill the eventual attached Sound Object*/
	SND_fn_vKillObjectSound2((long)M_GetSuperObject(p_stObject),g_lSoundObjectTypeAnim);
}
 
void fn_vKillEngineObjectOrAlwaysByPointer(struct tdstEngineObject_ *p_stObject)
{
	if (M_ObjectGetFamilyType(p_stObject)!=C_InvalidObjectType)
	{
		if (M_ObjectGetPersonalType(p_stObject)>=C_AlwaysObjectType)
			fn_vKillAlwaysByPersonalType(M_ObjectGetPersonalType(p_stObject));
		else
			fn_vKillEngineObjectNotAlways(p_stObject);
	}
	else
		M_GameWarningError(E_uwGameKillOneEngineObjectInvalid);
}
 
#ifndef _FIRE_DEADCODE_U64_
void fn_vKillEngineObjectOrAlwaysByPersonalType(tdObjectType otPersonalType)
{
	if (otPersonalType!=C_InvalidObjectType)
	{
		if (otPersonalType>=C_AlwaysObjectType)
			fn_vKillAlwaysByPersonalType(otPersonalType);
		else
		{
			/**** look for object's pointer ****/
			fn_vKillEngineObjectNotAlways(fn_p_stFindObjectByItsPersonalType(otPersonalType,gp_stActualWorld));
		}
	}
	else
		M_GameWarningError(E_uwGameKillOneEngineObjectInvalid);
}
#endif /* _FIRE_DEADCODE_U64_ */
 
/*-----------------------------------------------------------------------------
 *  Description :   Initialisation of all object when
 *                  a new game start
 *-----------------------------------------------------------------------------
 *  Input :         None
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 28/02/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vInitAllObjectsWhenNewGameStart(void)
{
}
/*************************************************************************/
void fn_vInitAllObjectsWhenMapJustLoaded(void)
{
	HIE_fn_vInitMatrixStack();
	fn_vInitAllObjectTree(OTI_MapLoaded,gp_stDynamicWorld);
	fn_vInitAlwaysModel(OTI_MapLoaded);
}
/*************************************************************************/
void fn_vInitAllObjectsWorld(enum tdeObjectTreeInit_ eObjIniType)
{
	fn_vInitAllObjectTree(eObjIniType,gp_stDynamicWorld);
	fn_vInitAllObjectTree(eObjIniType,gp_stInactiveDynamicWorld);
 
}
/*************************************************************************/
void fn_vInitAllObjectsWhenSavedGameJustLoaded(void)
{
	HIE_fn_vInitMatrixStack();
	fn_vInitAllObjectsWorld(OTI_LoadSavedGame);
	fn_vInitAlwaysModel(OTI_LoadSavedGame);
}
/*************************************************************************/
void fn_vReInitActorsWhenSectorBecomesActive(HIE_tdxHandleToSuperObject _hSector)
{
	SECT_tdxHandleOfElementLstCharacter hCharacterList,hNextCharacterList;
	HIE_tdxHandleToSuperObject hTreatCharacter;
	tdstEngineObject *p_stTreatObject;
	long j;

	SECT_M_ForEachXMovingCharListInSector(_hSector,hCharacterList,hNextCharacterList,j)
	{
		hTreatCharacter = SECT_GetCharacterInList(hCharacterList);
		p_stTreatObject = (tdstEngineObject *)HIE_fn_hGetSuperObjectObject(hTreatCharacter);
/* ANNECY MT - 16/12/98 {*/
		/*////////////////////// Previous test
		(
			(
			 (   fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stTreatObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone
			  || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stTreatObject->h_StandardGame) == OI_Always
			 )
			 && fn_bf1StandardGameGetIsDesactivateAtAll(p_stTreatObject->h_StandardGame)
			)
			|| (fn_eStandardGameGetInitFlagWhenOutOfZone(p_stTreatObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone)
		   );
		//////////////////////*/
		if(
			(fn_bf1StandardGameGetIsDesactivateAtAll(p_stTreatObject->h_StandardGame)	/* perso is dead*/
			 && ((fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stTreatObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone)
				 ||
				 (fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stTreatObject->h_StandardGame) == OI_Always)
				)
			)
			||
			(!fn_bf1StandardGameGetIsDesactivateAtAll(p_stTreatObject->h_StandardGame)	/* perso is alive*/
			 && (fn_eStandardGameGetInitFlagWhenOutOfZone(p_stTreatObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone)
			)
		   )
/* END ANNECY MT }*/
		{
			if( !fn_ucIsAnAlwaysObject(p_stTreatObject)
#ifndef U64
                && g_DEMO_KeybTest != DEMO_PLAY
#endif
              )
				fn_vInitOneObject(p_stTreatObject,OTI_WhenGoOutOfZone);
		}
	}
}
/*************************************************************************/
/* Modif : 22/05/98 loop to take perso in new active sector*/
void fn_vInitObjectsWhenPlayerChangeSector(HIE_tdxHandleToSuperObject hCharacter,
                                           HIE_tdxHandleToSuperObject hCurrentSector,
                                           HIE_tdxHandleToSuperObject hPreviousSector)
{
	long i,j;
	HIE_tdxHandleToSuperObject hListInActiveSector;
	HIE_tdxHandleToSuperObject hListInPreviousSector;
	SECT_tdxHandleOfElementLstActivityInteraction hActivityListInActiveSector;
	SECT_tdxHandleOfElementLstActivityInteraction hActivityListInPreviousSector;
	unsigned char bSectorWasAndIsActive;

	// reInit actors when inactive sectors become active

	// Sectors in Activation List of CurrentSector
	SECT_M_ForEachActivityNodeInActivityInteractionList(hCurrentSector,hActivityListInActiveSector,i)
	{
		bSectorWasAndIsActive = FALSE;
		hListInActiveSector = SECT_GetSectorInActivityList(hActivityListInActiveSector);
		if (hListInActiveSector==hPreviousSector)
			bSectorWasAndIsActive = TRUE;
		else
		{
			SECT_M_ForEachActivityNodeInActivityInteractionList(hPreviousSector,hActivityListInPreviousSector,j)
			{
				hListInPreviousSector = SECT_GetSectorInActivityList(hActivityListInPreviousSector);
				if (hListInPreviousSector==hListInActiveSector)
					bSectorWasAndIsActive = TRUE;
			}
		}
		if (bSectorWasAndIsActive == FALSE)
		{
			fn_vReInitActorsWhenSectorBecomesActive(hListInActiveSector);
		}
	}

	// Current Sector
	bSectorWasAndIsActive = FALSE;
	SECT_M_ForEachActivityNodeInActivityInteractionList(hPreviousSector,hActivityListInPreviousSector,j)
	{
		hListInPreviousSector = SECT_GetSectorInActivityList(hActivityListInPreviousSector);
		if (hListInPreviousSector==hCurrentSector)
			bSectorWasAndIsActive = TRUE;
	}
	if (bSectorWasAndIsActive == FALSE)
	{
		fn_vReInitActorsWhenSectorBecomesActive(hCurrentSector);
	}
}
/*************************************************************************/
 
/*-----------------------------------------------------------------------------
 *  Description :   Initialisation of all object when
 *                  the map just loaded
 *-----------------------------------------------------------------------------
 *  Input :         None
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 28/02/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vForceReinitAllObjects()
{
	HIE_fn_vInitMatrixStack();
	fn_vInitAllObjectsWorld(OTI_ReinitTheMap);
	fn_vInitAlwaysModel(OTI_ReinitTheMap);
 
	CAM_fn_vReInitCameras();
}
 
/*-----------------------------------------------------------------------------
 *  Description :   The player is dead, we must reinit the map by doing a dead loop
 *
 *-----------------------------------------------------------------------------
 *  Input :         None
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 25/06/97            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date : 13/09/97        Modification Author : Olivier
 *  Modifications : Special Position Flag use
 *---------------------------------------------------------------------------*/
void fn_vSnifThePlayerIsDead(void)
{
/* #ifndef ACTIVE_EDITOR*/
/*	fn_vStandardGameChangePositionFromSpecialPositionFlag();*/
/* #endif /* ACTIVE_EDITOR   */
/* ANNECY MT - 28/08/98 {*/
	/* No resurection*/
	POS_fn_vSetIdentityMatrix(&g_stEngineStructure.stMainCharacterPosition);
	POS_fn_vSetIdentityMatrix(&g_stEngineStructure.stMainCameraPosition);
	g_stEngineStructure.bResurection = FALSE;	
/* END ANNECY MT }*/
 
	/* JO 27/11/97*/
	/*SND_fn_vKillAllObjectSound();*/
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeAnim );
#ifdef D_USE_LIPSYNC
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeLipsSynchro );
#endif
	fn_vChangeEngineMode(EM_ModePlayerDead);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   The player is dead, we must reinit the map by doing a dead loop
 *                  Must replace fn_vSnifThePlayerIsDead
 *
 *-----------------------------------------------------------------------------
 *  Input :         BOOLEAN
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 02/10/97            Author : Olivier
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 /*
void fn_vSnifThePlayerIsDeadWithOption(ACP_tdxBool _bSpecialPositionUse)
{
#ifndef ACTIVE_EDITOR
   if( _bSpecialPositionUse )
      fn_vStandardGameChangePositionFromSpecialPositionFlag();
#endif // ACTIVE_EDITOR
 
	// JO 27/11/97
	//SND_fn_vKillAllObjectSound();
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeAnim );
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeLipsSynchro );
 
   fn_vChangeEngineMode(EM_ModePlayerDead);
}
*/
 
/*-----------------------------------------------------------------------------
 *  Description :   The player is dead, we must reinit the map by doing a
 *						  dead loop.
 *                  Principal character is initialized at characterOne's
 *						  position, and principal camera at CharacterTwo's
 *						  position, if SpecialPositionFlag(s) are valid.
 *						  Else, the fn_vSnifThePlayerIsDeadWithOption function
 *						  is called.
 *
 *-----------------------------------------------------------------------------
 *  Input :         two characters
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 26/11/97            Author : Olivier
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vSnifThePlayerIsDeadWithResurrectionAt(HIE_tdxHandleToSuperObject _hCharacterPositionOne,HIE_tdxHandleToSuperObject _hCharacterPositionTwo)
{
/* ANNECY MT - 28/08/98 {*/
	HIE_tdxHandleToSuperObject hMaincharacter = MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode() );
 
	g_stEngineStructure.bResurection = FALSE;
	/* save Main Character Resurection Position*/
	if(_hCharacterPositionOne && hMaincharacter)
	{
		/* save initial position*/
		POS_fn_vCopyMatrix
		(
			&g_stEngineStructure.stMainCharacterPosition,
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(hMaincharacter,3dData))
		);
		/* set initial position to resurection position*/
		POS_fn_vCopyMatrix
		(
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(hMaincharacter,3dData)),
			HIE_fn_hGetSuperObjectGlobalMatrix(_hCharacterPositionOne)
		);
		g_stEngineStructure.bResurection = TRUE;
	}
	else if(hMaincharacter)
	{
		/* save initial position*/
		POS_fn_vCopyMatrix
		(
			&g_stEngineStructure.stMainCharacterPosition,
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(hMaincharacter,3dData))
		);
		g_stEngineStructure.bResurection = TRUE;
	}
	/* save Main Camera Resurection Position*/
	if(_hCharacterPositionTwo && g_stEngineStructure.h_StdCamCaracter)
	{
		/* save initial position*/
		POS_fn_vCopyMatrix
		(
			&g_stEngineStructure.stMainCameraPosition,
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(g_stEngineStructure.h_StdCamCaracter,3dData))
		);
		/* set initial position to resurection position*/
		POS_fn_vCopyMatrix
		(
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(g_stEngineStructure.h_StdCamCaracter,3dData)),
			HIE_fn_hGetSuperObjectGlobalMatrix(_hCharacterPositionTwo)
		);
		g_stEngineStructure.bResurection = TRUE;
	}
	else if(g_stEngineStructure.h_StdCamCaracter)
	{
		/* save initial position*/
		POS_fn_vCopyMatrix
		(
			&g_stEngineStructure.stMainCameraPosition,
			fn_p_st3dDataGetMatrixInit(M_GetMSHandle(g_stEngineStructure.h_StdCamCaracter,3dData))
		);
		g_stEngineStructure.bResurection = TRUE;
	}
/* END ANNECY MT }*/
	/* JO 27/11/97*/
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeAnim );
#ifdef D_USE_LIPSYNC
	SND_fn_vKillAllObjectTypeSound( g_lSoundObjectTypeLipsSynchro );
#endif
   fn_vChangeEngineMode(EM_ModePlayerDead);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Initialisation of all object when
 *                  the player just dead
 *-----------------------------------------------------------------------------
 *  Input :         None
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 28/02/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vInitAllObjectsWhenPlayerDead(void)
{
	HIE_fn_vInitMatrixStack();
	fn_vInitAllObjectsWorld(OTI_PlayerDead);
	fn_vInitAlwaysModel(OTI_PlayerDead);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Read the objects tree and initialize engine objects
 *                  recursive function
 *-----------------------------------------------------------------------------
 *  Input :         Initialisation's type
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 06/09/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vInitAllObjectTree(enum tdeObjectTreeInit_ eObjIniType,HIE_tdxHandleToSuperObject p_stSupObject)
{
	HIE_tdxHandleToSuperObject p_stChild;
	long i;
	
	tdstEngineObject *p_stObject;
	unsigned long ulSuperObjectType;
	tdxHandleToFamilyList hFamily;
	MS_tdxHandleTo3dData h3dData;
	
	/**** Calculate absolute matrix ****/
	HIE_fn_vPushMatrix(p_stSupObject);
	GEO_M_vCopyMatrix(HIE_fn_hGetSuperObjectGlobalMatrix(p_stSupObject),g_p_stCurrentMatrix);
	HIE_fn_vInvalidateOneGlobalMatrix(p_stSupObject);
	
	/**** We cover all tree ****/
	HIE_M_ForEachChildOf(p_stSupObject,p_stChild,i)
		fn_vInitAllObjectTree(eObjIniType,p_stChild);
	
	HIE_fn_vPopMatrix();
	
	/**** We initialize this object ****/
	p_stObject = (tdstEngineObject *)HIE_fn_hGetSuperObjectObject(p_stSupObject);
	ulSuperObjectType = HIE_fn_ulGetSuperObjectType(p_stSupObject);
	
	
	if (p_stObject==NULL && (ulSuperObjectType & (HIE_C_ulSuperObject | HIE_C_ulNoAction)) == 0)
	{
		M_GameFatalError(E_uwGameInitAllObjectTreeNullPointer);
	}
	
	if (ulSuperObjectType == HIE_C_ulActor)
	{
		switch (eObjIniType)
		{
/* ANNECY MT - 28/08/98 {*/
		case OTI_ReinitTheMap:
			fn_vInitOneObject(p_stObject,eObjIniType);	/* always*/
			break;
/* END ANNECY MT }*/
		case OTI_MapLoaded:
		case OTI_LoadSavedGame:
			/**** Initialisation ****/
			if (
				(   fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_Always
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenPlayerIsDead
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenMapJustLoaded
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenSavedGameJustLoaded
				)
				||
				(     fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_NeverBackWhenTaken
				  && !fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame)
				)
			   )
			{
				
				if (eObjIniType==OTI_LoadSavedGame&&fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenSavedGameJustLoaded)
					fn_vInitOneObject(p_stObject,OTI_ReinitTheMap);
				else
					fn_vInitOneObject(p_stObject,eObjIniType);
			}
			else
				/**** Do not initialise it ****/
			{
				fn_vDesinitOneObject(p_stObject,eObjIniType);
			}
			/* CGHT 15/05/97*/
			h3dData=M_GetMSHandle(p_stSupObject,3dData);
			hFamily=fn_h3dDataGetFamily(h3dData);
			HIE_fn_vSetSuperObjectBoundingVolume(p_stSupObject,(void *)(hFamily->hBoundingSphere));
			
			break;
		case OTI_AlwaysCreated:
		case OTI_PlayerDead:
			if (
				(   fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenPlayerGoOutOfActionZone
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_Always
				 || fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenPlayerIsDead
				)
				||
				(	fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_NeverBackWhenTaken
				 && !fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame)
				)
				||
				(   fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_WhenMapJustLoaded
				 && !fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame)
				)
			   )
				/**** Initialise it ****/
			{
				fn_vInitOneObject(p_stObject,eObjIniType);
			}
			else
				/**** Do not initialise it ****/
			{
				fn_vDesinitOneObject(p_stObject,eObjIniType);
			}
			break;
		default :
			M_GameFatalError(E_uwGameInitAllObjectTreeBadInitType);
			break;
		}
		
		/* Update Global Matrix for correct DNM Init.*/
		HIE_fn_vPushMatrix(p_stSupObject);
		HIE_fn_vPopMatrix();
		
		/* Init DNM Object*/
		if(M_GetMSHandle(p_stSupObject,Dynam)!=NULL)
		{
			fn_vDynamInit(p_stObject,eObjIniType);
		}
	}
}
 
/* Create a child for a super object*/
 
#ifdef ACTIVE_EDITOR
 
void fn_vAddChildToSuperObject(HIE_tdxHandleToSuperObject hChild,char *szFamily,char *szModel,char *szPersonal)
{
	tdstEngineObject * p_stObject;
 
	p_stObject=(tdstEngineObject*)HIE_fn_hGetSuperObjectObject(hChild);
	fn_vStandardGameSetPersonalType(p_stObject->h_StandardGame,fn_otFindOrAddPersonalTypeOfPersonalTypeName(szPersonal));
	fn_vStandardGameSetFamilyType(p_stObject->h_StandardGame,fn_otFindOrAddFamilyTypeOfFamilyTypeName(szFamily));
	fn_vStandardGameSetModelType(p_stObject->h_StandardGame,fn_otFindOrAddModelTypeOfModelTypeName(szModel));
	fn_v3dDataSetFamily(p_stObject->h_3dData,fn_hFindOrAddFAmily(fn_otStandardGameGetFamilyType(p_stObject->h_StandardGame)));
	PLA_fn_vInitNewState(hChild,fn_h3dDataGetInitialState(p_stObject->h_3dData));
	PLA_fn_bDoFirstInitOfAnimPlayerForCharacter(hChild,fn_h3dDataGetInitialState(p_stObject->h_3dData)->p_stAnim);
}
#endif /* ACTIVE_EDITOR */
