osu/osu.Game/Beatmaps/Beatmap.cs

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

131 lines
4.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 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
2021-02-08 09:52:50 +00:00
using System;
using osu.Game.Beatmaps.Timing;
2017-04-18 07:05:58 +00:00
using osu.Game.Rulesets.Objects;
2017-03-11 15:34:21 +00:00
using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps.ControlPoints;
using Newtonsoft.Json;
using osu.Game.IO.Serialization.Converters;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Beatmaps
{
public class Beatmap<T> : IBeatmap<T>
2017-03-11 15:34:21 +00:00
where T : HitObject
{
private BeatmapDifficulty difficulty = new BeatmapDifficulty();
public BeatmapDifficulty Difficulty
{
get => difficulty;
set
{
difficulty = value;
if (beatmapInfo != null)
beatmapInfo.Difficulty = difficulty.Clone();
}
}
private BeatmapInfo beatmapInfo;
public BeatmapInfo BeatmapInfo
{
get => beatmapInfo;
set
{
beatmapInfo = value;
if (beatmapInfo?.Difficulty != null)
Difficulty = beatmapInfo.Difficulty.Clone();
}
}
public Beatmap()
2018-04-19 09:14:21 +00:00
{
beatmapInfo = new BeatmapInfo
2018-04-19 09:14:21 +00:00
{
Metadata = new BeatmapMetadata
{
Artist = @"Unknown",
Title = @"Unknown",
2022-01-18 14:30:40 +00:00
Author = { Username = @"Unknown Creator" },
},
DifficultyName = @"Normal",
Difficulty = Difficulty,
};
}
2018-04-13 09:19:50 +00:00
[JsonIgnore]
public BeatmapMetadata Metadata => BeatmapInfo.Metadata;
2018-04-13 09:19:50 +00:00
2018-04-19 09:14:21 +00:00
public ControlPointInfo ControlPointInfo { get; set; } = new ControlPointInfo();
public List<BreakPeriod> Breaks { get; set; } = new List<BreakPeriod>();
2018-04-13 09:19:50 +00:00
2017-12-07 05:40:28 +00:00
[JsonIgnore]
public double TotalBreakTime => Breaks.Sum(b => b.Duration);
2018-04-13 09:19:50 +00:00
2018-04-19 09:14:21 +00:00
[JsonConverter(typeof(TypedListConverter<HitObject>))]
public List<T> HitObjects { get; set; } = new List<T>();
IReadOnlyList<T> IBeatmap<T>.HitObjects => HitObjects;
2018-04-19 09:14:21 +00:00
IReadOnlyList<HitObject> IBeatmap.HitObjects => HitObjects;
2018-04-19 09:14:21 +00:00
public virtual IEnumerable<BeatmapStatistic> GetStatistics() => Enumerable.Empty<BeatmapStatistic>();
2021-01-15 05:28:49 +00:00
public double GetMostCommonBeatLength()
{
2022-12-02 07:05:14 +00:00
double lastTime;
2021-01-15 05:28:49 +00:00
// The last playable time in the beatmap - the last timing point extends to this time.
// Note: This is more accurate and may present different results because osu-stable didn't have the ability to calculate slider durations in this context.
2022-12-02 07:05:14 +00:00
if (!HitObjects.Any())
lastTime = ControlPointInfo.TimingPoints.LastOrDefault()?.Time ?? 0;
else
lastTime = this.GetLastObjectTime();
2021-01-15 05:28:49 +00:00
var mostCommon =
2021-01-15 05:28:49 +00:00
// Construct a set of (beatLength, duration) tuples for each individual timing point.
ControlPointInfo.TimingPoints.Select((t, i) =>
{
if (t.Time > lastTime)
return (beatLength: t.BeatLength, 0);
2022-01-11 04:53:51 +00:00
// osu-stable forced the first control point to start at 0.
// This is reproduced here to maintain compatibility around osu!mania scroll speed and song select display.
double currentTime = i == 0 ? 0 : t.Time;
double nextTime = i == ControlPointInfo.TimingPoints.Count - 1 ? lastTime : ControlPointInfo.TimingPoints[i + 1].Time;
return (beatLength: t.BeatLength, duration: nextTime - currentTime);
2021-01-15 05:28:49 +00:00
})
// Aggregate durations into a set of (beatLength, duration) tuples for each beat length
2021-02-08 09:52:50 +00:00
.GroupBy(t => Math.Round(t.beatLength * 1000) / 1000)
2021-01-15 05:28:49 +00:00
.Select(g => (beatLength: g.Key, duration: g.Sum(t => t.duration)))
// Get the most common one, or 0 as a suitable default (see handling below)
.OrderByDescending(i => i.duration).FirstOrDefault();
2021-01-15 05:28:49 +00:00
if (mostCommon.beatLength == 0)
return TimingControlPoint.DEFAULT_BEAT_LENGTH;
return mostCommon.beatLength;
2021-01-15 05:28:49 +00:00
}
2018-04-19 09:50:21 +00:00
IBeatmap IBeatmap.Clone() => Clone();
public Beatmap<T> Clone() => (Beatmap<T>)MemberwiseClone();
2017-03-11 15:34:21 +00:00
}
2018-04-13 09:19:50 +00:00
2017-03-11 15:34:21 +00:00
public class Beatmap : Beatmap<HitObject>
{
public new Beatmap Clone() => (Beatmap)base.Clone();
2019-08-11 16:40:11 +00:00
public override string ToString() => BeatmapInfo?.ToString() ?? base.ToString();
}
}