/*=========================================================================
* ObjType.c : This module contain all object's type functions
* This is a part of the Game project.
*
* Version                      1.0
* Creation date        15/10/96
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1996
*=======================================================================*/
 
#include "ToolsCPA.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Actions/AllActs.h"
 
#include "Structur/3DOSLkTb.h"
#include "Structur/ErrGame.h"
#include "Structur/MemGame.h"
#include "Structur/Objects.h"
#include "Structur/StdObjSt.h"
 
#include "ObjType.h"
#include "ZeMem.h"
 
/*-----------------------------------------------------------------------------
 *  Description :   Initialize the object's type structure (when the map is loaded)
 *-----------------------------------------------------------------------------
 *  Input :         void
 *  Output :        void
 *-----------------------------------------------------------------------------
 *  Creation date : 15/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vFirstInitObjectTypeStructure()
{
 LST2_M_DynamicInitAnchor(&g_stObjectTypes.hFamilyType);
 LST2_M_DynamicInitAnchor(&g_stObjectTypes.hModelType);
 LST2_M_DynamicInitAnchor(&g_stObjectTypes.hPersonalType);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Desinitialize the object's type structure (when the map is finish)
 *-----------------------------------------------------------------------------
 *  Input :         void
 *  Output :        void
 *-----------------------------------------------------------------------------
 *  Creation date : 15/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void fn_vLastDesinitObjectTypeStructure()
{
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

 
/*-----------------------------------------------------------------------------
 *  Description :   Find a object by its personal type (in the hierarchie)
 *-----------------------------------------------------------------------------
 *  Input :         personal type, first Super-Object
 *  Output :        object's pointer (NULL if not found)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
struct tdstEngineObject_ *fn_p_stFindObjectByItsPersonalType(tdObjectType otPersonalType,HIE_tdxHandleToSuperObject p_stSuperObject)
{
 tdstEngineObject *p_stEngineObject = NULL;
 tdstEngineObject *p_stTempEngineObject;
 long lSuperObjectType;
	HIE_tdxHandleToSuperObject p_stChild;
 long i;
 
 p_stTempEngineObject = (tdstEngineObject *)HIE_fn_hGetSuperObjectObject(p_stSuperObject);
 lSuperObjectType = HIE_fn_ulGetSuperObjectType(p_stSuperObject);
 
 if (  lSuperObjectType==HIE_C_ulActor
     &&M_ObjectGetPersonalType(p_stTempEngineObject)==otPersonalType)
  p_stEngineObject = p_stTempEngineObject;
 
	HIE_M_ForEachChildOf(p_stSuperObject,p_stChild,i)
 {
  if (p_stEngineObject==NULL)
   p_stEngineObject = fn_p_stFindObjectByItsPersonalType(otPersonalType,p_stChild);
 }
 
 return(p_stEngineObject);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Find or add a family type index of a family type name
 *-----------------------------------------------------------------------------
 *  Input :         family type name
 *  Output :        family type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdObjectType fn_otFindOrAddFamilyTypeOfFamilyTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 
 otType=fn_otFindFamilyTypeOfFamilyTypeName(szType);
 
 if (otType==-1)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement = M_p_GameMallocInHLM(sizeof(struct tdstObjectTypeElement_));
  LST2_M_DynamicInitElement(hHandleToObjectTypeElement);
  LST2_M_DynamicAddTail(&g_stObjectTypes.hFamilyType,hHandleToObjectTypeElement);
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(szType)+1);
  strcpy(hHandleToObjectTypeElement->szName,szType);
  otType = fn_otFindFamilyTypeOfFamilyTypeName(szType);
  hHandleToObjectTypeElement->ucElementPriority = GAM_g_ucHLMMemoryBlock;
 }
 return(otType);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a family type index of a family type name
 *-----------------------------------------------------------------------------
 *  Input :         family type name
 *  Output :        family type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdObjectType fn_otFindFamilyTypeOfFamilyTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicForEachElementOf(&g_stObjectTypes.hFamilyType,hHandleToObjectTypeElement,i)
 {
  if (!stricmp(szType,hHandleToObjectTypeElement->szName))
   otType = i;
 }
 return(otType);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a family type name of a family type index
 *-----------------------------------------------------------------------------
 *  Input :         family type index
 *  Output :        family type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
char *fn_szFindFamilyTypeNameOfFamilyType(tdObjectType otType)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hFamilyType,hHandleToObjectTypeElement,(unsigned long)otType,i);
 
 
 if (hHandleToObjectTypeElement)
  return(hHandleToObjectTypeElement->szName);
 else
  return(NULL);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Change a family type name of a family type index
 *-----------------------------------------------------------------------------
 *  Input :         family type index
 *  Output :        family type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
#if defined(ACTIVE_EDITOR)
void fn_vChangeFamilyTypeNameOfFamilyType(tdObjectType _otType,char *_szNewName)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hFamilyType,hHandleToObjectTypeElement,(unsigned long)_otType,i);
 
 if (hHandleToObjectTypeElement->szName!=NULL)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  M_GameFreeInHLM(hHandleToObjectTypeElement->szName);
 }
 
 MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
 hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(_szNewName)+1);
 strcpy(hHandleToObjectTypeElement->szName,_szNewName);
}
#endif /* ACTIVE_EDITOR */
 
