void Save( ISave *pSave )
	{
		CAchievementMgr *pAchievementMgr = dynamic_cast<CAchievementMgr *>( engine->GetAchievementMgr() );
		if ( !pAchievementMgr )
			return;

		// save global achievement mgr state to separate file if there have been any changes, so in case of a crash
		// the global state is consistent with last save game
		pAchievementMgr->SaveGlobalStateIfDirty();

		pSave->StartBlock( "Achievements" );
		int iTotalAchievements = pAchievementMgr->GetAchievementCount();
		short nSaveCount = 0;
		// count how many achievements should be saved. 
		for ( int i = 0; i < iTotalAchievements; i++ )
		{
			// We only save games in SP games so the assumption of SINGLE_PLAYER_SLOT is valid
			IAchievement *pAchievement = pAchievementMgr->GetAchievementByIndex( i, SINGLE_PLAYER_SLOT );
			if ( pAchievement->ShouldSaveWithGame() )
			{
				nSaveCount++;
			}
		}
		// Write # of saved achievements
		pSave->WriteShort( &nSaveCount );
		// Write out each achievement
		for ( int i = 0; i < iTotalAchievements; i++ )
		{
			// We only save games in SP games so the assumption of SINGLE_PLAYER_SLOT is valid
			IAchievement *pAchievement = pAchievementMgr->GetAchievementByIndex( i, SINGLE_PLAYER_SLOT );
			if ( pAchievement->ShouldSaveWithGame() )
			{				
				CBaseAchievement *pBaseAchievement = dynamic_cast< CBaseAchievement * >( pAchievement );
				if ( pBaseAchievement )
				{
					short iAchievementID = (short) pBaseAchievement->GetAchievementID();
					// write the achievement ID
					pSave->WriteShort( &iAchievementID );
					// write the achievement data
					pSave->WriteAll( pBaseAchievement, pBaseAchievement->GetDataDescMap() );
				}
			}
		}
		pSave->EndBlock();
	}
