diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 21:38:25 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 21:38:25 +0300 |
| commit | 04ac1b927abde1e5e448718f94834de83a01fff8 (patch) | |
| tree | 700bb593deccdd7d69ed5b4fdedda1a25f630573 /crates/fparkan-runtime/src/clan_targeting.rs | |
| parent | e1bf8981fc92dd134da925254f10721f30941369 (diff) | |
| download | fparkan-04ac1b927abde1e5e448718f94834de83a01fff8.tar.xz fparkan-04ac1b927abde1e5e448718f94834de83a01fff8.zip | |
feat(ai): send scripted capturers toward hostile buildings
Single.01 dispatches its placed warbot and later factory robots from stock startup. Approach uses the shared movement task. Ownership transfer, native approach points and the script completion expressions remain open.
Diffstat (limited to 'crates/fparkan-runtime/src/clan_targeting.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_targeting.rs | 126 |
1 files changed, 125 insertions, 1 deletions
diff --git a/crates/fparkan-runtime/src/clan_targeting.rs b/crates/fparkan-runtime/src/clan_targeting.rs index f8c1b4ca..05ac970a 100644 --- a/crates/fparkan-runtime/src/clan_targeting.rs +++ b/crates/fparkan-runtime/src/clan_targeting.rs @@ -3,9 +3,99 @@ use crate::{ MissionObjectDraft, }; use fparkan_world::{ - handle_by_original_id, identity_metadata, registration_sequence, transform_state, World, + handle_by_original_id, identity_metadata, registration_sequence, transform_state, ObjectHandle, + OwnerId, World, }; +#[derive(Clone, Copy)] +pub(super) struct Capturer { + handle: ObjectHandle, + target: ObjectHandle, + pub(super) id: u32, + target_id: u32, +} + +pub(super) fn send_capturer( + patrol: &mut Option<crate::unit_patrol::UnitPatrol>, + world: &World, + drafts: &[MissionObjectDraft], + unit: Capturer, + target_id: u32, +) -> Option<(ObjectHandle, ObjectHandle)> { + let pose = transform_state(world, unit.target).ok()?; + let target = identity_metadata(world, unit.target).ok()?; + if unit.target_id != target_id + || target_id & 0x8000_0000 == 0 + || target.owner_id == identity_metadata(world, unit.handle).ok()?.owner_id + || target + .original_id + .and_then(|id| drafts.get(id.0 as usize)) + .and_then(|draft| draft.unit_dat_header_flags) + == Some(0x8000_0200) + { + return None; + } + let [x, y, _] = pose.position.map(f32::from_bits); + // shortcut: route towards the building; native entrance selection and capture are still open. + patrol + .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted) + .move_to(world, unit.handle, [x, y], false); + eprintln!( + "sim_coverage script_capture_approach_stub=1 unit={} target={} target_id={target_id}", + unit.handle.slot, unit.target.slot + ); + Some((unit.handle, unit.target)) +} + +pub(super) fn nearest_capturer( + mission: &MissionDocument, + drafts: &[MissionObjectDraft], + roster: &crate::mission_events::MissionEvents, + world: &World, + owner: OwnerId, + target_id: u32, + available: impl Fn(ObjectHandle) -> bool, +) -> Option<Capturer> { + let target = crate::mission_events::placed_logic_objects(mission, world, target_id).next()?; + let point = transform_state(world, target) + .ok()? + .position + .map(f32::from_bits); + // shortcut: all routed robots currently share one speed, so travel time ranks by XY distance. + roster + .clan_units(world, owner, true) + .filter(|&handle| available(handle)) + .filter_map(|handle| { + let original_id = identity_metadata(world, handle).ok()?.original_id?.0; + let index = original_id as usize; + let chassis = drafts.get(index)?.unit_components.first()?; + if !crate::mission_events::capturer_chassis(&chassis.resource_raw) { + return None; + } + let position = transform_state(world, handle) + .ok()? + .position + .map(f32::from_bits); + let distance = (point[0] - position[0]).hypot(point[1] - position[1]); + // 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); + (id != u32::MAX).then_some(( + Capturer { + handle, + target, + id, + target_id, + }, + distance, + )) + }) + .min_by(|(_, a), (_, b)| a.total_cmp(b)) + .map(|(unit, _)| unit) +} + pub(super) fn encode_strength(strength: f32) -> f32 { // The original narrows the truncated x87 result to a DWORD before the script assignment. (strength as i64 as u32) as f32 @@ -74,6 +164,40 @@ mod tests { use super::*; #[test] + fn capture_order_replaces_patrol_and_rejects_a_friendly_target() { + use fparkan_world::{ + construct_object, new, register_object, set_owner, ObjectDraft, WorldConfig, + }; + + let mut world = new(WorldConfig); + let mut create = |owner| { + let handle = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap(); + register_object(&mut world, handle).unwrap(); + set_owner(&mut world, handle, Some(OwnerId(owner))).unwrap(); + handle + }; + let handle = create(1); + let target = create(0); + let capturer = Capturer { + handle, + target, + id: 3, + target_id: 0x8000_0002, + }; + let mut patrol = None; + assert_eq!( + send_capturer(&mut patrol, &world, &[], capturer, capturer.target_id), + Some((handle, target)) + ); + assert!(patrol.as_ref().unwrap().is_moving_to_destination(handle)); + set_owner(&mut world, target, Some(OwnerId(1))).unwrap(); + assert_eq!( + send_capturer(&mut patrol, &world, &[], capturer, capturer.target_id), + None + ); + } + + #[test] fn generators_and_factories_rank_ahead_of_other_buildings() { let classes = [ 0x8000_0002, |
