// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using osu.Game.Rulesets.Difficulty.Preprocessing; using osu.Game.Rulesets.Difficulty.Skills; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Taiko.Difficulty.Preprocessing; using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Utils; namespace osu.Game.Rulesets.Taiko.Difficulty.Skills { /// /// Calculates the rhythm coefficient of taiko difficulty. /// public class Rhythm : StrainDecaySkill { protected override double SkillMultiplier => 10; protected override double StrainDecayBase => 0; /// /// The note-based decay for rhythm strain. /// /// /// is not used here, as it's time- and not note-based. /// private const double strain_decay = 0.96; /// /// Maximum number of entries in . /// private const int rhythm_history_max_length = 8; /// /// Contains the last changes in note sequence rhythms. /// private readonly LimitedCapacityQueue rhythmHistory = new LimitedCapacityQueue(rhythm_history_max_length); /// /// Contains the rolling rhythm strain. /// Used to apply per-note decay. /// private double currentStrain; /// /// Number of notes since the last rhythm change has taken place. /// private int notesSinceRhythmChange; public Rhythm(Mod[] mods) : base(mods) { } protected override double StrainValueOf(DifficultyHitObject current) { // drum rolls and swells are exempt. if (!(current.BaseObject is Hit)) { resetRhythmAndStrain(); return 0.0; } currentStrain *= strain_decay; TaikoDifficultyHitObject hitObject = (TaikoDifficultyHitObject)current; notesSinceRhythmChange += 1; // rhythm difficulty zero (due to rhythm not changing) => no rhythm strain. if (hitObject.Rhythm.Difficulty == 0.0) { return 0.0; } double objectStrain = hitObject.Rhythm.Difficulty; objectStrain *= repetitionPenalties(hitObject); objectStrain *= patternLengthPenalty(notesSinceRhythmChange); objectStrain *= speedPenalty(hitObject.DeltaTime); // careful - needs to be done here since calls above read this value notesSinceRhythmChange = 0; currentStrain += objectStrain; return currentStrain; } /// /// Returns a penalty to apply to the current hit object caused by repeating rhythm changes. /// /// /// Repetitions of more recent patterns are associated with a higher penalty. /// /// The current hit object being considered. private double repetitionPenalties(TaikoDifficultyHitObject hitObject) { double penalty = 1; rhythmHistory.Enqueue(hitObject); for (int mostRecentPatternsToCompare = 2; mostRecentPatternsToCompare <= rhythm_history_max_length / 2; mostRecentPatternsToCompare++) { for (int start = rhythmHistory.Count - mostRecentPatternsToCompare - 1; start >= 0; start--) { if (!samePattern(start, mostRecentPatternsToCompare)) continue; int notesSince = hitObject.Index - rhythmHistory[start].Index; penalty *= repetitionPenalty(notesSince); break; } } return penalty; } /// /// Determines whether the rhythm change pattern starting at is a repeat of any of the /// . /// private bool samePattern(int start, int mostRecentPatternsToCompare) { for (int i = 0; i < mostRecentPatternsToCompare; i++) { if (rhythmHistory[start + i].Rhythm != rhythmHistory[rhythmHistory.Count - mostRecentPatternsToCompare + i].Rhythm) return false; } return true; } /// /// Calculates a single rhythm repetition penalty. /// /// Number of notes since the last repetition of a rhythm change. private static double repetitionPenalty(int notesSince) => Math.Min(1.0, 0.032 * notesSince); /// /// Calculates a penalty based on the number of notes since the last rhythm change. /// Both rare and frequent rhythm changes are penalised. /// /// Number of notes since the last rhythm change. private static double patternLengthPenalty(int patternLength) { double shortPatternPenalty = Math.Min(0.15 * patternLength, 1.0); double longPatternPenalty = Math.Clamp(2.5 - 0.15 * patternLength, 0.0, 1.0); return Math.Min(shortPatternPenalty, longPatternPenalty); } /// /// Calculates a penalty for objects that do not require alternating hands. /// /// Time (in milliseconds) since the last hit object. private double speedPenalty(double deltaTime) { if (deltaTime < 80) return 1; if (deltaTime < 210) return Math.Max(0, 1.4 - 0.005 * deltaTime); resetRhythmAndStrain(); return 0.0; } /// /// Resets the rolling strain value and counter. /// private void resetRhythmAndStrain() { currentStrain = 0.0; notesSinceRhythmChange = 0; } } }