diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-25 13:03:31 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-25 13:03:31 +0300 |
| commit | 2656cca01b65745e9e6aa7b11d80414039e4cc67 (patch) | |
| tree | aca8b310eb3ecebfb9f2f9c19770a0fed838b235 /crates/fparkan-runtime/src/clan_building_queries.rs | |
| parent | 3278b382a1580e45ba915b64ad283168ab9ea7e1 (diff) | |
| download | fparkan-2656cca01b65745e9e6aa7b11d80414039e4cc67.tar.xz fparkan-2656cca01b65745e9e6aa7b11d80414039e4cc67.zip | |
feat(ai): select convoy targets using original class queries
Connect Handler(71) to least-defended hostile buildings of the requested
class and Handler(72) to the object's prototype size class. The stock
Convoy script now assigns its delayed attack instead of stopping at
these queries. Keep descriptor binding, guard strength and candidate
order explicit approximations with coverage counters.
Execute the original ai.dll handlers and Behavior.dll class getter in
native oracles, including strict radius, ties, missing objects and name
classes. Add a Rust class rule test and a licensed stock-start test for
the delayed attack; check owner independence separately with a debug
change after the stock assertion.
Validate fmt, workspace check/tests, licensed test, release simulation
and the running mission with Vulkan validation. Accelerated scene time
is a mechanic check, not mission-completion acceptance. Handler(30)
message dispatch remains the next Convoy script stop.
Diffstat (limited to 'crates/fparkan-runtime/src/clan_building_queries.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_building_queries.rs | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_building_queries.rs b/crates/fparkan-runtime/src/clan_building_queries.rs new file mode 100644 index 00000000..399dd28c --- /dev/null +++ b/crates/fparkan-runtime/src/clan_building_queries.rs @@ -0,0 +1,170 @@ +use crate::{ + mission_events::MissionEvents, MissionControlDeviceTableState, MissionDocument, + MissionObjectDraft, +}; +use fparkan_world::{handle_by_original_id, transform_state, OwnerId, World}; + +pub(super) fn weakest_enemy_of_class( + mission: &MissionDocument, + roster: &MissionEvents, + drafts: &[MissionObjectDraft], + controls: &[MissionControlDeviceTableState], + world: &World, + caller: usize, + class: u32, + radius: f32, +) -> Option<(u32, f32)> { + let source = mission.clans.get(caller)?; + let enemies: Vec<_> = mission + .clans + .iter() + .enumerate() + .filter(|(index, candidate)| { + *index != caller + && source.relations.iter().any(|relation| { + relation.relation_value == 0 + && relation.other_clan_name.raw == candidate.name.raw + }) + }) + .flat_map(|(index, _)| roster.clan_units(world, OwnerId(index as u16), false)) + .collect(); + // shortcut: use mission order until the native ArealMap building lists are wired. + let candidates = drafts.iter().filter_map(|draft| { + if class & 0x8000_0000 == 0 + || draft.unit_dat_header_flags != Some(class) + || draft.identity_or_clan_raw == u32::MAX + { + return None; + } + let handle = handle_by_original_id(world, draft.original_id?)?; + if !enemies.contains(&handle) { + return None; + } + let position = transform_state(world, handle) + .ok()? + .position + .map(f32::from_bits); + let strength = crate::clan_targeting::area_strength( + world, + drafts, + controls, + enemies.iter().copied(), + position, + radius, + ); + Some((draft.identity_or_clan_raw, strength)) + }); + crate::clan_targeting::least_defended(candidates) +} + +#[cfg(test)] +mod tests { + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_stock_convoy_uses_size_class_and_assigns_an_attack() { + use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput}; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{identity_metadata, set_owner, OwnerId}; + 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/CAMPAIGN/CAMPAIGN.03/Mission.02/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + for now in (0..=900_000).step_by(100) { + crate::frame(&mut engine, now).unwrap(); + if engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .class_target_queries() + .any(|(clan, _, _, _)| clan == 2) + { + break; + } + } + let loaded = engine.loaded.as_ref().unwrap(); + let actor = loaded + .mission_events + .logic_objects(&loaded.mission, &engine.world, 15) + .next() + .unwrap(); + let target = loaded + .mission_events + .logic_objects(&loaded.mission, &engine.world, 0x8000_0001) + .next() + .unwrap(); + assert_eq!( + loaded + .unit_patrol + .as_ref() + .unwrap() + .attack_target(actor) + .map(|(unit, _)| unit), + Some(target) + ); + assert!(loaded + .clan_ai_events + .class_target_queries() + .any(|(clan, class, id, count)| { + clan == 2 && class == 0x8001_0000 && id == 0x8000_0001 && count > 0 + })); + assert!(loaded + .clan_ai_events + .size_class_queries() + .any(|(clan, id, count)| clan == 1 && id == 0x8000_0003 && count > 0)); + assert!(loaded + .clan_ai_events + .stops() + .all(|(stop, _)| !["Handler(71)", "Handler(72)"] + .iter() + .any(|handler| stop.reason.starts_with(handler)))); + + let classified = loaded + .mission_events + .logic_objects(&loaded.mission, &engine.world, 0x8000_0003) + .next() + .unwrap(); + let original_owner = identity_metadata(&engine.world, classified) + .unwrap() + .owner_id; + // Debug ownership change checks the descriptor binding, separately from the stock-start attack. + set_owner(&mut engine.world, classified, Some(OwnerId(3))).unwrap(); + assert_eq!( + crate::unit_size_class::query( + &loaded.mission, + &loaded.mission_events, + &loaded.object_drafts, + &engine.world, + 0x8000_0003 + ) + .unwrap(), + 2 + ); + set_owner(&mut engine.world, classified, original_owner).unwrap(); + assert_eq!( + crate::unit_size_class::query( + &loaded.mission, + &loaded.mission_events, + &loaded.object_drafts, + &engine.world, + u32::MAX + ) + .unwrap(), + u32::MAX + ); + } +} |
