From 6cce33ff5b29616fb3d5caf9325729068d0631eb Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 12:57:40 +0400 Subject: feat(ai): select free factories for scripted production Handler(13) UNIT_FREE_UNIT now selects the first live clan object of the requested exact class. Reserved objects, assigned runtime workers and busy factories are skipped. Coverage records each selected logical ID. Campaign mission 8 can execute PBM_ROBOT_NEEDED and replenish its guards. Original GOG ai.dll RVA 0x86fb confirmed roster order, exact class matching, exclusion of busy and reserved entries, and ERROR when none are free. Validated with matching Rust roster cases, a licensed stock mission 8 production test, fmt/check/test workspace, and 900-second simulations of missions 8 and 28. Mission 8 also ran in the window with Vulkan validation. Occupancy is inferred from runtime jobs; native problem-slot ownership, other selection modes and mission chronology beside the original remain open. --- crates/fparkan-runtime/src/mission_events.rs | 73 +++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'crates/fparkan-runtime/src/mission_events.rs') diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index a00e449c..fd0cc72a 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -188,11 +188,15 @@ impl MissionEvents { world: &World, clan: usize, class: u32, + available: impl Fn(ObjectHandle) -> bool, ) -> Option { self.placed_clan_objects .iter() .find_map(|&(handle, candidate)| { - if candidate != class || self.reserved_units.contains(&(clan, handle)) { + if candidate != class + || self.reserved_units.contains(&(clan, handle)) + || !available(handle) + { return None; } registration_sequence(world, handle).ok()??; @@ -1094,6 +1098,73 @@ mod tests { WorldConfig, }; + #[test] + fn free_unit_selection_skips_busy_reserved_foreign_and_removed_entries() { + let mission = crate::MissionDocument { + format_version: 1, + clan_section_version: 6, + object_section_version: 10, + extra_section_version: 1, + versions: vec![], + paths: vec![], + clans: vec![], + objects: vec![], + land_path: Default::default(), + mission_flag: 0, + description_raw: Default::default(), + extras: vec![], + raw: std::sync::Arc::from([]), + }; + let mut world = new(WorldConfig); + let factory = 0x8000_0010; + let mut events = MissionEvents::default(); + let entries = [ + (91, 0x0100_8000, 1), + (12345, factory, 1), + (45678, factory, 1), + (45679, factory, 2), + ] + .map(|(id, class, owner)| { + let unit = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap(); + set_owner(&mut world, unit, Some(OwnerId(owner))).unwrap(); + register_object(&mut world, unit).unwrap(); + events.placed_clan_objects.push((unit, class)); + events.issued_logic_ids.insert(unit, id); + unit + }); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |_| true), + Some(12345) + ); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |unit| unit + != entries[1]), + Some(45678) + ); + events.reserved_units.insert((1, entries[1])); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |_| true), + Some(45678) + ); + events.reserved_units.insert((1, entries[2])); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |_| true), + None + ); + events.reserved_units.remove(&(1, entries[2])); + request_delete(&mut world, entries[2]).unwrap(); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |_| true), + None + ); + events.reserved_units.clear(); + set_owner(&mut world, entries[1], Some(OwnerId(2))).unwrap(); + assert_eq!( + events.first_available_placed_unit(&mission, &world, 1, factory, |_| true), + None + ); + } + #[test] #[ignore = "requires licensed corpus"] fn licensed_clan_checks_for_live_unit_in_problems0() { -- cgit v1.2.3