/*
	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_NETWORK_H
#define GAME_BUILD_PATH_NETWORK_H

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

namespace Game
{	
	//=================================================================================================================
	//=================================================================================================================
	
	class DoomLevel;
	class BuildPathNode;
	class BuildPathSubsector;

	//=================================================================================================================
	/// @brief		This temporary unoptimised path network is used to build up the path network at the start of the
	///				level. Once the network is built then all this data in here is discarded. This class does a lot of
	///				the hard work of actually building the network up. The PathNetwork class stores it in a more 
	///				optimal format.
	//=================================================================================================================

	class BuildPathNetwork
	{
		public:
			
			//=========================================================================================================
			// Functions
			//=========================================================================================================
		
				//=====================================================================================================	
				/// @brief		Creates the temporary path network. Begins building the network immediately.
				/// @param		level - level the path network is for
				//=====================================================================================================
				
				BuildPathNetwork( const DoomLevel & level );
				
				/// Destroys the temporary path network
				
				~BuildPathNetwork();
				
				/// Returns the list of subsectors in the network
				
				inline std::vector<BuildPathSubsector*> & GetSubsectors(){ return m_subsectors; }
				
				/// Returns the list of nodes in the network
				
				inline std::vector<BuildPathNode*> & GetNodes(){ return m_nodes; }
				
		private:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates a list of nodes by looking at the doom level. We use the armor helmet pickup to denote a path node.
				
				void BuildNodeList();
				
				/// Builds a list of subsectors by looking at the doom level.
				
				void BuildSubsectorList();
				
				/// Assigns nodes to subsectors. Prunes the node list also if there are any two nodes in the same subsector.
				
				void AssignNodesToSubsectors();
				
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Flood fills subsectors without nodes to assign them with the closest node possible. 
				/// This needs to be called until it returns false.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				
				bool FloodFillSubsectors( std::list<BuildPathSubsector*> & subsectors );
				
				/// Builds nodes for subsectors that do not have nodes after flood filling
				
				void GenerateMissingNodes();
				
				/// Connects all nodes that can connect together
				
				void BuildNodeConnectivity();
				
				/// Assigns node numbers to all nodes
				
				void AssignNodeNumbers();

			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// This is a list of subsectors in the network, corresponding to subsectors in the level
				
				std::vector<BuildPathSubsector*> m_subsectors;
				
				/// This is a list of path nodes in the network
				
				std::vector<BuildPathNode*> m_nodes;
				
				/// This is the level the path network belongs to
				
				const DoomLevel * m_level;
	};
};

#endif
