From d92cde8b7209b83572aeabd5dab657149ed78d99 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 03:25:58 +0400 Subject: feat(diagnostics): count unit motion gaps by object Track temporary-controller updates and frame time discarded at waypoint boundaries for each object slot, generation and original TMA ID. Preserve completed/dead object records, expose them in --sim-report, and print the same counters when the game window closes. Movement and RNG decisions are unchanged. A bounded Ghidra inspection identifies Go at Behavior.dll RVA 0x2b670 and the MWalker empty-state helper at 0x3dd80. Restoring the MWalker/Control handoff remains open; no fixed-step or remainder-carry rule is substituted for native behavior. Document this explicit stub and bind the addresses to the verified GOG module hash. The licensed stock AutoDemo test first failed with empty observations and now reports actual controller updates and nonzero discarded time. A normal 91-second window run also emits per-generation totals after closure. All 29 stock --sim-report 900 runs preserve the preceding commit's world and combat summaries. At 60 Hz, AutoDemo's largest recorded loss for a single object lifetime is 492516 us; losses are observations of FParkan, not original-game timings. Validation: cargo fmt --all -- --check; cargo check --workspace; cargo test --workspace; release build; focused unit and ignored licensed-stock tests; stock AutoDemo window; all 29 mission coverage runs. The native movement solver and original chronology are not accepted by this diagnostic change. --- crates/fparkan-runtime/src/unit_motion.rs | 135 ++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 crates/fparkan-runtime/src/unit_motion.rs (limited to 'crates/fparkan-runtime/src/unit_motion.rs') diff --git a/crates/fparkan-runtime/src/unit_motion.rs b/crates/fparkan-runtime/src/unit_motion.rs new file mode 100644 index 00000000..101a6208 --- /dev/null +++ b/crates/fparkan-runtime/src/unit_motion.rs @@ -0,0 +1,135 @@ +//! Observations of the temporary unit controller, not a native motion solver. + +use crate::{CombatObject, Engine}; +use std::collections::BTreeMap; + +/// Work performed by the temporary controller for one retained object identity. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct UnitMotionSample { + pub updates: u64, + pub first_ms: u32, + pub last_ms: u32, + pub discarded_waypoints: u64, + /// Sum of unused frame intervals, individually rounded to microseconds. + pub discarded_time_us: u64, +} + +/// Counts persist after orders end or the object leaves the world. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct UnitMotionReport { + pub objects: BTreeMap, +} + +impl UnitMotionReport { + pub(super) fn update(&mut self, object: CombatObject, now: u32) { + let sample = self.objects.entry(object).or_insert_with(|| { + eprintln!("sim_coverage unit_motion_controller_stub=1 object={object} original_id={:?} first_ms={now}", object.original_id); + UnitMotionSample { + first_ms: now, + ..Default::default() + } + }); + sample.updates += 1; + sample.last_ms = now; + } + + pub(super) fn discard_waypoint_time(&mut self, object: CombatObject, seconds: f32) { + let time_us = (f64::from(seconds.max(0.0)) * 1_000_000.0).round() as u64; + if time_us == 0 { + return; + } + let sample = self + .objects + .get_mut(&object) + .expect("recorded motion update"); + sample.discarded_waypoints += 1; + sample.discarded_time_us += time_us; + } +} + +/// Reads observations shared by the interactive and headless mission paths. +#[must_use] +pub fn loaded_mission_unit_motion_report(engine: &Engine) -> Option<&UnitMotionReport> { + Some(&engine.loaded.as_ref()?.unit_patrol.as_ref()?.motion_report) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn counts_updates_and_unused_time_separately_for_reused_slots() { + let object = CombatObject { + slot: 4, + generation: 1, + original_id: Some(17), + }; + let replacement = CombatObject { + generation: 2, + ..object + }; + let mut report = UnitMotionReport::default(); + report.update(object, 0); + report.discard_waypoint_time(object, 0.125); + report.update(object, 500); + report.discard_waypoint_time(object, 0.0); + report.update(replacement, 1_000); + assert_eq!( + report.objects[&object], + UnitMotionSample { + updates: 2, + first_ms: 0, + last_ms: 500, + discarded_waypoints: 1, + discarded_time_us: 125_000, + } + ); + assert_eq!(report.objects[&replacement].updates, 1); + assert_eq!(report.objects[&replacement].discarded_time_us, 0); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_stock_patrol_reports_controller_updates_and_lost_time() { + use crate::{ + create, load_gameplay_mission, simulate_mission_report, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Autodemo.00/data.tma".into(), + mode: MissionMode::AutoDemo, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let report = simulate_mission_report(&mut engine, 120_000).unwrap(); + assert_eq!( + &report.unit_motion, + loaded_mission_unit_motion_report(&engine).unwrap() + ); + assert!(!report.unit_motion.objects.is_empty()); + assert!(report + .unit_motion + .objects + .keys() + .all(|object| object.original_id.is_some())); + assert!(report + .unit_motion + .objects + .values() + .any(|sample| sample.updates > 1 + && sample.last_ms > sample.first_ms + && sample.discarded_waypoints > 0 + && sample.discarded_time_us > 0)); + } +} -- cgit v1.2.3