//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "basecombatcharacter.h"
#include "hl2wars/util_hl2wars.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define SF_MESSAGE_ONCE			0x0001		// Fade in, not out

enum receiver_type_t
{
	ACTIVATOR_TEAMID = 0,
	TEAMID,
	ACTIVATOR,
};

class CRTSInfoMessage : public CPointEntity
{
public:
	DECLARE_CLASS( CRTSInfoMessage, CPointEntity );

	void	Spawn( void );

	inline void SetSource( string_t iszSource ) { m_iszSource = iszSource; }
	inline void SetMessage( string_t iszMessage ) { m_iszMessage = iszMessage; }

private:

	void InputShowMessage( inputdata_t &inputdata );

	string_t m_iszSource;		// The one that says the message
	string_t m_iszMessage;		// Message to display.

	float m_fExpireTime;
	int m_iTeamID;
	int m_iReceiverType;

	DECLARE_DATADESC();

	COutputEvent m_OnShowMessage;
};

LINK_ENTITY_TO_CLASS( env_rts_infomessage, CRTSInfoMessage );

BEGIN_DATADESC( CRTSInfoMessage )

	DEFINE_KEYFIELD( m_iszSource, FIELD_STRING, "source" ),
	DEFINE_KEYFIELD( m_iszMessage, FIELD_STRING, "message" ),
	DEFINE_KEYFIELD( m_iReceiverType, FIELD_INTEGER, "receiver_type" ),
	DEFINE_KEYFIELD( m_iTeamID, FIELD_INTEGER, "team_id" ),
	DEFINE_KEYFIELD( m_fExpireTime, FIELD_FLOAT, "expire_time" ),

	DEFINE_INPUTFUNC( FIELD_VOID, "ShowMessage", InputShowMessage ),

	DEFINE_OUTPUT(m_OnShowMessage, "OnShowMessage"),

END_DATADESC()



//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CRTSInfoMessage::Spawn( void )
{
	SetSolid( SOLID_NONE );
	SetMoveType( MOVETYPE_NONE );
}

//-----------------------------------------------------------------------------
// Purpose: Input handler for showing the message and/or playing the sound.
//-----------------------------------------------------------------------------
void CRTSInfoMessage::InputShowMessage( inputdata_t &inputdata )
{
	switch( m_iReceiverType )
	{
	case ACTIVATOR_TEAMID:
		{
			CBaseCombatCharacter *pReceiver = NULL;
			if ( inputdata.pActivator && ( inputdata.pActivator->IsPlayer() || inputdata.pActivator->IsNPC() ) )
				pReceiver = dynamic_cast<CBaseCombatCharacter *>(inputdata.pActivator);
			
			if( !pReceiver )
				return;

			UTIL_HudInfoText( STRING( m_iszSource ), STRING( m_iszMessage ), pReceiver->GetTeamRTSID(), m_fExpireTime );
			break;
		}
	case ACTIVATOR:
		{
			if( inputdata.pActivator && ( inputdata.pActivator->IsPlayer() ) )
				UTIL_HudInfoText( ToBasePlayer( inputdata.pActivator ), STRING( m_iszSource ), STRING( m_iszMessage ), m_fExpireTime );
			else
				return;
			break;
		}
	case TEAMID:
		{
			UTIL_HudInfoText( STRING( m_iszSource ), STRING( m_iszMessage ), m_iTeamID, m_fExpireTime );
			break;
		}
	default:
		break;
	}

	if ( m_spawnflags & SF_MESSAGE_ONCE )
	{
		UTIL_Remove( this );
	}

	m_OnShowMessage.FireOutput( inputdata.pActivator, this );
}
