/*=========================================================================
* LoadAni.c : Loading Animations Files
* This is a part of the Game project.
*
* Version                      1.0
* Creation date        18/10/96
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1996
*=======================================================================*/
 
#define  D_ObjsTbls_Define
 
#include "ToolsCPA.h"
 
#include "Main.h"
 
#include "Options/Def_Snd.h"
 
#include "Options/Options.h"
#include "Macros.h"
 
#include "Structur/Anim_s.h"
#include "Structur/ErrGame.h"
#include "Structur/GameScpt.h"
#include "Structur/MemGame.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
#include "Structur/3DOSLkTb.h"
 
/* For interpolated animations*/
#include "ChanList.h"
#include "PlayAnim/Interpol/a3x_intn.h"
#include "PlayAnim/playevts.h"
 
#include "Family.h"
#include "ObjType.h"
#include "Basic.h"
#include "LoadAni.h"
#include "playanim/playanim.h"
#include "Macros.h"
#include "ZeMem.h"
#include "Physicol.h"
#include "PO.h"
#include "Structur/ObjsTbls.h"
#include "CHLLoad.h"
 
 
/* current animation file version*/
#define C_AnimationFileVersion		6
 
unsigned short g_uwCurrentFrame;		/* MR2307*/
BOOL g_abActiveStatus[255];			/* MR2307*/
tdstAnim3d *g_p_stCurrentAnim;			/* MR2908*/
 
ACP_tdxBool GAM_g_bIsFirstAnim=TRUE;
 
 /*-----------------------------------------------------------------------------
 *  Description : initialize link table that will contain all link between
 *    animation name and pointer to animation
 *-----------------------------------------------------------------------------
 *  Input :  none
 *  Output : void           
 *-----------------------------------------------------------------------------
 *  Creation date : 25/02/97            Author : VL
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
void fn_vInitAnimationLinkTable( void )
{
 SCR_fn_v_Link_InitTable(&g_st3DOSLinkTable.stAnimation);
}
 
 /*-----------------------------------------------------------------------------
 *  Description : Close link table 
 *    animation name and pointer to animation
 *-----------------------------------------------------------------------------
 *  Input :  none
 *  Output : void           
 *-----------------------------------------------------------------------------
 *  Creation date : 29/05/97            Author : SD
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
void fn_vCloseAnimationLinkTable( void )
{
 SCR_fn_v_Link_CloseTable(&g_st3DOSLinkTable.stAnimation);
}
 
void fn_vDeleteAnimationLinkTable(unsigned char ucMin,unsigned char ucMax)
{
 SCR_fn_v_Link_DeleteEntryWithPriority(&g_st3DOSLinkTable.stAnimation,ucMin,ucMax);
}
 
/*-----------------------------------------------------------------------------
 *  Description : return Animation link table for use outside this module
 *-----------------------------------------------------------------------------
 *  Input :  none
 *  Output : SCR_tdst_Link_Table *           
 *-----------------------------------------------------------------------------
 *  Creation date : 25/02/97            Author : VL
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
SCR_tdst_Link_Table *fnp_stGetAnimationLinkTable( void )
{
 return (&g_st3DOSLinkTable.stAnimation);
}
 
 
/*-----------------------------------------------------------------------------
 *  Description :   Update the pointers into array of events of an animation
 *					Must be called after the load process of an A3i
 *					Sort Event to have Never Play at the beginning
 *-----------------------------------------------------------------------------
 *  Input :         _p_stAnim : The animation to process
 *					_h_ObjectsTablesListElement : The objects table containing
 *					the events
 *  Output :        
 *-----------------------------------------------------------------------------
 *  Creation date : Feb 98            Author : Al1
 *  Modif 08/10/98 : Sort Event - Carlos Torres
 *-----------------------------------------------------------------------------*/
