diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 18:43:04 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 18:43:04 +0300 |
| commit | b12a11f00cbd04c053bb6979c0534708d206c46a (patch) | |
| tree | 0a61d49c1bef3c4cb52c3370cc1c0119df422043 /apps | |
| parent | f3f5054641a3542e3154461f39a7a204cfd095ff (diff) | |
| download | fparkan-b12a11f00cbd04c053bb6979c0534708d206c46a.tar.xz fparkan-b12a11f00cbd04c053bb6979c0534708d206c46a.zip | |
fix(audio): play projectile launch sounds
Read phase-zero one-shot sounds from the projectile's initial Control
effect group. Reuse the decoded effects for trails and play the launch
sounds when the weapon creates a projectile.
The native mode-1 phase probe and the licensed hero missile fixture
both pass. A replayed trigger at the stock Firing Line start queued
H_fire_missile.wav once and Vulkan validation was clean.
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/fparkan-game/src/audio.rs | 1 | ||||
| -rw-r--r-- | apps/fparkan-game/src/main.rs | 8 | ||||
| -rw-r--r-- | apps/fparkan-game/src/player_fire.rs | 10 | ||||
| -rw-r--r-- | apps/fparkan-game/src/projectile_render.rs | 47 | ||||
| -rw-r--r-- | apps/fparkan-game/src/projectile_trail.rs | 33 | ||||
| -rw-r--r-- | apps/fparkan-game/src/shot_sound.rs | 97 |
6 files changed, 143 insertions, 53 deletions
diff --git a/apps/fparkan-game/src/audio.rs b/apps/fparkan-game/src/audio.rs index c0af0418..ec64df1e 100644 --- a/apps/fparkan-game/src/audio.rs +++ b/apps/fparkan-game/src/audio.rs @@ -1917,6 +1917,7 @@ mod tests { audio.set_paused(true); crate::shot_sound::play_autonomous( &sounds, + &[], std::slice::from_ref(event), &mut audio, ); diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index a8f3f431..dd186207 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -9627,12 +9627,6 @@ fn run_static_vulkan_mode( .effect .as_ref() .and_then(|(archive, name)| shot_sound::load(root, archive, name)); - if sound.is_none() { - eprintln!( - "sim_coverage player_weapon_sound_stub=1 object={index} projectile={}", - weapon.projectile - ); - } (pool, sound) }) .collect(); @@ -9665,6 +9659,7 @@ fn run_static_vulkan_mode( }; projectile_draws.push(draw); } + shot_sound::report_missing_player_sounds(&player_weapon_layouts, &projectile_draws); } let autodemo_draws = mission_engine.as_ref().map_or_else(Vec::new, |engine| { fparkan_runtime::autodemo_projectile_types(engine).iter().map(|kind| { @@ -11242,6 +11237,7 @@ impl StaticVulkanApp { } shot_sound::play_autonomous( &self.autonomous_shot_sounds, + &self.autodemo_draws, &frame.autodemo_combat_events, audio, ); diff --git a/apps/fparkan-game/src/player_fire.rs b/apps/fparkan-game/src/player_fire.rs index dfb0f8d6..870dfe2f 100644 --- a/apps/fparkan-game/src/player_fire.rs +++ b/apps/fparkan-game/src/player_fire.rs @@ -105,6 +105,7 @@ impl StaticVulkanApp { weapon.rounds_remaining(), ); let (pool, sound) = &layout.slots[slot]; + let mut launch_sounds = &[][..]; if let Some(draw) = self.projectile_draws[*pool].as_mut() { if let Some(instance) = draw .instances @@ -120,6 +121,7 @@ impl StaticVulkanApp { ); instance.shooter = Some(cockpit.handle); if let Some(flight) = &instance.flight { + launch_sounds = &draw.launch_sounds; eprintln!("player_projectile_spawn slot={} queue_time_ms={clock_ms} speed={} lifetime_ms={} position={muzzle:?} direction={:?}", slot + 1, draw.speed, draw.lifetime_ms, flight.direction); } } else { @@ -129,8 +131,12 @@ impl StaticVulkanApp { ); } } - if let (Some(sound), Some(audio)) = (sound, self.audio.as_mut()) { - if let Some(event) = sound.one_shot_at(muzzle) { + if let Some(audio) = self.audio.as_mut() { + for event in sound + .iter() + .chain(launch_sounds) + .filter_map(|sound| sound.one_shot_at(muzzle)) + { match audio.handle_sound_event(&event) { Ok(()) => eprintln!( "player_shot_sound slot={} resource={}:{} queue_time_ms={clock_ms}", diff --git a/apps/fparkan-game/src/projectile_render.rs b/apps/fparkan-game/src/projectile_render.rs index 7d1b9842..866d8c5b 100644 --- a/apps/fparkan-game/src/projectile_render.rs +++ b/apps/fparkan-game/src/projectile_render.rs @@ -1,9 +1,11 @@ //! Prepare and populate the shared projectile body and trail draw pools. use super::{ - append_static_preview_component, projectile_trail, static_preview_component_materials, - PreparedEffectMaterial, StaticPreviewScene, + append_static_preview_component, fixed_effect_field, projectile_trail, shot_sound, + static_preview_component_materials, PreparedEffectMaterial, StaticPreviewScene, }; +use fparkan_assets::{load_resource_entry_bytes_from_root, PreparedControlEffectSection}; +use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid, FxDocument}; use fparkan_render::LegacyIron3dEulerTransform; use fparkan_render_vulkan::{ project_msh_to_static_mesh_in_world_space_with_node_fallback_poses, VulkanStaticMesh, @@ -11,7 +13,7 @@ use fparkan_render_vulkan::{ }; use fparkan_runtime::{ autodemo_projectiles, autonomous_projectile_segments, load_player_projectile, Engine, - PlayerProjectile, + PlayerProjectile, ProjectileAssets, }; use fparkan_world::ObjectHandle; use std::{collections::HashMap, ops::Range, path::Path}; @@ -20,6 +22,7 @@ pub(super) struct ProjectileDraw { pub(super) speed: f32, pub(super) lifetime_ms: u32, pub(super) volume: Option<f32>, + pub(super) launch_sounds: Vec<Op2SoundCommand>, pub(super) local_vertices: Vec<VulkanStaticVertex>, pub(super) instances: Vec<ProjectileDrawSlot>, } @@ -67,7 +70,8 @@ pub(super) fn prepare_projectile_draw( draw_ranges: Vec::new(), } }; - let layers = projectile_trail::prepare(root, &projectile, name, preview, trail_materials); + let effects = initial_effects(root, &projectile, name); + let layers = projectile_trail::prepare(root, &effects, name, preview, trail_materials); if geometry.draw_ranges.is_empty() && layers.is_empty() { return Ok(None); } @@ -108,11 +112,46 @@ pub(super) fn prepare_projectile_draw( speed: projectile.speed, lifetime_ms: projectile.lifetime_ms, volume: projectile.volume, + launch_sounds: shot_sound::projectile_launch(effects.iter().map(|(_, effect)| effect)), local_vertices: geometry.vertices, instances, })) } +pub(super) fn initial_effects( + root: &Path, + projectile: &ProjectileAssets, + name: &str, +) -> Vec<(String, FxDocument)> { + let Some(control) = projectile.assets.object_component_controls[0][0].as_ref() else { + return Vec::new(); + }; + let PreparedControlEffectSection::Parsed(rows) = &control.effect_section else { + return Vec::new(); + }; + rows.iter() + .filter(|row| { + row.opcode == 4 + && Some(row.table_index) == control.initial_effect_group + && row.raw_condition == [0; 3] + }) + .filter_map(|row| { + let archive = fixed_effect_field(&row.resource.archive); + let fx = fixed_effect_field(&row.resource.name); + let effect = load_resource_entry_bytes_from_root(root, &archive, &fx) + .map_err(|error| error.to_string()) + .and_then(|bytes| decode_fxid(bytes).map_err(|error| error.to_string())); + match effect { + Ok(effect) => Some((format!("{archive}/{fx}"), effect)), + Err(_) => { + eprintln!("sim_coverage projectile_trail_resource_stub=1 projectile={name} effect={archive}/{fx}"); + None + } + } + }) + .collect() +} + pub(super) fn sample_autonomous(draws: &mut [Option<ProjectileDraw>], engine: &Engine) { for instance in draws .iter_mut() diff --git a/apps/fparkan-game/src/projectile_trail.rs b/apps/fparkan-game/src/projectile_trail.rs index 54445ac5..bcadb7ba 100644 --- a/apps/fparkan-game/src/projectile_trail.rs +++ b/apps/fparkan-game/src/projectile_trail.rs @@ -1,11 +1,9 @@ use super::{ ambient_effect_pipeline_state, append_environment_range, environment_vertex, - fixed_effect_field, prepare_effect_material, quad_indices, EnvironmentGpuRange, - PreparedEffectMaterial, StaticPreviewScene, + prepare_effect_material, quad_indices, EnvironmentGpuRange, PreparedEffectMaterial, + StaticPreviewScene, }; -use fparkan_assets::{load_resource_entry_bytes_from_root, PreparedControlEffectSection}; -use fparkan_fx::{decode_fxid, op5::TrailLayer, FxOpcode}; -use fparkan_runtime::ProjectileAssets; +use fparkan_fx::{op5::TrailLayer, FxDocument, FxOpcode}; use std::{collections::HashMap, path::Path}; pub(super) struct ProjectileTrail { @@ -16,32 +14,13 @@ pub(super) struct ProjectileTrail { pub(super) fn prepare( root: &Path, - projectile: &ProjectileAssets, + effects: &[(String, FxDocument)], name: &str, preview: &mut StaticPreviewScene, cache: &mut HashMap<String, PreparedEffectMaterial>, ) -> Vec<(TrailLayer, usize, fparkan_render::LegacyPipelineState)> { - let Some(control) = projectile.assets.object_component_controls[0][0].as_ref() else { - return Vec::new(); - }; - let PreparedControlEffectSection::Parsed(rows) = &control.effect_section else { - return Vec::new(); - }; let mut layers = Vec::new(); - for row in rows.iter().filter(|row| { - row.opcode == 4 - && Some(row.table_index) == control.initial_effect_group - && row.raw_condition == [0; 3] - }) { - let archive = fixed_effect_field(&row.resource.archive); - let fx = fixed_effect_field(&row.resource.name); - let Ok(document) = load_resource_entry_bytes_from_root(root, &archive, &fx) - .map_err(|e| e.to_string()) - .and_then(|bytes| decode_fxid(bytes).map_err(|e| e.to_string())) - else { - eprintln!("sim_coverage projectile_trail_resource_stub=1 projectile={name} effect={archive}/{fx}"); - continue; - }; + for (effect, document) in effects { let header = document.header(); let [scale, sy, sz] = header.scale; for command in document @@ -72,7 +51,7 @@ pub(super) fn prepare( if let Some(prepared) = prepared { layers.push(prepared); } else { - eprintln!("sim_coverage projectile_trail_command_stub=1 projectile={name} effect={archive}/{fx}"); + eprintln!("sim_coverage projectile_trail_command_stub=1 projectile={name} effect={effect}"); } } } diff --git a/apps/fparkan-game/src/shot_sound.rs b/apps/fparkan-game/src/shot_sound.rs index 12206a32..893b3a14 100644 --- a/apps/fparkan-game/src/shot_sound.rs +++ b/apps/fparkan-game/src/shot_sound.rs @@ -1,8 +1,8 @@ -//! Load the spatial one-shot from a weapon's firing effect. +//! Spatial one-shots from weapons and projectile startup effects. use crate::audio::GameAudio; use fparkan_assets::load_resource_entry_bytes_from_root; -use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid}; +use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid, FxDocument}; use fparkan_runtime::AutoDemoCombatEvent; use std::path::Path; @@ -15,32 +15,101 @@ pub(super) fn load(root: &Path, archive: &str, name: &str) -> Option<Op2SoundCom .find_map(Op2SoundCommand::from_command) } +pub(super) fn projectile_launch<'a>( + effects: impl Iterator<Item = &'a FxDocument>, +) -> Vec<Op2SoundCommand> { + effects + .filter(|effect| { + let header = effect.header(); + header.time_mode == 1 && header.flags == 0 && header.phase_jitter == 0.0 + }) + .flat_map(FxDocument::commands) + .filter_map(Op2SoundCommand::from_command) + .filter(Op2SoundCommand::starts_at_spawn) + .collect() +} + +pub(super) fn report_missing_player_sounds( + layouts: &std::collections::HashMap<usize, super::PlayerWeaponLayout>, + projectiles: &[Option<super::ProjectileDraw>], +) { + for (&object, layout) in layouts { + for (weapon, (pool, sound)) in layout.weapons.weapons().iter().zip(&layout.slots) { + let has_launch_sound = projectiles + .get(*pool) + .and_then(Option::as_ref) + .is_some_and(|draw| !draw.launch_sounds.is_empty()); + if sound.is_none() && !has_launch_sound { + eprintln!( + "sim_coverage player_weapon_sound_stub=1 object={object} projectile={}", + weapon.projectile + ); + } + } + } +} + pub(super) fn play_autonomous( sounds: &[Option<Op2SoundCommand>], + projectiles: &[Option<super::ProjectileDraw>], events: &[AutoDemoCombatEvent], audio: &mut GameAudio, ) { for event in events { let AutoDemoCombatEvent::Shot { - effect: Some(effect), + effect, + projectile, muzzle_bits, .. } = *event else { continue; }; - let Some(sound) = sounds - .get(effect) + let weapon_sound = effect + .and_then(|effect| sounds.get(effect)) + .and_then(Option::as_ref); + let launch_sounds = projectiles + .get(projectile) .and_then(Option::as_ref) - .and_then(|command| command.one_shot_at(muzzle_bits.map(f32::from_bits))) - else { - continue; - }; - if let Err(error) = audio.handle_sound_event(&sound) { - eprintln!( - "sim_coverage autonomous_weapon_sound_stub=1 effect={}:{} reason={error}", - sound.archive, sound.name - ); + .map_or(&[][..], |draw| draw.launch_sounds.as_slice()); + let position = muzzle_bits.map(f32::from_bits); + for sound in weapon_sound + .into_iter() + .chain(launch_sounds) + .filter_map(|sound| sound.one_shot_at(position)) + { + if let Err(error) = audio.handle_sound_event(&sound) { + eprintln!( + "sim_coverage autonomous_weapon_sound_stub=1 effect={}:{} reason={error}", + sound.archive, sound.name + ); + } } } } + +#[cfg(test)] +mod tests { + use super::*; + 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_hero_missile_prepares_launch_without_the_engine_loop() { + 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 = crate::projectile_render::initial_effects(&root, &missile, "bm_h_01"); + let sounds = projectile_launch(effects.iter().map(|(_, effect)| effect)); + let [sound] = sounds.as_slice() else { + panic!("expected the single launch sound, got {sounds:?}"); + }; + let position = [10.0, 20.0, 30.0]; + let event = sound.one_shot_at(position).unwrap(); + assert_eq!(event.position, position); + assert_eq!(event.archive, "sounds.lib"); + assert_eq!(event.name, "H_fire_missile.wav"); + } +} |
