1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 08:43:20 +08:00

Initial inefficient refactor of hitobject enumeration

This commit is contained in:
smoogipoo 2020-04-17 13:34:20 +09:00
parent 2af2a49f22
commit 3daacbc2d2
2 changed files with 29 additions and 49 deletions

View File

@ -1,9 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables; using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.UI; using osu.Game.Rulesets.UI;
@ -37,12 +37,9 @@ namespace osu.Game.Rulesets.Osu.UI
DrawableHitObject blockingObject = null; DrawableHitObject blockingObject = null;
// Find the last hitobject which blocks future hits. // Find the last hitobject which blocks future hits.
foreach (var obj in hitObjectContainer.AliveObjects) foreach (var obj in enumerateHitObjectsUpTo(hitObject))
{ {
if (obj == hitObject) if (hitObjectCanBlockFutureHits(obj))
break;
if (drawableCanBlockFutureHits(obj))
blockingObject = obj; blockingObject = obj;
} }
@ -64,64 +61,47 @@ namespace osu.Game.Rulesets.Osu.UI
/// Handles a <see cref="HitObject"/> being hit to potentially miss all earlier <see cref="HitObject"/>s. /// Handles a <see cref="HitObject"/> being hit to potentially miss all earlier <see cref="HitObject"/>s.
/// </summary> /// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> that was hit.</param> /// <param name="hitObject">The <see cref="HitObject"/> that was hit.</param>
public void HandleHit(HitObject hitObject) public void HandleHit(DrawableHitObject hitObject)
{ {
// Hitobjects which themselves don't block future hitobjects don't cause misses (e.g. slider ticks, spinners). // Hitobjects which themselves don't block future hitobjects don't cause misses (e.g. slider ticks, spinners).
if (!hitObjectCanBlockFutureHits(hitObject)) if (!hitObjectCanBlockFutureHits(hitObject))
return; return;
double maximumTime = hitObject.StartTime; foreach (var obj in enumerateHitObjectsUpTo(hitObject))
// Iterate through and apply miss results to all top-level and nested hitobjects which block future hits.
foreach (var obj in hitObjectContainer.AliveObjects)
{ {
if (obj.Judged || obj.HitObject.StartTime >= maximumTime) if (obj.Judged)
continue; continue;
if (hitObjectCanBlockFutureHits(obj.HitObject)) if (hitObjectCanBlockFutureHits(obj))
applyMiss(obj); ((DrawableOsuHitObject)obj).MissForcefully();
foreach (var nested in obj.NestedHitObjects)
{
if (nested.Judged || nested.HitObject.StartTime >= maximumTime)
continue;
if (hitObjectCanBlockFutureHits(nested.HitObject))
applyMiss(nested);
}
} }
static void applyMiss(DrawableHitObject obj) => ((DrawableOsuHitObject)obj).MissForcefully();
}
/// <summary>
/// Whether a <see cref="DrawableHitObject"/> blocks hits on future <see cref="DrawableHitObject"/>s until its start time is reached.
/// </summary>
/// <remarks>
/// This will ONLY match on top-most <see cref="DrawableHitObject"/>s.
/// </remarks>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to test.</param>
private static bool drawableCanBlockFutureHits(DrawableHitObject hitObject)
{
// Special considerations for slider tails aren't required since only top-most drawable hitobjects are being iterated over.
return hitObject is DrawableHitCircle || hitObject is DrawableSlider;
} }
/// <summary> /// <summary>
/// Whether a <see cref="HitObject"/> blocks hits on future <see cref="HitObject"/>s until its start time is reached. /// Whether a <see cref="HitObject"/> blocks hits on future <see cref="HitObject"/>s until its start time is reached.
/// </summary> /// </summary>
/// <remarks>
/// This is more rigorous and may not match on top-most <see cref="HitObject"/>s as <see cref="drawableCanBlockFutureHits"/> does.
/// </remarks>
/// <param name="hitObject">The <see cref="HitObject"/> to test.</param> /// <param name="hitObject">The <see cref="HitObject"/> to test.</param>
private static bool hitObjectCanBlockFutureHits(HitObject hitObject) private static bool hitObjectCanBlockFutureHits(DrawableHitObject hitObject)
{ => hitObject is DrawableHitCircle;
// Unlike the above we will receive slider tails, but they do not block future hits.
if (hitObject is SliderTailCircle)
return false;
// All other hitcircles continue to block future hits. // Todo: Inefficient
return hitObject is HitCircle; private IEnumerable<DrawableHitObject> enumerateHitObjectsUpTo(DrawableHitObject hitObject)
{
return enumerate(hitObjectContainer.AliveObjects);
IEnumerable<DrawableHitObject> enumerate(IEnumerable<DrawableHitObject> list)
{
foreach (var obj in list)
{
if (obj.HitObject.StartTime >= hitObject.HitObject.StartTime)
yield break;
yield return obj;
foreach (var nested in enumerate(obj.NestedHitObjects))
yield return nested;
}
}
} }
} }
} }

View File

@ -86,7 +86,7 @@ namespace osu.Game.Rulesets.Osu.UI
private void onNewResult(DrawableHitObject judgedObject, JudgementResult result) private void onNewResult(DrawableHitObject judgedObject, JudgementResult result)
{ {
// Hitobjects that block future hits should miss previous hitobjects if they're hit out-of-order. // Hitobjects that block future hits should miss previous hitobjects if they're hit out-of-order.
hitPolicy.HandleHit(result.HitObject); hitPolicy.HandleHit(judgedObject);
if (!judgedObject.DisplayResult || !DisplayJudgements.Value) if (!judgedObject.DisplayResult || !DisplayJudgements.Value)
return; return;