// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; namespace osu.Game.Rulesets.Objects { /// /// Used to find the lowest beat divisor that a aligns to in an . /// public class BeatDivisorFinder { private readonly IBeatmap beatmap; /// /// Creates a new instance. /// /// The beatmap to use when calculating beat divisor alignment. public BeatDivisorFinder(IBeatmap beatmap) { this.beatmap = beatmap; } private static readonly int[] snaps = { 1, 2, 3, 4, 6, 8, 12, 16 }; /// /// Finds the lowest beat divisor that the given aligns to. /// /// The to evaluate. public int FindDivisor(HitObject hitObject) { TimingControlPoint currentTimingPoint = beatmap.ControlPointInfo.TimingPointAt(hitObject.StartTime); double snapResult = (hitObject.StartTime - currentTimingPoint.Time) % (currentTimingPoint.BeatLength * 4); foreach (var snap in snaps) { if (almostDivisibleBy(snapResult, currentTimingPoint.BeatLength / snap)) return snap; } return 0; } private const double leniency_ms = 1.0; private static bool almostDivisibleBy(double dividend, double divisor) { double remainder = Math.Abs(dividend) % divisor; return Precision.AlmostEquals(remainder, 0, leniency_ms) || Precision.AlmostEquals(remainder - divisor, 0, leniency_ms); } } }