From 77940f76a2f2219535d5e370dbc9e6d964774081 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Fri, 14 Sep 2007 08:50:29 +0000 Subject: [PATCH] Move percent formatting into PlayerStageStats and apply the truncating rules in GetPercentScore(). The percent score that ends up in HighScore should match what is shown on the screen. --- stepmania/src/CommonMetrics.cpp | 2 +- stepmania/src/CommonMetrics.h | 1 + stepmania/src/PaneDisplay.cpp | 4 +-- stepmania/src/PercentageDisplay.cpp | 24 ----------------- stepmania/src/PercentageDisplay.h | 2 -- stepmania/src/PlayerStageStats.cpp | 42 ++++++++++++++++++++++++----- stepmania/src/PlayerStageStats.h | 2 ++ 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/stepmania/src/CommonMetrics.cpp b/stepmania/src/CommonMetrics.cpp index f1cae2f098..28f1c788b8 100644 --- a/stepmania/src/CommonMetrics.cpp +++ b/stepmania/src/CommonMetrics.cpp @@ -23,7 +23,7 @@ ThemeMetricDifficultiesToShow CommonMetrics::DIFFICULTIES_TO_SHOW ("Common","D ThemeMetricCourseDifficultiesToShow CommonMetrics::COURSE_DIFFICULTIES_TO_SHOW ("Common","CourseDifficultiesToShow"); ThemeMetricStepsTypesToShow CommonMetrics::STEPS_TYPES_TO_SHOW ("Common","StepsTypesToHide"); // This metric takes a list of StepsTypes to hide and returns a list of StepsTypes to show ThemeMetric CommonMetrics::AUTO_SET_STYLE ("Common","AutoSetStyle"); - +ThemeMetric CommonMetrics::PERCENT_SCORE_DECIMAL_PLACES ("Common","PercentScoreDecimalPlaces"); ThemeMetricDifficultiesToShow::ThemeMetricDifficultiesToShow( const RString& sGroup, const RString& sName ) : ThemeMetric(sGroup,sName) diff --git a/stepmania/src/CommonMetrics.h b/stepmania/src/CommonMetrics.h index b295d052ff..dfcec9d335 100644 --- a/stepmania/src/CommonMetrics.h +++ b/stepmania/src/CommonMetrics.h @@ -62,6 +62,7 @@ namespace CommonMetrics extern ThemeMetricCourseDifficultiesToShow COURSE_DIFFICULTIES_TO_SHOW; extern ThemeMetricStepsTypesToShow STEPS_TYPES_TO_SHOW; extern ThemeMetric AUTO_SET_STYLE; + extern ThemeMetric PERCENT_SCORE_DECIMAL_PLACES; RString LocalizeOptionItem( const RString &s, bool bOptional ); }; diff --git a/stepmania/src/PaneDisplay.cpp b/stepmania/src/PaneDisplay.cpp index 2b48a96805..e638f5d34f 100644 --- a/stepmania/src/PaneDisplay.cpp +++ b/stepmania/src/PaneDisplay.cpp @@ -11,9 +11,9 @@ #include "Style.h" #include "ActorUtil.h" #include "Foreach.h" -#include "PercentageDisplay.h" #include "LuaManager.h" #include "XmlFile.h" +#include "PlayerStageStats.h" #define SHIFT_X(p) THEME->GetMetricF(sMetricsGroup, ssprintf("ShiftP%iX", p+1)) #define SHIFT_Y(p) THEME->GetMetricF(sMetricsGroup, ssprintf("ShiftP%iY", p+1)) @@ -169,7 +169,7 @@ void PaneDisplay::SetContent( PaneContents c ) if( bIsPlayerEdit ) str = NOT_AVAILABLE; else - str = PercentageDisplay::FormatPercentScore( val ); + str = PlayerStageStats::FormatPercentScore( val ); break; case SONG_NUM_STEPS: case SONG_JUMPS: diff --git a/stepmania/src/PercentageDisplay.cpp b/stepmania/src/PercentageDisplay.cpp index d84fd48015..a07d07fa7c 100644 --- a/stepmania/src/PercentageDisplay.cpp +++ b/stepmania/src/PercentageDisplay.cpp @@ -10,8 +10,6 @@ #include "PlayerState.h" #include "XmlFile.h" -ThemeMetric PERCENT_DECIMAL_PLACES ( "PercentageDisplay", "PercentDecimalPlaces" ); -ThemeMetric PERCENT_TOTAL_SIZE ( "PercentageDisplay", "PercentTotalSize" ); REGISTER_ACTOR_CLASS( PercentageDisplay ) @@ -189,28 +187,6 @@ void PercentageDisplay::Refresh() m_textPercent.SetText( sNumToDisplay ); } -RString PercentageDisplay::FormatPercentScore( float fPercentDancePoints ) -{ - CLAMP( fPercentDancePoints, 0.f, 1.f ); - - // TRICKY: printf will round, but we want to truncate. Otherwise, we may display a percent - // score that's too high and doesn't match up with the calculated grade. - float fTruncInterval = powf( 0.1f, (float)PERCENT_TOTAL_SIZE-1 ); - - // TRICKY: ftruncf is rounding 1.0000000 to 0.99990004. Give a little boost to - // fPercentDancePoints to correct for this. - fPercentDancePoints += 0.000001f; - - fPercentDancePoints = ftruncf( fPercentDancePoints, fTruncInterval ); - - RString s = ssprintf( "%*.*f%%", (int)PERCENT_TOTAL_SIZE, (int)PERCENT_DECIMAL_PLACES, fPercentDancePoints*100 ); - return s; -} - -// lua start -#include "LuaManager.h" - -LuaFunction( FormatPercentScore, PercentageDisplay::FormatPercentScore( FArg(1) ) ) #include "LuaBinding.h" diff --git a/stepmania/src/PercentageDisplay.h b/stepmania/src/PercentageDisplay.h index e45aa9d55d..7c75d994c0 100644 --- a/stepmania/src/PercentageDisplay.h +++ b/stepmania/src/PercentageDisplay.h @@ -21,8 +21,6 @@ public: virtual void LoadFromNode( const XNode* pNode ); virtual PercentageDisplay *Copy() const; - static RString FormatPercentScore( float fPercentDancePoints ); - // // Lua // diff --git a/stepmania/src/PlayerStageStats.cpp b/stepmania/src/PlayerStageStats.cpp index bd67a070bf..8ce3010c49 100644 --- a/stepmania/src/PlayerStageStats.cpp +++ b/stepmania/src/PlayerStageStats.cpp @@ -10,6 +10,7 @@ #include "Steps.h" #include "ScoreKeeperNormal.h" #include "PrefsManager.h" +#include "CommonMetrics.h" #define GRADE_PERCENT_TIER(i) THEME->GetMetricF("PlayerStageStats",ssprintf("GradePercent%s",GradeToString((Grade)i).c_str())) #define GRADE_TIER02_IS_ALL_W2S THEME->GetMetricB("PlayerStageStats","GradeTier02IsAllW2s") @@ -228,20 +229,47 @@ Grade PlayerStageStats::GetGrade() const return grade; } -float PlayerStageStats::GetPercentDancePoints() const +float PlayerStageStats::MakePercentScore( int iActual, int iPossible ) { - if( m_iPossibleDancePoints == 0 ) + if( iPossible == 0 ) return 0; // div/0 - if( m_iActualDancePoints == m_iPossibleDancePoints ) + if( iActual == iPossible ) return 1; // correct for rounding error /* This can happen in battle, with transform attacks. */ - //ASSERT_M( iActualDancePoints <= iPossibleDancePoints, ssprintf("%i/%i", iActualDancePoints, iPossibleDancePoints) ); + //ASSERT_M( iActual <= iPossible, ssprintf("%i/%i", iActual, iPossible) ); - float fPercentDancePoints = m_iActualDancePoints / (float)m_iPossibleDancePoints; + float fPercent = iActual / (float)iPossible; + + // don't allow negative + fPercent = max( 0, fPercent ); + + int iPercentTotalDigits = 3 + CommonMetrics::PERCENT_SCORE_DECIMAL_PLACES; // "100" + "." + "00" + + // TRICKY: printf will round, but we want to truncate. Otherwise, we may display a percent + // score that's too high and doesn't match up with the calculated grade. + float fTruncInterval = powf( 0.1f, (float)iPercentTotalDigits-1 ); - return fPercentDancePoints; + // TRICKY: ftruncf is rounding 1.0000000 to 0.99990004. Give a little boost to + // fPercentDancePoints to correct for this. + fPercent += 0.000001f; + + fPercent = ftruncf( fPercent, fTruncInterval ); + return fPercent; +} + +RString PlayerStageStats::FormatPercentScore( float fPercentDancePoints ) +{ + int iPercentTotalDigits = 3 + CommonMetrics::PERCENT_SCORE_DECIMAL_PLACES; // "100" + "." + "00" + + RString s = ssprintf( "%*.*f%%", iPercentTotalDigits, (int)CommonMetrics::PERCENT_SCORE_DECIMAL_PLACES, fPercentDancePoints*100 ); + return s; +} + +float PlayerStageStats::GetPercentDancePoints() const +{ + return MakePercentScore( m_iActualDancePoints, m_iPossibleDancePoints ); } float PlayerStageStats::GetCurMaxPercentDancePoints() const @@ -634,7 +662,7 @@ bool PlayerStageStats::IsDisqualified() const } LuaFunction( GetGradeFromPercent, GetGradeFromPercent( FArg(1), false ) ) - +LuaFunction( FormatPercentScore, PlayerStageStats::FormatPercentScore( FArg(1) ) ) // lua start diff --git a/stepmania/src/PlayerStageStats.h b/stepmania/src/PlayerStageStats.h index a16c392115..3b8b6d7d89 100644 --- a/stepmania/src/PlayerStageStats.h +++ b/stepmania/src/PlayerStageStats.h @@ -20,6 +20,8 @@ public: void AddStats( const PlayerStageStats& other ); // accumulate Grade GetGrade() const; + static float MakePercentScore( int iActual, int iPossible ); + static RString FormatPercentScore( float fPercentScore ); float GetPercentDancePoints() const; float GetCurMaxPercentDancePoints() const;