/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GAME_BUILD_PATH_NODE_H
#define GAME_BUILD_PATH_NODE_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{	
	//=================================================================================================================
	//=================================================================================================================
	
	struct DoomGLSubsectorV2;
	class BuildPathSubsector;
	class DoomLevel;

	//=================================================================================================================
	/// @brief		Represents a node in network of path nodes. This node is only used to build the path network, 
	///				the actual optimised path network uses a structure of arrays to represent all the nodes for 
	///				the purposes of efficiency.
	//=================================================================================================================

	class BuildPathNode
	{
		public:
			
			//=========================================================================================================
			// Functions
			//=========================================================================================================
		
				//=====================================================================================================	
				/// @brief		Creates the path node:
				/// @param		pos_xz - x and z coordinates of the path node in the map
				/// @param		level - level the path node belongs to
				/// @pre		The given level cannot be null
				//=====================================================================================================
				
				BuildPathNode( Vec2f pos_xz , const DoomLevel & level );
				
				//=====================================================================================================
				/// @brief		Connects this path-node to another path node
				/// @param		other - other path node to connect to
				/// @pre		Given path-node must not be null
				//=====================================================================================================
				
				inline void Connect( BuildPathNode & other )
				{
					// Do not connect the node to itself
					
					if ( other == * this ) return;
				
					// Make sure we are not already connected to this node:
					
					for ( unsigned i = 0 ; i < m_connected_nodes.size() ; ++i )
					{
						if ( * ( m_connected_nodes[i] ) == other ) return;
					}
					
					// Connect:
					
					m_connected_nodes.push_back(&other);
				}
				
				//=====================================================================================================
				/// @brief		Sets the subsector of the level that this path-node is in
				/// @param		subsector - new subsector that the node is assigned to
				/// @returns	The subsector in the map this path-node belongs to
				//=====================================================================================================
				
				inline void SetSubsector( BuildPathSubsector * subsector ){ m_subsector = subsector; }
				
				//=====================================================================================================
				/// @brief		Returns the subsector of the level that this path-node is in
				/// @returns	The subsector in the map this path-node belongs to
				//=====================================================================================================
				
				inline const BuildPathSubsector * GetSubsector() const { return m_subsector; }
				
				//=====================================================================================================
				/// @brief		Returns a list of nodes this path node is connected to
				/// @returns	A list of nodes that this path node is connected to
				//=====================================================================================================
				
				inline const std::vector<BuildPathNode*> & GetConnectedNodes() const { return m_connected_nodes; }
				
				//=====================================================================================================
				/// @brief		Gets the position of this node
				/// @returns	Position of this node
				//=====================================================================================================
				
				inline const Vec2f & GetPosition() const { return m_position; }
				
				//=====================================================================================================
				/// @brief		Sets the node number for this node
				/// @param		node_num - node number for this node
				//=====================================================================================================
				
				inline void SetNodeNumber( unsigned node_num ){ m_node_num = node_num; }
				
				//=====================================================================================================
				/// @brief		Gets the node number for this node
				/// @returns	The node number for this node
				//=====================================================================================================
				
				inline unsigned GetNodeNumber() const { return m_node_num; }
				
				//=====================================================================================================
				/// @brief		Compares two path nodes for equality
				/// @param		other - other node to compare with
				//=====================================================================================================
				
				inline bool operator == ( const BuildPathNode & other ) const 
				{
					return m_position == other.m_position;
				}

				//=====================================================================================================
				/// @brief		Compares two path nodes for equality
				/// @param		other - other node to compare with
				//=====================================================================================================
				
				inline bool operator != ( const BuildPathNode & other ) const 
				{
					return m_position != other.m_position;
				}
				
		private:

			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Position of the path node along the x-z axes
				
				Vec2f m_position;
				
				/// This is the doom level the path node belongs to
				
				const DoomLevel * m_level;
				
				/// This is the subsector the path node belongs to
				
				const BuildPathSubsector * m_subsector;
				
				/// These are all the nodes the path node is connected to
				
				std::vector<BuildPathNode*> m_connected_nodes;
				
				/// This is the number of this node in the network
				
				unsigned m_node_num;
	};
};

#endif
