1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 07:23:22 +08:00
osu-lazer/osu.Game.Rulesets.Mania/Beatmaps/ManiaBeatmapConverter.cs

227 lines
8.8 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-03-11 23:34:21 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
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 osu.Game.Beatmaps;
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.MathUtils;
using osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy;
using OpenTK;
using osu.Game.Audio;
2017-03-11 23:34:21 +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;
protected override IEnumerable<Type> ValidConversionTypes { get; } = new[] { typeof(IHasXPosition) };
2017-04-17 14:44:46 +08:00
public int TargetColumns;
public readonly bool IsForCurrentRuleset;
2017-05-19 15:31:05 +08:00
private Pattern lastPattern = new Pattern();
private FastRandom random;
private ManiaBeatmap beatmap;
public ManiaBeatmapConverter(bool isForCurrentRuleset, Beatmap original)
2017-03-11 23:34:21 +08:00
{
IsForCurrentRuleset = isForCurrentRuleset;
2017-08-22 15:46:12 +08:00
2018-01-04 18:52:29 +08:00
var roundedCircleSize = Math.Round(original.BeatmapInfo.BaseDifficulty.CircleSize);
var roundedOverallDifficulty = Math.Round(original.BeatmapInfo.BaseDifficulty.OverallDifficulty);
if (isForCurrentRuleset)
2018-01-04 18:52:29 +08:00
TargetColumns = (int)Math.Max(1, roundedCircleSize);
else
{
float percentSliderOrSpinner = (float)original.HitObjects.Count(h => h is IHasEndTime) / original.HitObjects.Count;
if (percentSliderOrSpinner < 0.2)
TargetColumns = 7;
2018-01-04 18:52:29 +08:00
else if (percentSliderOrSpinner < 0.3 || roundedCircleSize >= 5)
TargetColumns = roundedOverallDifficulty > 5 ? 7 : 6;
else if (percentSliderOrSpinner > 0.6)
2018-01-04 18:52:29 +08:00
TargetColumns = roundedOverallDifficulty > 4 ? 5 : 4;
else
2018-01-04 18:52:29 +08:00
TargetColumns = Math.Max(4, Math.Min((int)roundedOverallDifficulty + 1, 7));
}
}
protected override Beatmap<ManiaHitObject> ConvertBeatmap(Beatmap original)
{
2017-10-19 13:05:11 +08:00
BeatmapDifficulty difficulty = original.BeatmapInfo.BaseDifficulty;
2017-05-19 15:31:05 +08:00
int seed = (int)Math.Round(difficulty.DrainRate + difficulty.CircleSize) * 20 + (int)(difficulty.OverallDifficulty * 41.2) + (int)Math.Round(difficulty.ApproachRate);
random = new FastRandom(seed);
2017-08-22 13:18:17 +08:00
return base.ConvertBeatmap(original);
2017-03-11 23:34:21 +08:00
}
2018-01-03 18:13:42 +08:00
protected override Beatmap<ManiaHitObject> CreateBeatmap() => beatmap = new ManiaBeatmap(new StageDefinition { Columns = TargetColumns });
protected override IEnumerable<ManiaHitObject> ConvertHitObject(HitObject original, Beatmap beatmap)
{
var maniaOriginal = original as ManiaHitObject;
if (maniaOriginal != null)
{
yield return maniaOriginal;
yield break;
}
var objects = IsForCurrentRuleset ? generateSpecific(original) : generateConverted(original);
2017-05-19 15:31:05 +08:00
if (objects == null)
yield break;
foreach (ManiaHitObject obj in objects)
yield return obj;
}
2017-05-22 12:43:53 +08:00
private readonly List<double> prevNoteTimes = new List<double>(max_notes_for_density);
private double density = int.MaxValue;
private void computeDensity(double newNoteTime)
{
if (prevNoteTimes.Count == max_notes_for_density)
prevNoteTimes.RemoveAt(0);
prevNoteTimes.Add(newNoteTime);
density = (prevNoteTimes[prevNoteTimes.Count - 1] - prevNoteTimes[0]) / prevNoteTimes.Count;
}
private double lastTime;
private Vector2 lastPosition;
private PatternType lastStair;
private void recordNote(double time, Vector2 position)
{
lastTime = time;
lastPosition = position;
}
2017-05-19 15:39:02 +08:00
/// <summary>
/// Method that generates hit objects for osu!mania specific beatmaps.
/// </summary>
/// <param name="original">The original hit object.</param>
/// <returns>The hit objects generated.</returns>
2017-05-19 15:49:05 +08:00
private IEnumerable<ManiaHitObject> generateSpecific(HitObject original)
2017-05-19 15:31:05 +08:00
{
var generator = new SpecificBeatmapPatternGenerator(random, original, beatmap, lastPattern);
2017-05-19 15:31:05 +08:00
Pattern newPattern = generator.Generate();
lastPattern = newPattern;
return newPattern.HitObjects;
}
2017-05-19 15:39:02 +08:00
/// <summary>
/// Method that generates hit objects for non-osu!mania beatmaps.
/// </summary>
/// <param name="original">The original hit object.</param>
/// <returns>The hit objects generated.</returns>
2017-05-19 15:49:05 +08:00
private IEnumerable<ManiaHitObject> generateConverted(HitObject original)
2017-05-19 15:31:05 +08:00
{
var endTimeData = original as IHasEndTime;
var distanceData = original as IHasDistance;
var positionData = original as IHasPosition;
// Following lines currently commented out to appease resharper
Patterns.PatternGenerator conversion = null;
2017-05-19 15:31:05 +08:00
if (distanceData != null)
conversion = new DistanceObjectPatternGenerator(random, original, beatmap, lastPattern);
2017-05-19 15:31:05 +08:00
else if (endTimeData != null)
conversion = new EndTimeObjectPatternGenerator(random, original, beatmap);
2017-05-19 15:31:05 +08:00
else if (positionData != null)
{
computeDensity(original.StartTime);
conversion = new HitObjectPatternGenerator(random, original, beatmap, lastPattern, lastTime, lastPosition, density, lastStair);
recordNote(original.StartTime, positionData.Position);
2017-05-19 15:31:05 +08:00
}
if (conversion == null)
return null;
2017-05-19 15:31:05 +08:00
Pattern newPattern = conversion.Generate();
lastPattern = newPattern;
var stairPatternGenerator = conversion as HitObjectPatternGenerator;
lastStair = stairPatternGenerator?.StairType ?? lastStair;
2017-05-19 15:31:05 +08:00
return newPattern.HitObjects;
2017-05-19 15:31:05 +08:00
}
/// <summary>
2017-05-19 15:39:02 +08:00
/// A pattern generator for osu!mania-specific beatmaps.
2017-05-19 15:31:05 +08:00
/// </summary>
private class SpecificBeatmapPatternGenerator : Patterns.Legacy.PatternGenerator
{
public SpecificBeatmapPatternGenerator(FastRandom random, HitObject hitObject, ManiaBeatmap beatmap, Pattern previousPattern)
: base(random, hitObject, beatmap, previousPattern)
2017-05-19 15:31:05 +08:00
{
}
public override Pattern Generate()
{
var endTimeData = HitObject as IHasEndTime;
var positionData = HitObject as IHasXPosition;
int column = GetColumn(positionData?.X ?? 0);
2017-05-19 15:31:05 +08:00
var pattern = new Pattern();
2017-05-19 15:31:05 +08:00
if (endTimeData != null)
{
pattern.Add(new HoldNote
{
StartTime = HitObject.StartTime,
Duration = endTimeData.Duration,
Column = column,
Head = { Samples = sampleInfoListAt(HitObject.StartTime) },
Tail = { Samples = sampleInfoListAt(endTimeData.EndTime) },
2017-05-19 15:31:05 +08:00
});
}
else if (positionData != null)
{
pattern.Add(new Note
{
StartTime = HitObject.StartTime,
Samples = HitObject.Samples,
Column = column
});
}
return pattern;
}
/// <summary>
/// Retrieves the sample info list at a point in time.
/// </summary>
/// <param name="time">The time to retrieve the sample info list from.</param>
/// <returns></returns>
2017-12-25 14:35:28 +08:00
private List<SampleInfo> sampleInfoListAt(double time)
{
var curveData = HitObject as IHasCurve;
if (curveData == null)
return HitObject.Samples;
double segmentTime = (curveData.EndTime - HitObject.StartTime) / curveData.SpanCount();
int index = (int)(segmentTime == 0 ? 0 : (time - HitObject.StartTime) / segmentTime);
return curveData.RepeatSamples[index];
}
}
2017-03-11 23:34:21 +08:00
}
}