#include "AIUseCPA.h"
 
#include "specif/AIOption.h"
#include "AIMacros.h"
#include "AI_Erm.h"
 
#include "AI_Struc.h"
#include "StrIntel.h"
 
#if !defined(OPTIMIZED_COMMAND)
#include "EnumFild.h"
#include "Convert.h"
#endif
 
#include "specif/AITools.h"
 
/*************************************/
/* get set field default functions : */
/*************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned char fn_ucGetFieldDefaultFunction(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
    hSuperObjPerso=hSuperObjPerso;
    p_stGetSetParam=p_stGetSetParam;
    return(C_INVALID_GET);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
unsigned char fn_ucSetFieldDefaultFunction(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
    hSuperObjPerso=hSuperObjPerso;
    p_stGetSetParam=p_stGetSetParam;
    return(C_INVALID_SET);
}
 
/***************************************************************************/
/* get set field functions : à completer en fonction de la structure perso */
/***************************************************************************/
 
unsigned char fn_ucGetFieldPrevComportIntell(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	tdstComport *p_stComport;
 
	p_stComport = M_GetPrevComport(AI_M_p_stGetIntelligence(AI_M_stGetMindOfSuperObj(hSuperObjPerso)));
	M_Full_GetSetParam_pComport(p_stGetSetParam, p_stComport);
    return(C_VALID_GET);
}
 
unsigned char fn_ucGetFieldPrevComportReflex(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	tdstComport *p_stComport;
 
	p_stComport = M_GetPrevComport(AI_M_p_stGetReflex(AI_M_stGetMindOfSuperObj(hSuperObjPerso)));
	M_Full_GetSetParam_pComport(p_stGetSetParam, p_stComport);
    return(C_VALID_GET);
}
 
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Field for Shadow Scale.*/
/*/////////////////////////////////////////////////////////////////////////////////*/
unsigned char fn_ucGetFieldShadowScaleX(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	M_Full_GetSetParam_Float(p_stGetSetParam, fn_x3dDataGetShadowScaleXFromSO(hSuperObjPerso));
 
    return(C_VALID_GET);
}
 
unsigned char fn_ucSetFieldShadowScaleX(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	fn_v3dDataSetShadowScaleXInSO(hSuperObjPerso, M_GetSetParam_xValue(p_stGetSetParam));
 
    return(C_VALID_SET);
}
 
unsigned char fn_ucGetFieldShadowScaleY(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	M_Full_GetSetParam_Float(p_stGetSetParam, fn_x3dDataGetShadowScaleYFromSO(hSuperObjPerso));
 
    return(C_VALID_GET);
}
 
unsigned char fn_ucSetFieldShadowScaleY(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	fn_v3dDataSetShadowScaleYInSO(hSuperObjPerso, M_GetSetParam_xValue(p_stGetSetParam));
 
    return(C_VALID_SET);
}
 
/* eField_Position*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_cSetFieldPosition*/
/*		set the value of the field "Position"*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan.15, 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned char fn_ucSetFieldPosition(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	hSuperObjPerso = hSuperObjPerso;
	p_stGetSetParam = p_stGetSetParam;
    return(C_INVALID_SET);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*=====================================================================================================================================
 Name:		fn_cGetFieldPosition( Perso, WP )
 Author:	yann le tensorer
 Created:	October 12,1998 (rewriten for optimisations)
 Last modified:
=====================================================================================================================================*/
unsigned char fn_ucGetFieldPosition(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	/* we just return the position*/
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,&(hSuperObjPerso->hGlobalMatrix->stTranslationVector));
	return(C_VALID_GET);
 
 
 
/*
    //GEO_tdxHandleToMatrix hTempMatrix;
    MTH3D_tdstVector stVertex;
 
    // get the vectex of the super object
    //hTempMatrix= HIE_fn_hGetSuperObjectMatrix(hSuperObjPerso);
    POS_fn_vGetTranslationVector(HIE_fn_hGetSuperObjectMatrix(hSuperObjPerso), &stVertex);
 
    // and affect return value
    //MTH3D_M_vCopyVector(&M_GetSetParam_stVertexValue(p_stGetSetParam),&stVertex);
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,&stVertex);
*/
    
}
 
