1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-31 09:52:58 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Beatmaps/ManiaBeatmapConverter.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

256 lines
9.8 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 17:19:50 +08:00
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Mania.Objects;
2017-04-17 14:44:46 +08:00
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Legacy;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
2017-05-19 15:31:05 +08:00
using osu.Game.Rulesets.Mania.Beatmaps.Patterns;
using osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects.Legacy;
using osu.Game.Rulesets.Scoring.Legacy;
using osu.Game.Utils;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.Mania.Beatmaps
2017-03-11 23:34:21 +08:00
{
public class ManiaBeatmapConverter : BeatmapConverter<ManiaHitObject>
2017-03-11 23:34:21 +08:00
{
/// <summary>
/// Maximum number of previous notes to consider for density calculation.
/// </summary>
private const int max_notes_for_density = 7;
2018-04-13 17:19:50 +08:00
/// <summary>
/// The total number of columns.
/// </summary>
public int TotalColumns => TargetColumns * (Dual ? 2 : 1);
/// <summary>
/// The number of columns per-stage.
/// </summary>
public int TargetColumns;
/// <summary>
/// Whether to double the number of stages.
/// </summary>
2019-02-28 22:40:03 +08:00
public bool Dual;
/// <summary>
/// Whether the beatmap instantiated with is for the mania ruleset.
/// </summary>
public readonly bool IsForCurrentRuleset;
2018-04-13 17:19:50 +08:00
2018-06-15 19:52:09 +08:00
// Internal for testing purposes
internal readonly LegacyRandom Random;
2018-06-15 19:52:09 +08:00
2017-05-19 15:31:05 +08:00
private Pattern lastPattern = new Pattern();
2018-04-13 17:19:50 +08:00
public ManiaBeatmapConverter(IBeatmap beatmap, Ruleset ruleset)
: this(beatmap, LegacyBeatmapConversionDifficultyInfo.FromBeatmap(beatmap), ruleset)
{
}
private ManiaBeatmapConverter(IBeatmap? beatmap, LegacyBeatmapConversionDifficultyInfo difficulty, Ruleset ruleset)
: base(beatmap!, ruleset)
2017-03-11 23:34:21 +08:00
{
IsForCurrentRuleset = difficulty.SourceRuleset.Equals(ruleset.RulesetInfo);
Random = new LegacyRandom((int)MathF.Round(difficulty.DrainRate + difficulty.CircleSize) * 20 + (int)(difficulty.OverallDifficulty * 41.2) + (int)MathF.Round(difficulty.ApproachRate));
TargetColumns = getColumnCount(difficulty);
2018-05-07 10:23:29 +08:00
if (IsForCurrentRuleset && TargetColumns > ManiaRuleset.MAX_STAGE_KEYS)
{
TargetColumns /= 2;
Dual = true;
}
static int getColumnCount(LegacyBeatmapConversionDifficultyInfo difficulty)
{
double roundedCircleSize = Math.Round(difficulty.CircleSize);
if (difficulty.SourceRuleset.ShortName == ManiaRuleset.SHORT_NAME)
return (int)Math.Max(1, roundedCircleSize);
double roundedOverallDifficulty = Math.Round(difficulty.OverallDifficulty);
if (difficulty.TotalObjectCount > 0 && difficulty.EndTimeObjectCount >= 0)
{
int countSliderOrSpinner = difficulty.EndTimeObjectCount;
// In osu!stable, this division appears as if it happens on floats, but due to release-mode
// optimisations, it actually ends up happening on doubles.
double percentSpecialObjects = (double)countSliderOrSpinner / difficulty.TotalObjectCount;
if (percentSpecialObjects < 0.2)
return 7;
if (percentSpecialObjects < 0.3 || roundedCircleSize >= 5)
return roundedOverallDifficulty > 5 ? 7 : 6;
if (percentSpecialObjects > 0.6)
return roundedOverallDifficulty > 4 ? 5 : 4;
}
return Math.Max(4, Math.Min((int)roundedOverallDifficulty + 1, 7));
}
}
public static int GetColumnCount(LegacyBeatmapConversionDifficultyInfo difficulty, IReadOnlyList<Mod>? mods = null)
{
var converter = new ManiaBeatmapConverter(null, difficulty, new ManiaRuleset());
2018-04-13 17:19:50 +08:00
if (mods != null)
{
foreach (var m in mods.OfType<IApplicableToBeatmapConverter>())
m.ApplyToBeatmapConverter(converter);
}
2018-04-13 17:19:50 +08:00
return converter.TotalColumns;
2017-03-11 23:34:21 +08:00
}
2018-04-13 17:19:50 +08:00
public override bool CanConvert() => Beatmap.HitObjects.All(h => h is IHasXPosition);
2019-02-28 18:07:43 +08:00
protected override Beatmap<ManiaHitObject> CreateBeatmap()
{
2024-03-28 21:47:43 +08:00
ManiaBeatmap beatmap = new ManiaBeatmap(new StageDefinition(TargetColumns));
2019-02-28 18:07:43 +08:00
2019-03-01 13:30:58 +08:00
if (Dual)
beatmap.Stages.Add(new StageDefinition(TargetColumns));
2019-02-28 18:07:43 +08:00
return beatmap;
}
2018-04-13 17:19:50 +08:00
protected override IEnumerable<ManiaHitObject> ConvertHitObject(HitObject original, IBeatmap beatmap, CancellationToken cancellationToken)
{
LegacyHitObjectType legacyType;
switch (original)
{
case ManiaHitObject maniaObj:
{
yield return maniaObj;
2019-02-28 12:31:40 +08:00
yield break;
}
2018-04-13 17:19:50 +08:00
case IHasLegacyHitObjectType legacy:
legacyType = legacy.LegacyType & LegacyHitObjectType.ObjectTypes;
break;
case IHasPath:
legacyType = LegacyHitObjectType.Slider;
break;
case IHasDuration:
legacyType = LegacyHitObjectType.Hold;
break;
default:
legacyType = LegacyHitObjectType.Circle;
break;
}
2018-04-13 17:19:50 +08:00
double startTime = original.StartTime;
double endTime = (original as IHasDuration)?.EndTime ?? startTime;
Vector2 position = (original as IHasPosition)?.Position ?? Vector2.Zero;
2018-04-13 17:19:50 +08:00
PatternGenerator conversion;
2018-04-13 17:19:50 +08:00
switch (legacyType)
2018-06-15 19:52:09 +08:00
{
case LegacyHitObjectType.Circle:
if (IsForCurrentRuleset)
{
conversion = new PassThroughPatternGenerator(Random, original, beatmap, TotalColumns, lastPattern);
recordNote(startTime, position);
}
else
{
// Note: The density is used during the pattern generator constructor, and intentionally computed first.
computeDensity(startTime);
conversion = new HitCirclePatternGenerator(Random, original, beatmap, TotalColumns, lastPattern, lastTime, lastPosition, density, lastStair);
recordNote(startTime, position);
}
2019-11-12 18:16:51 +08:00
break;
2019-11-12 18:16:51 +08:00
case LegacyHitObjectType.Slider:
if (IsForCurrentRuleset)
2019-11-12 18:16:51 +08:00
{
conversion = new PassThroughPatternGenerator(Random, original, beatmap, TotalColumns, lastPattern);
recordNote(original.StartTime, position);
2019-11-12 18:16:51 +08:00
}
else
{
var generator = new SliderPatternGenerator(Random, original, beatmap, TotalColumns, lastPattern);
conversion = generator;
2018-06-15 19:52:36 +08:00
for (int i = 0; i <= generator.SpanCount; i++)
{
double time = original.StartTime + generator.SegmentDuration * i;
2019-11-12 18:16:51 +08:00
recordNote(time, position);
computeDensity(time);
}
}
2019-11-12 18:16:51 +08:00
break;
2018-06-15 19:52:36 +08:00
case LegacyHitObjectType.Spinner:
// Note: Some older mania-specific beatmaps can have spinners that are converted rather than passed through.
// Newer beatmaps will usually use the "hold" hitobject type below.
conversion = new SpinnerPatternGenerator(Random, original, beatmap, TotalColumns, lastPattern);
recordNote(endTime, new Vector2(256, 192));
computeDensity(endTime);
break;
2018-04-13 17:19:50 +08:00
case LegacyHitObjectType.Hold:
conversion = new PassThroughPatternGenerator(Random, original, beatmap, TotalColumns, lastPattern);
recordNote(endTime, position);
computeDensity(endTime);
2019-11-12 18:16:51 +08:00
break;
2018-04-13 17:19:50 +08:00
default:
throw new ArgumentException($"Invalid legacy object type: {legacyType}", nameof(original));
}
2018-04-13 17:19:50 +08:00
foreach (var newPattern in conversion.Generate())
{
if (conversion is HitCirclePatternGenerator circleGenerator)
lastStair = circleGenerator.StairType;
if (conversion is HitCirclePatternGenerator || conversion is SliderPatternGenerator)
lastPattern = newPattern;
2018-04-13 17:19:50 +08:00
foreach (var obj in newPattern.HitObjects)
yield return obj;
}
2017-05-19 15:31:05 +08:00
}
2018-04-13 17:19:50 +08:00
private readonly LimitedCapacityQueue<double> prevNoteTimes = new LimitedCapacityQueue<double>(max_notes_for_density);
private double density = int.MaxValue;
private void computeDensity(double newNoteTime)
{
prevNoteTimes.Enqueue(newNoteTime);
if (prevNoteTimes.Count >= 2)
density = (prevNoteTimes[^1] - prevNoteTimes[0]) / prevNoteTimes.Count;
}
private double lastTime;
private Vector2 lastPosition;
private PatternType lastStair = PatternType.Stair;
private void recordNote(double time, Vector2 position)
{
lastTime = time;
lastPosition = position;
}
2017-03-11 23:34:21 +08:00
}
}