Skip to content

Commit

Permalink
refactor!: store lazer in Difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxOhn committed Oct 18, 2024
1 parent f72ff79 commit 2c77ddf
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/any/difficulty/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ pub struct InspectDifficulty {
///
/// Only relevant for osu!catch.
pub hardrock_offsets: Option<bool>,
/// Whether the calculated attributes belong to an osu!lazer or osu!stable
/// score.
///
/// Defaults to `true`.
pub lazer: Option<bool>,
}

impl InspectDifficulty {
Expand All @@ -41,6 +46,7 @@ impl InspectDifficulty {
hp,
od,
hardrock_offsets,
lazer,
} = self;

let mut difficulty = Difficulty::new().mods(mods);
Expand Down Expand Up @@ -73,6 +79,10 @@ impl InspectDifficulty {
difficulty = difficulty.hardrock_offsets(hardrock_offsets);
}

if let Some(lazer) = lazer {
difficulty = difficulty.lazer(lazer);
}

difficulty
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/any/difficulty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Difficulty {
hp: Option<ModsDependent>,
od: Option<ModsDependent>,
hardrock_offsets: Option<bool>,
lazer: Option<bool>,
}

/// Wrapper for beatmap attributes in [`Difficulty`].
Expand Down Expand Up @@ -97,6 +98,7 @@ impl Difficulty {
hp: None,
od: None,
hardrock_offsets: None,
lazer: None,
}
}

Expand All @@ -120,6 +122,7 @@ impl Difficulty {
hp,
od,
hardrock_offsets,
lazer,
} = self;

InspectDifficulty {
Expand All @@ -131,6 +134,7 @@ impl Difficulty {
hp,
od,
hardrock_offsets,
lazer,
}
}

Expand Down Expand Up @@ -268,6 +272,18 @@ impl Difficulty {
self
}

/// Whether the calculated attributes belong to an osu!lazer or osu!stable
/// score.
///
/// Defaults to `true`.
///
/// Only relevant for osu!standard performance calculation.
pub const fn lazer(mut self, lazer: bool) -> Self {
self.lazer = Some(lazer);

self
}

/// Perform the difficulty calculation.
pub fn calculate(&self, map: &Beatmap) -> DifficultyAttributes {
let map = Cow::Borrowed(map);
Expand Down Expand Up @@ -347,6 +363,10 @@ impl Difficulty {
self.hardrock_offsets
.unwrap_or_else(|| self.mods.hardrock_offsets())
}

pub(crate) fn get_lazer(&self) -> bool {
self.lazer.unwrap_or(true)
}
}

