From c83b6fcdc97a193184552b436086a76679d024ba Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 07:18:14 +0400 Subject: fix(audio): play engine loops for autonomous missiles Keep one spatial Op2 source per flight while it moves, with separate identities from manual and scene emitters. Confirm the original mode 5 phase and verify launch, overlap and teardown on the GOG AutoDemo. Flight-speed changes and a listening comparison with the original remain open. --- apps/fparkan-game/src/projectile_sound.rs | 91 ++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 14 deletions(-) (limited to 'apps/fparkan-game/src/projectile_sound.rs') diff --git a/apps/fparkan-game/src/projectile_sound.rs b/apps/fparkan-game/src/projectile_sound.rs index d8eef2ef..bdb3eed6 100644 --- a/apps/fparkan-game/src/projectile_sound.rs +++ b/apps/fparkan-game/src/projectile_sound.rs @@ -1,7 +1,7 @@ -//! Spatial Op2 engine loops owned by live player projectiles. +//! Spatial Op2 engine loops owned by live projectiles. use super::{ - audio::GameAudio, + 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}, @@ -10,7 +10,8 @@ use fparkan_fx::{ ambient_sound::{Op2SoundCommand, Op2SoundState}, clock::{FxInstanceClock, FxPhaseProvider}, }; -use fparkan_runtime::{PlayerProjectile, ProjectileAssets}; +use fparkan_runtime::{autonomous_projectile_flights, Engine, PlayerProjectile, ProjectileAssets}; +use std::collections::BTreeSet; pub(super) struct EngineSound { command: Op2SoundCommand, @@ -92,15 +93,55 @@ impl EngineSound { 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], - first_sound_id: usize, + autonomous_draws: &[Option], + 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], ) -> Result<(), String> { // Manual projectile slots keep their indices until collision or expiry. - let mut id = first_sound_id; + let mut index = 0; for draw in draws .iter_mut() .flatten() @@ -111,19 +152,14 @@ pub(super) fn update( 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(event) = instance - .flight - .as_ref() - .and_then(|flight| sound.sample(flight, audio.control_sound_is_playing(id))) - { - audio - .handle_control_sound(id, &event) - .map_err(|error| format!("play projectile engine sound: {error}"))?; + if let Some(flight) = &instance.flight { + sound.update(audio, id, flight)?; } - id += 1; + index += 1; } } } @@ -172,4 +208,31 @@ mod tests { 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]); + } + } } -- cgit v1.2.3