osu/osu.Game/Rulesets/Scoring/HealthProcessor.cs

168 lines
5.8 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.
using System;
2019-12-25 04:09:04 +00:00
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.MathUtils;
2019-12-25 04:32:58 +00:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Judgements;
2019-12-25 04:09:04 +00:00
using osu.Game.Rulesets.Objects;
namespace osu.Game.Rulesets.Scoring
{
public class HealthProcessor : JudgementProcessor
{
/// <summary>
/// Invoked when the <see cref="ScoreProcessor"/> is in a failed state.
/// Return true if the fail was permitted.
/// </summary>
public event Func<bool> Failed;
/// <summary>
/// Additional conditions on top of <see cref="DefaultFailCondition"/> that cause a failing state.
/// </summary>
public event Func<HealthProcessor, JudgementResult, bool> FailConditions;
/// <summary>
/// The current health.
/// </summary>
public readonly BindableDouble Health = new BindableDouble(1) { MinValue = 0, MaxValue = 1 };
2019-12-25 05:49:17 +00:00
/// <summary>
/// Whether gameplay is currently in a break.
/// </summary>
public readonly IBindable<bool> IsBreakTime = new Bindable<bool>();
/// <summary>
/// Whether this ScoreProcessor has already triggered the failed state.
/// </summary>
public bool HasFailed { get; private set; }
2019-12-25 06:33:13 +00:00
private readonly double gameplayStartTime;
private IBeatmap beatmap;
2019-12-25 04:34:13 +00:00
private List<(double time, double health)> healthIncreases;
2019-12-25 04:32:58 +00:00
private double targetMinimumHealth;
2019-12-25 04:09:04 +00:00
private double drainRate = 1;
2019-12-25 06:33:13 +00:00
public HealthProcessor(double gameplayStartTime)
{
this.gameplayStartTime = gameplayStartTime;
}
2019-12-25 04:32:58 +00:00
public override void ApplyBeatmap(IBeatmap beatmap)
{
2019-12-25 06:33:13 +00:00
this.beatmap = beatmap;
2019-12-25 04:34:13 +00:00
healthIncreases = new List<(double time, double health)>();
2019-12-25 10:19:57 +00:00
targetMinimumHealth = BeatmapDifficulty.DifficultyRange(beatmap.BeatmapInfo.BaseDifficulty.DrainRate, 0.95, 0.70, 0.30);
2019-12-25 04:34:13 +00:00
2019-12-25 04:32:58 +00:00
base.ApplyBeatmap(beatmap);
2019-12-25 04:34:13 +00:00
// Only required during the simulation stage
healthIncreases = null;
2019-12-25 04:32:58 +00:00
}
protected override void Update()
{
base.Update();
2019-12-25 05:49:17 +00:00
if (!IsBreakTime.Value)
{
// When jumping from before the gameplay start to after it or vice-versa, we only want to consider any drain since the gameplay start time
double lastTime = Math.Max(gameplayStartTime, Time.Current - Time.Elapsed);
Health.Value -= drainRate * (Time.Current - lastTime);
}
}
2019-12-25 04:09:04 +00:00
protected override void ApplyResultInternal(JudgementResult result)
{
result.HealthAtJudgement = Health.Value;
result.FailedAtJudgement = HasFailed;
2019-12-25 04:33:50 +00:00
double healthIncrease = result.Judgement.HealthIncreaseFor(result);
2019-12-25 04:34:13 +00:00
healthIncreases?.Add((result.HitObject.GetEndTime() + result.TimeOffset, healthIncrease));
2019-12-25 04:09:04 +00:00
if (HasFailed)
return;
2019-12-25 04:09:04 +00:00
Health.Value += healthIncrease;
if (!DefaultFailCondition && FailConditions?.Invoke(this, result) != true)
return;
if (Failed?.Invoke() != false)
HasFailed = true;
}
protected override void RevertResultInternal(JudgementResult result)
{
Health.Value = result.HealthAtJudgement;
// Todo: Revert HasFailed state with proper player support
}
/// <summary>
/// The default conditions for failing.
/// </summary>
protected virtual bool DefaultFailCondition => Precision.AlmostBigger(Health.MinValue, Health.Value);
protected override void Reset(bool storeResults)
{
base.Reset(storeResults);
2019-12-25 04:09:04 +00:00
drainRate = 1;
if (storeResults)
{
int count = 1;
while (true)
{
double currentHealth = 1;
double lowestHealth = 1;
2019-12-25 06:33:13 +00:00
int currentBreak = -1;
2019-12-25 04:09:04 +00:00
for (int i = 0; i < healthIncreases.Count; i++)
{
2019-12-25 06:33:13 +00:00
double currentTime = healthIncreases[i].time;
double lastTime = i > 0 ? healthIncreases[i - 1].time : gameplayStartTime;
// Subtract any break time from the duration since the last object
if (beatmap.Breaks.Count > 0)
{
while (currentBreak + 1 < beatmap.Breaks.Count && beatmap.Breaks[currentBreak + 1].EndTime < currentTime)
currentBreak++;
if (currentBreak >= 0)
lastTime = Math.Max(lastTime, beatmap.Breaks[currentBreak].EndTime);
}
2019-12-25 04:09:04 +00:00
2019-12-25 06:33:13 +00:00
// Apply health adjustments
2019-12-25 04:09:04 +00:00
currentHealth -= (healthIncreases[i].time - lastTime) * drainRate;
lowestHealth = Math.Min(lowestHealth, currentHealth);
currentHealth = Math.Min(1, currentHealth + healthIncreases[i].health);
// Common scenario for when the drain rate is definitely too harsh
if (lowestHealth < 0)
break;
}
2019-12-25 04:32:58 +00:00
if (Math.Abs(lowestHealth - targetMinimumHealth) <= 0.01)
2019-12-25 04:09:04 +00:00
break;
count *= 2;
2019-12-25 04:32:58 +00:00
drainRate += 1.0 / count * Math.Sign(lowestHealth - targetMinimumHealth);
2019-12-25 04:09:04 +00:00
}
}
healthIncreases.Clear();
Health.Value = 1;
HasFailed = false;
}
}
}