void GAM_fn_vUpdateEventArrayOfAnimation(struct tdstAnim3d_* _p_stAnim, tdxHandleToObjectsTablesList _h_ObjectsTablesListElement) {
	int i;
	tdstEvent * p_stEvent, * p_stEventNeverPlay;
 
	/* set TBL Event & put Never Play Event at the beggining*/
	for(i=0,p_stEvent=NULL,p_stEventNeverPlay=_p_stAnim->d_stAnimEvent;i<_p_stAnim->ucNumberOfEvents;++i,p_stEventNeverPlay++) {
		/* set the tbl event*/
		p_stEventNeverPlay->p_stfEventInTBL=
			(tdstEventInTable*)_h_ObjectsTablesListElement->d_stObjectsTable[p_stEventNeverPlay->usEventNumberInTBL].h_Target;
 
/* blindage*/
#ifdef _DEBUG
		if (!p_stEventNeverPlay->p_stfEventInTBL) {
			char szMessage[50];
			sprintf(szMessage,"Problem with event (element %d in TBL) - try to rebuild A3i file",p_stEventNeverPlay->usEventNumberInTBL+1);
			Erm_M_UpdateLastError( Game, C_ucErmDefaultChannel, E_uwGameErrorWhileLoadingAnimation,
				C_lErmNoDebugData,C_ucErmOpenInfoWindow, C_ucAllowStopForDebug,szMessage);
		}
#endif 
		/* found first non never play event*/
		if (!p_stEvent && (GAM_fn_ucGetFirstCallOfEvent(p_stEventNeverPlay) != C_ucNeverPlay))
			p_stEvent = p_stEventNeverPlay;
		/* found a never play */
		else if (GAM_fn_ucGetFirstCallOfEvent(p_stEventNeverPlay) == C_ucNeverPlay) {
			/* set the frame & channel to 0*/
			p_stEventNeverPlay->uwChannelNumber = p_stEventNeverPlay->uwFrameNumber = 0;
 
			/* there is a non never play before*/
			if (p_stEvent) {
				tdstEvent * p_stTmpEvent;
				/* we move up the never play event until the first non never play*/
				for(p_stTmpEvent = p_stEventNeverPlay-1;p_stTmpEvent >= p_stEvent;p_stTmpEvent--) {
					/* permute Event*/
					tdstEvent stEvent = *(p_stTmpEvent+1);
					*(p_stTmpEvent+1) = *p_stTmpEvent;
					*p_stTmpEvent = stEvent;
				}
 
				/* now the fisrt non never play has moved 1 place down*/
				p_stEvent++;
			}
		}
	}
 
 
}
 
/*----------------------------------------------------------------------------
//  Description : fn_vAddAnimIntoA3bLinkTable
----------------------------------------------------------------------------//
//  Methods : None
----------------------------------------------------------------------------//
//  Input :	_p_stAnim : pointer to the animation loaded
//			_szAnimFileName : Name of the animation file
//  Output :  
----------------------------------------------------------------------------//
//  Creation date : Jun 97			  		  Author: Alain Robin
----------------------------------------------------------------------------//
//  Modifications :	
//  Modification date :				          Modification author : 
----------------------------------------------------------------------------*/
void fn_vAddAnimIntoA3bLinkTable(tdstAnim3d* _p_stAnim, char* _szAnimFileName)
{
	char szCompleteName[256];
	SCR_tdst_Link_Table* p_stAnimationLinkTable=NULL;
 
	p_stAnimationLinkTable=fnp_stGetAnimationLinkTable();
	strcpy(szCompleteName,_szAnimFileName);
	szCompleteName[strlen(szCompleteName)-1]='d';
	SCR_fnp_st_Link_SetValue(p_stAnimationLinkTable,szCompleteName,(unsigned long)_p_stAnim);
}
 
/*----------------------------------------------------------------------------
//  Description : fn_p_stGetAnimIntoA3bLinkTable
----------------------------------------------------------------------------//
//  Methods : None
----------------------------------------------------------------------------//
//  Input :	_szAnimFileName : Name of the animation file
//  Output : NULL if the anim isn't already loaded the pointer instead  
----------------------------------------------------------------------------//
//  Creation date : Jun 97			  		  Author: Alain Robin
----------------------------------------------------------------------------//
//  Modifications :	
//  Modification date :				          Modification author : 
----------------------------------------------------------------------------*/
tdstAnim3d* fn_p_stGetAnimIntoA3bLinkTable(char* _szAnimFileName)
{
	SCR_tdst_Link_Value *p_stLinkValue;
	SCR_tdst_Link_Table* p_stAnimationLinkTable=NULL;
	char szCompleteName[256];
 
	strcpy(szCompleteName,_szAnimFileName);
	szCompleteName[strlen(szCompleteName)-1]='d';
	p_stAnimationLinkTable=fnp_stGetAnimationLinkTable();
	p_stLinkValue=SCR_fnp_st_Link_SearchKey(p_stAnimationLinkTable,szCompleteName);
	if(p_stLinkValue!=NULL)
		return (tdstAnim3d*)(p_stLinkValue->ulValue);
	else
		return NULL;
}
 
 
