osu/osu.Game/Rulesets/Difficulty/DifficultyCalculator.cs

77 lines
3.0 KiB
C#
Raw Normal View History

2018-04-13 09:19:50 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-06-06 07:20:17 +00:00
using System;
2018-04-13 09:19:50 +00:00
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Difficulty
2018-04-13 09:19:50 +00:00
{
public abstract class DifficultyCalculator
{
protected readonly IBeatmap Beatmap;
2018-04-13 09:19:50 +00:00
protected readonly Mod[] Mods;
protected double TimeRate { get; private set; } = 1;
2018-04-19 11:44:38 +00:00
protected DifficultyCalculator(IBeatmap beatmap, Mod[] mods = null)
2018-04-13 09:19:50 +00:00
{
Beatmap = beatmap;
2018-04-13 09:19:50 +00:00
Mods = mods ?? new Mod[0];
ApplyMods(Mods);
}
protected virtual void ApplyMods(Mod[] mods)
{
var clock = new StopwatchClock();
mods.OfType<IApplicableToClock>().ForEach(m => m.ApplyToClock(clock));
TimeRate = clock.Rate;
}
2018-06-06 07:20:17 +00:00
/// <summary>
/// Creates all <see cref="Mod"/> combinations which adjust the <see cref="Beatmap"/> difficulty.
/// </summary>
public Mod[] CreateDifficultyAdjustmentModCombinations()
{
return createDifficultyAdjustmentModCombinations(Enumerable.Empty<Mod>(), DifficultyAdjustmentMods).ToArray();
IEnumerable<Mod> createDifficultyAdjustmentModCombinations(IEnumerable<Mod> currentSet, Mod[] adjustmentSet, int currentSetCount = 0, int adjustmentSetStart = 0)
{
// Initial-case: Empty current set
if (currentSetCount == 0)
yield return new NoModMod();
if (currentSetCount == 1)
yield return currentSet.Single();
if (currentSetCount > 1)
yield return new MultiMod(currentSet.ToArray());
// Apply mods in the adjustment set recursively. Using the entire adjustment set would result in duplicate multi-mod mod
// combinations in further recursions, so a moving subset is used to eliminate this effect
for (int i = adjustmentSetStart; i < adjustmentSet.Length; i++)
{
var adjustmentMod = adjustmentSet[i];
if (currentSet.Any(c => c.IncompatibleMods.Any(m => m.IsInstanceOfType(adjustmentMod))))
continue;
foreach (var combo in createDifficultyAdjustmentModCombinations(currentSet.Append(adjustmentMod), adjustmentSet, currentSetCount + 1, i + 1))
yield return combo;
}
}
}
/// <summary>
/// Retrieves all <see cref="Mod"/>s which adjust the <see cref="Beatmap"/> difficulty.
/// </summary>
protected virtual Mod[] DifficultyAdjustmentMods => Array.Empty<Mod>();
public abstract double Calculate(Dictionary<string, double> categoryDifficulty = null);
2018-04-13 09:19:50 +00:00
}
}