From bbd6088b7fb1162a976bcfa1ca6720c2c91e8a72 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 15 Nov 2026 00:04:40 +0400 Subject: feat(ai): find and reserve sites for scripted construction Handler 18 scans base rings, checks terrain and clearance, and writes the site back to the script. The stock Two Fortresses window found a tower site and reached the builder order. The native DLL returned the expected site with and without a reservation, and zero for missing areas. The builder task remains explicit in coverage. --- crates/fparkan-runtime/src/clan_ai_events.rs | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'crates/fparkan-runtime/src/clan_ai_events.rs') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 676db9cf..17aab8a9 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -13,6 +13,7 @@ const STORAGE_CLASS: u32 = 0x8000_0008; const RESOURCE_ORE: u32 = 0x0200_0100; const BUILDING_PROTECT: u32 = 8; const OPTIMAL_TRANSPORT: u32 = 4; +const BUILDING_NEEDED: u32 = 3; pub(super) struct ScriptFactoryOrder { pub owner: OwnerId, @@ -232,6 +233,8 @@ pub(super) struct ClanAiEvents { groups: BTreeMap<(usize, Handler2RecordKey), CombatGroup>, waits: BTreeMap<(usize, Handler2RecordKey), Vec>, wait_starts: BTreeMap<(usize, u32), u64>, + building_sites: crate::construction_site::SiteReservations, + building_site_queries: BTreeMap<(usize, u32), u64>, patrol_continues: BTreeMap<(usize, u32), u64>, group_orders: BTreeMap<(usize, u32), u64>, } @@ -537,6 +540,13 @@ impl LoadedMissionState { .declarations .resolve_handler15_with_values(instruction, values) .map_err(|error| error.to_string())?; + if matches!((order.word_1, order.word_2, order.target), + (7, 3, Handler15TargetPayload::ByPlace { .. })) { + return Err(format!( + "ORDER_ROBOT_BUILD needs prepared scheme and builder task (builder={} class={:#010x})", + order.word_0, order.word_3, + )); + } if let (6, 3, Handler15TargetPayload::NotDefined(_), Some(problem)) = (order.word_1, order.word_2, order.target, problem.as_ref()) { @@ -902,6 +912,30 @@ impl LoadedMissionState { crate::mining::remaining_for_clan(&self.mine_sites, world, owner); Ok(Some(VarSetDefault::FloatBits(remaining.to_bits()))) } + ScriptDispatchSelector::Handler(18) + if problem.as_ref().is_some_and(|record| record.key.word_0 == BUILDING_NEEDED) => + { + let &[x, y] = instruction.references.as_slice() else { + return Err("Handler(18) requires two DWORD outputs".into()); + }; + if x == y || [x, y].into_iter().any(|index| !matches!(values.get(index as usize), Some(VarSetDefault::Dword(_)))) { + return Err("Handler(18) requires two distinct DWORD outputs".into()); + } + let base = self.mission.clans[clan].anchor.map(f32::round_ties_even); + let position = self.clan_ai_events.building_sites.find( + &self.terrain, world, &self.object_drafts, &self.mine_sites, base, now_ms, + ); + let [px, py] = position.unwrap_or([0, 0]); + values[x as usize] = VarSetDefault::Dword(px); + values[y as usize] = VarSetDefault::Dword(py); + let class = problem.as_ref().unwrap().key.argument_0; + let count = self.clan_ai_events.building_site_queries.entry((clan, class)).or_default(); + *count += 1; + if *count == 1 { + eprintln!("sim_coverage script_building_site_approx=1 clan={clan} class={class:#010x} site={position:?}"); + } + Ok(Some(VarSetDefault::Dword(1))) + } ScriptDispatchSelector::Handler(49) if instruction.references.is_empty() => { @@ -1195,6 +1229,12 @@ impl ClanAiEvents { .map(|(&(clan, unit), &count)| (clan, unit, count)) } + pub(super) fn building_site_queries(&self) -> impl Iterator + '_ { + self.building_site_queries + .iter() + .map(|(&(clan, class), &count)| (clan, class, count)) + } + pub(super) fn set_base_radius(&mut self, clan: usize, limit: f32) { if limit.is_finite() && limit > 0.0 { self.base_radius_limits.insert(clan, limit); -- cgit v1.2.3