//! 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, FxDocument}; 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 projectile_launch<'a>( effects: impl Iterator, ) -> Vec { effects .filter(|effect| { let header = effect.header(); // The rocket launch flags leave the Op2 clock unchanged. header.time_mode == 1 && matches!(header.flags, 0 | 0x6) && 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, projectiles: &[Option], control_sounds: &[super::ControlSoundSlot], ) { 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()); let has_node_sound = weapon .windup_sounds(object) .chain(weapon.node_sounds(object)) .any(|effect| { let key = super::ControlEffectInstanceKey::from(effect); control_sounds.iter().any(|slot| { slot.effect.random_instance_key == key && (slot.waits_for_windup || slot.follows_weapon_phase) }) }); if sound.is_none() && !has_launch_sound && !has_node_sound { eprintln!( "sim_coverage player_weapon_sound_stub=1 object={object} projectile={}", weapon.projectile ); } } } } pub(super) fn play_autonomous( sounds: &[Option], projectiles: &[Option], events: &[AutoDemoCombatEvent], audio: &mut GameAudio, ) { for event in events { let AutoDemoCombatEvent::Shot { effect, projectile, muzzle_bits, .. } = *event else { continue; }; 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) .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_heavy_flamer_crosses_its_sound_threshold_before_release() { use fparkan_fx::ambient_sound::Op2SoundState; use fparkan_runtime::{ load_gameplay_mission, loaded_mission_assets, prepared_warbot_weapons, MissionMode, MissionRequest, MissionStartupInput, }; let root = PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap()); let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root)))).unwrap(); load_gameplay_mission( &mut engine, MissionRequest { key: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.02/data.tma".into(), mode: MissionMode::SingleMission, }, MissionStartupInput { queue_time_ms: 0, research_creation_gate: false, }, None, ) .unwrap(); let (object, mut weapons) = prepared_warbot_weapons(&engine) .find(|(_, bank)| { bank.weapons() .first() .is_some_and(|weapon| weapon.projectile == "bf_b_01") }) .unwrap(); let link = weapons.weapons()[0] .node_sounds(object) .next() .expect("barrel sound row"); let assets = loaded_mission_assets(&engine).unwrap(); let control = assets.object_component_controls[object][link.component_index] .as_ref() .unwrap(); let row = control .weapon_node_sound_rows() .find(|row| (row.table_index, row.row_index) == (link.table_index, link.row_index)) .unwrap(); assert_eq!(row.point_offsets[0], 3); let archive = crate::fixed_effect_field(&row.resource.archive); let name = crate::fixed_effect_field(&row.resource.name); let sound = load(&root, &archive, &name).unwrap(); let mut state = Op2SoundState::default(); let identity = [ [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], ]; weapons.select(1); for now in [0, 2, 3, 17] { assert!(weapons.fire(true, now).next().is_none()); let phase = weapons .firing_effect_phases(object) .find_map(|(row, phase)| (row == link).then_some(phase)) .unwrap(); let event = sound.update(&mut state, phase, identity, false); assert_eq!( event.map(|event| event.name), (now == 3).then(|| "gun_flamer_03.wav".into()) ); } } #[test] #[ignore = "requires licensed projectile and effect resources"] fn licensed_missiles_prepare_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(); for (projectile, expected) in [ ("bm_h_01", "H_fire_missile.wav"), ("br_l_01", "gun_mis.wav"), ("br_b_01", "gun_mis.wav"), ] { let missile = load_player_projectile(&engine, "objects.rlb", projectile).unwrap(); let effects = crate::projectile_render::initial_effects(&root, &missile, projectile); let sounds = projectile_launch(effects.iter().map(|effect| &effect.document)); let [sound] = sounds.as_slice() else { panic!("expected one launch sound for {projectile}, 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, expected); } } }