/*BEGIN FS 10/8/98*/
#include "Ldt.h"
/*END FS 10/8/98*/
  
/*BEGIN FS 9/14/98*/
/*#if defined(AI_USE_SCRIPT)*/
/*----------------------------------------------------------------------
 *  Description: callback for WayPoint section from .WP script file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/14/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadWP_WayPoint( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szEntryName;
 
/* ANNECY MT - 16/09/98 {*/
	WP_tdhWayPoint				hWaypoint;
	char						szCompleteName [255];
/*	char						szWaypointName[255];*/
	MTH3D_tdstVector			stAbsolutePosition;
	HIE_tdxHandleToSuperObject	h_SOSector;
	HIE_tdxHandleToSuperObject	hSuperObjParent;
 
	static struct tdstEngineObject_		*hObjectParent;
	static MTH3D_tdstVector				stVertex;
	static MTH_tdxReal					xRadius;
 
	/*Working for BeginSection*/
	MTH3D_M_vNullVector(&stVertex);
	xRadius = MTH_C_ZERO;
	hObjectParent = NULL;
	
	/* Working for Entries*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		szEntryName=LDT_szGetEntryName();
		switch (*(long*)(szEntryName))
		{
		case 'treV': /*"Vertex"*/
			 /* load the vertex*/
			stVertex.xX = MTH_M_xFloatToReal((float) atof(LDT_szGetParam(1)));
			stVertex.xY = MTH_M_xFloatToReal((float) atof(LDT_szGetParam(2)));
			stVertex.xZ = MTH_M_xFloatToReal((float) atof(LDT_szGetParam(3)));     
			break;
		case 'idaR': /*"Radius"*/
			/* load the radius */
			xRadius = MTH_M_xFloatToReal((float) atof(LDT_szGetParam(1)));
			break;
		case 'htaF': /*"Father"*/
			char szForScript[256];
			SCR_tdst_Link_Value *p_stValue;
 
			strcpy(szForScript,fn_szGetLevelsDataPath());
			strcat(szForScript,"\\");
			strcat(szForScript,LDT_szGetParam(1));
 
			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);
			if(hObjectParent==NULL)
			{
#ifdef _DEBUG
				char szText[255],szSectionName[255];
				LDT_ComputeSectionName(pLink,szSectionName);
				sprintf(szText,"Le WayPoint %s a pour père l'acteur %s non chargé",szSectionName,LDT_szGetParam(1));
				MessageBox(NULL , szText , "ERROR" , MB_OK+MB_ICONERROR);
#endif
			}			
			break;
		}/*endswitch*/
		result=LDT_GetNextEntry();
	}/*endwhile*/
 
	/*Working for EndSection*/
 
	/* 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;
	}
 
	/* is waypoint into map ???*/
	h_SOSector=SECT_fn_hResearchInWhatSectorIAm(SECT_hFatherSector,&stAbsolutePosition);
 
	if (SECT_fn_bIsThisSectorUniverse(h_SOSector))
	{
#ifdef _DEBUG
		char szText[255],szSectionName[255];
		LDT_ComputeSectionName(pLink,szSectionName);
		sprintf(szText,"Le WayPoint %s est dans le secteur Univers",szSectionName);
		MessageBox(NULL , szText , "ERROR" , MB_OK+MB_ICONERROR);
#else
		return 0;
#endif
	}
 
	/* create waypoint*/
	hWaypoint =  WP_fnh_WayPoint_Create();
	WP_fnv_WayPoint_AddRef(hWaypoint);
/*!!! CE VREA AICI (CORELAT CU AlreadyAnalysed)*/
/*	SCR_M_RdL0_SetSectionLong(0, 0, (unsigned long)hWaypoint);*/
 
	/* update Link Table*/
	LDT_ComputeSectionName(pLink,szCompleteName);
	/*strcpy(szWaypointName,szCompleteName);*/
	/*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*/
	WP_M_vWayPointSetRadius(hWaypoint,xRadius);
 
	/* Set position*/
	WP_M_vWayPointSetVertex(hWaypoint, &stVertex);
 
	fn_vCreateGeoWayPoint((WP_tdstWayPoint *)hWaypoint);
 
	return 0;
}
/*#endif /* AI_USE_SCRIPT   */
/*END FS 9/14/98*/
 
