void CHudChat::OnTick( void )
{
	if ( GEMPRules() && m_IScheme && !m_nMessageMode )
	{
		if ( GEMPRules()->IsIntermission() && !m_bIntermissionSet )
		{
			m_bIntermissionSet = true;
			ApplySchemeSettings( m_IScheme );
		}
		else if ( !GEMPRules()->IsIntermission() && m_bIntermissionSet )
		{
			m_bIntermissionSet = false;
			// Reset our view after intermission is over
			ApplySchemeSettings( m_IScheme );
		}
	}

	BaseClass::OnTick();
}
void CGEStats::SetAwardsInEvent( IGameEvent *pEvent )
{   
	// Ignore awards when only 1 person is playing
	if ( GEMPRules()->GetNumActivePlayers() < 2 )
		return;

	CUtlVector<GEStatSort*> vAwards;
	GEStatSort *award;
	int i;

	// Prevent divide by zero
	if ( m_iRoundCount == 0 )
		m_iRoundCount = 1;

	for ( i=0; i < GE_AWARD_GIVEMAX; i++ )
	{
		// Check for valid award
		if ( AwardIDToIdent(i) )
		{
			award = new GEStatSort;
			// See if we are going to give this one out, if so store it, if not erase the dummy var
			if ( GetAwardWinner(i, *award) )
				vAwards.AddToTail( award );
			else
				delete award;
		}
	}

	// Sort our ratios from High to Low
	vAwards.Sort( &CGEStats::StatSortHigh );

	char eventid[16], eventwinner[16];
	CBaseEntity *pPlayer;
	for ( i=0; i < vAwards.Count(); i++ )
	{
		// Never give out more than 6 awards
		if ( i == 6 )
			break;

		pPlayer = m_pPlayerStats[ vAwards[i]->idx ]->GetPlayer();
		if ( !pPlayer )
			continue;

		Q_snprintf( eventid, 16, "award%i_id", i+1 );
		Q_snprintf( eventwinner, 16, "award%i_winner", i+1 );

		pEvent->SetInt( eventid, vAwards[i]->m_iAward );
		pEvent->SetInt( eventwinner, pPlayer->entindex() );
	}

	vAwards.PurgeAndDeleteElements();
}
	virtual void Event_EntityKilled( CBaseEntity *pVictim, CBaseEntity *pAttacker, CBaseEntity *pInflictor, IGameEvent *event )
	{
		if ( GEMPRules()->IsIntermission() )
			IncrementCount();
	}
void CGEPropDynamic::PickNewSkin(bool broken)
{
	// Basically a way of making sure each entity gets a unique skin.
	// We multiply the different coordinates with different coefficients in order to prevent certain arrangements from generating the same seed.
	// aka (-2, 0, 0) is the same as (-1, -1, 0) and many more.  Overlap is obviously still possible with this system too, but there are only a few
	// circumstances where the overlap would occour in locations near eachother.
	RandomSeed((int)(GetAbsOrigin().x * 100 + GetAbsOrigin().y * 10 + GetAbsOrigin().z) + GEMPRules()->GetRandomSeedOffset() + gpGlobals->curtime);

	int healthyskins = GetModelPtr()->numskinfamilies() - m_iBrokenSkinCount;

	if (broken)
	{
		m_nSkin = rand() % m_iBrokenSkinCount;
		m_bUsingBrokenSkin = true;
	}
	else
	{
		m_nSkin = m_iBrokenSkinCount + rand() % healthyskins;
		m_bUsingBrokenSkin = false;
	}
}
bool CGEStats::GetAwardWinner( int iAward, GEStatSort &winner )
{
	CUtlVector<GEStatSort*> stats;
	if ( GEMPRules()->GetNumActivePlayers() < 2 )
		return false;

	// Reset any previous inclinations
	winner.m_iAward = -1;
	winner.m_iStat = 0;

	CGEPlayer *pPlayer = NULL;
	// First parse out all the award to player information
	for ( int i=0; i < m_pPlayerStats.Count(); i++ )
	{
		// Don't try to give awards to invalid players
		pPlayer = m_pPlayerStats[i]->GetPlayer();
		if ( !pPlayer )
			continue;
		if ( pPlayer->GetTeamNumber() == TEAM_SPECTATOR )
			continue;

		GEStatSort *entry = new GEStatSort;
		entry->idx = i;

		// If this is the last report, then take the match stats, average them over the rounds, and use that as our stat
		if ( GEGameplay()->IsInFinalIntermission() )
			entry->m_iStat = m_pPlayerStats[i]->GetMatchStat( iAward ) / m_iRoundCount;
		else
			entry->m_iStat = m_pPlayerStats[i]->GetRoundStat( iAward );

		stats.AddToTail( entry );
	}

	// Make sure after our checks we have at least two players to do awards for
	if ( stats.Count() < 2 )
		return false;

	float playerrat = (float)(stats.Count() + GE_STATS_PLAYERRATIO) / (float)stats.Count();

	if ( GetAwardSort(iAward) == GE_SORT_HIGH )
		stats.Sort( &CGEStats::StatSortHigh );
	else
	{
		stats.Sort( &CGEStats::StatSortLow );
		// Since we are sorting low we have to invert our player ratio
		playerrat = 1 / playerrat;
	}


	// Prevent divide by zero and inflation
	if ( stats[1]->m_iStat == 0 )
		stats[1]->m_iStat = 1;

	float statrat = (float)stats[0]->m_iStat / (float)stats[1]->m_iStat;
	
	// Low sort should have a ratio less than the inverted playerrat
	if ( GetAwardSort(iAward) == GE_SORT_HIGH ? statrat > playerrat : statrat < playerrat )
	{
		// Prevent divide by zero
		if ( statrat == 0 )
			statrat = 1;

		winner.idx = stats[0]->idx;
		winner.m_iStat = (GetAwardSort(iAward) == GE_SORT_HIGH ? statrat : 1/statrat) * 1000;  // 3 decimal precision
		winner.m_iAward = iAward;

		stats.PurgeAndDeleteElements();
		return true;
	}

	stats.PurgeAndDeleteElements();
	return false;
}