/*=========================================================================
* ObjsTbls.c : This module contain all models functions
* This is a part of the Game project.
*
* Version              1.0
* Creation date        01/04/97
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1997
*=======================================================================*/
 
/*******************************************************/
/**** For the structures and variables declarations ****/
/*******************************************************/
#define  D_ObjsTbls_Define
#include "ToolsCPA.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Family.h"
#include "Structur/MemGame.h"
#include "Structur/ErrGame.h"
#include "Structur/Objects.h"
#include "Structur/GameScpt.h"
#include "Structur/MemGame.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
 
#include "Structur/ObjsTbls.h"
 
#include "ZeMem.h"
 
/* ************* ObjectsTablesList *************/
tdxHandleToObjectsTablesList fn_hObjectsTablesListAlloc(void)
{
	tdxHandleToObjectsTablesList h_ObjectsTablesListElement;
 
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLList , 0 );
	h_ObjectsTablesListElement = (tdxHandleToObjectsTablesList)M_p_GameMallocInHLM(sizeof(struct tdstObjectsTablesList_));
 
	h_ObjectsTablesListElement->wNumberOfElement = 0;
	h_ObjectsTablesListElement->d_stObjectsTable = NULL;
#ifndef U64 //AR9904 Useless !
	h_ObjectsTablesListElement->d_stInitObjectsTable = NULL;
#endif 
/* ANNECY AV {*/
    h_ObjectsTablesListElement->wZDxUsed = 0;
/* END ANNECY AV }*/
 
	return(h_ObjectsTablesListElement);
}
/* ************************************************************************************************* */
/* ************************************************************************************************* */
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_hObjectsTablesListFree(tdxHandleToObjectsTablesList h_ObjectsTableElement)
{
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLList , 0 );
	M_GameFreeInHLM(h_ObjectsTableElement);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/* ************************************************************************************************* */
/* ************************************************************************************************* */
 
void fn_vInitObjectsTablesList(struct tdstFamilyList_ *p_stFamily)
{
	/* Init the List structure*/
	LST2_M_StaticInitAnchor(&(p_stFamily->hForObjectsTablesList));
}
 
#ifndef LST2_StaticIsOptimised
void fn_vAddAnObjectsTablesInList(	struct tdstFamilyList_ *p_stFamily
																	,	tdxHandleToObjectsTablesList h_ObjectsTablesListElement)
{
	LST2_M_StaticAddTail(&(p_stFamily->hForObjectsTablesList),h_ObjectsTablesListElement);
}
#else
void fn_vAddAnObjectsTablesInList(	struct tdstFamilyList_ *p_stFamily,	tdxHandleToObjectsTablesList h_ObjectsTablesListElement)
{
	LST2_M_StaticSetNumberOfElements(&(p_stFamily->hForObjectsTablesList), 1);
	LST2_M_StaticSetFirstElement(&(p_stFamily->hForObjectsTablesList),h_ObjectsTablesListElement);
}
#endif /*AR9808*/
 
/* ************************************************************************************************* */
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vChangeObjectsTableForCharacter(HIE_tdxHandleToSuperObject p_stSuperObject, long l_Number)
{
	long i;
	tdxHandleToObjectsTablesList h_ObjectsTablesListElement;
 
	if(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))==NULL) return;
 
  LST2_M_StaticGetElementNumber(&(fn_h3dDataGetFamily(M_GetMSHandle(p_stSuperObject,3dData))->hForObjectsTablesList)
                            ,h_ObjectsTablesListElement
														,l_Number,i);
 
	fn_v3dDataSetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData),h_ObjectsTablesListElement);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/* ************************************************************************************************* */
 
/* ************************************************************************************************* */
void fn_vExchangeTwoObjectsInCurrentObjectsTable(HIE_tdxHandleToSuperObject p_stSuperObject
																								 ,unsigned short w_ObjectA ,unsigned short w_ObjectB)
{
	struct tdstObjectsTableElement_ stObjectsTableElement;
 
	if(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))==NULL) return;
 
	memcpy(	&stObjectsTableElement
				,	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectB])
				,	sizeof(struct tdstObjectsTableElement_));
 
	memcpy(	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectB])
				,	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectA])
				,	sizeof(struct tdstObjectsTableElement_));
 
	memcpy(	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectA])
				,	&stObjectsTableElement
				,	sizeof(struct tdstObjectsTableElement_));
 
}
/* ************************************************************************************************* */
/* ************************************************************************************************* */
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vCopyObjectDestFromSourceOfObjectsTable(HIE_tdxHandleToSuperObject p_stSuperObject
																								 ,unsigned short w_ObjectDest ,unsigned short w_ObjectSource)
{
	if(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))==NULL) return;
 
	memcpy(	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectDest])
				,	&(fn_h3dDataGetCurrentObjectsTable(M_GetMSHandle(p_stSuperObject,3dData))->d_stObjectsTable[w_ObjectSource])
				,	sizeof(struct tdstObjectsTableElement_));
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/* ************************************************************************************************* */
/* ************************************************************************************************* */
void fn_vCopyObjectFromObjectTabletoAnother(
	tdxHandleToObjectsTablesList _hTargetTable,
	tdxHandleToObjectsTablesList _hSourceTable,
	unsigned short _uwTargetObject,
	unsigned short _uwSourceObject
)
{
	if ( _hTargetTable == NULL || _hSourceTable == NULL ) return;
 
	_hTargetTable->d_stObjectsTable[_uwTargetObject] = _hSourceTable->d_stObjectsTable[_uwSourceObject];
}
/* ************************************************************************************************* */
/* ************************************************************************************************* */
unsigned short fn_uwGetObjectTableNumberOfelements(tdxHandleToObjectsTablesList _hTargetTable)
{
	if ( _hTargetTable )
		return  _hTargetTable->wNumberOfElement;
	else
		return 0;
}
/* ************************************************************************************************* */
/* ************************************************************************************************* */
 