/*-----------------------------------------------------------------------------
 *  Description :   Find or add a model type index of a model type name
 *-----------------------------------------------------------------------------
 *  Input :         model type name
 *  Output :        model type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdObjectType fn_otFindOrAddModelTypeOfModelTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 
 otType=fn_otFindModelTypeOfModelTypeName(szType);
 
 if (otType==-1)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement = M_p_GameMallocInHLM(sizeof(struct tdstObjectTypeElement_));
  LST2_M_DynamicInitElement(hHandleToObjectTypeElement);
  LST2_M_DynamicAddTail(&g_stObjectTypes.hModelType,hHandleToObjectTypeElement);
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(szType)+1);
  strcpy(hHandleToObjectTypeElement->szName,szType);
  otType=fn_otFindModelTypeOfModelTypeName(szType);
  hHandleToObjectTypeElement->ucElementPriority = GAM_g_ucHLMMemoryBlock;
 }
 return(otType);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a model type index of a model type name
 *-----------------------------------------------------------------------------
 *  Input :         model type name
 *  Output :        model type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
tdObjectType fn_otFindModelTypeOfModelTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicForEachElementOf(&g_stObjectTypes.hModelType,hHandleToObjectTypeElement,i)
 {
  if (!stricmp(szType,hHandleToObjectTypeElement->szName))
   otType = i;
 }
 return(otType);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a model type name of a model type index
 *-----------------------------------------------------------------------------
 *  Input :         model type index
 *  Output :        model type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
char *fn_szFindModelTypeNameOfModelType(tdObjectType otType)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hModelType,hHandleToObjectTypeElement,(unsigned long)otType,i);
 
 if (hHandleToObjectTypeElement)
  return(hHandleToObjectTypeElement->szName);
 else
  return(NULL);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Change a Model type name of a family type index
 *-----------------------------------------------------------------------------
 *  Input :         model type index
 *  Output :        model type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
#if defined(ACTIVE_EDITOR)
void fn_vChangeModelTypeNameOfModelType(tdObjectType _otType,char *_szNewName)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hModelType,hHandleToObjectTypeElement,(unsigned long)_otType,i);
 
 if (hHandleToObjectTypeElement->szName!=NULL)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  M_GameFreeInHLM(hHandleToObjectTypeElement->szName);
 }
 
 MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
 hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(_szNewName)+1);
 strcpy(hHandleToObjectTypeElement->szName,_szNewName);
}
#endif /* ACTIVE_EDITOR */
 
