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

#include "cbase.h"
#include "hl2wars/c_rts_basebuilding.h"
#include "hl2wars/c_rts_globalresource.h"
#include "hl2wars/clientmode_hl2warsnormal.h"

// ##################################################################################
//	>> Selection helper implementation
//
// ##################################################################################
class C_SelectionHelper : public C_MouseRTSModel
{
public:
	DECLARE_CLASS( C_SelectionHelper , C_MouseRTSModel );
	DECLARE_CLIENTCLASS();

	void OnCursorEntered()
	{
		if( GetOwnerEntity() && GetOwnerEntity()->IsRTSModel() )
		{
			C_MouseRTSModel *pRTSModel = assert_cast<C_MouseRTSModel *>( GetOwnerEntity() );
			if( pRTSModel )
				pRTSModel->OnCursorEntered();
		}
	}

	void OnCursorHover()
	{
		if( GetOwnerEntity() && GetOwnerEntity()->IsRTSModel() )
		{
			C_MouseRTSModel *pRTSModel = assert_cast<C_MouseRTSModel *>( GetOwnerEntity() );
			if( pRTSModel )
				pRTSModel->OnCursorHover();
		}
	}

	void OnCursorExited()
	{
		if( GetOwnerEntity() && GetOwnerEntity()->IsRTSModel() )
		{
			C_MouseRTSModel *pRTSModel = assert_cast<C_MouseRTSModel *>( GetOwnerEntity() );
			if( pRTSModel )
				pRTSModel->OnCursorExited();
		}
	}
};

IMPLEMENT_CLIENTCLASS_DT( C_SelectionHelper, DT_SelectionHelper, CSelectionHelper )
END_RECV_TABLE()

// ##################################################################################
//	>> Building model implementation
//
// ##################################################################################
IMPLEMENT_CLIENTCLASS_DT( C_BaseBuildingModel, DT_BaseBuildingModel, CBaseBuildingModel )
	// construction info
	RecvPropInt		(RECVINFO(m_iHealth)),
	RecvPropInt		(RECVINFO(m_iMaxHealth)),
	RecvPropInt		(RECVINFO(m_iConstructionState)),
	RecvPropInt		(RECVINFO(m_iConstructProgress)),

	// receive normal stuff
	RecvPropInt( RECVINFO( m_iOwnerShip ) ),
	RecvPropVector	(RECVINFO(m_vOwnerShipIconTarget)),
	RecvPropFloat( RECVINFO( m_fViewDistance ) ),
	RecvPropString( RECVINFO(m_iszNetIcon) ),
END_RECV_TABLE()


C_EntityClassList<C_BaseBuildingModel> g_BuildingModelList;
C_BaseBuildingModel *C_EntityClassList<C_BaseBuildingModel>::m_pClassList = NULL;
C_BaseBuildingModel *GetBuildingModelList()
{
	return g_BuildingModelList.m_pClassList;
}

C_BaseBuildingModel::C_BaseBuildingModel() 
{
	g_BuildingModelList.Insert( this );

	SetRenderMode(kRenderTransTexture);
	SetRenderColorA( 0 );
}

C_BaseBuildingModel::~C_BaseBuildingModel() 
{
	g_BuildingModelList.Remove( this );
}

void C_BaseBuildingModel::UpdateOnRemove( void )
{
	if( g_RTSR )
	{
		changefogofwar_t update;
		update.delay = gpGlobals->curtime + 3.5f;
		update.position = Vector2D( GetAbsOrigin().x, GetAbsOrigin().y );
		update.radius = GetViewDistance();
		update.type = FOW_ADDREMOVE;
		g_RTSR->m_utlFogInfo.AddToTail( update );
	}

	BaseClass::UpdateOnRemove();
}

void C_BaseBuildingModel::Spawn() 
{
	BaseClass::Spawn();
}

//-----------------------------------------------------------------------------
// Purpose:  Called when the mouse starts hovering at the building
//-----------------------------------------------------------------------------
void C_BaseBuildingModel::OnCursorEntered()
{
	if( !IsSelected() )
		SetDrawSelection( true );
}

//-----------------------------------------------------------------------------
// Purpose:  Called when the mouse is hovering at the building
//-----------------------------------------------------------------------------
void C_BaseBuildingModel::OnCursorHover()
{
	GetClientModeHL2WarsNormal()->m_pHL2WarsViewport->DrawHealthBar( this );
}

void C_BaseBuildingModel::SetSelected( bool selected )
{
	m_bSelected = selected;
	SetDrawSelection(m_bSelected);
}

color32 C_BaseBuildingModel::GetColor()
{
	Vector color = g_RTSR->GetColor255( GetOwnerShip() );
	color32 color32clr = { color.x, color.y, color.z, 200 };
	return color32clr;
}

//-----------------------------------------------------------------------------
// Purpose:  Make the building fade in when being constructed
//-----------------------------------------------------------------------------
void C_BaseBuildingModel::ClientThink( void )
{
	BaseClass::ClientThink();

	SetRenderMode(kRenderTransTexture);

	if( m_iConstructionState == BS_CONSTRUCTED )
	{
		SetRenderMode(kRenderNormal);
		return;	// stop thinking
	}

	SetRenderColorA( 40 + (int)((m_iConstructProgress / 100.0f)*215) );
	SetNextClientThink(gpGlobals->curtime + 0.2);
}

//-----------------------------------------------------------------------------
// Purpose:  Check our construction state
//-----------------------------------------------------------------------------
void C_BaseBuildingModel::OnDataChanged( DataUpdateType_t type )
{
	BaseClass::OnDataChanged( type );

	if( m_iConstructionState == BS_UNDERCONSTRUCTION )
	{
		SetRenderMode(kRenderTransTexture);
		SetNextClientThink(gpGlobals->curtime + 0.2);
	}
	else if( m_iConstructionState == BS_PRECONSTRUCTION )
	{
		SetRenderMode(kRenderTransTexture);
		SetRenderColorA( 40 );
	}
	else
	{
		SetRenderMode(kRenderNormal);
	}
}

// ##################################################################################
//	>> Building model component implementation
//
// ##################################################################################
IMPLEMENT_CLIENTCLASS_DT( C_BuildingComponent, DT_BuildingComponent, CBuildingComponent )
	RecvPropEHandle	( RECVINFO( m_hOuter)),
END_RECV_TABLE()

C_BuildingComponent::C_BuildingComponent()
{

}

void C_BuildingComponent::Spawn( void )
{
	BaseClass::Spawn();

	SetNextClientThink(gpGlobals->curtime + 0.2);
}

void C_BuildingComponent::ClientThink( void )
{
	BaseClass::ClientThink();

	if( !GetOuter() )
		return;

	SetRenderMode(kRenderTransTexture);

	if( !GetOuter()->IsUnderConstruction() )
	{
		SetRenderMode(kRenderNormal);
		return;	// stop thinking
	}

	SetRenderColorA( 40 + (int)((GetOuter()->GetConstructionProgress() / 100.0f)*215) );
	SetNextClientThink(gpGlobals->curtime + 0.2);
}

