/*=========================================================================
* StdGame.cpp : StdGame's functions
*
*
*
*         StdGame MUST BE REPLACE BY THE C FILENAME
*
*
* Version              1.0
* Creation date        01/10/96
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1996
*=======================================================================*/
 
/*******************************************************/
/**** For the structures and variables declarations ****/
/*******************************************************/
#define D_StdGame_StructureDefine
#define D_StdGame_VariableDefine
 
#include "ToolsCPA.h"
 
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/3DOSLkTb.h"
#include "Structur/MemGame.h"
#include "Structur/ErrGame.h"
#include "Structur/Objects.h"
#include "Structur/GameScpt.h"
#include "Structur/EngMode.h"
#include "structur/StdObjSt.h"
 
#include "safe.h" /*BART*/
#include "Basic.h"
#include "Family.h"
#include "ObjInit.h"
#include "ObjType.h"
#include "ZeMem.h"
#include "MainChar.h"
#include "ToolCam.h"
#include "Structur/ObjsTbls.h"
#include "Always.h"
 
/*******************************************************/
#define C_TRANSPARENCY_ZONE_MIN		70
#define C_TRANSPARENCY_ZONE_MAX		80
#define C_TOOFARLIMIT_INIT          70
/*******************************************************/
 
void fn_vStdGamePlayerSavedValueSAI(MS_tdxHandleToStandardGame h_StdGame);
void fn_vStdGamePlayerFreeInitValueSAI(MS_tdxHandleToStandardGame h_StdGame);
 
extern ACP_tdxBool  fn_bIsInFix(struct tdstEngineObject_ *p_stObject); 
/*-----------------------------------------------------------------------------
 *  Description : StdGame object action script callback
 *-----------------------------------------------------------------------------
 *  Input :         Object pointer
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 01/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#if defined(GAM_USE_SCRIPT)
SCR_tde_Anl_ReturnValue fn_eStdGameScriptCallBack(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
	MS_tdxHandleToStandardGame h_StdGame;
	struct tdstEngineObject_ *p_stEngineObject;
 
	_p_stFile=_p_stFile;
 
	SCR_M_RdL0_GetSectionLong(C_ParentSection,0,struct tdstEngineObject_ *, p_stEngineObject);
 
	if (M_IsTitle)
	{
		MMG_fn_vBeginMemoryInfo (MMG_C_lTypeMiniStructure , MMG_C_lSubTypeStdGame , p_stEngineObject);
		h_StdGame = fn_h_StdGameRealAlloc();
		MMG_fn_vEndMemoryInfo ();
		p_stEngineObject->h_StandardGame=h_StdGame;
		/* JO 04/11/97*/
		fn_ucStandardGameSetPlatFormType(h_StdGame,(unsigned char)0);
		/*fn_ucStandardGameSetSpecialPositionFlag(h_StdGame,(unsigned char)0);*/
		fn_vStandardGameSetCustomBits(h_StdGame,(unsigned long)GAM_C_CustBitUsesTransparencyZone);
		fn_vStandardGameSetTransparencyZoneMin(h_StdGame,C_TRANSPARENCY_ZONE_MIN);
		fn_vStandardGameSetTransparencyZoneMax(h_StdGame,C_TRANSPARENCY_ZONE_MAX);
/* ANNECY MT - 22/10/98 {*/
        fn_vStandardGameSetTooFarLimit(h_StdGame,C_TOOFARLIMIT_INIT);
