From c6ac60c0b5d5c88cb5d7fa611858507fa7f3c734 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 19 Jun 2022 13:07:10 +0200 Subject: [PATCH 01/15] Enhance target angle calculation --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 48 +++++++++++++++---- .../Utils/OsuHitObjectGenerationUtils.cs | 33 +++++++++++++ 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 4f83154728..475d5a2a67 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -37,23 +37,51 @@ namespace osu.Game.Rulesets.Osu.Mods var positionInfos = OsuHitObjectGenerationUtils.GeneratePositionInfos(osuBeatmap.HitObjects); - float rateOfChangeMultiplier = 0; + float sequenceOffset = 0; + bool flowDirection = false; - foreach (var positionInfo in positionInfos) + for (int i = 0; i < positionInfos.Count; i++) { - // rateOfChangeMultiplier only changes every 5 iterations in a combo - // to prevent shaky-line-shaped streams - if (positionInfo.HitObject.IndexInCurrentCombo % 5 == 0) - rateOfChangeMultiplier = (float)rng.NextDouble() * 2 - 1; + bool invertFlow = false; - if (positionInfo == positionInfos.First()) + if (i == 0 || + (positionInfos[i - 1].HitObject.NewCombo && (i <= 1 || !positionInfos[i - 2].HitObject.NewCombo) && (i <= 2 || !positionInfos[i - 3].HitObject.NewCombo)) || + OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || + (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.25)) { - positionInfo.DistanceFromPrevious = (float)(rng.NextDouble() * OsuPlayfield.BASE_SIZE.Y / 2); - positionInfo.RelativeAngle = (float)(rng.NextDouble() * 2 * Math.PI - Math.PI); + sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.02f); + + if (rng.NextDouble() < 0.6) + invertFlow = true; + } + + if (i == 0) + { + positionInfos[i].DistanceFromPrevious = (float)(rng.NextDouble() * OsuPlayfield.BASE_SIZE.Y / 2); + positionInfos[i].RelativeAngle = (float)(rng.NextDouble() * 2 * Math.PI - Math.PI); } else { - positionInfo.RelativeAngle = rateOfChangeMultiplier * 2 * (float)Math.PI * Math.Min(1f, positionInfo.DistanceFromPrevious / (playfield_diagonal * 0.5f)); + float flowChangeOffset = 0; + float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.03f); + + if (positionInfos[i - 1].HitObject.NewCombo && (i <= 1 || !positionInfos[i - 2].HitObject.NewCombo) && rng.NextDouble() < 0.6) + { + flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.05f); + + if (rng.NextDouble() < 0.8) + invertFlow = true; + } + + if (invertFlow) + flowDirection ^= true; + + positionInfos[i].RelativeAngle = OsuHitObjectGenerationUtils.GetRelativeTargetAngle( + positionInfos[i].DistanceFromPrevious, + (sequenceOffset + oneTimeOffset) * (float)Math.Sqrt(positionInfos[i].DistanceFromPrevious) + + flowChangeOffset * (float)Math.Sqrt(640 - positionInfos[i].DistanceFromPrevious), + flowDirection + ); } } diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index 5e827d4782..9044fe142b 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -8,6 +8,7 @@ using System.Linq; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Game.Rulesets.Osu.UI; using osu.Game.Rulesets.Objects; +using osu.Game.Rulesets.Osu.Beatmaps; using osu.Game.Rulesets.Osu.Objects; using osuTK; @@ -186,5 +187,37 @@ namespace osu.Game.Rulesets.Osu.Utils length * MathF.Sin(angle) ); } + + public static bool IsHitObjectOnBeat(OsuBeatmap beatmap, OsuHitObject hitObject, bool downbeatsOnly = false) + { + var timingPoints = beatmap.ControlPointInfo.TimingPoints; + var currentTimingPoint = timingPoints.Reverse().FirstOrDefault(p => p.Time <= hitObject.StartTime); + + if (currentTimingPoint == null) + return false; + + double timeSinceTimingPoint = hitObject.StartTime - currentTimingPoint.Time; + + double length = downbeatsOnly + ? currentTimingPoint.BeatLength * currentTimingPoint.TimeSignature.Numerator + : currentTimingPoint.BeatLength; + + return (timeSinceTimingPoint + 1) % length < 2; + } + + public static float GetRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) + { + float angle = (float)(3.3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); + float relativeAngle = MathHelper.Pi - angle; + return flowDirection ? -relativeAngle : relativeAngle; + } + + public static float RandomGaussian(Random rng, float mean = 0, float stdDev = 1) + { + double x1 = 1 - rng.NextDouble(); + double x2 = 1 - rng.NextDouble(); + double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(MathHelper.TwoPi * x2); + return mean + stdDev * (float)stdNormal; + } } } From 33c6c6af6b2995f4717009a2cc646775e32d419b Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 19 Jun 2022 13:50:09 +0200 Subject: [PATCH 02/15] Adjust target angle calculation parameters --- osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index 9044fe142b..b9c384f842 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -207,7 +207,7 @@ namespace osu.Game.Rulesets.Osu.Utils public static float GetRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) { - float angle = (float)(3.3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); + float angle = (float)(3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); float relativeAngle = MathHelper.Pi - angle; return flowDirection ? -relativeAngle : relativeAngle; } From 7317b9b90960a2b326ec858e61b668375fa7e0ac Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 19 Jun 2022 14:59:28 +0200 Subject: [PATCH 03/15] Remove unused field --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 475d5a2a67..2560ed921d 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -22,8 +22,6 @@ namespace osu.Game.Rulesets.Osu.Mods public override Type[] IncompatibleMods => base.IncompatibleMods.Append(typeof(OsuModTarget)).ToArray(); - private static readonly float playfield_diagonal = OsuPlayfield.BASE_SIZE.LengthFast; - private Random? rng; public void ApplyToBeatmap(IBeatmap beatmap) From 9090e7502075ed9731f75408b43f5d9afe35646d Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 19 Jun 2022 20:43:17 +0200 Subject: [PATCH 04/15] Add XML documentation --- .../Utils/OsuHitObjectGenerationUtils.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index b9c384f842..f503956aee 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -188,6 +188,11 @@ namespace osu.Game.Rulesets.Osu.Utils ); } + /// The beatmap hitObject is a part of. + /// The that should be checked. + /// If true, this method only returns true if hitObject is on a downbeat. + /// If false, it returns true if hitObject is on any beat. + /// true if hitObject is on a (down-)beat, false otherwise. public static bool IsHitObjectOnBeat(OsuBeatmap beatmap, OsuHitObject hitObject, bool downbeatsOnly = false) { var timingPoints = beatmap.ControlPointInfo.TimingPoints; @@ -205,6 +210,9 @@ namespace osu.Game.Rulesets.Osu.Utils return (timeSinceTimingPoint + 1) % length < 2; } + /// The target distance between the previous and the current . + /// The angle (in rad) by which the target angle should be offset. + /// Whether the relative angle should be positive or negative. public static float GetRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) { float angle = (float)(3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); From 1bb27cd4880f4dd4766d1cace022c37e0f23585b Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 19 Jun 2022 23:03:41 +0200 Subject: [PATCH 05/15] Code optimisation --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 23 +++++++++++++++---- .../Utils/OsuHitObjectGenerationUtils.cs | 18 ++++----------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 2560ed921d..55151ae2c1 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -8,6 +8,7 @@ using osu.Game.Beatmaps; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Osu.Beatmaps; +using osu.Game.Rulesets.Osu.Objects; using osu.Game.Rulesets.Osu.UI; using osu.Game.Rulesets.Osu.Utils; @@ -22,6 +23,8 @@ namespace osu.Game.Rulesets.Osu.Mods public override Type[] IncompatibleMods => base.IncompatibleMods.Append(typeof(OsuModTarget)).ToArray(); + private static readonly float playfield_diagonal = OsuPlayfield.BASE_SIZE.LengthFast; + private Random? rng; public void ApplyToBeatmap(IBeatmap beatmap) @@ -43,7 +46,7 @@ namespace osu.Game.Rulesets.Osu.Mods bool invertFlow = false; if (i == 0 || - (positionInfos[i - 1].HitObject.NewCombo && (i <= 1 || !positionInfos[i - 2].HitObject.NewCombo) && (i <= 2 || !positionInfos[i - 3].HitObject.NewCombo)) || + (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) || OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.25)) { @@ -63,7 +66,7 @@ namespace osu.Game.Rulesets.Osu.Mods float flowChangeOffset = 0; float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.03f); - if (positionInfos[i - 1].HitObject.NewCombo && (i <= 1 || !positionInfos[i - 2].HitObject.NewCombo) && rng.NextDouble() < 0.6) + if (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) { flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.05f); @@ -72,12 +75,12 @@ namespace osu.Game.Rulesets.Osu.Mods } if (invertFlow) - flowDirection ^= true; + flowDirection = !flowDirection; - positionInfos[i].RelativeAngle = OsuHitObjectGenerationUtils.GetRelativeTargetAngle( + positionInfos[i].RelativeAngle = getRelativeTargetAngle( positionInfos[i].DistanceFromPrevious, (sequenceOffset + oneTimeOffset) * (float)Math.Sqrt(positionInfos[i].DistanceFromPrevious) + - flowChangeOffset * (float)Math.Sqrt(640 - positionInfos[i].DistanceFromPrevious), + flowChangeOffset * (float)Math.Sqrt(playfield_diagonal - positionInfos[i].DistanceFromPrevious), flowDirection ); } @@ -85,5 +88,15 @@ namespace osu.Game.Rulesets.Osu.Mods osuBeatmap.HitObjects = OsuHitObjectGenerationUtils.RepositionHitObjects(positionInfos); } + + /// The target distance between the previous and the current . + /// The angle (in rad) by which the target angle should be offset. + /// Whether the relative angle should be positive or negative. + private static float getRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) + { + float angle = (float)(3 / (1 + 200 * Math.Exp(0.016 * (targetDistance - 466))) + 0.45 + offset); + float relativeAngle = (float)Math.PI - angle; + return flowDirection ? -relativeAngle : relativeAngle; + } } } diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index f503956aee..117d6f3a80 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -196,7 +196,7 @@ namespace osu.Game.Rulesets.Osu.Utils public static bool IsHitObjectOnBeat(OsuBeatmap beatmap, OsuHitObject hitObject, bool downbeatsOnly = false) { var timingPoints = beatmap.ControlPointInfo.TimingPoints; - var currentTimingPoint = timingPoints.Reverse().FirstOrDefault(p => p.Time <= hitObject.StartTime); + var currentTimingPoint = timingPoints.LastOrDefault(p => p.Time <= hitObject.StartTime); if (currentTimingPoint == null) return false; @@ -210,21 +210,11 @@ namespace osu.Game.Rulesets.Osu.Utils return (timeSinceTimingPoint + 1) % length < 2; } - /// The target distance between the previous and the current . - /// The angle (in rad) by which the target angle should be offset. - /// Whether the relative angle should be positive or negative. - public static float GetRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) - { - float angle = (float)(3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); - float relativeAngle = MathHelper.Pi - angle; - return flowDirection ? -relativeAngle : relativeAngle; - } - public static float RandomGaussian(Random rng, float mean = 0, float stdDev = 1) { - double x1 = 1 - rng.NextDouble(); - double x2 = 1 - rng.NextDouble(); - double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(MathHelper.TwoPi * x2); + double x1 = rng.NextDouble(); + double x2 = rng.NextDouble(); + double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(2 * Math.PI * x2); return mean + stdDev * (float)stdNormal; } } From 3356742ba2d5b74372c6641a4d857cf6138ad44f Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Mon, 20 Jun 2022 00:05:03 +0200 Subject: [PATCH 06/15] Adjust angle offset caluclations --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 55151ae2c1..ef3eaf2a9d 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -50,7 +50,7 @@ namespace osu.Game.Rulesets.Osu.Mods OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.25)) { - sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.02f); + sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0015f); if (rng.NextDouble() < 0.6) invertFlow = true; @@ -64,11 +64,11 @@ namespace osu.Game.Rulesets.Osu.Mods else { float flowChangeOffset = 0; - float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.03f); + float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0015f); if (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) { - flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.05f); + flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); if (rng.NextDouble() < 0.8) invertFlow = true; @@ -79,8 +79,8 @@ namespace osu.Game.Rulesets.Osu.Mods positionInfos[i].RelativeAngle = getRelativeTargetAngle( positionInfos[i].DistanceFromPrevious, - (sequenceOffset + oneTimeOffset) * (float)Math.Sqrt(positionInfos[i].DistanceFromPrevious) + - flowChangeOffset * (float)Math.Sqrt(playfield_diagonal - positionInfos[i].DistanceFromPrevious), + (sequenceOffset + oneTimeOffset) * positionInfos[i].DistanceFromPrevious + + flowChangeOffset * (playfield_diagonal - positionInfos[i].DistanceFromPrevious), flowDirection ); } From a912bcadf82aa7eb4e885cf81da35e980824dd55 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Mon, 20 Jun 2022 00:19:29 +0200 Subject: [PATCH 07/15] Fix possible exception caused by `log(0)` --- osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index 117d6f3a80..ce7f39cdf8 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -212,8 +212,8 @@ namespace osu.Game.Rulesets.Osu.Utils public static float RandomGaussian(Random rng, float mean = 0, float stdDev = 1) { - double x1 = rng.NextDouble(); - double x2 = rng.NextDouble(); + double x1 = 1 - rng.NextDouble(); + double x2 = 1 - rng.NextDouble(); double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(2 * Math.PI * x2); return mean + stdDev * (float)stdNormal; } From 2f1186d3284b8cad32d7c6ae83a50bc72d00b326 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Wed, 22 Jun 2022 16:49:07 +0200 Subject: [PATCH 08/15] Add comments and XML doc --- osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index ce7f39cdf8..a890dbde43 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -210,10 +210,16 @@ namespace osu.Game.Rulesets.Osu.Utils return (timeSinceTimingPoint + 1) % length < 2; } + /// + /// Generates a random number from a normal distribution using the Box-Muller transform. + /// public static float RandomGaussian(Random rng, float mean = 0, float stdDev = 1) { + // Generate 2 random numbers in the interval (0,1]. + // x1 must not be 0 since log(0) = undefined. double x1 = 1 - rng.NextDouble(); double x2 = 1 - rng.NextDouble(); + double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(2 * Math.PI * x2); return mean + stdDev * (float)stdNormal; } From da7d297d85e4ae5d59747dac3ae8686e1849433f Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Tue, 26 Jul 2022 19:07:25 +0200 Subject: [PATCH 09/15] Adjust parameters --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index ef3eaf2a9d..716fa990c9 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -43,17 +43,13 @@ namespace osu.Game.Rulesets.Osu.Mods for (int i = 0; i < positionInfos.Count; i++) { - bool invertFlow = false; - if (i == 0 || (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) || OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || - (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.25)) + (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.3)) { - sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0015f); - - if (rng.NextDouble() < 0.6) - invertFlow = true; + sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0012f); + flowDirection = !flowDirection; } if (i == 0) @@ -64,18 +60,13 @@ namespace osu.Game.Rulesets.Osu.Mods else { float flowChangeOffset = 0; - float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0015f); + float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); if (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) { flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); - - if (rng.NextDouble() < 0.8) - invertFlow = true; - } - - if (invertFlow) flowDirection = !flowDirection; + } positionInfos[i].RelativeAngle = getRelativeTargetAngle( positionInfos[i].DistanceFromPrevious, @@ -94,7 +85,7 @@ namespace osu.Game.Rulesets.Osu.Mods /// Whether the relative angle should be positive or negative. private static float getRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) { - float angle = (float)(3 / (1 + 200 * Math.Exp(0.016 * (targetDistance - 466))) + 0.45 + offset); + float angle = (float)(2.16 / (1 + 200 * Math.Exp(0.036 * (targetDistance - 320))) + 0.5 + offset); float relativeAngle = (float)Math.PI - angle; return flowDirection ? -relativeAngle : relativeAngle; } From 78fe72476de7ea3b591596480059741182259b45 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sat, 13 Aug 2022 00:01:40 +0200 Subject: [PATCH 10/15] Adjust parameters --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 716fa990c9..3c0642ca15 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -46,9 +46,9 @@ namespace osu.Game.Rulesets.Osu.Mods if (i == 0 || (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) || OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || - (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.3)) + (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.4)) { - sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0012f); + sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.001f); flowDirection = !flowDirection; } From 8c624d3269902ea8f83e874fbfebd204c97b8206 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sat, 13 Aug 2022 00:57:49 +0200 Subject: [PATCH 11/15] Add comments and improve code readability --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 62 +++++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 3c0642ca15..0a89f3eb43 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Collections.Generic; using System.Linq; using osu.Framework.Utils; using osu.Game.Beatmaps; @@ -29,7 +30,7 @@ namespace osu.Game.Rulesets.Osu.Mods public void ApplyToBeatmap(IBeatmap beatmap) { - if (!(beatmap is OsuBeatmap osuBeatmap)) + if (beatmap is not OsuBeatmap osuBeatmap) return; Seed.Value ??= RNG.Next(); @@ -38,17 +39,17 @@ namespace osu.Game.Rulesets.Osu.Mods var positionInfos = OsuHitObjectGenerationUtils.GeneratePositionInfos(osuBeatmap.HitObjects); - float sequenceOffset = 0; + // Offsets the angles of all hit objects in a "section" by the same amount. + float sectionOffset = 0; + + // Whether the angles are positive or negative (clockwise or counter-clockwise flow). bool flowDirection = false; for (int i = 0; i < positionInfos.Count; i++) { - if (i == 0 || - (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) || - OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject, true) || - (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(osuBeatmap, positionInfos[i - 1].HitObject) && rng.NextDouble() < 0.4)) + if (shouldStartNewSection(osuBeatmap, positionInfos, i, 0.6f, 0.4f)) { - sequenceOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.001f); + sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.001f); flowDirection = !flowDirection; } @@ -59,21 +60,25 @@ namespace osu.Game.Rulesets.Osu.Mods } else { + // Offsets only the angle of the current hit object if a flow change occurs. float flowChangeOffset = 0; + + // Offsets only the angle of the current hit object. float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); - if (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng.NextDouble() < 0.6) + if (shouldApplyFlowChange(positionInfos, i, 0.6f)) { flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); flowDirection = !flowDirection; } - positionInfos[i].RelativeAngle = getRelativeTargetAngle( - positionInfos[i].DistanceFromPrevious, - (sequenceOffset + oneTimeOffset) * positionInfos[i].DistanceFromPrevious + - flowChangeOffset * (playfield_diagonal - positionInfos[i].DistanceFromPrevious), - flowDirection - ); + float totalOffset = + // sectionOffset and oneTimeOffset should mainly affect patterns with large spacing. + (sectionOffset + oneTimeOffset) * positionInfos[i].DistanceFromPrevious + + // flowChangeOffset should mainly affect streams. + flowChangeOffset * (playfield_diagonal - positionInfos[i].DistanceFromPrevious); + + positionInfos[i].RelativeAngle = getRelativeTargetAngle(positionInfos[i].DistanceFromPrevious, totalOffset, flowDirection); } } @@ -89,5 +94,34 @@ namespace osu.Game.Rulesets.Osu.Mods float relativeAngle = (float)Math.PI - angle; return flowDirection ? -relativeAngle : relativeAngle; } + + /// + /// A new section should be started...
+ /// ...at the beginning of the .
+ /// ...on every combo start with a probability of (excluding new-combo-spam and 1-2-combos).
+ /// ...on every downbeat.
+ /// ...on every beat with a probability of .
+ ///
+ /// Whether a new section should be started at the current . + private bool shouldStartNewSection( + OsuBeatmap beatmap, + IReadOnlyList positionInfos, + int i, + float newComboProbability, + float beatProbability + ) => + i == 0 || + (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng?.NextDouble() < newComboProbability) || + OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject, true) || + (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject) && rng?.NextDouble() < beatProbability); + + /// + /// A flow change should occur on every combo start with a probability of (excluding new-combo-spam and 1-2-combos). + /// + /// Whether a flow change should be applied at the current . + private bool shouldApplyFlowChange(IReadOnlyList positionInfos, int i, float probability) => + positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && + positionInfos[i - 1].HitObject.NewCombo && + rng?.NextDouble() < probability; } } From 7a41b9f25a35c375670103baad777e15ea231e59 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sat, 13 Aug 2022 03:11:58 +0200 Subject: [PATCH 12/15] Adjust angle and `sectionOffset` calculations --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 0a89f3eb43..2be1ad231a 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -49,7 +49,7 @@ namespace osu.Game.Rulesets.Osu.Mods { if (shouldStartNewSection(osuBeatmap, positionInfos, i, 0.6f, 0.4f)) { - sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.001f); + sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0008f); flowDirection = !flowDirection; } @@ -90,7 +90,7 @@ namespace osu.Game.Rulesets.Osu.Mods /// Whether the relative angle should be positive or negative. private static float getRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) { - float angle = (float)(2.16 / (1 + 200 * Math.Exp(0.036 * (targetDistance - 320))) + 0.5 + offset); + float angle = (float)(2.16 / (1 + 200 * Math.Exp(0.036 * (targetDistance - 310))) + 0.5 + offset); float relativeAngle = (float)Math.PI - angle; return flowDirection ? -relativeAngle : relativeAngle; } From 5106c00a9c5010f751b428a7b0726d95bc5b7620 Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Sun, 14 Aug 2022 19:02:29 +0200 Subject: [PATCH 13/15] Improve code quality --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 54 +++++++++++----------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 2be1ad231a..ce54d87dfa 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -47,7 +47,7 @@ namespace osu.Game.Rulesets.Osu.Mods for (int i = 0; i < positionInfos.Count; i++) { - if (shouldStartNewSection(osuBeatmap, positionInfos, i, 0.6f, 0.4f)) + if (shouldStartNewSection(osuBeatmap, positionInfos, i)) { sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0008f); flowDirection = !flowDirection; @@ -66,7 +66,7 @@ namespace osu.Game.Rulesets.Osu.Mods // Offsets only the angle of the current hit object. float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); - if (shouldApplyFlowChange(positionInfos, i, 0.6f)) + if (shouldApplyFlowChange(positionInfos, i)) { flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); flowDirection = !flowDirection; @@ -95,33 +95,35 @@ namespace osu.Game.Rulesets.Osu.Mods return flowDirection ? -relativeAngle : relativeAngle; } - /// - /// A new section should be started...
- /// ...at the beginning of the .
- /// ...on every combo start with a probability of (excluding new-combo-spam and 1-2-combos).
- /// ...on every downbeat.
- /// ...on every beat with a probability of .
- ///
/// Whether a new section should be started at the current . - private bool shouldStartNewSection( - OsuBeatmap beatmap, - IReadOnlyList positionInfos, - int i, - float newComboProbability, - float beatProbability - ) => - i == 0 || - (positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo && rng?.NextDouble() < newComboProbability) || - OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject, true) || - (OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject) && rng?.NextDouble() < beatProbability); + private bool shouldStartNewSection(OsuBeatmap beatmap, IReadOnlyList positionInfos, int i) + { + if (i == 0) + return true; + + // Exclude new-combo-spam and 1-2-combos. + bool previousObjectStartedCombo = positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && + positionInfos[i - 1].HitObject.NewCombo; + bool previousObjectWasOnDownbeat = OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject, true); + bool previousObjectWasOnBeat = OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject); + + return (previousObjectStartedCombo && randomBool(0.6f)) || + previousObjectWasOnDownbeat || + (previousObjectWasOnBeat && randomBool(0.4f)); + } - /// - /// A flow change should occur on every combo start with a probability of (excluding new-combo-spam and 1-2-combos). - /// /// Whether a flow change should be applied at the current . - private bool shouldApplyFlowChange(IReadOnlyList positionInfos, int i, float probability) => - positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && - positionInfos[i - 1].HitObject.NewCombo && + private bool shouldApplyFlowChange(IReadOnlyList positionInfos, int i) + { + // Exclude new-combo-spam and 1-2-combos. + bool previousObjectStartedCombo = positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && + positionInfos[i - 1].HitObject.NewCombo; + + return previousObjectStartedCombo && randomBool(0.6f); + } + + /// true with a probability of , false otherwise. + private bool randomBool(float probability) => rng?.NextDouble() < probability; } } From a2f96ea120b9461edae3194e0bf7a6229fedceab Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 9 Sep 2022 17:55:29 +0900 Subject: [PATCH 14/15] Make `random` implicitly null to avoid potential incorrect behaviour in `randomBool` --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 17df1bf489..056a325dce 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -27,7 +27,7 @@ namespace osu.Game.Rulesets.Osu.Mods private static readonly float playfield_diagonal = OsuPlayfield.BASE_SIZE.LengthFast; - private Random? rng; + private Random random = null!; public void ApplyToBeatmap(IBeatmap beatmap) { @@ -36,7 +36,7 @@ namespace osu.Game.Rulesets.Osu.Mods Seed.Value ??= RNG.Next(); - rng = new Random((int)Seed.Value); + random = new Random((int)Seed.Value); var positionInfos = OsuHitObjectGenerationUtils.GeneratePositionInfos(osuBeatmap.HitObjects); @@ -50,14 +50,14 @@ namespace osu.Game.Rulesets.Osu.Mods { if (shouldStartNewSection(osuBeatmap, positionInfos, i)) { - sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.0008f); + sectionOffset = OsuHitObjectGenerationUtils.RandomGaussian(random, 0, 0.0008f); flowDirection = !flowDirection; } if (i == 0) { - positionInfos[i].DistanceFromPrevious = (float)(rng.NextDouble() * OsuPlayfield.BASE_SIZE.Y / 2); - positionInfos[i].RelativeAngle = (float)(rng.NextDouble() * 2 * Math.PI - Math.PI); + positionInfos[i].DistanceFromPrevious = (float)(random.NextDouble() * OsuPlayfield.BASE_SIZE.Y / 2); + positionInfos[i].RelativeAngle = (float)(random.NextDouble() * 2 * Math.PI - Math.PI); } else { @@ -65,11 +65,11 @@ namespace osu.Game.Rulesets.Osu.Mods float flowChangeOffset = 0; // Offsets only the angle of the current hit object. - float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); + float oneTimeOffset = OsuHitObjectGenerationUtils.RandomGaussian(random, 0, 0.002f); if (shouldApplyFlowChange(positionInfos, i)) { - flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(rng, 0, 0.002f); + flowChangeOffset = OsuHitObjectGenerationUtils.RandomGaussian(random, 0, 0.002f); flowDirection = !flowDirection; } @@ -108,9 +108,9 @@ namespace osu.Game.Rulesets.Osu.Mods bool previousObjectWasOnDownbeat = OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject, true); bool previousObjectWasOnBeat = OsuHitObjectGenerationUtils.IsHitObjectOnBeat(beatmap, positionInfos[i - 1].HitObject); - return (previousObjectStartedCombo && randomBool(0.6f)) || + return (previousObjectStartedCombo && random.NextDouble() < 0.6f) || previousObjectWasOnDownbeat || - (previousObjectWasOnBeat && randomBool(0.4f)); + (previousObjectWasOnBeat && random.NextDouble() < 0.4f); } /// Whether a flow change should be applied at the current . @@ -120,11 +120,7 @@ namespace osu.Game.Rulesets.Osu.Mods bool previousObjectStartedCombo = positionInfos[Math.Max(0, i - 2)].HitObject.IndexInCurrentCombo > 1 && positionInfos[i - 1].HitObject.NewCombo; - return previousObjectStartedCombo && randomBool(0.6f); + return previousObjectStartedCombo && random.NextDouble() < 0.6f; } - - /// true with a probability of , false otherwise. - private bool randomBool(float probability) => - rng?.NextDouble() < probability; } } From 31cd7cdca0902e10605552cfdb004531bfb4a3df Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 9 Sep 2022 18:00:51 +0900 Subject: [PATCH 15/15] Refactor `IsHitObjectOnBeat` to be understandable --- .../Utils/OsuHitObjectGenerationUtils.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs index a890dbde43..3a8b3f67d0 100644 --- a/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs +++ b/osu.Game.Rulesets.Osu/Utils/OsuHitObjectGenerationUtils.cs @@ -195,19 +195,17 @@ namespace osu.Game.Rulesets.Osu.Utils /// true if hitObject is on a (down-)beat, false otherwise. public static bool IsHitObjectOnBeat(OsuBeatmap beatmap, OsuHitObject hitObject, bool downbeatsOnly = false) { - var timingPoints = beatmap.ControlPointInfo.TimingPoints; - var currentTimingPoint = timingPoints.LastOrDefault(p => p.Time <= hitObject.StartTime); + var timingPoint = beatmap.ControlPointInfo.TimingPointAt(hitObject.StartTime); - if (currentTimingPoint == null) - return false; + double timeSinceTimingPoint = hitObject.StartTime - timingPoint.Time; - double timeSinceTimingPoint = hitObject.StartTime - currentTimingPoint.Time; + double beatLength = timingPoint.BeatLength; - double length = downbeatsOnly - ? currentTimingPoint.BeatLength * currentTimingPoint.TimeSignature.Numerator - : currentTimingPoint.BeatLength; + if (downbeatsOnly) + beatLength *= timingPoint.TimeSignature.Numerator; - return (timeSinceTimingPoint + 1) % length < 2; + // Ensure within 1ms of expected location. + return Math.Abs(timeSinceTimingPoint + 1) % beatLength < 2; } ///