osu/osu.Game.Rulesets.Osu/Mods/OsuModTarget.cs

530 lines
19 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
2021-05-19 14:25:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
2021-05-19 14:25:56 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
2021-05-19 14:25:56 +00:00
using osu.Framework.Utils;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Timing;
using osu.Game.Configuration;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics;
2021-05-19 14:25:56 +00:00
using osu.Game.Graphics.Containers;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
2021-05-19 14:25:56 +00:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types;
2021-05-19 14:25:56 +00:00
using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.Osu.UI;
2021-06-26 03:34:10 +00:00
using osu.Game.Rulesets.Osu.Utils;
2021-05-19 14:25:56 +00:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Skinning;
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Osu.Mods
{
2021-05-19 14:25:56 +00:00
public class OsuModTarget : ModWithVisibilityAdjustment, IApplicableToDrawableRuleset<OsuHitObject>,
2021-06-23 08:29:36 +00:00
IApplicableToHealthProcessor, IApplicableToDifficulty, IApplicableFailOverride,
IHasSeed, IMutateApproachCircles
2018-04-13 09:19:50 +00:00
{
public override string Name => "Target";
public override string Acronym => "TP";
public override ModType Type => ModType.Conversion;
2020-01-14 13:22:00 +00:00
public override IconUsage? Icon => OsuIcon.ModTarget;
2018-04-13 09:19:50 +00:00
public override string Description => @"Practice keeping up with the beat of the song.";
public override double ScoreMultiplier => 1;
2021-05-19 14:25:56 +00:00
2021-06-23 08:29:36 +00:00
public override Type[] IncompatibleMods => new[] { typeof(IMutateApproachCircles) };
[SettingSource("Seed", "Use a custom seed instead of a random one", SettingControlType = typeof(SeedSettingsControl))]
public Bindable<int?> Seed { get; } = new Bindable<int?>
{
Default = null,
Value = null
};
2021-06-23 08:50:05 +00:00
#region Constants
2021-06-10 02:58:42 +00:00
2021-06-18 06:39:46 +00:00
/// <summary>
/// Jump distance for circles in the last combo
2021-06-18 06:39:46 +00:00
/// </summary>
2021-06-19 02:04:48 +00:00
private const float max_base_distance = 333f;
2021-06-18 06:39:46 +00:00
/// <summary>
/// The maximum allowed jump distance after multipliers are applied
2021-06-18 06:39:46 +00:00
/// </summary>
2021-06-19 02:04:48 +00:00
private const float distance_cap = 380f;
// The distances from the hit objects to the borders of the playfield they start to "turn around" and curve towards the middle.
// The closer the hit objects draw to the border, the sharper the turn
private const byte border_distance_x = 192;
private const byte border_distance_y = 144;
2021-06-18 06:39:46 +00:00
/// <summary>
/// The extent of rotation towards playfield centre when a circle is near the edge
/// </summary>
private const float edge_rotation_multiplier = 0.75f;
2021-06-19 02:04:48 +00:00
/// <summary>
/// Number of recent circles to check for overlap
/// </summary>
private const int overlap_check_count = 5;
/// <summary>
/// Duration of the undimming animation
2021-06-18 06:39:46 +00:00
/// </summary>
private const double undim_duration = 96;
/// <summary>
/// Acceptable difference for timing comparisons
/// </summary>
private const double timing_precision = 1;
2021-06-23 08:50:05 +00:00
#endregion
#region Private Fields
private ControlPointInfo controlPointInfo;
private List<OsuHitObject> origHitObjects;
private Random rng;
2021-06-23 08:50:05 +00:00
#endregion
#region Sudden Death (IApplicableFailOverride)
public bool PerformFail() => true;
public bool RestartOnFail => false;
2021-05-19 14:25:56 +00:00
public void ApplyToHealthProcessor(HealthProcessor healthProcessor)
{
// Sudden death
healthProcessor.FailConditions += (_, result)
=> result.Type.AffectsCombo()
2021-06-14 13:34:34 +00:00
&& !result.IsHit;
2021-05-19 14:25:56 +00:00
}
2021-06-23 08:50:05 +00:00
#endregion
2021-06-15 03:06:56 +00:00
2021-06-23 08:50:05 +00:00
#region Reduce AR (IApplicableToDifficulty)
2021-06-15 03:06:56 +00:00
public void ReadFromDifficulty(BeatmapDifficulty difficulty)
{
}
public void ApplyToDifficulty(BeatmapDifficulty difficulty)
{
// Decrease AR to increase preempt time
difficulty.ApproachRate *= 0.5f;
}
2021-06-23 08:50:05 +00:00
#endregion
2021-06-23 08:50:05 +00:00
#region Circle Transforms (ModWithVisibilityAdjustment)
protected override void ApplyIncreasedVisibilityState(DrawableHitObject drawable, ArmedState state)
{
}
protected override void ApplyNormalVisibilityState(DrawableHitObject drawable, ArmedState state)
{
if (!(drawable is DrawableHitCircle circle)) return;
var h = (OsuHitObject)drawable.HitObject;
using (drawable.BeginAbsoluteSequence(h.StartTime - h.TimePreempt))
{
drawable.ScaleTo(0.5f)
.Then().ScaleTo(1f, h.TimePreempt);
var colour = drawable.Colour;
drawable.FadeColour(OsuColour.Gray(0.45f))
2021-06-18 06:39:46 +00:00
.Then().Delay(h.TimePreempt - controlPointInfo.TimingPointAt(h.StartTime).BeatLength - undim_duration)
.FadeColour(colour, undim_duration);
2021-06-24 05:39:50 +00:00
// Remove approach circles
circle.ApproachCircle.Hide();
}
}
2021-06-23 08:50:05 +00:00
#endregion
#region Beatmap Generation (IApplicableToBeatmap)
public override void ApplyToBeatmap(IBeatmap beatmap)
{
2021-06-23 08:50:05 +00:00
Seed.Value ??= RNG.Next();
rng = new Random(Seed.Value.GetValueOrDefault());
2021-06-23 08:50:05 +00:00
var osuBeatmap = (OsuBeatmap)beatmap;
if (osuBeatmap.HitObjects.Count == 0) return;
controlPointInfo = osuBeatmap.ControlPointInfo;
origHitObjects = osuBeatmap.HitObjects.OrderBy(x => x.StartTime).ToList();
2021-06-23 08:50:05 +00:00
var hitObjects = generateBeats(osuBeatmap)
2021-06-24 04:20:23 +00:00
.Select(beat =>
2021-06-23 08:50:05 +00:00
{
var newCircle = new HitCircle();
newCircle.ApplyDefaults(controlPointInfo, osuBeatmap.BeatmapInfo.BaseDifficulty);
2021-06-24 04:20:23 +00:00
newCircle.StartTime = beat;
2021-06-23 08:50:05 +00:00
return (OsuHitObject)newCircle;
}).ToList();
addHitSamples(hitObjects);
2021-06-23 08:50:05 +00:00
fixComboInfo(hitObjects);
2021-06-23 08:50:05 +00:00
randomizeCirclePos(hitObjects);
osuBeatmap.HitObjects = hitObjects;
base.ApplyToBeatmap(beatmap);
}
private IEnumerable<double> generateBeats(IBeatmap beatmap)
2021-06-15 03:06:56 +00:00
{
2021-05-19 14:25:56 +00:00
var startTime = origHitObjects.First().StartTime;
var endObj = origHitObjects.Last();
var endTime = endObj.GetEndTime();
2021-05-19 14:25:56 +00:00
2021-06-15 03:06:56 +00:00
var beats = beatmap.ControlPointInfo.TimingPoints
2021-06-24 04:20:23 +00:00
// Ignore timing points after endTime
.Where(timingPoint => almostBigger(endTime, timingPoint.Time))
2021-06-24 04:20:23 +00:00
// Generate the beats
.SelectMany(timingPoint => getBeatsForTimingPoint(timingPoint, endTime))
2021-06-24 04:20:23 +00:00
// Remove beats before startTime
.Where(beat => almostBigger(beat, startTime))
2021-06-24 04:20:23 +00:00
// Remove beats during breaks
2021-06-24 02:56:14 +00:00
.Where(beat => !isInsideBreakPeriod(beatmap.Breaks, beat))
2021-06-15 03:06:56 +00:00
.ToList();
// Remove beats that are too close to the next one (e.g. due to timing point changes)
for (var i = beats.Count - 2; i >= 0; i--)
2021-06-15 03:06:56 +00:00
{
var beat = beats[i];
2021-06-14 13:34:34 +00:00
if (almostBigger(beatmap.ControlPointInfo.TimingPointAt(beat).BeatLength / 2, beats[i + 1] - beat))
2021-06-24 02:56:14 +00:00
beats.RemoveAt(i);
2021-06-15 03:06:56 +00:00
}
2021-05-19 14:25:56 +00:00
2021-06-15 03:06:56 +00:00
return beats;
}
private void addHitSamples(IEnumerable<OsuHitObject> hitObjects)
2021-06-15 03:06:56 +00:00
{
var lastSampleIdx = 0;
foreach (var obj in hitObjects)
2021-05-20 03:57:13 +00:00
{
var samples = getSamplesAtTime(origHitObjects, obj.StartTime);
2021-06-14 13:34:34 +00:00
2021-05-20 03:57:13 +00:00
if (samples == null)
{
// If samples aren't available at the exact start time of the object,
// use samples (without additions) in the closest original hit object instead
while (lastSampleIdx < origHitObjects.Count && origHitObjects[lastSampleIdx].StartTime <= obj.StartTime)
lastSampleIdx++;
if (lastSampleIdx >= origHitObjects.Count) continue;
if (lastSampleIdx > 0)
{
2021-06-24 05:39:50 +00:00
// Get samples from the previous hit object if it is closer in time
if (obj.StartTime - origHitObjects[lastSampleIdx - 1].StartTime < origHitObjects[lastSampleIdx].StartTime - obj.StartTime)
lastSampleIdx--;
}
// Remove additions
obj.Samples = origHitObjects[lastSampleIdx].Samples.Where(s => !HitSampleInfo.AllAdditions.Contains(s.Name)).ToList();
2021-05-20 03:57:13 +00:00
}
else
obj.Samples = samples;
2021-05-20 03:57:13 +00:00
}
2021-06-15 03:06:56 +00:00
}
2021-05-19 14:25:56 +00:00
private void fixComboInfo(List<OsuHitObject> hitObjects)
2021-06-15 03:06:56 +00:00
{
2021-06-24 03:21:43 +00:00
// Copy combo indices from the closest preceding object
hitObjects.ForEach(obj =>
2021-05-20 03:57:13 +00:00
{
var closestOrigObj = origHitObjects.FindLast(y => almostBigger(obj.StartTime, y.StartTime));
2021-06-24 03:21:43 +00:00
2021-06-24 05:39:50 +00:00
// It shouldn't be possible for closestOrigObj to be null
2021-06-24 03:21:43 +00:00
// But if it is, obj should be in the first combo
obj.ComboIndex = closestOrigObj?.ComboIndex ?? 0;
2021-05-20 03:57:13 +00:00
});
2021-06-24 03:21:43 +00:00
// The copied combo indices may not be continuous if the original map starts and ends a combo in between beats
// e.g. A stream with each object starting a new combo
// So combo indices need to be reprocessed to ensure continuity
// Other kinds of combo info are also added in the process
2021-05-20 03:57:13 +00:00
var combos = hitObjects.GroupBy(x => x.ComboIndex).ToList();
2021-06-14 13:34:34 +00:00
for (var i = 0; i < combos.Count; i++)
2021-05-19 14:25:56 +00:00
{
2021-05-20 03:57:13 +00:00
var group = combos[i].ToList();
group.First().NewCombo = true;
group.Last().LastInCombo = true;
2021-05-19 14:25:56 +00:00
for (var j = 0; j < group.Count; j++)
2021-05-19 14:25:56 +00:00
{
2021-05-20 03:57:13 +00:00
var x = group[j];
x.ComboIndex = i;
x.IndexInCurrentCombo = j;
}
}
2021-06-15 03:06:56 +00:00
}
private void randomizeCirclePos(IReadOnlyList<OsuHitObject> hitObjects)
{
2021-06-19 03:12:29 +00:00
if (hitObjects.Count == 0) return;
2021-06-15 03:06:56 +00:00
float nextSingle(float max = 1f) => (float)(rng.NextDouble() * max);
2021-05-19 14:25:56 +00:00
const float two_pi = MathF.PI * 2;
var direction = two_pi * nextSingle();
var maxComboIndex = hitObjects.Last().ComboIndex;
2021-06-14 13:34:34 +00:00
for (var i = 0; i < hitObjects.Count; i++)
2021-05-20 03:57:13 +00:00
{
var obj = hitObjects[i];
var lastPos = i == 0
? Vector2.Divide(OsuPlayfield.BASE_SIZE, 2)
: hitObjects[i - 1].Position;
2021-06-19 03:12:29 +00:00
var distance = maxComboIndex == 0
? (float)obj.Radius
: map(obj.ComboIndex, 0, maxComboIndex, (float)obj.Radius, max_base_distance);
if (obj.NewCombo) distance *= 1.5f;
if (obj.Kiai) distance *= 1.2f;
2021-06-18 06:39:46 +00:00
distance = Math.Min(distance_cap, distance);
2021-06-19 02:04:48 +00:00
// Attempt to place the circle at a place that does not overlap with previous ones
var tryCount = 0;
do
{
if (tryCount > 0) direction = two_pi * nextSingle();
2021-06-19 02:04:48 +00:00
var relativePos = new Vector2(
distance * MathF.Cos(direction),
distance * MathF.Sin(direction)
2021-06-19 02:04:48 +00:00
);
2021-06-24 04:20:23 +00:00
// Rotate the new circle away from playfield border
2021-06-26 03:34:10 +00:00
relativePos = OsuHitObjectGenerationUtils.RotateAwayFromEdge(lastPos, relativePos, edge_rotation_multiplier);
direction = MathF.Atan2(relativePos.Y, relativePos.X);
2021-06-19 02:04:48 +00:00
var newPosition = Vector2.Add(lastPos, relativePos);
2021-06-14 13:34:34 +00:00
2021-06-19 02:04:48 +00:00
obj.Position = newPosition;
clampToPlayfield(obj);
2021-06-19 02:04:48 +00:00
tryCount++;
if (tryCount % 10 == 0) distance *= 0.9f;
} while (distance >= obj.Radius * 2 && checkForOverlap(hitObjects.SkipLast(hitObjects.Count - i).TakeLast(overlap_check_count), obj));
if (obj.LastInCombo)
direction = two_pi * nextSingle();
2021-05-20 03:57:13 +00:00
else
direction += distance / distance_cap * (nextSingle() * two_pi - MathF.PI);
2021-05-19 14:25:56 +00:00
}
}
2021-06-23 08:50:05 +00:00
#endregion
#region Metronome (IApplicableToDrawableRuleset)
public void ApplyToDrawableRuleset(DrawableRuleset<OsuHitObject> drawableRuleset)
{
drawableRuleset.Overlays.Add(new TargetBeatContainer());
}
public class TargetBeatContainer : BeatSyncedContainer
{
private PausableSkinnableSound sample;
public TargetBeatContainer()
{
Divisor = 1;
}
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
{
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
if (!IsBeatSyncedWithTrack) return;
sample?.Play();
}
[BackgroundDependencyLoader]
private void load()
{
InternalChildren = new Drawable[]
{
sample = new PausableSkinnableSound(new SampleInfo("Gameplay/nightcore-hat")) // todo: use another sample
};
}
}
#endregion
#region Helper Subroutines
/// <summary>
/// Check if a given time is inside a <see cref="BreakPeriod"/>.
/// </summary>
/// <remarks>
/// The given time is also considered to be inside a break if it is earlier than the
/// start time of the first original hit object after the break.
/// </remarks>
/// <param name="breaks">The breaks of the beatmap.</param>
/// <param name="time">The time to be checked.</param>=
private bool isInsideBreakPeriod(IEnumerable<BreakPeriod> breaks, double time)
{
2021-06-24 02:56:14 +00:00
return breaks.Any(breakPeriod =>
{
var firstObjAfterBreak = origHitObjects.First(obj => almostBigger(obj.StartTime, breakPeriod.EndTime));
return almostBigger(time, breakPeriod.StartTime)
&& almostBigger(firstObjAfterBreak.StartTime, time);
});
}
private IEnumerable<double> getBeatsForTimingPoint(TimingControlPoint timingPoint, double mapEndTime)
{
var beats = new List<double>();
int i = 0;
var currentTime = timingPoint.Time;
while (almostBigger(mapEndTime, currentTime) && controlPointInfo.TimingPointAt(currentTime) == timingPoint)
{
beats.Add(Math.Floor(currentTime));
i++;
currentTime = timingPoint.Time + i * timingPoint.BeatLength;
}
return beats;
}
2021-05-19 14:25:56 +00:00
/// <summary>
/// Get samples (if any) for a specific point in time.
2021-05-19 14:25:56 +00:00
/// </summary>
/// <remarks>
/// Samples will be returned if a hit circle or a slider node exists at that point of time.
2021-05-19 14:25:56 +00:00
/// </remarks>
/// <param name="hitObjects">The list of hit objects in a beatmap, ordered by StartTime</param>
/// <param name="time">The point in time to get samples for</param>
/// <returns>Hit samples</returns>
2021-06-14 13:34:34 +00:00
private IList<HitSampleInfo> getSamplesAtTime(IEnumerable<OsuHitObject> hitObjects, double time)
2021-05-19 14:25:56 +00:00
{
2021-06-24 04:20:23 +00:00
// Get a hit object that
// either has StartTime equal to the target time
// or has a repeat node at the target time
var sampleObj = hitObjects.FirstOrDefault(hitObject =>
2021-05-19 14:25:56 +00:00
{
if (almostEquals(time, hitObject.StartTime))
return true;
2021-06-14 13:34:34 +00:00
if (!(hitObject is IHasRepeats s))
2021-06-14 13:34:34 +00:00
return false;
if (!almostBigger(time, hitObject.StartTime)
|| !almostBigger(s.EndTime, time))
2021-06-14 13:34:34 +00:00
return false;
return nodeIndexFromTime(s, time - hitObject.StartTime) != -1;
2021-05-19 14:25:56 +00:00
});
if (sampleObj == null) return null;
2021-06-14 13:34:34 +00:00
IList<HitSampleInfo> samples;
if (sampleObj is IHasRepeats slider)
2021-06-24 02:38:21 +00:00
samples = slider.NodeSamples[nodeIndexFromTime(slider, time - sampleObj.StartTime)];
2021-05-19 14:25:56 +00:00
else
samples = sampleObj.Samples;
2021-06-14 13:34:34 +00:00
2021-05-19 14:25:56 +00:00
return samples;
}
/// <summary>
/// Get the repeat node at a point in time.
/// </summary>
/// <param name="curve">The slider.</param>
/// <param name="timeSinceStart">The time since the start time of the slider.</param>
/// <returns>Index of the node. -1 if there isn't a node at the specific time.</returns>
private int nodeIndexFromTime(IHasRepeats curve, double timeSinceStart)
{
double spanDuration = curve.Duration / curve.SpanCount();
double nodeIndex = timeSinceStart / spanDuration;
if (almostEquals(nodeIndex - Math.Round(nodeIndex), 0))
return (int)Math.Round(nodeIndex);
return -1;
}
private bool checkForOverlap(IEnumerable<OsuHitObject> objectsToCheck, OsuHitObject target)
2021-06-19 02:04:48 +00:00
{
return objectsToCheck.Any(h => Vector2.Distance(h.Position, target.Position) < target.Radius * 2);
2021-06-19 02:04:48 +00:00
}
2021-06-24 04:20:23 +00:00
/// <summary>
/// Move the hit object into playfield, taking its radius into account.
/// </summary>
/// <param name="obj">The hit object to be clamped.</param>
private void clampToPlayfield(OsuHitObject obj)
{
var position = obj.Position;
var radius = (float)obj.Radius;
if (position.Y < radius)
position.Y = radius;
else if (position.Y > OsuPlayfield.BASE_SIZE.Y - radius)
position.Y = OsuPlayfield.BASE_SIZE.Y - radius;
if (position.X < radius)
position.X = radius;
else if (position.X > OsuPlayfield.BASE_SIZE.X - radius)
position.X = OsuPlayfield.BASE_SIZE.X - radius;
obj.Position = position;
}
2021-06-23 08:50:05 +00:00
private static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh)
2021-05-19 14:25:56 +00:00
{
2021-06-23 08:50:05 +00:00
return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
2021-05-19 14:25:56 +00:00
}
2021-06-23 08:50:05 +00:00
private static bool almostBigger(double value1, double value2)
{
return Precision.AlmostBigger(value1, value2, timing_precision);
}
private static bool almostEquals(double value1, double value2)
{
return Precision.AlmostEquals(value1, value2, timing_precision);
}
2021-06-23 08:50:05 +00:00
#endregion
2018-04-13 09:19:50 +00:00
}
}