/* END ANNECY MT }*/
		SCR_M_RdL0_SetSectionLong(C_ThisSection,0,h_StdGame);
	}
	else if (M_IsEntry)
	{
		tdxHandleToObjectsTablesList h_ObjectsTablesListElement;
 
		SCR_M_RdL0_GetSectionLong(C_ThisSection,0,MS_tdxHandleToStandardGame,h_StdGame);
		if (M_ActionIs(C_EntryStdGameType))
		{
			tdxHandleToState h_InitialState=NULL;
			SCR_tdst_Link_Value *p_xIndex;
// Shaitan => module list in the level
			SCR_tdst_Cxt_Values *p_stVal;
			char * p_szInitialObjectTable;
// End Shaitan => module list in the level
 
			M_CheckScriptParamNumber(3);
 
			fn_vStandardGameSetPersonalType(h_StdGame,fn_otFindOrAddPersonalTypeOfPersonalTypeName(_ap_szParams[0]));
			fn_vStandardGameSetFamilyType(h_StdGame,fn_otFindOrAddFamilyTypeOfFamilyTypeName(_ap_szParams[2]));
			fn_vStandardGameSetModelType(h_StdGame,fn_otFindOrAddModelTypeOfModelTypeName(_ap_szParams[1]));
			fn_v3dDataSetFamily(p_stEngineObject->h_3dData,fn_hFindOrAddFAmily(fn_otStandardGameGetFamilyType(h_StdGame)));
// Shaitan => module list in the level
/*
			SCR_M_RdL0_GetContextLong(C_ParentContext,0,tdxHandleToObjectsTablesList,h_ObjectsTablesListElement);
*/
			SCR_M_RdL0_GetContextLong(C_ParentContext,0, char*, p_szInitialObjectTable);
			if (p_szInitialObjectTable)
			{
				SCR_M_RdL0_SetContextLong(C_GrandChildContext, 1, (long)fn_h3dDataGetFamily(p_stEngineObject->h_3dData));
				p_stVal = SCR_fnp_st_RdL0_AnalyseSection(p_szInitialObjectTable, SCR_CDF_uw_Anl_Normal);
				h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList) SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
//				free(p_szInitialObjectTable);
			}
			else
				h_ObjectsTablesListElement = NULL;
// End Shaitan => module list in the level
 
 
			if(h_ObjectsTablesListElement!=NULL)
			{
				fn_v3dDataSetCurrentObjectsTable(p_stEngineObject->h_3dData,h_ObjectsTablesListElement);
				fn_v3dDataSetInitialObjectsTable(p_stEngineObject->h_3dData,h_ObjectsTablesListElement);
				fn_vAddAnObjectsTablesInList(fn_h3dDataGetFamily(p_stEngineObject->h_3dData),h_ObjectsTablesListElement);
			}
			else
			{
				fn_v3dDataSetCurrentObjectsTable(p_stEngineObject->h_3dData,fn_vGetDefaultObjectsTableOfFamily(fn_h3dDataGetFamily(p_stEngineObject->h_3dData)));
				fn_v3dDataSetInitialObjectsTable(p_stEngineObject->h_3dData,fn_vGetDefaultObjectsTableOfFamily(fn_h3dDataGetFamily(p_stEngineObject->h_3dData)));
			}
			fn_vCheckObjectsTables(p_stEngineObject); /*Gizmo  do nothing in release*/
			h_InitialState = fn_h3dDataGetInitialState(p_stEngineObject->h_3dData);
			if(h_InitialState)
			{
				p_xIndex = SCR_fnp_st_Link_SearchKey(&g_st3DOSLinkTable.stState,(char *)h_InitialState);
				if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_Initialized)
				{
					MMG_fn_vAddMemoryInfo( MMG_C_lTypeMiniStructure , MMG_C_lSubType3dData , p_stEngineObject);
					TMP_M_Free(h_InitialState);
					fn_v3dDataSetInitialState(p_stEngineObject->h_3dData,(tdxHandleToState)(p_xIndex->ulValue));
				}
				else
				{
					fn_v3dDataSetInitialState(p_stEngineObject->h_3dData,NULL);
					M_GameWarningError(E_uwGamePLA_BadInitialState);
				}
			}
		}
		else if (M_ActionIs(C_EntryStdGameHitPointInit))
		{
			fn_vStandardGameSetHitPoints(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
		}
		else if (M_ActionIs(C_EntryStdGameHitPointMaxInit))
		{
			fn_vStandardGameSetHitPointsMax(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
		}
		else if (M_ActionIs(C_EntryStdGameHitPointMaxMax))
		{
			fn_vStandardGameSetHitPointsMaxMax(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
		}
		else if (M_ActionIs(C_EntryStdInitFlag))
		{
			M_CheckScriptParamNumber(2);
			if (!stricmp(_ap_szParams[0],C_ValueStdInitFlagNeverBack))
				fn_vStandardGameSetInitFlagWhenOutOfZone(h_StdGame,OI_NeverBackWhenTaken);
			else if (!stricmp(_ap_szParams[0],C_ValueStdInitFlagGoOutOfZone))
				fn_vStandardGameSetInitFlagWhenOutOfZone(h_StdGame,OI_WhenPlayerGoOutOfActionZone);
			if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagNeverBack))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_NeverBackWhenTaken);
			else if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagAlways))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_Always);
			else if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagGoOutOfZone))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenPlayerGoOutOfActionZone);
			else if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagPlayerDead))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenPlayerIsDead);
			else if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagMapLoaded))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenMapJustLoaded);
			else if (!stricmp(_ap_szParams[1],C_ValueStdInitFlagSavedGameLoaded))
				fn_vStandardGameSetInitFlagWhenDeadOrTaken(h_StdGame,OI_WhenSavedGameJustLoaded);
		}
		else if( M_ActionIs(C_EntryStdGamePlatFormType) )
		{
			M_CheckScriptParamNumber(1);
			fn_ucStandardGameSetPlatFormType(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
		}
		else if( M_ActionIs(C_EntryStdGameCustomBitsInit) )
		{
			unsigned long ulCustBits;
			
			M_CheckScriptParamNumber(1);
			ulCustBits = (unsigned long)fn_lAToI(_ap_szParams[0]);
#ifndef ACTIVE_EDITOR
			// GuS : If CB 32 is on a char from level, remove it.
			if( (ulCustBits & GAM_C_CustBitRayman) && ! GAM_fn_bIsEngineInFixMode() )
				ulCustBits &= ~GAM_C_CustBitRayman;
#endif

			fn_vStandardGameSetCustomBits(h_StdGame,ulCustBits);
		}
		else if( M_ActionIs(C_EntryStdGameCapabilities) )
		{
			unsigned long ulReturn;
			char* dummy;
			/*ANNECY Bart#01 14/02/98*/
			ulReturn = strtoul(_ap_szParams[0], &dummy, 2);
			fn_vStandardGameSetCapabilities(h_StdGame, ulReturn);
		}
		else if( M_ActionIs(C_EntryStdGameTractionFactor) )
		{
			fn_vStandardGameSetTractionFactor(h_StdGame, (unsigned char)fn_lAToI(_ap_szParams[0]));
		}
		else if( M_ActionIs(C_EntryStdGameTransparencyZone) )
		{
			M_CheckScriptParamNumber(2);
			fn_vStandardGameSetTransparencyZoneMin(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
			fn_vStandardGameSetTransparencyZoneMax(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[1]));
		}
/* ANNECY MT - 22/10/98 {*/
        else if( M_ActionIs(C_EntryStdGameTooFarLimit))
        {
            M_CheckScriptParamNumber(1);
            fn_vStandardGameSetTooFarLimit(h_StdGame,(unsigned char)fn_lAToI(_ap_szParams[0]));
        }
/* END ANNECY MT }*/
 }
 return(eReturnValue);
}
#endif /* GAM_USE_SCRIPT */
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame's mini-structure sizeof
 *-----------------------------------------------------------------------------
 *  Input :         None
 *  Output :        Size of
 *-----------------------------------------------------------------------------
 *  Creation date : 14/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
