2019-01-24 08:43:03 +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.
2018-04-13 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
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.Taiko.Objects ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
2020-09-17 07:30:34 +00:00
using osu.Framework.Utils ;
2020-09-17 08:40:05 +00:00
using System.Threading ;
2021-10-06 06:10:42 +00:00
using JetBrains.Annotations ;
2018-04-13 09:19:50 +00:00
using osu.Game.Audio ;
using osu.Game.Beatmaps.ControlPoints ;
2020-07-13 08:06:00 +00:00
using osu.Game.Beatmaps.Formats ;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Taiko.Beatmaps
{
internal class TaikoBeatmapConverter : BeatmapConverter < TaikoHitObject >
{
/// <summary>
/// Because swells are easier in taiko than spinners are in osu!,
/// legacy taiko multiplies a factor when converting the number of required hits.
/// </summary>
private const float swell_hit_multiplier = 1.65f ;
/// <summary>
/// Base osu! slider scoring distance.
/// </summary>
private const float osu_base_scoring_distance = 100 ;
/// <summary>
/// Drum roll distance that results in a duration of 1 speed-adjusted beat length.
/// </summary>
private const float taiko_base_distance = 100 ;
private readonly bool isForCurrentRuleset ;
2019-12-24 07:02:16 +00:00
public TaikoBeatmapConverter ( IBeatmap beatmap , Ruleset ruleset )
: base ( beatmap , ruleset )
2018-04-13 09:19:50 +00:00
{
2019-12-24 07:02:16 +00:00
isForCurrentRuleset = beatmap . BeatmapInfo . Ruleset . Equals ( ruleset . RulesetInfo ) ;
2018-04-13 09:19:50 +00:00
}
2019-12-23 08:44:18 +00:00
public override bool CanConvert ( ) = > true ;
2020-09-17 08:40:05 +00:00
protected override Beatmap < TaikoHitObject > ConvertBeatmap ( IBeatmap original , CancellationToken cancellationToken )
2018-04-13 09:19:50 +00:00
{
2021-10-09 12:25:40 +00:00
if ( ! ( original . Difficulty is TaikoMultiplierAppliedDifficulty ) )
2021-08-30 06:30:17 +00:00
{
// Rewrite the beatmap info to add the slider velocity multiplier
2021-10-09 12:25:40 +00:00
original . Difficulty = new TaikoMultiplierAppliedDifficulty ( original . Difficulty ) ;
2021-08-30 06:30:17 +00:00
}
2018-04-13 09:19:50 +00:00
2020-09-17 08:40:05 +00:00
Beatmap < TaikoHitObject > converted = base . ConvertBeatmap ( original , cancellationToken ) ;
2018-04-13 09:19:50 +00:00
2022-01-27 06:19:48 +00:00
if ( original . BeatmapInfo . Ruleset . OnlineID = = 3 )
2018-04-13 09:19:50 +00:00
{
// Post processing step to transform mania hit objects with the same start time into strong hits
converted . HitObjects = converted . HitObjects . GroupBy ( t = > t . StartTime ) . Select ( x = >
{
TaikoHitObject first = x . First ( ) ;
2020-12-14 20:46:02 +00:00
if ( x . Skip ( 1 ) . Any ( ) & & first is TaikoStrongableHitObject strong )
2020-12-13 11:36:39 +00:00
strong . IsStrong = true ;
2018-04-13 09:19:50 +00:00
return first ;
} ) . ToList ( ) ;
}
return converted ;
}
2020-09-17 08:40:05 +00:00
protected override IEnumerable < TaikoHitObject > ConvertHitObject ( HitObject obj , IBeatmap beatmap , CancellationToken cancellationToken )
2018-04-13 09:19:50 +00:00
{
// Old osu! used hit sounding to determine various hit type information
2019-11-08 05:04:57 +00:00
IList < HitSampleInfo > samples = obj . Samples ;
2018-04-13 09:19:50 +00:00
2019-11-12 10:16:51 +00:00
switch ( obj )
2018-04-13 09:19:50 +00:00
{
2019-11-12 10:16:51 +00:00
case IHasDistance distanceData :
{
2021-10-27 04:04:41 +00:00
if ( shouldConvertSliderToHits ( obj , beatmap , distanceData , out int taikoDuration , out double tickSpacing ) )
2018-04-13 09:19:50 +00:00
{
2021-10-23 08:59:07 +00:00
IList < IList < HitSampleInfo > > allSamples = obj is IHasPathWithRepeats curveData ? curveData . NodeSamples : new List < IList < HitSampleInfo > > ( new [ ] { samples } ) ;
2018-04-13 09:19:50 +00:00
2019-11-12 10:16:51 +00:00
int i = 0 ;
for ( double j = obj . StartTime ; j < = obj . StartTime + taikoDuration + tickSpacing / 8 ; j + = tickSpacing )
2018-04-13 09:19:50 +00:00
{
2019-11-12 10:16:51 +00:00
IList < HitSampleInfo > currentSamples = allSamples [ i ] ;
2020-03-23 03:08:15 +00:00
yield return new Hit
2018-04-13 09:19:50 +00:00
{
2020-03-23 03:08:15 +00:00
StartTime = j ,
Samples = currentSamples ,
} ;
2019-11-12 10:16:51 +00:00
i = ( i + 1 ) % allSamples . Count ;
2020-09-18 04:06:41 +00:00
if ( Precision . AlmostEquals ( 0 , tickSpacing ) )
break ;
2018-04-13 09:19:50 +00:00
}
}
2019-11-12 10:16:51 +00:00
else
2018-04-13 09:19:50 +00:00
{
2019-11-12 10:16:51 +00:00
yield return new DrumRoll
{
StartTime = obj . StartTime ,
Samples = obj . Samples ,
Duration = taikoDuration ,
2021-10-02 03:34:29 +00:00
TickRate = beatmap . Difficulty . SliderTickRate = = 3 ? 3 : 4
2019-11-12 10:16:51 +00:00
} ;
}
break ;
2018-04-13 09:19:50 +00:00
}
2020-05-27 03:38:39 +00:00
case IHasDuration endTimeData :
2018-04-13 09:19:50 +00:00
{
2021-10-02 03:34:29 +00:00
double hitMultiplier = IBeatmapDifficultyInfo . DifficultyRange ( beatmap . Difficulty . OverallDifficulty , 3 , 5 , 7.5 ) * swell_hit_multiplier ;
2018-04-13 09:19:50 +00:00
2019-11-12 10:16:51 +00:00
yield return new Swell
2018-04-13 09:19:50 +00:00
{
StartTime = obj . StartTime ,
Samples = obj . Samples ,
2019-11-12 10:16:51 +00:00
Duration = endTimeData . Duration ,
RequiredHits = ( int ) Math . Max ( 1 , endTimeData . Duration / 1000 * hitMultiplier )
2018-04-13 09:19:50 +00:00
} ;
2019-11-12 10:16:51 +00:00
break ;
2018-04-13 09:19:50 +00:00
}
2019-11-12 10:16:51 +00:00
default :
2018-04-13 09:19:50 +00:00
{
2020-03-23 03:08:15 +00:00
yield return new Hit
2019-11-12 10:16:51 +00:00
{
2020-03-23 03:08:15 +00:00
StartTime = obj . StartTime ,
2020-05-11 03:53:54 +00:00
Samples = samples ,
2020-03-23 03:08:15 +00:00
} ;
2019-11-12 10:16:51 +00:00
break ;
2018-04-13 09:19:50 +00:00
}
}
}
2018-05-07 01:51:30 +00:00
2021-01-12 08:50:22 +00:00
private bool shouldConvertSliderToHits ( HitObject obj , IBeatmap beatmap , IHasDistance distanceData , out int taikoDuration , out double tickSpacing )
2020-07-13 08:06:00 +00:00
{
// DO NOT CHANGE OR REFACTOR ANYTHING IN HERE WITHOUT TESTING AGAINST _ALL_ BEATMAPS.
// Some of these calculations look redundant, but they are not - extremely small floating point errors are introduced to maintain 1:1 compatibility with stable.
// Rounding cannot be used as an alternative since the error deltas have been observed to be between 1e-2 and 1e-6.
// The true distance, accounting for any repeats. This ends up being the drum roll distance later
int spans = ( obj as IHasRepeats ) ? . SpanCount ( ) ? ? 1 ;
2021-08-30 05:40:25 +00:00
double distance = distanceData . Distance * spans * LegacyBeatmapEncoder . LEGACY_TAIKO_VELOCITY_MULTIPLIER ;
2020-07-13 08:06:00 +00:00
TimingControlPoint timingPoint = beatmap . ControlPointInfo . TimingPointAt ( obj . StartTime ) ;
2021-08-31 07:31:48 +00:00
DifficultyControlPoint difficultyPoint = obj . DifficultyControlPoint ;
2020-07-13 08:06:00 +00:00
double beatLength ;
#pragma warning disable 618
if ( difficultyPoint is LegacyBeatmapDecoder . LegacyDifficultyControlPoint legacyDifficultyPoint )
#pragma warning restore 618
beatLength = timingPoint . BeatLength * legacyDifficultyPoint . BpmMultiplier ;
else
2021-08-31 14:59:36 +00:00
beatLength = timingPoint . BeatLength / difficultyPoint . SliderVelocity ;
2020-07-13 08:06:00 +00:00
2021-10-02 03:34:29 +00:00
double sliderScoringPointDistance = osu_base_scoring_distance * beatmap . Difficulty . SliderMultiplier / beatmap . Difficulty . SliderTickRate ;
2020-07-13 08:06:00 +00:00
// The velocity and duration of the taiko hit object - calculated as the velocity of a drum roll.
2021-10-02 03:34:29 +00:00
double taikoVelocity = sliderScoringPointDistance * beatmap . Difficulty . SliderTickRate ;
2021-01-12 08:50:22 +00:00
taikoDuration = ( int ) ( distance / taikoVelocity * beatLength ) ;
2020-07-13 08:06:00 +00:00
if ( isForCurrentRuleset )
{
tickSpacing = 0 ;
return false ;
}
double osuVelocity = taikoVelocity * ( 1000f / beatLength ) ;
// osu-stable always uses the speed-adjusted beatlength to determine the osu! velocity, but only uses it for conversion if beatmap version < 8
if ( beatmap . BeatmapInfo . BeatmapVersion > = 8 )
beatLength = timingPoint . BeatLength ;
// If the drum roll is to be split into hit circles, assume the ticks are 1/8 spaced within the duration of one beat
2021-10-02 03:34:29 +00:00
tickSpacing = Math . Min ( beatLength / beatmap . Difficulty . SliderTickRate , ( double ) taikoDuration / spans ) ;
2020-07-13 08:06:00 +00:00
return tickSpacing > 0
& & distance / osuVelocity * 1000 < 2 * beatLength ;
}
2018-05-07 01:51:30 +00:00
protected override Beatmap < TaikoHitObject > CreateBeatmap ( ) = > new TaikoBeatmap ( ) ;
2021-08-30 06:30:17 +00:00
2022-01-11 05:19:35 +00:00
// Important to note that this is subclassing a realm object.
// Realm doesn't allow this, but for now this can work since we aren't (in theory?) persisting this to the database.
// It is only used during beatmap conversion and processing.
2021-11-08 12:23:24 +00:00
internal class TaikoMultiplierAppliedDifficulty : BeatmapDifficulty
2021-08-30 06:30:17 +00:00
{
2021-10-09 12:25:40 +00:00
public TaikoMultiplierAppliedDifficulty ( IBeatmapDifficultyInfo difficulty )
2021-08-30 06:30:17 +00:00
{
2021-10-01 05:56:42 +00:00
CopyFrom ( difficulty ) ;
2021-08-30 06:30:17 +00:00
}
2021-10-06 06:10:42 +00:00
[UsedImplicitly]
2021-10-09 12:25:40 +00:00
public TaikoMultiplierAppliedDifficulty ( )
2021-10-06 06:10:42 +00:00
{
}
#region Overrides of BeatmapDifficulty
2021-10-01 05:56:42 +00:00
2022-01-13 04:23:41 +00:00
public override BeatmapDifficulty Clone ( ) = > new TaikoMultiplierAppliedDifficulty ( this ) ;
2021-10-06 06:10:42 +00:00
public override void CopyTo ( BeatmapDifficulty other )
{
base . CopyTo ( other ) ;
2021-10-09 12:25:40 +00:00
if ( ! ( other is TaikoMultiplierAppliedDifficulty ) )
2021-11-08 12:23:54 +00:00
other . SliderMultiplier / = LegacyBeatmapEncoder . LEGACY_TAIKO_VELOCITY_MULTIPLIER ;
2021-08-30 06:30:17 +00:00
}
2021-10-06 06:10:42 +00:00
public override void CopyFrom ( IBeatmapDifficultyInfo other )
{
base . CopyFrom ( other ) ;
2021-10-09 12:25:40 +00:00
if ( ! ( other is TaikoMultiplierAppliedDifficulty ) )
2021-10-06 06:10:42 +00:00
SliderMultiplier * = LegacyBeatmapEncoder . LEGACY_TAIKO_VELOCITY_MULTIPLIER ;
}
#endregion
2021-08-30 06:30:17 +00:00
}
2018-04-13 09:19:50 +00:00
}
}