diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 04:56:36 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 04:56:36 +0300 |
| commit | badbf2e74fceecadba1487b0863825e180e61c85 (patch) | |
| tree | b96583bb8cc39c3e367b891b95356ec8b495ffb7 /apps | |
| parent | 796a64e61643936e3b4517eb365e95639e68aaa5 (diff) | |
| download | fparkan-badbf2e74fceecadba1487b0863825e180e61c85.tar.xz fparkan-badbf2e74fceecadba1487b0863825e180e61c85.zip | |
feat(runtime): trigger tutorial voices from tactical areas
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/fparkan-game/src/audio.rs | 107 | ||||
| -rw-r--r-- | apps/fparkan-game/src/main.rs | 14 |
2 files changed, 118 insertions, 3 deletions
diff --git a/apps/fparkan-game/src/audio.rs b/apps/fparkan-game/src/audio.rs index 0d6a34f5..5f7001d4 100644 --- a/apps/fparkan-game/src/audio.rs +++ b/apps/fparkan-game/src/audio.rs @@ -628,6 +628,8 @@ pub(crate) struct GameAudio { ambient: Player, music: Player, variations: Player, + mission_voice: Player, + mission_cfg: NormalizedPath, weather_loop: NativeSpatialPlayer, control_sounds: BTreeMap<usize, NativeSpatialPlayer>, one_shots: Vec<SpatialOneShot>, @@ -663,6 +665,7 @@ impl GameAudio { let ambient = Player::connect_new(stream.mixer()); let music = Player::connect_new(stream.mixer()); let variations = Player::connect_new(stream.mixer()); + let mission_voice = Player::connect_new(stream.mixer()); let listener = Listener::default(); let (left, right) = ear_positions(listener)?; let weather_loop = @@ -672,6 +675,8 @@ impl GameAudio { ambient, music, variations, + mission_voice, + mission_cfg, weather_loop, control_sounds: BTreeMap::new(), one_shots: Vec::new(), @@ -918,7 +923,12 @@ impl GameAudio { /// Pauses or resumes every live audio source without changing selection. pub(crate) fn set_paused(&mut self, paused: bool) { self.paused = paused; - for player in [&self.ambient, &self.music, &self.variations] { + for player in [ + &self.ambient, + &self.music, + &self.variations, + &self.mission_voice, + ] { if paused { player.pause(); } else { @@ -951,6 +961,7 @@ impl GameAudio { self.ambient.stop(); self.music.stop(); self.variations.stop(); + self.mission_voice.stop(); self.weather_loop.stop(); for player in self.control_sounds.values() { player.stop(); @@ -960,6 +971,44 @@ impl GameAudio { } } + /// Queues a mission voice by its index in the map's message definitions. + pub(crate) fn queue_mission_voice(&mut self, index: u32) -> Result<String, String> { + let mission_cfg = self.mission_cfg.as_str(); + let messages = mission_cfg.rsplit_once('/').map_or_else( + || "messages.cfg".to_owned(), + |(directory, _)| format!("{directory}/messages.cfg"), + ); + let messages = normalize_relative(messages.as_bytes(), PathPolicy::StrictLegacy) + .map_err(|error| error.to_string())?; + let bytes = self + .library + .vfs + .read(&messages) + .map_err(|error| error.to_string())?; + let symbol = message_voice_symbol(&bytes, index)?.ok_or_else(|| { + format!( + "voice for index {index} is missing in {}", + messages.as_str() + ) + })?; + let mission = self + .library + .vfs + .read(&self.mission_cfg) + .map_err(|error| error.to_string())?; + let (archive, file) = voice_resource(&mission, &symbol)? + .ok_or_else(|| format!("voice resource {symbol} is missing in {mission_cfg}"))?; + let asset = self + .dynamic_sound(&archive, &file) + .map_err(|error| error.to_string())?; + self.mission_voice + .append(decoder_for(&asset).map_err(|error| error.to_string())?); + if !self.paused { + self.mission_voice.play(); + } + Ok(asset.resource_name()) + } + fn start_theme(&self) -> Result<(), AudioError> { self.ambient .append(decoder_for(&self.library.theme)?.repeat_infinite()); @@ -1394,6 +1443,50 @@ fn parse_mission_audio_config(bytes: &[u8]) -> Result<MissionAudioConfig, String }) } +fn config_objects(text: &str) -> impl Iterator<Item = BTreeMap<String, &str>> { + text.lines() + .map(|line| line.split('#').next().unwrap_or_default().trim()) + .scan(None, |object, line| { + let kind = line.split_whitespace().next().unwrap_or_default(); + if kind.eq_ignore_ascii_case("object") { + *object = Some(BTreeMap::new()); + } else if kind.eq_ignore_ascii_case("end") { + return Some(object.take()); + } else if let (Some(fields), Some((key, value))) = (object, line.split_once('=')) { + fields.insert(key.trim().to_ascii_uppercase(), value.trim()); + } + Some(None) + }) + .flatten() +} + +fn message_voice_symbol(bytes: &[u8], index: u32) -> Result<Option<String>, String> { + for object in config_objects(&String::from_utf8_lossy(bytes)) { + if object + .get("MESSAGE_INDEX") + .and_then(|index| parse_cfg_value(index).ok()) + .and_then(|value| value.parse::<u32>().ok()) + == Some(index) + { + return object + .get("VOICE_RESOURCE") + .map(|value| parse_cfg_value(value)) + .transpose(); + } + } + Ok(None) +} + +fn voice_resource(bytes: &[u8], symbol: &str) -> Result<Option<(String, String)>, String> { + let symbol = symbol.to_ascii_uppercase(); + for object in config_objects(&String::from_utf8_lossy(bytes)) { + if let (Some(library), Some(file)) = (object.get("LIBRARY"), object.get(&symbol)) { + return Ok(Some((parse_cfg_value(library)?, parse_cfg_value(file)?))); + } + } + Ok(None) +} + fn parse_cfg_value(value: &str) -> Result<String, String> { if let Some(rest) = value.strip_prefix('"') { let end = rest @@ -1413,6 +1506,18 @@ mod tests { use super::*; #[test] + fn mission_voice_uses_the_index_and_preserves_the_declared_archive() { + let messages = b"object message\nmessage_index = 14\nvoice_resource = \"hint\"\nend\n"; + let mission = b"object voices\nlibrary = \"custom.lib\"\nHINT = \"run.wav\"\nend\n# \xff"; + let symbol = message_voice_symbol(messages, 14).unwrap().unwrap(); + assert_eq!( + voice_resource(mission, &symbol).unwrap(), + Some(("custom.lib".into(), "run.wav".into())) + ); + assert_eq!(message_voice_symbol(messages, 15).unwrap(), None); + } + + #[test] fn parses_original_ambient_names_in_numeric_order() { let config = parse_mission_audio_config( br#" diff --git a/apps/fparkan-game/src/main.rs b/apps/fparkan-game/src/main.rs index 880b0a13..be480d2b 100644 --- a/apps/fparkan-game/src/main.rs +++ b/apps/fparkan-game/src/main.rs @@ -193,9 +193,9 @@ fn run(args: &[String]) -> Result<String, String> { log_mission_event_stop(stop); } return Ok(format!( - "simulation mission={} ticks={} queue_time_ms={} live_objects={} world_events={} init_states={} mission_events={}\n{SIMULATION_COVERAGE} {camera_coverage}", + "simulation mission={} ticks={} queue_time_ms={} live_objects={} world_events={} init_states={} mission_events={} mission_messages={}\n{SIMULATION_COVERAGE} {camera_coverage}", loaded.key, report.ticks, duration_ms, report.live_objects, report.world_events, - loaded.script_init_state_count, report.mission_events, + loaded.script_init_state_count, report.mission_events, report.mission_messages, )); } let mission_assets_duration = mission_assets_started.elapsed(); @@ -10493,6 +10493,16 @@ impl StaticVulkanApp { { log_mission_event_stop(stop); } + if let Some(tick) = &frame.mission_event { + for &index in &tick.messages { + if let Some(audio) = &mut self.audio { + match audio.queue_mission_voice(index) { + Ok(resource) => eprintln!("mission_voice index={index} resource={resource} queue_time_ms={control_clock_ms}"), + Err(error) => eprintln!("sim_coverage mission_voice_unavailable=1 index={index} reason={error}"), + } + } + } + } self.mission_frame = Some(frame); } let effect_elapsed_seconds = control_clock_ms as f32 / 1000.0; |