unsigned long fn_ulStdGameSizeOf()
{
 return(sizeof(struct tdstStandardGame_));
}
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame object action allocation
 *-----------------------------------------------------------------------------
 *  Input :         Object pointer
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 01/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vStdGameAlloc(struct tdstEngineObject_ *p_stObject)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_StandardGame!=NULL)
		M_GameFatalError(E_uwGameMiniStructAllocAlreadyAlloc);
 
	MMG_fn_vBeginMemoryInfo (MMG_C_lTypeMiniStructure , MMG_C_lSubTypeStdGame , p_stObject);
	p_stObject->h_StandardGame = fn_h_StdGameRealAlloc();
	MMG_fn_vEndMemoryInfo ();
	fn_vStdGameInitValueSAI(p_stObject->h_StandardGame);
}
 
MS_tdxHandleToStandardGame fn_h_StdGameRealAlloc()
{
	MS_tdxHandleToStandardGame h_StdGame;
 
	h_StdGame = (MS_tdxHandleToStandardGame)M_p_GameMallocInHLM(fn_ulStdGameSizeOf());
	fn_vStandardGameSetCustomBits(h_StdGame,(unsigned long)GAM_C_CustBitUsesTransparencyZone);
	fn_vStandardGameSetTransparencyZoneMin(h_StdGame,C_TRANSPARENCY_ZONE_MIN);
	fn_vStandardGameSetTransparencyZoneMax(h_StdGame,C_TRANSPARENCY_ZONE_MAX);
    fn_vStandardGameSetIsDesactivateAtAll(h_StdGame,0);
    fn_vStandardGameSetIsActivable(h_StdGame,1);
	fn_vStandardGameSetHitPoints(h_StdGame,0);
	fn_vStandardGameSetHitPointsMax(h_StdGame,0);
    fn_vStandardGameSetTooFarLimit(h_StdGame,C_TOOFARLIMIT_INIT);
/* ANNECY MT - 19/11/98 {*/
#if defined(ACTIVE_EDITOR)
	fn_vStdGameSetInAllSubMaps(h_StdGame,FALSE);
#endif
/* END ANNECY MT }*/
	return(h_StdGame);
}
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame object action free
 *-----------------------------------------------------------------------------
 *  Input :         Object pointer
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 01/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vStdGameFree(struct tdstEngineObject_ *p_stObject)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_StandardGame==NULL)
		M_GameFatalError(E_uwGameMiniStructNull);
 
	fn_vStdGameFreeInitValueSAI(p_stObject->h_StandardGame);
	MMG_fn_vAddMemoryInfo( MMG_C_lTypeMiniStructure , MMG_C_lSubTypeStdGame , p_stObject );
	if( p_stObject == HIE_fn_hGetSuperObjectObject( MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode() ) ) )
		fn_vStdGamePlayerFreeInitValueSAI(p_stObject->h_StandardGame);
	M_GameFreeInHLM(p_stObject->h_StandardGame);
}
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame object action initialisation
 *-----------------------------------------------------------------------------
 *  Input :         When this function's called (see ObjInit.h)
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 01/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vStdGameInit(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjectInit)
{
	struct MTH3D_tdstVector_ hPosition;
	POS_tdstCompletePosition *hMatrix;
	ACP_tdxBool				 bIsAnAlways;
 
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_StandardGame==NULL)
		M_GameFatalError(E_uwGameMiniStructNull);
 
	fn_vStandardGameSetLastTrame(p_stObject->h_StandardGame,0);
 
	bIsAnAlways = (ACP_tdxBool)fn_ucIsAnAlwaysObject(p_stObject);
 
    if (eObjectInit == OTI_ReinitTheMap || eObjectInit == OTI_PlayerDead || (eObjectInit == OTI_MapLoaded && fn_bIsInFix(p_stObject)))
        p_stObject -> h_StandardGame -> ulCustomBits = p_stObject -> h_StandardGame -> ulSaveCustomBits;

	switch(eObjectInit)
    {
/* ANNECY MT - 28/08/98 {*/
    case OTI_ReinitTheMap:
        if(!fn_ucIsAnAlwaysModel(p_stObject))
        {
            fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
            fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
            hMatrix = fn_p_st3dDataGetAbsoluteMatrix(p_stObject->h_3dData);
            POS_fn_vGetTranslationVector(hMatrix,&hPosition);
            fn_vSectInfoInitForSingleCharacter(M_GetSuperObject(p_stObject),&hPosition);
        }
        break;
    case OTI_AlwaysCreated:
        if(!fn_ucIsAnAlwaysModel(p_stObject))
        {
            fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
            hMatrix = fn_p_st3dDataGetAbsoluteMatrix(p_stObject->h_3dData);
            POS_fn_vGetTranslationVector(hMatrix,&hPosition);
            fn_vSectInfoInitForSingleCharacter(M_GetSuperObject(p_stObject),&hPosition);
        }
        break;
    case OTI_PlayerDead:
        if( !fn_ucIsAnAlwaysModel(p_stObject) &&
            ( !fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame) || fn_bf1StandardGameGetIsActivable(p_stObject->h_StandardGame) )
		  )
        {
            fn_vStandardGameSetIsDesactivateAtAll(p_stObject->h_StandardGame,0);
            fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
            hMatrix = fn_p_st3dDataGetAbsoluteMatrix(p_stObject->h_3dData);
            POS_fn_vGetTranslationVector(hMatrix,&hPosition);
            fn_vSectInfoInitForSingleCharacter(M_GetSuperObject(p_stObject),&hPosition);
        }
        break;
    case OTI_LoadSavedGame:
        if(!fn_ucIsAnAlwaysModel(p_stObject))
        {
            if (fn_eStandardGameGetInitFlagWhenDeadOrTaken(p_stObject->h_StandardGame) == OI_NeverBackWhenTaken)
            {
                fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,(unsigned char)(!fn_bf1StandardGameGetIsDesactivateAtAll(p_stObject->h_StandardGame)));
            }
            hMatrix = fn_p_st3dDataGetAbsoluteMatrix(p_stObject->h_3dData);
            POS_fn_vGetTranslationVector(hMatrix,&hPosition);
            fn_vSectInfoInitForSingleCharacter(M_GetSuperObject(p_stObject),&hPosition);
        }
        break;
