// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using osuTK; using osu.Game.Rulesets.Objects.Types; using System.Collections.Generic; using osu.Game.Rulesets.Objects; using System.Linq; using System.Threading; using JetBrains.Annotations; using Newtonsoft.Json; using osu.Framework.Bindables; using osu.Framework.Caching; using osu.Game.Audio; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects.Legacy; using osu.Game.Rulesets.Osu.Judgements; using osu.Game.Rulesets.Scoring; namespace osu.Game.Rulesets.Osu.Objects { public class Slider : OsuHitObject, IHasPathWithRepeats, IHasSliderVelocity, IHasGenerateTicks { public double EndTime => StartTime + this.SpanCount() * Path.Distance / Velocity; [JsonIgnore] public double Duration { get => EndTime - StartTime; set => throw new System.NotSupportedException($"Adjust via {nameof(RepeatCount)} instead"); // can be implemented if/when needed. } public override IList AuxiliarySamples => CreateSlidingSamples().Concat(TailSamples).ToArray(); private readonly Cached endPositionCache = new Cached(); public override Vector2 EndPosition => endPositionCache.IsValid ? endPositionCache.Value : endPositionCache.Value = Position + this.CurvePositionAt(1); public Vector2 StackedPositionAt(double t) => StackedPosition + this.CurvePositionAt(t); private readonly SliderPath path = new SliderPath { OptimiseCatmull = true }; public SliderPath Path { get => path; set { path.ControlPoints.Clear(); path.ControlPoints.AddRange(value.ControlPoints.Select(c => new PathControlPoint(c.Position, c.Type))); path.ExpectedDistance.Value = value.ExpectedDistance.Value; } } public double Distance => Path.Distance; public override Vector2 Position { get => base.Position; set { base.Position = value; updateNestedPositions(); } } /// /// 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; /// /// The time taken 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 double LazyTravelTime; public IList> NodeSamples { get; set; } = new List>(); [JsonIgnore] public IList TailSamples { get; private set; } private int repeatCount; public int RepeatCount { get => repeatCount; set { repeatCount = value; updateNestedPositions(); } } /// /// The length of one span of this . /// public double SpanDuration => Duration / this.SpanCount(); /// /// The computed velocity of this . This is the amount of path distance travelled in 1 ms. /// 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; /// /// If , 's judgement is fully handled by its nested s. /// If , this will be judged proportionally to the number of nested s hit. /// public bool ClassicSliderBehaviour { get => classicSliderBehaviour; set { classicSliderBehaviour = value; if (HeadCircle != null) HeadCircle.ClassicSliderBehaviour = value; if (TailCircle != null) TailCircle.ClassicSliderBehaviour = value; } } private bool classicSliderBehaviour; public BindableNumber SliderVelocityMultiplierBindable { get; } = new BindableDouble(1) { MinValue = 0.1, MaxValue = 10 }; public double SliderVelocityMultiplier { get => SliderVelocityMultiplierBindable.Value; set => SliderVelocityMultiplierBindable.Value = value; } public bool GenerateTicks { get; set; } = true; [JsonIgnore] public SliderHeadCircle HeadCircle { get; protected set; } [JsonIgnore] public SliderTailCircle TailCircle { get; protected set; } [JsonIgnore] [CanBeNull] public SliderRepeat LastRepeat { get; protected set; } public Slider() { SamplesBindable.CollectionChanged += (_, _) => UpdateNestedSamples(); Path.Version.ValueChanged += _ => updateNestedPositions(); } protected override void ApplyDefaultsToSelf(ControlPointInfo controlPointInfo, IBeatmapDifficultyInfo difficulty) { base.ApplyDefaultsToSelf(controlPointInfo, difficulty); TimingControlPoint timingPoint = controlPointInfo.TimingPointAt(StartTime); Velocity = BASE_SCORING_DISTANCE * difficulty.SliderMultiplier / LegacyRulesetExtensions.GetPrecisionAdjustedBeatLength(this, timingPoint, OsuRuleset.SHORT_NAME); // WARNING: this is intentionally not computed as `BASE_SCORING_DISTANCE * difficulty.SliderMultiplier` // for backwards compatibility reasons (intentionally introducing floating point errors to match stable). double scoringDistance = Velocity * timingPoint.BeatLength; TickDistance = GenerateTicks ? (scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier) : double.PositiveInfinity; } protected override void CreateNestedHitObjects(CancellationToken cancellationToken) { base.CreateNestedHitObjects(cancellationToken); var sliderEvents = SliderEventGenerator.Generate(StartTime, SpanDuration, Velocity, TickDistance, Path.Distance, this.SpanCount(), cancellationToken); foreach (var e in sliderEvents) { switch (e.Type) { case SliderEventType.Tick: AddNested(new SliderTick { SpanIndex = e.SpanIndex, SpanStartTime = e.SpanStartTime, StartTime = e.Time, Position = Position + Path.PositionAt(e.PathProgress), StackHeight = StackHeight, }); break; case SliderEventType.Head: AddNested(HeadCircle = new SliderHeadCircle { StartTime = e.Time, Position = Position, StackHeight = StackHeight, ClassicSliderBehaviour = ClassicSliderBehaviour, }); break; case SliderEventType.Tail: AddNested(TailCircle = new SliderTailCircle(this) { RepeatIndex = e.SpanIndex, StartTime = e.Time, Position = EndPosition, StackHeight = StackHeight, ClassicSliderBehaviour = ClassicSliderBehaviour, }); break; case SliderEventType.Repeat: AddNested(LastRepeat = new SliderRepeat(this) { RepeatIndex = e.SpanIndex, StartTime = StartTime + (e.SpanIndex + 1) * SpanDuration, Position = Position + Path.PositionAt(e.PathProgress), StackHeight = StackHeight, }); break; } } UpdateNestedSamples(); } private void updateNestedPositions() { endPositionCache.Invalidate(); if (HeadCircle != null) HeadCircle.Position = Position; if (TailCircle != null) TailCircle.Position = EndPosition; if (LastRepeat != null) LastRepeat.Position = RepeatCount % 2 == 0 ? Position : Position + Path.PositionAt(1); } protected void UpdateNestedSamples() { this.PopulateNodeSamples(); // TODO: remove this when guaranteed sort is present for samples (https://github.com/ppy/osu/issues/1933) HitSampleInfo tickSample = (Samples.FirstOrDefault(s => s.Name == HitSampleInfo.HIT_NORMAL) ?? Samples.FirstOrDefault())?.With("slidertick"); foreach (var nested in NestedHitObjects) { switch (nested) { case SliderTick tick: tick.SamplesBindable.Clear(); if (tickSample != null) tick.SamplesBindable.Add(tickSample); break; case SliderRepeat repeat: repeat.Samples = this.GetNodeSamples(repeat.RepeatIndex + 1); break; } } if (HeadCircle != null) HeadCircle.Samples = this.GetNodeSamples(0); // The samples should be attached to the slider tail, however this can only be done if LastTick is removed otherwise they would play earlier than they're intended to. // (see mapping logic in `CreateNestedHitObjects` above) // // For now, the samples are played by the slider itself at the correct end time. TailSamples = this.GetNodeSamples(repeatCount + 1); } public override Judgement CreateJudgement() => ClassicSliderBehaviour // Final combo is provided by the slider itself - see logic in `DrawableSlider.CheckForResult()` ? new OsuJudgement() // Final combo is provided by the tail circle - see `SliderTailCircle` : new OsuIgnoreJudgement(); protected override HitWindows CreateHitWindows() => HitWindows.Empty; } }