/* eField_Orientation*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_cSetFieldOrientation*/
/*		Set the value of the field "Orientation"*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan.28, 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
unsigned char fn_ucSetFieldOrientation(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
  /*  GEO_tdxHandleToMatrix hPersoMatrix;
    MTH3D_tdstVector *p_stOrientationVertex;
    MTH_tdxReal  xAngle,xRealX,xRealY;
 
    // get the new position
    p_stOrientationVertex= &M_GetSetParam_stVertexValue(p_stGetSetParam);
 
    // compute the angle
    xRealX=p_stOrientationVertex->xX;
    xRealY=p_stOrientationVertex->xY;
 
    if(xRealX)
        xAngle=MTH_M_xATan(xRealY / xRealX);
    else if(MTH_M_bLess(xRealY ,MTH_C_ZERO))
        xAngle=MTH_M_xNeg(MTH_C_PiBy2);
    else
        xAngle=MTH_C_PiBy2;
    if(MTH_M_bLess(xRealX ,MTH_C_ZERO))
        xAngle=MTH_M_xAdd(xAngle,MTH_C_Pi);
    if(MTH_M_bLess(xAngle,MTH_C_ZERO))
        xAngle=MTH_M_xAdd(xAngle,MTH_C_2Pi);
 
    // get the matrix of the super object
    hPersoMatrix= HIE_fn_hGetSuperObjectMatrix(hSuperObjPerso);
    // affect the matrix (Z-axis rotation)
    fn_vSetRotationMatrixZ(hPersoMatrix, xAngle);
    return(C_VALID_SET);
 
*/
    hSuperObjPerso=hSuperObjPerso;
    p_stGetSetParam=p_stGetSetParam;
    return(C_INVALID_SET);
 
}
 
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_cGetFieldOrientation*/
/*		Get the value of the field "Orientation"*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan.28, 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
unsigned char fn_ucGetFieldOrientation(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
 
	MTH3D_tdstVector stVertexDirection, stVertex;
 
	/* Axe -Y in global reference*/
	MTH3D_M_vSetVectorElements(&stVertexDirection, MTH_C_ZERO, MTH_C_MinusONE, MTH_C_ZERO);
	POS_fn_vMulMatrixVertex(&stVertexDirection,HIE_fn_hGetSuperObjectGlobalMatrix(hSuperObjPerso),&stVertexDirection);
	/* position of perso*/
	POS_fn_vGetTranslationVector(HIE_fn_hGetSuperObjectGlobalMatrix(hSuperObjPerso),&stVertex);
	/* (stVertexDirection - stVertex1) -> stVertexDirection*/
	MTH3D_M_vSubVector(&stVertexDirection, &stVertexDirection, &stVertex);
	MTH3D_M_vNormalizeVector(&stVertexDirection,&stVertexDirection);
 
    /* and affect return value*/
    /*MTH3D_M_vCopyVector(&M_GetSetParam_stVertexValue(p_stGetSetParam),&stVertexDirection);*/
	/*M_GetSetParam_Type(p_stGetSetParam) = E_vt_Vector;*/
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,&stVertexDirection);
    return(C_VALID_GET);
}
 
 
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_cGetFieldSpeed*/
/*		Get the value of the field "Speed"*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Methods:	Get the rotation matrix of the object*/
/*			and compute the direction of the perso*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan.28, 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
unsigned char fn_ucGetFieldSpeed(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MS_tdxHandleToDynam h_Dynam;
 
	MTH3D_tdstVector *p_stDest = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam);
 
	M_GetSetParam_Type(p_stGetSetParam) = E_vt_Vector;
	if ( h_Dynam )
	{
		struct DNM_stDynamics *p_stDynamic;
	  p_stDynamic = fn_p_stDynamGetDynamics(h_Dynam);
		if ( p_stDynamic )
		{
			/*MTH_tdxReal xReal;*/
			/*MTH3D_tdstVector *p_stCurrSpeed;*/
 
			*p_stDest = *DNM_M_pDynamicsGetPreviousSpeed (p_stDynamic);
 
			/*p_stCurrSpeed = DNM_M_pDynamicsGetPreviousSpeed (pstDynamic);
 
			xReal=MTH3D_M_xGetXofVector(p_stCurrSpeed);
				MTH3D_M_vSetXofVector(p_stDest,xReal);
 
			xReal=MTH3D_M_xGetYofVector(p_stCurrSpeed);
				MTH3D_M_vSetYofVector(p_stDest,xReal);
 
			xReal=MTH3D_M_xGetZofVector(p_stCurrSpeed);
				MTH3D_M_vSetZofVector(p_stDest,xReal);*/
			return(C_VALID_GET);
		}
	}
	MTH3D_M_vNullVector(p_stDest);
