1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Mods/OsuModTarget.cs

442 lines
17 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 17:19:50 +08:00
2021-05-19 22:25:56 +08: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 22:25:56 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
2021-05-19 22:25:56 +08:00
using osu.Framework.Utils;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Configuration;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
2021-05-19 22:25:56 +08:00
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Mods;
2021-05-19 22:25:56 +08:00
using osu.Game.Rulesets.Objects.Drawables;
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;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Mods
{
2021-05-19 22:25:56 +08:00
public class OsuModTarget : ModWithVisibilityAdjustment, IApplicableToDrawableRuleset<OsuHitObject>,
2021-06-14 21:34:34 +08:00
IApplicableToHealthProcessor, IApplicableToDifficulty, IApplicableFailOverride, IHasSeed
2018-04-13 17:19:50 +08:00
{
public override string Name => "Target";
public override string Acronym => "TP";
public override ModType Type => ModType.Conversion;
2020-01-14 21:22:00 +08:00
public override IconUsage? Icon => OsuIcon.ModTarget;
2018-04-13 17:19:50 +08:00
public override string Description => @"Practice keeping up with the beat of the song.";
public override double ScoreMultiplier => 1;
2021-05-19 22:25:56 +08:00
[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-10 10:58:42 +08:00
public bool RestartOnFail => false;
public bool DisplayResultsOnFail => true;
// Maximum distance to jump
private const float max_distance = 250f;
// 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;
private ControlPointInfo controlPointInfo;
public bool PerformFail()
{
return true;
}
2021-05-19 22:25:56 +08:00
public void ApplyToHealthProcessor(HealthProcessor healthProcessor)
{
// Sudden death
healthProcessor.FailConditions += (_, result)
=> result.Type.AffectsCombo()
2021-06-14 21:34:34 +08:00
&& !result.IsHit;
2021-05-19 22:25:56 +08:00
}
public override void ApplyToBeatmap(IBeatmap beatmap)
{
Seed.Value ??= RNG.Next();
2021-05-19 22:25:56 +08:00
var osuBeatmap = (OsuBeatmap)beatmap;
controlPointInfo = osuBeatmap.ControlPointInfo;
2021-05-19 22:25:56 +08:00
var origHitObjects = osuBeatmap.HitObjects.OrderBy(x => x.StartTime).ToList();
2021-06-15 11:06:56 +08:00
var hitObjects = generateBeats(osuBeatmap, origHitObjects)
.Select(x =>
{
var newCircle = new HitCircle();
newCircle.ApplyDefaults(controlPointInfo, osuBeatmap.BeatmapInfo.BaseDifficulty);
2021-06-15 11:06:56 +08:00
newCircle.StartTime = x;
return (OsuHitObject)newCircle;
}).ToList();
addHitSamples(hitObjects, origHitObjects);
fixComboInfo(hitObjects, origHitObjects);
randomizeCirclePos(hitObjects);
osuBeatmap.HitObjects = hitObjects;
base.ApplyToBeatmap(beatmap);
}
public void ReadFromDifficulty(BeatmapDifficulty difficulty)
{
}
public void ApplyToDifficulty(BeatmapDifficulty difficulty)
{
// Decrease AR to increase preempt time
difficulty.ApproachRate *= 0.5f;
difficulty.CircleSize *= 0.75f;
}
// Background metronome
public void ApplyToDrawableRuleset(DrawableRuleset<OsuHitObject> drawableRuleset)
{
drawableRuleset.Overlays.Add(new TargetBeatContainer());
}
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;
var avgColour = colour.AverageColour.Linear;
drawable.FadeColour(new Color4(avgColour.R * 0.45f, avgColour.G * 0.45f, avgColour.B * 0.45f, avgColour.A))
2021-06-18 13:18:44 +08:00
.Then().Delay(h.TimePreempt - controlPointInfo.TimingPointAt(h.StartTime).BeatLength - 96).FadeColour(colour, 96);
// remove approach circles
circle.ApproachCircle.Hide();
}
}
private static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh)
{
return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
}
2021-06-15 11:06:56 +08:00
private IEnumerable<double> generateBeats(IBeatmap beatmap, IReadOnlyCollection<OsuHitObject> origHitObjects)
{
2021-05-19 22:25:56 +08:00
var startTime = origHitObjects.First().StartTime;
var endObj = origHitObjects.Last();
2021-06-14 21:34:34 +08:00
var endTime = endObj switch
2021-05-19 22:25:56 +08:00
{
2021-06-14 21:34:34 +08:00
Slider slider => slider.EndTime,
Spinner spinner => spinner.EndTime,
_ => endObj.StartTime
};
2021-05-19 22:25:56 +08:00
2021-06-15 11:06:56 +08:00
var beats = beatmap.ControlPointInfo.TimingPoints
.Where(x => Precision.AlmostBigger(endTime, x.Time))
.SelectMany(tp =>
{
var tpBeats = new List<double>();
var currentTime = tp.Time;
while (Precision.AlmostBigger(endTime, currentTime) && beatmap.ControlPointInfo.TimingPointAt(currentTime) == tp)
{
tpBeats.Add(Math.Floor(currentTime));
2021-06-15 11:06:56 +08:00
currentTime += tp.BeatLength;
}
return tpBeats;
})
.Where(x => Precision.AlmostBigger(x, startTime))
// Remove beats during breaks
.Where(x => !beatmap.Breaks.Any(b =>
Precision.AlmostBigger(x, b.StartTime)
&& Precision.AlmostBigger(origHitObjects.First(y => Precision.AlmostBigger(y.StartTime, b.EndTime)).StartTime, x)
))
.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 11:06:56 +08:00
{
var beat = beats[i];
2021-06-14 21:34:34 +08:00
if (Precision.AlmostBigger(beatmap.ControlPointInfo.TimingPointAt(beat).BeatLength / 2, beats[i + 1] - beat)) beats.RemoveAt(i);
2021-06-15 11:06:56 +08:00
}
2021-05-19 22:25:56 +08:00
2021-06-15 11:06:56 +08:00
return beats;
}
private void addHitSamples(IEnumerable<OsuHitObject> hitObjects, IReadOnlyList<OsuHitObject> origHitObjects)
2021-06-15 11:06:56 +08:00
{
var lastSampleIdx = 0;
foreach (var x in hitObjects)
2021-05-20 11:57:13 +08:00
{
var samples = getSamplesAtTime(origHitObjects, x.StartTime);
2021-06-14 21:34:34 +08:00
2021-05-20 11:57:13 +08:00
if (samples == null)
{
while (lastSampleIdx < origHitObjects.Count && origHitObjects[lastSampleIdx].StartTime <= x.StartTime)
lastSampleIdx++;
lastSampleIdx--;
if (lastSampleIdx < 0 && lastSampleIdx >= origHitObjects.Count) continue;
if (lastSampleIdx < origHitObjects.Count - 1)
{
// get samples from the next hit object if it is closer in time
if (origHitObjects[lastSampleIdx + 1].StartTime - x.StartTime < x.StartTime - origHitObjects[lastSampleIdx].StartTime)
lastSampleIdx++;
}
x.Samples = origHitObjects[lastSampleIdx].Samples.Where(s => !HitSampleInfo.AllAdditions.Contains(s.Name)).ToList();
2021-05-20 11:57:13 +08:00
}
else
x.Samples = samples;
}
2021-06-15 11:06:56 +08:00
}
2021-05-19 22:25:56 +08:00
2021-06-15 11:06:56 +08:00
private void fixComboInfo(List<OsuHitObject> hitObjects, List<OsuHitObject> origHitObjects)
{
2021-05-20 11:57:13 +08:00
// First follow the combo indices in the original beatmap
hitObjects.ForEach(x =>
{
var origObj = origHitObjects.FindLast(y => Precision.AlmostBigger(x.StartTime, y.StartTime));
2021-06-14 21:34:34 +08:00
x.ComboIndex = origObj?.ComboIndex ?? 0;
2021-05-20 11:57:13 +08:00
});
// Then reprocess them to ensure continuity in the combo indices and add indices in current combo
var combos = hitObjects.GroupBy(x => x.ComboIndex).ToList();
2021-06-14 21:34:34 +08:00
for (var i = 0; i < combos.Count; i++)
2021-05-19 22:25:56 +08:00
{
2021-05-20 11:57:13 +08:00
var group = combos[i].ToList();
group.First().NewCombo = true;
group.Last().LastInCombo = true;
2021-05-19 22:25:56 +08:00
for (var j = 0; j < group.Count; j++)
2021-05-19 22:25:56 +08:00
{
2021-05-20 11:57:13 +08:00
var x = group[j];
x.ComboIndex = i;
x.IndexInCurrentCombo = j;
}
}
2021-06-15 11:06:56 +08:00
}
private void randomizeCirclePos(IReadOnlyList<OsuHitObject> hitObjects)
{
var rng = new Random(Seed.Value.GetValueOrDefault());
float nextSingle(float max = 1f) => (float)(rng.NextDouble() * max);
2021-05-19 22:25:56 +08:00
var direction = MathHelper.TwoPi * nextSingle();
var maxComboIndex = hitObjects.Last().ComboIndex;
2021-06-14 21:34:34 +08:00
for (var i = 0; i < hitObjects.Count; i++)
2021-05-20 11:57:13 +08:00
{
var obj = hitObjects[i];
var lastPos = i == 0
? Vector2.Divide(OsuPlayfield.BASE_SIZE, 2)
: hitObjects[i - 1].Position;
var distance = map(obj.ComboIndex, 0, maxComboIndex, (float)obj.Radius, 333f);
if (obj.NewCombo) distance *= 1.5f;
if (obj.Kiai) distance *= 1.2f;
distance = Math.Min(max_distance, distance);
var relativePos = new Vector2(
distance * (float)Math.Cos(direction),
distance * (float)Math.Sin(direction)
);
relativePos = getRotatedVector(lastPos, relativePos);
direction = (float)Math.Atan2(relativePos.Y, relativePos.X);
2021-06-14 21:34:34 +08:00
var newPosition = Vector2.Add(lastPos, relativePos);
if (newPosition.Y < 0)
newPosition.Y = 0;
else if (newPosition.Y > OsuPlayfield.BASE_SIZE.Y)
newPosition.Y = OsuPlayfield.BASE_SIZE.Y;
if (newPosition.X < 0)
newPosition.X = 0;
else if (newPosition.X > OsuPlayfield.BASE_SIZE.X)
newPosition.X = OsuPlayfield.BASE_SIZE.X;
obj.Position = newPosition;
if (obj.LastInCombo)
direction = MathHelper.TwoPi * nextSingle();
2021-05-20 11:57:13 +08:00
else
direction += distance / max_distance * (nextSingle() * MathHelper.TwoPi - MathHelper.Pi);
2021-05-19 22:25:56 +08:00
}
}
/// <summary>
/// Get samples (if any) for a specific point in time.
2021-05-19 22:25:56 +08:00
/// </summary>
/// <remarks>
/// Samples will be returned if a hit circle or a slider node exists at that point of time.
2021-05-19 22:25:56 +08: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 21:34:34 +08:00
private IList<HitSampleInfo> getSamplesAtTime(IEnumerable<OsuHitObject> hitObjects, double time)
2021-05-19 22:25:56 +08:00
{
var sampleObj = hitObjects.FirstOrDefault(x =>
{
if (Precision.AlmostEquals(time, x.StartTime)) return true;
2021-06-14 21:34:34 +08:00
if (!(x is Slider s))
return false;
if (!Precision.AlmostBigger(time, s.StartTime)
|| !Precision.AlmostBigger(s.EndTime, time))
return false;
return Precision.AlmostEquals((time - s.StartTime) % s.SpanDuration, 0);
2021-05-19 22:25:56 +08:00
});
if (sampleObj == null) return null;
2021-06-14 21:34:34 +08:00
IList<HitSampleInfo> samples;
2021-05-19 22:25:56 +08:00
if (sampleObj is Slider slider)
samples = slider.NodeSamples[(int)Math.Round((time - slider.StartTime) % slider.SpanDuration)];
else
samples = sampleObj.Samples;
2021-06-14 21:34:34 +08:00
2021-05-19 22:25:56 +08:00
return samples;
}
/// <summary>
/// Determines the position of the current hit object relative to the previous one.
2021-05-19 22:25:56 +08:00
/// </summary>
/// <returns>The position of the current hit object relative to the previous one</returns>
private Vector2 getRotatedVector(Vector2 prevPosChanged, Vector2 posRelativeToPrev)
{
var relativeRotationDistance = 0f;
var playfieldMiddle = Vector2.Divide(OsuPlayfield.BASE_SIZE, 2);
if (prevPosChanged.X < playfieldMiddle.X)
{
relativeRotationDistance = Math.Max(
(border_distance_x - prevPosChanged.X) / border_distance_x,
relativeRotationDistance
);
}
else
{
relativeRotationDistance = Math.Max(
(prevPosChanged.X - (OsuPlayfield.BASE_SIZE.X - border_distance_x)) / border_distance_x,
relativeRotationDistance
);
}
if (prevPosChanged.Y < playfieldMiddle.Y)
{
relativeRotationDistance = Math.Max(
(border_distance_y - prevPosChanged.Y) / border_distance_y,
relativeRotationDistance
);
}
else
{
relativeRotationDistance = Math.Max(
(prevPosChanged.Y - (OsuPlayfield.BASE_SIZE.Y - border_distance_y)) / border_distance_y,
relativeRotationDistance
);
}
return rotateVectorTowardsVector(
posRelativeToPrev,
Vector2.Subtract(playfieldMiddle, prevPosChanged),
Math.Min(1, relativeRotationDistance * 0.75f)
2021-05-19 22:25:56 +08:00
);
}
/// <summary>
/// Rotates vector "initial" towards vector "destination"
2021-05-19 22:25:56 +08:00
/// </summary>
/// <param name="initial">Vector to rotate to "destination"</param>
/// <param name="destination">Vector "initial" should be rotated to</param>
/// <param name="relativeDistance">
/// The angle the vector should be rotated relative to the difference between the angles of
/// the the two vectors.
/// </param>
2021-05-19 22:25:56 +08:00
/// <returns>Resulting vector</returns>
private Vector2 rotateVectorTowardsVector(Vector2 initial, Vector2 destination, float relativeDistance)
{
var initialAngleRad = Math.Atan2(initial.Y, initial.X);
var destAngleRad = Math.Atan2(destination.Y, destination.X);
var diff = destAngleRad - initialAngleRad;
while (diff < -Math.PI) diff += 2 * Math.PI;
2021-05-19 22:25:56 +08:00
while (diff > Math.PI) diff -= 2 * Math.PI;
2021-05-19 22:25:56 +08:00
var finalAngleRad = initialAngleRad + relativeDistance * diff;
return new Vector2(
initial.Length * (float)Math.Cos(finalAngleRad),
initial.Length * (float)Math.Sin(finalAngleRad)
);
}
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("spinnerbonus")) // todo: use another sample
};
}
2021-05-19 22:25:56 +08:00
}
2018-04-13 17:19:50 +08:00
}
}