diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 19:02:16 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 19:02:16 +0300 |
| commit | 4367287f9e07aca6a173a5eaef641a75918d66e0 (patch) | |
| tree | 84aaf51a48c743b5a5d9fc85a1f52a166a2fcb02 /crates | |
| parent | b12a11f00cbd04c053bb6979c0534708d206c46a (diff) | |
| download | fparkan-4367287f9e07aca6a173a5eaef641a75918d66e0.tar.xz fparkan-4367287f9e07aca6a173a5eaef641a75918d66e0.zip | |
fix(audio): start node-attached laser sounds during wind-up
Use the weapon descriptor's Control group to select the mode-1
sound rows. Update them through the existing FX clock and spatial
Op2 playback for both manual and autonomous weapons.
The GOG Control/Effect entry points confirm the selector, epoch and
phase threshold, including retriggering after a sample at the exact
phase peak. The stock Two Fortresses helicopter started the original
sound at both weapon nodes in a Vulkan window.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 55 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 9 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 49 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 45 |
4 files changed, 154 insertions, 4 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 73baa707..186285f0 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -1055,10 +1055,46 @@ pub struct PreparedComponentControl { pub initial_effect_group: Option<usize>, /// Node-to-effect links from Control opcode 14, resolved within each group. pub node_effect_links: Vec<(usize, u32, u32)>, + /// Changes to the clocks of existing effect instances, from opcode 10. + pub effect_modes: Vec<PreparedControlEffectMode>, /// Component-local points selected for parsed effect rows, when present. pub point_list: Option<PreparedControlPointList>, } +/// An opcode 10 command in a local Control effect group. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PreparedControlEffectMode { + pub table_index: usize, + pub raw_condition: [u32; 3], + pub selector: u32, + pub mode: u32, +} + +impl PreparedComponentControl { + /// Node-attached sounds whose clocks enter mode 1 at weapon wind-up. + pub fn weapon_windup_sound_rows( + &self, + record: &PreparedControlTypedRecord, + ) -> impl Iterator<Item = &PreparedControlEffectRow> { + let group = (record.tag == 2).then_some(record.fixed_words[3]); + let rows = match &self.effect_section { + PreparedControlEffectSection::Parsed(rows) => rows.as_slice(), + _ => &[], + }; + rows.iter().filter(move |row| { + row.opcode == 3 + && self.initial_effect_group == Some(row.table_index) + && row.raw_condition[0] & 0xf000_0000 == 0 + && self.effect_modes.iter().any(|change| { + Some(change.table_index as u32) == group + && change.raw_condition[0] & 0xf000_0000 == 0 + && change.selector == row.selector + && change.mode == 1 + }) + }) + } +} + /// Component-local BAS geometry loaded from an explicitly referenced BAS /// resource. This remains aligned with each component instance, not the shared /// model cache. @@ -3903,6 +3939,7 @@ struct DecodedComponentEffectSection { initial_group_opcodes: Option<Vec<u32>>, typed_records: Vec<PreparedControlTypedRecord>, node_effect_links: Vec<(usize, u32, u32)>, + effect_modes: Vec<PreparedControlEffectMode>, } fn decode_component_effect_section<F>( @@ -3929,6 +3966,7 @@ where initial_group_opcodes: (cursor == payload.len()).then(Vec::new), typed_records: Vec::new(), node_effect_links: Vec::new(), + effect_modes: Vec::new(), }); } if typed_record_count > CONTROL_TYPED_RECORD_LIMIT { @@ -3952,6 +3990,7 @@ where initial_group_opcodes: None, typed_records, node_effect_links: Vec::new(), + effect_modes: Vec::new(), }); } @@ -4054,6 +4093,7 @@ where initial_group_opcodes: complete.then(Vec::new), typed_records, node_effect_links: Vec::new(), + effect_modes: Vec::new(), }); } @@ -4074,6 +4114,7 @@ where let mut initial_group_opcodes = Some(Vec::new()); let mut effect_rows = Vec::new(); let mut node_effect_links = Vec::new(); + let mut effect_modes = Vec::new(); for table_index in 0..effect_group_count { let row_count = usize::try_from( control_u32_at(payload, cursor) @@ -4111,7 +4152,7 @@ where .expect("valid initial group has an opcode list") .push(opcode); } - if !matches!(opcode, 3 | 4) { + if !matches!(opcode, 3 | 4 | 10) { continue; } let raw_condition = [ @@ -4119,6 +4160,15 @@ where control_u32_at(row, 4).expect("fixed effect row has condition word 1"), control_u32_at(row, 8).expect("fixed effect row has condition word 2"), ]; + if opcode == 10 { + effect_modes.push(PreparedControlEffectMode { + table_index, + raw_condition, + selector: control_u32_at(row, 0x10).expect("fixed effect row has a selector"), + mode: control_u32_at(row, 0x14).expect("fixed effect row has a time mode"), + }); + continue; + } let point_offsets = [ control_u32_at(row, 0x10).expect("fixed effect row has point offset 0"), control_u32_at(row, 0x14).expect("fixed effect row has point offset 1"), @@ -4154,6 +4204,7 @@ where initial_group_opcodes: None, typed_records, node_effect_links: Vec::new(), + effect_modes: Vec::new(), }); } Ok(DecodedComponentEffectSection { @@ -4162,6 +4213,7 @@ where initial_group_opcodes, typed_records, node_effect_links, + effect_modes, }) } @@ -4342,6 +4394,7 @@ fn decode_component_control_payload_with_opcodes( effect_section: decoded_effect.section, initial_effect_group: decoded_effect.initial_effect_group, node_effect_links: decoded_effect.node_effect_links, + effect_modes: decoded_effect.effect_modes, point_list: None, }, initial_group_opcodes, diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 1a32d6cd..02333bd0 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -3253,7 +3253,9 @@ pub fn simulate_mission_report( AutoDemoCombatEvent::Destroyed { .. } => { report.autodemo_first_destroyed_ms.get_or_insert(clock_ms) } - AutoDemoCombatEvent::BuildingHit { .. } => continue, + AutoDemoCombatEvent::BuildingHit { .. } | AutoDemoCombatEvent::WindupSound(_) => { + continue + } }; } report.mission_messages += frame @@ -8827,6 +8829,7 @@ mod tests { effect_section: PreparedControlEffectSection::Parsed(rows), initial_effect_group, node_effect_links: Vec::new(), + effect_modes: Vec::new(), point_list: point_count.map(|count| PreparedControlPointList { source: synthetic_resource_key(941), points: (0..count) @@ -9332,6 +9335,7 @@ mod tests { effect_section, initial_effect_group: None, node_effect_links: Vec::new(), + effect_modes: Vec::new(), point_list: None, } } @@ -16678,7 +16682,8 @@ mod tests { .any(|(handle, _)| *handle == target)); first_destroyed.get_or_insert(now); } - AutoDemoCombatEvent::BuildingHit { .. } => {} + AutoDemoCombatEvent::BuildingHit { .. } + | AutoDemoCombatEvent::WindupSound(_) => {} } } for replacement in result.autodemo_reinforcements { diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index cfec0524..0ab0613f 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -1,4 +1,4 @@ -use crate::{Engine, StartingCockpit}; +use crate::{Engine, FiringEffectInstance, StartingCockpit}; use fparkan_assets::{ PreparedComponentControl, PreparedControlEffectRow, PreparedControlEffectSection, PreparedControlPoint, PreparedControlTimelineRow, @@ -29,6 +29,8 @@ pub struct PlayerWeapon { /// Optional type-4 effect started by this weapon. pub effect: Option<(String, String)>, pub(super) effect_row: Option<(usize, usize)>, + windup_sound_rows: Vec<(usize, usize)>, + windup_started: bool, barrels: Vec<Barrel>, barrel_index: usize, rounds_remaining: Option<u32>, @@ -77,6 +79,17 @@ impl PlayerWeapons { &self.weapons } + /// Control sound instances whose wind-up began on this sample. + pub fn started_windup_sounds( + &self, + object_index: usize, + ) -> impl Iterator<Item = FiringEffectInstance> + '_ { + self.weapons + .iter() + .filter(|weapon| weapon.windup_started) + .flat_map(move |weapon| weapon.windup_sounds(object_index)) + } + /// Samples the barrel tracks after advancing the weapon bank on this frame. pub fn apply_firing_pose(&self, component: usize, node_times: &mut [Option<f32>]) { for weapon in self @@ -155,6 +168,7 @@ impl PlayerWeapons { impl PlayerWeapon { fn sample_fire(&mut self, trigger: bool, now: f64) -> Option<usize> { let mut shot = None; + self.windup_started = false; while now > self.phase_end_ms || self.phase == FirePhase::Idle { // shortcut: leave a second overdue shot pending until the next simulation sample. if self.phase == FirePhase::Shot && shot.is_some() { @@ -186,6 +200,7 @@ impl PlayerWeapon { break; } self.phase = FirePhase::Shot; + self.windup_started = true; barrel.travel_to(0.5) } FirePhase::Shot => { @@ -215,6 +230,21 @@ impl PlayerWeapon { shot } + /// Prepared node-attached sound rows belonging to this device. + pub fn windup_sounds( + &self, + object_index: usize, + ) -> impl Iterator<Item = FiringEffectInstance> + '_ { + self.windup_sound_rows + .iter() + .map(move |&(table_index, row_index)| FiringEffectInstance { + object_index, + component_index: self.component_index, + table_index, + row_index, + }) + } + /// Remaining shots, or `None` for an unlimited weapon. pub fn rounds_remaining(&self) -> Option<u32> { self.rounds_remaining @@ -360,6 +390,11 @@ fn prepare_weapons( projectile_archive, effect, effect_row: effect_row.map(|row| (row.table_index, row.row_index)), + windup_sound_rows: control + .weapon_windup_sound_rows(record) + .map(|row| (row.table_index, row.row_index)) + .collect(), + windup_started: false, barrels, barrel_index: 0, rounds_remaining, @@ -443,6 +478,8 @@ mod tests { projectile_archive: String::from("test.rlb"), effect: None, effect_row: None, + windup_sound_rows: vec![(0, 1)], + windup_started: false, barrels: vec![Barrel { muzzle: PreparedControlPoint { raw_flags: 0, @@ -482,7 +519,17 @@ mod tests { }); let mut node_times = [None]; assert!(weapons.fire(true, 0).next().is_none()); + assert_eq!( + weapons.started_windup_sounds(7).collect::<Vec<_>>(), + [FiringEffectInstance { + object_index: 7, + component_index: 0, + table_index: 0, + row_index: 1, + }] + ); assert!(weapons.fire(false, 5).next().is_none()); + assert!(weapons.started_windup_sounds(7).next().is_none()); weapons.apply_firing_pose(0, &mut node_times); assert_eq!(node_times, [Some(5.0)]); assert!(weapons.fire(false, 10).next().is_none()); diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index 224213bb..c43cc302 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -49,6 +49,8 @@ pub struct FiringEffectInstance { /// One autonomous combat event on the mission clock. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum AutoDemoCombatEvent { + /// The wind-up started a node-attached weapon sound. + WindupSound(FiringEffectInstance), /// A weapon released a projectile. Shot { /// Owner of the weapon. @@ -685,6 +687,11 @@ impl AutoDemoCombat { .unavailable(shooter, "projectile_launch", now, 1); } } + events.extend( + unit.weapons + .started_windup_sounds(unit.index) + .map(AutoDemoCombatEvent::WindupSound), + ); missing += due_missing; if missing > 0 { let count = self @@ -1178,6 +1185,44 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn licensed_stock_helicopter_starts_both_laser_sound_rows() { + let mut engine = stock_two_fortresses(); + let helicopter = mission_object_handle(&engine, OriginalObjectId(11)).unwrap(); + let mut first_shot = None; + let mut first_windup_by_component = [None; 2]; + + for tick in 0..=60 * 28 { + let now = tick * 1000 / 60; + let result = frame(&mut engine, now).unwrap(); + for event in result.autodemo_combat_events { + match event { + AutoDemoCombatEvent::WindupSound(effect) + if effect.object_index == 11 + && matches!(effect.component_index, 10 | 11) => + { + assert_eq!((effect.table_index, effect.row_index), (0, 1)); + first_windup_by_component[effect.component_index - 10].get_or_insert(now); + } + AutoDemoCombatEvent::Shot { shooter, .. } if shooter == helicopter => { + first_shot.get_or_insert(now); + } + _ => {} + } + } + if first_shot.is_some() && first_windup_by_component.iter().all(Option::is_some) { + break; + } + } + + let first_shot = first_shot.expect("stock helicopter fires"); + let [Some(left), Some(right)] = first_windup_by_component else { + panic!("both authored gun sounds need a wind-up, got {first_windup_by_component:?}"); + }; + assert!(left.min(right) <= first_shot); + } + + #[test] + #[ignore = "requires licensed corpus"] fn licensed_scripted_patrol_fires_without_a_guard_fallback() { let mut engine = stock_two_fortresses(); assert_eq!(mission_guard_fallback_count(&engine), 0); |
