diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 19:58:58 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 19:58:58 +0300 |
| commit | 77ab4031f5c140ae5bce4096abab0552a0675ef9 (patch) | |
| tree | 64763b31961c71bcd6591d65c3631e51b083f145 /crates | |
| parent | e5e9f2a8ddc41f0e1af0e1ada5c1da669d40ceb0 (diff) | |
| download | fparkan-77ab4031f5c140ae5bce4096abab0552a0675ef9.tar.xz fparkan-77ab4031f5c140ae5bce4096abab0552a0675ef9.zip | |
feat(ai): dispatch scripted attacks against logical targets
Resolve the stock logical target and feed it to unit combat. Mission 20 assigns the attack at startup and continues through the remaining Init commands. All 29 original missions completed 15 minutes without a hard Init or periodic Mission stop.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_script_init.rs | 12 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 14 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/script_patrol.rs | 26 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 17 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 53 |
5 files changed, 121 insertions, 1 deletions
diff --git a/crates/fparkan-runtime/src/clan_script_init.rs b/crates/fparkan-runtime/src/clan_script_init.rs index fedc8ee9..3e102be3 100644 --- a/crates/fparkan-runtime/src/clan_script_init.rs +++ b/crates/fparkan-runtime/src/clan_script_init.rs @@ -5,6 +5,10 @@ use fparkan_script::{ pub(super) enum InitOrder { Patrol(ScriptPatrol), + Attack { + id: u32, + target: u32, + }, Move { id: u32, destination: [u32; 2], @@ -46,6 +50,14 @@ pub(super) fn unit_order( let order = declarations .resolve_handler15_with_values(instruction, values) .ok()?; + if let (3, 3, Handler15TargetPayload::ByLogicId(target)) = + (order.word_1, order.word_2, order.target) + { + return Some(InitOrder::Attack { + id: order.word_0, + target, + }); + } if let (2, insertion @ (1 | 3), Handler15TargetPayload::ByPlace { first, second }) = (order.word_1, order.word_2, order.target) { diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 9996aca1..9fa36f84 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2446,6 +2446,7 @@ fn load_mission_with_options( matches!( order, clan_script_init::InitOrder::Patrol(_) + | clan_script_init::InitOrder::Attack { .. } | clan_script_init::InitOrder::Move { .. } | clan_script_init::InitOrder::Shutdown(_) ) @@ -2541,6 +2542,19 @@ fn load_mission_with_options( clan_script_init::InitOrder::Reserve { clan, id, reserved } => { mission_events.set_reservation(&mission, &new_runtime_world, clan, id, reserved); } + clan_script_init::InitOrder::Attack { id, target } => { + let Some(patrol) = &mut unit_patrol else { + continue; + }; + script_patrol::apply_attack_order( + patrol, + &mission, + &object_drafts, + &new_runtime_world, + id, + target, + ); + } clan_script_init::InitOrder::Move { id, destination, diff --git a/crates/fparkan-runtime/src/script_patrol.rs b/crates/fparkan-runtime/src/script_patrol.rs index 427dc397..0d19dbef 100644 --- a/crates/fparkan-runtime/src/script_patrol.rs +++ b/crates/fparkan-runtime/src/script_patrol.rs @@ -5,6 +5,32 @@ use fparkan_world::{ handle_by_original_id, transform_state, ObjectHandle, OriginalObjectId, World, }; +pub(super) fn apply_attack_order( + patrol: &mut UnitPatrol, + mission: &MissionDocument, + drafts: &[MissionObjectDraft], + world: &World, + id: u32, + target_id: u32, +) -> Option<()> { + let actor = warbot_by_logic_id(mission, drafts, world, id)?; + let target = crate::mission_events::placed_logic_objects(mission, world, target_id).next()?; + let index = fparkan_world::identity_metadata(world, target) + .ok()? + .original_id? + .0 as usize; + let [x, y, _] = transform_state(world, target) + .ok()? + .position + .map(f32::from_bits); + patrol.set_attack_target(world, actor, target, index, [x, y]); + eprintln!( + "sim_coverage script_attack_approx=1 unit={} id={id} target_id={target_id} target_unit={}", + actor.slot, target.slot + ); + Some(()) +} + pub(super) fn warbot_by_logic_id( mission: &MissionDocument, drafts: &[MissionObjectDraft], diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index 97544f6f..03851e36 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -251,6 +251,23 @@ impl AutoDemoCombat { }) { return None; } + if let Some((target, index)) = + patrol.and_then(|patrol| patrol.attack_target(handle)) + { + // shortcut: aim at the target bounds until the original Attack task is wired. + let state = transform_state(world, target).ok()?; + let (center, radius) = unit_sphere(assets, index, state)?; + let distance = (center[0] - from[0]).hypot(center[1] - from[1]); + return Some(( + handle, + CombatTarget { + handle: target, + center, + radius, + in_range: distance <= FIRE_RANGE, + }, + )); + } let limit = patrol.and_then(|patrol| patrol.attack_limit(world, handle)); if limit.is_some_and(|limit| !limit.allows_attack(position)) { return None; diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index ee5b359c..c4b3b969 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -31,6 +31,7 @@ struct PatrollingUnit { points: VecDeque<[f32; 2]>, retry_ms: u32, attack: Option<ObjectHandle>, + attack_target: Option<(ObjectHandle, usize)>, raid_factory: Option<(ObjectHandle, usize)>, } @@ -155,9 +156,11 @@ 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; + let was_attacking = unit.attack_target.take().is_some(); let was_moving = unit.destinations.take().is_some(); if unit.center_object.take().is_some() || was_moving + || was_attacking || unit.center != center || unit.radius != radius { @@ -178,6 +181,7 @@ impl UnitPatrol { points: VecDeque::new(), retry_ms: 0, attack: None, + attack_target: None, raid_factory: None, }); true @@ -218,7 +222,10 @@ impl UnitPatrol { radius: f32, ) -> bool { if self.units.iter().any(|unit| { - unit.handle == handle && unit.center_object == Some(target) && unit.radius == radius + unit.handle == handle + && unit.attack_target.is_none() + && unit.center_object == Some(target) + && unit.radius == radius }) { return false; } @@ -258,6 +265,28 @@ impl UnitPatrol { .raid_factory } + pub(super) fn set_attack_target( + &mut self, + world: &World, + handle: ObjectHandle, + target: ObjectHandle, + target_index: usize, + center: [f32; 2], + ) { + self.order(world, handle, center, 60.0); + if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { + unit.center_object = Some(target); + unit.attack_target = Some((target, target_index)); + } + } + + pub(super) fn attack_target(&self, handle: ObjectHandle) -> Option<(ObjectHandle, usize)> { + self.units + .iter() + .find(|unit| unit.handle == handle)? + .attack_target + } + pub(super) fn attack_limit( &self, world: &World, @@ -307,6 +336,7 @@ impl UnitPatrol { points: VecDeque::new(), retry_ms: 0, attack: None, + attack_target: None, raid_factory: None, }) .collect(); @@ -400,6 +430,8 @@ impl UnitPatrol { .transpose()? .flatten() .unwrap_or_default() + } else if unit.attack_target.is_some() { + VecDeque::new() } else { choose_route(terrain, from, center, unit.radius, &mut state.rng)? }; @@ -444,6 +476,25 @@ impl UnitPatrol { } } +#[cfg(test)] +#[test] +fn new_patrol_replaces_a_script_attack_order() { + let world = fparkan_world::new(fparkan_world::WorldConfig::default()); + let actor = ObjectHandle { + slot: 0, + generation: 1, + }; + let target = ObjectHandle { + slot: 1, + generation: 1, + }; + let mut patrol = UnitPatrol::scripted(); + patrol.set_attack_target(&world, actor, target, 1, [50.0, 60.0]); + assert_eq!(patrol.attack_target(actor), Some((target, 1))); + assert!(patrol.order_around_object(&world, actor, target, [50.0, 60.0], 60.0)); + assert_eq!(patrol.attack_target(actor), None); +} + impl PatrollingUnit { fn center(&self, world: &World) -> Option<[f32; 2]> { // shortcut: use the target's transform as the patrol center until native building approaches are wired. |