/* END ANNECY MT }*/
    case OTI_MapLoaded:
        fn_vStdGameSavedValueSAI(p_stObject->h_StandardGame);
        fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,1);
        /* We save current hit points and max hit points for rayman only */
        /* GuS if( p_stObject == HIE_fn_hGetSuperObjectObject( MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode() ) ) )*/
        if (fn_ulStandardGameGetCustomBits(p_stObject -> h_StandardGame) & GAM_C_CustBitRayman)
            fn_vStdGamePlayerSavedValueSAI(p_stObject->h_StandardGame);
    case OTI_WhenGameStart:
        if (!fn_bIsAllSecondPassDone(p_stObject->h_StandardGame))
            fn_vStdGameInitValueSAI(p_stObject->h_StandardGame);
        break;
    }
 
	
 
    {
        MS_tdxHandleToStandardGame h_StdGame = p_stObject -> h_StandardGame;
 
		/* ANNECY YLT - 25/11/98 {*/
		if (fn_ulStandardGameGetCustomBits(h_StdGame) & GAM_C_CustBitRayman)
		{
			/* for main actor only: we dont itialise the number of hitpoints except at the beginning of the game*/
		
			if (/*eObjectInit == OTI_ReinitTheMap || eObjectInit == OTI_PlayerDead ||*/ eObjectInit == OTI_WhenGameStart)
			{
				h_StdGame -> ucHitPoints = h_StdGame -> ucSaveHitPoints;
				/*h_StdGame -> ucHitPointsMax = h_StdGame -> ucSaveHitPointsMax;*/
			}
		}
		else
		/* END ANNECY YLT }*/
		{
			if (eObjectInit == OTI_ReinitTheMap || eObjectInit == OTI_PlayerDead || eObjectInit == OTI_WhenGameStart)
				h_StdGame -> ucHitPoints = h_StdGame -> ucSaveHitPoints;
			/*
			if (eObjectInit == OTI_ReinitTheMap || eObjectInit == OTI_MapLoaded || eObjectInit == OTI_WhenGameStart)
				h_StdGame -> ucHitPointsMax = h_StdGame -> ucSaveHitPointsMax;
			*/
		}
 
		
        if (eObjectInit == OTI_ReinitTheMap || eObjectInit == OTI_MapLoaded)
        {
            h_StdGame -> ucMiscFlags = (unsigned char ) ( (h_StdGame->ucMiscFlags & ~GAM_C_SavedMiscFlags) | h_StdGame->ucSaveMiscFlags );
        }
 
		/*if (eObjectInit == OTI_PlayerDead && bIsAnAlways)*/
			/*h_StdGame -> ulCustomBits |= GAM_C_CustBitAlways;*/
    }
 
}
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame object action desinitialisation
 *-----------------------------------------------------------------------------
 *  Input :         When this function's called (see ObjInit.h)
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 01/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vStdGameDesinit(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjectInit)
{
 if (p_stObject==NULL)
  M_GameFatalError(E_uwGameMiniStructObjectNull);
 
 if (p_stObject->h_StandardGame==NULL)
  M_GameFatalError(E_uwGameMiniStructNull);
 
 switch(eObjectInit)
 {
 	case OTI_AlwaysCreated:
  case OTI_ReinitTheMap:
  case OTI_MapLoaded:
  case OTI_PlayerDead:
  case OTI_LoadSavedGame:
   fn_vStandardGameSetIsActivable(p_stObject->h_StandardGame,0);
   break;
	default:
     break;
 }
}
 
