From 6150e6897f4ca4010eafa0f8d0a180f6b26d2124 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 15:01:09 +0400 Subject: feat(runtime): let AI guards defend in single missions --- crates/fparkan-runtime/src/autodemo_combat.rs | 13 +++++ crates/fparkan-runtime/src/lib.rs | 83 ++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/autodemo_combat.rs b/crates/fparkan-runtime/src/autodemo_combat.rs index ab48f376..1779d136 100644 --- a/crates/fparkan-runtime/src/autodemo_combat.rs +++ b/crates/fparkan-runtime/src/autodemo_combat.rs @@ -52,6 +52,7 @@ pub(super) struct AutoDemoCombat { units: Vec, reserve: Vec, hostile: Vec>, + guard_clans: Vec, projectiles: Vec, flights: Vec, pub(super) unavailable: usize, @@ -86,6 +87,7 @@ impl AutoDemoCombat { handles: &[ObjectHandle], assets: &MissionAssets, reserve_start: usize, + guard_clans: Vec, ) -> Self { let hostile = mission .clans @@ -107,6 +109,7 @@ impl AutoDemoCombat { units: Vec::new(), reserve: Vec::new(), hostile, + guard_clans, projectiles: Vec::new(), flights: Vec::new(), unavailable: 0, @@ -195,6 +198,10 @@ impl AutoDemoCombat { &self.projectiles } + pub(super) fn guard_clan_count(&self) -> usize { + self.guard_clans.iter().filter(|&&enabled| enabled).count() + } + pub(super) fn flights(&self) -> impl Iterator + '_ { self.flights .iter() @@ -285,7 +292,13 @@ impl AutoDemoCombat { }; let targets = self.targets(&engine.world, &mission.mission_assets); for unit in &mut self.units { + // shortcut: armed AI units hold position and defend it while their Init is incomplete. + let guard = identity_metadata(&engine.world, unit.handle) + .ok() + .and_then(|identity| identity.owner_id) + .is_some_and(|owner| self.guard_clans.get(usize::from(owner.0)) == Some(&true)); if mission.script_host_mode == super::MissionMode::SingleMission + && !guard && !mission .unit_patrol .as_ref() diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index e4cd4076..932e22ff 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2444,7 +2444,27 @@ fn load_mission_with_options( } else { None }; - let autodemo_combat = unit_patrol.as_ref().map(|_| { + let guard_clans: Vec<_> = + if options.start_autodemo && request.mode == MissionMode::SingleMission { + mission + .clans + .iter() + .enumerate() + .map(|(index, clan)| { + clan.mode == 2 + && loaded_scripts.bundles.iter().any(|bundle| { + bundle.clan_index == index + && matches!( + bundle.init_execution, + MissionScriptInitExecution::Unsupported { .. } + ) + }) + }) + .collect() + } else { + Vec::new() + }; + let autodemo_combat = (unit_patrol.is_some() || guard_clans.contains(&true)).then(|| { autodemo_combat::AutoDemoCombat::new( &vfs, &mission, @@ -2452,6 +2472,7 @@ fn load_mission_with_options( &handles, &mission_assets, reserve_start, + guard_clans, ) }); for &handle in &handles[reserve_start..] { @@ -2826,6 +2847,15 @@ pub fn autodemo_combat_unavailable(engine: &Engine) -> usize { .map_or(0, |combat| combat.unavailable) } +/// Counts AI clans using the stationary defense fallback in this mission. +#[must_use] +pub fn mission_guard_fallback_count(engine: &Engine) -> usize { + engine + .autodemo_combat + .as_ref() + .map_or(0, autodemo_combat::AutoDemoCombat::guard_clan_count) +} + /// Returns the loaded mission summary. #[must_use] pub fn loaded_mission(engine: &Engine) -> Option<&LoadedMission> { @@ -18001,6 +18031,57 @@ mod tests { } } + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_two_fortresses_ai_defends_when_the_player_approaches() { + let vfs: Arc = Arc::new(DirectoryVfs::new(licensed_root("IS"))); + let mut engine = create(EngineServices::new(vfs)).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + assert_eq!(mission_guard_fallback_count(&engine), 1); + assert!(frame(&mut engine, 0) + .unwrap() + .autodemo_combat_events + .is_empty()); + let hero = starting_cockpit(&engine).unwrap(); + let enemy = mission_object_handle(&engine, OriginalObjectId(11)).unwrap(); + let enemy_pose = transform_state(&engine.world, enemy).unwrap(); + let [x, y, z] = enemy_pose.position.map(f32::from_bits); + let mut pose = transform_state(&engine.world, hero.handle).unwrap(); + pose.position = [x - 10.0, y, z + 18.0].map(f32::to_bits); + set_transform(&mut engine.world, hero.handle, pose).unwrap(); + let shots = (100..=6_000) + .step_by(50) + .flat_map(|now| frame(&mut engine, now).unwrap().autodemo_combat_events) + .collect::>(); + let assets = loaded_mission_assets(&engine).unwrap(); + let weapons = player_weapon::prepare_unit_weapons(&assets.object_component_controls[11]); + assert!( + shots.iter().any(|event| matches!(event, + AutoDemoCombatEvent::Shot { shooter, target, .. } + if *shooter == enemy && *target == hero.handle)), + "{shots:?}, enemy weapons: {}", + weapons.weapons().len() + ); + assert!(!shots.iter().any(|event| matches!(event, + AutoDemoCombatEvent::Shot { shooter, .. } if *shooter == hero.handle))); + assert_eq!( + transform_state(&engine.world, enemy).unwrap().position, + enemy_pose.position + ); + } + #[test] #[ignore = "requires licensed corpus"] fn licensed_teleport_helicopter_climbs_and_keeps_altitude() { -- cgit v1.2.3