From 0da658fa6dfc7b2a052066561d904353532220ce Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 13:36:08 +0400 Subject: fix(ai): choose fast combat designs for factory orders Prepare a combat catalog entry for SELECT_FASTEST alongside SELECT_STRONGEST. Rank the fast choice using the projected Control forward limit and preserve catalog order on ties. The GOG selector picks 12hel1.dat from the initial scream catalog. Both newly built small factories in Third Part now complete that design from a stock start. --- crates/fparkan-runtime/src/clan_factory_schemes.rs | 98 +++++++++++++++------- crates/fparkan-runtime/src/lib.rs | 6 +- crates/fparkan-runtime/src/production.rs | 73 ++++++++++++---- 3 files changed, 127 insertions(+), 50 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/clan_factory_schemes.rs b/crates/fparkan-runtime/src/clan_factory_schemes.rs index 8d6d9f47..4cf78352 100644 --- a/crates/fparkan-runtime/src/clan_factory_schemes.rs +++ b/crates/fparkan-runtime/src/clan_factory_schemes.rs @@ -17,12 +17,25 @@ pub(super) const SELECT_STRONGEST: u32 = 5; pub(super) const AUXILIARY_CLASSES: [u32; 2] = [TRANSPORT_CLASS, crate::construction::BUILDER_CLASS]; +pub(super) struct ScriptDesign { + pub owner: OwnerId, + pub class: u32, + pub selection: u32, + pub path: NormalizedPath, +} + +#[derive(Clone, Copy)] +struct DesignRatings { + combat: Option, + speed: Option, +} + pub(super) fn available( assets: &AssetManager, vfs: &dyn Vfs, mission: &MissionDocument, bundles: &[MissionScriptBundle], -) -> Vec<(OwnerId, u32, NormalizedPath)> { +) -> Vec { let clans = bundles.iter().filter(|bundle| { mission.clans[bundle.clan_index].mode == 2 && bundle.package.events.iter().any(|event| { @@ -75,23 +88,43 @@ pub(super) fn available( .entries .into_iter() .filter(|entry| entry.status != 0); - let design = if class == WARBOT_CLASS { - let candidates = available.filter_map(|entry| { - let score = *ratings - .entry(entry.path.clone()) - .or_insert_with(|| combat_rating(assets, vfs, &entry.path)); - Some((entry.path, score?)) - }); - best_design(candidates) + if class == WARBOT_CLASS { + let candidates: Vec<_> = available + .filter_map(|entry| { + let scores = (*ratings + .entry(entry.path.clone()) + .or_insert_with(|| design_ratings(assets, vfs, &entry.path)))?; + Some((entry.path, scores)) + }) + .collect(); + for selection in [SELECT_STRONGEST, SELECT_FASTEST] { + let ranked = candidates.iter().filter_map(|(path, scores)| { + let score = if selection == SELECT_STRONGEST { + scores.combat + } else { + scores.speed + }?; + Some((path.clone(), score)) + }); + if let Some(path) = best_design(ranked) { + selected.push(ScriptDesign { + owner, + class, + selection, + path, + }); + } + } } else { // shortcut: select the sole qualified auxiliary until those ranks are wired. - available - .next() - .filter(|_| available.next().is_none()) - .map(|entry| entry.path) - }; - if let Some(path) = design { - selected.push((owner, class, path)); + if let Some(entry) = available.next().filter(|_| available.next().is_none()) { + selected.push(ScriptDesign { + owner, + class, + selection: SELECT_FASTEST, + path: entry.path, + }); + } } } } @@ -105,11 +138,11 @@ fn best_design(candidates: impl Iterator) -> Optio .map(|(path, _)| path) } -fn combat_rating( +fn design_ratings( assets: &AssetManager, vfs: &dyn Vfs, path: &NormalizedPath, -) -> Option { +) -> Option { let root = crate::resource_name(path.as_bytes()); let (graph, prototypes, report) = build_prototype_graph_report(assets.repository(), vfs, &[root]); @@ -149,18 +182,23 @@ fn combat_rating( }, ) .ok()?; - let volume = f32::from_bits(crate::initial_node_provider_amount( - &projection.node_provider_status, - )?); - let resistance = projection - .projectile_resistance_bits - .map_or(1.0, |[factor, _]| f32::from_bits(factor)); - // shortcut: selector 0x36 adds a shield term and selector 0x204 reads native bank totals. - let score = rating( - volume / resistance, - weapon_power(&projection.devices, &projection.node_provider_status), - ); - score.is_finite().then_some(score) + let combat = crate::initial_node_provider_amount(&projection.node_provider_status) + .map(|bits| { + let resistance = projection + .projectile_resistance_bits + .map_or(1.0, |[factor, _]| f32::from_bits(factor)); + // shortcut: selector 0x36 adds a shield term and selector 0x204 reads native bank totals. + rating( + f32::from_bits(bits) / resistance, + weapon_power(&projection.devices, &projection.node_provider_status), + ) + }) + .filter(|score| score.is_finite()); + let speed = projection + .owner_profile + .map(|profile| f32::from_bits(profile.current_words[7])) + .filter(|speed| speed.is_finite() && *speed > 0.0); + Some(DesignRatings { combat, speed }) } #[cfg(test)] diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index da5af6d8..85052120 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2055,9 +2055,9 @@ fn load_mission_with_options( } else { Vec::new() }; - for (_, _, path) in &ai_schemes { - if !build_schemes.contains(path) { - build_schemes.push(path.clone()); + for design in &ai_schemes { + if !build_schemes.contains(&design.path) { + build_schemes.push(design.path.clone()); } } let research_trf_tables = if options.asset_scope == MissionAssetScope::Full diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 957a9c5e..b33fbf1e 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -72,7 +72,7 @@ pub(super) struct Production { schemes: Vec, player_scheme_count: usize, customized: bool, - ai_schemes: Vec<(OwnerId, u32, NormalizedPath)>, + ai_schemes: Vec, controls: Vec, plants: Vec<(usize, u32)>, pending: VecDeque, @@ -170,7 +170,7 @@ impl Production { schemes: Vec, player_scheme_count: usize, customized: bool, - ai_schemes: Vec<(OwnerId, u32, NormalizedPath)>, + ai_schemes: Vec, controls: Vec, drafts: &[MissionObjectDraft], ) -> Result { @@ -577,21 +577,13 @@ pub(super) fn order_script_factory( .find(|&(_, handle, _)| handle == plant) .ok_or_else(|| production_error("script factory is unavailable"))?; let auxiliary = crate::clan_factory_schemes::AUXILIARY_CLASSES.contains(&class); - let use_catalog = (auxiliary && selection == crate::clan_factory_schemes::SELECT_FASTEST) - || (class == crate::boarding::WARBOT_CLASS - && selection == crate::clan_factory_schemes::SELECT_STRONGEST); - let ai_scheme = if use_catalog { - production - .ai_schemes - .iter() - .find_map(|(clan, kind, path)| (*clan == owner && *kind == class).then_some(path)) - } else { - None - }; + let ai_scheme = production.ai_schemes.iter().find(|design| { + design.owner == owner && design.class == class && design.selection == selection + }); let scheme = ai_scheme .and_then(|selected| { schemes_for_plant(mission, production, index) - .find(|&(_, path)| selected.as_str() == path) + .find(|&(_, path)| selected.path.as_str() == path) }) .or_else(|| { if auxiliary { @@ -824,6 +816,52 @@ mod tests { use fparkan_world::{set_transform, InputSnapshot, OriginalObjectId}; use std::sync::Arc; + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_new_small_factories_build_the_fast_combat_design() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.02/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + + let production = engine.production.as_ref().unwrap(); + for owner in [OwnerId(1), OwnerId(2)] { + let design = production + .ai_schemes + .iter() + .find(|design| { + design.owner == owner + && design.class == crate::boarding::WARBOT_CLASS + && design.selection == crate::clan_factory_schemes::SELECT_FASTEST + }) + .expect("eligible fast combat design"); + assert!( + design.path.as_str().ends_with("/12hel1.dat"), + "{}", + design.path.as_str() + ); + } + + let report = crate::simulate_mission_report(&mut engine, 180_000).unwrap(); + for clan in [1, 2] { + assert!( + report.first_factory_completion_ms.contains_key(&clan), + "clan {clan} did not build a warbot" + ); + } + } + #[test] #[ignore = "requires licensed corpus"] fn licensed_enemy_designs_leave_a_player_factory_slot_available() { @@ -1026,12 +1064,13 @@ mod tests { let cockpit = super::super::starting_cockpit(&engine).unwrap(); let factory = handle_by_original_id(&engine.world, OriginalObjectId(5)).unwrap(); let production = engine.production.as_ref().unwrap(); - let (owner, _, transport) = production + let design = production .ai_schemes .iter() - .find(|(_, class, _)| *class == crate::clan_factory_schemes::TRANSPORT_CLASS) + .find(|design| design.class == crate::clan_factory_schemes::TRANSPORT_CLASS) .expect("eligible AI transport"); - assert_eq!(*owner, OwnerId(1)); + assert_eq!(design.owner, OwnerId(1)); + let transport = &design.path; assert!(transport.as_str().ends_with("/sml_cargo.dat")); let transport_index = production .schemes -- cgit v1.2.3