//-----------------------------------------------------------------------------
// Purpose: called when the local player object is being destroyed
//-----------------------------------------------------------------------------
void CTFStatPanel::OnLocalPlayerRemove( C_TFPlayer *pPlayer )
{
	// this handles the case of map change/server shutdown while player is still alive -- accumulate values for this life.
	if ( pPlayer->IsAlive() && g_AchievementMgrTF.CheckAchievementsEnabled() )
	{
		m_RoundStatsCurrentLife.m_iStat[TFSTAT_PLAYTIME] = gpGlobals->curtime - m_flTimeCurrentLifeStart;
		ClassStats_t &classStats = GetClassStats( m_iClassCurrentLife );
		classStats.AccumulateRound( m_RoundStatsCurrentLife );
		classStats.accumulated.m_iStat[TFSTAT_MAXSENTRYKILLS] = 0;	// sentry kills is a max value rather than a count, meaningless to accumulate
		m_bStatsChanged = true;
	}
}
	void Restore( IRestore *pRestore, bool createPlayers )
	{
		CAchievementMgr *pAchievementMgr = dynamic_cast<CAchievementMgr *>( engine->GetAchievementMgr() );
		if ( !pAchievementMgr )
			return;

		if ( m_fDoLoad )
		{
			pAchievementMgr->PreRestoreSavedGame();

			pRestore->StartBlock();
			// read # of achievements
			int nSavedAchievements = pRestore->ReadShort();
			
			while ( nSavedAchievements-- )
			{
				// read achievement ID
				int iAchievementID = pRestore->ReadShort();
				// find the corresponding achievement object
				// We only save games in SP games so the assumption of SINGLE_PLAYER_SLOT is valid
				CBaseAchievement *pAchievement = pAchievementMgr->GetAchievementByID( iAchievementID, SINGLE_PLAYER_SLOT );				
				Assert( pAchievement );		// It's a bug if we don't understand this achievement
				if ( pAchievement )
				{
					// read achievement data
					pRestore->ReadAll( pAchievement, pAchievement->GetDataDescMap() );
				}
				else
				{
					// if we don't recognize the achievement for some reason, read and discard the data and keep going
					CBaseAchievement ignored;
					pRestore->ReadAll( &ignored, ignored.GetDataDescMap() );
				}
			}
			pRestore->EndBlock();

			pAchievementMgr->PostRestoreSavedGame();
		}
	}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFStatPanel::UpdateStats( int iMsgType )
{
	C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pPlayer )
		return;

	// don't count stats if cheats on, commentary mode, etc
	if ( !g_AchievementMgrTF.CheckAchievementsEnabled() )
		return;

	ClassStats_t &classStats = GetClassStats( m_iClassCurrentLife );
	
	if ( iMsgType == STATMSG_PLAYERDEATH || iMsgType == STATMSG_PLAYERRESPAWN )
	{
		// if the player just died, accumulate current life into total and check for maxs and records
		classStats.AccumulateRound( m_RoundStatsCurrentLife );
		classStats.accumulated.m_iStat[TFSTAT_MAXSENTRYKILLS] = 0;	// sentry kills is a max value rather than a count, meaningless to accumulate
		CalcMaxsAndRecords();

		// reset current life stats
		m_iClassCurrentLife = TF_CLASS_UNDEFINED;
		m_iTeamCurrentLife = TEAM_UNASSIGNED;
		m_RoundStatsCurrentLife.Reset();
	}
	
	m_bStatsChanged = true;

	if ( m_statRecord > TFSTAT_UNDEFINED )
	{
		bool bAlive = ( iMsgType != STATMSG_PLAYERDEATH );
		if ( !bAlive || ( gpGlobals->curtime - m_flTimeLastSpawn < 3.0 ) )
		{
			// show the panel now if dead or very recently spawned
			vgui::ivgui()->AddTickSignal( GetVPanel(), 1000 );
			ShowStatPanel( m_iCurStatClass, m_iCurStatTeam, m_iCurStatValue, m_statRecord, m_recordBreakType, bAlive );
			m_flTimeHide = gpGlobals->curtime + ( bAlive ? 12.0f : 20.0f );
			m_statRecord = TFSTAT_UNDEFINED;
		}
	}

	IGameEvent * event = gameeventmanager->CreateEvent( "player_stats_updated" );
	if ( event )
	{
		event->SetBool( "forceupload", false );
		gameeventmanager->FireEventClientSide( event );
	}

	UpdateStatSummaryPanel();
}
Ejemplo n.º 5
0
void MedalStatLine::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	LoadControlSettings( "resource/UI/MedalStatLine.res" );

	if ( m_nMedalIndex != -1 )
	{
		//int nMedalImage = m_nMedalIndex;
		bool bCheckSingleplayerTooltip = false;
// 		if ( nMedalImage >= 44 && nMedalImage <= 51 )	// speedrun
// 		{
// 			nMedalImage = 44;
// 			bCheckSingleplayerTooltip = true;
// 		}

		if ( ASWGameRules() )
		{
			int nAchievementIndex = GetAchievementIndexForMedal( m_nMedalIndex );
			if ( nAchievementIndex != -1 )
			{
				CAchievementMgr *pAchievementMgr = dynamic_cast<CAchievementMgr *>( engine->GetAchievementMgr() );
				if ( pAchievementMgr )
				{
					CBaseAchievement *pAch = pAchievementMgr->GetAchievementByID( nAchievementIndex, 0 );
					if ( pAch )
					{
						char buffer[128];
						Q_snprintf( buffer, sizeof(buffer), "achievements/%s", pAch->GetName() );
						m_pIcon->SetImage(buffer);
					}
				}
			}
		}

		//m_pIcon->SetImage( VarArgs( "swarm/medals/medal%d", nMedalImage ) );
		Q_snprintf( m_szMedalName, sizeof( m_szMedalName ), "#asw_medal%d", m_nMedalIndex );
		m_pTitle->SetText( m_szMedalName );

		if ( bCheckSingleplayerTooltip && ASWGameResource() && ASWGameResource()->IsOfflineGame() )
		{
			Q_snprintf( m_szMedalDescription, sizeof( m_szMedalDescription ), "#asw_medaltt%dsp", m_nMedalIndex );
		}
		else
		{
			Q_snprintf( m_szMedalDescription, sizeof( m_szMedalDescription ), "#asw_medaltt%d", m_nMedalIndex );
		}
		m_pDescription->SetText( m_szMedalDescription );
		m_pCounter->SetText( VarArgs( "+%d", GetXPForMedal( m_nMedalIndex ) ) );
		m_nXP = GetXPForMedal( m_nMedalIndex );
	}
	else if ( m_nAchievementIndex != -1 )
	{
		CAchievementMgr *pAchievementMgr = dynamic_cast<CAchievementMgr *>( engine->GetAchievementMgr() );
		if ( !pAchievementMgr )
			return;

		CBaseAchievement *pAchievement = pAchievementMgr->GetAchievementByID ( m_nAchievementIndex, 0 );
		if ( pAchievement)
		{
			m_pIcon->SetImage( pAchievement->GetIconPath() );
			Q_snprintf( m_szMedalName, sizeof( m_szMedalName ), "#%s_NAME", pAchievement->GetName() );
			m_pTitle->SetText( m_szMedalName );

			Q_snprintf( m_szMedalDescription, sizeof( m_szMedalDescription ), "#%s_DESC", pAchievement->GetName() );
			m_pDescription->SetText( m_szMedalDescription );
			m_pCounter->SetText( VarArgs( "+%d", GetXPForMedal( -m_nAchievementIndex ) ) );
			m_nXP = GetXPForMedal( -m_nAchievementIndex );
		}
	}
}