1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Code optimisation

This commit is contained in:
Pasi4K5 2022-06-19 23:03:41 +02:00
parent 9090e75020
commit 1bb27cd488
2 changed files with 22 additions and 19 deletions

View File

@ -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);
}
/// <param name="targetDistance">The target distance between the previous and the current <see cref="OsuHitObject"/>.</param>
/// <param name="offset">The angle (in rad) by which the target angle should be offset.</param>
/// <param name="flowDirection">Whether the relative angle should be positive or negative.</param>
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;
}
}
}

View File

@ -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;
}
/// <param name="targetDistance">The target distance between the previous and the current <see cref="OsuHitObject"/>.</param>
/// <param name="offset">The angle (in rad) by which the target angle should be offset.</param>
/// <param name="flowDirection">Whether the relative angle should be positive or negative.</param>
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;
}
}