From c75b08d39057a502598ba9d9959edadb36fcf595 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 22:21:17 +0400 Subject: feat(ai): select targets for scripted capture requests Confirm the target ranking, duplicate exclusion and strength output with GOG ai.dll at RVA 0xc847. Single.01 reaches PBM_BUILDING_CAPTURE from its stock start. The executor selection remains pending. --- crates/fparkan-runtime/src/clan_targeting.rs | 94 ++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 crates/fparkan-runtime/src/clan_targeting.rs (limited to 'crates/fparkan-runtime/src/clan_targeting.rs') diff --git a/crates/fparkan-runtime/src/clan_targeting.rs b/crates/fparkan-runtime/src/clan_targeting.rs new file mode 100644 index 00000000..f8c1b4ca --- /dev/null +++ b/crates/fparkan-runtime/src/clan_targeting.rs @@ -0,0 +1,94 @@ +use crate::{ + clan_combat_strength::unit_strength, MissionControlDeviceTableState, MissionDocument, + MissionObjectDraft, +}; +use fparkan_world::{ + handle_by_original_id, identity_metadata, registration_sequence, transform_state, World, +}; + +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 +} + +pub(super) fn capture_target( + mission: &MissionDocument, + drafts: &[MissionObjectDraft], + controls: &[MissionControlDeviceTableState], + world: &World, + clan: usize, + radius: f32, + already_targeted: impl Fn(u32) -> bool, +) -> Option<(u32, f32)> { + let source = mission.clans.get(clan)?; + // shortcut: use placed-object order until the native ArealMap roster is wired. + let hostile_objects = || { + drafts.iter().filter_map(|draft| { + let class = draft.unit_dat_header_flags?; + let handle = handle_by_original_id(world, draft.original_id?)?; + registration_sequence(world, handle).ok()??; + let owner = usize::from(identity_metadata(world, handle).ok()?.owner_id?.0); + let other = mission.clans.get(owner)?; + if owner == clan + || !source.relations.iter().any(|relation| { + relation.relation_value == 0 && relation.other_clan_name.raw == other.name.raw + }) + { + return None; + } + let position = transform_state(world, handle) + .ok()? + .position + .map(f32::from_bits); + Some((draft.identity_or_clan_raw, class, handle, position)) + }) + }; + let (id, _, _, position) = hostile_objects() + .filter(|&(id, class, _, _)| { + id != u32::MAX && class & 0x8000_0000 != 0 && !already_targeted(id) + }) + .min_by_key(|&(_, class, _, _)| building_priority(class))?; + let strength = hostile_objects() + .filter(|&(_, class, _, candidate)| { + class & crate::autodemo::MOBILE_UNIT != 0 + && (candidate[0] - position[0]).hypot(candidate[1] - position[1]) < radius + }) + .map(|(_, _, handle, _)| unit_strength(controls, handle)) + .fold(0.0, |sum, amount| sum + amount); + Some((id, strength)) +} + +fn building_priority(class: u32) -> u8 { + match class { + 0x8000_0002 => 1, + crate::autodemo::PLANT_CLASS => 2, + crate::mining::MINE_CLASS => 3, + 0x8000_0400 => 4, + 0x8000_0008 => 5, + _ => 10, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn generators_and_factories_rank_ahead_of_other_buildings() { + let classes = [ + 0x8000_0002, + 0x8000_0010, + 0x8000_0004, + 0x8000_0400, + 0x8000_0008, + 0x8002_0000, + ]; + assert_eq!(classes.map(building_priority), [1, 2, 3, 4, 5, 10]); + } + + #[test] + fn strength_output_truncates_and_keeps_the_native_error_value() { + assert_eq!(encode_strength(8.2), 8.0); + assert_eq!(encode_strength(1e12), 3_567_583_232.0); + } +} -- cgit v1.2.3