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
use std::fmt::{Debug, Formatter, Result as FmtResult};

use rosu_mods::{
    generated_mods::{
        DifficultyAdjustCatch, DifficultyAdjustMania, DifficultyAdjustOsu, DifficultyAdjustTaiko,
    },
    GameMod, GameModIntermode, GameMods as GameModsLazer, GameModsIntermode, GameModsLegacy,
};

/// Collection of game mods.
///
/// This type can be created through its `From<T>` implementations where `T`
/// can be
/// - `u32`
/// - [`rosu_mods::GameModsLegacy`]
/// - [`rosu_mods::GameMods`]
/// - [`rosu_mods::GameModsIntermode`]
/// - [`&rosu_mods::GameModsIntermode`](rosu_mods::GameModsIntermode)
///
/// # Example
///
/// ```
/// use rosu_pp::GameMods;
/// use rosu_mods::{GameModsIntermode, GameModsLegacy, GameMods as GameModsLazer};
///
/// let int = GameMods::from(64 + 8);
/// let legacy = GameMods::from(GameModsLegacy::Hidden | GameModsLegacy::Easy);
/// let lazer = GameMods::from(GameModsLazer::new());
/// let intermode = GameMods::from(GameModsIntermode::new());
/// ```
#[derive(Clone, PartialEq)]
pub struct GameMods {
    inner: GameModsInner,
}

impl Debug for GameMods {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self.inner {
            GameModsInner::Lazer(ref mods) => Debug::fmt(mods, f),
            GameModsInner::Intermode(ref mods) => Debug::fmt(mods, f),
            GameModsInner::Legacy(ref mods) => Debug::fmt(mods, f),
        }
    }
}

/// Inner type of [`GameMods`] so that remote types contained in variants don't
/// need to be re-exported.
#[derive(Clone, PartialEq)]
enum GameModsInner {
    Lazer(GameModsLazer),
    Intermode(GameModsIntermode),
    Legacy(GameModsLegacy),
}

impl GameMods {
    pub(crate) const DEFAULT: Self = Self {
        inner: GameModsInner::Legacy(GameModsLegacy::NoMod),
    };

    /// Returns the mods' clock rate.
    ///
    /// In case of variable clock rates like for `WindUp`, this will return
    /// `1.0`.
    pub(crate) fn clock_rate(&self) -> f32 {
        match self.inner {
            GameModsInner::Lazer(ref mods) => mods.clock_rate().unwrap_or(1.0),
            GameModsInner::Intermode(ref mods) => mods.legacy_clock_rate(),
            GameModsInner::Legacy(mods) => mods.clock_rate(),
        }
    }

    pub(crate) fn od_ar_hp_multiplier(&self) -> f64 {
        if self.hr() {
            1.4
        } else if self.ez() {
            0.5
        } else {
            1.0
        }
    }

    /// Check whether the mods enable `hardrock_offsets`.
    pub(crate) fn hardrock_offsets(&self) -> bool {
        fn custom_hardrock_offsets(mods: &GameMods) -> Option<bool> {
            match mods.inner {
                GameModsInner::Lazer(ref mods) => mods.iter().find_map(|gamemod| match gamemod {
                    GameMod::DifficultyAdjustCatch(DifficultyAdjustCatch {
                        hard_rock_offsets,
                        ..
                    }) => *hard_rock_offsets,
                    _ => None,
                }),
                GameModsInner::Intermode(_) | GameModsInner::Legacy(_) => None,
            }
        }

        custom_hardrock_offsets(self).unwrap_or_else(|| self.hr())
    }
}

