2019-01-24 16:43:03 +08: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 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2017-11-17 11:35:23 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2018-05-15 16:38:04 +08:00
using osu.Game.Rulesets.Difficulty ;
2017-11-17 11:35:23 +08:00
using osu.Game.Rulesets.Osu.Mods ;
using osu.Game.Rulesets.Scoring ;
2018-11-28 15:12:57 +08:00
using osu.Game.Scoring ;
2018-04-13 17:19:50 +08:00
2018-05-17 15:58:22 +08:00
namespace osu.Game.Rulesets.Osu.Difficulty
2017-11-17 11:35:23 +08:00
{
2018-04-19 21:04:12 +08:00
public class OsuPerformanceCalculator : PerformanceCalculator
2017-11-17 11:35:23 +08:00
{
private double accuracy ;
private int scoreMaxCombo ;
2018-05-16 11:46:31 +08:00
private int countGreat ;
2020-09-29 16:16:55 +08:00
private int countOk ;
2018-05-16 11:46:31 +08:00
private int countMeh ;
2017-11-17 11:35:23 +08:00
private int countMiss ;
2018-04-13 17:19:50 +08:00
2022-01-05 19:36:07 +08:00
private double effectiveMissCount ;
2021-10-22 02:37:06 +08:00
2022-03-15 11:37:39 +08:00
public OsuPerformanceCalculator ( )
: base ( new OsuRuleset ( ) )
2017-11-17 11:35:23 +08:00
{
}
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
protected override PerformanceAttributes CreatePerformanceAttributes ( ScoreInfo score , DifficultyAttributes attributes )
2017-11-17 11:35:23 +08:00
{
2022-03-14 13:25:26 +08:00
var osuAttributes = ( OsuDifficultyAttributes ) attributes ;
accuracy = score . Accuracy ;
scoreMaxCombo = score . MaxCombo ;
countGreat = score . Statistics . GetValueOrDefault ( HitResult . Great ) ;
countOk = score . Statistics . GetValueOrDefault ( HitResult . Ok ) ;
countMeh = score . Statistics . GetValueOrDefault ( HitResult . Meh ) ;
countMiss = score . Statistics . GetValueOrDefault ( HitResult . Miss ) ;
effectiveMissCount = calculateEffectiveMissCount ( osuAttributes ) ;
2018-04-13 17:19:50 +08:00
2021-08-12 07:54:25 +08:00
double multiplier = 1.12 ; // This is being adjusted to keep the final pp value scaled around what it used to be when changing things.
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModNoFail ) )
2021-10-22 02:37:06 +08:00
multiplier * = Math . Max ( 0.90 , 1.0 - 0.02 * effectiveMissCount ) ;
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModSpunOut ) & & totalHits > 0 )
multiplier * = 1.0 - Math . Pow ( ( double ) osuAttributes . SpinnerCount / totalHits , 0.85 ) ;
2021-10-01 22:29:20 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( h = > h is OsuModRelax ) )
2021-10-06 19:25:19 +08:00
{
2021-11-07 21:50:00 +08:00
// As we're adding Oks and Mehs to an approximated number of combo breaks the result can be higher than total hits in specific scenarios (which breaks some calculations) so we need to clamp it.
2021-11-07 12:54:17 +08:00
effectiveMissCount = Math . Min ( effectiveMissCount + countOk + countMeh , totalHits ) ;
2021-11-07 21:50:00 +08:00
2021-10-06 19:25:19 +08:00
multiplier * = 0.6 ;
}
2022-03-14 13:25:26 +08:00
double aimValue = computeAimValue ( score , osuAttributes ) ;
double speedValue = computeSpeedValue ( score , osuAttributes ) ;
double accuracyValue = computeAccuracyValue ( score , osuAttributes ) ;
double flashlightValue = computeFlashlightValue ( score , osuAttributes ) ;
2017-11-17 11:35:23 +08:00
double totalValue =
Math . Pow (
2019-11-26 18:34:23 +08:00
Math . Pow ( aimValue , 1.1 ) +
Math . Pow ( speedValue , 1.1 ) +
2021-08-09 06:31:28 +08:00
Math . Pow ( accuracyValue , 1.1 ) +
2021-08-08 21:56:03 +08:00
Math . Pow ( flashlightValue , 1.1 ) , 1.0 / 1.1
2017-11-17 11:35:23 +08:00
) * multiplier ;
2018-04-13 17:19:50 +08:00
2021-12-21 18:08:31 +08:00
return new OsuPerformanceAttributes
2017-11-17 11:35:23 +08:00
{
2021-12-21 18:08:31 +08:00
Aim = aimValue ,
Speed = speedValue ,
Accuracy = accuracyValue ,
Flashlight = flashlightValue ,
EffectiveMissCount = effectiveMissCount ,
Total = totalValue
} ;
2017-11-17 11:35:23 +08:00
}
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
private double computeAimValue ( ScoreInfo score , OsuDifficultyAttributes attributes )
2017-11-17 11:35:23 +08:00
{
2022-03-14 13:25:26 +08:00
double rawAim = attributes . AimDifficulty ;
2019-01-31 10:51:40 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModTouchDevice ) )
2019-01-31 10:51:40 +08:00
rawAim = Math . Pow ( rawAim , 0.8 ) ;
2019-11-26 18:34:23 +08:00
double aimValue = Math . Pow ( 5.0 * Math . Max ( 1.0 , rawAim / 0.0675 ) - 4.0 , 3.0 ) / 100000.0 ;
2018-04-13 17:19:50 +08:00
2019-11-26 18:34:23 +08:00
double lengthBonus = 0.95 + 0.4 * Math . Min ( 1.0 , totalHits / 2000.0 ) +
( totalHits > 2000 ? Math . Log10 ( totalHits / 2000.0 ) * 0.5 : 0.0 ) ;
2017-11-17 15:54:50 +08:00
aimValue * = lengthBonus ;
2018-04-13 17:19:50 +08:00
2020-12-11 08:51:54 +08:00
// Penalize misses by assessing # of misses relative to the total # of objects. Default a 3% reduction for any # of misses.
2021-10-22 02:37:06 +08:00
if ( effectiveMissCount > 0 )
2022-01-05 20:07:02 +08:00
aimValue * = 0.97 * Math . Pow ( 1 - Math . Pow ( effectiveMissCount / totalHits , 0.775 ) , effectiveMissCount ) ;
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
aimValue * = getComboScalingFactor ( attributes ) ;
2018-04-13 17:19:50 +08:00
2020-12-07 08:06:36 +08:00
double approachRateFactor = 0.0 ;
2022-03-14 13:25:26 +08:00
if ( attributes . ApproachRate > 10.33 )
approachRateFactor = 0.3 * ( attributes . ApproachRate - 10.33 ) ;
else if ( attributes . ApproachRate < 8.0 )
approachRateFactor = 0.1 * ( 8.0 - attributes . ApproachRate ) ;
2018-04-13 17:19:50 +08:00
2021-11-04 00:39:05 +08:00
aimValue * = 1.0 + approachRateFactor * lengthBonus ; // Buff for longer maps with high AR.
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModBlinds ) )
aimValue * = 1.3 + ( totalHits * ( 0.0016 / ( 1 + 2 * effectiveMissCount ) ) * Math . Pow ( accuracy , 16 ) ) * ( 1 - 0.003 * attributes . DrainRate * attributes . DrainRate ) ;
else if ( score . Mods . Any ( h = > h is OsuModHidden ) )
2021-10-09 17:08:57 +08:00
{
// We want to give more reward for lower AR when it comes to aim and HD. This nerfs high AR and buffs lower AR.
2022-03-14 13:25:26 +08:00
aimValue * = 1.0 + 0.04 * ( 12.0 - attributes . ApproachRate ) ;
2021-10-09 17:08:57 +08:00
}
2018-04-13 17:19:50 +08:00
2021-11-12 18:41:01 +08:00
// We assume 15% of sliders in a map are difficult since there's no way to tell from the performance calculator.
2022-03-14 13:25:26 +08:00
double estimateDifficultSliders = attributes . SliderCount * 0.15 ;
2021-06-13 21:18:35 +08:00
2022-03-14 13:25:26 +08:00
if ( attributes . SliderCount > 0 )
2021-11-12 18:41:01 +08:00
{
2022-03-14 13:25:26 +08:00
double estimateSliderEndsDropped = Math . Clamp ( Math . Min ( countOk + countMeh + countMiss , attributes . MaxCombo - scoreMaxCombo ) , 0 , estimateDifficultSliders ) ;
double sliderNerfFactor = ( 1 - attributes . SliderFactor ) * Math . Pow ( 1 - estimateSliderEndsDropped / estimateDifficultSliders , 3 ) + attributes . SliderFactor ;
2021-11-12 18:41:01 +08:00
aimValue * = sliderNerfFactor ;
}
2021-10-22 01:18:24 +08:00
aimValue * = accuracy ;
2021-12-21 19:03:24 +08:00
// It is important to consider accuracy difficulty when scaling with accuracy.
2022-03-14 13:25:26 +08:00
aimValue * = 0.98 + Math . Pow ( attributes . OverallDifficulty , 2 ) / 2500 ;
2018-04-13 17:19:50 +08:00
2017-11-17 11:35:23 +08:00
return aimValue ;
}
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
private double computeSpeedValue ( ScoreInfo score , OsuDifficultyAttributes attributes )
2017-11-17 11:35:23 +08:00
{
2022-03-14 13:25:26 +08:00
double speedValue = Math . Pow ( 5.0 * Math . Max ( 1.0 , attributes . SpeedDifficulty / 0.0675 ) - 4.0 , 3.0 ) / 100000.0 ;
2018-04-13 17:19:50 +08:00
2020-12-07 08:06:36 +08:00
double lengthBonus = 0.95 + 0.4 * Math . Min ( 1.0 , totalHits / 2000.0 ) +
( totalHits > 2000 ? Math . Log10 ( totalHits / 2000.0 ) * 0.5 : 0.0 ) ;
speedValue * = lengthBonus ;
2018-04-13 17:19:50 +08:00
2020-12-11 08:51:54 +08:00
// Penalize misses by assessing # of misses relative to the total # of objects. Default a 3% reduction for any # of misses.
2021-10-22 02:37:06 +08:00
if ( effectiveMissCount > 0 )
2022-01-05 20:07:02 +08:00
speedValue * = 0.97 * Math . Pow ( 1 - Math . Pow ( effectiveMissCount / totalHits , 0.775 ) , Math . Pow ( effectiveMissCount , . 875 ) ) ;
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
speedValue * = getComboScalingFactor ( attributes ) ;
2019-01-09 16:47:39 +08:00
2020-12-07 08:06:36 +08:00
double approachRateFactor = 0.0 ;
2022-03-14 13:25:26 +08:00
if ( attributes . ApproachRate > 10.33 )
approachRateFactor = 0.3 * ( attributes . ApproachRate - 10.33 ) ;
2019-01-09 16:42:48 +08:00
2021-11-04 00:39:05 +08:00
speedValue * = 1.0 + approachRateFactor * lengthBonus ; // Buff for longer maps with high AR.
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModBlinds ) )
2021-10-09 17:08:57 +08:00
{
// Increasing the speed value by object count for Blinds isn't ideal, so the minimum buff is given.
2021-09-26 00:34:24 +08:00
speedValue * = 1.12 ;
2021-10-09 17:08:57 +08:00
}
2022-03-14 13:25:26 +08:00
else if ( score . Mods . Any ( m = > m is OsuModHidden ) )
2021-10-09 17:08:57 +08:00
{
// We want to give more reward for lower AR when it comes to aim and HD. This nerfs high AR and buffs lower AR.
2022-03-14 13:25:26 +08:00
speedValue * = 1.0 + 0.04 * ( 12.0 - attributes . ApproachRate ) ;
2021-10-09 17:08:57 +08:00
}
2018-05-12 14:46:25 +08:00
2021-10-11 09:12:57 +08:00
// Calculate accuracy assuming the worst case scenario
2022-06-20 10:15:35 +08:00
double relevantTotalDiff = totalHits - attributes . SpeedNoteCount ;
2021-10-11 09:12:57 +08:00
double relevantCountGreat = Math . Max ( 0 , countGreat - relevantTotalDiff ) ;
double relevantCountOk = Math . Max ( 0 , countOk - Math . Max ( 0 , relevantTotalDiff - countGreat ) ) ;
double relevantCountMeh = Math . Max ( 0 , countMeh - Math . Max ( 0 , relevantTotalDiff - countGreat - countOk ) ) ;
2022-06-29 15:29:14 +08:00
double relevantAccuracy = attributes . SpeedNoteCount = = 0 ? 0 : ( relevantCountGreat * 6.0 + relevantCountOk * 2.0 + relevantCountMeh ) / ( attributes . SpeedNoteCount * 6.0 ) ;
2021-10-11 09:12:57 +08:00
2021-08-12 07:54:25 +08:00
// Scale the speed value with accuracy and OD.
2022-06-20 10:15:35 +08:00
speedValue * = ( 0.95 + Math . Pow ( attributes . OverallDifficulty , 2 ) / 750 ) * Math . Pow ( ( accuracy + relevantAccuracy ) / 2.0 , ( 14.5 - Math . Max ( attributes . OverallDifficulty , 8 ) ) / 2 ) ;
2021-10-09 17:08:57 +08:00
2020-12-10 10:07:52 +08:00
// Scale the speed value with # of 50s to punish doubletapping.
2020-12-15 02:41:24 +08:00
speedValue * = Math . Pow ( 0.98 , countMeh < totalHits / 500.0 ? 0 : countMeh - totalHits / 500.0 ) ;
2018-04-13 17:19:50 +08:00
2017-11-17 11:35:23 +08:00
return speedValue ;
}
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
private double computeAccuracyValue ( ScoreInfo score , OsuDifficultyAttributes attributes )
2017-11-17 11:35:23 +08:00
{
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( h = > h is OsuModRelax ) )
2021-10-03 18:27:17 +08:00
return 0.0 ;
2021-08-12 07:54:25 +08:00
// This percentage only considers HitCircles of any value - in this part of the calculation we focus on hitting the timing hit window.
2017-11-17 15:54:50 +08:00
double betterAccuracyPercentage ;
2022-03-14 13:25:26 +08:00
int amountHitObjectsWithAccuracy = attributes . HitCircleCount ;
2018-04-13 17:19:50 +08:00
2017-11-17 11:35:23 +08:00
if ( amountHitObjectsWithAccuracy > 0 )
2020-09-29 16:16:55 +08:00
betterAccuracyPercentage = ( ( countGreat - ( totalHits - amountHitObjectsWithAccuracy ) ) * 6 + countOk * 2 + countMeh ) / ( double ) ( amountHitObjectsWithAccuracy * 6 ) ;
2017-11-17 11:35:23 +08:00
else
betterAccuracyPercentage = 0 ;
2018-04-13 17:19:50 +08:00
2021-08-12 07:54:25 +08:00
// It is possible to reach a negative accuracy with this formula. Cap it at zero - zero points.
2017-11-17 11:35:23 +08:00
if ( betterAccuracyPercentage < 0 )
betterAccuracyPercentage = 0 ;
2018-04-13 17:19:50 +08:00
2017-11-17 11:35:23 +08:00
// Lots of arbitrary values from testing.
2021-08-12 07:54:25 +08:00
// Considering to use derivation from perfect accuracy in a probabilistic manner - assume normal distribution.
2022-03-14 13:25:26 +08:00
double accuracyValue = Math . Pow ( 1.52163 , attributes . OverallDifficulty ) * Math . Pow ( betterAccuracyPercentage , 24 ) * 2.83 ;
2018-04-13 17:19:50 +08:00
2021-08-12 07:54:25 +08:00
// Bonus for many hitcircles - it's harder to keep good accuracy up for longer.
2019-11-26 18:34:23 +08:00
accuracyValue * = Math . Min ( 1.15 , Math . Pow ( amountHitObjectsWithAccuracy / 1000.0 , 0.3 ) ) ;
2018-04-13 17:19:50 +08:00
2021-09-26 00:34:24 +08:00
// Increasing the accuracy value by object count for Blinds isn't ideal, so the minimum buff is given.
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModBlinds ) )
2021-09-26 00:34:24 +08:00
accuracyValue * = 1.14 ;
2022-03-14 13:25:26 +08:00
else if ( score . Mods . Any ( m = > m is OsuModHidden ) )
2019-11-26 18:34:23 +08:00
accuracyValue * = 1.08 ;
2021-10-09 17:08:57 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModFlashlight ) )
2019-11-26 18:34:23 +08:00
accuracyValue * = 1.02 ;
2018-04-13 17:19:50 +08:00
2017-11-17 11:35:23 +08:00
return accuracyValue ;
}
2018-04-13 17:19:50 +08:00
2022-03-14 13:25:26 +08:00
private double computeFlashlightValue ( ScoreInfo score , OsuDifficultyAttributes attributes )
2021-08-08 21:56:03 +08:00
{
2022-03-14 13:25:26 +08:00
if ( ! score . Mods . Any ( h = > h is OsuModFlashlight ) )
2021-09-13 15:39:05 +08:00
return 0.0 ;
2021-08-08 21:56:03 +08:00
2022-03-14 13:25:26 +08:00
double rawFlashlight = attributes . FlashlightDifficulty ;
2021-08-11 04:14:38 +08:00
2022-03-14 13:25:26 +08:00
if ( score . Mods . Any ( m = > m is OsuModTouchDevice ) )
2021-09-13 15:39:05 +08:00
rawFlashlight = Math . Pow ( rawFlashlight , 0.8 ) ;
2021-08-11 04:14:38 +08:00
2021-09-13 15:39:05 +08:00
double flashlightValue = Math . Pow ( rawFlashlight , 2.0 ) * 25.0 ;
2021-08-09 06:31:28 +08:00
2021-09-13 15:39:05 +08:00
// Penalize misses by assessing # of misses relative to the total # of objects. Default a 3% reduction for any # of misses.
2021-10-22 02:37:06 +08:00
if ( effectiveMissCount > 0 )
2022-01-05 20:07:02 +08:00
flashlightValue * = 0.97 * Math . Pow ( 1 - Math . Pow ( effectiveMissCount / totalHits , 0.775 ) , Math . Pow ( effectiveMissCount , . 875 ) ) ;
2021-08-08 21:56:03 +08:00
2022-03-14 13:25:26 +08:00
flashlightValue * = getComboScalingFactor ( attributes ) ;
2021-08-08 21:56:03 +08:00
2021-09-13 15:39:05 +08:00
// Account for shorter maps having a higher ratio of 0 combo/100 combo flashlight radius.
flashlightValue * = 0.7 + 0.1 * Math . Min ( 1.0 , totalHits / 200.0 ) +
( totalHits > 200 ? 0.2 * Math . Min ( 1.0 , ( totalHits - 200 ) / 200.0 ) : 0.0 ) ;
2021-08-08 21:56:03 +08:00
2021-09-13 15:39:05 +08:00
// Scale the flashlight value with accuracy _slightly_.
flashlightValue * = 0.5 + accuracy / 2.0 ;
// It is important to also consider accuracy difficulty when doing that.
2022-03-14 13:25:26 +08:00
flashlightValue * = 0.98 + Math . Pow ( attributes . OverallDifficulty , 2 ) / 2500 ;
2021-08-08 21:56:03 +08:00
return flashlightValue ;
}
2022-03-14 13:25:26 +08:00
private double calculateEffectiveMissCount ( OsuDifficultyAttributes attributes )
2021-10-22 02:37:06 +08:00
{
2021-11-07 21:51:17 +08:00
// Guess the number of misses + slider breaks from combo
2021-10-22 02:37:06 +08:00
double comboBasedMissCount = 0.0 ;
2022-03-14 13:25:26 +08:00
if ( attributes . SliderCount > 0 )
2021-10-22 02:37:06 +08:00
{
2022-03-14 13:25:26 +08:00
double fullComboThreshold = attributes . MaxCombo - 0.1 * attributes . SliderCount ;
2021-10-22 02:37:06 +08:00
if ( scoreMaxCombo < fullComboThreshold )
comboBasedMissCount = fullComboThreshold / Math . Max ( 1.0 , scoreMaxCombo ) ;
}
2022-01-05 19:36:07 +08:00
// Clamp miss count since it's derived from combo and can be higher than total hits and that breaks some calculations
2021-10-22 02:37:06 +08:00
comboBasedMissCount = Math . Min ( comboBasedMissCount , totalHits ) ;
2022-01-05 19:36:07 +08:00
return Math . Max ( countMiss , comboBasedMissCount ) ;
2021-10-22 02:37:06 +08:00
}
2022-03-14 13:25:26 +08:00
private double getComboScalingFactor ( OsuDifficultyAttributes attributes ) = > attributes . MaxCombo < = 0 ? 1.0 : Math . Min ( Math . Pow ( scoreMaxCombo , 0.8 ) / Math . Pow ( attributes . MaxCombo , 0.8 ) , 1.0 ) ;
2020-09-29 16:16:55 +08:00
private int totalHits = > countGreat + countOk + countMeh + countMiss ;
private int totalSuccessfulHits = > countGreat + countOk + countMeh ;
2017-11-17 11:35:23 +08:00
}
}