// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using osu.Framework.Bindables; using osu.Framework.Extensions; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Mods; using osu.Game.Scoring; namespace osu.Game.Rulesets.Scoring { public class ScoreProcessor : JudgementProcessor { private const double base_portion = 0.3; private const double combo_portion = 0.7; private const double max_score = 1000000; /// /// The current total score. /// public readonly BindableDouble TotalScore = new BindableDouble { MinValue = 0 }; /// /// The current accuracy. /// public readonly BindableDouble Accuracy = new BindableDouble(1) { MinValue = 0, MaxValue = 1 }; /// /// The current combo. /// public readonly BindableInt Combo = new BindableInt(); /// /// The current selected mods /// public readonly Bindable> Mods = new Bindable>(Array.Empty()); /// /// The current rank. /// public readonly Bindable Rank = new Bindable(ScoreRank.X); /// /// The highest combo achieved by this score. /// public readonly BindableInt HighestCombo = new BindableInt(); /// /// The used to calculate scores. /// public readonly Bindable Mode = new Bindable(); private double maxHighestCombo; private double maxBaseScore; private double rollingMaxBaseScore; private double baseScore; private double bonusScore; private double scoreMultiplier = 1; public ScoreProcessor() { Debug.Assert(base_portion + combo_portion == 1.0); Combo.ValueChanged += combo => HighestCombo.Value = Math.Max(HighestCombo.Value, combo.NewValue); Accuracy.ValueChanged += accuracy => { Rank.Value = rankFrom(accuracy.NewValue); foreach (var mod in Mods.Value.OfType()) Rank.Value = mod.AdjustRank(Rank.Value, accuracy.NewValue); }; Mode.ValueChanged += _ => updateScore(); Mods.ValueChanged += mods => { scoreMultiplier = 1; foreach (var m in mods.NewValue) scoreMultiplier *= m.ScoreMultiplier; updateScore(); }; } private readonly Dictionary scoreResultCounts = new Dictionary(); protected sealed override void ApplyResultInternal(JudgementResult result) { result.ComboAtJudgement = Combo.Value; result.HighestComboAtJudgement = HighestCombo.Value; if (result.FailedAtJudgement) return; if (result.Judgement.AffectsCombo) { switch (result.Type) { case HitResult.None: break; case HitResult.Miss: Combo.Value = 0; break; default: Combo.Value++; break; } } if (result.Judgement.IsBonus) { if (result.IsHit) bonusScore += result.Judgement.NumericResultFor(result); } else { if (result.HasResult) scoreResultCounts[result.Type] = scoreResultCounts.GetOrDefault(result.Type) + 1; baseScore += result.Judgement.NumericResultFor(result); rollingMaxBaseScore += result.Judgement.MaxNumericResult; } updateScore(); } protected sealed override void RevertResultInternal(JudgementResult result) { Combo.Value = result.ComboAtJudgement; HighestCombo.Value = result.HighestComboAtJudgement; if (result.FailedAtJudgement) return; if (result.Judgement.IsBonus) { if (result.IsHit) bonusScore -= result.Judgement.NumericResultFor(result); } else { if (result.HasResult) scoreResultCounts[result.Type] = scoreResultCounts.GetOrDefault(result.Type) - 1; baseScore -= result.Judgement.NumericResultFor(result); rollingMaxBaseScore -= result.Judgement.MaxNumericResult; } updateScore(); } private void updateScore() { if (rollingMaxBaseScore != 0) Accuracy.Value = baseScore / rollingMaxBaseScore; TotalScore.Value = getScore(Mode.Value); } private double getScore(ScoringMode mode) { switch (mode) { default: case ScoringMode.Standardised: return (max_score * (base_portion * baseScore / maxBaseScore + combo_portion * HighestCombo.Value / maxHighestCombo) + bonusScore) * scoreMultiplier; case ScoringMode.Classic: // should emulate osu-stable's scoring as closely as we can (https://osu.ppy.sh/help/wiki/Score/ScoreV1) return bonusScore + baseScore * (1 + Math.Max(0, HighestCombo.Value - 1) * scoreMultiplier / 25); } } private ScoreRank rankFrom(double acc) { if (acc == 1) return ScoreRank.X; if (acc > 0.95) return ScoreRank.S; if (acc > 0.9) return ScoreRank.A; if (acc > 0.8) return ScoreRank.B; if (acc > 0.7) return ScoreRank.C; return ScoreRank.D; } public int GetStatistic(HitResult result) => scoreResultCounts.GetOrDefault(result); public double GetStandardisedScore() => getScore(ScoringMode.Standardised); /// /// Resets this ScoreProcessor to a default state. /// /// Whether to store the current state of the for future use. protected override void Reset(bool storeResults) { base.Reset(storeResults); scoreResultCounts.Clear(); if (storeResults) { maxHighestCombo = HighestCombo.Value; maxBaseScore = baseScore; if (maxBaseScore == 0 || maxHighestCombo == 0) { Mode.Value = ScoringMode.Classic; Mode.Disabled = true; } } baseScore = 0; rollingMaxBaseScore = 0; bonusScore = 0; TotalScore.Value = 0; Accuracy.Value = 1; Combo.Value = 0; Rank.Value = ScoreRank.X; HighestCombo.Value = 0; } /// /// Retrieve a score populated with data for the current play this processor is responsible for. /// public virtual void PopulateScore(ScoreInfo score) { score.TotalScore = (long)Math.Round(TotalScore.Value); score.Combo = Combo.Value; score.MaxCombo = HighestCombo.Value; score.Accuracy = Math.Round(Accuracy.Value, 4); score.Rank = Rank.Value; score.Date = DateTimeOffset.Now; var hitWindows = CreateHitWindows(); foreach (var result in Enum.GetValues(typeof(HitResult)).OfType().Where(r => r > HitResult.None && hitWindows.IsHitResultAllowed(r))) score.Statistics[result] = GetStatistic(result); } /// /// Create a for this processor. /// public virtual HitWindows CreateHitWindows() => new HitWindows(); } public enum ScoringMode { Standardised, Classic } }