osu/osu.Game.Rulesets.Mania/Beatmaps/ManiaBeatmapConverter.cs

273 lines
10 KiB
C#
Raw Normal View History

// 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.
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Mania.Objects;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using osu.Game.Audio;
2018-04-13 09:19:50 +00:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Mania.Beatmaps.Patterns;
using osu.Game.Rulesets.Mania.MathUtils;
using osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Mania.Beatmaps
{
public class ManiaBeatmapConverter : BeatmapConverter<ManiaHitObject>
{
/// <summary>
/// Maximum number of previous notes to consider for density calculation.
/// </summary>
private const int max_notes_for_density = 7;
public int TargetColumns;
2019-02-28 14:40:03 +00:00
public bool Dual;
2018-04-13 09:19:50 +00:00
public readonly bool IsForCurrentRuleset;
2018-06-15 11:52:09 +00:00
// Internal for testing purposes
internal FastRandom Random { get; private set; }
2018-04-13 09:19:50 +00:00
private Pattern lastPattern = new Pattern();
private ManiaBeatmap beatmap;
public ManiaBeatmapConverter(IBeatmap beatmap, Ruleset ruleset)
: base(beatmap, ruleset)
2018-04-13 09:19:50 +00:00
{
IsForCurrentRuleset = beatmap.BeatmapInfo.Ruleset.Equals(ruleset.RulesetInfo);
2018-05-07 02:23:29 +00:00
var roundedCircleSize = Math.Round(beatmap.BeatmapInfo.BaseDifficulty.CircleSize);
var roundedOverallDifficulty = Math.Round(beatmap.BeatmapInfo.BaseDifficulty.OverallDifficulty);
2018-04-13 09:19:50 +00:00
2018-05-07 00:17:12 +00:00
if (IsForCurrentRuleset)
2019-02-28 10:07:43 +00:00
{
2018-04-13 09:19:50 +00:00
TargetColumns = (int)Math.Max(1, roundedCircleSize);
2019-04-01 03:16:05 +00:00
2020-04-21 01:56:04 +00:00
if (TargetColumns > ManiaRuleset.MAX_STAGE_KEYS)
2019-02-28 10:07:43 +00:00
{
2019-11-12 09:56:38 +00:00
TargetColumns /= 2;
2019-02-28 14:40:03 +00:00
Dual = true;
2019-02-28 10:07:43 +00:00
}
}
2018-04-13 09:19:50 +00:00
else
{
2020-05-27 03:38:39 +00:00
float percentSliderOrSpinner = (float)beatmap.HitObjects.Count(h => h is IHasDuration) / beatmap.HitObjects.Count;
2018-04-13 09:19:50 +00:00
if (percentSliderOrSpinner < 0.2)
TargetColumns = 7;
else if (percentSliderOrSpinner < 0.3 || roundedCircleSize >= 5)
TargetColumns = roundedOverallDifficulty > 5 ? 7 : 6;
else if (percentSliderOrSpinner > 0.6)
TargetColumns = roundedOverallDifficulty > 4 ? 5 : 4;
else
TargetColumns = Math.Max(4, Math.Min((int)roundedOverallDifficulty + 1, 7));
}
}
public override bool CanConvert() => Beatmap.HitObjects.All(h => h is IHasXPosition);
protected override Beatmap<ManiaHitObject> ConvertBeatmap(IBeatmap original, CancellationToken cancellationToken)
2018-04-13 09:19:50 +00:00
{
BeatmapDifficulty difficulty = original.BeatmapInfo.BaseDifficulty;
int seed = (int)MathF.Round(difficulty.DrainRate + difficulty.CircleSize) * 20 + (int)(difficulty.OverallDifficulty * 41.2) + (int)MathF.Round(difficulty.ApproachRate);
2018-06-15 11:52:09 +00:00
Random = new FastRandom(seed);
2018-04-13 09:19:50 +00:00
return base.ConvertBeatmap(original, cancellationToken);
2018-04-13 09:19:50 +00:00
}
2019-02-28 10:07:43 +00:00
protected override Beatmap<ManiaHitObject> CreateBeatmap()
{
beatmap = new ManiaBeatmap(new StageDefinition { Columns = TargetColumns });
2019-03-01 05:30:58 +00:00
if (Dual)
2019-02-28 10:07:43 +00:00
beatmap.Stages.Add(new StageDefinition { Columns = TargetColumns });
return beatmap;
}
2018-04-13 09:19:50 +00:00
protected override IEnumerable<ManiaHitObject> ConvertHitObject(HitObject original, IBeatmap beatmap, CancellationToken cancellationToken)
2018-04-13 09:19:50 +00:00
{
2019-02-28 05:35:00 +00:00
if (original is ManiaHitObject maniaOriginal)
2018-04-13 09:19:50 +00:00
{
yield return maniaOriginal;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
yield break;
}
var objects = IsForCurrentRuleset ? generateSpecific(original, beatmap) : generateConverted(original, beatmap);
if (objects == null)
yield break;
foreach (ManiaHitObject obj in objects)
yield return obj;
}
private readonly List<double> prevNoteTimes = new List<double>(max_notes_for_density);
private double density = int.MaxValue;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
private void computeDensity(double newNoteTime)
{
if (prevNoteTimes.Count == max_notes_for_density)
prevNoteTimes.RemoveAt(0);
prevNoteTimes.Add(newNoteTime);
2019-12-14 12:54:22 +00:00
density = (prevNoteTimes[^1] - prevNoteTimes[0]) / prevNoteTimes.Count;
2018-04-13 09:19:50 +00:00
}
private double lastTime;
private Vector2 lastPosition;
2018-06-15 13:08:24 +00:00
private PatternType lastStair = PatternType.Stair;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
private void recordNote(double time, Vector2 position)
{
lastTime = time;
lastPosition = position;
}
/// <summary>
/// Method that generates hit objects for osu!mania specific beatmaps.
/// </summary>
/// <param name="original">The original hit object.</param>
/// <param name="originalBeatmap">The original beatmap. This is used to look-up any values dependent on a fully-loaded beatmap.</param>
/// <returns>The hit objects generated.</returns>
2018-04-19 11:44:38 +00:00
private IEnumerable<ManiaHitObject> generateSpecific(HitObject original, IBeatmap originalBeatmap)
2018-04-13 09:19:50 +00:00
{
2018-06-15 11:52:09 +00:00
var generator = new SpecificBeatmapPatternGenerator(Random, original, beatmap, lastPattern, originalBeatmap);
2018-04-13 09:19:50 +00:00
foreach (var newPattern in generator.Generate())
{
lastPattern = newPattern;
2018-04-13 09:19:50 +00:00
foreach (var obj in newPattern.HitObjects)
yield return obj;
}
2018-04-13 09:19:50 +00:00
}
/// <summary>
/// Method that generates hit objects for non-osu!mania beatmaps.
/// </summary>
/// <param name="original">The original hit object.</param>
/// <param name="originalBeatmap">The original beatmap. This is used to look-up any values dependent on a fully-loaded beatmap.</param>
/// <returns>The hit objects generated.</returns>
2018-04-19 11:44:38 +00:00
private IEnumerable<ManiaHitObject> generateConverted(HitObject original, IBeatmap originalBeatmap)
2018-04-13 09:19:50 +00:00
{
Patterns.PatternGenerator conversion = null;
2019-11-12 10:16:51 +00:00
switch (original)
2018-06-15 11:52:09 +00:00
{
2019-11-12 10:16:51 +00:00
case IHasDistance _:
{
var generator = new DistanceObjectPatternGenerator(Random, original, beatmap, lastPattern, originalBeatmap);
conversion = generator;
var positionData = original as IHasPosition;
for (int i = 0; i <= generator.SpanCount; i++)
2019-11-12 10:16:51 +00:00
{
double time = original.StartTime + generator.SegmentDuration * i;
2019-11-12 10:16:51 +00:00
recordNote(time, positionData?.Position ?? Vector2.Zero);
computeDensity(time);
}
2018-06-15 11:52:36 +00:00
2019-11-12 10:16:51 +00:00
break;
}
2020-05-27 03:38:39 +00:00
case IHasDuration endTimeData:
2018-06-15 11:52:36 +00:00
{
2019-11-12 10:16:51 +00:00
conversion = new EndTimeObjectPatternGenerator(Random, original, beatmap, originalBeatmap);
recordNote(endTimeData.EndTime, new Vector2(256, 192));
computeDensity(endTimeData.EndTime);
break;
2018-06-15 11:52:36 +00:00
}
2019-11-12 10:16:51 +00:00
case IHasPosition positionData:
{
computeDensity(original.StartTime);
2018-04-13 09:19:50 +00:00
2019-11-12 10:16:51 +00:00
conversion = new HitObjectPatternGenerator(Random, original, beatmap, lastPattern, lastTime, lastPosition, density, lastStair, originalBeatmap);
2018-04-13 09:19:50 +00:00
2019-11-12 10:16:51 +00:00
recordNote(original.StartTime, positionData.Position);
break;
}
2018-04-13 09:19:50 +00:00
}
if (conversion == null)
yield break;
2018-04-13 09:19:50 +00:00
foreach (var newPattern in conversion.Generate())
{
lastPattern = conversion is EndTimeObjectPatternGenerator ? lastPattern : newPattern;
lastStair = (conversion as HitObjectPatternGenerator)?.StairType ?? lastStair;
2018-04-13 09:19:50 +00:00
foreach (var obj in newPattern.HitObjects)
yield return obj;
}
2018-04-13 09:19:50 +00:00
}
/// <summary>
/// A pattern generator for osu!mania-specific beatmaps.
/// </summary>
private class SpecificBeatmapPatternGenerator : Patterns.Legacy.PatternGenerator
{
2018-04-19 11:44:38 +00:00
public SpecificBeatmapPatternGenerator(FastRandom random, HitObject hitObject, ManiaBeatmap beatmap, Pattern previousPattern, IBeatmap originalBeatmap)
2018-04-13 09:19:50 +00:00
: base(random, hitObject, beatmap, previousPattern, originalBeatmap)
{
}
public override IEnumerable<Pattern> Generate()
{
yield return generate();
}
private Pattern generate()
2018-04-13 09:19:50 +00:00
{
var positionData = HitObject as IHasXPosition;
int column = GetColumn(positionData?.X ?? 0);
var pattern = new Pattern();
2020-05-27 03:38:39 +00:00
if (HitObject is IHasDuration endTimeData)
2018-04-13 09:19:50 +00:00
{
pattern.Add(new HoldNote
{
StartTime = HitObject.StartTime,
Duration = endTimeData.Duration,
2020-04-22 04:45:12 +00:00
Column = column,
2020-04-21 07:33:19 +00:00
Samples = HitObject.Samples,
NodeSamples = (HitObject as IHasRepeats)?.NodeSamples ?? defaultNodeSamples
2018-04-13 09:19:50 +00:00
});
}
2019-11-12 10:16:51 +00:00
else if (HitObject is IHasXPosition)
2018-04-13 09:19:50 +00:00
{
pattern.Add(new Note
{
StartTime = HitObject.StartTime,
Samples = HitObject.Samples,
Column = column
});
}
return pattern;
}
/// <remarks>
/// osu!mania-specific beatmaps in stable only play samples at the start of the hold note.
/// </remarks>
private List<IList<HitSampleInfo>> defaultNodeSamples
=> new List<IList<HitSampleInfo>>
{
HitObject.Samples,
new List<HitSampleInfo>()
};
2018-04-13 09:19:50 +00:00
}
}
}