/*!
	@project Rune
	@file RuPathGraph_AStar.h

	Copyright (c) 2004-2005 All rights reserved

	@author John Tang
	@date 2005/12/22
*/

#ifndef _RUPATHGRAPH_ASTAR_H_
#define _RUPATHGRAPH_ASTAR_H_

#include "../PathEngine/RuPathGraph_Base.h"
#include "../PathEngine/RuPathFinder_AStar.h"
#include "../Core/Type/RuType_BitField.h"

#pragma unmanaged

// ************************************************************************************************************************************************************

class CRuPathGraph_AStar : public IRuPathGraph_Base
{
	ruRTTI_DECLARE
	ruHEAP_DECLARE
	ruCLASSGUID_DECLARE

protected:
	struct PathNodeLinkDesc
	{
		INT32								m_nodeID;						//!< Source link node ID
		RuPathNodeLinkType					m_linkType;						//!< Node link type
		REAL								m_linkDistance;					//!< Link distance
		INT32								m_linkData;						//!< Link data, semantics depend on link type
	};

	INT32									m_graphKey;						//!< Graph key
	CRuAABB									m_bounds;						//!< Graph bounds

	CRuArrayList<CRuPathNode *>				m_pathNodes;
	CRuArrayList<PathNodeLinkDesc>			m_pathNodeLinks;

	CRuPathNodePartition*					m_pathNodePartition;

	// Other runtime data
	IRuPathFinder_Base*						m_pathFinder;
	IRuPathRover_Base*						m_pathRover;

public:
											CRuPathGraph_AStar();
											CRuPathGraph_AStar(const CRuAABB &bounds);
	virtual									~CRuPathGraph_AStar();

	// Serialization interface
	virtual BOOL							SerializeFrom(IRuStream *stream);
	virtual BOOL							SerializeTo(IRuStream *stream);

	virtual BOOL							SetPathFinder(IRuPathFinder_Base *pathFinder);
	virtual BOOL							SetPathRover(IRuPathRover_Base *pathRover);

	virtual void							SetGraphKey(INT32 graphKey);
	virtual INT32							GetGraphKey() const;

	virtual const CRuAABB&					Bounds();

	virtual INT32							GetNumPathNodes();
	virtual const CRuPathNode*				GetPathNode(INT32 nodeID);

	virtual INT32							InsertPathNode(const CRuVector3 &position);
	virtual BOOL							DeletePathNode(INT32 nodeID);

	virtual INT32							GetNumPathNodeLinks();
	virtual BOOL							GetPathNodeLink(INT32 linkID, INT32 &nodeIDOut, RuPathNodeLinkType &linkTypeOut, REAL &linkDistanceOut, INT32 &linkDataOut);
	virtual BOOL							SetPathNodeLink(INT32 nodeID, RuPathNodeLinkType linkType, REAL linkDistance, INT32 linkData);
	virtual INT32							FindPathNodeLink(INT32 nodeID);

	virtual BOOL							EnumeratePathNodes(const CRuSphere &bounds, CRuArrayList<INT32> &pathNodesOut);
	virtual BOOL							FindPath(INT32 startNodeID, INT32 endNodeID, CRuArrayList<INT32> &pathOut);

protected:
	void									ConnectNodeToNeighbors(INT32 nodeID);
};

// ************************************************************************************************************************************************************

ruOBJECTMODULE_DEFINE(CRuObjectModule_PathGraph_AStar, CRuPathGraph_AStar)

// ************************************************************************************************************************************************************
#pragma managed

#endif
