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
//! Decoder for converting FNF .json to `RoxChart`.
use crate::codec::Decoder;
use crate::error::RoxResult;
use crate::model::{Metadata, Note, RoxChart, TimingPoint};
use super::parser;
use super::types::{FnfChart, FnfSide};
/// Decoder for Friday Night Funkin' charts.
pub struct FnfDecoder;
impl FnfDecoder {
/// Decode with a specific side selection.
///
/// # Errors
///
/// Returns an error if parsing fails.
pub fn decode_with_side(data: &[u8], side: FnfSide) -> RoxResult<RoxChart> {
let fnf = parser::parse(data)?;
Ok(Self::from_fnf(&fnf, side))
}
/// Convert an `FnfChart` to `RoxChart` with the specified side.
#[must_use]
pub fn from_fnf(fnf: &FnfChart, side: FnfSide) -> RoxChart {
let key_count = match side {
FnfSide::Player | FnfSide::Opponent => 4,
FnfSide::Both => 8,
};
let mut chart = RoxChart::new(key_count);
// Map metadata
chart.metadata = Metadata {
title: fnf.song.song.clone(),
creator: fnf.song.player2.clone(),
difficulty_name: "Normal".to_string(),
is_coop: side == FnfSide::Both, // true for 8K coop mode
..Default::default()
};
// Track current BPM for timing points
let mut current_bpm = fnf.song.bpm;
let mut added_initial_bpm = false;
// Process each section
for section in &fnf.song.notes {
// Handle BPM changes
if section.change_bpm && section.bpm > 0.0 {
// Find the first note time in this section for the timing point
if let Some(first_note) = section.section_notes.first() {
#[allow(clippy::cast_possible_truncation)]
let time_us = (first_note.time_ms() * 1000.0) as i64;
chart
.timing_points
.push(TimingPoint::bpm(time_us, section.bpm));
current_bpm = section.bpm;
}
} else if !added_initial_bpm {
// Add initial BPM at time 0
chart.timing_points.push(TimingPoint::bpm(0, current_bpm));
added_initial_bpm = true;
}
// Process notes in this section
for fnf_note in §ion.section_notes {
let raw_lane = fnf_note.lane();
// Determine if this note belongs to player or opponent
// In FNF: mustHitSection determines which side is which
// mustHitSection=true: lanes 0-3 = player, 4-7 = opponent
// mustHitSection=false: lanes 0-3 = opponent, 4-7 = player
let (is_player_note, base_lane) = if raw_lane < 4 {
(section.must_hit_section, raw_lane)
} else {
(!section.must_hit_section, raw_lane - 4)
};
// Filter based on requested side
let column = match side {
FnfSide::Player => {
if is_player_note {
Some(base_lane)
} else {
None
}
}
FnfSide::Opponent => {
if is_player_note {
None
} else {
Some(base_lane)
}
}
FnfSide::Both => {
// Opponent on left (0-3), player on right (4-7)
if is_player_note {
Some(base_lane + 4)
} else {
Some(base_lane)
}
}
};
if let Some(col) = column {
#[allow(clippy::cast_possible_truncation)]
let time_us = (fnf_note.time_ms() * 1000.0) as i64;
let note = if fnf_note.is_hold() {
#[allow(clippy::cast_possible_truncation)]
let duration_us = (fnf_note.duration_ms() * 1000.0) as i64;
Note::hold(time_us, duration_us, col)
} else {
Note::tap(time_us, col)
};
chart.notes.push(note);
}
}
}
// Add initial BPM if no sections had notes
if !added_initial_bpm {
chart.timing_points.push(TimingPoint::bpm(0, fnf.song.bpm));
}
// Sort notes and timing points by time
chart.notes.sort_by_key(|n| n.time_us);
chart.timing_points.sort_by_key(|tp| tp.time_us);
chart
}
}
impl Decoder for FnfDecoder {
/// Decode FNF chart, extracting player notes only (4K).
fn decode(data: &[u8]) -> RoxResult<RoxChart> {
Self::decode_with_side(data, FnfSide::Player)
}
}