//	return(C_INVALID_GET);
	return(C_VALID_GET);
}
 
unsigned char fn_ucGetFieldNormSpeed(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MS_tdxHandleToDynam h_Dynam;
 
	h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam);
 
	M_GetSetParam_Type(p_stGetSetParam) = E_vt_Float;
 
	if (h_Dynam!=NULL)
	{
		struct DNM_stDynamics* pstDynamic;
	  pstDynamic = fn_p_stDynamGetDynamics(h_Dynam);
		if ( pstDynamic )
		{
			MTH3D_tdstVector *p_stCurrSpeed = DNM_M_pDynamicsGetPreviousSpeed (pstDynamic);
			M_GetSetParam_xValue(p_stGetSetParam) = MTH3D_M_xNormVector(p_stCurrSpeed);
			return(C_VALID_GET);
		}
	}
 
	M_GetSetParam_xValue(p_stGetSetParam) = MTH_C_ZERO;
//	return(C_INVALID_GET);
	return(C_VALID_GET);
}
 
/*
unsigned char fn_ucGetFieldSightAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V1,*p_V2;
	MS_tdxHandleToDynam h_Dynam;
 
	M_GetSetParam_Type(p_stGetSetParam) = E_vt_Vector;
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_GET);
 
	p_V1 = fn_p_DynamGetSightAxis(h_Dynam);
    p_V2 = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	MTH3D_M_vCopyVector(p_V2,p_V1);
    return(C_VALID_SET);
	return(C_INVALID_GET);
}
 
unsigned char fn_ucGetFieldFirstCompAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V1,*p_V2;
	MS_tdxHandleToDynam h_Dynam;
 
	M_GetSetParam_Type(p_stGetSetParam) = E_vt_Vector;
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_GET);
 
	p_V1 = fn_p_DynamGetFirstComplementaryAxis(h_Dynam);
    p_V2 = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	MTH3D_M_vCopyVector(p_V2,p_V1);
    return(C_VALID_GET);
	return(C_INVALID_GET);
}
 
unsigned char fn_ucGetFieldSecondCompAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V1,*p_V2;
	MS_tdxHandleToDynam h_Dynam;
 
	M_GetSetParam_Type(p_stGetSetParam) = E_vt_Vector;
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_GET);
 
	p_V1 = fn_p_DynamGetSecondComplementaryAxis(h_Dynam);
    p_V2 = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	MTH3D_M_vCopyVector(p_V2,p_V1);
    return(C_VALID_GET);
	return(C_INVALID_GET);
}
 
unsigned char fn_ucSetFieldSightAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V;
	MS_tdxHandleToDynam h_Dynam;
 
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_SET);
 
    p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	fn_vDynamSetSightAxis(h_Dynam,p_V);
    return(C_VALID_SET);
	return(C_INVALID_SET);
}
 
unsigned char fn_ucSetFieldFirstCompAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V;
	MS_tdxHandleToDynam h_Dynam;
 
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_SET);
 
    p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	fn_vDynamSetFirstComplementaryAxis(h_Dynam,p_V);
    return(C_VALID_SET);
	return(C_INVALID_SET);
}
 
unsigned char fn_ucSetFieldSecondCompAxis(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MTH3D_tdstVector *p_V;
	MS_tdxHandleToDynam h_Dynam;
 
	if((h_Dynam = M_GetMSHandle(hSuperObjPerso,Dynam)) == NULL )
		return(C_INVALID_SET);
 
    p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));
	fn_vDynamSetSecondComplementaryAxis(h_Dynam,p_V);
    return(C_VALID_SET);
	return(C_INVALID_SET);
}
*/
 
unsigned char fn_ucGetFieldAbsoluteAxisX(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	/*MTH3D_tdstVector *p_V;*/
 
	POS_tdstCompletePosition *p_stAbsolutePosition = NULL;
	MTH3D_tdstVector *p_stI = NULL,*p_stJ = NULL,*p_stK = NULL;
 
	p_stAbsolutePosition  =  HIE_fn_hGetSuperObjectGlobalMatrix(hSuperObjPerso);
	POS_fn_vGetRotationMatrix(p_stAbsolutePosition ,p_stI,p_stJ,p_stK);
    /*p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));*/
	/*MTH3D_M_vCopyVector(p_V,p_stI);*/
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,p_stI);
 
    return(C_VALID_GET);
}
 
