// Asura_ServerEntity_DynamicMusicTrigger.cpp
// Thomas Young - 14.08.08
// Copyright (C) 2008 Rebellion, All Rights Reserved.

#include "Core/Asura.h"

#ifdef ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Entity/Triggers/Asura_ServerEntity_DynamicMusicTrigger.h"
#include "Entity/Asura_Entity_Classification.h"
#include "Sound/Asura_Dynamic_Music_System.h"
#include "Sound/Asura_Dynamic_Music_System_Triggers.h"

#ifdef ASURA_TOOLS
#include "Entity/Tools_Entity_Data.h"
#endif //ASURA_TOOLS


// S T A T I C   M E M B E R   V A R I A B L E S /////////////////////////////

Asura_Hashed_List<Asura_ServerEntity_DynamicMusicTrigger> Asura_ServerEntity_DynamicMusicTrigger::s_xList;

// F U N C T I O N   D E F I N I T I O N S ///////////////////////////////////

Asura_ServerEntity_DynamicMusicTrigger::Asura_ServerEntity_DynamicMusicTrigger( bool bActive ) : Asura_ServerEntity_Trigger( bActive )
{ 
	s_xList.AddToList(this);

	Classification = AsuraEntityClass_DynamicMusicTrigger;
	
	m_uTriggerHash = ASURA_HASH_ID_UNSET;
}

Asura_ServerEntity_DynamicMusicTrigger::Asura_ServerEntity_DynamicMusicTrigger	( Asura_Guid uGuid ) : Asura_ServerEntity_Trigger ( uGuid )
{
	s_xList.AddToList(this);

	Classification = AsuraEntityClass_DynamicMusicTrigger;
	
	m_uTriggerHash = ASURA_HASH_ID_UNSET;
}

Asura_ServerEntity_DynamicMusicTrigger::~Asura_ServerEntity_DynamicMusicTrigger()
{
	s_xList.RemoveFromList(this);
}

void Asura_ServerEntity_DynamicMusicTrigger::CreateFromChunkStream(Asura_Guid uGuid, Asura_Chunk_Stream& xStream)
{
	Asura_ServerEntity_DynamicMusicTrigger* pxEntity = new Asura_ServerEntity_DynamicMusicTrigger( uGuid );
	if (pxEntity)
	{
		pxEntity->ReadFromChunkStream( xStream );
	}
}

void Asura_ServerEntity_DynamicMusicTrigger::WriteToChunkStream (Asura_Chunk_Stream& xStream) const
{
	PARENT::WriteToChunkStream( xStream );

	int iVersion = Asura_Dynamic_Music_System::DYNAMIC_MUSIC_CURRENT_TRIGGER_ENTITY_VERSION;
	xStream << iVersion;

	//We just save our trigger hash, this will be used as a lookup into the stored list of triggers in the music system
	xStream << m_uTriggerHash;
}

void Asura_ServerEntity_DynamicMusicTrigger::ReadFromChunkStream (Asura_Chunk_Stream& xStream)
{
	PARENT::ReadFromChunkStream( xStream );

	int iVersion;
	xStream >> iVersion;
	Asura_Assert(iVersion <= Asura_Dynamic_Music_System::DYNAMIC_MUSIC_CURRENT_TRIGGER_ENTITY_VERSION, "Unknown dynamic music trigger chunk version, you need new code");

	// < version 2 is a GUID, which is still a unique unsigned int - so we're ok
	xStream >> m_uTriggerHash;
}

void Asura_ServerEntity_DynamicMusicTrigger::OnTrigger()
{
	#ifdef ASURA_TOOLS
	Asura_Dynamic_Music_System::ActivateTrigger ( m_uTriggerHash );
	#else
	//Send a message to all clients with the ID of my trigger
	Asura_Message* pxMessage = Asura_Network::CreateMessage( ASURA_MSG_DYNAMIC_MUSIC_TRIGGER, sizeof(Asura_Hash_ID) );
	
	pxMessage->To = ASURA_GUID_ALL_CLIENTMACHINES;
	pxMessage->From = Guid;

	Asura_MsgData_DynamicMusic_Trigger* pxData = static_cast<Asura_MsgData_DynamicMusic_Trigger*>( pxMessage->GetData() );
	pxData->m_uTriggerHash = m_uTriggerHash;

	Asura_Network::Send(pxMessage);
	#endif
}

bool Asura_ServerEntity_DynamicMusicTrigger::IsTriggered ()
{
	return false;
}

Asura_ServerEntity_DynamicMusicTrigger* Asura_ServerEntity_DynamicMusicTrigger::FindEntityByHash(Asura_Hash_ID uHash)
{
	Asura_ServerEntity_DynamicMusicTrigger* pxEntity = NULL;

	// Find entity with this hash
	for( Asura_Hashed_List_It<Asura_ServerEntity_DynamicMusicTrigger> xIt(GetList()); !xIt.Done(); xIt.Next() )
	{
		Asura_ServerEntity_DynamicMusicTrigger* pxFoundEntity = xIt.GetCurrent();
		if (pxFoundEntity->m_uTriggerHash == uHash)
		{
			pxEntity = pxFoundEntity;
			break;
		}
	}

	return pxEntity;
}

#endif //ASURA_SUPPORT_DYNAMIC_MUSIC_SYSTEM

//eof
