#include "AIUseCPA.h"
#include "Mth.h"
 
#include "specif/AIOption.h"
 
#include "AIMacros.h"
 
#include "AI_Mmg.h"
#include "AI_Erm.h"
 
/* in order to compile AI in C++ and link with GAM in C*/
#include "AI_Proto.h"
 
 
#define _WP_D_DEFINE_WAYPOINTS_
#include "WP_Handl.h"
/* in order to compile AI in C++ and link with GAM in C*/
#include "WP_Func.h"
#undef _WP_D_DEFINE_WAYPOINTS_
 
#define _WP_D_WAYPOINT_FRIEND_
#define WP_D_WAYPOINT_GLOBALS_
#include "WPWayPt.h"
#undef WP_D_WAYPOINT_GLOBALS_
#undef _WP_D_WAYPOINT_FRIEND_
 
#include "WPgraphe.h"


// FBF N64-format pour coords, et logfile dans x:\exe\geodump.log{
#ifdef U64CONVERTETLOG
#ifdef __cplusplus
extern "C"
{
#endif

extern float	fScale;
extern FILE	*dump;

#ifdef __cplusplus
}
#endif

#endif //U64CONVERTLOG
// } fin N64-format


/*
------------------------------------------------------------------------------------------
GLOBAL VARIABLES DECLARATION:
------------------------------------------------------------------------------------------
*/
#if defined(AI_USE_SCRIPT)
static SCR_tdst_Link_Table WP_gs_stWayPointLinkTable;
#endif /* AI_USE_SCRIPT */
 
#if defined(ACTIVE_EDITOR)
static unsigned long gs_Way_ulNbOfWarnings = 0;
static FILE *gs_fWpWarningsFile = NULL;
#endif
/*
------------------------------------------------------------------------------------------
FUNCTIONS AND MACROS-FUNCTIONS DECLARATION:
------------------------------------------------------------------------------------------
*/
#define WP_M_vWayPointMalloc(_Var,_Type,_Size) M_AIAlloc(_Var,_Type,_Size)
#define WP_M_vWayPointFree(_Var) M_AIFree(_Var)
#define WP_M_vWayPointMemset(_Var,_Char,_Size) M_AI_MemSet(_Var,_Char,_Size)
#define WP_M_vWayPointRealloc(_Var,_AdrSrc,_Type,_Size) M_AIRealloc(_Var,_AdrSrc,_Type,_Size)
 
#define WP_M_vWayPointAssert(_Expr,_Error) M_AI_DEBUG_ASSERT(_Expr,_Error)
 
#define WP_M_vWayPointFatalError(_Error) M_AIFatalError(_Error)
/* Bart:
#define WP_M_vWayPointWarningError(_Error) M_AIWarningError(_Error)
*/
/*ANNECY Bart 21/01/98 {*/
#define WP_M_vWayPointWarningError(_Error, _ucOpenInfoWindow) M_AIWarningError(_Error, _ucOpenInfoWindow)
/*ENDANNECY Bart }*/
#define WP_M_vWayPointIfErrorUpdateError(_Module,_NewError) M_AIIfErrorUpdateFatalError(_Module,_NewError)
 
