2019-02-12 16:03:28 +09:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-01-24 17:43:03 +09:00
// See the LICENCE file in the repository root for full licence text.
2018-04-13 18:19:50 +09:00
using System ;
using System.Linq ;
2019-02-12 16:03:28 +09:00
using osu.Game.Rulesets.Difficulty.Preprocessing ;
using osu.Game.Rulesets.Objects ;
2018-04-13 18:19:50 +09:00
using osu.Game.Rulesets.Osu.Objects ;
2018-11-20 16:51:59 +09:00
using osuTK ;
2018-04-13 18:19:50 +09:00
2018-05-15 17:36:29 +09:00
namespace osu.Game.Rulesets.Osu.Difficulty.Preprocessing
2018-04-13 18:19:50 +09:00
{
2019-02-12 16:03:28 +09:00
public class OsuDifficultyHitObject : DifficultyHitObject
2018-04-13 18:19:50 +09:00
{
2021-09-25 03:02:33 +00:00
private const int normalized_radius = 50 ; // Change radius to 50 to make 100 the diameter. Easier for mental maths.
2021-10-21 16:00:57 +00:00
private const int min_delta_time = 25 ;
2018-05-15 21:44:45 +09:00
2019-02-12 16:03:28 +09:00
protected new OsuHitObject BaseObject = > ( OsuHitObject ) base . BaseObject ;
2018-04-13 18:19:50 +09:00
2021-09-15 11:24:48 +01:00
/// <summary>
2021-09-25 03:02:33 +00:00
/// Normalized distance from the end position of the previous <see cref="OsuDifficultyHitObject"/> to the start position of this <see cref="OsuDifficultyHitObject"/>.
2021-09-15 11:24:48 +01:00
/// </summary>
2021-09-25 03:02:33 +00:00
public double JumpDistance { get ; private set ; }
2021-09-15 11:24:48 +01:00
2018-04-13 18:19:50 +09:00
/// <summary>
2021-10-13 15:41:24 +00:00
/// Minimum distance from the end position of the previous <see cref="OsuDifficultyHitObject"/> to the start position of this <see cref="OsuDifficultyHitObject"/>.
2018-04-13 18:19:50 +09:00
/// </summary>
2021-10-13 15:41:24 +00:00
public double MovementDistance { get ; private set ; }
2018-10-08 17:37:33 +09:00
2021-10-21 16:00:57 +00:00
/// <summary>
2018-10-09 12:03:47 +09:00
/// Normalized distance between the start and end position of the previous <see cref="OsuDifficultyHitObject"/>.
2018-10-08 17:37:33 +09:00
/// </summary>
public double TravelDistance { get ; private set ; }
2018-04-13 18:19:50 +09:00
/// <summary>
2019-02-12 16:03:28 +09:00
/// Angle the player has to take to hit this <see cref="OsuDifficultyHitObject"/>.
/// Calculated as the angle between the circles (current-2, current-1, current).
2018-04-13 18:19:50 +09:00
/// </summary>
2019-02-12 16:03:28 +09:00
public double? Angle { get ; private set ; }
2018-04-13 18:19:50 +09:00
2021-10-13 15:41:24 +00:00
/// <summary>
2021-11-02 23:47:20 +09:00
/// Milliseconds elapsed since the end time of the previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 25ms.
2021-10-13 15:41:24 +00:00
/// </summary>
public double MovementTime { get ; private set ; }
/// <summary>
2021-11-02 23:47:20 +09:00
/// Milliseconds elapsed since the start time of the previous <see cref="OsuDifficultyHitObject"/> to the end time of the same previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 25ms.
2021-10-13 15:41:24 +00:00
/// </summary>
public double TravelTime { get ; private set ; }
2021-09-25 03:02:33 +00:00
/// <summary>
2021-09-26 20:39:57 -05:00
/// Milliseconds elapsed since the start time of the previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 25ms.
2021-09-25 03:02:33 +00:00
/// </summary>
public readonly double StrainTime ;
2018-12-09 20:42:27 +09:00
private readonly OsuHitObject lastLastObject ;
2018-05-15 21:22:57 +09:00
private readonly OsuHitObject lastObject ;
2018-04-13 18:19:50 +09:00
2019-02-19 17:43:12 +09:00
public OsuDifficultyHitObject ( HitObject hitObject , HitObject lastLastObject , HitObject lastObject , double clockRate )
: base ( hitObject , lastObject , clockRate )
2018-04-13 18:19:50 +09:00
{
2019-02-12 16:03:28 +09:00
this . lastLastObject = ( OsuHitObject ) lastLastObject ;
this . lastObject = ( OsuHitObject ) lastObject ;
2018-05-15 21:22:57 +09:00
2021-11-02 23:47:20 +09:00
// Capped to 25ms to prevent difficulty calculation breaking from simultaneous objects.
2021-10-21 16:00:57 +00:00
StrainTime = Math . Max ( DeltaTime , min_delta_time ) ;
2021-10-13 15:41:24 +00:00
setDistances ( clockRate ) ;
2018-04-13 18:19:50 +09:00
}
2021-10-13 15:41:24 +00:00
private void setDistances ( double clockRate )
2018-04-13 18:19:50 +09:00
{
2021-10-09 21:11:24 +03:00
// We don't need to calculate either angle or distance when one of the last->curr objects is a spinner
2021-10-09 20:28:42 +03:00
if ( BaseObject is Spinner | | lastObject is Spinner )
return ;
2018-04-13 18:19:50 +09:00
// We will scale distances by this factor, so we can assume a uniform CircleSize among beatmaps.
2018-10-08 17:37:33 +09:00
float scalingFactor = normalized_radius / ( float ) BaseObject . Radius ;
2019-04-01 12:16:05 +09:00
2018-04-13 18:19:50 +09:00
if ( BaseObject . Radius < 30 )
{
2018-12-21 22:52:27 +09:00
float smallCircleBonus = Math . Min ( 30 - ( float ) BaseObject . Radius , 5 ) / 50 ;
2018-04-13 18:19:50 +09:00
scalingFactor * = 1 + smallCircleBonus ;
}
2018-12-09 20:42:27 +09:00
if ( lastObject is Slider lastSlider )
2018-12-23 16:26:23 +09:00
{
computeSliderCursorPosition ( lastSlider ) ;
2021-10-21 16:00:57 +00:00
TravelDistance = lastSlider . LazyTravelDistance * scalingFactor ;
TravelTime = Math . Max ( lastSlider . LazyTravelTime / clockRate , min_delta_time ) ;
MovementTime = Math . Max ( StrainTime - TravelTime , min_delta_time ) ;
2021-10-13 16:04:39 +00:00
MovementDistance = Vector2 . Subtract ( lastSlider . TailCircle . StackedPosition , BaseObject . StackedPosition ) . Length * scalingFactor ;
2018-12-23 16:26:23 +09:00
}
2018-12-09 20:42:27 +09:00
Vector2 lastCursorPosition = getEndCursorPosition ( lastObject ) ;
2018-04-13 18:19:50 +09:00
2021-10-21 16:00:57 +00:00
JumpDistance = ( BaseObject . StackedPosition * scalingFactor - lastCursorPosition * scalingFactor ) . Length ;
MovementDistance = Math . Min ( JumpDistance , MovementDistance ) ;
2018-12-08 15:01:26 +09:00
2021-10-09 20:28:42 +03:00
if ( lastLastObject ! = null & & ! ( lastLastObject is Spinner ) )
2018-12-08 15:01:26 +09:00
{
2018-12-09 20:42:27 +09:00
Vector2 lastLastCursorPosition = getEndCursorPosition ( lastLastObject ) ;
2018-12-08 15:01:26 +09:00
2018-12-09 20:42:27 +09:00
Vector2 v1 = lastLastCursorPosition - lastObject . StackedPosition ;
Vector2 v2 = BaseObject . StackedPosition - lastCursorPosition ;
2018-12-08 15:01:26 +09:00
float dot = Vector2 . Dot ( v1 , v2 ) ;
float det = v1 . X * v2 . Y - v1 . Y * v2 . X ;
2018-12-09 20:31:04 +09:00
Angle = Math . Abs ( Math . Atan2 ( det , dot ) ) ;
2018-12-08 15:01:26 +09:00
}
2018-04-13 18:19:50 +09:00
}
private void computeSliderCursorPosition ( Slider slider )
{
if ( slider . LazyEndPosition ! = null )
return ;
2019-02-28 13:31:40 +09:00
2018-10-11 13:53:29 +09:00
slider . LazyEndPosition = slider . StackedPosition ;
2018-04-13 18:19:50 +09:00
2021-10-21 16:00:57 +00:00
float followCircleRadius = ( float ) ( slider . Radius * 2.4 ) ;
2018-04-13 18:19:50 +09:00
var computeVertex = new Action < double > ( t = >
{
2019-01-15 19:07:25 +09:00
double progress = ( t - slider . StartTime ) / slider . SpanDuration ;
if ( progress % 2 > = 1 )
2018-10-08 18:37:30 +09:00
progress = 1 - progress % 1 ;
else
2019-11-12 17:56:38 +08:00
progress % = 1 ;
2018-10-08 18:37:30 +09:00
2018-04-13 18:19:50 +09:00
// ReSharper disable once PossibleInvalidOperationException (bugged in current r# version)
2018-11-01 03:52:24 +09:00
var diff = slider . StackedPosition + slider . Path . PositionAt ( progress ) - slider . LazyEndPosition . Value ;
2018-04-13 18:19:50 +09:00
float dist = diff . Length ;
2021-10-13 15:41:24 +00:00
slider . LazyTravelTime = t - slider . StartTime ;
2021-10-21 16:00:57 +00:00
if ( dist > followCircleRadius )
2018-04-13 18:19:50 +09:00
{
// The cursor would be outside the follow circle, we need to move it
diff . Normalize ( ) ; // Obtain direction of diff
2021-10-21 16:00:57 +00:00
dist - = followCircleRadius ;
2018-04-13 18:19:50 +09:00
slider . LazyEndPosition + = diff * dist ;
slider . LazyTravelDistance + = dist ;
}
} ) ;
2018-05-15 21:25:33 +09:00
// Skip the head circle
var scoringTimes = slider . NestedHitObjects . Skip ( 1 ) . Select ( t = > t . StartTime ) ;
2021-10-27 13:04:41 +09:00
foreach ( double time in scoringTimes )
2018-04-13 18:19:50 +09:00
computeVertex ( time ) ;
}
2018-12-09 20:42:27 +09:00
private Vector2 getEndCursorPosition ( OsuHitObject hitObject )
{
Vector2 pos = hitObject . StackedPosition ;
2019-02-28 14:35:00 +09:00
if ( hitObject is Slider slider )
2018-12-09 20:42:27 +09:00
{
computeSliderCursorPosition ( slider ) ;
pos = slider . LazyEndPosition ? ? pos ;
}
return pos ;
}
2018-04-13 18:19:50 +09:00
}
}