//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudTimeStatus::OnThink()
{
	if ( m_flNextThink < gpGlobals->curtime )
	{
		CTeamRoundTimer *pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_iTimerIndex ) );
		// get the time remaining (in seconds)
		if ( pTimer )
		{
			int nTotalTime = pTimer->GetTimerMaxLength();
			int nTimeRemaining = pTimer->GetTimeRemaining();

			if ( m_pTimeValue && m_pTimeValue->IsVisible() )
			{
				// set our label
				int nMinutes = 0;
				int nSeconds = 0;
				char temp[256];

				if ( nTimeRemaining <= 0 )
				{
					nMinutes = 0;
					nSeconds = 0;
				}
				else
				{
					nMinutes = nTimeRemaining / 60;
					nSeconds = nTimeRemaining % 60;
				}				

				Q_snprintf( temp, sizeof( temp ), "%d:%02d", nMinutes, nSeconds );
				m_pTimeValue->SetText( temp );
			}
	
			// let the progress bar know the percentage of time that's passed ( 0.0 -> 1.0 )
			if ( m_pProgressBar && m_pProgressBar->IsVisible() )
			{
				m_pProgressBar->SetPercentage( ( (float)nTotalTime - nTimeRemaining ) / (float)nTotalTime );
			}
		}

		m_flNextThink = gpGlobals->curtime + 0.1f;
	}
}
//-----------------------------------------------------------------------------
// Purpose: Check that the points aren't all held by one team if they are 
//			this will reset the round and will reset all the points
//-----------------------------------------------------------------------------
int CTeamControlPointRound::CheckWinConditions( void )
{
	int iWinners = TeamOwnsAllPoints();
	if ( ( m_iInvalidCapWinner != 1 ) &&
		 ( iWinners >= FIRST_GAME_TEAM ) && 
		 ( iWinners != m_iInvalidCapWinner ) )
	{
		bool bWinner = true;

#if defined( TF_DLL)
		if ( TFGameRules() && TFGameRules()->IsInKothMode() )
		{
			CTeamRoundTimer *pTimer = NULL;
			if ( iWinners == TF_TEAM_RED )
			{
				pTimer = TFGameRules()->GetRedKothRoundTimer();
			}
			else if ( iWinners == TF_TEAM_BLUE )
			{
				pTimer = TFGameRules()->GetBlueKothRoundTimer();
			}

			if ( pTimer )
			{
				if ( pTimer->GetTimeRemaining() > 0 || TFGameRules()->TimerMayExpire() == false )
				{
					bWinner = false;
				}
			}
		}
#endif
		if ( bWinner )
		{
			FireTeamWinOutput( iWinners );
			return iWinners;
		}
	}

	return -1;
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamRoundTimer::SendTimeWarning( int nWarning )
{
	// don't play sounds if the level designer has turned them off or if it's during the WaitingForPlayers time
	if ( !m_bTimerPaused && m_bAutoCountdown && !TeamplayRoundBasedRules()->IsInWaitingForPlayers() )
	{
		C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
		if ( pPlayer )
		{
			if ( ObjectiveResource() )
			{
				bool bShouldPlaySound = false;

				if ( TeamplayRoundBasedRules()->IsInTournamentMode() == true && TeamplayRoundBasedRules()->IsInStopWatch() == true )
				{
					int iActiveTimer = ObjectiveResource()->GetTimerToShowInHUD();
					int iStopWatchTimer = ObjectiveResource()->GetStopWatchTimer();

					if ( IsStopWatchTimer() == true && IsWatchingTimeStamps() == false )
					{
						CTeamRoundTimer *pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( iActiveTimer ) );

						if ( pTimer && pTimer->IsTimerPaused() == false && pTimer->GetTimeRemaining() > GetTimeRemaining() )
						{
							bShouldPlaySound = true;
						}
					}
					else
					{
						CTeamRoundTimer *pStopWatch = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( iStopWatchTimer ) );

						if ( ObjectiveResource()->GetTimerToShowInHUD() == entindex()  )
						{
							if ( pStopWatch )
							{
								if ( pStopWatch->IsTimerPaused() == true )
								{
									bShouldPlaySound = true;
								}

								if ( pStopWatch->GetTimeRemaining() > GetTimeRemaining() && pStopWatch->IsWatchingTimeStamps() == false )
								{
									bShouldPlaySound = true;
								}

								if ( pStopWatch->IsWatchingTimeStamps() == true )
								{
									bShouldPlaySound = true;
								}
							}
							else
							{
								bShouldPlaySound = true;
							}
						}
					}
				}
				else 
				{
					if( ObjectiveResource()->GetTimerToShowInHUD() == entindex())
					{
						bShouldPlaySound = true;
					}
				}

				if ( bShouldPlaySound == true )
				{
					pPlayer->EmitSound( GetTimeWarningSound( nWarning ) );
				}
			}
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudTimeStatus::SetExtraTimePanels()
{
	if ( !TFGameRules() )
		return;

	CTeamRoundTimer *pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_iTimerIndex ) );
	if ( !pTimer )
		return;

	if ( m_pSetupLabel )
	{
		// get the time remaining (in seconds)
		if ( pTimer )
		{
			bool bInSetup = TFGameRules()->InSetup();

			if ( m_pSetupBG )
				m_pSetupBG->SetVisible( bInSetup );

			m_pSetupLabel->SetVisible( bInSetup );
		}
	}

	// Set the Sudden Death panels to be visible
	if ( m_pSuddenDeathLabel )
	{
		bool bInSD = TFGameRules()->InStalemate();

		if ( m_pSuddenDeathBG )
			m_pSuddenDeathBG->SetVisible( bInSD );

		m_pSuddenDeathLabel->SetVisible( bInSD );
	}

	if ( m_pOvertimeLabel )
	{
		bool bInOver = TFGameRules()->InOvertime();

		if ( TFGameRules()->IsInKothMode() )
		{
			bInOver = pTimer->GetTimeRemaining() <= 0.0f;
		}

		if ( bInOver )
		{
			if ( m_pOvertimeBG && !m_pOvertimeBG->IsVisible() )
			{
				m_pOvertimeLabel->SetAlpha( 0 );
				m_pOvertimeBG->SetAlpha( 0 );

				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( this, "OvertimeShow" ); 

				// need to turn off the SuddenDeath images if they're on
				if ( m_pSuddenDeathBG )
					m_pSuddenDeathBG->SetVisible( false );

				m_pSuddenDeathLabel->SetVisible( false );
			}

			if ( m_pOvertimeBG )
				m_pOvertimeBG->SetVisible( true );

			m_pOvertimeLabel->SetVisible( true );

			CheckClockLabelLength( m_pOvertimeLabel, m_pOvertimeBG );
		}
		else
		{
			if ( m_pOvertimeBG )
				m_pOvertimeBG->SetVisible( false );

			m_pOvertimeLabel->SetVisible( false );
		}
	}

	if ( m_pWaitingForPlayersLabel )
	{
		bool bInWaitingForPlayers = TFGameRules()->IsInWaitingForPlayers();

		m_pWaitingForPlayersLabel->SetVisible( bInWaitingForPlayers );

		if ( m_pWaitingForPlayersBG )
			m_pWaitingForPlayersBG->SetVisible( bInWaitingForPlayers );

		if ( bInWaitingForPlayers )
		{
			// can't be waiting for players *AND* in setup at the same time

			if ( m_pSetupLabel )
				m_pSetupLabel->SetVisible( false );

			if ( m_pSetupBG )
				m_pSetupBG->SetVisible( false );

			CheckClockLabelLength( m_pWaitingForPlayersLabel, m_pWaitingForPlayersBG );
		}
	}
}