/*BEGIN FS 9/14/98*/
/*#if defined(AI_USE_SCRIPT)*/
/*----------------------------------------------------------------------
 *  Description: callback for WPGraph section from .WP sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/14/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadWP_Graph( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szSectionName;
	
	WP_tdHandleOfGraph hGraph = NULL;
	char sz255Error[255];
 
	szSectionName=LDT_szGetSectionName();
	if (strlen(szSectionName)==0)
	{
		sprintf(sz255Error, "invalid Name for WPGraph !");
		M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
		while( LDT_GetNextEntry()!=ParseResult_EndSection );
		return -1;
	}
	if (LDT_iGetNbParams()<1)
	{
		sprintf(sz255Error, "invalid Reference for WPGraph (%s) !", szSectionName);
		M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
		while( LDT_GetNextEntry()!=ParseResult_EndSection );
		return -1;
	}
	hGraph = WPG_fn_hCreate(szSectionName,LDT_szGetParam(1));
	WPG_fn_vAddGlobalGraph(hGraph) ;
	pLink->pObject=hGraph;
	
	/* read the next line in script file*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		/* Load WpSommet SubSection of WpGraph section*/
		LDT_LoadSection(NULL);
		result=LDT_GetNextEntry();
	}	
	return 0;
}
/*#endif /* AI_USE_SCRIPT   */
/*END FS 9/14/98*/
 
/*BEGIN FS 9/14/98*/
/*#if defined(AI_USE_SCRIPT)*/
/*----------------------------------------------------------------------
 *  Description: callback for ... of ... sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/14/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadWP_Sommet( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szSectionName, *szEntryName, *szParam1=NULL;
 
	WP_tdHandleOfGraph hGraph;
	WP_tdhWayPoint hWayPoint, hDstWayPoint;
	long lCapacity, lValuation, lTypeOfWP;
	char sz255Error[255];
	char* dummy;
 
	/*Working for BeginSection*/
	hGraph=(WP_tdHandleOfGraph)pLink->pParent->pObject;
	szSectionName=LDT_szGetSectionName();
	hWayPoint = GetWPFromName(szSectionName, hGraph);
	if (!hWayPoint)
	{
		sprintf(sz255Error, "unknown WayPoint (%s) in WPGraph !", szSectionName);
		M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
		while( LDT_GetNextEntry()!=ParseResult_EndSection );
		return -1;
	}
	WPG_fn_lAddWayPointIfNotExists(hGraph, hWayPoint, 0L);/* first ignore the Type Of WP*/
	if (LDT_iGetNbParams()>1)
	{ /* then change the Type Of WP, if one ! !*/
		lTypeOfWP = (long) strtoul(LDT_szGetParam(1), &dummy, 2);
		WPG_fn_lChangeTypeOfWP (hGraph, hWayPoint, lTypeOfWP); /*BART*/
	}
 
	/*Working for Entries*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		szEntryName=LDT_szGetEntryName();
		hDstWayPoint = GetWPFromName(szEntryName, hGraph);
		if (!hDstWayPoint)
		{
			sprintf(sz255Error, "unknown WayPoint (%s) in WPGraph !", szEntryName);
			M_AIWarningErrorAndAllowStopForDebug(E_uwAIWarningScriptError, C_ucErmOpenInfoWindow, sz255Error);
			while( LDT_GetNextEntry()!=ParseResult_EndSection );
			return -1;
		}
		WPG_fn_lAddWayPointIfNotExists(hGraph, hDstWayPoint, 0L); /* ignore the Type Of WP*/
		lValuation = atoi(LDT_szGetParam(1));
		lCapacity = (long) strtoul(LDT_szGetParam(2), &dummy, 2);
		WPG_fn_lAddArcToWayPoint (hGraph, hWayPoint, hDstWayPoint, lValuation, lCapacity);
		result=LDT_GetNextEntry();
	}
	return 0;
}
/*#endif /* AI_USE_SCRIPT   */
/*END FS 9/14/98*/
 
/*BEGIN FS 9/14/98*/
/*----------------------------------------------------------------------
 *  Description: callback for Isolate and InWay sections from .WP sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/14/98
 *  Modif date:
 *----------------------------------------------------------------------*/
/*#if defined(AI_USE_SCRIPT)*/
int LoadWP_Isolate_InWay( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	while((result=LDT_GetNextEntry())!=ParseResult_EndSection )
	{
		/* Load WayPoint SubSection of Isolate section*/
		if(result == ParseResult_BeginSection)
			LDT_LoadSection(NULL);
	}
	return 0;
}
/*#endif /* AI_USE_SCRIPT   */
/*END FS 9/14/98*/
 
