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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use rosu_map::section::general::GameMode;
use self::calculator::CatchPerformanceCalculator;
use crate::{
Performance,
any::{
CalculateError, Difficulty, HitResultGenerator, InspectablePerformance,
IntoModePerformance, IntoPerformance, hitresult_generator::Fast,
},
catch::{CatchHitResults, performance::inspect::InspectCatchPerformance},
model::{mode::ConvertError, mods::GameMods},
osu::OsuPerformance,
util::map_or_attrs::MapOrAttrs,
};
use super::{Catch, attributes::CatchPerformanceAttributes, score_state::CatchScoreState};
mod calculator;
pub mod gradual;
mod hitresult_generator;
mod inspect;
/// Performance calculator on osu!catch maps.
#[derive(Clone, Debug)]
#[must_use]
pub struct CatchPerformance<'map> {
map_or_attrs: MapOrAttrs<'map, Catch>,
difficulty: Difficulty,
acc: Option<f64>,
combo: Option<u32>,
fruits: Option<u32>,
droplets: Option<u32>,
tiny_droplets: Option<u32>,
tiny_droplet_misses: Option<u32>,
misses: Option<u32>,
hitresult_generator: Option<fn(InspectCatchPerformance<'_>) -> CatchHitResults>,
}
// Manual implementation because of the `hitresult_generator` function pointer
impl PartialEq for CatchPerformance<'_> {
fn eq(&self, other: &Self) -> bool {
let Self {
map_or_attrs,
difficulty,
acc,
combo,
fruits,
droplets,
tiny_droplets,
tiny_droplet_misses,
misses,
hitresult_generator: _,
} = self;
map_or_attrs == &other.map_or_attrs
&& difficulty == &other.difficulty
&& acc == &other.acc
&& combo == &other.combo
&& fruits == &other.fruits
&& droplets == &other.droplets
&& tiny_droplets == &other.tiny_droplets
&& tiny_droplet_misses == &other.tiny_droplet_misses
&& misses == &other.misses
}
}
impl<'map> CatchPerformance<'map> {
/// Create a new performance calculator for osu!catch maps.
///
/// The argument `map_or_attrs` must be either
/// - previously calculated attributes ([`CatchDifficultyAttributes`]
/// or [`CatchPerformanceAttributes`])
/// - a [`Beatmap`] (by reference or value)
///
/// If a map is given, difficulty attributes will need to be calculated
/// internally which is a costly operation. Hence, passing attributes
/// should be prefered.
///
/// However, when passing previously calculated attributes, make sure they
/// have been calculated for the same map and [`Difficulty`] settings.
/// Otherwise, the final attributes will be incorrect.
///
/// [`Beatmap`]: crate::model::beatmap::Beatmap
/// [`CatchDifficultyAttributes`]: crate::catch::CatchDifficultyAttributes
pub fn new(map_or_attrs: impl IntoModePerformance<'map, Catch>) -> Self {
map_or_attrs.into_performance()
}
/// Try to create a new performance calculator for osu!catch maps.
///
/// Returns `None` if `map_or_attrs` does not belong to osu!catch i.e.
/// a [`DifficultyAttributes`] or [`PerformanceAttributes`] of a different
/// mode.
///
/// See [`CatchPerformance::new`] for more information.
///
/// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
/// [`PerformanceAttributes`]: crate::any::PerformanceAttributes
pub fn try_new(map_or_attrs: impl IntoPerformance<'map>) -> Option<Self> {
if let Performance::Catch(calc) = map_or_attrs.into_performance() {
Some(calc)
} else {
None
}
}
/// Specify mods.
///
/// Accepted types are
/// - `u32`
/// - [`rosu_mods::GameModsLegacy`]
/// - [`rosu_mods::GameMods`]
/// - [`rosu_mods::GameModsIntermode`]
/// - [`&rosu_mods::GameModsIntermode`](rosu_mods::GameModsIntermode)
///
/// See <https://github.com/ppy/osu-api/wiki#mods>
pub fn mods(mut self, mods: impl Into<GameMods>) -> Self {
self.difficulty = self.difficulty.mods(mods);
self
}
/// Specify the max combo of the play.
pub const fn combo(mut self, combo: u32) -> Self {
self.combo = Some(combo);
self
}
/// Specify the amount of fruits of a play i.e. n300.
pub const fn fruits(mut self, n_fruits: u32) -> Self {
self.fruits = Some(n_fruits);
self
}
/// Specify the amount of droplets of a play i.e. n100.
pub const fn droplets(mut self, n_droplets: u32) -> Self {
self.droplets = Some(n_droplets);
self
}
/// Specify the amount of tiny droplets of a play i.e. n50.
pub const fn tiny_droplets(mut self, n_tiny_droplets: u32) -> Self {
self.tiny_droplets = Some(n_tiny_droplets);
self
}
/// Specify the amount of tiny droplet misses of a play i.e. `n_katu`.
pub const fn tiny_droplet_misses(mut self, n_tiny_droplet_misses: u32) -> Self {
self.tiny_droplet_misses = Some(n_tiny_droplet_misses);
self
}
/// Specify the amount of fruit / droplet misses of the play.
pub const fn misses(mut self, n_misses: u32) -> Self {
self.misses = Some(n_misses);
self
}
/// Use the specified settings of the given [`Difficulty`].
pub fn difficulty(mut self, difficulty: Difficulty) -> Self {
self.difficulty = difficulty;
self
}
/// Amount of passed objects for partial plays, e.g. a fail.
///
/// If you want to calculate the performance after every few objects,
/// instead of using [`CatchPerformance`] multiple times with different
/// `passed_objects`, you should use [`CatchGradualPerformance`].
///
/// [`CatchGradualPerformance`]: crate::catch::CatchGradualPerformance
pub fn passed_objects(mut self, passed_objects: u32) -> Self {
self.difficulty = self.difficulty.passed_objects(passed_objects);
self
}
/// Adjust the clock rate used in the calculation.
///
/// If none is specified, it will take the clock rate based on the mods
/// i.e. 1.5 for DT, 0.75 for HT and 1.0 otherwise.
///
/// | Minimum | Maximum |
/// | :-----: | :-----: |
/// | 0.01 | 100 |
pub fn clock_rate(mut self, clock_rate: f64) -> Self {
self.difficulty = self.difficulty.clock_rate(clock_rate);
self
}
/// Override a beatmap's set AR.
///
/// `fixed` determines if the given value should be used before
/// or after accounting for mods, e.g. on `true` the value will be
/// used as is and on `false` it will be modified based on the mods.
///
/// | Minimum | Maximum |
/// | :-----: | :-----: |
/// | -20 | 20 |
pub fn ar(mut self, ar: f32, fixed: bool) -> Self {
self.difficulty = self.difficulty.ar(ar, fixed);
self
}
/// Override a beatmap's set CS.
///
/// `fixed` determines if the given value should be used before
/// or after accounting for mods, e.g. on `true` the value will be
/// used as is and on `false` it will be modified based on the mods.
///
/// | Minimum | Maximum |
/// | :-----: | :-----: |
/// | -20 | 20 |
pub fn cs(mut self, cs: f32, fixed: bool) -> Self {
self.difficulty = self.difficulty.cs(cs, fixed);
self
}
/// Override a beatmap's set HP.
///
/// `fixed` determines if the given value should be used before
/// or after accounting for mods, e.g. on `true` the value will be
/// used as is and on `false` it will be modified based on the mods.
///
/// | Minimum | Maximum |
/// | :-----: | :-----: |
/// | -20 | 20 |
pub fn hp(mut self, hp: f32, fixed: bool) -> Self {
self.difficulty = self.difficulty.hp(hp, fixed);
self
}
/// Override a beatmap's set OD.
///
/// `fixed` determines if the given value should be used before
/// or after accounting for mods, e.g. on `true` the value will be
/// used as is and on `false` it will be modified based on the mods.
///
/// | Minimum | Maximum |
/// | :-----: | :-----: |
/// | -20 | 20 |
pub fn od(mut self, od: f32, fixed: bool) -> Self {
self.difficulty = self.difficulty.od(od, fixed);
self
}
/// Adjust patterns as if the HR mod is enabled.
pub fn hardrock_offsets(mut self, hardrock_offsets: bool) -> Self {
self.difficulty = self.difficulty.hardrock_offsets(hardrock_offsets);
self
}
/// Specify how hitresults should be generated.
pub fn hitresult_generator<H: HitResultGenerator<Catch>>(self) -> CatchPerformance<'map> {
CatchPerformance {
map_or_attrs: self.map_or_attrs,
difficulty: self.difficulty,
acc: self.acc,
combo: self.combo,
fruits: self.fruits,
droplets: self.droplets,
tiny_droplets: self.tiny_droplets,
tiny_droplet_misses: self.tiny_droplet_misses,
misses: self.misses,
hitresult_generator: Some(H::generate_hitresults),
}
}
/// Provide parameters through an [`CatchScoreState`].
#[expect(clippy::needless_pass_by_value, reason = "more sensible")]
pub const fn state(mut self, state: CatchScoreState) -> Self {
let CatchScoreState {
max_combo,
hitresults,
} = state;
self.combo = Some(max_combo);
self.hitresults(hitresults)
}
/// Provide parameters through [`CatchHitResults`].
pub const fn hitresults(mut self, hitresults: CatchHitResults) -> Self {
let CatchHitResults {
fruits: n_fruits,
droplets: n_droplets,
tiny_droplets: n_tiny_droplets,
tiny_droplet_misses: n_tiny_droplet_misses,
misses,
} = hitresults;
self.fruits = Some(n_fruits);
self.droplets = Some(n_droplets);
self.tiny_droplets = Some(n_tiny_droplets);
self.tiny_droplet_misses = Some(n_tiny_droplet_misses);
self.misses = Some(misses);
self
}
/// Specify the accuracy of a play between `0.0` and `100.0`.
/// This will be used to generate matching hitresults.
pub fn accuracy(mut self, acc: f64) -> Self {
self.acc = Some(acc.clamp(0.0, 100.0) / 100.0);
self
}
/// Create the [`CatchScoreState`] that will be used for performance
/// calculation.
///
/// If this [`CatchPerformance`] contained a [`Beatmap`], it will be
/// replaced by [`CatchDifficultyAttributes`].
///
/// [`Beatmap`]: crate::Beatmap
/// [`CatchDifficultyAttributes`]: crate::catch::CatchDifficultyAttributes
pub fn generate_state(&mut self) -> Result<CatchScoreState, ConvertError> {
self.map_or_attrs.insert_attrs(&self.difficulty)?;
// SAFETY: We just calculated and inserted the attributes.
let state = unsafe { generate_state(self) };
Ok(state)
}
/// Same as [`CatchPerformance::generate_state`] but verifies that the map
/// was not suspicious.
pub fn checked_generate_state(&mut self) -> Result<CatchScoreState, CalculateError> {
self.map_or_attrs.checked_insert_attrs(&self.difficulty)?;
// SAFETY: We just calculated and inserted the attributes.
let state = unsafe { generate_state(self) };
Ok(state)
}
/// Calculate all performance related values, including pp and stars.
pub fn calculate(mut self) -> Result<CatchPerformanceAttributes, ConvertError> {
let state = self.generate_state()?;
// SAFETY: Attributes are calculated in `generate_state`.
let attrs = unsafe { self.map_or_attrs.into_attrs() };
Ok(CatchPerformanceCalculator::new(attrs, self.difficulty.get_mods(), state).calculate())
}
/// Same as [`CatchPerformance::calculate`] but verifies that the map was
/// not suspicious.
pub fn checked_calculate(mut self) -> Result<CatchPerformanceAttributes, CalculateError> {
let state = self.checked_generate_state()?;
// SAFETY: Attributes are calculated in `checked_generate_state`.
let attrs = unsafe { self.map_or_attrs.into_attrs() };
Ok(CatchPerformanceCalculator::new(attrs, self.difficulty.get_mods(), state).calculate())
}
pub(crate) const fn from_map_or_attrs(map_or_attrs: MapOrAttrs<'map, Catch>) -> Self {
Self {
map_or_attrs,
difficulty: Difficulty::new(),
acc: None,
combo: None,
fruits: None,
droplets: None,
tiny_droplets: None,
tiny_droplet_misses: None,
misses: None,
hitresult_generator: None,
}
}
}
impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> {
type Error = OsuPerformance<'map>;
/// Try to create [`CatchPerformance`] through [`OsuPerformance`].
///
/// Returns `None` if [`OsuPerformance`] does not contain a beatmap, i.e.
/// if it was constructed through attributes or
/// [`OsuPerformance::generate_state`] was called.
fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> {
let mods = osu.difficulty.get_mods();
let map = match OsuPerformance::try_convert_map(osu.map_or_attrs, GameMode::Catch, mods) {
Ok(map) => map,
Err(map_or_attrs) => {
osu.map_or_attrs = map_or_attrs;
return Err(osu);
}
};
let OsuPerformance {
map_or_attrs: _,
difficulty,
acc,
combo,
large_tick_hits: _,
small_tick_hits: _,
slider_end_hits: _,
n300,
n100,
n50,
misses,
hitresult_priority: _,
hitresult_generator: _,
legacy_total_score: _,
} = osu;
Ok(Self {
map_or_attrs: MapOrAttrs::Map(map),
difficulty,
acc,
combo,
fruits: n300,
droplets: n100,
tiny_droplets: n50,
tiny_droplet_misses: None,
misses,
hitresult_generator: None,
})
}
}
impl<'map, T: IntoModePerformance<'map, Catch>> From<T> for CatchPerformance<'map> {
fn from(into: T) -> Self {
into.into_performance()
}
}
/// # Safety
/// Caller must ensure that the internal [`MapOrAttrs`] contains attributes.
unsafe fn generate_state(perf: &mut CatchPerformance) -> CatchScoreState {
// SAFETY: Ensured by caller
let attrs = unsafe { perf.map_or_attrs.get_attrs() };
let inspect = Catch::inspect_performance(perf, attrs);
let misses = inspect.misses();
let max_combo = perf.combo.unwrap_or_else(|| attrs.max_combo() - misses);
let hitresults = match perf.hitresult_generator {
Some(generator) => generator(inspect),
None => <Fast as HitResultGenerator<Catch>>::generate_hitresults(inspect),
};
let CatchHitResults {
fruits,
droplets,
tiny_droplets,
tiny_droplet_misses,
misses,
} = hitresults;
perf.combo = Some(max_combo);
perf.fruits = Some(fruits);
perf.droplets = Some(droplets);
perf.tiny_droplets = Some(tiny_droplets);
perf.tiny_droplet_misses = Some(tiny_droplet_misses);
perf.misses = Some(misses);
CatchScoreState {
max_combo,
hitresults,
}
}
#[cfg(test)]
mod test {
use std::sync::OnceLock;
use rosu_map::section::general::GameMode;
use crate::{
Beatmap,
any::{DifficultyAttributes, PerformanceAttributes},
catch::CatchDifficultyAttributes,
osu::{OsuDifficultyAttributes, OsuPerformanceAttributes},
};
use super::*;
static ATTRS: OnceLock<CatchDifficultyAttributes> = OnceLock::new();
const N_FRUITS: u32 = 728;
const N_DROPLETS: u32 = 2;
const N_TINY_DROPLETS: u32 = 263;
fn beatmap() -> Beatmap {
Beatmap::from_path("./resources/2118524.osu").unwrap()
}
fn attrs() -> CatchDifficultyAttributes {
ATTRS
.get_or_init(|| {
let map = beatmap();
let attrs = Difficulty::new().calculate_for_mode::<Catch>(&map).unwrap();
assert_eq!(N_FRUITS, attrs.n_fruits);
assert_eq!(N_DROPLETS, attrs.n_droplets);
assert_eq!(N_TINY_DROPLETS, attrs.n_tiny_droplets);
attrs
})
.to_owned()
}
#[test]
fn fruits_missing_objects() {
let state = CatchPerformance::from(attrs())
.fruits(N_FRUITS - 10)
.droplets(N_DROPLETS - 1)
.tiny_droplets(N_TINY_DROPLETS - 50)
.tiny_droplet_misses(20)
.misses(2)
.generate_state()
.unwrap();
let expected = CatchScoreState {
max_combo: N_FRUITS + N_DROPLETS - 2,
hitresults: {
CatchHitResults {
fruits: N_FRUITS - 2,
droplets: N_DROPLETS,
tiny_droplets: N_TINY_DROPLETS - 20,
tiny_droplet_misses: 20,
misses: 2,
}
},
};
assert_eq!(state, expected);
}
#[test]
fn create() {
let mut map = beatmap();
let _ = CatchPerformance::new(CatchDifficultyAttributes::default());
let _ = CatchPerformance::new(CatchPerformanceAttributes::default());
let _ = CatchPerformance::new(&map);
let _ = CatchPerformance::new(map.clone());
let _ = CatchPerformance::try_new(CatchDifficultyAttributes::default()).unwrap();
let _ = CatchPerformance::try_new(CatchPerformanceAttributes::default()).unwrap();
let _ = CatchPerformance::try_new(DifficultyAttributes::Catch(
CatchDifficultyAttributes::default(),
))
.unwrap();
let _ = CatchPerformance::try_new(PerformanceAttributes::Catch(
CatchPerformanceAttributes::default(),
))
.unwrap();
let _ = CatchPerformance::try_new(&map).unwrap();
let _ = CatchPerformance::try_new(map.clone()).unwrap();
let _ = CatchPerformance::from(CatchDifficultyAttributes::default());
let _ = CatchPerformance::from(CatchPerformanceAttributes::default());
let _ = CatchPerformance::from(&map);
let _ = CatchPerformance::from(map.clone());
let _ = CatchDifficultyAttributes::default().performance();
let _ = CatchPerformanceAttributes::default().performance();
assert!(
map.convert_mut(GameMode::Osu, &GameMods::default())
.is_err()
);
assert!(CatchPerformance::try_new(OsuDifficultyAttributes::default()).is_none());
assert!(CatchPerformance::try_new(OsuPerformanceAttributes::default()).is_none());
assert!(
CatchPerformance::try_new(
DifficultyAttributes::Osu(OsuDifficultyAttributes::default())
)
.is_none()
);
assert!(
CatchPerformance::try_new(PerformanceAttributes::Osu(
OsuPerformanceAttributes::default()
))
.is_none()
);
}
}