//
// Asura_ServerEntity_Activatable.h
//
// Created by Richard Rice - 18/09/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef __ASURA_SERVERENTITY_ACTIVATABLE_H__
#define __ASURA_SERVERENTITY_ACTIVATABLE_H__

#include "Entity/Asura_Entity.h"

#ifdef ASURA_TOOLS
#include "Tools_System.h"
#endif


class Asura_ServerEntity_Activatable : public Asura_Entity_Server
{
	typedef Asura_Entity_Server PARENT;

	public :
		Asura_ServerEntity_Activatable(bool bActive = true);
		virtual ~Asura_ServerEntity_Activatable() {}

		//constructor from load
		Asura_ServerEntity_Activatable(Asura_Guid uGuid,const char*& pcChunkBuffer);
		//and coonstructor for subsequent initialisation using InitialiseFromChunkBuffer
		Asura_ServerEntity_Activatable(Asura_Guid uGuid);
	
		// override of HasClientEntities() - we don't have any.
		virtual bool HasClientEntities() const { return false; }

		virtual bool HandleMessage(const Asura_Message* pxMessage);

		inline void SetActive (const bool bActive)
		{
			// Are we activating/deactivating?
			if (!m_bActive && bActive)
			{
				// Activating - call the appropriate event...
				OnActivation	();
			}

			else if (m_bActive && !bActive)
			{
				// Deactivating - call the appropriate event...
				OnDeactivation	();
			}

			// Set the state...
			SetInternalActiveFlag(bActive);
		}

		inline bool IsActive (void) const
		{
			// Return the state...
			return	(m_bActive);
		}		

        // static function used to cope with Asura_ClientEntity_SoundController
        // being derived from Asura_Entity_Client instead of a client equivalent of this
        // class. This allows the chunk buffer to be read in order to move the chunk data
        // pointer past the data and to extract any useful info, e.g. bActive.
        // This is now called by the constructor.
        static void ParseChunkBuffer(const char*& pcChunkBuffer, bool* pActive, Asura_Entity_Server* pxServerEntity = 0);
        static void ParseChunkStream(Asura_Chunk_Stream& xStream, bool* pActive, Asura_Entity_Server* pxServerEntity = 0);


		#ifdef ASURA_SUPPORT_ENTITYWATCH
		virtual void DisplayEntityWatchText();
		#endif

		// MGH 5/9/2006: Don't use this function unless you know what you are doing.
		// MGH 30/11/2006: I've made this public as I need it for the persistent
		// zone data serialisation code in HP5.
		void SetInternalActiveFlag_WithoutChangingUpdateList(const bool bActive)
		{
			m_bActive = bActive;
		}

	protected :

		// ART - 18/07/2006 - Made active booleans private and provided accessor function...
		void SetInternalActiveFlag(const bool bActive)
		{
			m_bActive	= bActive;

			if(bActive)
			{
				SetNeverUpdates(false);
			}
			else
			{
				SetNeverUpdates(true);
			}
		
		}


		virtual void OnActivation();
		virtual void OnDeactivation();
		virtual void OnTrigger() {}
		virtual void OnUnTrigger() {}
		virtual void OnDestruction();

		//virtual function overides for saving
//		virtual	bool	IsFilledFromChunkBuffer		(void) const					{ return (true); }

		virtual bool	CanBeSavedToChunk() const {return true;}
		virtual	int		GetChunkBufferSize			(void);
		virtual	void	FillInChunkBuffer			(char*& pcChunkBuffer);
		virtual	void	InitialiseFromChunkBuffer	(const char*& pcChunkBuffer);

		virtual	void	ReadFromChunkStream			(Asura_Chunk_Stream& xStream);
		virtual	void	WriteToChunkStream			(Asura_Chunk_Stream& xStream) const;


	private:
		unsigned char m_bActive :1;


		friend class Tools_Entity_Activatable_Object;
};


//variation of activatable that stores some extra position and orientation information ,
//but does nothing with it of its own accord.
//RAR 04/03/03
class Asura_ServerEntity_ActivatableWithPosition : public Asura_ServerEntity_Activatable
{
	public :
		Asura_ServerEntity_ActivatableWithPosition(Asura_Guid uGuid = ASURA_GUID_UNREGISTERED);
		//constructor from load
		Asura_ServerEntity_ActivatableWithPosition(Asura_Guid uGuid,const char*& pcChunkBuffer);

		virtual ~Asura_ServerEntity_ActivatableWithPosition();
	
		#ifdef ASURA_TOOLS
		virtual void ApplyChangesForTemplateInstance( class Tools_EntityTemplateInstance* pxInstance );
		#endif

		// Public accessors for position & orientation matrix.
		const Asura_Vector_3&		GetPosition()		const	{	return m_xPosition;		}
		const Asura_Matrix_3x3&		GetOrientation()	const	{	return m_xOrientation;	}
		
		// static members for list management
		static Asura_ServerEntity_ActivatableWithPosition* FindEntity( Asura_Guid uGuid )
		{
			return s_xList.FindEntity(uGuid);
		}
		static Asura_Hashed_List<Asura_ServerEntity_ActivatableWithPosition>* GetList()
		{
			return &s_xList;
		}

	protected :

		//virtual function overides for saving
//		virtual	bool	IsFilledFromChunkBuffer		(void) const					{ return (true); }

		virtual	int		GetChunkBufferSize			(void);
		virtual	void	FillInChunkBuffer			(char*& pcChunkBuffer);
		virtual	void	InitialiseFromChunkBuffer	(const char*& pcChunkBuffer);

		virtual	void	ReadFromChunkStream			(Asura_Chunk_Stream& xStream);
		virtual	void	WriteToChunkStream			(Asura_Chunk_Stream& xStream) const;

		Asura_Vector_3 m_xPosition;
		Asura_Matrix_3x3 m_xOrientation;


		friend class Tools_Entity_ActivatableWithPosition_Object;

	private:
		static Asura_Hashed_List<Asura_ServerEntity_ActivatableWithPosition> s_xList;

};


#endif //__ASURA_SERVERENTITY_ACTIVATABLE_H__
