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 /crates/fparkan-runtime/src | |
| parent | 796a64e61643936e3b4517eb365e95639e68aaa5 (diff) | |
| download | fparkan-badbf2e74fceecadba1487b0863825e180e61c85.tar.xz fparkan-badbf2e74fceecadba1487b0863825e180e61c85.zip | |
feat(runtime): trigger tutorial voices from tactical areas
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 16 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mission_events.rs | 113 |
2 files changed, 120 insertions, 9 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 1fa80aa5..55a1f324 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2257,7 +2257,7 @@ pub fn step_headless( let mission_event = engine .loaded .as_mut() - .and_then(|state| state.tick_mission_event(queue_time_ms)); + .and_then(|state| state.tick_mission_event(&engine.world, queue_time_ms)); Ok(FrameResult { snapshot, mission_event, @@ -2275,7 +2275,7 @@ pub fn frame(engine: &mut Engine, queue_time_ms: u32) -> Result<FrameResult, Eng /// Coverage entries for the subsystems still absent from the mission loop. pub const SIMULATION_COVERAGE: &str = - "sim_coverage research_startup_gate_stub=1 mission_handlers_stub=1 unit_behavior_stub=1 render_object_lifecycle_stub=1 render_winding_change_stub=1 mission_event_clock_approx=1"; + "sim_coverage research_startup_gate_stub=1 mission_handlers_stub=1 unit_behavior_stub=1 render_object_lifecycle_stub=1 render_winding_change_stub=1 mission_event_clock_approx=1 mission_tactical_ids_stub=1 mission_message_ui_stub=1"; /// Totals from a headless mission coverage run. #[derive(Clone, Debug, Eq, PartialEq)] @@ -2290,6 +2290,8 @@ pub struct SimulationReport { pub mission_events: u64, /// Distinct uncovered periodic event instructions. pub mission_event_stops: Vec<MissionEventStop>, + /// Total requests for mission information messages. + pub mission_messages: u64, } /// Steps a freshly loaded mission from queue time zero at 60 Hz for coverage. @@ -2309,6 +2311,7 @@ pub fn simulate_mission_report( world_events: 0, mission_events: 0, mission_event_stops: Vec::new(), + mission_messages: 0, }; let steps = (u64::from(duration_ms) * 60).div_ceil(1000); for tick in 1..=steps { @@ -2320,6 +2323,7 @@ pub fn simulate_mission_report( if let Some(event) = frame.mission_event { report.mission_events += 1; report.mission_event_stops.extend(event.stop); + report.mission_messages += event.messages.len() as u64; } } Ok(report) @@ -17057,7 +17061,7 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] - fn licensed_firing_range_reports_first_periodic_instruction() { + fn licensed_firing_range_voices_spawn_area_messages_once() { let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(licensed_root("IS"))); let mut engine = create(EngineServices::new(vfs)).unwrap(); load_mission_with_init_stubs( @@ -17073,15 +17077,17 @@ mod tests { ) .unwrap(); let first = frame(&mut engine, 5).unwrap().mission_event.unwrap(); + assert_eq!(first.messages, [11, 14]); let stop = first.stop.unwrap(); assert_eq!( (stop.clan_index, stop.event_index, stop.instruction_index), - (0, 2, 0) + (0, 2, 49) ); - assert_eq!(stop.reason, "Handler(32)"); + assert_eq!(stop.reason, "Handler(31)"); assert!(frame(&mut engine, 2_004).unwrap().mission_event.is_none()); let next = frame(&mut engine, 2_005).unwrap().mission_event.unwrap(); assert!(next.stop.is_none()); + assert!(next.messages.is_empty()); } #[test] diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index e1177c7a..6151d704 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -1,4 +1,6 @@ use super::LoadedMissionState; +use fparkan_script::{ScriptDispatchSelector, VarSetDefault}; +use fparkan_world::{handle_by_original_id, transform_state, OriginalObjectId, World}; use std::collections::HashSet; /// First encounter with an uncovered periodic script instruction. @@ -21,12 +23,15 @@ pub struct MissionEventStop { pub struct MissionEventTick { /// An uncovered instruction encountered for the first time, if any. pub stop: Option<MissionEventStop>, + /// IDs of messages requested by this invocation, in script order. + pub messages: Vec<u32>, } #[derive(Default)] pub(super) struct MissionEvents { next_ms: u32, reported_stops: HashSet<usize>, + voiced_messages: HashSet<u32>, } impl MissionEvents { @@ -40,7 +45,11 @@ impl MissionEvents { } impl LoadedMissionState { - pub(super) fn tick_mission_event(&mut self, now_ms: u32) -> Option<MissionEventTick> { + pub(super) fn tick_mission_event( + &mut self, + world: &World, + now_ms: u32, + ) -> Option<MissionEventTick> { // shortcut: use the local clan (0 in the observed standalone modes), // until runtime has the original active-clan selector. let bundle = self @@ -65,8 +74,43 @@ impl LoadedMissionState { .script_varset_states .iter_mut() .find(|state| state.clan_index == bundle.clan_index)?; + let mut messages = Vec::new(); let stop = declarations - .execute_mission_prefix(event, bundle.formula_table.as_ref(), &mut state.values) + .execute_mission_prefix( + event, + bundle.formula_table.as_ref(), + &mut state.values, + |instruction, values| match instruction.dispatch_selector() { + ScriptDispatchSelector::Handler(32) => { + let [area, id] = *instruction.references.as_slice() else { + return Err("Handler(32) requires two DWORD operands".into()); + }; + let (Some(VarSetDefault::Dword(area)), Some(VarSetDefault::Dword(id))) = + (values.get(area as usize), values.get(id as usize)) + else { + return Err("Handler(32) requires two DWORD operands".into()); + }; + let inside = tactical_area_contains(&self.mission, world, *area, *id); + Ok(Some(u32::from(inside))) + } + ScriptDispatchSelector::Handler(30) => { + let command = declarations + .resolve_handler30_with_values(instruction, values) + .map_err(|error| error.to_string())?; + if self.script_host_mode == super::MissionMode::AutoDemo { + return Ok(None); + } + if command.first != 1 { + return Err(format!("Handler(30) command {}", command.first)); + } + if self.mission_events.voiced_messages.insert(command.second) { + messages.push(command.second); + } + Ok(None) + } + selector => Err(format!("{selector:?}")), + }, + ) .err() .filter(|(index, _)| self.mission_events.reported_stops.insert(*index)) .map(|(instruction_index, reason)| MissionEventStop { @@ -76,13 +120,74 @@ impl LoadedMissionState { instruction_index, reason, }); - Some(MissionEventTick { stop }) + Some(MissionEventTick { stop, messages }) } } +fn tactical_area_contains( + mission: &super::MissionDocument, + world: &World, + area: u32, + id: u32, +) -> bool { + // MisLoad allocates a slot per path and selects the first record matching + // the slot ID. Out-of-range IDs and unfilled slots yield an empty list. + if area as usize >= mission.paths.len() || id == u32::MAX { + return false; + } + let Some(path) = mission.paths.iter().find(|path| path.id as u32 == area) else { + return false; + }; + // shortcut: resolve placed objects through their TMA logic ID until the + // world assigns native logic IDs to units created during gameplay. + mission.objects.iter().enumerate().any(|(index, object)| { + object.identity_or_clan_raw == id + && handle_by_original_id(world, OriginalObjectId(index as u32)) + .and_then(|handle| transform_state(world, handle).ok()) + .is_some_and(|pose| { + let [x, y, _] = pose.position.map(f32::from_bits); + in_tactical_polygon(&path.points, x, y) + }) + }) +} + +fn in_tactical_polygon(vertices: &[[f32; 3]], x: f32, y: f32) -> bool { + let Some(&[mut previous_x, mut previous_y, _]) = vertices.last() else { + return false; + }; + let mut inside = false; + for &[edge_x, edge_y, _] in vertices { + if (y <= edge_y) != (y <= previous_y) { + let crossed = if (x <= edge_x) == (x <= previous_x) { + x <= edge_x + } else { + x <= (y - edge_y) * (previous_x - edge_x) / (previous_y - edge_y) + edge_x + }; + inside ^= crossed; + } + previous_x = edge_x; + previous_y = edge_y; + } + inside +} + #[cfg(test)] mod tests { - use super::MissionEvents; + use super::{in_tactical_polygon, MissionEvents}; + + #[test] + fn tactical_polygon_preserves_original_edge_inclusion() { + let points = [[0., 0., 0.], [4., 0., 1.], [4., 4., 2.], [0., 4., 3.]]; + for (point, expected) in [ + ([2., 2.], true), + ([0., 2.], false), + ([4., 2.], true), + ([2., 0.], false), + ([2., 4.], true), + ] { + assert_eq!(in_tactical_polygon(&points, point[0], point[1]), expected); + } + } #[test] fn deadline_is_inclusive_and_reschedules_from_current_sample_without_catch_up() { |