WP_tdhWayPoint WP_fnh_WayPoint_Create(void)
{
 WP_tdhWayPoint hNewWayPoint=NULL;
 /* Allocate a new waypoint */
 WP_M_vWayPointMalloc(hNewWayPoint,WP_tdhWayPoint,WP_M_uwWayPointSizeOf());
 if(hNewWayPoint)
 {
  WP_M_vWayPointMemset(hNewWayPoint,0xa,WP_M_uwWayPointSizeOf());
  /*WP_M_vWayPointSetUsedCount(hNewWayPoint,0);*/
  WP_M_vWayPointSetSuperObject(hNewWayPoint,0);
  return hNewWayPoint;
 }
 else
  return WP_C_hWayPointInvalid;
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_Destroy
(WP_tdhWayPoint _hOldWayPoint)
{
 WP_M_vWayPointAssert(_hOldWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 WP_M_vWayPointFree(_hOldWayPoint);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
void WP_fnv_WayPoint_AddRef
(WP_tdhWayPoint _hWayPoint)
{
 _hWayPoint = _hWayPoint;
 /* WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);*/
 /* WP_M_vWayPointSetUsedCount(_hWayPoint,(unsigned char) (WP_M_ucWayPointGetUsedCount(_hWayPoint)+1));*/
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_Release
(WP_tdhWayPoint _hWayPoint)
{
 _hWayPoint = _hWayPoint;
 /*WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);*/
 /*WP_M_vWayPointSetUsedCount(_hWayPoint,(unsigned char) (WP_M_ucWayPointGetUsedCount(_hWayPoint)-1));*/
 /*if(!WP_M_ucWayPointGetUsedCount(_hWayPoint))*/
 /*{*/
 /* WP_fnv_WayPoint_Destroy(_hWayPoint);*/
 /*}*/
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
WP_tdhWayPoint WP_fnh_WayPoint_Build
(
 MTH3D_tdstVector*_pstVector,
 MTH_tdxReal _xRadius,
 HIE_tdxHandleToSuperObject _hSuperObject
)
{
 WP_tdhWayPoint hNewWayPoint;
 hNewWayPoint = WP_fnh_WayPoint_Create();
 if(hNewWayPoint!=WP_C_hWayPointInvalid)
 {
  WP_M_vWayPointSetVertex(hNewWayPoint,_pstVector);
  WP_M_vWayPointSetRadius(hNewWayPoint,_xRadius);
  WP_M_vWayPointSetSuperObject(hNewWayPoint,_hSuperObject);
  return hNewWayPoint;
 }
 else
  return WP_C_hWayPointInvalid;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
WP_tdhWayPoint WP_fnh_WayPoint_Duplicate
(WP_tdhWayPoint _hWayPointSrc)
{
 MTH3D_tdstVector stVertex;
 WP_M_vWayPointAssert(_hWayPointSrc!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 WP_M_vWayPointGetVertex(_hWayPointSrc,&stVertex);
 return WP_fnh_WayPoint_Build
 (
  &stVertex,
  WP_M_xWayPointGetRadius(_hWayPointSrc),
  WP_M_hWayPointGetSuperObject(_hWayPointSrc)
 );
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_Copy
(
 WP_tdhWayPoint _hWayPointDest,
 WP_tdhWayPoint _hWayPointSrc
)
{
 MTH3D_tdstVector stVertex;
 WP_M_vWayPointAssert
 (
    (_hWayPointDest!=WP_C_hWayPointInvalid)
  &&(_hWayPointSrc!=WP_C_hWayPointInvalid),
  E_uwAIFatalInvalidParameter
 );
 WP_M_vWayPointGetVertex(_hWayPointSrc,&stVertex);
 WP_M_vWayPointSetVertex(_hWayPointDest,&stVertex);
 WP_M_vWayPointSetRadius
 (
  _hWayPointDest,
  WP_M_xWayPointGetRadius(_hWayPointSrc)
 );
 WP_M_vWayPointSetSuperObject
 (
  _hWayPointDest,
  WP_M_hWayPointGetSuperObject(_hWayPointDest)
 );
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned short WP_fnuw_WayPoint_SizeOf(void)
{
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 return WP_M_uwWayPointSizeOf();
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
void WP_fnv_WayPoint_GetVertex
(
 WP_tdhWayPoint _hWayPoint,
 MTH3D_tdstVector*_pstVector
)
{
 WP_M_vWayPointAssert
 (
    (_hWayPoint!=WP_C_hWayPointInvalid)
  &&(_pstVector),
  E_uwAIFatalInvalidParameter
 );
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 WP_M_vWayPointGetVertex(_hWayPoint,_pstVector);
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_SetVertex
(
 WP_tdhWayPoint _hWayPoint,
 MTH3D_tdstVector*_pstNewVector
)
{
 WP_M_vWayPointAssert
 (
    (_hWayPoint!=WP_C_hWayPointInvalid)
  &&(_pstNewVector),
  E_uwAIFatalInvalidParameter
 );
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 WP_M_vWayPointSetVertex(_hWayPoint,_pstNewVector);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
MTH_tdxReal WP_fnx_WayPoint_GetRadius
(WP_tdhWayPoint _hWayPoint)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 return WP_M_xWayPointGetRadius(_hWayPoint);
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_SetRadius
(
 WP_tdhWayPoint _hWayPoint,
 MTH_tdxReal _xNewRadius
)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 WP_M_vWayPointSetRadius(_hWayPoint,_xNewRadius);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
HIE_tdxHandleToSuperObject WP_fnh_WayPoint_GetSuperObject
(WP_tdhWayPoint _hWayPoint)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 return WP_M_hWayPointGetSuperObject(_hWayPoint);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_SetSuperObject
(
 WP_tdhWayPoint _hWayPoint,
 HIE_tdxHandleToSuperObject _hNewSuperObject
)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 /*
 Using the macro makes it compulsory to define the function
 with the _WP_D_WAYPOINT_FRIEND_ compilation directive, in order to
 be sure that the structure is known at this level.
 */
 WP_M_vWayPointSetSuperObject(_hWayPoint,_hNewSuperObject);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_Invalidate
(WP_tdhWayPoint*_p_hWayPoint)
{
 WP_M_vWayPointAssert(_p_hWayPoint,E_uwAIFatalInvalidParameter);
 *_p_hWayPoint = WP_C_hWayPointInvalid;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool WP_fnb_WayPoint_IsValid
(WP_tdhWayPoint _hWayPoint)
{
 return _hWayPoint != WP_C_hWayPointInvalid;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool WP_fnb_WayPoint_IsInvalid
(WP_tdhWayPoint _hWayPoint)
{
 return _hWayPoint == WP_C_hWayPointInvalid;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool WP_fnb_WayPoint_IsDynamic
(WP_tdhWayPoint _hWayPoint)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 return WP_M_hWayPointGetSuperObject(_hWayPoint)!=(HIE_tdxHandleToSuperObject)0;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
ACP_tdxBool WP_fnb_WayPoint_IsStatic
(WP_tdhWayPoint _hWayPoint)
{
 WP_M_vWayPointAssert(_hWayPoint!=WP_C_hWayPointInvalid,E_uwAIFatalInvalidParameter);
 return WP_M_hWayPointGetSuperObject(_hWayPoint)==(HIE_tdxHandleToSuperObject)0;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#undef extern
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vCreateGeoWayPoint(WP_tdstWayPoint *p_stWayPt)
{
	p_stWayPt = p_stWayPt;
	/*MTH3D_tdstVector stPosWP;
	WP_M_vWayPointGetVertex(p_stWayPt, &stPosWP);
 
	p_stWayPt->m_p_stGeoObj = fn_pCreateSphere(MTH3D_M_xGetXofVector(&stPosWP),MTH3D_M_xGetYofVector(&stPosWP),
		MTH3D_M_xGetZofVector(&stPosWP),WP_M_xWayPointGetRadius(p_stWayPt));
		*/
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void fn_vDrawWayPoint(WP_tdstWayPoint *p_stWayPt)
{
	p_stWayPt = p_stWayPt;
	/*POS_tdstCompletePosition stMatrix;
 
	POS_fn_vSetIdentityMatrix(&stMatrix);
	HIE_fn_bLoadMatrix(&stMatrix);
	GLI_xSendObjectToViewport(&gs_stViewportAttr, p_stWayPt->m_p_stGeoObj,
			 gp_stLight, GLI_C_lAllIsEnable-GLI_C_lIsNotDrawCollideInformation);
	HIE_fn_vPopMatrix();*/
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#if defined(AI_USE_SCRIPT)
SCR_tde_Anl_ReturnValue WP_fne_WayPoint_ScriptCallBackLoad
(
 SCR_tdst_File_Description *_p_fFile,
 char *_szAction,
 char *_szParams[],
 SCR_tde_Anl_Action _cType
)
{
/* ANNECY MT - 16/09/98 {*/
	static struct tdstEngineObject_		*hObjectParent;
	static MTH3D_tdstVector				stVertex;
	static MTH_tdxReal					xRadius;
 
 
	_p_fFile=_p_fFile;
 
	switch (_cType)
	{
	case SCR_EA_Anl_BeginSection:
		MTH3D_M_vNullVector(&stVertex);
		xRadius = MTH_C_ZERO;
		hObjectParent = NULL;
		break;
 
	case SCR_EA_Anl_Entry:
		if (stricmp(_szAction,"Vertex")==0)
		{
			/* load the vertex*/
			stVertex.xX = MTH_M_xFloatToReal((float) atof(_szParams[0]));
			stVertex.xY = MTH_M_xFloatToReal((float) atof(_szParams[1]));
			stVertex.xZ = MTH_M_xFloatToReal((float) atof(_szParams[2]));
		}
 
		else if (stricmp(_szAction,"Radius")==0)
		{
			xRadius = MTH_M_xFloatToReal((float) atof(_szParams[0]));
		}
 
		else if (stricmp(_szAction,"Father")==0)
		{
			char szForScript[256];
			SCR_tdst_Link_Value *p_stValue;
 
			strcpy(szForScript,fn_szGetLevelsDataPath());
			strcat(szForScript,"\\");
			strcat(szForScript,_szParams[0]);
 
			p_stValue = SCR_fnp_st_Link_CreateOrGetLinkFromKey(&g_st3DOSLinkTable.stCharacter, szForScript);
			if(SCR_M_e_Link_GetState(p_stValue)==SCR_ELS_Link_Initialized)
			{
				hObjectParent = (struct tdstEngineObject_*)SCR_M_ul_Link_GetValue(p_stValue);
			}
#ifndef U64 
#ifdef _DEBUG
			if(hObjectParent==NULL)
			{
				char szText[255];
 
/* Oliv' - Portage v15 - Please don't let accentuated characters be the last one of a string ! gcc sucks !!!*/
/* So I added a space character at the end*/
/*				sprintf(szText,"Le WayPoint %s a pour père l'acteur %s non chargé" , SCR_M_RdL0_GetCompleteSectionNameR(0), _szParams[0]);*/
				sprintf(szText,"Le WayPoint %s a pour père l'acteur %s non chargé " , SCR_M_RdL0_GetCompleteSectionNameR(0), _szParams[0]);
/* EndOfOliv' - Thnx :-)*/
				MessageBox(NULL , szText , "ERROR" , MB_OK+MB_ICONERROR);
			}			
#endif /*_DEBUG*/
#endif /* U64*/
		}
		break;
 
	case SCR_EA_Anl_EndSection:
		{
			WP_tdhWayPoint				hWaypoint;
			char						szCompleteName [255];
			char						szWaypointName[255];
			MTH3D_tdstVector			stAbsolutePosition;
			HIE_tdxHandleToSuperObject	h_SOSector;
			HIE_tdxHandleToSuperObject	hSuperObjParent;
 
			/* compute absolute position usinf father position if needed*/
			if(hObjectParent)
			{
				POS_tdxHandleToPosition hGlobalMatrix;
 
				hSuperObjParent = M_GetSuperObject(hObjectParent);
				hGlobalMatrix   = fn_p_st3dDataGetMatrix (hObjectParent->h_3dData);
				POS_fn_vMulMatrixVertex( &stAbsolutePosition, hGlobalMatrix, &stVertex );
			}
			else
			{
				MTH3D_M_vCopyVector( &stAbsolutePosition, &stVertex );
				hSuperObjParent = NULL;
			}
 
			if(fn_lGetSubMapNumber() != 0)
			{
				/* is waypoint into map ???*/
				h_SOSector=SECT_fn_hResearchInWhatSectorIAm(SECT_hFatherSector,&stAbsolutePosition);
 
				if (SECT_fn_bIsThisSectorUniverse(h_SOSector))
				{
#if defined(ACTIVE_EDITOR)
					char szText[255];
 
					sprintf(szText,"The WayPoint %s is in the sector Universe" , SCR_M_RdL0_GetCompleteSectionNameR(0));
 
					gs_Way_ulNbOfWarnings++;
					if(gs_Way_ulNbOfWarnings==1)
					{
						MessageBox(NULL , szText , "ERROR" , MB_OK+MB_ICONERROR);
					}
					else
					{
						if(!gs_fWpWarningsFile)
						{
							gs_fWpWarningsFile = fopen ("WpWarnings.log","at");
							if( gs_fWpWarningsFile )
							{
								fprintf( gs_fWpWarningsFile,"==============================================================================\n");
								fprintf( gs_fWpWarningsFile,"Map : %s     Submap : %02d\n" , fn_p_szGetLevelName() , GAM_M_GetSubMap(fn_lGetSubMapNumber()) );
								fprintf( gs_fWpWarningsFile,"==============================================================================\n");
							}							
						}
						if(gs_fWpWarningsFile)
						{
							fprintf (gs_fWpWarningsFile , "%s\n" , szText);
						}
					}
#endif
					return(SCR_ERV_Anl_NormalReturn);
				}
			}
 
			/* create waypoint*/
			hWaypoint =  WP_fnh_WayPoint_Create();
			WP_fnv_WayPoint_AddRef(hWaypoint);
			SCR_M_RdL0_SetSectionLong(0, 0, (unsigned long)hWaypoint);
 
			/* update Link Table*/
			strcpy(szWaypointName,SCR_M_RdL0_GetCompleteSectionNameR(0));
			strcpy(szCompleteName,fn_szGetLevelsDataPath());
			strcat(szCompleteName,"\\");
			strcat(szCompleteName,szWaypointName);
			SCR_fnp_st_Link_SetValue( &WP_gs_stWayPointLinkTable, szCompleteName, (unsigned long)hWaypoint );
 
			/* Set father*/
			WP_M_vWayPointSetSuperObject(hWaypoint,hSuperObjParent);
 
			/* Set radius*/
// FBF N64-format pour coords, et logfile dans x:\exe\geodump.log{
#ifdef U64CONVERTETLOG
			xRadius =  (float) (( (long)( (xRadius*fScale) + 0.5) ) / fScale);		
			stVertex.xX = (float) (( (long)( (stVertex.xX*fScale) + 0.5) ) / fScale);		
			stVertex.xY = (float) (( (long)( (stVertex.xY*fScale) + 0.5) ) / fScale);		
			stVertex.xZ = (float) (( (long)( (stVertex.xZ*fScale) + 0.5) ) / fScale);		
				
			fprintf(dump,"---------waypoint %s\nRadius: %f\nposition : %f,%f,%f\n",szWaypointName,xRadius,stVertex.xX,stVertex.xY,stVertex.xZ);
			fflush(dump);

#endif //U64CONVERTLOG
// } fin N64-format
			WP_M_vWayPointSetRadius(hWaypoint,xRadius);
 
			/* Set position*/
			WP_M_vWayPointSetVertex(hWaypoint, &stVertex);
 
			fn_vCreateGeoWayPoint((WP_tdstWayPoint *)hWaypoint);
			break;
		}
 
	case SCR_EA_Anl_AlreadyAnalysed:
		{
			WP_tdhWayPoint	hWaypoint;
			SCR_M_RdL0_GetSectionLong(0, 0, WP_tdhWayPoint, hWaypoint);
			WP_fnv_WayPoint_AddRef(hWaypoint);
		}
		break;
	}
 
	return SCR_ERV_Anl_NormalReturn;
/* END ANNECY MT }*/
}
 
 
 
/*
----------------------------------------------------------------------------------------
  Description   : WP_fn_vReloadWPGraph
                  called by the command console to reinit one (or more) graph script
  szNameOfGraph       -> giving by the console, it's the name of the graph to reload
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart' Compagnon
  Date          : 19/01/98
----------------------------------------------------------------------------------------
*/
/* script only*/
void WP_fn_vReloadWPGraph(char* szNameOfGraph)
{
  char szLevelName[255];
  char szWPSectionName[255];
  char sz255Error[255];
 
  /*WPG_fn_vDestroyAllGraph () ; // made in fn_vTreatCommandLine() cf. GAM\commands.c*/
 
  strcpy(szLevelName, fn_p_szGetLevelName());
  sprintf(szWPSectionName, "%s\\%s.%s^%s:%s", szLevelName, szLevelName, C_ScriptWayPointSuffixe, C_SectionWPGraphDescription, szNameOfGraph);
 
  if (SCR_fn_c_RdL0_IsSectionExists(szWPSectionName))
  {
    SCR_fnp_st_RdL0_AnalyseSection(szWPSectionName, SCR_CDF_uw_Anl_ForceAnalyse);
  }
  else
  {
    sprintf(sz255Error, "unknown Name of WPGraph (%s) !", szNameOfGraph);
    M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
  }
}
 
/*
----------------------------------------------------------------------------------------
  Description   : WP_fne_WPGraph_ScriptCallBackLoad
                  to read the WPGraph section in the script
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart' Compagnon
  Date          : 19/01/98
----------------------------------------------------------------------------------------
*/
/* script only*/
SCR_tde_Anl_ReturnValue WP_fne_WPGraph_ScriptCallBackLoad(SCR_tdst_File_Description *_p_fFile, char *_szAction, char *_szParams[], SCR_tde_Anl_Action _cType)
{
  SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  WP_tdHandleOfGraph hGraph = NULL;
  char sz255Error[255];
 
  switch (_cType)
  {
    case SCR_EA_Anl_BeginSection:
      if (_szAction[0] == 0)
      {
        sprintf(sz255Error, "invalid Name for WPGraph !");
        M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
        return SCR_ERV_Anl_TerminateCurrentSection;
      }
      if (strlen(_szParams[0]) == 0)
      {
        sprintf(sz255Error, "invalid Reference for WPGraph (%s) !", _szAction);
        M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
        return SCR_ERV_Anl_TerminateCurrentSection;
      }
 
      hGraph = WPG_fn_hCreate(_szAction, _szParams[0]);
			WPG_fn_vAddGlobalGraph(hGraph) ;
      SCR_M_RdL0_SetSectionLong(0, 0, (unsigned long)hGraph);
      break;
    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdHandleOfGraph, hGraph);
      break;
    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdHandleOfGraph, hGraph);
      break;
    case SCR_EA_Anl_AlreadyAnalysed:
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdHandleOfGraph, hGraph);
      break;
  }
  return eReturnValue;
}
 
/*
----------------------------------------------------------------------------------------
  Description   : GetWPFromName
 
  _szName             -> Name of the WayPoint
  _hGraph             -> handle to the graph
  Returns (WP_tdhWayPoint ) handle to the WayPoint
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart' Compagnon
  Date          : 19/01/98
----------------------------------------------------------------------------------------
*/
 
/* script only*/
WP_tdhWayPoint GetWPFromName(char* _szName, WP_tdHandleOfGraph _hGraph)
{
  char szCompleteName[255];
  WP_tdhWayPoint hWayPoint = NULL;
  SCR_tdst_Link_Value  *p_stLinkValue=NULL;
  char sz255Error[255];
 
  strcpy(szCompleteName, fn_szGetLevelsDataPath());
	strcat(szCompleteName,"\\");
	strcat(szCompleteName, _hGraph->m_szReferenceSection);
	strcat(szCompleteName, _szName);
 
  p_stLinkValue = SCR_fnp_st_Link_SearchKey(WP_fnp_WayPoint_GetLinkTable(), szCompleteName);
	if ( (p_stLinkValue!=NULL) && (SCR_M_e_Link_GetState(p_stLinkValue)!=SCR_ELS_Link_NotInitialized) )
  {
	  hWayPoint = (WP_tdhWayPoint)(p_stLinkValue->ulValue);
  }
  else
  {
	sprintf(sz255Error, "Invalid Link Table for WPGraphe : %s", szCompleteName);
	if(fn_lGetSubMapNumber() == 0)
		M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
#if defined(ACTIVE_EDITOR)
	if(gs_fWpWarningsFile != NULL)
		fprintf (gs_fWpWarningsFile , "%s\n" , sz255Error);
#endif
  }
 
  return(hWayPoint);
}
/*
----------------------------------------------------------------------------------------
  Description   : WP_fne_WPSommet_ScriptCallBackLoad
                  to read the WPSommet section in the script
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart' Compagnon
  Date          : 19/02/98
----------------------------------------------------------------------------------------
*/
/* script only*/
SCR_tde_Anl_ReturnValue WP_fne_WPSommet_ScriptCallBackLoad(SCR_tdst_File_Description *_p_fFile, char *_szAction, char *_szParams[], SCR_tde_Anl_Action _cType)
{
  SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  WP_tdHandleOfGraph hGraph;
  WP_tdhWayPoint hWayPoint, hDstWayPoint;
  unsigned long ulCapability;
  long lWeight, lTypeOfWP;
  char sz255Error[255];
  char* dummy;
 
  switch (_cType)
  {
    case SCR_EA_Anl_BeginSection:
      SCR_M_RdL0_GetSectionLong(-1, 0, WP_tdHandleOfGraph, hGraph);
      hWayPoint = GetWPFromName(_szAction, hGraph);
      if (!hWayPoint)
      {
		sprintf(sz255Error, "unknown WayPoint (%s) in WPGraph !", _szAction);
#if defined(ACTIVE_EDITOR)
		if(gs_fWpWarningsFile != NULL)
			fprintf (gs_fWpWarningsFile , "%s\n" , sz255Error);
#endif
		if(fn_lGetSubMapNumber() == 0)
			M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
        return SCR_ERV_Anl_TerminateCurrentSection;
      }
      WPG_fn_lAddWayPointIfNotExists(hGraph, hWayPoint, 0L);/* first ignore the Type Of WP*/
      if (_szParams[0])
      { /* then change the Type Of WP, if one ! !*/
        lTypeOfWP = (long) strtoul(_szParams[0], &dummy, 2);
        WPG_fn_lChangeTypeOfWP (hGraph, hWayPoint, lTypeOfWP); /*BART*/
      }
      SCR_M_RdL0_SetSectionLong(0, 0, (unsigned long)hWayPoint);
      break;
 
    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetSectionLong(-1, 0, WP_tdHandleOfGraph, hGraph);
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdhWayPoint, hWayPoint);
      hDstWayPoint = GetWPFromName(_szAction, hGraph);
      if (!hDstWayPoint)
      {
		sprintf(sz255Error, "unknown WayPoint (%s) in WPGraph !", _szAction);
#if defined(ACTIVE_EDITOR)
		if(gs_fWpWarningsFile != NULL)
			fprintf (gs_fWpWarningsFile , "%s\n" , sz255Error);
#endif
		if(fn_lGetSubMapNumber() == 0)
			M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
        return SCR_ERV_Anl_TerminateCurrentSection;
      }
      WPG_fn_lAddWayPointIfNotExists(hGraph, hDstWayPoint, 0L); /* ignore the Type Of WP*/
      lWeight = atoi(_szParams[0]);
      ulCapability = strtoul(_szParams[1], &dummy, 2);
      WPG_fn_lAddArcToWayPoint (hGraph, hWayPoint, hDstWayPoint, lWeight, ulCapability);
      break;
 
    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdhWayPoint, hWayPoint);
      break;
 
    case SCR_EA_Anl_AlreadyAnalysed:
      SCR_M_RdL0_GetSectionLong(0, 0, WP_tdhWayPoint, hWayPoint);
      break;
  }
  return eReturnValue;
}
 
/* script only*/
SCR_tde_Anl_ReturnValue WP_fne_WayPoint_ScriptSubSectionCallBackLoad
 
(
 SCR_tdst_File_Description*_p_fFile,
 char*_szAction,
 char*_d_szParams[],
 SCR_tde_Anl_Action _eAction
)
{
	_p_fFile=_p_fFile;
	_szAction=_szAction;
	_d_szParams=_d_szParams;
	_eAction=_eAction;
 
	/* test just to export the function*/
	return SCR_ERV_Anl_NormalReturn;
}
 
/* script only*/
SCR_tdst_Link_Value* WP_fnh_WayPoint_GetLoaded
(
 char *_szWPRefName
)
{
 /* check parameter validity :*/
 WP_M_vWayPointAssert(_szWPRefName,E_uwAIFatalInvalidParameter);
 return SCR_fnp_st_Link_CreateOrGetLinkFromKey
 (
  &WP_gs_stWayPointLinkTable,
  _szWPRefName
 );
}
#endif /* AI_USE_SCRIPT */
 
void WP_fnv_WayPoint_ComputeLocation( WP_tdhWayPoint _hWayPoint, MTH3D_tdstVector*_p_stVertex)
{
	MTH3D_tdstVector stWPVertex;
	POS_tdxHandleToPosition hGlobalMatrix;
 
	WP_M_vWayPointAssert ( (_hWayPoint!=WP_C_hWayPointInvalid) && (_p_stVertex), E_uwAIFatalInvalidParameter );
 
	/* cas des Waypoints dynamiques, c'est a dire qui bougent (rare...) */
	if (WP_M_hWayPointGetSuperObject(_hWayPoint))
	{
		WP_M_vWayPointGetVertex(_hWayPoint,&stWPVertex);
		hGlobalMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (WP_M_hWayPointGetSuperObject(_hWayPoint));
		POS_fn_vMulMatrixVertex( _p_stVertex, HIE_fn_hGetSuperObjectGlobalMatrix (WP_M_hWayPointGetSuperObject(_hWayPoint)), &stWPVertex );
	}
	else
		WP_M_vWayPointGetVertex(_hWayPoint,_p_stVertex);
}
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_InitLinkTable(void)
{
#if defined(AI_USE_SCRIPT)
 SCR_fn_v_Link_InitTable(&WP_gs_stWayPointLinkTable);
#endif /* AI_USE_SCRIPT */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_CloseLinkTable(void)
{
#if defined(AI_USE_SCRIPT)
 SCR_fn_v_Link_CloseTable(&WP_gs_stWayPointLinkTable);
#endif /* AI_USE_SCRIPT */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
#if defined(AI_USE_SCRIPT)
SCR_tdst_Link_Table* WP_fnp_WayPoint_GetLinkTable (void)
{
	return &WP_gs_stWayPointLinkTable;
}
#endif /* AI_USE_SCRIPT */
 
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_SecondPass(WP_tdhWayPoint _hWayPoint)
{
#if defined(AI_USE_SCRIPT)
 if(WP_M_hWayPointGetSuperObject(_hWayPoint))
 {
  WP_M_vWayPointAssert
  (
   (SCR_M_e_Link_GetState((SCR_tdst_Link_Value *)
     (WP_M_hWayPointGetSuperObject(_hWayPoint))) == SCR_ELS_Link_Initialized),
   E_uwAIFatalReferenceNotLoaded
  );
 
  /* retrieve the father handle : */
  WP_M_vWayPointSetSuperObject
  (
   _hWayPoint,
   M_GetSuperObject(
	(struct tdstEngineObject_*)
		(SCR_M_ul_Link_GetValue((SCR_tdst_Link_Value *)
		(WP_M_hWayPointGetSuperObject(_hWayPoint)))
	)
   )
  );
 }
#endif /* AI_USE_SCRIPT */
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
#if defined(AI_USE_SCRIPT)
WP_tdhWayPoint WP_fnh_WayPoint_GetHandleFromLinkTable
(SCR_tdst_Link_Value*_p_stLinkValue)
{
 WP_tdhWayPoint hWayPoint;
 WP_M_vWayPointAssert
 (
  (SCR_M_e_Link_GetState(_p_stLinkValue) == SCR_ELS_Link_Initialized),
  E_uwAIFatalReferenceNotLoaded
 );
 hWayPoint = (WP_tdhWayPoint)SCR_M_ul_Link_GetValue(_p_stLinkValue);
 /* WP_fnv_WayPoint_SecondPass(hWayPoint);       del by jt*/
 return hWayPoint;
}
#endif /* AI_USE_SCRIPT */
 
 
/* add by jt*/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WP_fnv_WayPoint_LinkTableSecondPass(void)
{
#if defined(ACTIVE_EDITOR)
	if( (fn_lGetSubMapNumber() != 0) && gs_Way_ulNbOfWarnings > 1)
	{
		MessageBox(NULL , "Some Waypoints are in the Sector Universe.\n Please see the 'WpWarnings.log' file" , "ERROR" , MB_OK);
	}
	if(gs_fWpWarningsFile)
	{
		fprintf ( gs_fWpWarningsFile , "==============================================================================\n\n");
		fclose  (gs_fWpWarningsFile);
	}
	gs_fWpWarningsFile = NULL;
	gs_Way_ulNbOfWarnings = 0;
#endif
}
 
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
