/*
----------------------------------------
File Name :
 AIUseCPA.h
----------------------------------------
Required for :
 ACP basic definitions.
----------------------------------------
*/
#include "AIUseCPA.h"
 
/*
----------------------------------------
File Name :
 AIMacros.h
----------------------------------------
Required for :
 AI main macros definitions.
----------------------------------------
*/
#include "AIMacros.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_WPARC_FREIND_
#include "WParc.h"
#undef _WP_D_WPARC_FREIND_
 
#define _WP_D_WAYPOINT_FRIEND_
#define WP_D_WAYPOINT_GLOBALS_
#include "WPWayPt.h"
#undef WP_D_WAYPOINT_GLOBALS_
#undef _WP_D_WAYPOINT_FRIEND_
 
/*****************************************************
 * Function : WPARCLST_fn_hInit
 * Description : initialise a WParc list to an empty 
 *               list. it also allocate memory for the
 *               list.
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
WP_tdHandleOfListOfArc WPARCLST_fn_hInit (void) 
{
	WP_tdHandleOfListOfArc hList ;
 
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeListOfArc , NULL);
 
#ifdef U64
	/* on U64, AI is a block without free, so alloc in TMP*/
	hList = (WP_tdHandleOfListOfArc) TMP_M_p_Malloc(sizeof(WP_tdstListOfArc));
#else
	M_AIAlloc(hList, WP_tdHandleOfListOfArc, sizeof(WP_tdstListOfArc)) ;
#endif
 
	if ( hList ) 
		LST2_M_DynamicInitAnchor(&hList->m_hArc) ;
	return (hList) ;
}
/*****************************************************
 * Function : WPARCLST_fn_vDestroy
 * Description : free memory allocated for the list
 *               and destroy each element of the list
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
void WPARCLST_fn_vDestroy (WP_tdHandleOfListOfArc hList) 
{
	WP_tdHandleOfArc hElement, hNextElement ;
	int i ;
	
	if ( hList ) 
	{
		LST2_M_DynamicForEachMovingElementOf(&hList->m_hArc, hElement, hNextElement, i)
		{
			LST2_M_DynamicIsolate(hElement) ;
			WPARC_fn_vDestroyArc (hElement) ;
		}
 
#ifdef U64
		/* on U64, AI block is replaced by TMP block, so free in TMP*/
		TMP_M_Free(hList);
#else
		M_AIFree(hList);
#endif
 
		hList = NULL; /* BART ?*/
	}
}
 
/*****************************************************
 * Function : WPARCLST_fn_vAdd
 * Description : add a new element at the end of the 
 *               list.
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
void WPARCLST_fn_vAdd (WP_tdHandleOfListOfArc hListOfArc, WP_tdhGraphNode hNode, 
									     long lWeight, unsigned long ulCapability) 
{
	WP_tdHandleOfArc hNewArc ;
  char bOK=0;
  int i;
  WP_tdHandleOfArc hElement;
 
	if ( hListOfArc )
  {
		LST2_M_DynamicForEachElementOf(&hListOfArc->m_hArc, hElement, i) /* if already exists !*/
    {
      if (hElement->m_hNode == hNode)
      {
        hElement->m_ulCapabilityInit = ulCapability;
        hElement->m_ulCapability     = ulCapability;
        hElement->m_lWeightInit = lWeight;
        hElement->m_lWeight     = lWeight;
        bOK = 1;
        break;
      }
    }
    if (bOK == 0)
    {
      hNewArc = WPARC_CreateArc (hNode, lWeight, ulCapability) ;
		  if ( hNewArc ) {
  			LST2_M_DynamicAddTail( &hListOfArc->m_hArc, hNewArc ) ;
	  	}
    }
	}
}
 
/*****************************************************
 * Function : WPARCLST_fn_vRemove
 * Description : remove an element from the list
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void WPARCLST_fn_vRemove (WP_tdHandleOfListOfArc hList, WP_tdhGraphNode hNode ) {
	WP_tdHandleOfArc hElement, hNextElement ;
	int i ;
 
	if ( hList ) {
		LST2_M_DynamicForEachMovingElementOf(&hList->m_hArc, hElement, hNextElement, i) {
			if ( hElement->m_hNode == hNode ) {	
				LST2_M_DynamicIsolate(hElement) ;
				WPARC_fn_vDestroyArc (hElement) ;
				break ;
			}
		}
	}
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*****************************************************
 * Function : WPARCLST_fn_lChangeCapabilities
 * Description : change the Capability of an existing arc
 *               return -1 if the link doesn't exist
 *               return 0 if the Capability has been change
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
long WPARCLST_fn_lChangeCapabilities (WP_tdHandleOfListOfArc hListOfArc, WP_tdhGraphNode hNode, unsigned long ulCapability, long lValue) {
	WP_tdHandleOfArc hElement ;
	int i ;
 
	if ( hListOfArc && hNode ) {
		LST2_M_DynamicForEachElementOf(&hListOfArc->m_hArc, hElement, i) {
			if ( hElement->m_hNode == hNode ) {
        switch (lValue)
        {
        case 0:
        case 2:
          hElement->m_ulCapability |= ulCapability; /* add the type*/
          break;
        case 1:
        case 3:
          hElement->m_ulCapability = ulCapability; /* change all the types field*/
          break;
        case 4:
          hElement->m_ulCapability &= ~ulCapability;; /* sub the type*/
          break;
        case 5:
          hElement->m_ulCapability = 0; /* erase all the types field*/
          break;
        }
				return (0) ;
			}
		}
 
	}
	return (-1) ;
}
 
