2019-12-25 20:16:40 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2019-12-25 20:16:40 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-04-06 02:32:55 +08:00
|
|
|
using System.Linq;
|
2019-12-25 20:16:40 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets.Judgements;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2020-05-11 00:32:38 +08:00
|
|
|
using osu.Game.Utils;
|
2019-12-25 20:16:40 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Scoring
|
|
|
|
{
|
2019-12-25 20:22:46 +08:00
|
|
|
/// <summary>
|
2019-12-27 11:46:35 +08:00
|
|
|
/// A <see cref="HealthProcessor"/> which continuously drains health.<br />
|
|
|
|
/// At HP=0, the minimum health reached for a perfect play is 95%.<br />
|
|
|
|
/// At HP=5, the minimum health reached for a perfect play is 70%.<br />
|
|
|
|
/// At HP=10, the minimum health reached for a perfect play is 30%.
|
2019-12-25 20:22:46 +08:00
|
|
|
/// </summary>
|
2019-12-25 20:16:40 +08:00
|
|
|
public partial class DrainingHealthProcessor : HealthProcessor
|
|
|
|
{
|
2019-12-26 16:36:40 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A reasonable allowable error for the minimum health offset from <see cref="targetMinimumHealth"/>. A 1% error is unnoticeable.
|
|
|
|
/// </summary>
|
|
|
|
private const double minimum_health_error = 0.01;
|
2019-12-25 20:16:40 +08:00
|
|
|
|
2019-12-27 11:46:35 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The minimum health target at an HP drain rate of 0.
|
|
|
|
/// </summary>
|
2022-05-17 13:59:18 +08:00
|
|
|
private const double min_health_target = 0.99;
|
2019-12-27 11:46:35 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The minimum health target at an HP drain rate of 5.
|
|
|
|
/// </summary>
|
2022-05-17 13:59:18 +08:00
|
|
|
private const double mid_health_target = 0.9;
|
2019-12-27 11:46:35 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The minimum health target at an HP drain rate of 10.
|
|
|
|
/// </summary>
|
2022-05-17 13:59:18 +08:00
|
|
|
private const double max_health_target = 0.4;
|
2019-12-27 11:46:35 +08:00
|
|
|
|
2023-11-13 12:46:47 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The drain rate as a proportion of the total health drained per millisecond.
|
|
|
|
/// </summary>
|
2023-11-21 18:18:02 +08:00
|
|
|
public double DrainRate { get; private set; }
|
2023-11-13 12:46:47 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The beatmap.
|
|
|
|
/// </summary>
|
|
|
|
protected IBeatmap Beatmap { get; private set; }
|
2019-12-26 14:46:07 +08:00
|
|
|
|
2023-11-13 12:46:47 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The time at which health starts draining.
|
|
|
|
/// </summary>
|
|
|
|
protected readonly double DrainStartTime;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An amount of lenience to apply to the drain rate.
|
|
|
|
/// </summary>
|
|
|
|
protected readonly double DrainLenience;
|
2019-12-27 15:14:49 +08:00
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
private readonly List<HealthIncrease> healthIncreases = new List<HealthIncrease>();
|
|
|
|
private readonly Dictionary<int, double> densityMultiplierByGroup = new Dictionary<int, double>();
|
|
|
|
|
2023-11-13 12:46:47 +08:00
|
|
|
private double gameplayEndTime;
|
2019-12-25 20:16:40 +08:00
|
|
|
private double targetMinimumHealth;
|
|
|
|
|
2020-05-11 00:32:38 +08:00
|
|
|
private PeriodTracker noDrainPeriodTracker;
|
2020-04-06 02:32:55 +08:00
|
|
|
|
2019-12-25 20:22:46 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="DrainingHealthProcessor"/>.
|
|
|
|
/// </summary>
|
2019-12-27 15:14:49 +08:00
|
|
|
/// <param name="drainStartTime">The time after which draining should begin.</param>
|
2020-06-21 17:05:26 +08:00
|
|
|
/// <param name="drainLenience">A lenience to apply to the default drain rate.<br />
|
|
|
|
/// A value of 0 uses the default drain rate.<br />
|
|
|
|
/// A value of 0.5 halves the drain rate.<br />
|
|
|
|
/// A value of 1 completely removes drain.</param>
|
|
|
|
public DrainingHealthProcessor(double drainStartTime, double drainLenience = 0)
|
2019-12-25 20:16:40 +08:00
|
|
|
{
|
2023-11-13 12:46:47 +08:00
|
|
|
DrainStartTime = drainStartTime;
|
|
|
|
DrainLenience = Math.Clamp(drainLenience, 0, 1);
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2020-05-11 00:32:38 +08:00
|
|
|
if (noDrainPeriodTracker?.IsInAny(Time.Current) == true)
|
2020-04-06 02:32:55 +08:00
|
|
|
return;
|
2019-12-26 14:46:07 +08:00
|
|
|
|
2020-04-06 02:32:55 +08:00
|
|
|
// When jumping in and out of gameplay time within a single frame, health should only be drained for the period within the gameplay time
|
2023-11-13 12:46:47 +08:00
|
|
|
double lastGameplayTime = Math.Clamp(Time.Current - Time.Elapsed, DrainStartTime, gameplayEndTime);
|
|
|
|
double currentGameplayTime = Math.Clamp(Time.Current, DrainStartTime, gameplayEndTime);
|
2020-04-06 02:32:55 +08:00
|
|
|
|
2023-11-13 12:46:47 +08:00
|
|
|
if (DrainLenience < 1)
|
|
|
|
Health.Value -= DrainRate * (currentGameplayTime - lastGameplayTime);
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void ApplyBeatmap(IBeatmap beatmap)
|
|
|
|
{
|
2023-11-13 12:46:47 +08:00
|
|
|
Beatmap = beatmap;
|
2019-12-25 20:16:40 +08:00
|
|
|
|
2019-12-26 14:46:07 +08:00
|
|
|
if (beatmap.HitObjects.Count > 0)
|
|
|
|
gameplayEndTime = beatmap.HitObjects[^1].GetEndTime();
|
|
|
|
|
2023-11-14 00:11:17 +08:00
|
|
|
noDrainPeriodTracker = new PeriodTracker(
|
|
|
|
beatmap.Breaks.Select(breakPeriod =>
|
|
|
|
new Period(
|
|
|
|
beatmap.HitObjects
|
|
|
|
.Select(hitObject => hitObject.GetEndTime())
|
|
|
|
.Where(endTime => endTime <= breakPeriod.StartTime)
|
|
|
|
.DefaultIfEmpty(double.MinValue)
|
|
|
|
.Last(),
|
|
|
|
beatmap.HitObjects
|
|
|
|
.Select(hitObject => hitObject.StartTime)
|
|
|
|
.Where(startTime => startTime >= breakPeriod.EndTime)
|
|
|
|
.DefaultIfEmpty(double.MaxValue)
|
|
|
|
.First()
|
|
|
|
)));
|
2020-04-06 02:32:55 +08:00
|
|
|
|
2021-10-02 11:34:29 +08:00
|
|
|
targetMinimumHealth = IBeatmapDifficultyInfo.DifficultyRange(beatmap.Difficulty.DrainRate, min_health_target, mid_health_target, max_health_target);
|
2020-06-21 18:31:00 +08:00
|
|
|
|
|
|
|
// Add back a portion of the amount of HP to be drained, depending on the lenience requested.
|
2023-11-13 12:46:47 +08:00
|
|
|
targetMinimumHealth += DrainLenience * (1 - targetMinimumHealth);
|
2020-06-21 18:31:00 +08:00
|
|
|
|
|
|
|
// Ensure the target HP is within an acceptable range.
|
|
|
|
targetMinimumHealth = Math.Clamp(targetMinimumHealth, 0, 1);
|
2019-12-25 20:16:40 +08:00
|
|
|
|
|
|
|
base.ApplyBeatmap(beatmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ApplyResultInternal(JudgementResult result)
|
|
|
|
{
|
|
|
|
base.ApplyResultInternal(result);
|
2020-07-11 19:17:40 +08:00
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
if (IsSimulating && !result.Type.IsBonus())
|
|
|
|
{
|
|
|
|
healthIncreases.Add(new HealthIncrease(
|
|
|
|
result.HitObject.GetEndTime() + result.TimeOffset,
|
|
|
|
GetHealthIncreaseFor(result),
|
|
|
|
GetDensityGroup(result.HitObject)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override double GetHealthIncreaseFor(JudgementResult result) => base.GetHealthIncreaseFor(result) * getDensityMultiplier(GetDensityGroup(result.HitObject));
|
|
|
|
|
|
|
|
private double getDensityMultiplier(int? group)
|
|
|
|
{
|
|
|
|
if (group == null)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return densityMultiplierByGroup.TryGetValue(group.Value, out double multiplier) ? multiplier : 1;
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
protected virtual int? GetDensityGroup(HitObject hitObject) => null;
|
|
|
|
|
2019-12-25 20:16:40 +08:00
|
|
|
protected override void Reset(bool storeResults)
|
|
|
|
{
|
|
|
|
base.Reset(storeResults);
|
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
densityMultiplierByGroup.Clear();
|
|
|
|
|
2019-12-25 20:16:40 +08:00
|
|
|
if (storeResults)
|
2023-11-13 12:46:47 +08:00
|
|
|
DrainRate = ComputeDrainRate();
|
2019-12-27 11:39:25 +08:00
|
|
|
|
|
|
|
healthIncreases.Clear();
|
2019-12-26 16:36:40 +08:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:46:47 +08:00
|
|
|
protected virtual double ComputeDrainRate()
|
2019-12-26 16:36:40 +08:00
|
|
|
{
|
2020-09-25 22:29:40 +08:00
|
|
|
if (healthIncreases.Count <= 1)
|
2019-12-26 16:36:40 +08:00
|
|
|
return 0;
|
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
// Normalise the health gain during sections with higher densities.
|
|
|
|
(int group, double avgIncrease)[] avgIncreasesByGroup = healthIncreases
|
|
|
|
.Where(i => i.Group != null)
|
|
|
|
.GroupBy(i => i.Group)
|
|
|
|
.Select(g => ((int)g.Key!, g.Sum(i => i.Amount) / (g.Max(i => i.Time) - g.Min(i => i.Time) + 1)))
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
if (avgIncreasesByGroup.Length > 1)
|
|
|
|
{
|
|
|
|
double overallAverageIncrease = avgIncreasesByGroup.Average(g => g.avgIncrease);
|
|
|
|
|
|
|
|
foreach ((int group, double avgIncrease) in avgIncreasesByGroup)
|
|
|
|
{
|
|
|
|
// Reduce the health increase for groups that return more health than average.
|
|
|
|
densityMultiplierByGroup[group] = Math.Min(1, overallAverageIncrease / avgIncrease);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 16:36:40 +08:00
|
|
|
int adjustment = 1;
|
|
|
|
double result = 1;
|
|
|
|
|
|
|
|
// Although we expect the following loop to converge within 30 iterations (health within 1/2^31 accuracy of the target),
|
|
|
|
// we'll still keep a safety measure to avoid infinite loops by detecting overflows.
|
|
|
|
while (adjustment > 0)
|
2019-12-25 20:16:40 +08:00
|
|
|
{
|
2019-12-26 16:36:40 +08:00
|
|
|
double currentHealth = 1;
|
|
|
|
double lowestHealth = 1;
|
2023-11-14 00:11:17 +08:00
|
|
|
int currentBreak = 0;
|
2019-12-25 20:16:40 +08:00
|
|
|
|
2019-12-26 16:36:40 +08:00
|
|
|
for (int i = 0; i < healthIncreases.Count; i++)
|
2019-12-25 20:16:40 +08:00
|
|
|
{
|
2023-12-22 16:55:06 +08:00
|
|
|
double currentTime = healthIncreases[i].Time;
|
|
|
|
double lastTime = i > 0 ? healthIncreases[i - 1].Time : DrainStartTime;
|
2019-12-25 20:16:40 +08:00
|
|
|
|
2023-11-14 00:11:17 +08:00
|
|
|
while (currentBreak < Beatmap.Breaks.Count && Beatmap.Breaks[currentBreak].EndTime <= currentTime)
|
2019-12-25 20:16:40 +08:00
|
|
|
{
|
2023-11-14 00:11:17 +08:00
|
|
|
// If two hitobjects are separated by a break period, there is no drain for the full duration between the hitobjects.
|
|
|
|
// This differs from legacy (version < 8) beatmaps which continue draining until the break section is entered,
|
|
|
|
// but this shouldn't have a noticeable impact in practice.
|
|
|
|
lastTime = currentTime;
|
|
|
|
currentBreak++;
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:55:06 +08:00
|
|
|
double multiplier = getDensityMultiplier(healthIncreases[i].Group);
|
|
|
|
|
2019-12-26 16:36:40 +08:00
|
|
|
// Apply health adjustments
|
2023-11-14 00:11:17 +08:00
|
|
|
currentHealth -= (currentTime - lastTime) * result;
|
2019-12-26 16:36:40 +08:00
|
|
|
lowestHealth = Math.Min(lowestHealth, currentHealth);
|
2023-12-22 16:55:06 +08:00
|
|
|
currentHealth = Math.Min(1, currentHealth + healthIncreases[i].Amount * multiplier);
|
2019-12-25 20:16:40 +08:00
|
|
|
|
2019-12-26 16:36:40 +08:00
|
|
|
// Common scenario for when the drain rate is definitely too harsh
|
|
|
|
if (lowestHealth < 0)
|
|
|
|
break;
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
2019-12-26 16:36:40 +08:00
|
|
|
|
|
|
|
// Stop if the resulting health is within a reasonable offset from the target
|
|
|
|
if (Math.Abs(lowestHealth - targetMinimumHealth) <= minimum_health_error)
|
|
|
|
break;
|
|
|
|
|
2019-12-26 16:54:31 +08:00
|
|
|
// This effectively works like a binary search - each iteration the search space moves closer to the target, but may exceed it.
|
2019-12-26 16:36:40 +08:00
|
|
|
adjustment *= 2;
|
|
|
|
result += 1.0 / adjustment * Math.Sign(lowestHealth - targetMinimumHealth);
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 16:36:40 +08:00
|
|
|
return result;
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
2023-12-22 16:55:06 +08:00
|
|
|
|
|
|
|
private record struct HealthIncrease(double Time, double Amount, int? Group);
|
2019-12-25 20:16:40 +08:00
|
|
|
}
|
|
|
|
}
|