1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
//! 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<Op2SoundCommand> {
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<Item = &'a FxDocument>,
) -> Vec<Op2SoundCommand> {
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<usize, super::PlayerWeaponLayout>,
projectiles: &[Option<super::ProjectileDraw>],
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<Op2SoundCommand>],
projectiles: &[Option<super::ProjectileDraw>],
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);
}
}
}
|