diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 178 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/clan_combat_groups.rs | 15 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/clan_targeting.rs | 22 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 19 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 35 |
6 files changed, 212 insertions, 63 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] diff --git a/crates/fparkan-runtime/src/clan_combat_groups.rs b/crates/fparkan-runtime/src/clan_combat_groups.rs index 50c86e16..b0b0de11 100644 --- a/crates/fparkan-runtime/src/clan_combat_groups.rs +++ b/crates/fparkan-runtime/src/clan_combat_groups.rs @@ -1,6 +1,6 @@ use crate::clan_combat_strength::unit_strength; use crate::unit_patrol::UnitPatrol; -use crate::{MissionControlDeviceTableState, MissionDocument, MissionObjectDraft}; +use crate::{MissionControlDeviceTableState, MissionObjectDraft}; use fparkan_script::{Handler15Invocation, Handler15TargetPayload}; use fparkan_world::{identity_metadata, transform_state, ObjectHandle, OwnerId, World}; @@ -19,13 +19,12 @@ pub(super) enum GroupTarget { impl GroupTarget { fn resolve( world: &World, - mission: &MissionDocument, + lookup: impl FnOnce(u32) -> Option<ObjectHandle>, target: Handler15TargetPayload, ) -> Option<Self> { match target { Handler15TargetPayload::ByLogicId(id) => { - let handle = - crate::mission_events::placed_logic_objects(mission, world, id).next()?; + let handle = lookup(id)?; Some(Self::Object( handle, identity_metadata(world, handle).ok()?.owner_id, @@ -94,11 +93,11 @@ pub(super) fn recruit( world: &World, units: impl Iterator<Item = ObjectHandle>, controls: &[MissionControlDeviceTableState], - mission: &MissionDocument, + lookup: impl FnOnce(u32) -> Option<ObjectHandle>, target: Handler15TargetPayload, requested: u32, ) -> (Option<CombatGroup>, f32) { - let Some(target) = GroupTarget::resolve(world, mission, target) else { + let Some(target) = GroupTarget::resolve(world, lookup, target) else { return (None, 0.0); }; let Some([x, y]) = target.position(world) else { @@ -143,7 +142,7 @@ impl CombatGroup { &mut self, patrol: &mut UnitPatrol, world: &World, - mission: &MissionDocument, + lookup: impl FnOnce(u32) -> Option<ObjectHandle>, drafts: &[MissionObjectDraft], order: Handler15Invocation, ) -> Result<(), String> { @@ -153,7 +152,7 @@ impl CombatGroup { "Handler(28) requires a supported patrol or attack order on group 0".into(), ); } - let target = GroupTarget::resolve(world, mission, order.target) + let target = GroupTarget::resolve(world, lookup, order.target) .ok_or("Handler(28) target is unavailable")?; for &unit in &self.members { let class = identity_metadata(world, unit) diff --git a/crates/fparkan-runtime/src/clan_targeting.rs b/crates/fparkan-runtime/src/clan_targeting.rs index 4e710bca..78967333 100644 --- a/crates/fparkan-runtime/src/clan_targeting.rs +++ b/crates/fparkan-runtime/src/clan_targeting.rs @@ -39,7 +39,7 @@ pub(super) fn send_capturer( let index = target.original_id?.0 as usize; let class = drafts.get(index)?.unit_dat_header_flags?; if unit.target_id != target_id - || target_id & 0x8000_0000 == 0 + || class & 0x8000_0000 == 0 || target.owner_id == identity_metadata(world, unit.handle).ok()?.owner_id || class == 0x8000_0200 { @@ -67,7 +67,7 @@ pub(super) fn send_capturer( let indoor_route = passage.is_some(); patrol .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted) - .move_to_capture(world, unit.handle, destination, passage); + .move_to_capture(world, unit.handle, unit.target, destination, passage); if !indoor_route { eprintln!( "sim_coverage script_capture_approach_stub=1 unit={} target={} target_id={target_id} destination={destination:?}", @@ -97,6 +97,16 @@ fn distance_squared(from: [f32; 3], to: [f32; 3]) -> f32 { } impl CaptureApproach { + pub(super) fn is_active( + self, + world: &World, + patrol: Option<&crate::unit_patrol::UnitPatrol>, + owner: OwnerId, + ) -> bool { + identity_metadata(world, self.unit).is_ok_and(|identity| identity.owner_id == Some(owner)) + && patrol.is_some_and(|patrol| patrol.has_capture_order(self.unit, self.target)) + } + pub(super) fn reached(self, world: &World) -> bool { transform_state(world, self.unit) .is_ok_and(|pose| at_platform(pose.position.map(f32::from_bits), self.point)) @@ -116,7 +126,7 @@ pub(super) fn nearest_capturer<'a>( target_id: u32, available_control: impl Fn(ObjectHandle) -> Option<&'a MissionControlDeviceTableState>, ) -> Option<Capturer> { - let target = crate::mission_events::placed_logic_objects(mission, world, target_id).next()?; + let target = roster.logic_objects(mission, world, target_id).next()?; let point = transform_state(world, target) .ok()? .position @@ -137,11 +147,7 @@ pub(super) fn nearest_capturer<'a>( .map(f32::from_bits); let score = capture_arrival_score(position, point, f32::from_bits(profile.current_words[7]))?; - // shortcut: generated ID is local to this script section until native robot IDs are allocated. - let id = mission - .objects - .get(index) - .map_or(original_id, |object| object.identity_or_clan_raw); + let id = roster.logic_id(mission, world, handle)?; (id != u32::MAX).then_some(( Capturer { handle, diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index aa9b804a..c0db14ab 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -339,14 +339,14 @@ impl Production { let class = draft .unit_dat_header_flags .expect("prepared factory object has a class"); + mission + .mission_events + .issue_logic_id(&mission.mission, world, handle); if crate::clan_factory_schemes::AUXILIARY_CLASSES.contains(&class) { let id = draft .original_id .expect("prepared factory object has an ID"); mission - .mission_events - .issue_logic_id(&mission.mission, world, handle); - mission .behavior_ore_states .retain(|ore| ore.original_id != id); mission diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 59965b4c..44e83986 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -57,6 +57,7 @@ struct PatrollingUnit { attack_target: Option<(ObjectHandle, usize)>, raid_factory: Option<(ObjectHandle, usize)>, ground_support: Option<Option<GroundSupport>>, + capture_target: Option<ObjectHandle>, capture_passage: Option<CapturePassage>, animal_clearance: Option<f32>, } @@ -192,6 +193,8 @@ impl UnitPatrol { .retain(|unit| transform_state(world, unit.handle).is_ok()); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.raid_factory = None; + unit.capture_target = None; + unit.capture_passage = None; let was_attacking = unit.attack_target.take().is_some(); let was_moving = unit.destinations.take().is_some(); if unit.center_object.take().is_some() @@ -222,6 +225,7 @@ impl UnitPatrol { attack_target: None, raid_factory: None, ground_support: None, + capture_target: None, capture_passage: None, animal_clearance: None, }); @@ -258,11 +262,13 @@ impl UnitPatrol { &mut self, world: &World, handle: ObjectHandle, + building: ObjectHandle, destination: [f32; 2], passage: Option<CapturePassage>, ) { self.move_to(world, handle, destination, false); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { + unit.capture_target = Some(building); unit.capture_passage = passage; } } @@ -311,14 +317,10 @@ impl UnitPatrol { .filter_map(|route| route.opening) } - pub(super) fn clear_capture_passage(&mut self, unit: ObjectHandle, building: ObjectHandle) { - self.units.retain(|patrol| { - patrol.handle != unit - || patrol - .capture_passage - .as_ref() - .is_none_or(|route| route.building != building) - }); + pub(super) fn has_capture_order(&self, unit: ObjectHandle, building: ObjectHandle) -> bool { + self.units + .iter() + .any(|order| order.handle == unit && order.capture_target == Some(building)) } pub(super) fn order_around_object( @@ -484,6 +486,7 @@ impl UnitPatrol { attack_target: None, raid_factory: None, ground_support: None, + capture_target: None, capture_passage: None, animal_clearance: None, }) diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index cd21e4ff..cde2bd09 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -50,6 +50,21 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn clear_capture_order(&mut self, unit: ObjectHandle, building: ObjectHandle) { + self.units + .retain(|order| order.handle != unit || order.capture_target != Some(building)); + if let Some((_, queue)) = self + .script_orders + .iter_mut() + .find(|(handle, _)| *handle == unit) + { + queue.retain(|order| { + !matches!(order, UnitOrder::Resume(state) if state.capture_target == Some(building)) + }); + } + self.script_orders.retain(|(_, queue)| !queue.is_empty()); + } + pub(crate) fn has_object_order(&self, handle: ObjectHandle, target: ObjectHandle) -> bool { self.units .iter() @@ -266,6 +281,9 @@ impl UnitPatrol { .iter() .find(|unit| unit.handle == actor) .is_none_or(|unit| { + if unit.capture_target.is_some() { + return false; + } if let Some(points) = &unit.destinations { let [x, y, _] = pose.position.map(f32::from_bits); return points.is_empty() @@ -395,6 +413,23 @@ mod tests { } #[test] + fn capture_holds_an_appended_order_until_released() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let building = object(&mut world); + let mut patrol = UnitPatrol::scripted(); + patrol.move_to_capture(&world, actor, building, [10.0, 0.0], None); + patrol.enqueue_script_order(&world, actor, UnitOrder::Shutdown, true); + place(&mut world, actor, 10.0); + patrol.advance_script_orders(&world, &[]); + assert!(patrol.has_capture_order(actor, building)); + assert!(!patrol.is_shut_down(actor)); + patrol.clear_capture_order(actor, building); + patrol.advance_script_orders(&world, &[]); + assert!(patrol.is_shut_down(actor)); + } + + #[test] fn attack_holds_an_appended_patrol_until_the_target_is_removed() { let mut world = fparkan_world::new(Default::default()); let actor = object(&mut world); |
