-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ScoreProcessor.cs
557 lines (446 loc) · 19.2 KB
/
ScoreProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using MessagePack;
using osu.Framework.Bindables;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
using osu.Game.Localisation;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Replays;
using osu.Game.Scoring;
namespace osu.Game.Rulesets.Scoring
{
public partial class ScoreProcessor : JudgementProcessor
{
public const double MAX_SCORE = 1000000;
private const double accuracy_cutoff_x = 1;
private const double accuracy_cutoff_s = 0.95;
private const double accuracy_cutoff_a = 0.9;
private const double accuracy_cutoff_b = 0.8;
private const double accuracy_cutoff_c = 0.7;
private const double accuracy_cutoff_d = 0;
/// <summary>
/// Whether <see cref="HitEvents"/> should be populated during application of results.
/// </summary>
/// <remarks>
/// Should only be disabled for special cases.
/// When disabled, <see cref="JudgementProcessor.RevertResult"/> cannot be used.</remarks>
internal bool TrackHitEvents = true;
/// <summary>
/// Invoked when this <see cref="ScoreProcessor"/> was reset from a replay frame.
/// </summary>
public event Action? OnResetFromReplayFrame;
/// <summary>
/// The current total score.
/// </summary>
public readonly BindableLong TotalScore = new BindableLong { MinValue = 0 };
/// <summary>
/// The current accuracy.
/// </summary>
public readonly BindableDouble Accuracy = new BindableDouble(1) { MinValue = 0, MaxValue = 1 };
/// <summary>
/// The minimum achievable accuracy for the whole beatmap at this stage of gameplay.
/// Assumes that all objects that have not been judged yet will receive the minimum hit result.
/// </summary>
public readonly BindableDouble MinimumAccuracy = new BindableDouble { MinValue = 0, MaxValue = 1 };
/// <summary>
/// The maximum achievable accuracy for the whole beatmap at this stage of gameplay.
/// Assumes that all objects that have not been judged yet will receive the maximum hit result.
/// </summary>
public readonly BindableDouble MaximumAccuracy = new BindableDouble(1) { MinValue = 0, MaxValue = 1 };
/// <summary>
/// The current combo.
/// </summary>
public readonly BindableInt Combo = new BindableInt();
/// <summary>
/// The current selected mods
/// </summary>
public readonly Bindable<IReadOnlyList<Mod>> Mods = new Bindable<IReadOnlyList<Mod>>(Array.Empty<Mod>());
/// <summary>
/// The current rank.
/// </summary>
public readonly Bindable<ScoreRank> Rank = new Bindable<ScoreRank>(ScoreRank.X);
/// <summary>
/// The highest combo achieved by this score.
/// </summary>
public readonly BindableInt HighestCombo = new BindableInt();
/// <summary>
/// The <see cref="HitEvent"/>s collected during gameplay thus far.
/// Intended for use with various statistics displays.
/// </summary>
public IReadOnlyList<HitEvent> HitEvents => hitEvents;
/// <summary>
/// The ruleset this score processor is valid for.
/// </summary>
public readonly Ruleset Ruleset;
/// <summary>
/// The maximum achievable total score.
/// </summary>
public long MaximumTotalScore { get; private set; }
/// <summary>
/// The maximum sum of accuracy-affecting judgements at the current point in time.
/// </summary>
/// <remarks>
/// Used to compute accuracy.
/// </remarks>
private double currentMaximumBaseScore;
/// <summary>
/// The sum of all accuracy-affecting judgements at the current point in time.
/// </summary>
/// <remarks>
/// Used to compute accuracy.
/// </remarks>
private double currentBaseScore;
/// <summary>
/// The maximum sum of all accuracy-affecting judgements in the beatmap.
/// </summary>
private double maximumBaseScore;
/// <summary>
/// The count of all accuracy-affecting judgements in the beatmap.
/// </summary>
private int maximumAccuracyJudgementCount;
/// <summary>
/// The count of accuracy-affecting judgements at the current point in time.
/// </summary>
private int currentAccuracyJudgementCount;
/// <summary>
/// The maximum combo score in the beatmap.
/// </summary>
private double maximumComboPortion;
/// <summary>
/// The combo score at the current point in time.
/// </summary>
private double currentComboPortion;
/// <summary>
/// The bonus score at the current point in time.
/// </summary>
private double currentBonusPortion;
/// <summary>
/// The total score multiplier.
/// </summary>
private double scoreMultiplier = 1;
public Dictionary<HitResult, int> MaximumStatistics
{
get
{
if (!beatmapApplied)
throw new InvalidOperationException($"Cannot access maximum statistics before calling {nameof(ApplyBeatmap)}.");
return new Dictionary<HitResult, int>(maximumResultCounts);
}
}
private bool beatmapApplied;
private readonly Dictionary<HitResult, int> scoreResultCounts = new Dictionary<HitResult, int>();
private readonly Dictionary<HitResult, int> maximumResultCounts = new Dictionary<HitResult, int>();
private readonly List<HitEvent> hitEvents = new List<HitEvent>();
private HitObject? lastHitObject;
public ScoreProcessor(Ruleset ruleset)
{
Ruleset = ruleset;
Combo.ValueChanged += combo => HighestCombo.Value = Math.Max(HighestCombo.Value, combo.NewValue);
Accuracy.ValueChanged += accuracy =>
{
Rank.Value = RankFromAccuracy(accuracy.NewValue);
foreach (var mod in Mods.Value.OfType<IApplicableToScoreProcessor>())
Rank.Value = mod.AdjustRank(Rank.Value, accuracy.NewValue);
};
Mods.ValueChanged += mods =>
{
scoreMultiplier = 1;
foreach (var m in mods.NewValue)
scoreMultiplier *= m.ScoreMultiplier;
updateScore();
};
}
public override void ApplyBeatmap(IBeatmap beatmap)
{
base.ApplyBeatmap(beatmap);
beatmapApplied = true;
}
protected sealed override void ApplyResultInternal(JudgementResult result)
{
result.ComboAtJudgement = Combo.Value;
result.HighestComboAtJudgement = HighestCombo.Value;
if (result.FailedAtJudgement)
return;
scoreResultCounts[result.Type] = scoreResultCounts.GetValueOrDefault(result.Type) + 1;
if (!result.Type.IsScorable())
return;
if (result.Type.IncreasesCombo())
Combo.Value++;
else if (result.Type.BreaksCombo())
Combo.Value = 0;
result.ComboAfterJudgement = Combo.Value;
if (result.Type.AffectsAccuracy())
{
currentMaximumBaseScore += Judgement.ToNumericResult(result.Judgement.MaxResult);
currentBaseScore += Judgement.ToNumericResult(result.Type);
currentAccuracyJudgementCount++;
}
if (result.Type.IsBonus())
currentBonusPortion += GetBonusScoreChange(result);
else
currentComboPortion += GetComboScoreChange(result);
ApplyScoreChange(result);
if (!IsSimulating)
{
if (TrackHitEvents)
{
hitEvents.Add(CreateHitEvent(result));
lastHitObject = result.HitObject;
}
updateScore();
}
}
/// <summary>
/// Creates the <see cref="HitEvent"/> that describes a <see cref="JudgementResult"/>.
/// </summary>
/// <param name="result">The <see cref="JudgementResult"/> to describe.</param>
/// <returns>The <see cref="HitEvent"/>.</returns>
protected virtual HitEvent CreateHitEvent(JudgementResult result)
=> new HitEvent(result.TimeOffset, result.Type, result.HitObject, lastHitObject, null);
protected sealed override void RevertResultInternal(JudgementResult result)
{
if (!TrackHitEvents)
throw new InvalidOperationException(@$"Rewind is not supported when {nameof(TrackHitEvents)} is disabled.");
Combo.Value = result.ComboAtJudgement;
HighestCombo.Value = result.HighestComboAtJudgement;
if (result.FailedAtJudgement)
return;
scoreResultCounts[result.Type] = scoreResultCounts.GetValueOrDefault(result.Type) - 1;
if (!result.Type.IsScorable())
return;
if (result.Type.AffectsAccuracy())
{
currentMaximumBaseScore -= Judgement.ToNumericResult(result.Judgement.MaxResult);
currentBaseScore -= Judgement.ToNumericResult(result.Type);
currentAccuracyJudgementCount--;
}
if (result.Type.IsBonus())
currentBonusPortion -= GetBonusScoreChange(result);
else
currentComboPortion -= GetComboScoreChange(result);
RemoveScoreChange(result);
Debug.Assert(hitEvents.Count > 0);
lastHitObject = hitEvents[^1].LastHitObject;
hitEvents.RemoveAt(hitEvents.Count - 1);
updateScore();
}
protected virtual double GetBonusScoreChange(JudgementResult result) => Judgement.ToNumericResult(result.Type);
protected virtual double GetComboScoreChange(JudgementResult result) => Judgement.ToNumericResult(result.Type) * (1 + result.ComboAfterJudgement / 10d);
protected virtual void ApplyScoreChange(JudgementResult result)
{
}
protected virtual void RemoveScoreChange(JudgementResult result)
{
}
private void updateScore()
{
Accuracy.Value = currentMaximumBaseScore > 0 ? currentBaseScore / currentMaximumBaseScore : 1;
MinimumAccuracy.Value = maximumBaseScore > 0 ? currentBaseScore / maximumBaseScore : 0;
MaximumAccuracy.Value = maximumBaseScore > 0 ? (currentBaseScore + (maximumBaseScore - currentMaximumBaseScore)) / maximumBaseScore : 1;
double comboProgress = maximumComboPortion > 0 ? currentComboPortion / maximumComboPortion : 1;
double accuracyProcess = maximumAccuracyJudgementCount > 0 ? (double)currentAccuracyJudgementCount / maximumAccuracyJudgementCount : 1;
TotalScore.Value = (long)Math.Round(ComputeTotalScore(comboProgress, accuracyProcess, currentBonusPortion) * scoreMultiplier);
}
protected virtual double ComputeTotalScore(double comboProgress, double accuracyProgress, double bonusPortion)
{
return 700000 * comboProgress +
300000 * Math.Pow(Accuracy.Value, 10) * accuracyProgress +
bonusPortion;
}
/// <summary>
/// Resets this ScoreProcessor to a default state.
/// </summary>
/// <param name="storeResults">Whether to store the current state of the <see cref="ScoreProcessor"/> for future use.</param>
protected override void Reset(bool storeResults)
{
// Run one last time to store max values.
updateScore();
base.Reset(storeResults);
hitEvents.Clear();
lastHitObject = null;
if (storeResults)
{
maximumBaseScore = currentBaseScore;
maximumComboPortion = currentComboPortion;
maximumAccuracyJudgementCount = currentAccuracyJudgementCount;
maximumResultCounts.Clear();
maximumResultCounts.AddRange(scoreResultCounts);
MaximumTotalScore = TotalScore.Value;
}
scoreResultCounts.Clear();
currentBaseScore = 0;
currentMaximumBaseScore = 0;
currentAccuracyJudgementCount = 0;
currentComboPortion = 0;
currentBonusPortion = 0;
TotalScore.Value = 0;
Accuracy.Value = 1;
Combo.Value = 0;
Rank.Disabled = false;
Rank.Value = ScoreRank.X;
HighestCombo.Value = 0;
}
/// <summary>
/// Retrieve a score populated with data for the current play this processor is responsible for.
/// </summary>
public virtual void PopulateScore(ScoreInfo score)
{
score.Combo = Combo.Value;
score.MaxCombo = HighestCombo.Value;
score.Accuracy = Accuracy.Value;
score.Rank = Rank.Value;
score.HitEvents = hitEvents;
score.Statistics.Clear();
score.MaximumStatistics.Clear();
foreach (var result in HitResultExtensions.ALL_TYPES)
score.Statistics[result] = scoreResultCounts.GetValueOrDefault(result);
foreach (var result in HitResultExtensions.ALL_TYPES)
score.MaximumStatistics[result] = maximumResultCounts.GetValueOrDefault(result);
// Populate total score after everything else.
score.TotalScore = TotalScore.Value;
}
/// <summary>
/// Populates a failed score, marking it with the <see cref="ScoreRank.F"/> rank.
/// </summary>
public void FailScore(ScoreInfo score)
{
if (Rank.Value == ScoreRank.F)
return;
score.Passed = false;
Rank.Value = ScoreRank.F;
PopulateScore(score);
}
public override void ResetFromReplayFrame(ReplayFrame frame)
{
base.ResetFromReplayFrame(frame);
if (frame.Header == null)
return;
Combo.Value = frame.Header.Combo;
HighestCombo.Value = frame.Header.MaxCombo;
TotalScore.Value = frame.Header.TotalScore;
scoreResultCounts.Clear();
scoreResultCounts.AddRange(frame.Header.Statistics);
SetScoreProcessorStatistics(frame.Header.ScoreProcessorStatistics);
updateScore();
OnResetFromReplayFrame?.Invoke();
}
public ScoreProcessorStatistics GetScoreProcessorStatistics() => new ScoreProcessorStatistics
{
MaximumBaseScore = currentMaximumBaseScore,
BaseScore = currentBaseScore,
AccuracyJudgementCount = currentAccuracyJudgementCount,
ComboPortion = currentComboPortion,
BonusPortion = currentBonusPortion
};
public void SetScoreProcessorStatistics(ScoreProcessorStatistics statistics)
{
currentMaximumBaseScore = statistics.MaximumBaseScore;
currentBaseScore = statistics.BaseScore;
currentAccuracyJudgementCount = statistics.AccuracyJudgementCount;
currentComboPortion = statistics.ComboPortion;
currentBonusPortion = statistics.BonusPortion;
}
#region Static helper methods
/// <summary>
/// Given an accuracy (0..1), return the correct <see cref="ScoreRank"/>.
/// </summary>
public static ScoreRank RankFromAccuracy(double accuracy)
{
if (accuracy == accuracy_cutoff_x)
return ScoreRank.X;
if (accuracy >= accuracy_cutoff_s)
return ScoreRank.S;
if (accuracy >= accuracy_cutoff_a)
return ScoreRank.A;
if (accuracy >= accuracy_cutoff_b)
return ScoreRank.B;
if (accuracy >= accuracy_cutoff_c)
return ScoreRank.C;
return ScoreRank.D;
}
/// <summary>
/// Given a <see cref="ScoreRank"/>, return the cutoff accuracy (0..1).
/// Accuracy must be greater than or equal to the cutoff to qualify for the provided rank.
/// </summary>
public static double AccuracyCutoffFromRank(ScoreRank rank)
{
switch (rank)
{
case ScoreRank.X:
case ScoreRank.XH:
return accuracy_cutoff_x;
case ScoreRank.S:
case ScoreRank.SH:
return accuracy_cutoff_s;
case ScoreRank.A:
return accuracy_cutoff_a;
case ScoreRank.B:
return accuracy_cutoff_b;
case ScoreRank.C:
return accuracy_cutoff_c;
case ScoreRank.D:
return accuracy_cutoff_d;
default:
throw new ArgumentOutOfRangeException(nameof(rank), rank, null);
}
}
#endregion
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
hitEvents.Clear();
}
}
public enum ScoringMode
{
[LocalisableDescription(typeof(GameplaySettingsStrings), nameof(GameplaySettingsStrings.StandardisedScoreDisplay))]
Standardised,
[LocalisableDescription(typeof(GameplaySettingsStrings), nameof(GameplaySettingsStrings.ClassicScoreDisplay))]
Classic
}
[Serializable]
[MessagePackObject]
public class ScoreProcessorStatistics
{
/// <summary>
/// The sum of all accuracy-affecting judgements at the current point in time.
/// </summary>
/// <remarks>
/// Used to compute accuracy.
/// See: <see cref="HitResultExtensions.IsBasic"/> and <see cref="Judgement.ToNumericResult"/>.
/// </remarks>
[Key(0)]
public double BaseScore { get; set; }
/// <summary>
/// The maximum sum of accuracy-affecting judgements at the current point in time.
/// </summary>
/// <remarks>
/// Used to compute accuracy.
/// </remarks>
[Key(1)]
public double MaximumBaseScore { get; set; }
/// <summary>
/// The count of accuracy-affecting judgements at the current point in time.
/// </summary>
[Key(2)]
public int AccuracyJudgementCount { get; set; }
/// <summary>
/// The combo score at the current point in time.
/// </summary>
[Key(3)]
public double ComboPortion { get; set; }
/// <summary>
/// The bonus score at the current point in time.
/// </summary>
[Key(4)]
public double BonusPortion { get; set; }
}
}