From 594e910853c98b1b35809799657b058d7b7dc2a0 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 09:06:11 +0400 Subject: fix(ai): wait for mine recapture in clan requests Follow the current world object for logical-ID wait conditions. Check the stock Two Fortresses recapture event against the original AI condition oracle and a licensed regression. --- crates/fparkan-runtime/src/clan_ai_events.rs | 84 +++++++++++++++++++--- .../fparkan-runtime/src/clan_problem_conditions.rs | 32 ++++++--- 2 files changed, 97 insertions(+), 19 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 9d035503..b6ef965e 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -16,7 +16,6 @@ const STORAGE_CLASS: u32 = 0x8000_0008; const RESOURCE_ORE: u32 = 0x0200_0100; const BUILDING_PROTECT: u32 = 8; const PLACE_PROTECT: u32 = 11; -const ATTACK_UNIT: u32 = 13; const OPTIMAL_TRANSPORT: u32 = 4; const BUILDING_NEEDED: u32 = 3; const MINE_NEEDED: u32 = 1; @@ -159,6 +158,7 @@ pub(super) fn tick( OwnerId(clan as u16), loaded.unit_patrol.as_ref(), group, + |id| loaded.mission_events.logic_objects(&loaded.mission, &engine.world, id).next(), |unit| loaded.construction.active(unit) || loaded.laboratories.active(unit) || ai.transports.iter().any(|(job, task)| *job == (clan, key) && task.unit == unit), ) @@ -978,14 +978,8 @@ impl LoadedMissionState { .ok_or("Handler(27) task wait requires the assigned unit")?; Some(Condition::TaskInactive(unit)) } - (1 | 3, 0) if matches!(key.word_0, 7 | 9) || (action == 1 && matches!(key.word_0, BUILDING_PROTECT | ATTACK_UNIT | MAKE_RESEARCH)) => { - let target = self.mission_events.logic_objects( - &self.mission, world, id - ).next(); - Some(if action == 1 { Condition::ObjectDestroyed(target) } else { - Condition::BuildingCaptured(target.ok_or("Handler(27) capture target is unavailable")?) - }) - } + (1, 0) => Some(Condition::ObjectDestroyed(id)), + (3, 0) => Some(Condition::BuildingCaptured(id)), (4 | 5, 1) if id == 0 && self.clan_ai_events.groups.contains_key(&(clan, key)) => { Some(if action == 4 { Condition::GroupDestroyed } else { Condition::GroupIdle }) } @@ -1730,6 +1724,78 @@ impl ClanAiEvents { mod tests { use super::*; + const ATTACK_UNIT: u32 = 13; + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_mine_request_waits_for_its_capture_order() { + use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput}; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{handle_by_original_id, set_owner, OriginalObjectId}; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus root"); + let mut engine = + crate::create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + crate::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(); + let mine = handle_by_original_id(&engine.world, OriginalObjectId(3)).unwrap(); + let loaded = engine.loaded.as_mut().unwrap(); + let logical_id = loaded + .mission_events + .logic_id(&loaded.mission, &engine.world, mine) + .unwrap(); + // Isolate the late stock script branch after the enemy mine changes hands. + set_owner(&mut engine.world, mine, Some(OwnerId(0))).unwrap(); + let request = Handler2RecordInput { + word_0: MINE_NEEDED, + scalar_1: 100.0, + word_2: 0, + word_3: 0, + argument_0: 0, + argument_1: 0, + argument_2: 0, + }; + let wait = (1, request.into()); + loaded + .clan_ai_events + .problems + .entry(1) + .or_default() + .submit(request); + for now in (0..=4_000).step_by(100) { + crate::frame(&mut engine, now).unwrap(); + } + let ai = &engine.loaded.as_ref().unwrap().clan_ai_events; + assert!(ai.waits.get(&wait).is_some_and( + |conditions| conditions.contains(&Condition::BuildingCaptured(logical_id)) + )); + assert!(!ai + .stops() + .any(|(stop, _)| stop.clan_index == 1 && stop.reason.contains("Handler(27)"))); + + set_owner(&mut engine.world, mine, Some(OwnerId(1))).unwrap(); + crate::frame(&mut engine, 4_100).unwrap(); + assert!(!engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .waits + .contains_key(&wait)); + } + #[test] #[ignore = "requires licensed corpus"] fn licensed_free_group_waits_empty_then_attacks_after_a_debug_roster_transfer() { diff --git a/crates/fparkan-runtime/src/clan_problem_conditions.rs b/crates/fparkan-runtime/src/clan_problem_conditions.rs index e0d11336..a7c2c298 100644 --- a/crates/fparkan-runtime/src/clan_problem_conditions.rs +++ b/crates/fparkan-runtime/src/clan_problem_conditions.rs @@ -4,8 +4,8 @@ use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle, Owne #[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum Condition { - ObjectDestroyed(Option), - BuildingCaptured(ObjectHandle), + ObjectDestroyed(u32), + BuildingCaptured(u32), GroupDestroyed, GroupIdle, TaskInactive(ObjectHandle), @@ -18,17 +18,18 @@ impl Condition { clan: OwnerId, patrol: Option<&UnitPatrol>, group: Option<&CombatGroup>, + lookup: impl Fn(u32) -> Option, task_active: impl Fn(ObjectHandle) -> bool, ) -> bool { let live = |unit| registration_sequence(world, unit).is_ok_and(|sequence| sequence.is_some()); match self { - Self::ObjectDestroyed(target) => target.is_none_or(|target| !live(target)), - Self::BuildingCaptured(target) => { + Self::ObjectDestroyed(id) => lookup(id).is_none_or(|target| !live(target)), + Self::BuildingCaptured(id) => lookup(id).is_some_and(|target| { live(target) && identity_metadata(world, target) .is_ok_and(|identity| identity.owner_id == Some(clan)) - } + }), Self::GroupDestroyed => { group.is_none_or(|group| group.members.iter().all(|&unit| !live(unit))) } @@ -55,14 +56,25 @@ mod tests { let target = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap(); register_object(&mut world, target).unwrap(); let clan = OwnerId(1); + let id = 0x8000_0009; + let lookup = |candidate| (candidate == id).then_some(target); + let captured = Condition::BuildingCaptured(id); + let destroyed = Condition::ObjectDestroyed(id); set_owner(&mut world, target, Some(OwnerId(0))).unwrap(); - assert!(!Condition::BuildingCaptured(target).met(&world, clan, None, None, |_| false)); - assert!(!Condition::ObjectDestroyed(Some(target)).met(&world, clan, None, None, |_| false)); + assert!(!captured.met(&world, clan, None, None, lookup, |_| false)); + assert!(!destroyed.met(&world, clan, None, None, lookup, |_| false)); set_owner(&mut world, target, Some(clan)).unwrap(); - assert!(Condition::BuildingCaptured(target).met(&world, clan, None, None, |_| false)); + assert!(captured.met(&world, clan, None, None, lookup, |_| false)); fparkan_world::request_delete(&mut world, target).unwrap(); fparkan_world::step(&mut world, &fparkan_world::InputSnapshot).unwrap(); - assert!(Condition::ObjectDestroyed(Some(target)).met(&world, clan, None, None, |_| false)); - assert!(!Condition::BuildingCaptured(target).met(&world, clan, None, None, |_| false)); + assert!(destroyed.met(&world, clan, None, None, |_| None, |_| false)); + assert!(!captured.met(&world, clan, None, None, |_| None, |_| false)); + + let replacement = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap(); + register_object(&mut world, replacement).unwrap(); + let lookup = |candidate| (candidate == id).then_some(replacement); + assert!(!destroyed.met(&world, clan, None, None, lookup, |_| false)); + set_owner(&mut world, replacement, Some(clan)).unwrap(); + assert!(captured.met(&world, clan, None, None, lookup, |_| false)); } } -- cgit v1.2.3