/* ************* ObjectsTables *************/
void fn_vObjectsTablesAlloc(tdxHandleToObjectsTablesList h_ObjectsTablesListElement,unsigned short wNumberOfElement)
{
	unsigned long i;
 
	h_ObjectsTablesListElement->wNumberOfElement = wNumberOfElement;

    MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , 0 );
	h_ObjectsTablesListElement->d_stObjectsTable
				= (struct tdstObjectsTableElement_ *)M_p_GameMallocInHLM(wNumberOfElement*sizeof(struct tdstObjectsTableElement_));

#ifndef U64 //AR9904 Useless !
	h_ObjectsTablesListElement->d_stInitObjectsTable = NULL;
#endif
 
	for ( i = 0; i < wNumberOfElement; i ++ )
		h_ObjectsTablesListElement->d_stObjectsTable[i].wTypeOfTarget = C_wTdO_Undefined;
}
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vDuplicateObjectTable(tdxHandleToObjectsTablesList hObjTableList)
{
    MMG_fn_vAddMemoryInfo( MMG_C_lTypeFamily , MMG_C_lSubTypeTBLElement , 0 );
	hObjTableList->d_stInitObjectsTable
				= (struct tdstObjectsTableElement_ *)M_p_GameMallocInHLM(hObjTableList->wNumberOfElement*sizeof(struct tdstObjectsTableElement_));
	memcpy(hObjTableList->d_stInitObjectsTable,hObjTableList->d_stObjectsTable,sizeof(struct tdstObjectsTableElement_)*hObjTableList->wNumberOfElement);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vReInitObjectTable(tdxHandleToObjectsTablesList hObjTableList)
{
	memcpy(hObjTableList->d_stObjectsTable,hObjTableList->d_stInitObjectsTable,sizeof(struct tdstObjectsTableElement_)*hObjTableList->wNumberOfElement);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/* ***** ACCESS FUNCTIONS BY HANDLES ***** */
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdxHandleToObjectsTableElement fn_d_hGetObjectsTable(tdxHandleToObjectsTablesList h_ObjectsTablesListElement)
{
  return(h_ObjectsTablesListElement->d_stObjectsTable);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdxHandleToObjectsTableElement fn_d_hGetInitObjectsTable(tdxHandleToObjectsTablesList h_ObjectsTablesListElement)
{
  return(h_ObjectsTablesListElement->d_stInitObjectsTable);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdxHandleToObjectsTableElement fn_d_hGetElementByNumberInObjectsTable(tdxHandleToObjectsTablesList h_ObjectsTablesListElement
																	  ,unsigned short wNumberInTable)
{
  return(&(h_ObjectsTablesListElement->d_stObjectsTable[wNumberInTable]));
}
 
 
/* *** TypeOfTarget*/
unsigned short fn_wObjectsTableGetTypeOfTarget(tdxHandleToObjectsTableElement h_ObjectsTablesElement)
{
  return(h_ObjectsTablesElement->wTypeOfTarget);
}
 
void fn_vObjectsTableSetTypeOfTarget(tdxHandleToObjectsTableElement h_ObjectsTablesElement
									,unsigned short wTypeOfTarget)
{
  h_ObjectsTablesElement->wTypeOfTarget = wTypeOfTarget;
}
 
/* *** h_Target*/
tdxHandleToVoid fn_hObjectsTableGetTarget(tdxHandleToObjectsTableElement h_ObjectsTablesElement)
{
  return(h_ObjectsTablesElement->h_Target);
}
 
void fn_vObjectsTableSetTarget(tdxHandleToObjectsTableElement h_ObjectsTablesElement
									,tdxHandleToVoid h_Target)
{
  h_ObjectsTablesElement->h_Target = h_Target;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*
// MR0707
void fn_vSetObjectTableListDuplicate(tdxHandleToObjectsTablesList hObjTableList,BOOL bDuplicate)
{
	hObjTableList->bDuplicateTable=bDuplicate;
}
 
BOOL fn_bGetObjectTableListDuplicate(tdxHandleToObjectsTablesList hObjTableList)
{
	return hObjTableList->bDuplicateTable;
}
// End MR0707
*/
 
/* ANNECY AV {*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool fn_bObjectTableHasZdd (tdxHandleToObjectsTablesList _hObjectTableList)
{
    return (ACP_tdxBool) ((_hObjectTableList -> wZDxUsed & C_wObjectTableHasZdd) != 0);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
ACP_tdxBool fn_bObjectTableHasZde (tdxHandleToObjectsTablesList _hObjectTableList)
{
    return (ACP_tdxBool) ((_hObjectTableList -> wZDxUsed & C_wObjectTableHasZde) != 0);
}
/* END ANNECY AV }*/
