aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_ai_events.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 18:34:08 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 18:34:08 +0300
commit2d01cd35b069d0d1aa78a064593b54f51b23540d (patch)
tree07fe0aa111c41cadc6b14548881731886dc8f0d1 /crates/fparkan-runtime/src/clan_ai_events.rs
parent72ae7666d428c783a03b5378658890947f205de0 (diff)
downloadfparkan-2d01cd35b069d0d1aa78a064593b54f51b23540d.tar.xz
fparkan-2d01cd35b069d0d1aa78a064593b54f51b23540d.zip
fix(ai): resume building capture after losing its assignee
Diffstat (limited to 'crates/fparkan-runtime/src/clan_ai_events.rs')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs178
1 files changed, 142 insertions, 36 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index 2373f220..715e2cc4 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -16,6 +16,7 @@ const STORAGE_CLASS: u32 = 0x8000_0008;
const RESOURCE_ORE: u32 = 0x0200_0100;
const BUILDING_PROTECT: u32 = 8;
const PLACE_PROTECT: u32 = 11;
+const BUILDING_INF_CAPTURE: u32 = 14;
const OPTIMAL_TRANSPORT: u32 = 4;
const BUILDING_NEEDED: u32 = 3;
const MINE_NEEDED: u32 = 1;
@@ -40,8 +41,7 @@ pub(super) fn resolve_captures(
let mut captured = false;
let mut index = 0;
while let Some(&(owner, key, approach)) = loaded.clan_ai_events.capture_orders.get(index) {
- let valid_unit = identity_metadata(&engine.world, approach.unit)
- .is_ok_and(|identity| identity.owner_id == Some(owner));
+ let valid_unit = approach.is_active(&engine.world, loaded.unit_patrol.as_ref(), owner);
let target = identity_metadata(&engine.world, approach.target).ok();
let target = target
.and_then(|identity| identity.original_id.zip(identity.owner_id))
@@ -72,12 +72,14 @@ pub(super) fn resolve_captures(
}
}
if let Some(patrol) = &mut loaded.unit_patrol {
- patrol.clear_capture_passage(approach.unit, approach.target);
+ patrol.clear_capture_order(approach.unit, approach.target);
}
loaded.clan_ai_events.capture_orders.remove(index);
let clan = usize::from(owner.0);
let ai = &mut loaded.clan_ai_events;
- if !valid_unit || !ai.waits.contains_key(&(clan, key)) {
+ if (!valid_unit && key.word_0 != BUILDING_INF_CAPTURE)
+ || !ai.waits.contains_key(&(clan, key))
+ {
if let Some(queue) = ai.problems.get_mut(&clan) {
queue.remove(key);
}
@@ -351,8 +353,10 @@ impl LoadedMissionState {
.flat_map(|queue| queue.records())
.rev()
.filter(|record| {
- matches!(record.key.word_0, BUILDING_PROTECT | PLACE_PROTECT)
- && self.clan_ai_events.waits.contains_key(&(clan, record.key))
+ matches!(
+ record.key.word_0,
+ BUILDING_PROTECT | PLACE_PROTECT | BUILDING_INF_CAPTURE
+ ) && self.clan_ai_events.waits.contains_key(&(clan, record.key))
})
.cloned()
.collect::<Vec<_>>();
@@ -392,7 +396,7 @@ impl LoadedMissionState {
if *count == 1 {
eprintln!("sim_coverage script_point_patrol_continue_approx=1 clan={clan} x={x} y={y}");
}
- } else {
+ } else if problem_id == BUILDING_PROTECT {
let target = problem.key.argument_0;
let count = self
.clan_ai_events
@@ -655,7 +659,10 @@ impl LoadedMissionState {
for previous in self.clan_ai_events.groups.values_mut() {
previous.members.retain(|unit| !recruited.members.contains(unit));
}
- self.clan_ai_events.capture_orders.retain(|(_, _, order)| !recruited.members.contains(&order.unit));
+ self.clan_ai_events.release_capture_orders(
+ &mut self.unit_patrol,
+ |(_, _, order)| recruited.members.contains(&order.unit),
+ );
}
}
if first_recruitment {
@@ -676,8 +683,9 @@ impl LoadedMissionState {
_ => return Err("Handler(25) requires a logical ID or XY point".into()),
};
// shortcut: use free warbots until lower-priority orders can be preempted.
+ let lookup = |id| self.mission_events.logic_objects(&self.mission, world, id).next();
let (recruited, strength) = crate::clan_combat_groups::recruit(
- world, units, &self.control_device_tables, &self.mission, target, requested,
+ world, units, &self.control_device_tables, lookup, target, requested,
);
if strength < requested as f32 || strength == 0.0 {
values[hits as usize] = VarSetDefault::Dword(strength as u32);
@@ -694,7 +702,8 @@ impl LoadedMissionState {
.map_err(|error| error.to_string())?;
let mut group = group.take().ok_or("Handler(28) requires an active combat group")?;
let patrol = self.unit_patrol.get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted);
- group.dispatch(patrol, world, &self.mission, &self.object_drafts, order)?;
+ let lookup = |id| self.mission_events.logic_objects(&self.mission, world, id).next();
+ group.dispatch(patrol, world, lookup, &self.object_drafts, order)?;
let key = problem.as_ref().expect("group order requires a problem").key;
let (name, count) = match group.target {
GroupTarget::Unassigned => return Err("Handler(28) group has no assigned order".into()),
@@ -964,21 +973,34 @@ impl LoadedMissionState {
});
Ok(None)
}
- ScriptDispatchSelector::Handler(handler @ (7 | 45))
+ ScriptDispatchSelector::Handler(50)
+ if problem.as_ref().is_some_and(|record| record.key.word_0 == BUILDING_INF_CAPTURE) =>
+ {
+ if single_dword(&instruction.references, values) != Some(0x11) {
+ return Err("Handler(50) requires ORDER_ROBOT_CAPTURE".into());
+ }
+ let key = problem.as_ref().expect("recapture problem").key;
+ let owner = OwnerId(clan as u16);
+ let assigned = self.clan_ai_events.capture_orders.iter().find_map(|&(clan, problem, order)| {
+ (clan == owner && problem == key && order.is_active(world, self.unit_patrol.as_ref(), owner))
+ .then(|| self.mission_events.logic_id(&self.mission, world, order.unit))
+ .flatten()
+ });
+ Ok(Some(VarSetDefault::Dword(assigned.unwrap_or(u32::MAX))))
+ }
+ ScriptDispatchSelector::Handler(7)
+ if instruction.references.is_empty()
+ && problem.as_ref().is_some_and(|record| matches!(record.key.word_0, BUILDING_PROTECT | PLACE_PROTECT | BUILDING_INF_CAPTURE)) =>
+ {
+ let key = problem.as_ref().expect("assigned problem").key;
+ self.clan_ai_events.release_problem_orders(clan, key, &mut self.unit_patrol);
+ Ok(Some(VarSetDefault::Dword(0)))
+ }
+ ScriptDispatchSelector::Handler(45)
if instruction.references.is_empty()
&& problem.as_ref().is_some_and(|record| matches!(record.key.word_0, BUILDING_PROTECT | PLACE_PROTECT)) =>
{
let key = problem.as_ref().expect("protection problem").key;
- if handler == 7 {
- if let Some(assigned) = self.clan_ai_events.groups.remove(&(clan, key)) {
- if let Some(patrol) = &mut self.unit_patrol {
- for unit in assigned.members {
- assigned.target.clear_order(patrol, unit);
- }
- }
- }
- return Ok(None);
- }
let strength = self.clan_ai_events.groups.get(&(clan, key))
.into_iter().flat_map(|assigned| &assigned.members)
.map(|&unit| crate::clan_combat_strength::unit_strength(&self.control_device_tables, unit))
@@ -1359,9 +1381,21 @@ impl LoadedMissionState {
&& (!conditions.is_empty()
|| matches!(
problem.key.word_0,
- OPTIMAL_TRANSPORT | BUILDING_PROTECT | PLACE_PROTECT
+ OPTIMAL_TRANSPORT
+ | BUILDING_PROTECT
+ | PLACE_PROTECT
+ | BUILDING_INF_CAPTURE
))
{
+ if problem.key.word_0 == BUILDING_INF_CAPTURE && conditions.is_empty() {
+ // The no-capturer branch can enter ST_SOLVING before it registers
+ // the target conditions. Retain the target through reassignment.
+ let target = problem.key.argument_0;
+ conditions.extend([
+ Condition::ObjectDestroyed(target),
+ Condition::BuildingCaptured(target),
+ ]);
+ }
let count = self
.clan_ai_events
.wait_starts
@@ -1445,6 +1479,40 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32
}
impl ClanAiEvents {
+ fn release_problem_orders(
+ &mut self,
+ clan: usize,
+ key: Handler2RecordKey,
+ patrol: &mut Option<crate::unit_patrol::UnitPatrol>,
+ ) {
+ if let Some(assigned) = self.groups.remove(&(clan, key)) {
+ if let Some(patrol) = patrol.as_mut() {
+ for unit in assigned.members {
+ assigned.target.clear_order(patrol, unit);
+ }
+ }
+ }
+ self.release_capture_orders(patrol, |(owner, problem, _)| {
+ usize::from(owner.0) == clan && problem == key
+ });
+ }
+
+ fn release_capture_orders(
+ &mut self,
+ patrol: &mut Option<crate::unit_patrol::UnitPatrol>,
+ matches: impl Fn((OwnerId, Handler2RecordKey, CaptureApproach)) -> bool,
+ ) {
+ self.capture_orders.retain(|job| {
+ if !matches(*job) {
+ return true;
+ }
+ if let Some(patrol) = patrol.as_mut() {
+ patrol.clear_capture_order(job.2.unit, job.2.target);
+ }
+ false
+ });
+ }
+
pub(super) fn submit_problem(
&mut self,
clan: usize,
@@ -1781,7 +1849,7 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
- fn licensed_enemy_factory_capture_queues_a_counterattack() {
+ fn licensed_enemy_factory_recapture_survives_interruption_and_death() {
use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput};
use fparkan_vfs::DirectoryVfs;
use fparkan_world::{
@@ -1842,29 +1910,67 @@ mod tests {
.unwrap()
.records()
.iter()
- .find(|record| record.key.word_0 == 14 && record.key.argument_0 == logic_id)
+ .find(|record| {
+ record.key.word_0 == BUILDING_INF_CAPTURE && record.key.argument_0 == logic_id
+ })
.expect("Fort_Captured requested recapture for the former owner");
assert!((request.scalar_1 - 0.85).abs() < 1e-6);
assert!(!ai.problems.contains_key(&0));
let key = request.key;
- // The isolated capture must not also put the hero in the clan's
- // immediate defence radius, which can take over the capturer.
+ // Return the hero to the bunker to isolate recapture from base defence.
set_transform(&mut engine.world, hero.handle, start).unwrap();
- for now in (0..=10_000).step_by(100) {
- crate::frame(&mut engine, now).unwrap();
+ let current_capturer = |engine: &crate::Engine| {
let ai = &engine.loaded.as_ref().unwrap().clan_ai_events;
- if ai
- .capture_orders
+ ai.capture_orders
.iter()
- .any(|&(owner, assigned, approach)| {
- owner == OwnerId(1) && assigned == key && approach.target == factory
+ .find_map(|&(owner, assigned, approach)| {
+ (owner == OwnerId(1) && assigned == key && approach.target == factory)
+ .then_some(approach.unit)
})
- {
- return;
- }
+ };
+ let (first_ms, first) = (0..=10_000)
+ .step_by(100)
+ .find_map(|now| {
+ crate::frame(&mut engine, now).unwrap();
+ current_capturer(&engine).map(|unit| (now, unit))
+ })
+ .expect("the script sends a capturer to retake the lost factory");
+ let loaded = engine.loaded.as_mut().unwrap();
+ let [x, y, _] = start.position.map(f32::from_bits);
+ loaded
+ .unit_patrol
+ .as_mut()
+ .unwrap()
+ .move_to(&engine.world, first, [x, y], false);
+ assert!(!loaded
+ .unit_patrol
+ .as_ref()
+ .unwrap()
+ .has_capture_order(first, factory));
+ let (resume_ms, resumed) = (first_ms + 100..=first_ms + 25_000)
+ .step_by(100)
+ .find_map(|now| {
+ crate::frame(&mut engine, now).unwrap();
+ current_capturer(&engine).map(|unit| (now, unit))
+ })
+ .expect("the script reassigns capture after the move order");
+ assert!(engine
+ .loaded
+ .as_ref()
+ .unwrap()
+ .unit_patrol
+ .as_ref()
+ .unwrap()
+ .has_capture_order(resumed, factory));
+ fparkan_world::request_delete(&mut engine.world, resumed).unwrap();
+ for now in (resume_ms + 100..=resume_ms + 12_000).step_by(100) {
+ crate::frame(&mut engine, now).unwrap();
}
- panic!("the script must send a capturer to retake the lost factory");
+ assert_ne!(current_capturer(&engine), Some(resumed));
+ let ai = &engine.loaded.as_ref().unwrap().clan_ai_events;
+ assert!(ai.waits.contains_key(&(1, key)));
+ assert_eq!(ai.started.get(&(1, BUILDING_INF_CAPTURE)), Some(&1));
}
#[test]