/*ANNECY CT 02/02/98{*/
/*-----------------------------------------------------------------------------
 *  Description :   Compute Surface Speed of the material collided (for scrolling texture)
 *-----------------------------------------------------------------------------
 *  Input :         a pointer on the SO
 *					vector to return speed
 *					type of surface to search speed
 *-----------------------------------------------------------------------------
 *  Creation date : 02/02/98            Author : Carlos Torres
 *---------------------------------------------------------------------------*/
void fn_vGetScrollSpeedMatCollided(HIE_tdxHandleToSuperObject p_stSuperObj,MTH3D_tdstVector * p_stVector,unsigned long ulObstacleType) 
{
	DNM_tdstReport * p_stDynamReport;
	/*unsigned long ulSurfaceState;*/
	MS_tdxHandleToDynam h_Dynam;
	DNM_tdstDynamics *p_stDynamics;
	DNM_tdstObstacle * p_stObstacle ;
	long bScrollMode;
	MTH_tdxReal xUSpeed,xVSpeed;
	GMT_tdxHandleToGameMaterial hGmt;
	ACP_tdxHandleOfMaterial hVisualMaterial;
		
	/* set return vector to null*/
	MTH3D_M_vNullVector(p_stVector);
 
	/* get the dymanic report, if exist*/
	if((h_Dynam = M_GetMSHandle(p_stSuperObj,Dynam)) == NULL ) 
		return;
	
	if((p_stDynamics = fn_p_stDynamGetDynamics(h_Dynam)) == NULL)
		return;
	
	p_stDynamReport = DNM_M_p_stDynamicsGetReport(p_stDynamics);
	if (!p_stDynamReport) 
		return;
 
	/* check if there is collide with a character*/
	/* and update last collided character */
	/*ulSurfaceState = DNM_M_ulReportGetSurfaceState(p_stDynamReport);
	if (ulSurfaceState & ulObstacleType)
	{*/
 
		switch ( ulObstacleType )
		{
		default: /*get the last collided obstacle*/
			p_stObstacle = DNM_M_p_stReportGetObstacle(p_stDynamReport);
			break;
 
		case 1: /*get the last ground*/
			p_stObstacle = DNM_M_p_stReportGetGround(p_stDynamReport);
			break;
 
		case 2: /*get the last wall*/
			p_stObstacle = DNM_M_p_stReportGetWall(p_stDynamReport);
			break;
 
		case 3: /*get the last character*/
			p_stObstacle = DNM_M_p_stReportGetCharacter(p_stDynamReport);
			break;
 
		case 4: /*get the last uncollidable/water plane*/
			p_stObstacle = DNM_M_p_stReportGetWater(p_stDynamReport);
			break;
 
		case 5: /* get the last ceiling*/
			p_stObstacle = DNM_M_p_stReportGetCeil(p_stDynamReport);
			break;
		}
		/* get obstacle*/
		/*if (ulObstacleType & C_WOT_ulGround)
			p_stObstacle = DNM_M_p_stReportGetGround(p_stDynamReport);
		else
			p_stObstacle = DNM_M_p_stReportGetWall(p_stDynamReport);*/
		/* get scroll mode & scroll speed*/
		hGmt = DNM_M_hObstacleGetCollidedMaterial(p_stObstacle);
		if ( !GMT_fn_b_ulIsValid(hGmt) )
			return;
 
		/*AR9901*/
		hVisualMaterial=GMT_fn_hGetVisualMaterial(hGmt);
		if(hVisualMaterial==NULL)
			return;
 
		GLI_xGetMaterialTextureScrollingCoef(hVisualMaterial,0,&bScrollMode,&xUSpeed,&xVSpeed);
		/* test if obstacle is scrolling*/
		if ( bScrollMode ) 
		{
			/* get obstacle super object*/
			HIE_tdxHandleToSuperObject p_stObjectCollided = DNM_M_p_stObstacleGetObject(p_stObstacle);
/*XB980821*/
#ifndef D_THROW_IPO
			IPO_tdxHandleToInstanciatedPhysicalObject p_stInstanciatedPhysicalObject;
#endif /* D_THROW_IPO */
/*End XB*/
			PO_tdxHandleToPhysicalObject p_stPhysicalObject;
#ifndef D_THROW_VISUAL_SET
			GEO_tdxHandleToVisualSet hVisualSet;
			MTH_tdxReal xThreshold;
#endif /* D_THROW_VISUAL_SET */
			ACP_tdxHandleOfObject hObject;
 
/*XB980821*/
#ifndef D_THROW_IPO
			/* get the IPO*/
			p_stInstanciatedPhysicalObject=(IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(p_stObjectCollided);
			/* get PO*/
			p_stPhysicalObject=IPO_fn_hGetPhysicalObject(p_stInstanciatedPhysicalObject);
#else
			/* get PO*/
			p_stPhysicalObject=(PO_tdxHandleToPhysicalObject)HIE_fn_hGetSuperObjectObject(p_stObjectCollided);
#endif /* D_THROW_IPO */
/*End XB*/
#ifndef D_THROW_VISUAL_SET
			/* get visual set*/
			hVisualSet=PO_fn_hGetVisualSet(p_stPhysicalObject);
			/* get LOD*/
			GLI_vGetVisualSetLOD(hVisualSet,0,&xThreshold,&hObject);
#else
			hObject=PO_fn_hGetGeometricObject(p_stPhysicalObject);
#endif /* D_THROW_VISUAL_SET */
			/* for indexed triangle*/
			{
				MTH3D_tdstVector vXYZSpeed,vGlobalSpeed;
				POS_tdstCompletePosition mGlobalToPerso;
				struct GLI_tdstDataOfElement_ stElement;
				ACP_tdxIndex xNbFaceCollided;
				MTH3D_tdstVector vContact,vNorm,vPt1,vPt2,vV12;
				POS_tdstCompletePosition mGlobalToObst;
 
				/* set matrix global to local obstacle*/
				POS_fn_vInvertMatrix(&mGlobalToObst,HIE_fn_hGetSuperObjectGlobalMatrix(p_stObjectCollided));
				/* change to local obstacle vertex*/
				POS_fn_vMulMatrixVertex(&vContact,&mGlobalToObst,DNM_M_p_stObstacleGetContact(p_stObstacle));
				POS_fn_vMulMatrixVector(&vNorm,&mGlobalToObst,DNM_M_p_stObstacleGetNorm(p_stObstacle));
				/* init Pt 1&2*/
				MTH3D_M_vCopyVector(&vPt1,&vContact);
				MTH3D_M_vCopyVector(&vPt2,&vContact);
				/* build segment*/
				MTH3D_M_vAddVector(&vPt1,&vPt1,&vNorm);
				MTH3D_M_vSubVector(&vPt2,&vPt2,&vNorm);
				MTH3D_M_vSubVector(&vV12,&vPt2,&vPt1);
				/* found collided face*/
				INT_fn_bIntersectSegmentWithFaceOfGeometricObject(
					&vPt1,&vPt2,&vV12,hObject,1,&xNbFaceCollided,&stElement);
				/* probleme don't found face collided*/
				if ( !xNbFaceCollided )
					return;
 
				/*GEO_vGetFaceSurfaceSpeed(hObject,&vPt1,&vPt2,&vV12,&vXYZSpeed);*/
				GEO_vGetFaceSurfaceSpeed(hObject,stElement.xElements,stElement.xIndexOfFace,&vXYZSpeed);
 
				/* change to global vertex*/
				POS_fn_vMulMatrixVector(&vGlobalSpeed,HIE_fn_hGetSuperObjectGlobalMatrix(p_stObjectCollided),&vXYZSpeed);
				/* set matrix global to local perso*/
				POS_fn_vInvertIsoMatrix(&mGlobalToPerso,HIE_fn_hGetSuperObjectGlobalMatrix(p_stSuperObj));
				/* change to local perso vertex*/
				POS_fn_vMulMatrixVector(p_stVector,&mGlobalToPerso,&vGlobalSpeed);
			}
		}
	/*}*/
}
/*ENDANNECY CT}*/
 
/*ANNECY CT 18/02/98{*/
/*-----------------------------------------------------------------------------
 *  Description : Set last collided Character
 *-----------------------------------------------------------------------------
 *  Input :         a pointer on the SO
 *					new character collided
 *-----------------------------------------------------------------------------
 *  Creation date : 18/02/98            Author : Carlos Torres
 *---------------------------------------------------------------------------*/
void fn_vSuperObjectSetLastCollidedCharacter(HIE_tdxHandleToSuperObject hCharacter,HIE_tdxHandleToSuperObject _h_LastCollidedCharacter) {
	DNM_tdstReport * p_stDynamReport;
	MS_tdxHandleToDynam h_Dynam;
	DNM_tdstDynamics *p_stDynamics;
	
	/* get the dymanic report, if exist*/
	if((h_Dynam = M_GetMSHandle(hCharacter,Dynam)) == NULL ) 
		return;
	
	if((p_stDynamics = fn_p_stDynamGetDynamics(h_Dynam)) == NULL)
		return;
	
	p_stDynamReport = DNM_M_p_stDynamicsGetReport(p_stDynamics);
	if (!p_stDynamReport) 
		return;
 
	DNM_M_p_stObstacleSetObject(DNM_M_p_stReportGetCharacter(p_stDynamReport),_h_LastCollidedCharacter);
 
	/* clear character flag if _h_LastCollidedCharacter==NULL*/
	if (!_h_LastCollidedCharacter)
		DNM_M_ulReportSetSurfaceState (p_stDynamReport, (DNM_M_ulReportGetSurfaceState(p_stDynamReport) & ~C_WOT_ulMobile));
}
 
/*-----------------------------------------------------------------------------
 *  Description : Get last collided Character
 *-----------------------------------------------------------------------------
 *  Input :         a pointer on the SO
 *	Output :        character collided
 *-----------------------------------------------------------------------------
 *  Creation date : 18/02/98            Author : Carlos Torres
 *---------------------------------------------------------------------------*/
HIE_tdxHandleToSuperObject fn_hSuperObjectGetLastCollidedCharacter(HIE_tdxHandleToSuperObject hCharacter) {
	DNM_tdstReport * p_stDynamReport;
	MS_tdxHandleToDynam h_Dynam;
	DNM_tdstDynamics *p_stDynamics;
	HIE_tdxHandleToSuperObject p_stObjectCollided;
	
	/* get the dymanic report, if exist*/
	if((h_Dynam = M_GetMSHandle(hCharacter,Dynam)) == NULL ) 
		return NULL;
	
	if((p_stDynamics = fn_p_stDynamGetDynamics(h_Dynam)) == NULL)
		return NULL;
	
	p_stDynamReport = DNM_M_p_stDynamicsGetReport(p_stDynamics);
	if (!p_stDynamReport) 
		return NULL;
 
	/* get character collided in report*/
	p_stObjectCollided=DNM_M_p_stObstacleGetObject(DNM_M_p_stReportGetCharacter(p_stDynamReport));
 
	/* search the SO of type charcter*/
	while (p_stObjectCollided && (p_stObjectCollided->ulTypeOfLinkedObject != HIE_C_ulActor)) {
		/* go up in the hierarchy*/
/*		p_stObjectCollided = DNM_p_stGetFather(p_stObjectCollided);*/
		p_stObjectCollided = HIE_fn_hGetSuperObjectFather(p_stObjectCollided);
	}
 
	/* for always test if existing yet*/
	if (p_stObjectCollided && !M_GetMSHandle(p_stObjectCollided,StandardGame)) {
		p_stObjectCollided = NULL;
		DNM_M_p_stObstacleSetObject(DNM_M_p_stReportGetCharacter(p_stDynamReport),NULL);
	}
 
	return p_stObjectCollided;
}
/*ENDANNECY CT}*/