/*************************************************************************/
void fn_vStdGameInitValueSAI(MS_tdxHandleToStandardGame h_StdGame)
{
/* ANNECY AV {*/
/*
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucHitPoints,			SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenNewGameStart);
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucHitPointsMax,		SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucMiscFlags,			SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded);
 SAI_fn_bInsertLongInTable((void*)&h_StdGame->ulCustomBits,			SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);
*/
    h_StdGame -> ucSaveHitPoints    = h_StdGame -> ucHitPoints;
    h_StdGame -> ucSaveHitPointsMax = h_StdGame -> ucHitPointsMax;
/* ANNECY MT - 09/10/98 {*/
    h_StdGame -> ucSaveMiscFlags    = (unsigned char)(h_StdGame -> ucMiscFlags & GAM_C_SavedMiscFlags);
/* END ANNECY MT }*/
    h_StdGame -> ulSaveCustomBits   = h_StdGame -> ulCustomBits;
/* END ANNECY AV }*/
}
/*************************************************************************/
void fn_vStdGameSavedValueSAI(MS_tdxHandleToStandardGame h_StdGame)
{
#ifndef U64
	if( fn_eStandardGameGetInitFlagWhenDeadOrTaken(h_StdGame) == OI_NeverBackWhenTaken )
		SAI_fn_bInsertBoolInTable((void*)&h_StdGame->ucMiscFlags,GAM_C_DesactivateAtAll,SAI_eLevelSaveCurrentValue);
#endif
}
/*************************************************************************/
void fn_vStdGamePlayerSavedValueSAI(MS_tdxHandleToStandardGame h_StdGame)
{
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucHitPoints,SAI_ePlayerSaveCurrentValue);
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucHitPointsMax,SAI_ePlayerSaveCurrentValue);
 SAI_fn_bInsertCharInTable((void*)&h_StdGame->ucHitPointsMaxMax,SAI_ePlayerSaveCurrentValue);
}
/*************************************************************************/
void fn_vStdGameFreeInitValueSAI(MS_tdxHandleToStandardGame h_StdGame)
{
/* ANNECY AV {*/
/*
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucHitPoints,		SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenNewGameStart);
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucHitPointsMax,	SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucMiscFlags,		SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded);
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ulCustomBits,		SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);
*/
/* END ANNECY AV }*/
#ifndef U64
	if( fn_eStandardGameGetInitFlagWhenDeadOrTaken(h_StdGame) == OI_NeverBackWhenTaken )
		SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucMiscFlags,SAI_eLevelSaveCurrentValue);
