osu/osu.Game.Rulesets.Catch/Beatmaps/CatchBeatmapProcessor.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

256 lines
10 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
using System;
using System.Linq;
2017-09-18 03:48:33 +00:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.UI;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Utils;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Catch.Beatmaps
{
public class CatchBeatmapProcessor : BeatmapProcessor
{
public const int RNG_SEED = 1337;
public bool HardRockOffsets { get; set; }
public CatchBeatmapProcessor(IBeatmap beatmap)
: base(beatmap)
{
}
public override void PreProcess()
{
IHasComboInformation? lastObj = null;
// For sanity, ensures that both the first hitobject and the first hitobject after a banana shower start a new combo.
// This is normally enforced by the legacy decoder, but is not enforced by the editor.
foreach (var obj in Beatmap.HitObjects.OfType<IHasComboInformation>())
{
if (obj is not BananaShower && (lastObj == null || lastObj is BananaShower))
obj.NewCombo = true;
lastObj = obj;
}
base.PreProcess();
}
public override void PostProcess()
{
base.PostProcess();
2018-04-13 09:19:50 +00:00
2019-07-31 09:44:01 +00:00
ApplyPositionOffsets(Beatmap);
int index = 0;
2019-04-01 03:16:05 +00:00
foreach (var obj in Beatmap.HitObjects.OfType<CatchHitObject>())
{
obj.IndexInBeatmap = index;
foreach (var nested in obj.NestedHitObjects.OfType<CatchHitObject>())
nested.IndexInBeatmap = index;
if (obj.LastInCombo && obj.NestedHitObjects.LastOrDefault() is IHasComboInformation lastNested)
lastNested.LastInCombo = true;
index++;
}
}
2018-04-13 09:19:50 +00:00
public void ApplyPositionOffsets(IBeatmap beatmap)
{
var rng = new LegacyRandom(RNG_SEED);
2019-07-31 09:44:01 +00:00
float? lastPosition = null;
double lastStartTime = 0;
foreach (var obj in beatmap.HitObjects.OfType<CatchHitObject>())
{
obj.XOffset = 0;
switch (obj)
{
2019-07-31 09:44:01 +00:00
case Fruit fruit:
if (HardRockOffsets)
2019-07-31 09:44:01 +00:00
applyHardRockOffset(fruit, ref lastPosition, ref lastStartTime, rng);
break;
case BananaShower bananaShower:
2018-06-29 06:01:33 +00:00
foreach (var banana in bananaShower.NestedHitObjects.OfType<Banana>())
{
banana.XOffset = (float)(rng.NextDouble() * CatchPlayfield.WIDTH);
rng.Next(); // osu!stable retrieved a random banana type
rng.Next(); // osu!stable retrieved a random banana rotation
2018-06-13 12:10:54 +00:00
rng.Next(); // osu!stable retrieved a random banana colour
}
2019-02-28 04:31:40 +00:00
break;
2019-04-01 03:16:05 +00:00
case JuiceStream juiceStream:
// Todo: BUG!! Stable used the last control point as the final position of the path, but it should use the computed path instead.
lastPosition = juiceStream.OriginalX + juiceStream.Path.ControlPoints[^1].Position.X;
// Todo: BUG!! Stable attempted to use the end time of the stream, but referenced it too early in execution and used the start time instead.
lastStartTime = juiceStream.StartTime;
foreach (var nested in juiceStream.NestedHitObjects)
{
2019-08-01 05:57:17 +00:00
var catchObject = (CatchHitObject)nested;
catchObject.XOffset = 0;
if (catchObject is TinyDroplet)
catchObject.XOffset = Math.Clamp(rng.Next(-20, 20), -catchObject.OriginalX, CatchPlayfield.WIDTH - catchObject.OriginalX);
2019-08-01 05:57:17 +00:00
else if (catchObject is Droplet)
2018-06-13 10:52:04 +00:00
rng.Next(); // osu!stable retrieved a random droplet rotation
}
2019-02-28 04:31:40 +00:00
break;
}
}
initialiseHyperDash(beatmap);
}
private static void applyHardRockOffset(CatchHitObject hitObject, ref float? lastPosition, ref double lastStartTime, LegacyRandom rng)
2019-07-31 09:44:01 +00:00
{
float offsetPosition = hitObject.OriginalX;
2019-07-31 09:44:01 +00:00
double startTime = hitObject.StartTime;
if (lastPosition == null)
{
lastPosition = offsetPosition;
2019-07-31 09:44:01 +00:00
lastStartTime = startTime;
return;
}
float positionDiff = offsetPosition - lastPosition.Value;
// Todo: BUG!! Stable calculated time deltas as ints, which affects randomisation. This should be changed to a double.
int timeDiff = (int)(startTime - lastStartTime);
2019-07-31 09:44:01 +00:00
if (timeDiff > 1000)
{
lastPosition = offsetPosition;
2019-07-31 09:44:01 +00:00
lastStartTime = startTime;
return;
}
if (positionDiff == 0)
{
applyRandomOffset(ref offsetPosition, timeDiff / 4d, rng);
hitObject.XOffset = offsetPosition - hitObject.OriginalX;
2019-07-31 09:44:01 +00:00
return;
}
// ReSharper disable once PossibleLossOfFraction
if (Math.Abs(positionDiff) < timeDiff / 3)
applyOffset(ref offsetPosition, positionDiff);
2019-07-31 09:44:01 +00:00
hitObject.XOffset = offsetPosition - hitObject.OriginalX;
2019-07-31 09:44:01 +00:00
lastPosition = offsetPosition;
2019-07-31 09:44:01 +00:00
lastStartTime = startTime;
}
/// <summary>
/// Applies a random offset in a random direction to a position, ensuring that the final position remains within the boundary of the playfield.
/// </summary>
/// <param name="position">The position which the offset should be applied to.</param>
/// <param name="maxOffset">The maximum offset, cannot exceed 20px.</param>
/// <param name="rng">The random number generator.</param>
private static void applyRandomOffset(ref float position, double maxOffset, LegacyRandom rng)
2019-07-31 09:44:01 +00:00
{
bool right = rng.NextBool();
float rand = Math.Min(20, (float)rng.Next(0, Math.Max(0, maxOffset)));
2019-07-31 09:44:01 +00:00
if (right)
{
// Clamp to the right bound
if (position + rand <= CatchPlayfield.WIDTH)
2019-07-31 09:44:01 +00:00
position += rand;
else
position -= rand;
}
else
{
// Clamp to the left bound
if (position - rand >= 0)
position -= rand;
else
position += rand;
}
}
/// <summary>
/// Applies an offset to a position, ensuring that the final position remains within the boundary of the playfield.
/// </summary>
/// <param name="position">The position which the offset should be applied to.</param>
/// <param name="amount">The amount to offset by.</param>
private static void applyOffset(ref float position, float amount)
{
if (amount > 0)
{
// Clamp to the right bound
if (position + amount < CatchPlayfield.WIDTH)
2019-07-31 09:44:01 +00:00
position += amount;
}
else
{
// Clamp to the left bound
if (position + amount > 0)
position += amount;
}
}
private static void initialiseHyperDash(IBeatmap beatmap)
2017-11-28 12:22:57 +00:00
{
var palpableObjects = CatchBeatmap.GetPalpableObjects(beatmap.HitObjects)
.Where(h => h is Fruit || (h is Droplet && h is not TinyDroplet))
.ToArray();
double halfCatcherWidth = Catcher.CalculateCatchWidth(beatmap.Difficulty) / 2;
// Todo: This is wrong. osu!stable calculated hyperdashes using the full catcher size, excluding the margins.
// This should theoretically cause impossible scenarios, but practically, likely due to the size of the playfield, it doesn't seem possible.
// For now, to bring gameplay (and diffcalc!) completely in-line with stable, this code also uses the full catcher size.
halfCatcherWidth /= Catcher.ALLOWED_CATCH_RANGE;
int lastDirection = 0;
double lastExcess = halfCatcherWidth;
2018-04-13 09:19:50 +00:00
for (int i = 0; i < palpableObjects.Length - 1; i++)
{
var currentObject = palpableObjects[i];
var nextObject = palpableObjects[i + 1];
2018-04-13 09:19:50 +00:00
// Reset variables in-case values have changed (e.g. after applying HR)
currentObject.HyperDashTarget = null;
currentObject.DistanceToHyperDash = 0;
int thisDirection = nextObject.EffectiveX > currentObject.EffectiveX ? 1 : -1;
// Int truncation added to match osu!stable.
double timeToNext = (int)nextObject.StartTime - (int)currentObject.StartTime - 1000f / 60f / 4; // 1/4th of a frame of grace time, taken from osu-stable
double distanceToNext = Math.Abs(nextObject.EffectiveX - currentObject.EffectiveX) - (lastDirection == thisDirection ? lastExcess : halfCatcherWidth);
float distanceToHyper = (float)(timeToNext * Catcher.BASE_DASH_SPEED - distanceToNext);
2019-04-01 03:16:05 +00:00
if (distanceToHyper < 0)
{
currentObject.HyperDashTarget = nextObject;
lastExcess = halfCatcherWidth;
}
else
{
currentObject.DistanceToHyperDash = distanceToHyper;
lastExcess = Math.Clamp(distanceToHyper, 0, halfCatcherWidth);
}
2018-04-13 09:19:50 +00:00
lastDirection = thisDirection;
}
2017-11-28 12:22:57 +00:00
}
}
}