/*=========================================================================
* MSSound.cpp : MSSound's functions
*
*
*
*         MSSound 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_MSSound_StructureDefine
#define D_MSSound_VariableDefine
 
#include "ToolsCPA.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/MemGame.h"
#include "Structur/ErrGame.h"
#include "Structur/Objects.h"
#include "Structur/GameScpt.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
 
#include "Basic.h"
#include "ObjInit.h"
#include "ZeMem.h"
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's object action script callback
*-----------------------------------------------------------------------------
*  Input :         Object pointer
*  Output :        None
*-----------------------------------------------------------------------------
*  Creation date : 01/10/96            Author : xxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
#if defined(GAM_USE_SCRIPT)
SCR_tde_Anl_ReturnValue fn_eMSSoundScriptCallback(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_tdxHandleToMSSound h_MSSound;
	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_lSubTypeMSSound , p_stEngineObject);
		h_MSSound = fn_h_MSSoundRealAlloc();
        MMG_fn_vEndMemoryInfo ();
		p_stEngineObject->h_MSSound = h_MSSound;
 
		SCR_M_RdL0_SetSectionLong(C_ThisSection,0,h_MSSound);
	}
	else if (M_IsEntry)
	{
		SCR_M_RdL0_GetSectionLong(C_ThisSection,0,MS_tdxHandleToMSSound,h_MSSound);
      if( M_ActionIs(C_EntryMSSoundSaturationDistance) )
      {
         M_CheckScriptParamNumber(1);
			/*h_MSSound->stRollOff.rDistSatur = M_Fixed1616ToRealSnd(MTH_M_xRealToFixed16_16(MTH_M_xDoubleToReal(atof(_ap_szParams[0]))));*/
			h_MSSound->stRollOff.rDistSatur = atol(_ap_szParams[0]);
      }
      else if( M_ActionIs(C_EntryMSSoundBackGroundDistance) )
      {
         M_CheckScriptParamNumber(1);
			/*h_MSSound->stRollOff.rDistBackGround = M_Fixed1616ToRealSnd(MTH_M_xRealToFixed16_16(MTH_M_xDoubleToReal(atof(_ap_szParams[0]))));*/
			h_MSSound->stRollOff.rDistBackGround = atol(_ap_szParams[0]);
      }
	}
 
	return(eReturnValue);
}
#endif /* GAM_USE_SCRIPT */
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's save structure
*-----------------------------------------------------------------------------
*  Input :         SCR_tdst_File_Description *, struct tdstEngineObject_ *
*  Output :        Void
*-----------------------------------------------------------------------------
*  Creation date : 28/02/97            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
/*
void fn_vMSSoundSaveMiniStructure(SCR_tdst_File_Description *p_stFile,struct tdstEngineObject_ *p_stObject)
{
char szSection[SCR_CV_ui_Cfg_MaxLenName];
MS_tdxHandleToMSSound h_MSSound = p_stObject->h_MSSound;
 
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  SCR_M_SvL0_SaveComment(p_stFile, "**** MSSound mini-structure definition ****");
 
	 sprintf(szSection,"%s:",C_SectionMSSoundDescription);
	 SCR_M_SvL0_SaveBeginSection(p_stFile,szSection,SCR_CC_C_Cfg_EOL);
 
		// ** Put here the saving script functions **
 
		  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL)
		  }
*/
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's mini-structure sizeof
*-----------------------------------------------------------------------------
*  Input :         None
*  Output :        Size of
*-----------------------------------------------------------------------------
*  Creation date : 14/10/96            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
unsigned long fn_ulMSSoundSizeOf()
{
	return(sizeof(struct tdstMSSound_));
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's object action allocation
*-----------------------------------------------------------------------------
*  Input :         Object pointer
*  Output :        None
*-----------------------------------------------------------------------------
*  Creation date : 01/10/96            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
void fn_vMSSoundAlloc(struct tdstEngineObject_ *p_stObject)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_MSSound!=NULL)
		M_GameFatalError(E_uwGameMiniStructAllocAlreadyAlloc);
 
    MMG_fn_vBeginMemoryInfo (MMG_C_lTypeMiniStructure , MMG_C_lSubTypeMSSound , p_stObject);
	p_stObject->h_MSSound = fn_h_MSSoundRealAlloc();
    MMG_fn_vEndMemoryInfo ();
	fn_vMSSoundInitValueSAI(p_stObject->h_MSSound);
}
 