#endif
}
/*************************************************************************/
void fn_vStdGamePlayerFreeInitValueSAI(MS_tdxHandleToStandardGame h_StdGame)
{
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucHitPoints,SAI_ePlayerSaveCurrentValue);
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucHitPointsMax,SAI_ePlayerSaveCurrentValue);
 SAI_fn_bRemoveValueFromTable((void*)&h_StdGame->ucHitPointsMaxMax,SAI_ePlayerSaveCurrentValue);
}
/**************************************************************************/
void fn_vSetHitPointsToInitValue(struct tdstEngineObject_ *_p_stObject)
{
/* ANNECY AV {*/
/* SAI_fn_bInitOneValueWhen((void*)&_p_stObject->h_StandardGame->ucHitPoints,SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenNewGameStart);*/
   _p_stObject -> h_StandardGame -> ucHitPoints = _p_stObject -> h_StandardGame -> ucSaveHitPoints;
/* END ANNECY AV }*/
}
/**************************************************************************/
void fn_vSetHitPointsMaxToInitValue(struct tdstEngineObject_ *_p_stObject)
{
/* ANNECY AV {*/
/* SAI_fn_bInitOneValueWhen((void*)&_p_stObject->h_StandardGame->ucHitPointsMax,SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);*/
   _p_stObject -> h_StandardGame -> ucHitPointsMax = _p_stObject -> h_StandardGame -> ucSaveHitPointsMax;
/* END ANNECY AV }*/
}
/**************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vSetCustomBitsToInitValue(struct tdstEngineObject_ *_p_stObject)
{
/* ANNECY AV {*/
/* SAI_fn_bInitOneValueWhen((void*)&_p_stObject->h_StandardGame->ulCustomBits,SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);*/
   _p_stObject -> h_StandardGame -> ulCustomBits = _p_stObject -> h_StandardGame -> ulSaveCustomBits;
