From 5a2aa6ff6b6f4ab2480eed5be90957377d00c9e2 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 06:40:09 +0400 Subject: feat(combat): play spatial sounds for autonomous weapon shots Carry the firing effect and muzzle position on the shot event. Resolve single-gun effects by their matching muzzle point when there is no node link. Play each one-shot as its simulation frame arrives. Verified a stock gun sound enqueues a spatial voice with the flight starting position. AutoDemo ran with Vulkan validation clean and no playback errors. Comparison by ear with the original remains open. --- apps/fparkan-game/src/audio.rs | 62 +++++++++++++++++++++++++++++++++++++ apps/fparkan-game/src/main.rs | 32 +++++++++++++------ apps/fparkan-game/src/shot_sound.rs | 46 +++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 apps/fparkan-game/src/shot_sound.rs (limited to 'apps/fparkan-game/src') diff --git a/apps/fparkan-game/src/audio.rs b/apps/fparkan-game/src/audio.rs index f70f36c2..c0af0418 100644 --- a/apps/fparkan-game/src/audio.rs +++ b/apps/fparkan-game/src/audio.rs @@ -1869,6 +1869,68 @@ mod tests { assert!(night_pools > 0, "GOG missions must exercise NIGHT pool"); } + #[test] + #[ignore = "requires installed GOG assets and a usable OS audio output"] + fn stock_autonomous_shot_starts_a_spatial_voice_at_its_muzzle() { + use fparkan_runtime::{ + autonomous_weapon_effects, create, frame, load_gameplay_mission, AutoDemoCombatEvent, + EngineServices, MissionMode, MissionRequest, MissionStartupInput, + }; + const MISSION: &str = "MISSIONS/Autodemo.00/data.tma"; + let root = std::env::var_os("FPARKAN_GAME_ROOT") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| std::path::PathBuf::from(r"C:\GOG Games\Parkan - Iron Strategy")); + let vfs: Arc = Arc::new(fparkan_vfs::DirectoryVfs::new(&root)); + let mut engine = create(EngineServices::new(vfs.clone())).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: MISSION.into(), + mode: MissionMode::AutoDemo, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let sounds: Vec<_> = autonomous_weapon_effects(&engine) + .iter() + .map(|(archive, name)| crate::shot_sound::load(&root, archive, name)) + .collect(); + for now in (0..=60_000).step_by(50) { + let frame = frame(&mut engine, now).unwrap(); + for event in &frame.autodemo_combat_events { + let AutoDemoCombatEvent::Shot { + effect: Some(effect), + muzzle_bits, + .. + } = *event + else { + continue; + }; + if sounds[effect].is_none() { + continue; + } + let mut audio = GameAudio::new(vfs, MISSION).expect("open game audio"); + audio.set_paused(true); + crate::shot_sound::play_autonomous( + &sounds, + std::slice::from_ref(event), + &mut audio, + ); + let voice = audio.one_shots.last().expect("queued spatial weapon sound"); + assert_eq!( + voice.player.state.lock().unwrap().emitter_position, + muzzle_bits.map(f32::from_bits) + ); + return; + } + } + panic!("no stock autonomous shot selected a supported sound effect"); + } + #[test] #[ignore = "requires installed GOG assets and a usable OS audio output"] fn starts_pauses_and_drops_gog_audio() { diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index 8699d98e..6b337fc8 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -15,6 +15,7 @@ mod mission_pause; mod player_collision; mod projectile_render; mod projectile_trail; +mod shot_sound; use projectile_render::{prepare_projectile_draw, ProjectileDraw}; mod script_coverage; mod sun_visibility; @@ -9630,15 +9631,10 @@ fn run_static_vulkan_mode( pools.len() - 1 }); pools[pool].2.push(weapon.interval_ms()); - let sound = weapon.effect.as_ref().and_then(|(archive, name)| { - let bytes = - load_resource_entry_bytes_from_root(root, archive, name).ok()?; - let effect = decode_fxid(bytes).ok()?; - effect - .commands() - .iter() - .find_map(Op2SoundCommand::from_command) - }); + let sound = weapon + .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={}", @@ -9690,6 +9686,12 @@ fn run_static_vulkan_mode( } }).collect() }); + let autonomous_shot_sounds = mission_engine.as_ref().map_or_else(Vec::new, |engine| { + fparkan_runtime::autonomous_weapon_effects(engine) + .iter() + .map(|(archive, name)| shot_sound::load(root, archive, name)) + .collect() + }); #[cfg(windows)] let mission_hints = if mission_engine.is_some() && demo_camera.is_none() { mission_hints::MissionHints::load(root, mission, &mut preview) @@ -9746,6 +9748,7 @@ fn run_static_vulkan_mode( } app.projectile_draws = projectile_draws; app.autodemo_draws = autodemo_draws; + app.autonomous_shot_sounds = autonomous_shot_sounds; if let Some(engine) = &app.mission_engine { app.mission_render_roots = loaded_mission_object_drafts(engine) .unwrap_or_default() @@ -9923,6 +9926,7 @@ struct StaticVulkanApp { player_weapon_banks: HashMap, projectile_draws: Vec>, autodemo_draws: Vec>, + autonomous_shot_sounds: Vec>, player_fire_held: bool, player_fire_pressed: bool, player_capture_pressed: bool, @@ -10068,6 +10072,7 @@ impl StaticVulkanApp { player_weapon_banks: HashMap::new(), projectile_draws: Vec::new(), autodemo_draws: Vec::new(), + autonomous_shot_sounds: Vec::new(), player_fire_held: false, player_fire_pressed: false, player_capture_pressed: false, @@ -10671,7 +10676,7 @@ impl StaticVulkanApp { for event in &frame.autodemo_combat_events { use fparkan_runtime::AutoDemoCombatEvent::{BuildingHit, Destroyed, Shot}; match *event { - Shot { shooter, target, projectile } => eprintln!( + Shot { shooter, target, projectile, .. } => eprintln!( "{combat}_shot shooter={} target={} projectile={projectile} queue_time_ms={clock_ms}", shooter.slot, target.slot ), Destroyed { shooter, target } => eprintln!( @@ -11273,6 +11278,13 @@ impl StaticVulkanApp { { self.present_mission_notice(notice, control_clock_ms); } + if let Some(audio) = self.audio.as_mut() { + shot_sound::play_autonomous( + &self.autonomous_shot_sounds, + &frame.autodemo_combat_events, + audio, + ); + } self.mission_frame = Some(frame); } if self.mission_engine.is_some() { diff --git a/apps/fparkan-game/src/shot_sound.rs b/apps/fparkan-game/src/shot_sound.rs new file mode 100644 index 00000000..12206a32 --- /dev/null +++ b/apps/fparkan-game/src/shot_sound.rs @@ -0,0 +1,46 @@ +//! Load the spatial one-shot from a weapon's firing effect. + +use crate::audio::GameAudio; +use fparkan_assets::load_resource_entry_bytes_from_root; +use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid}; +use fparkan_runtime::AutoDemoCombatEvent; +use std::path::Path; + +pub(super) fn load(root: &Path, archive: &str, name: &str) -> Option { + let bytes = load_resource_entry_bytes_from_root(root, archive, name).ok()?; + let effect = decode_fxid(bytes).ok()?; + effect + .commands() + .iter() + .find_map(Op2SoundCommand::from_command) +} + +pub(super) fn play_autonomous( + sounds: &[Option], + events: &[AutoDemoCombatEvent], + audio: &mut GameAudio, +) { + for event in events { + let AutoDemoCombatEvent::Shot { + effect: Some(effect), + muzzle_bits, + .. + } = *event + else { + continue; + }; + let Some(sound) = sounds + .get(effect) + .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 + ); + } + } +} -- cgit v1.2.3