MS_tdxHandleToMSSound fn_h_MSSoundRealAlloc()
{
	MS_tdxHandleToMSSound h_MSSound;
 
	h_MSSound = (MS_tdxHandleToMSSound)M_p_GameMallocInHLM(fn_ulMSSoundSizeOf());
 
	return(h_MSSound);
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's object action free
*-----------------------------------------------------------------------------
*  Input :         Object pointer
*  Output :        None
*-----------------------------------------------------------------------------
*  Creation date : 01/10/96            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
void fn_vMSSoundFree(struct tdstEngineObject_ *p_stObject)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_MSSound==NULL)
		M_GameFatalError(E_uwGameMiniStructNull);
 
	fn_vMSSoundFreeInitValueSAI(p_stObject->h_MSSound);
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeMiniStructure , MMG_C_lSubTypeMSSound , p_stObject );
	M_GameFreeInHLM(p_stObject->h_MSSound);
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's object action initialisation
*-----------------------------------------------------------------------------
*  Input :         When this function's called (see ObjInit.h)
*  Output :        None
*-----------------------------------------------------------------------------
*  Creation date : 01/10/96            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
void fn_vMSSoundInit(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjectInit)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_MSSound==NULL)
		M_GameFatalError(E_uwGameMiniStructNull);
 
	switch(eObjectInit)
	{
	case OTI_WhenGameStart:
		break;
	case OTI_ReinitTheMap:
		break;
	case OTI_LoadSavedGame:
		break;
	case OTI_MapLoaded:
		fn_vMSSoundInitValueSAI(p_stObject->h_MSSound);
		fn_vMSSoundSavedValueSAI(p_stObject->h_MSSound);
		break;
	case OTI_PlayerDead:
		break;
	default:
     break;
	}
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound's object action desinitialisation
*-----------------------------------------------------------------------------
*  Input :         When this function's called (see ObjInit.h)
*  Output :        None
*-----------------------------------------------------------------------------
*  Creation date : 01/10/96            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
void fn_vMSSoundDesinit(struct tdstEngineObject_ *p_stObject,enum tdeObjectTreeInit_ eObjectInit)
{
	if (p_stObject==NULL)
		M_GameFatalError(E_uwGameMiniStructObjectNull);
 
	if (p_stObject->h_MSSound==NULL)
		M_GameFatalError(E_uwGameMiniStructNull);
 
	switch(eObjectInit)
	{
	case OTI_WhenGameStart:
		break;
	case OTI_ReinitTheMap:
		break;
	case OTI_MapLoaded:
		break;
	case OTI_PlayerDead:
		break;
	default:
     break;
	}
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound'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_vMSSoundCopyClone(struct tdstEngineObject_ * _p_stObjectDest,struct tdstEngineObject_ * _p_stObjectSrc)
{
   /* If MSSound doesn't contains pointers to structure that may be*/
   /* changed during the game, this function must only be a :*/
   memcpy(_p_stObjectDest->h_MSSound,_p_stObjectSrc->h_MSSound,fn_ulMSSoundSizeOf());
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound access functions
*-----------------------------------------------------------------------------
*  Input :         MS_tdxHandleToMSSound + (value)
*  Output :        (value or pointer)
*-----------------------------------------------------------------------------
*  Creation date : 29/01/97            Author : Olivier (JO) Jourdan
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
 

SndReal fn_xMSSoundGetSaturationDistance(MS_tdxHandleToMSSound h_MSSound)
{
	return( h_MSSound->stRollOff.rDistSatur );
}

void fn_vMSSoundSetSaturationDistance(MS_tdxHandleToMSSound h_MSSound,SndReal _xSaturationDistance)
{
	h_MSSound->stRollOff.rDistSatur = _xSaturationDistance;
}

SndReal fn_xMSSoundGetBackGroundDistance(MS_tdxHandleToMSSound h_MSSound)
{
	return( h_MSSound->stRollOff.rDistBackGround );
}

void fn_vMSSoundSetBackGroundDistance(MS_tdxHandleToMSSound h_MSSound,SndReal _xBackGroundDistance)
{
	h_MSSound->stRollOff.rDistBackGround = _xBackGroundDistance;
}

 
SndBool fn_bRetRollOffFactor(long _lObj,RollOffParam* _p_stRolloff)
{
	MS_tdxHandleToMSSound hMSSound;
 
	if(!_lObj || _lObj==SND_C_OBJET_FANTOME)
		return FALSE;	   
 
	hMSSound = M_GetMSHandle( (HIE_tdxHandleToSuperObject)_lObj,MSSound );
	if( !hMSSound )
		return FALSE;
	_p_stRolloff->rDistSatur = hMSSound->stRollOff.rDistSatur;
	_p_stRolloff->rDistBackGround = hMSSound->stRollOff.rDistBackGround;
	return TRUE;
}
 
/*-----------------------------------------------------------------------------
*  Description : MSSound SAI functions
*-----------------------------------------------------------------------------
*  Input :         MS_tdxHandleToMSSound + (value)
*  Output :        (value or pointer)
*-----------------------------------------------------------------------------
*  Creation date : 29/01/97            Author : xxxxxxxxx
*-----------------------------------------------------------------------------
*  Modification date :                 Modification Author :
*  Modifications :
*---------------------------------------------------------------------------*/
#define C_Sound_SAIInitFlags		SAI_eInitWhenPlayerDead|SAI_eInitWhenReinitTheMap|SAI_eInitWhenMapLoaded


void fn_vMSSoundInitValueSAI(MS_tdxHandleToMSSound h_MSSound)
{
	SAI_fn_bInsertLongInTable((void*)&h_MSSound->stRollOff.rDistSatur,C_Sound_SAIInitFlags);
	SAI_fn_bInsertLongInTable((void*)&h_MSSound->stRollOff.rDistBackGround,C_Sound_SAIInitFlags);
}

void fn_vMSSoundSavedValueSAI(MS_tdxHandleToMSSound h_MSSound)
{
	/**** Put here the functions :*/
	/****     SAI_fn_bInsertMTHVectorInTable((void*)&p_MSSound->xxx,SAI_eSaveCurrentValue);*/
}

void fn_vMSSoundFreeInitValueSAI(MS_tdxHandleToMSSound h_MSSound)
{
	SAI_fn_bRemoveValueFromTable((void*)&h_MSSound->stRollOff.rDistSatur,C_Sound_SAIInitFlags);
	SAI_fn_bRemoveValueFromTable((void*)&h_MSSound->stRollOff.rDistBackGround,C_Sound_SAIInitFlags);
}

#ifdef ACTIVE_EDITOR	/* MT { */
SndReal *fn_p_xMSSoundGetSaturationDistanceInit(MS_tdxHandleToMSSound h_MSSound)
{
	return (SndReal *)SAI_fn_p_vGetTableValuePointerOf((void*)&h_MSSound->stRollOff.rDistSatur, C_Sound_SAIInitFlags);
}

SndReal *fn_p_xMSSoundGetBackGroundDistanceInit(MS_tdxHandleToMSSound h_MSSound)
{
	return (SndReal *)SAI_fn_p_vGetTableValuePointerOf((void*)&h_MSSound->stRollOff.rDistBackGround, C_Sound_SAIInitFlags);
}
#endif /* MT } */