/* END ANNECY AV }*/
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*ENDANNECY BBB 20/11/97 }*/
/*************************************************************************/
 
/*-----------------------------------------------------------------------------
 *  Description : StdGame's object copy clone action
 *-----------------------------------------------------------------------------
 *  Input :         Handle to destination, handle to source
 *  Output :        None
 *-----------------------------------------------------------------------------
 *  Creation date : 30/06/97            Author : Olivier Jourdan
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vStdGameCopyClone(struct tdstEngineObject_ * _p_stObjectDest,struct tdstEngineObject_ * _p_stObjectSrc)
{
   /* If StdGame doesn't contains pointers to structure that may be*/
   /* changed during the game, this function must only be a :*/
   memcpy(_p_stObjectDest->h_StandardGame,_p_stObjectSrc->h_StandardGame,fn_ulStdGameSizeOf());
}
 
/*-----------------------------------------------------------------------------
 *  Description : StandardGame access functions
 *-----------------------------------------------------------------------------
 *  Input :         MS_tdxHandleToStandardGame + (value)
 *  Output :        (value or pointer)
 *-----------------------------------------------------------------------------
 *  Creation date : 30/01/97            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
HIE_tdxHandleToSuperObject fn_h_StandardGameGetSuperObject(MS_tdxHandleToStandardGame h_StdGame)
{
  SAF_M_AssertWithMsg((h_StdGame != NULL),"StdGame est NULL")
  return h_StdGame ? h_StdGame->h_SuperObject : NULL;
}
void fn_vStandardGameSetSuperObject(MS_tdxHandleToStandardGame h_StdGame,HIE_tdxHandleToSuperObject h_SuperObject)
{
 h_StdGame->h_SuperObject=h_SuperObject;
 /**** We put the absolute matrix in 3dData structure ****/
 /*RB to avoid NULL pointer access*/
	if( h_SuperObject )
	{
		fn_v3dDataSetAbsoluteMatrix(
			M_GetMSHandle(h_SuperObject,3dData),
			HIE_fn_hGetSuperObjectGlobalMatrix(h_SuperObject) );
	}
}
 
unsigned long fn_ulStandardGameGetCustomBitsSO (HIE_tdxHandleToSuperObject hSupObj)
{
    return fn_ulStandardGameGetCustomBits (M_GetMSHandle(hSupObj,StandardGame));
}
 
void fn_vStandardGameSetCustomBitsSO(HIE_tdxHandleToSuperObject hSupObj,unsigned long ulCustomBits)
{
	fn_vStandardGameSetCustomBits(M_GetMSHandle(hSupObj,StandardGame),ulCustomBits);
}
 
ACP_tdxBool fn_bStandardGameIsAPlatform (HIE_tdxHandleToSuperObject hSupObj)
{
    return (ACP_tdxBool) (fn_ucStandardGameGetPlatFormType(M_GetMSHandle(hSupObj,StandardGame))==C_ucPlatFormType_StandardPlatForm);
}
 