unsigned char fn_ucGetFieldAbsoluteAxisY(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
/*	MTH3D_tdstVector *p_V;*/
 
	POS_tdstCompletePosition *p_stAbsoluteGliPosition = NULL;
	MTH3D_tdstVector *p_stI = NULL,*p_stJ = NULL,*p_stK = NULL;
 
	p_stAbsoluteGliPosition  =  HIE_fn_hGetSuperObjectGlobalMatrix(hSuperObjPerso);
	POS_fn_vGetRotationMatrix(p_stAbsoluteGliPosition ,p_stI,p_stJ,p_stK);
/*   p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));*/
/*	MTH3D_M_vCopyVector(p_V,p_stJ);*/
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,p_stI);
    return(C_VALID_GET);
}
 
unsigned char fn_ucGetFieldAbsoluteAxisZ(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
/*	MTH3D_tdstVector *p_V;*/
 
	POS_tdstCompletePosition *p_stAbsoluteGliPosition = NULL;
	MTH3D_tdstVector *p_stI = NULL,*p_stJ = NULL,*p_stK = NULL;
 
	p_stAbsoluteGliPosition  =  HIE_fn_hGetSuperObjectGlobalMatrix(hSuperObjPerso);
	POS_fn_vGetRotationMatrix(p_stAbsoluteGliPosition ,p_stI,p_stJ,p_stK);
/*    p_V = &(M_GetSetParam_stVertexValue(p_stGetSetParam));*/
/*	MTH3D_M_vCopyVector(p_V,p_stK);*/
	M_Full_GetSetParam_p_stVertex(p_stGetSetParam,p_stI);
    return(C_VALID_GET);
}
/* JO 07/11/97*/
/* ANNECY AV {*/
/* These functions are not used anymore
unsigned char fn_ucSetFieldCollisionFlag(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MS_tdxHandleToCollSet hMSCollSet;
	unsigned char ucCollisionFlag;
 
	if((hMSCollSet = M_GetMSHandle(hSuperObjPerso,CollSet)) == NULL )
		return(C_INVALID_SET);
 
    ucCollisionFlag = M_GetSetParam_cValue(p_stGetSetParam);
	fn_vCollSetSetCharacterCollisionFlag(hMSCollSet,ucCollisionFlag);
    return(C_VALID_SET);
}
 
unsigned char fn_ucGetFieldCollisionFlag(HIE_tdxHandleToSuperObject hSuperObjPerso, tdstGetSetParam *p_stGetSetParam)
{
	MS_tdxHandleToCollSet hMSCollSet;
 
	// pas bon
//	if((hMSCollSet = M_GetMSHandle(hSuperObjPerso,CollSet)) == NULL )
//		return( C_INVALID_GET );
 
//	M_GetSetParam_xValue(p_stGetSetParam) = fn_ucCollSetGetCharacterCollisionFlag(hMSCollSet);
//    return(C_VALID_GET);
 
	long lValue=0;
	char ret=C_INVALID_GET;
	if((hMSCollSet = M_GetMSHandle(hSuperObjPerso,CollSet)) != NULL )
	{
		lValue=(long) fn_ucCollSetGetCharacterCollisionFlag(hMSCollSet);
		ret=C_VALID_GET;
	}
	M_Full_GetSetParam_Integer(p_stGetSetParam,lValue);
	return(ret);
}
*/
/* END ANNECY AV }*/
 
/***********************************************************************************************/
#if !defined(OPTIMIZED_COMMAND)
void fn_vInitFieldEntries(void)
{
/* Init*/
#if defined(__DEBUG_AI__)
	fn_vInitFieldTable(fn_ucGetFieldDefaultFunction,fn_ucSetFieldDefaultFunction);
#endif
 
/* Field definitions*/
    #define M_DEFINE_FIELD(a,b,english,c,d,e,f)  fn_vDefineFieldEntry(M_FIELD_ENTRY(a,b,english,c,d,e,f));
    #include "DefFild.h"
	#undef M_DEFINE_FIELD
 
#if defined(__DEBUG_AI__)
	fn_vCheckFieldTable(fn_ucGetFieldDefaultFunction,fn_ucSetFieldDefaultFunction);
#endif
 
}
#endif /* OPTIMIZED_COMMAND*/
