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
use std::{borrow::Cow, cell::RefCell, rc::Rc, vec::IntoIter};

use crate::{beatmap::BeatmapHitWindows, taiko::rescale, Beatmap, GameMode, Mods};

use super::{
    colours::ColourDifficultyPreprocessor,
    difficulty_object::{MonoIndex, ObjectLists, TaikoDifficultyObject},
    skills::{Peaks, PeaksDifficultyValues, Skill},
    taiko_object::IntoTaikoObjectIter,
    TaikoDifficultyAttributes, DIFFICULTY_MULTIPLIER,
};

/// Gradually calculate the difficulty attributes of an osu!taiko map.
///
/// Note that this struct implements [`Iterator`](std::iter::Iterator).
/// On every call of [`Iterator::next`](std::iter::Iterator::next), the map's next hit object will
/// be processed and the [`TaikoDifficultyAttributes`] will be updated and returned.
///
/// If you want to calculate performance attributes, use
/// [`TaikoGradualPerformanceAttributes`](crate::taiko::TaikoGradualPerformanceAttributes) instead.
///
/// # Example
///
/// ```
/// use rosu_pp::{Beatmap, taiko::TaikoGradualDifficultyAttributes};
///
/// # /*
/// let map: Beatmap = ...
/// # */
/// # let map = Beatmap::default();
///
/// let mods = 64; // DT
/// let mut iter = TaikoGradualDifficultyAttributes::new(&map, mods);
///
/// let attrs1 = iter.next(); // the difficulty of the map after the first hit object
/// let attrs2 = iter.next(); //                           after the second hit object
///
/// // Remaining hit objects
/// for difficulty in iter {
///     // ...
/// }
/// ```
#[derive(Clone, Debug)]
pub struct TaikoGradualDifficultyAttributes {
    attrs: TaikoDifficultyAttributes,
    hit_objects: IntoIter<Rc<RefCell<TaikoDifficultyObject>>>,
    lists: ObjectLists,
    peaks: Peaks,
    total_hits: usize,
    is_convert: bool,
    pub(crate) started: bool,
}

impl TaikoGradualDifficultyAttributes {
    /// Create a new difficulty attributes iterator for osu!taiko maps.
    pub fn new(map: &Beatmap, mods: u32) -> Self {
        let map = map.convert_mode(GameMode::Taiko);
        let is_convert = matches!(map, Cow::Owned(_));
        let peaks = Peaks::new();
        let clock_rate = mods.clock_rate();

        let BeatmapHitWindows { od: hit_window, .. } = map
            .attributes()
            .mods(mods)
            .clock_rate(clock_rate)
            .hit_windows();

        let mut attrs = TaikoDifficultyAttributes {
            stamina: 0.0,
            rhythm: 0.0,
            colour: 0.0,
            peak: 0.0,
            hit_window,
            stars: 0.0,
            max_combo: 0,
        };

        if map.hit_objects.len() < 2 {
            return Self {
                hit_objects: Vec::new().into_iter(),
                lists: ObjectLists::default(),
                peaks,
                attrs,
                total_hits: 0,
                is_convert,
                started: false,
            };
        }

        attrs.max_combo += map.hit_objects[0].is_circle() as usize;
        attrs.max_combo += map.hit_objects[1].is_circle() as usize;
        let mut total_hits = attrs.max_combo;

        let mut diff_objects = map
            .taiko_objects()
            .skip(2)
            .zip(map.hit_objects.iter().skip(1))
            .zip(map.hit_objects.iter())
            .enumerate()
            .fold(
                ObjectLists::default(),
                |mut lists, (idx, (((base, base_start_time), last), last_last))| {
                    total_hits += base.is_hit as usize;

                    let diff_obj = TaikoDifficultyObject::new(
                        base,
                        base_start_time,
                        last.start_time,
                        last_last.start_time,
                        clock_rate,
                        &lists,
                        idx,
                    );

                    match &diff_obj.mono_idx {
                        MonoIndex::Centre(_) => lists.centres.push(idx),
                        MonoIndex::Rim(_) => lists.rims.push(idx),
                        MonoIndex::None => {}
                    }

                    if diff_obj.note_idx.is_some() {
                        lists.notes.push(idx);
                    }

                    lists.all.push(Rc::new(RefCell::new(diff_obj)));

                    lists
                },
            );

        ColourDifficultyPreprocessor::process_and_assign(&mut diff_objects);

        Self {
            hit_objects: diff_objects.all.clone().into_iter(),
            lists: diff_objects,
            peaks,
            attrs,
            total_hits,
            is_convert,
            started: false,
        }
    }
}

impl Iterator for TaikoGradualDifficultyAttributes {
    type Item = TaikoDifficultyAttributes;

    fn next(&mut self) -> Option<Self::Item> {
        self.started = true;

        loop {
            let curr = self.hit_objects.next()?;
            let borrowed = curr.borrow();
            self.peaks.process(&borrowed, &self.lists);

            if borrowed.base.is_hit {
                self.attrs.max_combo += 1;

                break;
            }
        }

        let PeaksDifficultyValues {
            mut colour_rating,
            mut rhythm_rating,
            mut stamina_rating,
            mut combined_rating,
        } = self.peaks.clone().difficulty_values();

        colour_rating *= DIFFICULTY_MULTIPLIER;
        rhythm_rating *= DIFFICULTY_MULTIPLIER;
        stamina_rating *= DIFFICULTY_MULTIPLIER;
        combined_rating *= DIFFICULTY_MULTIPLIER;

        let mut star_rating = rescale(combined_rating * 1.4);

        // * TODO: This is temporary measure as we don't detect abuse of multiple-input
        // * playstyles of converts within the current system.
        if self.is_convert {
            star_rating *= 0.925;

            // * For maps with low colour variance and high stamina requirement,
            // * multiple inputs are more likely to be abused.
            if colour_rating < 2.0 && stamina_rating > 8.0 {
                star_rating *= 0.8;
            }
        }

        self.attrs.stamina = stamina_rating;
        self.attrs.colour = colour_rating;
        self.attrs.rhythm = rhythm_rating;
        self.attrs.peak = combined_rating;
        self.attrs.stars = star_rating;

        Some(self.attrs.clone())
    }

    #[inline]
    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 = n
            .min(self.total_hits - self.attrs.max_combo)
            .saturating_sub(1);

        for _ in 0..skip {
            loop {
                let curr = self.hit_objects.next()?;
                let borrowed = curr.borrow();
                self.peaks.process(&borrowed, &self.lists);

                if borrowed.base.is_hit {
                    self.attrs.max_combo += 1;

                    break;
                }
            }
        }

        self.next()
    }
}

impl ExactSizeIterator for TaikoGradualDifficultyAttributes {
    #[inline]
    fn len(&self) -> usize {
        self.hit_objects.len()
    }
}