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-02 01:53:28 +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.Audio.Track;
|
|
|
|
using osu.Framework.Bindables;
|
2022-03-02 12:06:28 +00:00
|
|
|
using osu.Framework.Graphics;
|
2022-03-01 13:12:06 +00:00
|
|
|
using osu.Framework.Graphics.Audio;
|
|
|
|
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-02 12:06:28 +00:00
|
|
|
using osu.Game.Screens.Play;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Mods
|
|
|
|
{
|
2022-03-02 12:06:28 +00:00
|
|
|
public class ModAdaptiveSpeed : Mod, IApplicableToRate, IApplicableToDrawableHitObject, IApplicableToBeatmap, IApplicableToHUD
|
2022-03-01 13:12:06 +00:00
|
|
|
{
|
2022-03-02 01:53:28 +00:00
|
|
|
// use a wider range so there's still room for adjustment when the initial rate is extreme
|
|
|
|
private const double fastest_rate = 2.5f;
|
|
|
|
private const double slowest_rate = 0.4f;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adjust track rate using the average speed of the last x hits
|
|
|
|
/// </summary>
|
2022-03-02 13:07:57 +00:00
|
|
|
private const int average_count = 6;
|
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;
|
|
|
|
|
|
|
|
public override Type[] IncompatibleMods => new[] { typeof(ModRateAdjust), typeof(ModTimeRamp) };
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
public BindableNumber<double> SpeedChange { get; } = new BindableDouble
|
|
|
|
{
|
|
|
|
Default = 1,
|
|
|
|
Value = 1,
|
2022-03-02 01:53:28 +00:00
|
|
|
Precision = 0.01
|
2022-03-01 13:12:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private ITrack track;
|
2022-03-02 12:06:28 +00:00
|
|
|
private HUDOverlay overlay;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-03 02:18:36 +00:00
|
|
|
private readonly List<double> recentRates = Enumerable.Repeat(1d, average_count).ToList();
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-02 01:53:28 +00:00
|
|
|
// rate for a hit is calculated using the end time of another hit object earlier in time
|
2022-03-01 13:12:06 +00:00
|
|
|
// caching them here for easy access
|
|
|
|
private readonly Dictionary<HitObject, double> previousEndTimes = new Dictionary<HitObject, double>();
|
|
|
|
|
2022-03-02 01:53:28 +00:00
|
|
|
// record the value removed from recentRates when an object is hit
|
|
|
|
// for rewind support
|
|
|
|
private readonly Dictionary<HitObject, double> dequeuedRates = new Dictionary<HitObject, double>();
|
|
|
|
|
2022-03-01 13:12:06 +00:00
|
|
|
public ModAdaptiveSpeed()
|
|
|
|
{
|
2022-03-01 13:50:17 +00:00
|
|
|
InitialRate.BindValueChanged(val => SpeedChange.Value = val.NewValue);
|
2022-03-01 13:12:06 +00:00
|
|
|
AdjustPitch.BindValueChanged(applyPitchAdjustment);
|
|
|
|
}
|
|
|
|
|
2022-03-02 12:06:28 +00:00
|
|
|
public void ApplyToHUD(HUDOverlay overlay)
|
|
|
|
{
|
|
|
|
// this is only used to transform the SpeedChange bindable
|
|
|
|
this.overlay = overlay;
|
|
|
|
}
|
|
|
|
|
2022-03-01 13:12:06 +00:00
|
|
|
public void ApplyToTrack(ITrack track)
|
|
|
|
{
|
|
|
|
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 02:18:36 +00:00
|
|
|
recentRates.AddRange(Enumerable.Repeat(InitialRate.Value, average_count));
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ApplyToSample(DrawableSample sample)
|
|
|
|
{
|
|
|
|
sample.AddAdjustment(AdjustableProperty.Frequency, SpeedChange);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
private void applyPitchAdjustment(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;
|
|
|
|
|
|
|
|
public void ApplyToDrawableHitObject(DrawableHitObject drawable)
|
|
|
|
{
|
|
|
|
drawable.OnNewResult += (o, result) =>
|
|
|
|
{
|
2022-03-02 12:48:57 +00:00
|
|
|
if (dequeuedRates.ContainsKey(result.HitObject)) return;
|
2022-03-03 02:43:04 +00:00
|
|
|
if (!shouldProcessResult(result)) return;
|
2022-03-01 13:12:06 +00:00
|
|
|
|
|
|
|
double prevEndTime = previousEndTimes[result.HitObject];
|
|
|
|
|
|
|
|
recentRates.Add(Math.Clamp((result.HitObject.GetEndTime() - prevEndTime) / (result.TimeAbsolute - prevEndTime) * SpeedChange.Value, slowest_rate, fastest_rate));
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-02 12:48:57 +00:00
|
|
|
dequeuedRates.Add(result.HitObject, recentRates[0]);
|
|
|
|
recentRates.RemoveAt(0);
|
2022-03-02 01:53:28 +00:00
|
|
|
|
2022-03-02 12:06:28 +00:00
|
|
|
overlay.TransformBindableTo(SpeedChange, recentRates.Average(), 100);
|
2022-03-02 01:53:28 +00:00
|
|
|
};
|
|
|
|
drawable.OnRevertResult += (o, result) =>
|
|
|
|
{
|
2022-03-02 12:48:57 +00:00
|
|
|
if (!dequeuedRates.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-02 12:48:57 +00:00
|
|
|
recentRates.Insert(0, dequeuedRates[result.HitObject]);
|
|
|
|
recentRates.RemoveAt(recentRates.Count - 1);
|
|
|
|
dequeuedRates.Remove(result.HitObject);
|
2022-03-01 13:12:06 +00:00
|
|
|
|
2022-03-02 12:06:28 +00:00
|
|
|
overlay.TransformBindableTo(SpeedChange, recentRates.Average(), 100);
|
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)
|
|
|
|
previousEndTimes.Add(hitObject, endTimes[index]);
|
2022-03-02 01:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.IsHit) return false;
|
|
|
|
if (!result.Type.AffectsAccuracy()) return false;
|
|
|
|
if (!previousEndTimes.ContainsKey(result.HitObject)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-01 13:12:06 +00:00
|
|
|
}
|
|
|
|
}
|