fn non_zero_u32_to_f32(n: NonZeroU32) -> f32 {
Expand All @@ -364,6 +384,7 @@ impl Debug for Difficulty {
hp,
od,
hardrock_offsets,
lazer,
} = self;

f.debug_struct("Difficulty")
Expand All @@ -375,6 +396,7 @@ impl Debug for Difficulty {
.field("hp", hp)
.field("od", od)
.field("hardrock_offsets", hardrock_offsets)
.field("lazer", lazer)
.finish()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/any/performance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<'map> Performance<'map> {
/// Whether the calculated attributes belong to an osu!lazer or osu!stable
/// score.
///
/// Defaults to lazer.
/// Defaults to `true`.
///
/// This affects internal accuracy calculation because lazer considers
/// slider heads for accuracy whereas stable does not.
Expand Down
1 change: 0 additions & 1 deletion src/catch/performance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> {
n50,
misses,
hitresult_priority: _,
lazer: _,
} = osu;

Ok(Self {
Expand Down
1 change: 0 additions & 1 deletion src/mania/performance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ impl<'map> TryFrom<OsuPerformance<'map>> for ManiaPerformance<'map> {
n50,
misses,
hitresult_priority,
lazer: _,
} = osu;

Ok(Self {
Expand Down
12 changes: 1 addition & 11 deletions src/osu/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,8 @@ pub fn convert_objects(
OsuObjectKind::Circle => attrs.n_circles += 1,
OsuObjectKind::Slider(ref slider) => {
attrs.n_sliders += 1;
attrs.n_slider_ticks += slider.tick_count() as u32;
attrs.max_combo += slider.nested_objects.len() as u32;

attrs.n_slider_ticks += slider
.nested_objects
.iter()
.filter(|nested| {
matches!(
nested.kind,
NestedSliderObjectKind::Tick | NestedSliderObjectKind::Repeat
)
})
.count() as u32;
}
OsuObjectKind::Spinner(_) => attrs.n_spinners += 1,
}
Expand Down
2 changes: 2 additions & 0 deletions src/osu/difficulty/gradual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl OsuGradualDifficulty {

attrs.n_circles = 0;
attrs.n_sliders = 0;
attrs.n_slider_ticks = 0;
attrs.n_spinners = 0;
attrs.max_combo = 0;

Expand Down Expand Up @@ -128,6 +129,7 @@ impl OsuGradualDifficulty {
OsuObjectKind::Circle => attrs.n_circles += 1,
OsuObjectKind::Slider(slider) => {
attrs.n_sliders += 1;
attrs.n_slider_ticks += slider.tick_count() as u32;
attrs.max_combo += slider.nested_objects.len() as u32;
}
OsuObjectKind::Spinner { .. } => attrs.n_spinners += 1,
Expand Down
13 changes: 13 additions & 0 deletions src/osu/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ impl OsuSlider {
.count()
}

/// Counts both ticks and repeats
pub fn tick_count(&self) -> usize {
self.nested_objects
.iter()
.filter(|nested| {
matches!(
nested.kind,
NestedSliderObjectKind::Tick | NestedSliderObjectKind::Repeat
)
})
.count()
}

pub fn tail(&self) -> Option<&NestedSliderObject> {
self.nested_objects
.iter()
Expand Down
5 changes: 4 additions & 1 deletion src/osu/performance/gradual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ use super::{OsuPerformanceAttributes, OsuScoreState};
/// [`next`]: OsuGradualPerformance::next
/// [`nth`]: OsuGradualPerformance::nth
pub struct OsuGradualPerformance {
lazer: bool,
difficulty: OsuGradualDifficulty,
}

impl OsuGradualPerformance {
/// Create a new gradual performance calculator for osu!standard maps.
pub fn new(difficulty: Difficulty, converted: &OsuBeatmap<'_>) -> Self {
let lazer = difficulty.get_lazer();
let difficulty = OsuGradualDifficulty::new(difficulty, converted);

Self { difficulty }
Self { lazer, difficulty }
}

/// Process the next hit object and calculate the performance attributes
Expand All @@ -113,6 +115,7 @@ impl OsuGradualPerformance {
.difficulty
.nth(n)?
.performance()
.lazer(self.lazer)
.state(state)
.difficulty(self.difficulty.difficulty.clone())
.passed_objects(self.difficulty.idx as u32)
Expand Down
12 changes: 5 additions & 7 deletions src/osu/performance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub struct OsuPerformance<'map> {
pub(crate) n50: Option<u32>,
pub(crate) misses: Option<u32>,
pub(crate) hitresult_priority: HitResultPriority,
pub(crate) lazer: Option<bool>,
}

impl<'map> OsuPerformance<'map> {
Expand Down Expand Up @@ -158,12 +157,12 @@ impl<'map> OsuPerformance<'map> {
/// Whether the calculated attributes belong to an osu!lazer or osu!stable
/// score.
///
/// Defaults to lazer.
/// Defaults to `true`.
///
/// This affects internal accuracy calculation because lazer considers
/// slider heads for accuracy whereas stable does not.
pub const fn lazer(mut self, lazer: bool) -> Self {
self.lazer = Some(lazer);
pub fn lazer(mut self, lazer: bool) -> Self {
self.difficulty = self.difficulty.lazer(lazer);

self
}
Expand Down Expand Up @@ -366,7 +365,7 @@ impl<'map> OsuPerformance<'map> {
let mut n100 = self.n100.map_or(0, |n| cmp::min(n, n_remaining));
let mut n50 = self.n50.map_or(0, |n| cmp::min(n, n_remaining));

let lazer = self.lazer.unwrap_or(true);
let lazer = self.difficulty.get_lazer();

let (n_slider_ends, n_slider_ticks, max_slider_ends, max_slider_ticks) = if lazer {
let n_slider_ends = self
Expand Down Expand Up @@ -627,7 +626,7 @@ impl<'map> OsuPerformance<'map> {

let effective_miss_count = calculate_effective_misses(&attrs, &state);

let lazer = self.lazer.unwrap_or(true);
let lazer = self.difficulty.get_lazer();

let (n_slider_ends, n_slider_ticks) = if lazer {
(attrs.n_sliders, attrs.n_slider_ticks)
Expand Down Expand Up @@ -660,7 +659,6 @@ impl<'map> OsuPerformance<'map> {
n50: None,
misses: None,
hitresult_priority: HitResultPriority::DEFAULT,
lazer: None,
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/taiko/performance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ impl<'map> TryFrom<OsuPerformance<'map>> for TaikoPerformance<'map> {
n50: _,
misses,
hitresult_priority,
lazer: _,
} = osu;

Ok(Self {
Expand Down

0 comments on commit 2c77ddf

Please sign in to comment.