bsru 0.7.0

Beatsaber Rust Utilities: A Beatsaber V3 parsing library.
Documentation
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
//! Events that control the color of lights.

use crate::difficulty::lightshow::DistributionType;
use crate::difficulty::lightshow::easing::Easing;
use crate::difficulty::lightshow::filter::Filter;
use crate::loose_bool::LooseBool;
use crate::{impl_event_box, impl_event_data, impl_event_group, impl_timed};
use loose_enum::loose_enum;
use serde::{Deserialize, Serialize};

/// A collection of [`ColorEventGroup`]s that share the same group ID and beat.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
    feature = "bevy_reflect",
    derive(bevy_reflect::Reflect),
    reflect(Debug, Clone, PartialEq)
)]
pub struct ColorEventBox {
    /// The time the event takes place.
    #[serde(rename = "b")]
    pub beat: f32,
    /// The ID of the collection of objects that this event effects.
    #[serde(rename = "g")]
    pub group_id: i32,
    #[serde(rename = "e")]
    pub groups: Vec<ColorEventGroup>,
}

impl Default for ColorEventBox {
    fn default() -> Self {
        Self {
            beat: 0.0,
            group_id: 0,
            groups: vec![ColorEventGroup::default()],
        }
    }
}

impl_timed!(ColorEventBox::beat);
impl_event_box!(ColorEventBox, ColorEventGroup, ColorEventData);

/// A collection of [`ColorEventData`] that share the same [`Filter`] and distribution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
    feature = "bevy_reflect",
    derive(bevy_reflect::Reflect),
    reflect(Debug, Clone, PartialEq)
)]
pub struct ColorEventGroup {
    #[serde(rename = "f")]
    pub filter: Filter,
    #[serde(rename = "d")]
    pub beat_dist_type: DistributionType,
    /// The strength of the beat distribution. Dependent on the [distribution type](Self::beat_dist_type).
    ///
    /// A value of zero will have no effect.
    #[serde(rename = "w")]
    pub beat_dist_value: f32,

    #[serde(rename = "t")]
    pub bright_dist_type: DistributionType,
    /// The strength of the brightness distribution. Dependent on the [distribution type](Self::bright_dist_type).
    ///
    /// A value of zero will have no effect.
    #[serde(rename = "r")]
    pub bright_dist_value: f32,
    /// Whether the first [`ColorEventData`] of the group will be effected by brightness distribution.
    #[serde(rename = "b")]
    pub bright_dist_effect_first: LooseBool,
    /// > Only present in difficulty file V3.2 or higher.
    #[serde(rename = "i")]
    pub bright_dist_easing: Option<Easing>,
    #[serde(rename = "e")]
    pub data: Vec<ColorEventData>,
}

impl Default for ColorEventGroup {
    fn default() -> Self {
        Self {
            filter: Default::default(),
            beat_dist_type: Default::default(),
            beat_dist_value: 0.0,
            bright_dist_type: Default::default(),
            bright_dist_value: 0.0,
            bright_dist_effect_first: Default::default(),
            bright_dist_easing: Some(Easing::Linear),
            data: vec![ColorEventData::default()],
        }
    }
}

impl_event_group!(ColorEventGroup::get_brightness_offset, ColorEventData);

impl ColorEventGroup {
    /// Returns the brightness that the event will be offset for a given light ID.
    /// # Panics
    /// Will panic if the light ID is greater than or equal to the group size.
    #[deprecated(note = "Experimental. Does not consider random in filter calculations.")]
    #[allow(deprecated)]
    pub fn get_brightness_offset(&self, light_id: i32, group_size: i32) -> f32 {
        self.bright_dist_type.compute_value_offset(
            light_id,
            group_size,
            &self.filter,
            self.bright_dist_value,
            self.data.last().map(|data| data.beat_offset),
            self.bright_dist_easing,
        )
    }
}

/// The lowest-level group event type, which determines the color of the event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
    feature = "bevy_reflect",
    derive(bevy_reflect::Reflect),
    reflect(Debug, Clone, PartialEq)
)]
pub struct ColorEventData {
    /// The number of beats the event will be offset from the [`ColorEventBox`]'s beat.
    #[serde(rename = "b")]
    pub beat_offset: f32,
    #[serde(rename = "i")]
    pub transition_type: ColorTransitionType,
    #[serde(rename = "c")]
    pub color: LightColor,
    /// Controls how bright the light is, with zero being off and one being normal brightness.
    #[serde(rename = "s")]
    pub brightness: f32,
    /// Determines the number of strobes that will take place each beat.
    /// A value of zero will result in no strobing.
    #[serde(rename = "f")]
    pub strobe_frequency: i32,
    /// > Only present in difficulty file V3.3 or higher.
    ///
    /// Controls the brightness of the "off" strobe state.
    /// If this equals the event's [brightness](Self::brightness), strobing will have no effect.
    #[serde(rename = "sb")]
    pub strobe_brightness: Option<f32>,
    /// > Only present in difficulty file V3.3 or higher.
    ///
    /// Whether to fade between strobe states or not.
    #[serde(rename = "sf")]
    pub strobe_fade: Option<LooseBool>,
}

impl Default for ColorEventData {
    fn default() -> Self {
        Self {
            beat_offset: 0.0,
            transition_type: Default::default(),
            color: Default::default(),
            brightness: 1.0,
            strobe_frequency: 0,
            strobe_brightness: Some(0.0),
            strobe_fade: Some(LooseBool::False),
        }
    }
}