unsigned char fn_ucStandardGameGetTransparencyZoneMinSO(HIE_tdxHandleToSuperObject hSupObj)
{
	register MS_tdxHandleToStandardGame hStdGame = M_GetMSHandle(hSupObj,StandardGame);
	return (unsigned char)(hStdGame ? fn_ucStandardGameGetTransparencyZoneMin(hStdGame) : C_TRANSPARENCY_ZONE_MIN);
}
 
unsigned char fn_ucStandardGameGetTransparencyZoneMaxSO(HIE_tdxHandleToSuperObject hSupObj)
{
	register MS_tdxHandleToStandardGame hStdGame = M_GetMSHandle(hSupObj,StandardGame);
	return (unsigned char)(hStdGame ? fn_ucStandardGameGetTransparencyZoneMax(hStdGame) : C_TRANSPARENCY_ZONE_MAX);
}
 
/* ANNECY MT - 22/10/98 {*/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
unsigned char fn_ucStandardGameGetTooFarLimitSO(HIE_tdxHandleToSuperObject hSupObj)
{
	register MS_tdxHandleToStandardGame hStdGame = M_GetMSHandle(hSupObj,StandardGame);
    return (unsigned char)(hStdGame ? fn_ucStandardGameGetTooFarLimit(hStdGame) : C_TOOFARLIMIT_INIT);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

/* END ANNECY MT }*/
 
/*************************************/
/**** Access functions for Editor ****/
/*************************************/
#if defined(ACTIVE_EDITOR)
/*************************************************************************/
unsigned char *fn_p_ucStdGameGetHitPointsInit(MS_tdxHandleToStandardGame h_StdGame)
{
/* ANNECY AV {*/
/* return (unsigned char *)SAI_fn_p_vGetTableValuePointerOf((void*)&h_StdGame->ucHitPoints,SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenNewGameStart);*/
   return & h_StdGame -> ucSaveHitPoints;
/* END ANNECY AV }*/
}
/*************************************************************************/
unsigned char *fn_p_ucStdGameGetHitPointsMaxInit(MS_tdxHandleToStandardGame h_StdGame)
{
/* ANNECY AV {*/
/* return (unsigned char *)SAI_fn_p_vGetTableValuePointerOf((void*)&h_StdGame->ucHitPointsMax,SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);*/
   return & h_StdGame -> ucSaveHitPointsMax;
/* END ANNECY AV }*/
}
 
/*************************************************************************/
unsigned long *fn_p_ulStdGameGetCustomBitsInit(MS_tdxHandleToStandardGame _hStdGame)
{
/* ANNECY AV {*/
/* return (unsigned long *)SAI_fn_p_vGetTableValuePointerOf((void*)&_hStdGame->ulCustomBits,SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded|SAI_eInitWhenNewGameStart);*/
   return & _hStdGame -> ulSaveCustomBits;
/* END ANNECY AV }*/
}
 
/*************************************************************************/
void fn_vStdGameSetLoadedInFix(MS_tdxHandleToStandardGame h_StdGame,unsigned char bValue)
{
 h_StdGame->bIsLoadedInFix = bValue;
}
/*************************************************************************/
unsigned char fn_bStdGameIsLoadedInFix(MS_tdxHandleToStandardGame h_StdGame)
{
 return(h_StdGame->bIsLoadedInFix);
}
/*************************************************************************/
 
/* ANNECY MT - 19/11/98 {*/
void fn_vStdGameSetInAllSubMaps(MS_tdxHandleToStandardGame h_StdGame,unsigned char bValue)
{
 h_StdGame->bIsInAllSubMaps = bValue;
}
/*************************************************************************/
unsigned char fn_bStdGameIsInAllSubMaps(MS_tdxHandleToStandardGame h_StdGame)
{
 return(h_StdGame->bIsInAllSubMaps);
}
/* END ANNECY MT }*/
 
/*************************************************************************/
#endif /* ACTIVE_EDITOR */
 
unsigned char fn_ucStdGameCheckValidity(HIE_tdxHandleToSuperObject _h_SprObj) {
	if ( M_GetMSHandle(_h_SprObj, StandardGame )) 
		return (TRUE) ;
	return (FALSE) ;
}
