/*=========================================================================
* RND_Main.c : 
*     Variables declaration and init function
*
* (c) Ubi Studios 1997
*=======================================================================*/
#include "Acp_base.h"
 
#include "RND_CPA.h"
 
#define D_RND_Input_StructureDefine
#define D_RND_Input_VariableDefine
 
#include "TMR.h"
#include "RND.h"
 
#include "MemRND.h"
#include "ErrRND.h"
#include "RND_Scpt.h"
#include "RND_Str.h"
 
#undef D_RND_Input_StructureDefine
#undef D_RND_Input_VariableDefine
 
/* LOL portage*/
#ifdef U64
unsigned long	RND_ulRandVal=	1L;
 
#define	RND_M_ulNextRand()		\
	(RND_ulRandVal= RND_ulRandVal * 1103515245L + 12345)
 
#define	RND_M_ulRand()		\
	((RND_M_ulNextRand() >> 16) & 0x7FFF)
 
#endif /*U64*/
 
/*************************************************************************/
#define RND_M_vIncIndex(_lIndexNumber) \
{\
 RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]++;\
 if (RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]>=RND_g_stRandomStructure.ulSizeOfTable)\
  RND_g_stRandomStructure.a_ulIndex[_lIndexNumber] = 0;\
}
/*************************************************************************/
#define RND_M_ulGetRandomValue(_lIndexNumber)\
 (RND_g_stRandomStructure.p_ulTable[RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]])
 
/*************************************************************************/
#define RND_M_vGetRandomVector(_lIndexNumber,Vect)										\
	{	register unsigned long ulTemp=RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]; \
		(Vect)->xX=(MTH_tdxReal) RND_g_stRandomStructure.p_ulTable[ulTemp++];		    \
		if (ulTemp>=RND_g_stRandomStructure.ulSizeOfTable) ulTemp = 0;					\
		(Vect)->xY=(MTH_tdxReal) RND_g_stRandomStructure.p_ulTable[ulTemp++];		    \
		if (ulTemp>=RND_g_stRandomStructure.ulSizeOfTable) ulTemp = 0;					\
		(Vect)->xZ=(MTH_tdxReal) RND_g_stRandomStructure.p_ulTable[ulTemp++];		    \
		if (ulTemp>=RND_g_stRandomStructure.ulSizeOfTable) ulTemp = 0;					\
		RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]=ulTemp;						\
	}
