/*
	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_GAME_SESSION_DIRECTOR_SERVER_H
#define GAME_GAME_SESSION_DIRECTOR_SERVER_H

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

namespace Game
{	
	//=================================================================================================================
	//=================================================================================================================
	
	class GameSession;
	class GamePacket;
	class GamePeer;
	class NetworkAddress;
	class DoomLevel;
	struct DoomPPNode;

	//=================================================================================================================
	/// @brief		This helper class handles sending and receiving of data for the server.
	///				It is contained within the GameSession class itself.
	//=================================================================================================================

	class GameSessionDirector_Server
	{
		public:
			
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				//=====================================================================================================
				/// @brief		Creates this helper object
				/// @param		session - session we belong to
				//=====================================================================================================
				
				GameSessionDirector_Server( GameSession & session );
				
				/// Does an update of the server session: sends and receives data.
				
				void Update();
				
		private:
		
			//=========================================================================================================
			// Constants
			//=========================================================================================================
			
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// During area of interest management we check if one sector in the level is visible from another
				/// using doom's reject map. This is done by getting the sector our client is in, and the sector the 
				/// other client is in, and then doing the reject lookup. In addition to this however, we add some 
				/// lienency to the algorithm by checking sectors within a certain radius of the object we are determining
				/// is in scope. This is that radius:
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				
				static const float AOI_RADIUS;
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Copying is disallowed
				
				GameSessionDirector_Server( const GameSessionDirector_Server & other );
				
				/// Copying is disallowed
				
				GameSessionDirector_Server & operator = ( const GameSessionDirector_Server & other );
				
				/// Initialises the game world
				
				void InitialiseWorld();
				
				/// Does all receiving for the client
				
				void Receive( GamePacket & in , GamePacket & out );				
				
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Dispatches a packet to the given client if the packet is valid and if the client is valid. 
				/// Clears the packet aftewords also so it can be reused. Also sends other data to the client that 
				/// the server considers it intersted in, including create/delete messages and updates for other 
				/// non-client controlled objects.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				
				void DispatchPacket( GamePacket & packet , const NetworkAddress & client );
				
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Sends world data to the given client. This includes data for it's own object as well as data for
				/// any other objects in the world that are in scope to the client.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				
				void SendWorldData( GamePacket & packet , GamePeer & client );
				
				/// Creates the control object for a given client
				
				void CreateClientControlObject( GamePeer & client );
				
				/// Handles a disconnect packet chunk from the client
				
				bool HandlePacketChunk_Disconnect( GamePacket & in );

				/// Handles a join packet chunk from the client
				
				bool HandlePacketChunk_JoinRequest( GamePacket & in , GamePacket & out );
				
				/// Handles a game object data packet chunk from the client
				
				bool HandlePacketChunk_ObjectData( GamePacket & in );
				
				/// Handles a create message acknowledgement from the client
				
				bool HandlePacketChunk_CreateAcknowledgement( GamePacket & in );
				
				/// Handles a delete message acknowledgement from the client
				
				bool HandlePacketChunk_DeleteAcknowledgement( GamePacket & in );			
				
				/// Perfoms an area of interest check against the two points on the map
				
				bool IsObjectInAreaOfInterest( const DoomLevel & level , const Vec3f & client_pos , const Vec3f & obj_pos ) const;
				
				/// Recursively performs area of interest checks by going through the BSP tree in the level
				
				bool AoiCheckRecursive( const DoomLevel & level , const DoomPPNode & node , const Vec3f & client_pos , const Vec3f & obj_pos ) const;
				
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Session we belong to
				
				GameSession & m_session;
	};
};

#endif
