//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Entity which alters the relationships between entities via entity I/O
//
//=====================================================================================//

#include "cbase.h"
#include "ndebugoverlay.h"
//#include "ai_basenpc.h"
#include "hl2wars/rts_globalresource.h"
#include "hl2wars/util_hl2wars.h"
#include "hl2wars/util_hl2wars_shared.h"

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

//=========================================================
//=========================================================
class CAI_RTSRelationship : public CBaseEntity
{
	DECLARE_CLASS( CAI_RTSRelationship, CBaseEntity );

public:
	CAI_RTSRelationship() { }

	void Spawn();
	void Activate();

	void SetActive( bool bActive );
	void ChangeRelationships( int disposition);

	void ApplyRelationship( );

private:

	void	ApplyRelationshipThink( void );

	int			m_iTeamID1;
	int			m_iTeamID2;

	int			m_iDisposition;
	bool		m_fStartActive;
	bool		m_bIsActive;
	bool		m_bReciprocal;

public:
	// Input functions
	void InputApplyRelationship( inputdata_t &inputdata );

	DECLARE_DATADESC();
};

LINK_ENTITY_TO_CLASS( ai_rts_relationship, CAI_RTSRelationship );

BEGIN_DATADESC( CAI_RTSRelationship )
	DEFINE_THINKFUNC( ApplyRelationshipThink ),

	DEFINE_KEYFIELD( m_iTeamID1, FIELD_INTEGER, "teamid1" ),
	DEFINE_KEYFIELD( m_iTeamID2, FIELD_INTEGER, "teamid2" ),
	
	DEFINE_KEYFIELD( m_iDisposition, FIELD_INTEGER, "disposition" ),
	DEFINE_KEYFIELD( m_fStartActive, FIELD_BOOLEAN, "StartActive" ),
	DEFINE_FIELD( m_bIsActive, FIELD_BOOLEAN ),
	DEFINE_KEYFIELD( m_bReciprocal, FIELD_BOOLEAN, "reciprocal" ),

	// Inputs
	DEFINE_INPUTFUNC( FIELD_VOID, "ApplyRelationship", InputApplyRelationship ),
END_DATADESC()


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CAI_RTSRelationship::Spawn()
{
	m_bIsActive = false;

	if( !UTIL_IsValidTeam( m_iTeamID1, false ) || !UTIL_IsValidTeam( m_iTeamID2, false ) )
	{
		DevWarning("ai_rts_relationship with invalid teams! \n");
		UTIL_Remove(this);
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
void CAI_RTSRelationship::Activate()
{
	if ( m_fStartActive )
	{
		ApplyRelationship();

		// Clear this flag so that nothing happens when the level is loaded (which calls activate again)
		m_fStartActive = false;
	}

	BaseClass::Activate();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : bActive - 
//-----------------------------------------------------------------------------
void CAI_RTSRelationship::SetActive( bool bActive )
{
	m_bIsActive = bActive;
}

//---------------------------------------------------------
//---------------------------------------------------------
void CAI_RTSRelationship::InputApplyRelationship( inputdata_t &inputdata )
{
	ApplyRelationship( );
}

//-----------------------------------------------------------------------------
// Purpose: This think function is used to wait until the player has properly
//			spawned, after all the NPCs have spawned.  Once that occurs, this
//			function terminates.
//-----------------------------------------------------------------------------
void CAI_RTSRelationship::ApplyRelationshipThink( void )
{
	// Call down to the base until the player has properly spawned
	ApplyRelationship();
}

//---------------------------------------------------------
// Purpose: Applies the desired relationships to an entity
//---------------------------------------------------------
void CAI_RTSRelationship::ApplyRelationship()
{
	if ( !m_bIsActive )
	{
		SetActive( true );
	}

	ChangeRelationships( m_iDisposition );
}

//---------------------------------------------------------
//---------------------------------------------------------
void CAI_RTSRelationship::ChangeRelationships( int disposition )
{
	g_RelationshipsRTS[m_iTeamID1 - DEFAULT_TEAMS][m_iTeamID2 - DEFAULT_TEAMS] = (Disposition_t)disposition;

	if( m_bReciprocal )
		g_RelationshipsRTS[m_iTeamID2 - DEFAULT_TEAMS][m_iTeamID1 - DEFAULT_TEAMS] = (Disposition_t)disposition;
}