/*****************************************************
 * Function : WPARCLST_fn_lGetCapabilities
 * Description : get the Capabilities of an existing arc
 *               return -1 if the link doesn't exist
 *               return 0 otherwise
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
long WPARCLST_fn_lGetCapabilities (WP_tdHandleOfListOfArc hListOfArc, 
															     WP_tdhGraphNode hNode, unsigned long* p_ulCapability) {
	WP_tdHandleOfArc hElement ;
	int i ;
 
	if ( hListOfArc && hNode ) {
		LST2_M_DynamicForEachElementOf(&hListOfArc->m_hArc, hElement, i) {
			if ( hElement->m_hNode == hNode ) {
				*p_ulCapability = hElement->m_ulCapability ;
				return (0) ;
			}
		}
	}
	return (-1) ;
}
 
 
long WPARCLST_fn_lChangeWeight (WP_tdHandleOfListOfArc hListOfArc, WP_tdhGraphNode hNode, long lWeight) {
	WP_tdHandleOfArc hElement ;
	int i ;
 
	if ( hListOfArc && hNode ) {
		LST2_M_DynamicForEachElementOf(&hListOfArc->m_hArc, hElement, i) {
			if ( hElement->m_hNode == hNode ) {
				hElement->m_lWeight = lWeight;
				return (0) ;
			}
		}
 
	}
	return (-1) ;
}
 
long WPARCLST_fn_lGetWeight (WP_tdHandleOfListOfArc hListOfArc, 
                             WP_tdhGraphNode hNode, long *p_lWeight) {
	WP_tdHandleOfArc hElement ;
	int i ;
 
	if ( hListOfArc && hNode ) {
		LST2_M_DynamicForEachElementOf(&hListOfArc->m_hArc, hElement, i) {
			if ( hElement->m_hNode == hNode ) {
				*p_lWeight = hElement->m_lWeight ;
				return (0) ;
			}
		}
	}
	return (-1) ;
}
 
 
/*****************************************************
 * Function : WPARC_CreateArc
 * Description : allocate memory for an Element and
 *               set his value
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
WP_tdHandleOfArc WPARC_CreateArc (WP_tdhGraphNode hNode, long lWeight, unsigned long ulCapability) 
{
	WP_tdHandleOfArc hNewArc ;
 
    MMG_fn_vAddMemoryInfo (MMG_C_lTypeAI , MMG_C_lSubTypeArc , NULL);
 
#ifdef U64
	/* on U64, AI is a block without free, so alloc in TMP*/
	hNewArc = (WP_tdHandleOfArc) TMP_M_p_Malloc(sizeof(WP_tdstWayArc));
#else
	M_AIAlloc ( hNewArc, WP_tdHandleOfArc, sizeof ( WP_tdstWayArc ) ) ;
#endif
	
	if ( hNewArc ) 
	{
		LST2_M_DynamicInitElement(hNewArc) ;
		hNewArc->m_hNode = hNode ;
		hNewArc->m_ulCapabilityInit = ulCapability ;
		hNewArc->m_ulCapability     = ulCapability ;
		hNewArc->m_lWeightInit = lWeight ;
		hNewArc->m_lWeight     = lWeight ;
	}
	return (hNewArc) ;
}
 
 
void WPARC_fn_vDestroyArc(WP_tdHandleOfArc hArc)
{
	if ( hArc )
	{
#ifdef U64
		/* on U64, AI block is replaced by TMP block, so free in TMP*/
		TMP_M_Free(hArc);
#else
		M_AIFree(hArc) ;
#endif
		hArc = NULL;
	}
}
/*****************************************************
 * Function : WPARC_fn_lSetWeight
 * Description : access function, set the Weight 
 *               field
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
long WPARC_fn_lSetWeight (WP_tdHandleOfArc hArc, long lWeight) {
  if ( hArc ) {
    hArc->m_lWeightInit = lWeight;
		return ( !(hArc->m_lWeight = lWeight) ) ;
  }
	return (-1) ;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
/*****************************************************
 * Function : WPARC_fn_lGetWeight
 * Description : access function, get the Weight 
 *               field
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
long WPARC_fn_lGetWeight (WP_tdHandleOfArc hArc) {
	if ( hArc ) 
		return (hArc->m_lWeight) ;
	return (-1) ;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*****************************************************
 * Function : WPARC_fn_lSetCapability
 * Description : access function, set the Capability field
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
long WPARC_fn_lSetCapability (WP_tdHandleOfArc hArc, unsigned long ulCapability) {
  if ( hArc ) {
    hArc->m_ulCapabilityInit = ulCapability;
		return ( !(hArc->m_ulCapability = ulCapability) ) ;
  }
	return (-1) ;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/* Function : WPARC_fn_lAddCapability
		//return ( !(hArc->m_ulCapability |= ulCapability) ) ;
    hArc->m_ulCapability |= ulCapability;
*/
 
/* Function : WPARC_fn_lSubCapability
		//return ( !(hArc->m_ulCapability ^=ulCapability) ) ;
    hArc->m_ulCapability -= (hArc->m_ulCapability & ulCapability);
*/
 
/*****************************************************
 * Function : WPARC_fn_ulGetCapability
 * Description : access function, get the Capability field
 *****************************************************
 * Author : Jean-Marc Drouaud
 * Date   : 13/01/98
 *****************************************************/
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
unsigned long WPARC_fn_ulGetCapability (WP_tdHandleOfArc hArc) {
	if ( hArc )
		return ( hArc->m_ulCapability ) ;
	return ((unsigned long)0) ;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
 
 
 
