//
// Asura_ServerEntity_EffectVolume.cpp
//
// Created by Emil Popovic - 15/12/2008
// 
// Copyright (C) 2008 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Entity/Triggers/EffectVolume/Asura_ServerEntity_EffectVolume.h"

#include "Debugging/Asura_Assert.h"
#include "Entity/Asura_Entity_Classification.h"
#include "Network/Asura_Network.h"
#include "Network/Asura_Message.h"



////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Version	Author	Comment
// ~~~~~~~	~~~~~~	~~~~~~~
// 1		EP		Created.
////////////////////////////////////////////////////////////////////////////////
const u_int	Asura_ServerEntity_EffectVolume::s_uChunkVersion	= 1;



Asura_Hashed_List<Asura_ServerEntity_EffectVolume> Asura_ServerEntity_EffectVolume::s_xList;



Asura_ServerEntity_EffectVolume::Asura_ServerEntity_EffectVolume( Asura_Guid uGuid)
: PARENT( uGuid )
{
	Classification = AsuraEntityClass_EffectVolume;

	s_xList.AddToList(this);
	SetNeverUpdateClient();
}

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


void Asura_ServerEntity_EffectVolume::MakeCreateMessage(Asura_Guid uRequestingClient, bool bSendGuaranteed )
{
	Asura_Chunk_Memory_Stream xStream;

#ifdef ASURA_TOOLS
	xStream.SetSwapEndian(false);
#endif

	WriteToClientCreateStream(xStream);

	u_int uSize = xStream.GetPosition();

	u_short uMsgID;
#ifdef ASURA_ALLOW_INACTIVE_CLIENT_ENTITIES
	if ( !IsEntityActive() )
	{
		uMsgID = ASURA_MSG_CLIENTCREATEINACTIVE;
	}
	else
#endif //#ifdef ASURA_ALLOW_INACTIVE_CLIENT_ENTITIES
	{
		uMsgID = ASURA_MSG_CLIENTCREATE;
	}

	Asura_Message* pxMessage = Asura_Network::CreateMessage(uMsgID, uSize, GetClassification(), xStream.GetStartAddress());
	if(pxMessage)
	{
		pxMessage->SetToGuid( ASURA_GUID_MY_CLIENT_ENTITIES );
		pxMessage->SetFromGuid( GetGuid() );
		if (bSendGuaranteed)
		{
			pxMessage->UseGuaranteedDelivery();
		}
		Asura_Network::SendToSpecificMachine( pxMessage, uRequestingClient );
	}
}


void Asura_ServerEntity_EffectVolume::CreateFromChunkStream( const Asura_Guid uGuid, Asura_Chunk_Stream& xStream )
{
	Asura_ServerEntity_EffectVolume* pxTT = new Asura_ServerEntity_EffectVolume( uGuid );
	Asura_Assert( pxTT, "Failed to allocate memory for new ServerEntity_EffectVolume object." );

	if ( pxTT )
	{
		pxTT->ReadFromChunkStream( xStream );
	}
}


void Asura_ServerEntity_EffectVolume::ReadFromChunkStream(Asura_Chunk_Stream& xStream)
{
	//check our version
	u_int uVersion;
	xStream >> uVersion;

	if( uVersion>s_uChunkVersion )
	{
		Asura_Assertf(false,("Unknown chunk version %d for Server EffectVolume entity",uVersion));
		
		// Don't continue to read the if chunk is somehow corrupted.
		return;
	}

	//read parent
	PARENT::ReadFromChunkStream(xStream);


	m_xData.ReadFromChunkStream(xStream);

	SetNeverUpdates(true);
}

void Asura_ServerEntity_EffectVolume::WriteToChunkStream(Asura_Chunk_Stream& xStream) const
{
	//save our version
	xStream << s_uChunkVersion;

	//save the parent
	PARENT::WriteToChunkStream(xStream);

	m_xData.WriteToChunkStream(xStream);
}

void Asura_ServerEntity_EffectVolume::WriteToClientCreateStream(Asura_Chunk_Memory_Stream& xStream) const
{
	m_xData.WriteToChunkStream(xStream);
}



Asura_EffectVolume_Effect* Asura_ServerEntity_EffectVolume::AddEffect( int iEffectType )
{
	Asura_EffectVolume_Effect*	pxNewEffect = NULL;
	
	switch( iEffectType )
	{
	case uEFFECTVOLUME_EFFECT_FOG:
		{
			pxNewEffect = new Asura_EffectVolume_Effect_Fog();
			break;
		}

#if defined(ASURA_USE_FSFX)
	case uEFFECTVOLUME_EFFECT_FSFX:
		{
			pxNewEffect = new Asura_EffectVolume_Effect_FSFX();
			break;
		}
#endif	//defined(ASURA_USE_FSFX)

	default:
		{
			Asura_Assert( false, "Unknown type of effect to add in Asura_ServerEntity_EffectVolume::AddEffect." );
		}
	}

	if( pxNewEffect)
	{
		m_xData.AddNewEffect( pxNewEffect );
	}

	return pxNewEffect;
}


void Asura_ServerEntity_EffectVolume::RemoveEffect( u_int uNumOfEffectToRemove )
{
	m_xData.RemoveEffect( uNumOfEffectToRemove );
}

Asura_EffectVolume_Effect* Asura_ServerEntity_EffectVolume::GetEffect( u_int uNumOfEffectToGet )
{
	return m_xData.GetEffect( uNumOfEffectToGet );
}

u_int Asura_ServerEntity_EffectVolume::GetNumOfEffects()
{
	return m_xData.GetNumOfEffects();
}



#ifdef ASURA_TOOLS
void Asura_ServerEntity_EffectVolume::ApplyChangesForTemplateInstance(class Tools_EntityTemplateInstance* pxInstance)
{
	PARENT::ApplyChangesForTemplateInstance(pxInstance);

	m_xData.ApplyChangesForTemplateInstance(pxInstance);
}

bool Asura_ServerEntity_EffectVolume::CanBeIncludedInTemplate() const
{
	return Classification == AsuraEntityClass_EffectVolume; 
}

#endif
