// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using osuTK; using osu.Game.Rulesets.Objects.Types; using System.Collections.Generic; using osu.Game.Rulesets.Objects; using System.Linq; using osu.Framework.Caching; using osu.Framework.Configuration; using osu.Game.Audio; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Osu.Judgements; namespace osu.Game.Rulesets.Osu.Objects { public class Slider : OsuHitObject, IHasCurve { /// /// Scoring distance with a speed-adjusted beat length of 1 second. /// private const float base_scoring_distance = 100; public double EndTime => StartTime + this.SpanCount() * Path.Distance / Velocity; public double Duration => EndTime - StartTime; private Cached endPositionCache; public override Vector2 EndPosition => endPositionCache.IsValid ? endPositionCache.Value : endPositionCache.Value = Position + this.CurvePositionAt(1); public Vector2 StackedPositionAt(double t) => StackedPosition + this.CurvePositionAt(t); public override int ComboIndex { get => base.ComboIndex; set { base.ComboIndex = value; foreach (var n in NestedHitObjects.OfType()) n.ComboIndex = value; } } public override int IndexInCurrentCombo { get => base.IndexInCurrentCombo; set { base.IndexInCurrentCombo = value; foreach (var n in NestedHitObjects.OfType()) n.IndexInCurrentCombo = value; } } public readonly Bindable PathBindable = new Bindable(); public SliderPath Path { get => PathBindable.Value; set { PathBindable.Value = value; endPositionCache.Invalidate(); } } public double Distance => Path.Distance; public override Vector2 Position { get => base.Position; set { base.Position = value; if (HeadCircle != null) HeadCircle.Position = value; if (TailCircle != null) TailCircle.Position = EndPosition; endPositionCache.Invalidate(); } } public double? LegacyLastTickOffset { get; set; } /// /// The position of the cursor at the point of completion of this if it was hit /// with as few movements as possible. This is set and used by difficulty calculation. /// internal Vector2? LazyEndPosition; /// /// The distance travelled by the cursor upon completion of this if it was hit /// with as few movements as possible. This is set and used by difficulty calculation. /// internal float LazyTravelDistance; public List> NodeSamples { get; set; } = new List>(); private int repeatCount; public int RepeatCount { get => repeatCount; set { repeatCount = value; endPositionCache.Invalidate(); } } /// /// The length of one span of this . /// public double SpanDuration => Duration / this.SpanCount(); /// /// Velocity of this . /// public double Velocity { get; private set; } /// /// Spacing between s of this . /// public double TickDistance { get; private set; } /// /// An extra multiplier that affects the number of s generated by this . /// An increase in this value increases , which reduces the number of ticks generated. /// public double TickDistanceMultiplier = 1; public HitCircle HeadCircle; public SliderTailCircle TailCircle; protected override void ApplyDefaultsToSelf(ControlPointInfo controlPointInfo, BeatmapDifficulty difficulty) { base.ApplyDefaultsToSelf(controlPointInfo, difficulty); TimingControlPoint timingPoint = controlPointInfo.TimingPointAt(StartTime); DifficultyControlPoint difficultyPoint = controlPointInfo.DifficultyPointAt(StartTime); double scoringDistance = base_scoring_distance * difficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier; Velocity = scoringDistance / timingPoint.BeatLength; TickDistance = scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier; } protected override void CreateNestedHitObjects() { base.CreateNestedHitObjects(); createSliderEnds(); createTicks(); createRepeatPoints(); if (LegacyLastTickOffset != null) TailCircle.StartTime = Math.Max(StartTime + Duration / 2, TailCircle.StartTime - LegacyLastTickOffset.Value); } private void createSliderEnds() { HeadCircle = new SliderCircle { StartTime = StartTime, Position = Position, Samples = getNodeSamples(0), SampleControlPoint = SampleControlPoint, IndexInCurrentCombo = IndexInCurrentCombo, ComboIndex = ComboIndex, }; TailCircle = new SliderTailCircle(this) { StartTime = EndTime, Position = EndPosition, IndexInCurrentCombo = IndexInCurrentCombo, ComboIndex = ComboIndex, }; AddNested(HeadCircle); AddNested(TailCircle); } private void createTicks() { // A very lenient maximum length of a slider for ticks to be generated. // This exists for edge cases such as /b/1573664 where the beatmap has been edited by the user, and should never be reached in normal usage. const double max_length = 100000; var length = Math.Min(max_length, Path.Distance); var tickDistance = MathHelper.Clamp(TickDistance, 0, length); if (tickDistance == 0) return; var minDistanceFromEnd = Velocity * 0.01; var spanCount = this.SpanCount(); for (var span = 0; span < spanCount; span++) { var spanStartTime = StartTime + span * SpanDuration; var reversed = span % 2 == 1; for (var d = tickDistance; d <= length; d += tickDistance) { if (d > length - minDistanceFromEnd) break; var distanceProgress = d / length; var timeProgress = reversed ? 1 - distanceProgress : distanceProgress; var firstSample = Samples.FirstOrDefault(s => s.Name == SampleInfo.HIT_NORMAL) ?? Samples.FirstOrDefault(); // TODO: remove this when guaranteed sort is present for samples (https://github.com/ppy/osu/issues/1933) var sampleList = new List(); if (firstSample != null) sampleList.Add(new SampleInfo { Bank = firstSample.Bank, Volume = firstSample.Volume, Name = @"slidertick", }); AddNested(new SliderTick { SpanIndex = span, SpanStartTime = spanStartTime, StartTime = spanStartTime + timeProgress * SpanDuration, Position = Position + Path.PositionAt(distanceProgress), StackHeight = StackHeight, Scale = Scale, Samples = sampleList }); } } } private void createRepeatPoints() { for (int repeatIndex = 0, repeat = 1; repeatIndex < RepeatCount; repeatIndex++, repeat++) { AddNested(new RepeatPoint { RepeatIndex = repeatIndex, SpanDuration = SpanDuration, StartTime = StartTime + repeat * SpanDuration, Position = Position + Path.PositionAt(repeat % 2), StackHeight = StackHeight, Scale = Scale, Samples = getNodeSamples(1 + repeatIndex) }); } } private List getNodeSamples(int nodeIndex) { if (nodeIndex < NodeSamples.Count) return NodeSamples[nodeIndex]; return Samples; } public override Judgement CreateJudgement() => new OsuJudgement(); } }