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
|
//! Spatial Op2 engine loops owned by live projectiles.
use super::{
audio::{ControlSoundId, GameAudio},
control_matrix_from_columns, control_row_points, control_type4_effect_local_matrix,
matrix_to_row_major, multiply_row_major,
projectile_render::{flight_basis, ProjectileDraw, ProjectileEffect},
};
use fparkan_fx::{
ambient_sound::{Op2SoundCommand, Op2SoundState},
clock::{FxInstanceClock, FxPhaseProvider},
};
use fparkan_runtime::{autonomous_projectile_flights, Engine, PlayerProjectile, ProjectileAssets};
use std::collections::BTreeSet;
pub(super) struct EngineSound {
command: Op2SoundCommand,
projectile_from_effect: [[f32; 4]; 4],
phase: f32,
}
pub(super) fn prepare(
projectile: &ProjectileAssets,
effects: &[ProjectileEffect<'_>],
) -> Vec<EngineSound> {
let Some(control) = projectile.assets.object_component_controls[0][0].as_ref() else {
return Vec::new();
};
let Some(points) = &control.point_list else {
return Vec::new();
};
let mut owner = FxPhaseProvider::idle();
owner.linear_velocity = [0.0, projectile.speed, 0.0];
owner.linear_velocity_limit =
std::array::from_fn(|axis| f32::from_bits(control.native_profile_words[6 + axis]));
let mut sounds = Vec::new();
for effect in effects {
let header = effect.document.header();
if header.time_mode != 5 || header.flags != 0 {
continue;
}
let Some(mut clock) = FxInstanceClock::new(5, 0, header.duration_seconds, 0.0) else {
continue;
};
// This owner has a constant local forward velocity throughout its flight.
let Some(phase) = clock.phase(0.0, false, Some(&owner)) else {
continue;
};
let Some(anchors) = control_row_points(points, effect.row) else {
continue;
};
if anchors[0].raw_signed_field_04 != 0 {
continue;
}
let Some(projectile_from_effect) = control_type4_effect_local_matrix(anchors, header.scale)
else {
continue;
};
sounds.extend(
effect
.document
.commands()
.iter()
.filter_map(Op2SoundCommand::from_command)
.filter(Op2SoundCommand::is_looping)
.map(|command| EngineSound {
command,
projectile_from_effect,
phase: phase.clamp(0.0, 1.0),
}),
);
}
sounds
}
impl EngineSound {
fn sample(
&self,
flight: &PlayerProjectile,
playing: bool,
) -> Option<fparkan_fx::environment::SoundEvent> {
let [right, forward, up] = flight_basis(flight.direction);
let world_from_projectile =
control_matrix_from_columns(right, forward, up, flight.position);
let matrix = multiply_row_major(
matrix_to_row_major(world_from_projectile),
matrix_to_row_major(self.projectile_from_effect),
);
self.command.update(
&mut Op2SoundState::default(),
self.phase,
std::array::from_fn(|row| std::array::from_fn(|column| matrix[row * 4 + column])),
playing,
)
}
fn update(
&self,
audio: &mut GameAudio,
id: ControlSoundId,
flight: &PlayerProjectile,
) -> Result<(), String> {
if let Some(event) = self.sample(flight, audio.control_sound_is_playing(id)) {
audio
.handle_control_sound(id, &event)
.map_err(|error| format!("play projectile engine sound: {error}"))?;
}
Ok(())
}
}
pub(super) fn update(
audio: &mut GameAudio,
draws: &mut [Option<ProjectileDraw>],
autonomous_draws: &[Option<ProjectileDraw>],
engine: Option<&Engine>,
) -> Result<(), String> {
update_player(audio, draws)?;
let mut live = BTreeSet::new();
for (flight_id, projectile, flight) in
engine.into_iter().flat_map(autonomous_projectile_flights)
{
let Some(draw) = autonomous_draws.get(projectile).and_then(Option::as_ref) else {
continue;
};
for (index, sound) in draw.engine_sounds.iter().enumerate() {
live.insert(flight_id);
sound.update(
audio,
ControlSoundId::AutonomousProjectile(flight_id, index),
&flight,
)?;
}
}
audio.stop_finished_projectile_sounds(&live);
Ok(())
}
fn update_player(
audio: &mut GameAudio,
draws: &mut [Option<ProjectileDraw>],
) -> Result<(), String> {
// Manual projectile slots keep their indices until collision or expiry.
let mut index = 0;
for draw in draws
.iter_mut()
.flatten()
.filter(|draw| !draw.engine_sounds.is_empty())
{
for instance in &mut draw.instances {
let born_ms = instance.flight.as_ref().map(PlayerProjectile::born_ms);
let replaced = born_ms != instance.sound_born_ms;
instance.sound_born_ms = born_ms;
for sound in &draw.engine_sounds {
let id = ControlSoundId::PlayerProjectile(index);
if replaced {
audio.stop_control_sound(id);
}
if let Some(flight) = &instance.flight {
sound.update(audio, id, flight)?;
}
index += 1;
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::projectile_render::initial_effects;
use fparkan_fx::environment::SoundAction;
use fparkan_runtime::{create, load_player_projectile, EngineServices};
use fparkan_vfs::DirectoryVfs;
use std::{path::PathBuf, sync::Arc};
#[test]
#[ignore = "requires licensed projectile and effect resources"]
fn licensed_missile_engine_loop_follows_the_tail() {
let root = PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap());
let engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root)))).unwrap();
let missile = load_player_projectile(&engine, "objects.rlb", "bm_h_01").unwrap();
let effects = initial_effects(&root, &missile, "bm_h_01");
let sounds = prepare(&missile, &effects);
let [sound] = sounds.as_slice() else {
panic!("expected one engine loop");
};
assert_eq!(sound.phase, 1.0);
let mut flight = PlayerProjectile::new(
[10.0, 20.0, 30.0],
[1.0, 0.0, 0.0],
missile.speed,
missile.lifetime_ms,
100,
)
.unwrap();
let start = sound.sample(&flight, false).unwrap();
assert_eq!(start.action, SoundAction::StartLoop);
assert_eq!(start.archive, "sounds.lib");
assert_eq!(start.name, "H_eng_missile.wav");
assert!((start.position[0] - (10.0 - 0.40465)).abs() < 0.0001);
assert_eq!(&start.position[1..], &[20.0, 30.0]);
assert!(flight.advance(200, |_| Ok(false)).unwrap());
let next = sound.sample(&flight, true).unwrap();
assert_eq!(next.action, SoundAction::SetLoopVolume);
assert!((next.position[0] - start.position[0] - 7.0).abs() < 0.0001);
assert_eq!(&next.position[1..], &[20.0, 30.0]);
}
#[test]
#[ignore = "requires licensed projectile and effect resources"]
fn licensed_warbot_missile_engines_start_in_flight() {
let root = PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap());
let engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root)))).unwrap();
for name in ["bm_l_01", "bm_m_01"] {
let missile = load_player_projectile(&engine, "objects.rlb", name).unwrap();
let effects = initial_effects(&root, &missile, name);
let sounds = prepare(&missile, &effects);
let [sound] = sounds.as_slice() else {
panic!("expected one engine loop for {name}");
};
let flight = PlayerProjectile::new(
[10.0, 20.0, 30.0],
[1.0, 0.0, 0.0],
missile.speed,
missile.lifetime_ms,
100,
)
.unwrap();
let event = sound.sample(&flight, false).unwrap();
assert_eq!(event.name, "eng_mis.wav");
assert_eq!(event.action, SoundAction::StartLoop);
assert!(event.position[0] < flight.position[0]);
}
}
}
|