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
use std::cmp;

use crate::{
    any::difficulty::skills::Skill,
    catch::{
        attributes::{GradualObjectCount, ObjectCountBuilder},
        convert::convert_objects,
        CatchBeatmap, CatchDifficultyAttributes,
    },
    Difficulty,
};

use super::{
    object::CatchDifficultyObject, skills::movement::Movement, CatchDifficultySetup,
    DifficultyValues,
};

/// Gradually calculate the difficulty attributes of an osu!catch map.
///
/// Note that this struct implements [`Iterator`].
/// On every call of [`Iterator::next`], the map's next fruit or droplet
/// will be processed and the [`CatchDifficultyAttributes`] will be updated and
/// returned.
///
/// Note that it does not return attributes after a tiny droplet. Only for
/// fruits and droplets.
///
/// If you want to calculate performance attributes, use
/// [`CatchGradualPerformance`] instead.
///
/// # Example
///
/// ```
/// use rosu_pp::{Beatmap, Difficulty};
/// use rosu_pp::catch::{Catch, CatchGradualDifficulty};
///
/// let converted = Beatmap::from_path("./resources/2118524.osu")
///     .unwrap()
///     .unchecked_into_converted::<Catch>();
///
/// let difficulty = Difficulty::new().mods(64); // DT
/// let mut iter = CatchGradualDifficulty::new(difficulty, &converted);
///
/// // the difficulty of the map after the first hit object
/// let attrs1 = iter.next();
/// // ... after the second hit object
/// let attrs2 = iter.next();
///
/// // Remaining hit objects
/// for difficulty in iter {
///     // ...
/// }
/// ```
///
/// [`CatchGradualPerformance`]: crate::catch::CatchGradualPerformance
pub struct CatchGradualDifficulty {
    pub(crate) idx: usize,
    pub(crate) difficulty: Difficulty,
    attrs: CatchDifficultyAttributes,
    /// The delta of object counts after each palpable object
    count: Vec<GradualObjectCount>,
    diff_objects: Box<[CatchDifficultyObject]>,
    movement: Movement,
}

impl CatchGradualDifficulty {
    /// Create a new difficulty attributes iterator for osu!catch maps.
    pub fn new(difficulty: Difficulty, converted: &CatchBeatmap<'_>) -> Self {
        let clock_rate = difficulty.get_clock_rate();

        let CatchDifficultySetup { map_attrs, attrs } =
            CatchDifficultySetup::new(&difficulty, converted);

        let hr_offsets = difficulty.get_hardrock_offsets();
        let mut count = ObjectCountBuilder::new_gradual();
        let palpable_objects =
            convert_objects(converted, &mut count, hr_offsets, map_attrs.cs as f32);

        let diff_objects = DifficultyValues::create_difficulty_objects(
            &map_attrs,
            clock_rate,
            palpable_objects.iter(),
        );

        let count = count.into_gradual();
        let movement = Movement::new(clock_rate);

        Self {
            idx: 0,
            difficulty,
            attrs,
            count,
            diff_objects,
            movement,
        }
    }
}

impl Iterator for CatchGradualDifficulty {
    type Item = CatchDifficultyAttributes;

    fn next(&mut self) -> Option<Self::Item> {
        // The first difficulty object belongs to the second palpable object
        // since each difficulty object requires the current and the last note.
        // Hence, if we're still on the first object, we don't have a difficulty
        // object yet and just skip processing.
        if self.idx > 0 {
            let curr = self.diff_objects.get(self.idx - 1)?;
            Skill::new(&mut self.movement, &self.diff_objects).process(curr);
        } else if self.count.is_empty() {
            return None;
        }

        self.attrs.add_object_count(self.count[self.idx]);
        self.idx += 1;

        let mut attrs = self.attrs.clone();

        let movement = self.movement.as_difficulty_value();
        DifficultyValues::eval(&mut attrs, movement);

        Some(attrs)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();

        (len, Some(len))
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        let skip_iter = self.diff_objects.iter().skip(self.idx.saturating_sub(1));

        let mut take = cmp::min(n, self.len().saturating_sub(1));

        // The first palpable object has no difficulty object
        if self.idx == 0 && take > 0 {
            take -= 1;
            self.attrs.add_object_count(self.count[self.idx]);
            self.idx += 1;
        }

        let mut movement = Skill::new(&mut self.movement, &self.diff_objects);

        for curr in skip_iter.take(take) {
            movement.process(curr);

            self.attrs.add_object_count(self.count[self.idx]);
            self.idx += 1;
        }

        self.next()
    }
}

impl ExactSizeIterator for CatchGradualDifficulty {
    fn len(&self) -> usize {
        self.diff_objects.len() + 1 - self.idx
    }
}

#[cfg(test)]
mod tests {
    use crate::Beatmap;

    use super::*;

    #[test]
    fn empty() {
        let converted = Beatmap::from_bytes(&[]).unwrap().unchecked_into_converted();

        let mut gradual = CatchGradualDifficulty::new(Difficulty::new(), &converted);

        assert!(gradual.next().is_none());
    }

    #[test]
    fn next_and_nth() {
        let converted = Beatmap::from_path("./resources/2118524.osu")
            .unwrap()
            .unchecked_into_converted();

        let difficulty = Difficulty::new();

        let mut gradual = CatchGradualDifficulty::new(difficulty.clone(), &converted);
        let mut gradual_2nd = CatchGradualDifficulty::new(difficulty.clone(), &converted);
        let mut gradual_3rd = CatchGradualDifficulty::new(difficulty.clone(), &converted);

        for i in 1.. {
            let Some(next_gradual) = gradual.next() else {
                assert_eq!(i, 731);
                assert!(gradual_2nd.last().is_none()); // 730 % 2 == 0
                assert!(gradual_3rd.last().is_some()); // 730 % 3 == 1
                break;
            };

            if i % 2 == 0 {
                let next_gradual_2nd = gradual_2nd.nth(1).unwrap();
                assert_eq!(next_gradual, next_gradual_2nd);
            }

            if i % 3 == 0 {
                let next_gradual_3rd = gradual_3rd.nth(2).unwrap();
                assert_eq!(next_gradual, next_gradual_3rd);
            }

            let expected = difficulty
                .clone()
                .passed_objects(i as u32)
                .with_mode()
                .calculate(&converted);

            assert_eq!(next_gradual, expected);
        }
    }
}