macro_rules! impl_map_attr {
    ( $( $fn:ident: $field:ident [ $( $mode:ident ),* ] [$s:literal] ;)* ) => {
        impl GameMods {
            $(
                #[doc = "Check whether the mods specify a custom "]
                #[doc = $s]
                #[doc = "value."]
                pub(crate) fn $fn(&self) -> Option<f32> {
                    match self.inner {
                        GameModsInner::Lazer(ref mods) => mods.iter().find_map(|gamemod| match gamemod {
                            $( impl_map_attr!( @ $mode $field) => *$field, )*
                            _ => None,
                        }),
                        GameModsInner::Intermode(_) | GameModsInner::Legacy(_) => None,
                    }
                }
            )*
        }
    };

    ( @ Osu $field:ident) => { GameMod::DifficultyAdjustOsu(DifficultyAdjustOsu { $field, .. }) };
    ( @ Taiko $field:ident) => { GameMod::DifficultyAdjustTaiko(DifficultyAdjustTaiko { $field, .. }) };
    ( @ Catch $field:ident) => { GameMod::DifficultyAdjustCatch(DifficultyAdjustCatch { $field, .. }) };
    ( @ Mania $field:ident) => { GameMod::DifficultyAdjustMania(DifficultyAdjustMania { $field, .. }) };
}

impl_map_attr! {
    ar: approach_rate [Osu, Catch] ["ar"];
    cs: circle_size [Osu, Catch] ["cs"];
    hp: drain_rate [Osu, Taiko, Catch, Mania] ["hp"];
    od: overall_difficulty [Osu, Taiko, Catch, Mania] ["od"];
}

macro_rules! impl_has_mod {
    ( $( $fn:ident: $sign:tt $name:ident [ $s:literal ], )* ) => {
        impl GameMods {
            $(
                // workaround for <https://github.com/rust-lang/rust-analyzer/issues/8092>
                #[doc = "Check whether [`GameMods`] contain `"]
                #[doc = $s]
                #[doc = "`."]
                pub(crate) fn $fn(&self) -> bool {
                    match self.inner {
                        GameModsInner::Lazer(ref mods) => {
                            mods.contains_intermode(GameModIntermode::$name)
                        },
                        GameModsInner::Intermode(ref mods) => {
                            mods.contains(GameModIntermode::$name)
                        },
                        GameModsInner::Legacy(_mods) => {
                            impl_has_mod!(LEGACY $sign $name _mods)
                        },
                    }
                }
            )*
        }
    };

    ( LEGACY + $name:ident $mods:ident ) => {
        $mods.contains(GameModsLegacy::$name)
    };

    ( LEGACY - $name:ident $mods:ident ) => {
        false
    };
}

impl_has_mod! {
    nf: + NoFail ["NoFail"],
    ez: + Easy ["Easy"],
    td: + TouchDevice ["TouchDevice"],
    hd: + Hidden ["Hidden"],
    hr: + HardRock ["HardRock"],
    rx: + Relax ["Relax"],
    fl: + Flashlight ["Flashlight"],
    so: + SpunOut ["SpunOut"],
    bl: - Blinds ["Blinds"],
}

impl Default for GameMods {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl From<GameModsLazer> for GameMods {
    fn from(mods: GameModsLazer) -> Self {
        Self {
            inner: GameModsInner::Lazer(mods),
        }
    }
}

impl From<GameModsIntermode> for GameMods {
    fn from(mods: GameModsIntermode) -> Self {
        Self {
            inner: GameModsInner::Intermode(mods),
        }
    }
}

impl From<&GameModsIntermode> for GameMods {
    fn from(mods: &GameModsIntermode) -> Self {
        // If only legacy mods are set, use `GameModsLegacy` and thus avoid
        // allocating an owned `GameModsIntermode` instance.
        match mods.checked_bits() {
            Some(bits) => bits.into(),
            None => mods.to_owned().into(),
        }
    }
}

impl From<GameModsLegacy> for GameMods {
    fn from(mods: GameModsLegacy) -> Self {
        Self {
            inner: GameModsInner::Legacy(mods),
        }
    }
}

impl From<u32> for GameMods {
    fn from(bits: u32) -> Self {
        GameModsLegacy::from_bits(bits).into()
    }
}