/*-----------------------------------------------------------------------------
 *  Description :   Find or add a personal type index of a personal type name
 *-----------------------------------------------------------------------------
 *  Input :         personal type name
 *  Output :        personal type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
tdObjectType fn_otFindOrAddPersonalTypeOfPersonalTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 
 otType=fn_otFindPersonalTypeOfPersonalTypeName(szType);
 
 if (otType==-1)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement = M_p_GameMallocInHLM(sizeof(struct tdstObjectTypeElement_));
  LST2_M_DynamicInitElement(hHandleToObjectTypeElement);
  LST2_M_DynamicAddTail(&g_stObjectTypes.hPersonalType,hHandleToObjectTypeElement);
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(szType)+1);
  strcpy(hHandleToObjectTypeElement->szName,szType);
  otType=fn_otFindPersonalTypeOfPersonalTypeName(szType);
  hHandleToObjectTypeElement->ucElementPriority = GAM_g_ucHLMMemoryBlock;
 }
 return(otType);
}
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a personal type index of a personal type name
 *-----------------------------------------------------------------------------
 *  Input :         personal type name
 *  Output :        personal type (-1 if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
tdObjectType fn_otFindPersonalTypeOfPersonalTypeName(char *szType)
{
 tdObjectType otType = -1;
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicForEachElementOf(&g_stObjectTypes.hPersonalType,hHandleToObjectTypeElement,i)
 {
  if (!stricmp(szType,hHandleToObjectTypeElement->szName))
   otType = i;
 }
 return(otType);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a personal type name of a personal type index
 *-----------------------------------------------------------------------------
 *  Input :         personal type index
 *  Output :        personal type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
char *fn_szFindPersonalTypeNameOfPersonalType(tdObjectType otType)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hPersonalType,hHandleToObjectTypeElement,(unsigned long)otType,i);
 
 if (hHandleToObjectTypeElement)
  return(hHandleToObjectTypeElement->szName);
 else
  return(NULL);
}
 
#ifndef _FIRE_DEADCODE_U64_
/*-----------------------------------------------------------------------------
 *  Description :   Change a personal type name of a family type index
 *-----------------------------------------------------------------------------
 *  Input :         personal type index
 *  Output :        personal type name (NULL if bad)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vChangePersonalTypeNameOfPersonalType(tdObjectType _otType,char *_szNewName)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicGetElementNumber(&g_stObjectTypes.hPersonalType,hHandleToObjectTypeElement,(unsigned long)_otType,i);
 
 if (hHandleToObjectTypeElement->szName!=NULL)
 {
  MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
  M_GameFreeInHLM(hHandleToObjectTypeElement->szName);
 }
 
 MMG_fn_vAddMemoryInfo( MMG_C_lTypeGAM , MMG_C_lSubTypeObjectTypeElement , 0 );
 hHandleToObjectTypeElement->szName = M_p_GameMallocInHLM(strlen(_szNewName)+1);
 strcpy(hHandleToObjectTypeElement->szName,_szNewName);
}
/*XB980507*/
#endif /* _FIRE_DEADCODE_U64_ */
/*End XB*/
 
/*-----------------------------------------------------------------------------
 *  Description :   Destroy all level elements
 *-----------------------------------------------------------------------------
 *  Input :         void
 *  Output :        void
 *-----------------------------------------------------------------------------
 *  Creation date : 05/09/97            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
void fn_vFreeAllObjectTypeForPriority(unsigned char ucPriority)
{
 tdhHandleToObjectTypeElement hHandleToObjectTypeElement,hHandleToNextObjectTypeElement;
 unsigned long i;
 
 LST2_M_DynamicForEachMovingElementOf(&g_stObjectTypes.hFamilyType,hHandleToObjectTypeElement,hHandleToNextObjectTypeElement,i)
 {
  if (hHandleToObjectTypeElement->ucElementPriority==ucPriority)
   LST2_M_DynamicIsolate(hHandleToObjectTypeElement);
 }
 LST2_M_DynamicForEachMovingElementOf(&g_stObjectTypes.hModelType,hHandleToObjectTypeElement,hHandleToNextObjectTypeElement,i)
 {
  if (hHandleToObjectTypeElement->ucElementPriority==ucPriority)
   LST2_M_DynamicIsolate(hHandleToObjectTypeElement);
 }
 LST2_M_DynamicForEachMovingElementOf(&g_stObjectTypes.hPersonalType,hHandleToObjectTypeElement,hHandleToNextObjectTypeElement,i)
 {
  if (hHandleToObjectTypeElement->ucElementPriority==ucPriority)
   LST2_M_DynamicIsolate(hHandleToObjectTypeElement);
 }
}
/*XB980507*/
/*#endif /* _FIRE_DEADCODE_U64_   */
/*End XB*/
 
/*-----------------------------------------------------------------------------
 *  Description :   Find a object by its personal type name (in the hierarchie)
 *-----------------------------------------------------------------------------
 *  Input :         personal type name, first Super-Object
 *  Output :        object's pointer (NULL if not found)
 *-----------------------------------------------------------------------------
 *  Creation date : 16/10/96            Author : Francois
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author :
 *  Modifications :
 *---------------------------------------------------------------------------*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
struct tdstEngineObject_ *fn_p_stFindObjectByItsPersonalTypeName(char *szPersonalType,HIE_tdxHandleToSuperObject p_stSuperObject)
{
 tdstEngineObject *p_stEngineObject = NULL;
 tdObjectType otPersonalType;
 
 otPersonalType = fn_otFindPersonalTypeOfPersonalTypeName(szPersonalType);
 if (otPersonalType!=-1)
  p_stEngineObject = fn_p_stFindObjectByItsPersonalType(otPersonalType,p_stSuperObject);
 
 return(p_stEngineObject);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
