// 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.Linq; using osu.Game.Rulesets.Difficulty.Preprocessing; using osu.Game.Rulesets.Difficulty.Utils; using osu.Game.Rulesets.Mods; namespace osu.Game.Rulesets.Difficulty.Skills { /// /// Used to processes strain values of s, keep track of strain levels caused by the processed objects /// and to calculate a final difficulty value representing the difficulty of hitting all the processed objects. /// public abstract class Skill { /// /// The peak strain for each section of the beatmap. /// public IReadOnlyList StrainPeaks => strainPeaks; /// /// Strain values are multiplied by this number for the given skill. Used to balance the value of different skills between each other. /// protected abstract double SkillMultiplier { get; } /// /// Determines how quickly strain decays for the given skill. /// For example a value of 0.15 indicates that strain decays to 15% of its original value in one second. /// protected abstract double StrainDecayBase { get; } /// /// The weight by which each strain value decays. /// protected virtual double DecayWeight => 0.9; /// /// s that were processed previously. They can affect the strain values of the following objects. /// protected readonly LimitedCapacityStack Previous = new LimitedCapacityStack(2); // Contained objects not used yet /// /// The current strain level. /// protected double CurrentStrain { get; private set; } = 1; /// /// Mods for use in skill calculations. /// protected IReadOnlyList Mods => mods; private double currentSectionPeak = 1; // We also keep track of the peak strain level in the current section. private readonly List strainPeaks = new List(); private readonly Mod[] mods; protected Skill(Mod[] mods) { this.mods = mods; } /// /// Process a and update current strain values accordingly. /// public void Process(DifficultyHitObject current) { CurrentStrain *= strainDecay(current.DeltaTime); CurrentStrain += StrainValueOf(current) * SkillMultiplier; currentSectionPeak = Math.Max(CurrentStrain, currentSectionPeak); Previous.Push(current); } /// /// Saves the current peak strain level to the list of strain peaks, which will be used to calculate an overall difficulty. /// public void SaveCurrentPeak() { if (Previous.Count > 0) strainPeaks.Add(currentSectionPeak); } /// /// Sets the initial strain level for a new section. /// /// The beginning of the new section in milliseconds. public void StartNewSectionFrom(double time) { // The maximum strain of the new section is not zero by default, strain decays as usual regardless of section boundaries. // This means we need to capture the strain level at the beginning of the new section, and use that as the initial peak level. if (Previous.Count > 0) currentSectionPeak = GetPeakStrain(time); } /// /// Retrieves the peak strain at a point in time. /// /// The time to retrieve the peak strain at. /// The peak strain. protected virtual double GetPeakStrain(double time) => CurrentStrain * strainDecay(time - Previous[0].BaseObject.StartTime); /// /// Returns the calculated difficulty value representing all processed s. /// public double DifficultyValue() { double difficulty = 0; double weight = 1; // Difficulty is the weighted sum of the highest strains from every section. // We're sorting from highest to lowest strain. foreach (double strain in strainPeaks.OrderByDescending(d => d)) { difficulty += strain * weight; weight *= DecayWeight; } return difficulty; } /// /// Calculates the strain value of a . This value is affected by previously processed objects. /// protected abstract double StrainValueOf(DifficultyHitObject current); private double strainDecay(double ms) => Math.Pow(StrainDecayBase, ms / 1000); } }