From 7a888d0cde24576a7b36576d26dd9931e3ae8543 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Fri, 27 Nov 2026 00:55:21 +0400 Subject: fix(ai): rank capturers by Control travel time The Handler(14) oracle executes ai.dll with the real Control selector 0x91 and verifies faster distant candidates, the one-second bias and the minimum speed. Use the same current forward limit in the stock capture query. Original roster ordering and task preemption remain approximate. --- crates/fparkan-runtime/src/clan_ai_events.rs | 8 ++++-- crates/fparkan-runtime/src/clan_targeting.rs | 37 +++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 8 deletions(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index ee19f56e..9d035503 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -1028,8 +1028,12 @@ impl LoadedMissionState { capturer = crate::clan_targeting::nearest_capturer( &self.mission, &self.object_drafts, &self.mission_events, world, OwnerId(clan as u16), target, |handle| { - !self.clan_ai_events.unit_assigned(handle) - && self.unit_patrol.as_ref().is_none_or(|patrol| !patrol.is_shut_down(handle)) + if self.clan_ai_events.unit_assigned(handle) + || self.unit_patrol.as_ref().is_some_and(|patrol| patrol.is_shut_down(handle)) + { + return None; + } + self.control_device_tables.iter().find(|table| table.handle == handle) }, ); let id = capturer.map_or(u32::MAX, |unit| unit.id); diff --git a/crates/fparkan-runtime/src/clan_targeting.rs b/crates/fparkan-runtime/src/clan_targeting.rs index 0555e0fb..ef580e43 100644 --- a/crates/fparkan-runtime/src/clan_targeting.rs +++ b/crates/fparkan-runtime/src/clan_targeting.rs @@ -107,25 +107,24 @@ fn at_platform([x, y, z]: [f32; 3], [tx, ty, tz]: [f32; 3]) -> bool { (x - tx).hypot(y - ty) <= 0.05 && (z - tz).abs() <= 2.0 } -pub(super) fn nearest_capturer( +pub(super) fn nearest_capturer<'a>( mission: &MissionDocument, drafts: &[MissionObjectDraft], roster: &crate::mission_events::MissionEvents, world: &World, owner: OwnerId, target_id: u32, - available: impl Fn(ObjectHandle) -> bool, + available_control: impl Fn(ObjectHandle) -> Option<&'a MissionControlDeviceTableState>, ) -> Option { let target = crate::mission_events::placed_logic_objects(mission, world, target_id).next()?; let point = transform_state(world, target) .ok()? .position .map(f32::from_bits); - // shortcut: all routed robots currently share one speed, so travel time ranks by XY distance. roster .clan_units(world, owner, true) - .filter(|&handle| available(handle)) .filter_map(|handle| { + let profile = available_control(handle)?.owner_profile.as_ref()?; let original_id = identity_metadata(world, handle).ok()?.original_id?.0; let index = original_id as usize; let chassis = drafts.get(index)?.unit_components.first()?; @@ -136,7 +135,8 @@ pub(super) fn nearest_capturer( .ok()? .position .map(f32::from_bits); - let distance = (point[0] - position[0]).hypot(point[1] - position[1]); + let score = + capture_arrival_score(position, point, f32::from_bits(profile.current_words[7]))?; // shortcut: generated ID is local to this script section until native robot IDs are allocated. let id = mission .objects @@ -149,13 +149,26 @@ pub(super) fn nearest_capturer( id, target_id, }, - distance, + score, )) }) .min_by(|(_, a), (_, b)| a.total_cmp(b)) .map(|(unit, _)| unit) } +fn capture_arrival_score([x, y, _]: [f32; 3], [tx, ty, _]: [f32; 3], speed: f32) -> Option { + if !speed.is_finite() || speed <= 1.0 { + return None; + } + let distance = (tx - x).hypot(ty - y); + let score = if distance <= speed { + 0.0 + } else { + distance / speed + }; + (score < 1e9).then_some(score) +} + pub(super) fn encode_strength(strength: f32) -> f32 { // The original narrows the truncated x87 result to a DWORD before the script assignment. (strength as i64 as u32) as f32 @@ -457,6 +470,18 @@ fn building_priority(class: u32) -> u8 { mod tests { use super::*; + #[test] + fn capture_arrival_uses_the_live_speed_and_scores_a_one_second_approach_as_zero() { + let score = |x, speed| capture_arrival_score([x, 0.0, 800.0], [0.0; 3], speed); + assert_eq!(score(20.0, 2.0), Some(10.0)); + assert_eq!(score(30.0, 10.0), Some(3.0)); + assert_eq!(score(7.0, 9.0), Some(0.0)); + assert_eq!(score(2.0, 3.0), Some(0.0)); + assert_eq!(score(3.0, 3.0), Some(0.0)); + assert_eq!(score(0.0, 1.0), None); + assert_eq!(score(0.0, 0.5), None); + } + #[test] fn defence_selects_the_smallest_strength_and_keeps_the_first_tie() { assert_eq!( -- cgit v1.2.3