diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 21:21:17 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 21:21:17 +0300 |
| commit | c75b08d39057a502598ba9d9959edadb36fcf595 (patch) | |
| tree | cc5e772b9dd5cd82f3cfa6fe68a50ca5da17aaf8 /crates/fparkan-runtime | |
| parent | 893f6ecd8ac49627185c035adba94065dddcdd85 (diff) | |
| download | fparkan-c75b08d39057a502598ba9d9959edadb36fcf595.tar.xz fparkan-c75b08d39057a502598ba9d9959edadb36fcf595.zip | |
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.
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 46 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/clan_targeting.rs | 94 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 6 |
3 files changed, 146 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 45796a9e..481e9d08 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -87,6 +87,7 @@ pub(super) struct ClanAiEvents { factory_jobs: Vec<((u16, Handler2RecordKey), ObjectHandle)>, factory_rejections: BTreeMap<(u16, u32), u64>, strength_queries: BTreeMap<(usize, u16), u64>, + capture_queries: BTreeMap<usize, u64>, } impl LoadedMissionState { @@ -151,6 +152,45 @@ impl LoadedMissionState { .scalar_1; Ok(Some(VarSetDefault::FloatBits(priority.to_bits()))) } + ScriptDispatchSelector::Handler(35) => { + let &[radius, estimate] = instruction.references.as_slice() else { + return Err("Handler(35) requires a radius and strength output".into()); + }; + let radius = values.get(radius as usize).and_then(|cell| cell.as_float()) + .ok_or("Handler(35) requires a float radius")?; + if !matches!(values.get(instruction.header_words[1] as usize), + Some(VarSetDefault::Dword(_)) | None) { + return Err("Handler(35) requires a DWORD result".into()); + } + let estimate = values.get_mut(estimate as usize) + .ok_or("Handler(35) strength output is unavailable")?; + let capture = crate::clan_targeting::capture_target( + &self.mission, &self.object_drafts, &self.control_device_tables, + world, clan, radius, |id| { + self.clan_ai_events.problems.get(&clan).is_some_and(|scheduler| { + scheduler.records().iter().any(|record| { + record.key.word_0 == 9 + && problem_dword(record.key.scalar_4_bits) == Some(id) + }) + }) + }, + ); + let (target, strength) = capture.unwrap_or((u32::MAX, 1e12_f32)); + let strength = crate::clan_targeting::encode_strength(strength); + *estimate = match *estimate { + VarSetDefault::Dword(_) => VarSetDefault::Dword( + problem_dword(strength.to_bits()) + .ok_or("Handler(35) strength is outside the DWORD range")? + ), + VarSetDefault::FloatBits(_) => VarSetDefault::FloatBits(strength.to_bits()), + }; + let count = self.clan_ai_events.capture_queries.entry(clan).or_default(); + *count += 1; + if *count == 1 { + eprintln!("sim_coverage script_capture_target_approx=1 clan={clan} target={target} strength={strength}"); + } + Ok(Some(VarSetDefault::Dword(target))) + } ScriptDispatchSelector::Handler(38) => { let (target, mode) = instruction.dword_pair(values) .ok_or("Handler(38) requires a clan and unit-selection mode")?; @@ -681,6 +721,12 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32 } impl ClanAiEvents { + pub(super) fn capture_queries(&self) -> impl Iterator<Item = (usize, u64)> + '_ { + self.capture_queries + .iter() + .map(|(&clan, &count)| (clan, count)) + } + pub(super) fn strength_queries(&self) -> impl Iterator<Item = (usize, u16, u64)> + '_ { self.strength_queries .iter() 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); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 61ee81be..475a4c2c 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -47,6 +47,7 @@ mod building_door; mod clan_ai_events; mod clan_combat_strength; mod clan_script_init; +mod clan_targeting; mod mesh_collision; mod mining; mod mission_events; @@ -2911,6 +2912,7 @@ pub struct SimulationReport { pub script_factory_orders: Vec<(u16, u32, u64)>, /// Approximate clan strength queries, grouped by caller and target clan. pub script_strength_queries: Vec<(usize, u16, u64)>, + pub script_capture_queries: Vec<(usize, u64)>, /// First factory completion by clan, in milliseconds. pub first_factory_completion_ms: std::collections::BTreeMap<u16, u32>, /// Total requests for mission information messages. @@ -2953,6 +2955,7 @@ pub fn simulate_mission_report( script_factory_rejections: Vec::new(), script_factory_orders: Vec::new(), script_strength_queries: Vec::new(), + script_capture_queries: Vec::new(), first_factory_completion_ms: Default::default(), mission_messages: 0, autodemo_first_shot_ms: None, @@ -3032,6 +3035,9 @@ pub fn simulate_mission_report( report .script_strength_queries .extend(loaded.clan_ai_events.strength_queries()); + report + .script_capture_queries + .extend(loaded.clan_ai_events.capture_queries()); } Ok(report) } |
