diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 23:04:06 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 23:04:06 +0300 |
| commit | 8d033a0777a7f86101c86ff3a1940dca6e5d5419 (patch) | |
| tree | 0157ab7f5e2e2d40f5296f2a377c46e19800fc65 /crates/fparkan-runtime/src | |
| parent | f626d7759e371ba29efbb733c394751fea547d04 (diff) | |
| download | fparkan-8d033a0777a7f86101c86ff3a1940dca6e5d5419.tar.xz fparkan-8d033a0777a7f86101c86ff3a1940dca6e5d5419.zip | |
fix(combat): hold default warbot targets between scans
Use the Behavior mode-2 score on root XY with the strict 500-unit boundary and preserve the first equal score. Sample the two original timer intervals with their verified CRT step. Keep the prepared hostile-unit roster and disclose the open perception and RNG ordering. In the stock Two Fortresses window the stationary defender turns its turret and fires at the approaching helicopter.
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/default_weapon_target.rs | 80 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 81 |
3 files changed, 143 insertions, 19 deletions
diff --git a/crates/fparkan-runtime/src/default_weapon_target.rs b/crates/fparkan-runtime/src/default_weapon_target.rs new file mode 100644 index 00000000..6d523387 --- /dev/null +++ b/crates/fparkan-runtime/src/default_weapon_target.rs @@ -0,0 +1,80 @@ +use super::{behavior_clock::next_deadline, ObjectHandle}; + +#[derive(Clone, Default)] +pub(super) struct DefaultWeaponTarget { + pub(super) selected: Option<ObjectHandle>, + update_ms: u32, + search_ms: u32, +} + +impl DefaultWeaponTarget { + pub(super) fn searches_at(&self, now: u32) -> bool { + due(self.update_ms, now) && due(self.search_ms, now) + } + + pub(super) fn update(&mut self, now: u32, candidate: Option<ObjectHandle>, rng: &mut u32) { + if !due(self.update_ms, now) { + return; + } + self.update_ms = next_deadline(now, 1, 1, rng); + if due(self.search_ms, now) { + self.search_ms = next_deadline(now, 31, 15, rng); + self.selected = candidate; + } + } +} + +fn due(deadline: u32, now: u32) -> bool { + deadline == 0 || deadline < now +} + +// Behavior mode 2 rounds the score before comparing it, retaining the first tie. +pub(super) fn score(from: [f32; 3], candidate: [f32; 3]) -> Option<f32> { + let x = from[0] - candidate[0]; + let y = from[1] - candidate[1]; + let distance = (x * x + y * y).sqrt(); + (distance < 500.0).then(|| 1.0 / (10.0 + distance)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn score_uses_root_xy_and_a_strict_search_radius() { + let origin = [5.0, -10.0, 1.0]; + assert_eq!( + score(origin, [35.0, 30.0, 1000.0]), + score(origin, [55.0, -10.0, -1000.0]) + ); + assert_eq!(score(origin, [305.0, 390.0, 0.0]), None); + assert!(score(origin, [504.9, -10.0, 0.0]).is_some()); + assert_eq!( + score([0.0; 3], [16.0, 0.0, 0.0]), + score([0.0; 3], [15.999999, 0.0, 0.0]) + ); + } + + #[test] + fn keeps_selected_target_until_search_is_due() { + let mut target = DefaultWeaponTarget::default(); + let one = ObjectHandle { + slot: 1, + generation: 0, + }; + let two = ObjectHandle { + slot: 2, + generation: 0, + }; + let mut rng = 1; + target.update(100, Some(one), &mut rng); + assert_eq!((target.update_ms, target.search_ms), (174, 2215)); + assert!(!target.searches_at(174)); + target.update(175, Some(two), &mut rng); + assert_eq!(target.selected, Some(one)); + assert!(!target.searches_at(2215)); + assert!(target.searches_at(2216)); + target.update(2216, Some(two), &mut rng); + assert_eq!(target.selected, Some(two)); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 3aa78bbe..683c70ca 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -84,6 +84,7 @@ mod clan_targeting; mod construction; mod construction_site; mod debris_motion; +mod default_weapon_target; pub use debris_motion::DebrisMotion; mod destruction; pub use destruction::{ diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index 10c13f01..2d6a3715 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -2,6 +2,7 @@ use super::{ apply_projectile_impact, autodemo::MOBILE_UNIT, combat_report::{CombatObject, CombatReport}, + default_weapon_target::{self, DefaultWeaponTarget}, player_weapon::prepare_unit_weapons, projectile::{load_projectile, segment_sphere_fraction, unit_sphere, ANIMAL_UNIT_CLASS}, projectile_terrain_hit, projectile_unit_hit, @@ -112,11 +113,20 @@ pub(super) struct CombatTarget { struct Unit { handle: ObjectHandle, index: usize, + class: u32, original_id: u32, weapons: PlayerWeapons, slots: Vec<Option<usize>>, effects: Vec<Option<usize>>, poses: WeaponPoses, + idle_target: DefaultWeaponTarget, +} + +impl Unit { + fn uses_default_targeting(&self, patrol: Option<&UnitPatrol>) -> bool { + self.class == super::boarding::WARBOT_CLASS + && patrol.is_none_or(|patrol| !patrol.is_patrolling(self.handle)) + } } struct Flight { @@ -254,10 +264,12 @@ impl AutoDemoCombat { units.push(Unit { handle, index, + class, weapons, slots, effects, poses: WeaponPoses::new(draft, assets, index), + idle_target: DefaultWeaponTarget::default(), original_id: draft.original_id.map_or(handle.slot, |id| id.0), }); } @@ -328,7 +340,7 @@ impl AutoDemoCombat { patrol: Option<&UnitPatrol>, controls: &[super::MissionControlDeviceTableState], ) -> Vec<(ObjectHandle, CombatTarget)> { - self.select_targets(world, assets, patrol, controls, false) + self.select_targets(world, assets, patrol, controls, None) } fn select_targets( @@ -337,8 +349,9 @@ impl AutoDemoCombat { assets: &MissionAssets, patrol: Option<&UnitPatrol>, controls: &[super::MissionControlDeviceTableState], - use_weapon_range: bool, + fire_clock_ms: Option<u32>, ) -> Vec<(ObjectHandle, CombatTarget)> { + let use_weapon_range = fire_clock_ms.is_some(); let live: Vec<_> = self .units .iter() @@ -395,22 +408,34 @@ impl AutoDemoCombat { if limit.is_some_and(|limit| !limit.allows_attack(position)) { return None; } - let search_distance = if use_weapon_range { + let idle_clock = fire_clock_ms.filter(|_| unit.uses_default_targeting(patrol)); + let search_distance = if use_weapon_range && idle_clock.is_none() { self.maximum_firing_distance(unit) } else { ATTACK_RANGE }; - let (enemy, center, radius, distance) = live + let (enemy, center, radius, _) = live .iter() .filter_map(|&(enemy, other, to, radius, target_position)| { let enemy = enemy.handle; - let distance = (to[0] - from[0]).hypot(to[1] - from[1]); - (enemy != handle - && distance <= search_distance - && limit - .is_none_or(|limit| limit.allows_target(enemy, target_position)) - && self.hostile.get(clan)?.get(other) == Some(&true)) - .then_some((enemy, to, radius, distance)) + if enemy == handle + || limit + .is_some_and(|limit| !limit.allows_target(enemy, target_position)) + || self.hostile.get(clan)?.get(other) != Some(&true) + { + return None; + } + let rank = if let Some(now) = idle_clock { + if unit.idle_target.searches_at(now) { + -default_weapon_target::score(position, target_position)? + } else { + (unit.idle_target.selected == Some(enemy)).then_some(0.0)? + } + } else { + let distance = (to[0] - from[0]).hypot(to[1] - from[1]); + (distance <= search_distance).then_some(distance)? + }; + Some((enemy, to, radius, rank)) }) .min_by(|a, b| a.3.total_cmp(&b.3))?; Some(( @@ -419,7 +444,8 @@ impl AutoDemoCombat { handle: enemy, center, radius, - in_range: distance <= ATTACK_STOP_RANGE, + in_range: (center[0] - from[0]).hypot(center[1] - from[1]) + <= ATTACK_STOP_RANGE, }, )) }) @@ -533,7 +559,7 @@ impl AutoDemoCombat { &mission.mission_assets, mission.unit_patrol.as_ref(), &mission.control_device_tables, - true, + Some(now), ); for unit in &mut self.units { unit.weapons.read_control_energy(engine, unit.handle); @@ -556,8 +582,8 @@ impl AutoDemoCombat { // shortcut: free warbots defend from their current position until the // default Behavior task and its pursuit rules are wired. let defends_from_position = matches!( - mission.object_drafts[unit.index].unit_dat_header_flags, - Some(super::boarding::WARBOT_CLASS | ANIMAL_UNIT_CLASS) + unit.class, + super::boarding::WARBOT_CLASS | ANIMAL_UNIT_CLASS ); let eligible = !manually_controlled && (mission.script_host_mode != super::MissionMode::SingleMission @@ -567,12 +593,29 @@ impl AutoDemoCombat { .unit_patrol .as_ref() .is_some_and(|patrol| patrol.is_patrolling(unit.handle))); + let selected = targets + .iter() + .find(|(handle, _)| *handle == unit.handle) + .map(|(_, target)| *target); + let patrol = mission.unit_patrol.as_ref(); + if eligible + && unit.uses_default_targeting(patrol) + && !patrol.is_some_and(|patrol| { + patrol.is_shut_down(unit.handle) || patrol.is_leaving_factory(unit.handle) + }) + { + unit.idle_target.update( + now, + selected.map(|target| target.handle), + &mut engine.behavior_rng_state, + ); + } else { + unit.idle_target = DefaultWeaponTarget::default(); + } let aim = eligible .then(|| { - targets - .iter() - .find(|(handle, _)| *handle == unit.handle) - .map(|(_, target)| (target.handle, target.center, target.radius)) + selected + .map(|target| (target.handle, target.center, target.radius)) .or_else(|| { let (factory, index) = mission.unit_patrol.as_ref()?.raid_factory(unit.handle)?; |
