bool CHudMenuEngyBuild::SendDestroyMessage( int iSlot )
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pLocalPlayer )
		return false;

	bool bSuccess = false;

	int iBuilding = GetBuildingIDFromSlot( iSlot );

	C_BaseObject *pObj = pLocalPlayer->GetObjectOfType( iBuilding );

	if ( pObj != NULL )
	{
		char szCmd[128];
		Q_snprintf( szCmd, sizeof(szCmd), "destroy %d", iBuilding );
		engine->ClientCmd( szCmd );
		bSuccess = true; 
	}
	else
	{
		pLocalPlayer->EmitSound( "Player.DenyWeaponSelection" );
	}

	return bSuccess;
}
void CHudBuildingStatusContainer::OnBuildingChanged( int iBuildingType, bool bBuildingIsDead )
{
	bool bFound = false;
	for ( int i = 0; i < m_BuildingPanels.Count() && !bFound; i++ )
	{
		CBuildingStatusItem *pItem = m_BuildingPanels.Element(i);

		if ( pItem && pItem->GetRepresentativeObjectType() == iBuildingType )
		{
			// find the item that represents this building type
			C_BaseObject *pObj = NULL;

			// find the object
			C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
			if ( pLocalPlayer )
			{
				pObj = pLocalPlayer->GetObjectOfType( iBuildingType );
				pItem->SetObject( pObj );
			}

			pItem->InvalidateLayout( true );
			bFound = true;

			RecalculateAlertState();
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudBuildingStatusContainer::UpdateAllBuildings( void )
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pLocalPlayer )
		return;

	for ( int i = 0; i < m_BuildingPanels.Count(); i++ )
	{
		CBuildingStatusItem *pItem = m_BuildingPanels.Element(i);

		if ( pItem )
		{
			// find the item that represents this building type
			C_BaseObject *pObj = NULL;

			// find the object
			pObj = pLocalPlayer->GetObjectOfType( pItem->GetRepresentativeObjectType() );

			pItem->SetObject( pObj );

			pItem->InvalidateLayout( true );
			RecalculateAlertState();
		}
	}
}
void CHudMenuEngyBuild::SetVisible( bool state )
{
	if ( state == true )
	{
		// close the weapon selection menu
		engine->ClientCmd( "cancelselect" );

		bool bConsoleMode = ( IsConsole() || tf_build_menu_controller_mode.GetBool() );

		if ( bConsoleMode != m_bInConsoleMode )
		{
			InvalidateLayout( true, true );
			m_bInConsoleMode = bConsoleMode;
		}

		// set the %lastinv% dialog var to our binding
		const char *key = engine->Key_LookupBinding( "lastinv" );
		if ( !key )
		{
			key = "< not bound >";
		}

		SetDialogVariable( "lastinv", key );

		// Set selection to the first available building that we can build

		C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();

		if ( !pLocalPlayer )
			return;

		int iDefaultSlot = 1;

		// Find the first slot that represents a building that we haven't built
		int iSlot;
		for ( iSlot = 1; iSlot <= 4; iSlot++ )
		{
			int iBuilding = GetBuildingIDFromSlot( iSlot );
			C_BaseObject *pObj = pLocalPlayer->GetObjectOfType( iBuilding );

			if ( pObj == NULL )
			{
				iDefaultSlot = iSlot;
				break;
			}
		}

		m_iSelectedItem = -1;	//force redo
		SetSelectedItem( iDefaultSlot );

		HideLowerPriorityHudElementsInGroup( "mid" );
	}
	else
	{
		UnhideLowerPriorityHudElementsInGroup( "mid" );
	}

	BaseClass::SetVisible( state );
}
void CHudMenuEngyBuild::OnTick( void )
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pLocalPlayer )
		return;

	int iAccount = pLocalPlayer->GetAmmoCount( TF_AMMO_METAL );

	for ( int i=0;i<4; i++ )
	{
		int iRemappedObjectID = GetBuildingIDFromSlot( i + 1 );

		// update this slot
		C_BaseObject *pObj = NULL;

		if ( pLocalPlayer )
		{
			pObj = pLocalPlayer->GetObjectOfType( iRemappedObjectID );
		}			

		m_pAvailableObjects[i]->SetVisible( false );
		m_pAlreadyBuiltObjects[i]->SetVisible( false );
		m_pCantAffordObjects[i]->SetVisible( false );

		// If the building is already built
		if ( pObj != NULL && !pObj->IsPlacing() )
		{
			m_pAlreadyBuiltObjects[i]->SetVisible( true );
		}
		// See if we can afford it
		else if ( iAccount < GetObjectInfo( iRemappedObjectID )->m_Cost )
		{
			m_pCantAffordObjects[i]->SetVisible( true );
		}
		else
		{
			// we can buy it
			m_pAvailableObjects[i]->SetVisible( true );
		}
	}
}
void CHudMenuEngyBuild::UpdateHintLabels( void )
{
	// hilight the action we can perform ( build or destroy or neither )
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( pLocalPlayer )
	{
		int iBuilding = GetBuildingIDFromSlot( m_iSelectedItem );
		C_BaseObject *pObj = pLocalPlayer->GetObjectOfType( iBuilding );

		bool bDestroyLabelBright = false;
		bool bBuildLabelBright = false;

		if ( pObj )
		{
			// hilight destroy, we have a building
			bDestroyLabelBright = true;
		}
		else if ( pLocalPlayer->GetAmmoCount( TF_AMMO_METAL ) >= GetObjectInfo( iBuilding )->m_Cost )	// I can afford it
		{
			// hilight build, we can build this
			bBuildLabelBright = true;
		}
		else
		{
			// dim both, do nothing
		}

		if ( m_pDestroyLabelBright && m_pDestroyLabelDim && m_pBuildLabelBright && m_pBuildLabelDim )
		{
			m_pDestroyLabelBright->SetVisible( bDestroyLabelBright );
			m_pDestroyLabelDim->SetVisible( !bDestroyLabelBright );

			m_pBuildLabelBright->SetVisible( bBuildLabelBright );
			m_pBuildLabelDim->SetVisible( !bBuildLabelBright );
		}
	}
}
void CHudMenuEngyBuild::SendBuildMessage( int iSlot )
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pLocalPlayer )
		return;

	int iBuilding = GetBuildingIDFromSlot( iSlot );

	C_BaseObject *pObj = pLocalPlayer->GetObjectOfType( iBuilding );
	int iCost = GetObjectInfo( iBuilding )->m_Cost;

	if ( pObj == NULL && pLocalPlayer->GetAmmoCount( TF_AMMO_METAL ) >= iCost )
	{
		char szCmd[128];
		Q_snprintf( szCmd, sizeof(szCmd), "build %d", iBuilding );
		engine->ClientCmd( szCmd );
	}
	else
	{
		pLocalPlayer->EmitSound( "Player.DenyWeaponSelection" );
	}
}