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.
// 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.Drawables;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.UI;
@ -37,12 +37,9 @@ namespace osu.Game.Rulesets.Osu.UI
DrawableHitObject blockingObject = null;
// Find the last hitobject which blocks future hits.
foreach (var obj in hitObjectContainer.AliveObjects)
foreach (var obj in enumerateHitObjectsUpTo(hitObject))
{
if (obj == hitObject)
break;
if (drawableCanBlockFutureHits(obj))
if (hitObjectCanBlockFutureHits(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.
/// </summary>
/// <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).
if (!hitObjectCanBlockFutureHits(hitObject))
return;
double maximumTime = hitObject.StartTime;
// Iterate through and apply miss results to all top-level and nested hitobjects which block future hits.
foreach (var obj in hitObjectContainer.AliveObjects)
foreach (var obj in enumerateHitObjectsUpTo(hitObject))
{
if (obj.Judged || obj.HitObject.StartTime >= maximumTime)
if (obj.Judged)
continue;
if (hitObjectCanBlockFutureHits(obj.HitObject))
applyMiss(obj);
foreach (var nested in obj.NestedHitObjects)
{
if (nested.Judged || nested.HitObject.StartTime >= maximumTime)
continue;
if (hitObjectCanBlockFutureHits(nested.HitObject))
applyMiss(nested);
}
if (hitObjectCanBlockFutureHits(obj))
((DrawableOsuHitObject)obj).MissForcefully();
}
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>
/// Whether a <see cref="HitObject"/> blocks hits on future <see cref="HitObject"/>s until its start time is reached.
/// </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>
private static bool hitObjectCanBlockFutureHits(HitObject hitObject)
{
// Unlike the above we will receive slider tails, but they do not block future hits.
if (hitObject is SliderTailCircle)
return false;
private static bool hitObjectCanBlockFutureHits(DrawableHitObject hitObject)
=> hitObject is DrawableHitCircle;
// All other hitcircles continue to block future hits.
return hitObject is HitCircle;
// Todo: Inefficient
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)
{
// 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)
return;