impl_event_data!(ColorEventData);

loose_enum! {
    /// Controls how the state is changed relative to the previous event.
    #[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
    #[cfg_attr(
        feature = "bevy_reflect",
        derive(bevy_reflect::Reflect),
        reflect(Debug, Clone, PartialEq)
    )]
    pub enum ColorTransitionType: i32 {
        /// Unique to color events.
        /// Has the same effect as using [`TransitionType::Transition`](crate::lightshow::TransitionType::Transition)
        /// and [`Easing::None`] in rotation/translation events.
        #[default]
        Instant = 0,
        /// The state will blend from the previous event's state, using the events [easing](Easing).
        Transition = 1,
        /// The event's state will be ignored, replaced with the state from the previous event.
        Extend = 2,
    }
}

loose_enum! {
    /// Controls which color to display, based on a map or environment's [color scheme](crate::info::color_scheme::ColorScheme).
    #[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
    #[cfg_attr(
        feature = "bevy_reflect",
        derive(bevy_reflect::Reflect),
        reflect(Debug, Clone, PartialEq)
    )]
    pub enum LightColor: i32 {
        #[default]
        Primary = 0,
        Secondary = 1,
        White = 2,
    }
}

#[allow(deprecated)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::LimitBehaviour;
    use crate::difficulty::lightshow::filter::FilterType;
    use crate::difficulty::lightshow::group::EventGroup;

    #[test]
    fn beat_wave() {
        let group = ColorEventGroup {
            beat_dist_type: DistributionType::Wave,
            beat_dist_value: 12.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_beat_offset(i, 12) == i as f32));
    }

    #[test]
    fn beat_step() {
        let group = ColorEventGroup {
            beat_dist_type: DistributionType::Step,
            beat_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_beat_offset(i, 12) == i as f32));
    }

    #[test]
    fn beat_wave_with_division_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::Division,
                parameter1: 2,
                parameter2: 1,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Wave,
            beat_dist_value: 6.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_beat_offset(i + 6, 12) == i as f32));
    }

    #[test]
    fn brightness_wave_with_division_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::Division,
                parameter1: 2,
                parameter2: 1,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Wave,
            bright_dist_value: 6.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_brightness_offset(i + 6, 12) == i as f32));
    }

    #[test]
    fn beat_step_with_division_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::Division,
                parameter1: 2,
                parameter2: 1,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Step,
            beat_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_beat_offset(i + 6, 12) == i as f32));
    }

    #[test]
    fn brightness_step_with_division_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::Division,
                parameter1: 2,
                parameter2: 1,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Step,
            bright_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_brightness_offset(i + 6, 12) == i as f32));
    }

    #[test]
    fn beat_wave_with_step_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::StepAndOffset,
                parameter1: 0,
                parameter2: 2,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Wave,
            beat_dist_value: 6.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_beat_offset(i * 2, 12) == i as f32));
    }

    #[test]
    fn brightness_wave_with_step_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::StepAndOffset,
                parameter1: 0,
                parameter2: 2,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Wave,
            bright_dist_value: 6.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_brightness_offset(i * 2, 12) == i as f32));
    }

    #[test]
    fn beat_step_with_step_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::StepAndOffset,
                parameter1: 0,
                parameter2: 2,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Step,
            beat_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_beat_offset(i * 2, 12) == i as f32));
    }

    #[test]
    fn brightness_step_with_step_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                filter_type: FilterType::StepAndOffset,
                parameter1: 0,
                parameter2: 2,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Step,
            bright_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_brightness_offset(i * 2, 12) == i as f32));
    }

    #[test]
    fn beat_wave_with_reverse_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                reverse: LooseBool::True,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Wave,
            beat_dist_value: 12.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_beat_offset(i, 12) == 12.0 - i as f32));
    }

    #[test]
    fn brightness_wave_with_reverse_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                reverse: LooseBool::True,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Wave,
            bright_dist_value: 12.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_brightness_offset(i, 12) == 12.0 - i as f32));
    }

    #[test]
    fn beat_step_with_reverse_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                reverse: LooseBool::True,
                ..Default::default()
            },
            beat_dist_type: DistributionType::Step,
            beat_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_beat_offset(i, 12) == 12.0 - i as f32));
    }

    #[test]
    fn brightness_step_with_reverse_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                reverse: LooseBool::True,
                ..Default::default()
            },
            bright_dist_type: DistributionType::Step,
            bright_dist_value: 1.0,
            ..Default::default()
        };

        assert!((0..12).all(|i| group.get_brightness_offset(i, 12) == 12.0 - i as f32));
    }

    #[test]
    fn beat_wave_with_limit_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                limit_behaviour: Some(LimitBehaviour::Beat),
                limit_percent: Some(0.5),
                ..Default::default()
            },
            beat_dist_type: DistributionType::Wave,
            beat_dist_value: 12.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_beat_offset(i, 12) == (i * 2) as f32));
    }

    #[test]
    fn brightness_wave_with_limit_filter() {
        let group = ColorEventGroup {
            filter: Filter {
                limit_behaviour: Some(LimitBehaviour::Value),
                limit_percent: Some(0.5),
                ..Default::default()
            },
            bright_dist_type: DistributionType::Wave,
            bright_dist_value: 12.0,
            ..Default::default()
        };

        assert!((0..6).all(|i| group.get_brightness_offset(i, 12) == (i * 2) as f32));
    }
}