2022-03-01 13:12:06 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2022-03-03 05:09:29 +00:00
|
|
|
|
2022-03-01 13:12:06 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Bindables;
|
2022-03-03 04:02:39 +00:00
|
|
|
using osu.Framework.Utils;
|
2022-03-01 13:12:06 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Configuration;
|
2022-03-03 02:43:04 +00:00
|
|
|
using osu.Game.Rulesets.Judgements;
|
2022-03-01 13:12:06 +00:00
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
2022-03-02 01:53:28 +00:00
|
|
|
using osu.Game.Rulesets.Scoring;
|
2022-03-03 04:02:39 +00:00
|
|
|
using osu.Game.Rulesets.UI;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Mods
|
|
|
|
{
|
2022-03-03 04:02:39 +00:00
|
|
|
public class ModAdaptiveSpeed : Mod, IApplicableToRate, IApplicableToDrawableHitObject, IApplicableToBeatmap, IUpdatableByPlayfield
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
|
|
|
public override string Name => "Adaptive Speed";
|
|
|
|
|
|
|
|
public override string Acronym => "AS";
|
|
|
|
|
|
|
|
public override string Description => "Let track speed adapt to you.";
|
|
|
|
|
|
|
|
public override ModType Type => ModType.Fun;
|
|
|
|
|
|
|
|
public override double ScoreMultiplier => 1;
|
|
|
|
|
2022-05-05 11:37:38 +00:00
|
|
|
public override bool ValidForMultiplayer => false;
|
|
|
|
public override bool ValidForMultiplayerAsFreeMod => false;
|
2022-03-17 00:40:40 +00:00
|
|
|
|
2022-07-17 16:10:49 +00:00
|
|
|
public override Type[] IncompatibleMods => new[] { typeof(ModRateAdjust), typeof(ModTimeRamp), typeof(ModAutoplay) };
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-01 13:50:17 +00:00
|
|
|
[SettingSource("Initial rate", "The starting speed of the track")]
|
|
|
|
public BindableNumber<double> InitialRate { get; } = new BindableDouble
|
|
|
|
{
|
|
|
|
MinValue = 0.5,
|
|
|
|
MaxValue = 2,
|
|
|
|
Default = 1,
|
|
|
|
Value = 1,
|
2022-03-02 01:53:28 +00:00
|
|
|
Precision = 0.01
|
2022-03-01 13:50:17 +00:00
|
|
|
};
|
|
|
|
|
2022-03-01 13:12:06 +00:00
|
|
|
[SettingSource("Adjust pitch", "Should pitch be adjusted with speed")]
|
|
|
|
public BindableBool AdjustPitch { get; } = new BindableBool
|
|
|
|
{
|
|
|
|
Default = true,
|
|
|
|
Value = true
|
|
|
|
};
|
|
|
|
|
2022-03-03 16:25:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The instantaneous rate of the track.
|
|
|
|
/// Every frame this mod will attempt to smoothly adjust this to meet <see cref="targetRate"/>.
|
|
|
|
/// </summary>
|
2022-03-01 13:12:06 +00:00
|
|
|
public BindableNumber<double> SpeedChange { get; } = new BindableDouble
|
|
|
|
{
|
2022-03-03 16:07:36 +00:00
|
|
|
MinValue = min_allowable_rate,
|
|
|
|
MaxValue = max_allowable_rate,
|
2022-03-01 13:12:06 +00:00
|
|
|
Default = 1,
|
2022-03-03 04:02:39 +00:00
|
|
|
Value = 1
|
2022-03-01 13:12:06 +00:00
|
|
|
};
|
|
|
|
|
2022-03-03 16:07:36 +00:00
|
|
|
// The two constants below denote the maximum allowable range of rates that `SpeedChange` can take.
|
|
|
|
// The range is purposefully wider than the range of values that `InitialRate` allows
|
|
|
|
// in order to give some leeway for change even when extreme initial rates are chosen.
|
2022-03-04 03:48:48 +00:00
|
|
|
private const double min_allowable_rate = 0.4d;
|
|
|
|
private const double max_allowable_rate = 2.5d;
|
|
|
|
|
|
|
|
// The two constants below denote the maximum allowable change in rate caused by a single hit
|
|
|
|
// This prevents sudden jolts caused by a badly-timed hit.
|
2022-03-05 13:48:57 +00:00
|
|
|
private const double min_allowable_rate_change = 0.9d;
|
|
|
|
private const double max_allowable_rate_change = 1.11d;
|
2022-03-04 03:48:48 +00:00
|
|
|
|
|
|
|
// Apply a fixed rate change when missing, allowing the player to catch up when the rate is too fast.
|
|
|
|
private const double rate_change_on_miss = 0.95d;
|
2022-03-03 16:07:36 +00:00
|
|
|
|
2022-07-03 14:19:35 +00:00
|
|
|
private IAdjustableAudioComponent? track;
|
2022-03-03 04:02:39 +00:00
|
|
|
private double targetRate = 1d;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-03 16:25:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The number of most recent track rates (approximated from how early/late each object was hit relative to the previous object)
|
2022-03-03 16:39:47 +00:00
|
|
|
/// which should be averaged to calculate <see cref="targetRate"/>.
|
2022-03-03 16:25:49 +00:00
|
|
|
/// </summary>
|
2022-03-05 13:48:57 +00:00
|
|
|
private const int recent_rate_count = 8;
|
2022-03-03 16:25:49 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stores the most recent <see cref="recent_rate_count"/> approximated track rates
|
2022-03-03 16:39:47 +00:00
|
|
|
/// which are averaged to calculate the value of <see cref="targetRate"/>.
|
2022-03-03 16:25:49 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This list is used as a double-ended queue with fixed capacity
|
|
|
|
/// (items can be enqueued/dequeued at either end of the list).
|
|
|
|
/// When time is elapsing forward, items are dequeued from the start and enqueued onto the end of the list.
|
|
|
|
/// When time is being rewound, items are dequeued from the end and enqueued onto the start of the list.
|
|
|
|
/// </remarks>
|
|
|
|
/// <example>
|
|
|
|
/// <para>
|
2022-03-04 03:03:57 +00:00
|
|
|
/// The track rate approximation is calculated as follows:
|
2022-03-03 16:25:49 +00:00
|
|
|
/// </para>
|
|
|
|
/// <para>
|
|
|
|
/// Consider a hitobject which ends at 1000ms, and assume that its preceding hitobject ends at 500ms.
|
|
|
|
/// This gives a time difference of 1000 - 500 = 500ms.
|
|
|
|
/// </para>
|
|
|
|
/// <para>
|
|
|
|
/// Now assume that the user hit this object at 980ms rather than 1000ms.
|
|
|
|
/// When compared to the preceding hitobject, this gives 980 - 500 = 480ms.
|
|
|
|
/// </para>
|
|
|
|
/// <para>
|
|
|
|
/// With the above assumptions, the player is rushing / hitting early, which means that the track should speed up to match.
|
|
|
|
/// Therefore, the approximated target rate for this object would be equal to 500 / 480 * <see cref="InitialRate"/>.
|
|
|
|
/// </para>
|
|
|
|
/// </example>
|
2022-03-04 03:03:57 +00:00
|
|
|
private readonly List<double> recentRates = Enumerable.Repeat(1d, recent_rate_count).ToList();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// For each given <see cref="HitObject"/> in the map, this dictionary maps the object onto the latest end time of any other object
|
|
|
|
/// that precedes the end time of the given object.
|
|
|
|
/// This can be loosely interpreted as the end time of the preceding hit object in rulesets that do not have overlapping hit objects.
|
|
|
|
/// </summary>
|
|
|
|
private readonly Dictionary<HitObject, double> precedingEndTimes = new Dictionary<HitObject, double>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// For each given <see cref="HitObject"/> in the map, this dictionary maps the object onto the track rate dequeued from
|
|
|
|
/// <see cref="recentRates"/> (i.e. the oldest value in the queue) when the object is hit. If the hit is then reverted,
|
|
|
|
/// the mapped value can be re-introduced to <see cref="recentRates"/> to properly rewind the queue.
|
|
|
|
/// </summary>
|
|
|
|
private readonly Dictionary<HitObject, double> ratesForRewinding = new Dictionary<HitObject, double>();
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-01 13:12:06 +00:00
|
|
|
public ModAdaptiveSpeed()
|
|
|
|
{
|
2022-03-03 04:02:39 +00:00
|
|
|
InitialRate.BindValueChanged(val =>
|
|
|
|
{
|
|
|
|
SpeedChange.Value = val.NewValue;
|
|
|
|
targetRate = val.NewValue;
|
|
|
|
});
|
2022-03-03 05:07:30 +00:00
|
|
|
AdjustPitch.BindValueChanged(adjustPitchChanged);
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:01:15 +00:00
|
|
|
public void ApplyToTrack(IAdjustableAudioComponent track)
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
|
|
|
this.track = track;
|
|
|
|
|
2022-03-01 13:50:17 +00:00
|
|
|
InitialRate.TriggerChange();
|
2022-03-01 13:12:06 +00:00
|
|
|
AdjustPitch.TriggerChange();
|
2022-03-01 13:50:17 +00:00
|
|
|
recentRates.Clear();
|
2022-03-03 16:25:49 +00:00
|
|
|
recentRates.AddRange(Enumerable.Repeat(InitialRate.Value, recent_rate_count));
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:01:36 +00:00
|
|
|
public void ApplyToSample(IAdjustableAudioComponent sample)
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
|
|
|
sample.AddAdjustment(AdjustableProperty.Frequency, SpeedChange);
|
|
|
|
}
|
|
|
|
|
2022-03-03 04:02:39 +00:00
|
|
|
public void Update(Playfield playfield)
|
|
|
|
{
|
|
|
|
SpeedChange.Value = Interpolation.DampContinuously(SpeedChange.Value, targetRate, 50, playfield.Clock.ElapsedFrameTime);
|
|
|
|
}
|
|
|
|
|
2022-03-02 01:53:28 +00:00
|
|
|
public double ApplyToRate(double time, double rate = 1) => rate * InitialRate.Value;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
|
|
|
public void ApplyToDrawableHitObject(DrawableHitObject drawable)
|
|
|
|
{
|
2022-06-24 12:25:23 +00:00
|
|
|
drawable.OnNewResult += (_, result) =>
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
2022-03-04 03:03:57 +00:00
|
|
|
if (ratesForRewinding.ContainsKey(result.HitObject)) return;
|
2022-03-03 02:43:04 +00:00
|
|
|
if (!shouldProcessResult(result)) return;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-04 03:03:57 +00:00
|
|
|
ratesForRewinding.Add(result.HitObject, recentRates[0]);
|
2022-03-02 12:48:57 +00:00
|
|
|
recentRates.RemoveAt(0);
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-04 03:48:48 +00:00
|
|
|
recentRates.Add(Math.Clamp(getRelativeRateChange(result) * SpeedChange.Value, min_allowable_rate, max_allowable_rate));
|
2022-03-04 03:03:57 +00:00
|
|
|
|
2022-03-05 13:48:57 +00:00
|
|
|
updateTargetRate();
|
2022-03-02 01:53:28 +00:00
|
|
|
};
|
2022-06-24 12:25:23 +00:00
|
|
|
drawable.OnRevertResult += (_, result) =>
|
2022-03-02 01:53:28 +00:00
|
|
|
{
|
2022-03-04 03:03:57 +00:00
|
|
|
if (!ratesForRewinding.ContainsKey(result.HitObject)) return;
|
2022-03-03 02:43:04 +00:00
|
|
|
if (!shouldProcessResult(result)) return;
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-04 03:03:57 +00:00
|
|
|
recentRates.Insert(0, ratesForRewinding[result.HitObject]);
|
|
|
|
ratesForRewinding.Remove(result.HitObject);
|
|
|
|
|
2022-03-02 12:48:57 +00:00
|
|
|
recentRates.RemoveAt(recentRates.Count - 1);
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-05 13:48:57 +00:00
|
|
|
updateTargetRate();
|
2022-03-01 13:12:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ApplyToBeatmap(IBeatmap beatmap)
|
|
|
|
{
|
2022-03-02 12:48:57 +00:00
|
|
|
var hitObjects = getAllApplicableHitObjects(beatmap.HitObjects).ToList();
|
2022-03-03 03:21:20 +00:00
|
|
|
var endTimes = hitObjects.Select(x => x.GetEndTime()).OrderBy(x => x).Distinct().ToList();
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-02 12:48:57 +00:00
|
|
|
foreach (HitObject hitObject in hitObjects)
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
2022-03-03 03:21:20 +00:00
|
|
|
int index = endTimes.BinarySearch(hitObject.GetEndTime());
|
|
|
|
if (index < 0) index = ~index; // BinarySearch returns the next larger element in bitwise complement if there's no exact match
|
|
|
|
index -= 1;
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-03 03:21:20 +00:00
|
|
|
if (index >= 0)
|
2022-03-03 16:25:49 +00:00
|
|
|
precedingEndTimes.Add(hitObject, endTimes[index]);
|
2022-03-02 01:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 05:09:29 +00:00
|
|
|
private void adjustPitchChanged(ValueChangedEvent<bool> adjustPitchSetting)
|
|
|
|
{
|
|
|
|
track?.RemoveAdjustment(adjustmentForPitchSetting(adjustPitchSetting.OldValue), SpeedChange);
|
|
|
|
track?.AddAdjustment(adjustmentForPitchSetting(adjustPitchSetting.NewValue), SpeedChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private AdjustableProperty adjustmentForPitchSetting(bool adjustPitchSettingValue)
|
|
|
|
=> adjustPitchSettingValue ? AdjustableProperty.Frequency : AdjustableProperty.Tempo;
|
|
|
|
|
2022-03-02 12:48:57 +00:00
|
|
|
private IEnumerable<HitObject> getAllApplicableHitObjects(IEnumerable<HitObject> hitObjects)
|
2022-03-02 01:53:28 +00:00
|
|
|
{
|
|
|
|
foreach (var hitObject in hitObjects)
|
|
|
|
{
|
|
|
|
if (!(hitObject.HitWindows is HitWindows.EmptyHitWindows))
|
2022-03-02 12:48:57 +00:00
|
|
|
yield return hitObject;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-02 12:48:57 +00:00
|
|
|
foreach (HitObject nested in getAllApplicableHitObjects(hitObject.NestedHitObjects))
|
|
|
|
yield return nested;
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 02:43:04 +00:00
|
|
|
|
|
|
|
private bool shouldProcessResult(JudgementResult result)
|
|
|
|
{
|
|
|
|
if (!result.Type.AffectsAccuracy()) return false;
|
2022-03-03 16:25:49 +00:00
|
|
|
if (!precedingEndTimes.ContainsKey(result.HitObject)) return false;
|
2022-03-03 02:43:04 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-04 03:48:48 +00:00
|
|
|
|
|
|
|
private double getRelativeRateChange(JudgementResult result)
|
|
|
|
{
|
|
|
|
if (!result.IsHit)
|
|
|
|
return rate_change_on_miss;
|
|
|
|
|
|
|
|
double prevEndTime = precedingEndTimes[result.HitObject];
|
|
|
|
return Math.Clamp(
|
|
|
|
(result.HitObject.GetEndTime() - prevEndTime) / (result.TimeAbsolute - prevEndTime),
|
|
|
|
min_allowable_rate_change,
|
|
|
|
max_allowable_rate_change
|
|
|
|
);
|
|
|
|
}
|
2022-03-05 13:48:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Update <see cref="targetRate"/> based on the values in <see cref="recentRates"/>.
|
|
|
|
/// </summary>
|
|
|
|
private void updateTargetRate()
|
|
|
|
{
|
|
|
|
// Compare values in recentRates to see how consistent the player's speed is
|
|
|
|
// If the player hits half of the notes too fast and the other half too slow: Abs(consistency) = 0
|
|
|
|
// If the player hits all their notes too fast or too slow: Abs(consistency) = recent_rate_count - 1
|
|
|
|
int consistency = 0;
|
|
|
|
|
|
|
|
for (int i = 1; i < recentRates.Count; i++)
|
|
|
|
{
|
|
|
|
consistency += Math.Sign(recentRates[i] - recentRates[i - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale the rate adjustment based on consistency
|
|
|
|
targetRate = Interpolation.Lerp(targetRate, recentRates.Average(), Math.Abs(consistency) / (recent_rate_count - 1d));
|
|
|
|
}
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
}
|