2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System ;
using System.Linq ;
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
{
/// <summary>
/// A wrapper around <see cref="OsuHitObject"/> extending it with additional data required for difficulty calculation.
/// </summary>
public class OsuDifficultyHitObject
{
2018-05-15 20:44:45 +08:00
private const int normalized_radius = 52 ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// The <see cref="OsuHitObject"/> this <see cref="OsuDifficultyHitObject"/> refers to.
/// </summary>
public OsuHitObject BaseObject { get ; }
/// <summary>
2018-10-09 11:03:47 +08:00
/// Normalized 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>
2018-10-08 16:37:33 +08:00
public double JumpDistance { get ; private set ; }
/// <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>
/// Milliseconds elapsed since the StartTime of the previous <see cref="OsuDifficultyHitObject"/>.
/// </summary>
public double DeltaTime { get ; private set ; }
2018-10-10 17:53:54 +08:00
/// <summary>
/// Milliseconds elapsed since the start time of the previous <see cref="OsuDifficultyHitObject"/>, with a minimum of 50ms.
/// </summary>
public double StrainTime { get ; private set ; }
2018-12-08 14:01:26 +08:00
public double? Angle { get ; private set ; }
2018-05-15 20:22:57 +08:00
private readonly OsuHitObject lastObject ;
2018-12-08 14:01:26 +08:00
private readonly OsuHitObject nextObject ;
2018-04-13 17:19:50 +08:00
private readonly double timeRate ;
/// <summary>
/// Initializes the object calculating extra data required for difficulty calculation.
/// </summary>
2018-12-08 14:01:26 +08:00
public OsuDifficultyHitObject ( OsuHitObject lastObject , OsuHitObject currentObject , OsuHitObject nextObject , double timeRate )
2018-04-13 17:19:50 +08:00
{
2018-05-15 20:22:57 +08:00
this . lastObject = lastObject ;
2018-12-08 14:01:26 +08:00
this . nextObject = nextObject ;
2018-04-13 17:19:50 +08:00
this . timeRate = timeRate ;
2018-05-15 20:22:57 +08:00
BaseObject = currentObject ;
2018-04-13 17:19:50 +08:00
setDistances ( ) ;
setTimingValues ( ) ;
// Calculate angle here
}
private void setDistances ( )
{
// 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 ;
2018-04-13 17:19:50 +08:00
if ( BaseObject . Radius < 30 )
{
2018-10-08 16:37:33 +08:00
float smallCircleBonus = Math . Min ( 30 - ( float ) BaseObject . Radius , 5 ) / 50 ;
2018-04-13 17:19:50 +08:00
scalingFactor * = 1 + smallCircleBonus ;
}
2018-10-10 17:08:46 +08:00
Vector2 lastCursorPosition = lastObject . StackedPosition ;
2018-04-13 17:19:50 +08:00
2018-05-15 20:22:57 +08:00
var lastSlider = lastObject as Slider ;
2018-04-13 17:19:50 +08:00
if ( lastSlider ! = null )
{
computeSliderCursorPosition ( lastSlider ) ;
lastCursorPosition = lastSlider . LazyEndPosition ? ? lastCursorPosition ;
2018-10-18 10:29:50 +08:00
TravelDistance = lastSlider . LazyTravelDistance * scalingFactor ;
2018-04-13 17:19:50 +08:00
}
2018-10-10 10:49:54 +08:00
// Don't need to jump to reach spinners
if ( ! ( BaseObject is Spinner ) )
2018-10-10 17:08:46 +08:00
JumpDistance = ( BaseObject . StackedPosition * scalingFactor - lastCursorPosition * scalingFactor ) . Length ;
2018-12-08 14:01:26 +08:00
if ( nextObject ! = null )
{
var endCursorPosition = BaseObject . StackedPosition ;
var currentSlider = BaseObject as Slider ;
if ( currentSlider ! = null )
{
computeSliderCursorPosition ( currentSlider ) ;
endCursorPosition = currentSlider . LazyEndPosition ? ? endCursorPosition ;
}
Vector2 v1 = lastCursorPosition - BaseObject . StackedPosition ;
Vector2 v2 = nextObject . StackedPosition - endCursorPosition ;
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 setTimingValues ( )
{
2018-10-10 17:53:54 +08:00
DeltaTime = ( BaseObject . StartTime - lastObject . StartTime ) / timeRate ;
// Every strain interval is hard capped at the equivalent of 375 BPM streaming speed as a safety measure
StrainTime = Math . Max ( 50 , DeltaTime ) ;
2018-04-13 17:19:50 +08:00
}
private void computeSliderCursorPosition ( Slider slider )
{
if ( slider . LazyEndPosition ! = null )
return ;
2018-10-11 12:53:29 +08:00
slider . LazyEndPosition = slider . StackedPosition ;
2018-04-13 17:19:50 +08:00
float approxFollowCircleRadius = ( float ) ( slider . Radius * 3 ) ;
var computeVertex = new Action < double > ( t = >
{
2018-10-08 17:37:30 +08:00
double progress = ( ( int ) t - ( int ) slider . StartTime ) / ( float ) ( int ) slider . SpanDuration ;
if ( progress % 2 > 1 )
progress = 1 - progress % 1 ;
else
progress = progress % 1 ;
2018-04-13 17:19:50 +08:00
// ReSharper disable once PossibleInvalidOperationException (bugged in current r# version)
2018-11-01 02:52:24 +08:00
var diff = slider . StackedPosition + slider . Path . PositionAt ( progress ) - slider . LazyEndPosition . Value ;
2018-04-13 17:19:50 +08:00
float dist = diff . Length ;
if ( dist > approxFollowCircleRadius )
{
// The cursor would be outside the follow circle, we need to move it
diff . Normalize ( ) ; // Obtain direction of diff
dist - = approxFollowCircleRadius ;
slider . LazyEndPosition + = diff * dist ;
slider . LazyTravelDistance + = dist ;
}
} ) ;
2018-05-15 20:25:33 +08:00
// Skip the head circle
var scoringTimes = slider . NestedHitObjects . Skip ( 1 ) . Select ( t = > t . StartTime ) ;
2018-04-13 17:19:50 +08:00
foreach ( var time in scoringTimes )
computeVertex ( time ) ;
computeVertex ( slider . EndTime ) ;
}
}
}