/*************************************************************************/
#ifdef U64
void RND_fn_vSetSeed(unsigned long _ulSeed)
{
	RND_ulRandVal=_ulSeed;
}
#endif
/*************************************************************************/
void RND_fn_vFirstInitRandom(void)
{
 Erm_M_InitErrMsg(RND);
 Mmg_M_InitMmg(RND);
 Mmg_fn_cCheckAlignmentValidity();
 RND_g_stRandomStructure.hLastUsedIndex = 0;
}
/*************************************************************************/
void RND_fn_vInitRandom(void)
{
#if !defined(U64)

 /**** Script initialisation ****/
 SCR_fn_v_RdL0_RegisterCallback("RandomDescription",RND_fn_eRandomScriptCallBack,SCR_CRC_c_RdL0_ForSection);

#else /* U64 */

	#define D_U64_NB_ELEMENTS	256

	#ifdef CHECK_MEMORY
		Mmg_M_InitSpecificBlock(RND,E_ucRNDMemoryBlock, C_uwAllocSize+D_U64_NB_ELEMENTS*sizeof(unsigned long), C_ulDefaultMaxNbStaticMalloc, 4, C_BlockWithFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#else /* CHECK_MEMORY */
		Mmg_M_InitSpecificBlock(RND,E_ucRNDMemoryBlock, D_U64_NB_ELEMENTS*sizeof(unsigned long), C_ulDefaultMaxNbStaticMalloc, 4, C_BlockWithoutFreeFlag | C_Check_AlignementFlag | C_Check_OverflowFlag);
	#endif /* CHECK_MEMORY */

	#ifndef FINAL_VERSION
		RND_fn_vSetSeed( 1 );
	#else
		RND_fn_vSetSeed( osGetCount() );
	#endif /* FINAL_VERSION */

	Mmg_M_SetModeAlloc4Ch(RND,E_ucRNDMemoryBlock,C_ucMmgDefaultChannel);
 
	RND_g_stRandomStructure.ulSizeOfTable = D_U64_NB_ELEMENTS;
	MMG_fn_vAddMemoryInfo (MMG_C_lTypeRND , MMG_C_lSubTypeRND , NULL);
	RND_g_stRandomStructure.p_ulTable = Mmg_fn_p_vAlloc4Ch(D_U64_NB_ELEMENTS*sizeof(unsigned long),C_ucMmgDefaultChannel);

	RND_fn_vComputeRandomTable();
#endif /* U64 */
}
/*************************************************************************/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void RND_fn_vDesinitRandom(void)
{
#if !defined(U64)
 SCR_fn_v_RdL0_DeleteRegisterCallback("RandomDescription",SCR_CRC_c_RdL0_ForSection, SCR_CDR_c_RdL0_Contains);
#endif /* U64 */
 
 Mmg_M_DeleteBlock(RND,E_ucRNDMemoryBlock);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

/*************************************************************************/
 
 
#if !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS)
void RND_PrintUsedStaticMemory(void)
{
	Mmg_M_PrintUsedStaticMemoryInModule(RND);
}
#endif !defined(RETAIL) || defined(FINAL_VERSION_FOR_TESTERS)
 
/**************************************************************************/
/*XB*/
#ifdef CHECK_MEMORY
void RND_CheckMemory(void)
{
	Mmg_M_CheckMemory(RND);
}
#endif /* CHECK_MEMORY */
/*End XB*/

/**************************************************************************/
/* XB 02/06/99 */
#ifndef FINAL_VERSION
unsigned long RND_fn_ulGetUsedStaticMemory(void)
{
	return Mmg_M_GetUsedStaticMemory(RND);
}
#endif /* FINAL_VERSION */
/* End XB 02/06/99 */
 
/**************************************************************************/
RND_tdxHandleToRandomIndex RND_fn_hReserveANewHandleIndex(void)
{
 if (RND_g_stRandomStructure.hLastUsedIndex>RND_C_MaxIndex)
  M_RNDFatalError(E_uwRNDIncreaseRND_C_MaxIndex);
 
 RND_g_stRandomStructure.hLastUsedIndex++;
 return(RND_g_stRandomStructure.hLastUsedIndex-1);
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void RND_fn_vReadRandomScript(char *_szName)
{
#if !defined(U64)
 SCR_fnp_st_RdL0_AnalyseSection(_szName, SCR_CDF_uw_Anl_ForceAnalyse);
#endif /* U64 */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 

void RND_fn_vComputeRandomTable(void)
{
 unsigned long i;
 
#ifndef U64
 srand((unsigned)GetTickCount());
#endif /* U64 */
 
 RND_g_stRandomStructure.ulMaxValueInTable = 0;
 for (i=0;i<RND_g_stRandomStructure.ulSizeOfTable;i++)
 {
#ifndef U64
  RND_g_stRandomStructure.p_ulTable[i] = rand();
#else /* U64 */
  RND_g_stRandomStructure.p_ulTable[i] = RND_M_ulRand();
#endif /* U64 */
 }
 RND_fn_vRemapRandomTable();
}
 
/*************************************************************************/
void RND_fn_vRemapRandomTable(void)
{
 unsigned long i;
 unsigned long ulMinValueInTable;
 
 ulMinValueInTable = RND_g_stRandomStructure.p_ulTable[0];
 RND_g_stRandomStructure.ulMaxValueInTable = 0;
 for (i=0;i<RND_g_stRandomStructure.ulSizeOfTable;i++)
 {
  if (RND_g_stRandomStructure.p_ulTable[i]<ulMinValueInTable)
   ulMinValueInTable = RND_g_stRandomStructure.p_ulTable[i];
 }
 for (i=0;i<RND_g_stRandomStructure.ulSizeOfTable;i++)
 {
  RND_g_stRandomStructure.p_ulTable[i]-=ulMinValueInTable;
  if (RND_g_stRandomStructure.p_ulTable[i]>RND_g_stRandomStructure.ulMaxValueInTable)
   RND_g_stRandomStructure.ulMaxValueInTable = RND_g_stRandomStructure.p_ulTable[i];
 }
 /*RND_g_stRandomStructure.xMaxValueInTable = (MTH_tdxReal)RND_g_stRandomStructure.ulMaxValueInTable;*/
 RND_g_stRandomStructure.ulMaxValueInTable ++;
 RND_g_stRandomStructure.xInverseMaxValueInTable = MTH_M_xDiv(MTH_C_ONE,(MTH_tdxReal)RND_g_stRandomStructure.ulMaxValueInTable);
 
/* Oliv' - Portage v14 - why isn't it initialized at all ?*/
	memset( RND_g_stRandomStructure.a_ulIndex, 0, RND_C_MaxIndex*sizeof(unsigned long) );
/* EndOfOliv'*/
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void RND_fn_vReinitRandomTable(void)
{
 long i;
 
 for (i=0;i<RND_C_MaxIndex;i++)
  RND_g_stRandomStructure.a_ulIndex[i] = 0;
}
/*ANNECY BBB {*/
/*************************************************************************/
void RDN_fn_vSetRandomSeed(RND_tdxHandleToRandomIndex _lIndexNumber, long _lNewSeed)
{
	if ( _lNewSeed < 0 )
		_lNewSeed = 0;
	else
		_lNewSeed %= RND_g_stRandomStructure.ulSizeOfTable;
 
	RND_g_stRandomStructure.a_ulIndex[_lIndexNumber] = _lNewSeed;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*ANNECY BBB }*/
/*************************************************************************/
unsigned long RND_fn_ulGetUnsignedLongRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,unsigned long _ulMin,unsigned long _ulMax)
{
 unsigned long ulValue;
 
 ulValue = (_ulMin + ((1+_ulMax-_ulMin)*RND_M_ulGetRandomValue(_lIndexNumber))/(RND_g_stRandomStructure.ulMaxValueInTable+1));
 
 RND_M_vIncIndex(_lIndexNumber);
 return (ulValue);
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned char RND_fn_ulGetUnsignedCharRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,unsigned char _ucMin,unsigned char _ucMax)
{
 unsigned char ucValue;
 
 ucValue = (unsigned char)(_ucMin + ((_ucMax+1-_ucMin)*RND_M_ulGetRandomValue(_lIndexNumber))/(RND_g_stRandomStructure.ulMaxValueInTable+1));
 
 RND_M_vIncIndex(_lIndexNumber);
 return (ucValue);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
long RND_fn_lGetLongRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,long _lMin,long _lMax)
{
 long lValue;
 lValue = (_lMin + ((_lMax+1-_lMin)*(long)RND_M_ulGetRandomValue(_lIndexNumber))/(RND_g_stRandomStructure.ulMaxValueInTable+1));
 
 RND_M_vIncIndex(_lIndexNumber);
 return (lValue);
}
/*************************************************************************/
MTH_tdxReal RND_fn_xGetRealRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,MTH_tdxReal _xMin,MTH_tdxReal _xMax)
{
 MTH_tdxReal xValue;
 
/* xValue = (_xMin + ((_xMax-_xMin)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);*/
 xValue = (_xMin + ((_xMax-_xMin)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))*RND_g_stRandomStructure.xInverseMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 return (xValue);
}
/*************************************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
struct MTH3D_tdstVector_ *RND_fn_p_stGetVectorRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,struct MTH3D_tdstVector_ *_p_stMin,struct MTH3D_tdstVector_ *_p_stMax,struct MTH3D_tdstVector_ *_p_stReturnedVector)
{
/*
 _p_stReturnedVector->xX = (_p_stMin->xX + ((_p_stMax->xX-_p_stMin->xX)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedVector->xY = (_p_stMin->xY + ((_p_stMax->xY-_p_stMin->xY)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedVector->xZ = (_p_stMin->xZ + ((_p_stMax->xZ-_p_stMin->xZ)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
*/
  MTH3D_tdstVector stScaleVector;
  MTH3D_M_vSubVector(_p_stReturnedVector,_p_stMax,_p_stMin);
  RND_M_vGetRandomVector(_lIndexNumber,&stScaleVector);
  MTH3D_M_vScaleVector(_p_stReturnedVector,_p_stReturnedVector,&stScaleVector);
  MTH3D_M_vAddVector(_p_stReturnedVector,_p_stReturnedVector,_p_stMin);
  MTH3D_M_vMulScalarVector(_p_stReturnedVector,RND_g_stRandomStructure.xInverseMaxValueInTable,_p_stReturnedVector);
 
  return (_p_stReturnedVector);
}
/*************************************************************************/
struct MTH3D_tdstMatrix_ *RND_fn_p_stGetMatrixRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber,struct MTH3D_tdstMatrix_ *_p_stMin,struct MTH3D_tdstMatrix_ *_p_stMax,struct MTH3D_tdstMatrix_ *_p_stReturnedMatrix)
{
 /*
 _p_stReturnedMatrix->stCol_0.xX = (_p_stMin->stCol_0.xX + ((_p_stMax->stCol_0.xX-_p_stMin->stCol_0.xX)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_0.xY = (_p_stMin->stCol_0.xY + ((_p_stMax->stCol_0.xY-_p_stMin->stCol_0.xY)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_0.xZ = (_p_stMin->stCol_0.xZ + ((_p_stMax->stCol_0.xZ-_p_stMin->stCol_0.xZ)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_1.xX = (_p_stMin->stCol_1.xX + ((_p_stMax->stCol_1.xX-_p_stMin->stCol_1.xX)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_1.xY = (_p_stMin->stCol_1.xY + ((_p_stMax->stCol_1.xY-_p_stMin->stCol_1.xY)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_1.xZ = (_p_stMin->stCol_1.xZ + ((_p_stMax->stCol_1.xZ-_p_stMin->stCol_1.xZ)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_2.xX = (_p_stMin->stCol_2.xX + ((_p_stMax->stCol_2.xX-_p_stMin->stCol_2.xX)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_2.xY = (_p_stMin->stCol_2.xY + ((_p_stMax->stCol_2.xY-_p_stMin->stCol_2.xY)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 _p_stReturnedMatrix->stCol_2.xZ = (_p_stMin->stCol_2.xZ + ((_p_stMax->stCol_2.xZ-_p_stMin->stCol_2.xZ)*(MTH_tdxReal)RND_M_ulGetRandomValue(_lIndexNumber))/RND_g_stRandomStructure.xMaxValueInTable);
 RND_M_vIncIndex(_lIndexNumber);
 
 
 RND_M_vIncIndex(_lIndexNumber);
*/
 MTH3D_tdstVector stScaleVector;
 
 MTH3D_M_vSubMatrix(_p_stReturnedMatrix,_p_stMax,_p_stMin);
 
 RND_M_vGetRandomVector(_lIndexNumber,&stScaleVector);
 MTH3D_M_vScaleVector(&(_p_stReturnedMatrix->stCol_0),&(_p_stReturnedMatrix->stCol_0),&stScaleVector);
 
 RND_M_vGetRandomVector(_lIndexNumber,&stScaleVector);
 MTH3D_M_vScaleVector(&(_p_stReturnedMatrix->stCol_1),&(_p_stReturnedMatrix->stCol_1),&stScaleVector);
 
 RND_M_vGetRandomVector(_lIndexNumber,&stScaleVector);
 MTH3D_M_vScaleVector(&(_p_stReturnedMatrix->stCol_2),&(_p_stReturnedMatrix->stCol_2),&stScaleVector);
 
 MTH3D_M_vAddMatrix(_p_stReturnedMatrix,_p_stReturnedMatrix,_p_stMin);
 MTH3D_M_vMulScalarMatrix(_p_stReturnedMatrix,RND_g_stRandomStructure.xInverseMaxValueInTable,_p_stReturnedMatrix);
 
 return (_p_stReturnedMatrix);
}
/*************************************************************************/
unsigned long RND_fn_ulGetRandomValue(RND_tdxHandleToRandomIndex _lIndexNumber)
{
 return(RND_g_stRandomStructure.p_ulTable[RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]]);
}
/*************************************************************************/
void RND_fn_vIncIndex(RND_tdxHandleToRandomIndex _lIndexNumber)
{
 RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]++;
 if (RND_g_stRandomStructure.a_ulIndex[_lIndexNumber]>=RND_g_stRandomStructure.ulSizeOfTable)
  RND_fn_vReinitRandomTable();
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*************************************************************************/
 
