2019-02-12 15:03:28 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-01-24 16:43:03 +08:00
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System ;
2019-02-12 15:03:28 +08:00
using osu.Game.Rulesets.Difficulty.Preprocessing ;
using osu.Game.Rulesets.Objects ;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Osu.Objects ;
2018-11-20 15:51:59 +08:00
using osuTK ;
2018-04-13 17:19:50 +08:00
2018-05-15 16:36:29 +08:00
namespace osu.Game.Rulesets.Osu.Difficulty.Preprocessing
2018-04-13 17:19:50 +08:00
{
2019-02-12 15:03:28 +08:00
public class OsuDifficultyHitObject : DifficultyHitObject
2018-04-13 17:19:50 +08:00
{
2021-09-25 11:02:33 +08:00
private const int normalized_radius = 50 ; // Change radius to 50 to make 100 the diameter. Easier for mental maths.
2021-10-22 00:00:57 +08:00
private const int min_delta_time = 25 ;
2021-11-07 03:16:58 +08:00
private const float maximum_slider_radius = normalized_radius * 2.4f ;
2021-11-11 08:42:06 +08:00
private const float assumed_slider_radius = normalized_radius * 1.8f ;
2018-05-15 20:44:45 +08:00
2019-02-12 15:03:28 +08:00
protected new OsuHitObject BaseObject = > ( OsuHitObject ) base . BaseObject ;
2018-04-13 17:19:50 +08:00
2021-09-15 18:24:48 +08:00
/// <summary>
2021-09-25 11:02:33 +08: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 18:24:48 +08:00
/// </summary>
2021-09-25 11:02:33 +08:00
public double JumpDistance { get ; private set ; }
2021-09-15 18:24:48 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
2021-10-13 23:41:24 +08: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 17:19:50 +08:00
/// </summary>
2021-10-13 23:41:24 +08:00
public double MovementDistance { get ; private set ; }
2018-10-08 16:37:33 +08:00
2021-10-22 00:00:57 +08:00
/// <summary>
2018-10-09 11:03:47 +08:00
/// Normalized distance between the start and end position of the previous <see cref="OsuDifficultyHitObject"/>.
2018-10-08 16:37:33 +08:00
/// </summary>
public double TravelDistance { get ; private set ; }
2018-04-13 17:19:50 +08:00
/// <summary>
2019-02-12 15:03:28 +08: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 17:19:50 +08:00
/// </summary>
2019-02-12 15:03:28 +08:00
public double? Angle { get ; private set ; }
2018-04-13 17:19:50 +08:00
2021-10-13 23:41:24 +08:00
/// <summary>
2021-11-02 22:47:20 +08:00
/// Milliseconds elapsed since the end time of the previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 25ms.
2021-10-13 23:41:24 +08:00
/// </summary>
public double MovementTime { get ; private set ; }
/// <summary>
2021-11-02 22:47:20 +08: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 23:41:24 +08:00
/// </summary>
public double TravelTime { get ; private set ; }
2021-09-25 11:02:33 +08:00
/// <summary>
2021-09-27 09:39:57 +08:00
/// Milliseconds elapsed since the start time of the previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 25ms.
2021-09-25 11:02:33 +08:00
/// </summary>
public readonly double StrainTime ;
2018-12-09 19:42:27 +08:00
private readonly OsuHitObject lastLastObject ;
2018-05-15 20:22:57 +08:00
private readonly OsuHitObject lastObject ;
2021-11-21 20:40:15 +08:00
private readonly double clockRate ;
2018-04-13 17:19:50 +08:00
2019-02-19 16:43:12 +08:00
public OsuDifficultyHitObject ( HitObject hitObject , HitObject lastLastObject , HitObject lastObject , double clockRate )
: base ( hitObject , lastObject , clockRate )
2018-04-13 17:19:50 +08:00
{
2019-02-12 15:03:28 +08:00
this . lastLastObject = ( OsuHitObject ) lastLastObject ;
this . lastObject = ( OsuHitObject ) lastObject ;
2021-11-21 20:40:15 +08:00
this . clockRate = clockRate ;
2018-05-15 20:22:57 +08:00
2021-11-02 22:47:20 +08:00
// Capped to 25ms to prevent difficulty calculation breaking from simultaneous objects.
2021-10-22 00:00:57 +08:00
StrainTime = Math . Max ( DeltaTime , min_delta_time ) ;
2021-10-13 23:41:24 +08:00
setDistances ( clockRate ) ;
2018-04-13 17:19:50 +08:00
}
2021-11-30 10:52:58 +08:00
public double Opacity ( double mapTime , bool hidden )
2021-11-21 20:40:15 +08:00
{
2021-11-30 10:52:58 +08:00
double ms = ( BaseObject . StartTime - mapTime ) / clockRate ;
2021-11-30 09:58:49 +08:00
if ( ms < 0 )
return 0.0 ;
2021-11-30 10:54:41 +08:00
2021-11-21 20:40:15 +08:00
double preemptTime = BaseObject . TimePreempt / clockRate ;
2021-11-22 05:32:35 +08:00
double fadeInTime = BaseObject . TimeFadeIn / clockRate ;
2021-11-21 20:53:40 +08:00
2021-11-21 20:40:15 +08:00
if ( hidden )
2021-11-22 05:32:35 +08:00
return Math . Clamp ( Math . Min ( ( 1.0 - ms / preemptTime ) * 2.5 , ( ms / preemptTime - 0.3 ) * ( 1.0 / 0.3 ) ) , 0.0 , 1.0 ) ;
2021-11-21 20:40:15 +08:00
else
2021-11-22 05:32:35 +08:00
return Math . Clamp ( ( preemptTime - ms ) / fadeInTime , 0.0 , 1.0 ) ;
2021-11-21 20:40:15 +08:00
}
2021-10-13 23:41:24 +08:00
private void setDistances ( double clockRate )
2018-04-13 17:19:50 +08:00
{
2021-10-10 02:11:24 +08:00
// We don't need to calculate either angle or distance when one of the last->curr objects is a spinner
2021-10-10 01:28:42 +08:00
if ( BaseObject is Spinner | | lastObject is Spinner )
return ;
2018-04-13 17:19:50 +08:00
// We will scale distances by this factor, so we can assume a uniform CircleSize among beatmaps.
2018-10-08 16:37:33 +08:00
float scalingFactor = normalized_radius / ( float ) BaseObject . Radius ;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
if ( BaseObject . Radius < 30 )
{
2018-12-21 21:52:27 +08:00
float smallCircleBonus = Math . Min ( 30 - ( float ) BaseObject . Radius , 5 ) / 50 ;
2018-04-13 17:19:50 +08:00
scalingFactor * = 1 + smallCircleBonus ;
}
2021-11-03 00:04:07 +08:00
Vector2 lastCursorPosition = getEndCursorPosition ( lastObject ) ;
JumpDistance = ( BaseObject . StackedPosition * scalingFactor - lastCursorPosition * scalingFactor ) . Length ;
2018-12-09 19:42:27 +08:00
if ( lastObject is Slider lastSlider )
2018-12-23 15:26:23 +08:00
{
computeSliderCursorPosition ( lastSlider ) ;
2021-11-07 03:16:58 +08:00
TravelDistance = lastSlider . LazyTravelDistance ;
2021-10-22 00:00:57 +08:00
TravelTime = Math . Max ( lastSlider . LazyTravelTime / clockRate , min_delta_time ) ;
MovementTime = Math . Max ( StrainTime - TravelTime , min_delta_time ) ;
2018-12-09 19:42:27 +08:00
2021-11-03 00:04:07 +08:00
// Jump distance from the slider tail to the next object, as opposed to the lazy position of JumpDistance.
float tailJumpDistance = Vector2 . Subtract ( lastSlider . TailCircle . StackedPosition , BaseObject . StackedPosition ) . Length * scalingFactor ;
2018-04-13 17:19:50 +08:00
2021-11-03 00:04:07 +08:00
// For hitobjects which continue in the direction of the slider, the player will normally follow through the slider,
// such that they're not jumping from the lazy position but rather from very close to (or the end of) the slider.
// In such cases, a leniency is applied by also considering the jump distance from the tail of the slider, and taking the minimum jump distance.
2021-11-04 00:14:45 +08:00
// Additional distance is removed based on position of jump relative to slider follow circle radius.
2021-11-07 22:49:26 +08:00
// JumpDistance is the leniency distance beyond the assumed_slider_radius. tailJumpDistance is maximum_slider_radius since the full distance of radial leniency is still possible.
2021-11-07 03:16:58 +08:00
MovementDistance = Math . Max ( 0 , Math . Min ( JumpDistance - ( maximum_slider_radius - assumed_slider_radius ) , tailJumpDistance - maximum_slider_radius ) ) ;
2021-11-03 00:04:07 +08:00
}
else
{
MovementTime = StrainTime ;
MovementDistance = JumpDistance ;
}
2018-12-08 14:01:26 +08:00
2021-10-10 01:28:42 +08:00
if ( lastLastObject ! = null & & ! ( lastLastObject is Spinner ) )
2018-12-08 14:01:26 +08:00
{
2018-12-09 19:42:27 +08:00
Vector2 lastLastCursorPosition = getEndCursorPosition ( lastLastObject ) ;
2018-12-08 14:01:26 +08:00
2018-12-09 19:42:27 +08:00
Vector2 v1 = lastLastCursorPosition - lastObject . StackedPosition ;
Vector2 v2 = BaseObject . StackedPosition - lastCursorPosition ;
2018-12-08 14:01:26 +08:00
float dot = Vector2 . Dot ( v1 , v2 ) ;
float det = v1 . X * v2 . Y - v1 . Y * v2 . X ;
2018-12-09 19:31:04 +08:00
Angle = Math . Abs ( Math . Atan2 ( det , dot ) ) ;
2018-12-08 14:01:26 +08:00
}
2018-04-13 17:19:50 +08:00
}
private void computeSliderCursorPosition ( Slider slider )
{
if ( slider . LazyEndPosition ! = null )
return ;
2019-02-28 12:31:40 +08:00
2021-11-07 03:42:54 +08:00
slider . LazyTravelTime = slider . NestedHitObjects [ ^ 1 ] . StartTime - slider . StartTime ;
2018-04-13 17:19:50 +08:00
2021-11-07 03:16:58 +08:00
double endTimeMin = slider . LazyTravelTime / slider . SpanDuration ;
2021-11-07 03:42:54 +08:00
if ( endTimeMin % 2 > = 1 )
endTimeMin = 1 - endTimeMin % 1 ;
else
endTimeMin % = 1 ;
2021-11-07 03:16:58 +08:00
slider . LazyEndPosition = slider . StackedPosition + slider . Path . PositionAt ( endTimeMin ) ; // temporary lazy end position until a real result can be derived.
var currCursorPosition = slider . StackedPosition ;
double scalingFactor = normalized_radius / slider . Radius ; // lazySliderDistance is coded to be sensitive to scaling, this makes the maths easier with the thresholds being used.
for ( int i = 1 ; i < slider . NestedHitObjects . Count ; i + + )
{
var currMovementObj = ( OsuHitObject ) slider . NestedHitObjects [ i ] ;
Vector2 currMovement = Vector2 . Subtract ( currMovementObj . StackedPosition , currCursorPosition ) ;
double currMovementLength = scalingFactor * currMovement . Length ;
2021-11-07 22:26:13 +08:00
// Amount of movement required so that the cursor position needs to be updated.
double requiredMovement = assumed_slider_radius ;
2018-10-08 17:37:30 +08:00
2021-11-07 03:16:58 +08:00
if ( i = = slider . NestedHitObjects . Count - 1 )
{
// The end of a slider has special aim rules due to the relaxed time constraint on position.
// There is both a lazy end position as well as the actual end slider position. We assume the player takes the simpler movement.
// For sliders that are circular, the lazy end position may actually be farther away than the sliders true end.
// This code is designed to prevent buffing situations where lazy end is actually a less efficient movement.
Vector2 lazyMovement = Vector2 . Subtract ( ( Vector2 ) slider . LazyEndPosition , currCursorPosition ) ;
if ( lazyMovement . Length < currMovement . Length )
currMovement = lazyMovement ;
2018-04-13 17:19:50 +08:00
2021-11-07 03:16:58 +08:00
currMovementLength = scalingFactor * currMovement . Length ;
}
2021-11-07 22:21:18 +08:00
else if ( currMovementObj is SliderRepeat )
2021-11-07 22:26:13 +08:00
{
// For a slider repeat, assume a tighter movement threshold to better assess repeat sliders.
requiredMovement = normalized_radius ;
}
2021-11-07 22:21:18 +08:00
2021-11-07 22:26:13 +08:00
if ( currMovementLength > requiredMovement )
2018-04-13 17:19:50 +08:00
{
2021-11-07 22:21:18 +08:00
// this finds the positional delta from the required radius and the current position, and updates the currCursorPosition accordingly, as well as rewarding distance.
2021-11-07 22:26:13 +08:00
currCursorPosition = Vector2 . Add ( currCursorPosition , Vector2 . Multiply ( currMovement , ( float ) ( ( currMovementLength - requiredMovement ) / currMovementLength ) ) ) ;
currMovementLength * = ( currMovementLength - requiredMovement ) / currMovementLength ;
2021-11-07 03:16:58 +08:00
slider . LazyTravelDistance + = ( float ) currMovementLength ;
2018-04-13 17:19:50 +08:00
}
2021-11-07 22:21:18 +08:00
if ( i = = slider . NestedHitObjects . Count - 1 )
slider . LazyEndPosition = currCursorPosition ;
2021-11-07 03:16:58 +08:00
}
2018-04-13 17:19:50 +08:00
2021-11-07 03:16:58 +08:00
slider . LazyTravelDistance * = ( float ) Math . Pow ( 1 + slider . RepeatCount / 2.5 , 1.0 / 2.5 ) ; // Bonus for repeat sliders until a better per nested object strain system can be achieved.
2018-04-13 17:19:50 +08:00
}
2018-12-09 19:42:27 +08:00
private Vector2 getEndCursorPosition ( OsuHitObject hitObject )
{
Vector2 pos = hitObject . StackedPosition ;
2019-02-28 13:35:00 +08:00
if ( hitObject is Slider slider )
2018-12-09 19:42:27 +08:00
{
computeSliderCursorPosition ( slider ) ;
pos = slider . LazyEndPosition ? ? pos ;
}
return pos ;
}
2018-04-13 17:19:50 +08:00
}
}