1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 03:43:22 +08:00
osu-lazer/osu.Game/Rulesets/Objects/Legacy/LegacyRulesetExtensions.cs
Bartłomiej Dach 7c9adc7ad3
Fix incorrect score conversion on selected beatmaps due to incorrect difficultyPeppyStars rounding
Fixes issue that occurs on *about* 246 beatmaps and was first described
by me on discord:

    https://discord.com/channels/188630481301012481/188630652340404224/1154367700378865715

and then rediscovered again during work on
https://github.com/ppy/osu/pull/26405:

    https://gist.github.com/bdach/414d5289f65b0399fa8f9732245a4f7c#venenog-on-ultmate-end-by-blacky-overdose-631

It so happens that in stable, due to .NET Framework internals, float
math would be performed using x87 registers and opcodes.
.NET (Core) however uses SSE instructions on 32- and 64-bit words.
x87 registers are _80 bits_ wide. Which is notably wider than _both_
float and double. Therefore, on a significant number of beatmaps,
the rounding would not produce correct values due to insufficient
precision.

See following gist for corroboration of the above:

    https://gist.github.com/bdach/dcde58d5a3607b0408faa3aa2b67bf10

Thus, to crudely - but, seemingly accurately, after checking across
all ranked maps - emulate this, use `decimal`, which is slow, but has
bigger precision than `double`. The single known exception beatmap
in whose case this results in an incorrect result is

    https://osu.ppy.sh/beatmapsets/1156087#osu/2625853

which is considered an "acceptable casualty" of sorts.

Doing this requires some fooling of the compiler / runtime (see second
inline comment in new method). To corroborate that this is required,
you can try the following code snippet:

    Console.WriteLine(string.Join(' ', BitConverter.GetBytes(1.3f).Select(x => x.ToString("X2"))));
    Console.WriteLine(string.Join(' ', BitConverter.GetBytes(1.3).Select(x => x.ToString("X2"))));
    Console.WriteLine();

    decimal d1 = (decimal)1.3f;
    decimal d2 = (decimal)1.3;
    decimal d3 = (decimal)(double)1.3f;

    Console.WriteLine(string.Join(' ', decimal.GetBits(d1).SelectMany(BitConverter.GetBytes).Select(x => x.ToString("X2"))));
    Console.WriteLine(string.Join(' ', decimal.GetBits(d2).SelectMany(BitConverter.GetBytes).Select(x => x.ToString("X2"))));
    Console.WriteLine(string.Join(' ', decimal.GetBits(d3).SelectMany(BitConverter.GetBytes).Select(x => x.ToString("X2"))));

which will print

    66 66 A6 3F
    CD CC CC CC CC CC F4 3F

    0D 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00
    0D 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00
    8C 5D 89 FB 3B 76 00 00 00 00 00 00 00 00 0E 00

Note that despite `d1` being converted from a less-precise floating-
-point value than `d2`, it still is represented 100% accurately as
a decimal number.

After applying this change, recomputation of legacy scoring attributes
for *all* rulesets will be required.
2024-01-10 19:30:18 +01:00

97 lines
5.1 KiB
C#

// 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.
using System;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects.Types;
namespace osu.Game.Rulesets.Objects.Legacy
{
public static class LegacyRulesetExtensions
{
/// <summary>
/// Introduces floating-point errors to post-multiplied beat length for legacy rulesets that depend on it.
/// You should definitely not use this unless you know exactly what you're doing.
/// </summary>
public static double GetPrecisionAdjustedBeatLength(IHasSliderVelocity hasSliderVelocity, TimingControlPoint timingControlPoint, string rulesetShortName)
{
double sliderVelocityAsBeatLength = -100 / hasSliderVelocity.SliderVelocityMultiplier;
// Note: In stable, the division occurs on floats, but with compiler optimisations turned on actually seems to occur on doubles via some .NET black magic (possibly inlining?).
double bpmMultiplier;
switch (rulesetShortName)
{
case "taiko":
case "mania":
bpmMultiplier = sliderVelocityAsBeatLength < 0 ? Math.Clamp((float)-sliderVelocityAsBeatLength, 10, 10000) / 100.0 : 1;
break;
case "osu":
case "fruits":
bpmMultiplier = sliderVelocityAsBeatLength < 0 ? Math.Clamp((float)-sliderVelocityAsBeatLength, 10, 1000) / 100.0 : 1;
break;
default:
throw new ArgumentException("Must be a legacy ruleset", nameof(rulesetShortName));
}
return timingControlPoint.BeatLength * bpmMultiplier;
}
/// <summary>
/// Calculates scale from a CS value, with an optional fudge that was historically applied to the osu! ruleset.
/// </summary>
public static float CalculateScaleFromCircleSize(float circleSize, bool applyFudge = false)
{
// The following comment is copied verbatim from osu-stable:
//
// Builds of osu! up to 2013-05-04 had the gamefield being rounded down, which caused incorrect radius calculations
// in widescreen cases. This ratio adjusts to allow for old replays to work post-fix, which in turn increases the lenience
// for all plays, but by an amount so small it should only be effective in replays.
//
// To match expectations of gameplay we need to apply this multiplier to circle scale. It's weird but is what it is.
// It works out to under 1 game pixel and is generally not meaningful to gameplay, but is to replay playback accuracy.
const float broken_gamefield_rounding_allowance = 1.00041f;
return (float)(1.0f - 0.7f * IBeatmapDifficultyInfo.DifficultyRange(circleSize)) / 2 * (applyFudge ? broken_gamefield_rounding_allowance : 1);
}
public static int CalculateDifficultyPeppyStars(BeatmapDifficulty difficulty, int objectCount, int drainLength)
{
/*
* WARNING: DO NOT TOUCH IF YOU DO NOT KNOW WHAT YOU ARE DOING
*
* It so happens that in stable, due to .NET Framework internals, float math would be performed
* using x87 registers and opcodes.
* .NET (Core) however uses SSE instructions on 32- and 64-bit words.
* x87 registers are _80 bits_ wide. Which is notably wider than _both_ float and double.
* Therefore, on a significant number of beatmaps, the rounding would not produce correct values.
*
* Thus, to crudely - but, seemingly *mostly* accurately, after checking across all ranked maps - emulate this,
* use `decimal`, which is slow, but has bigger precision than `double`.
* At the time of writing, there is _one_ ranked exception to this - namely https://osu.ppy.sh/beatmapsets/1156087#osu/2625853 -
* but it is considered an "acceptable casualty", since in that case scores aren't inflated by _that_ much compared to others.
*/
decimal objectToDrainRatio = drainLength != 0
? Math.Clamp((decimal)objectCount / drainLength * 8, 0, 16)
: 16;
/*
* Notably, THE `double` CASTS BELOW ARE IMPORTANT AND MUST REMAIN.
* Their goal is to trick the compiler / runtime into NOT promoting from single-precision float, as doing so would prompt it
* to attempt to "silently" fix the single-precision values when converting to decimal,
* which is NOT what the x87 FPU does.
*/
decimal drainRate = (decimal)(double)difficulty.DrainRate;
decimal overallDifficulty = (decimal)(double)difficulty.OverallDifficulty;
decimal circleSize = (decimal)(double)difficulty.CircleSize;
return (int)Math.Round((drainRate + overallDifficulty + circleSize + objectToDrainRatio) / 38 * 5);
}
}
}