From 3732b97e0393de323f26f750083f556b16ff6b9d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 00:12:41 +0400 Subject: fix(patrol): use native cyclic PatrolPlace point batches Generate 15-19 goals with Behavior's two random streams, reject candidates within 100 units of map edges, and retain the cyclic list until the native 20-second plus 10-second jitter timer expires. Draw coordinate and timer randomness from the shared Behavior CRT stream rather than a patrol-local copy. Cache chassis CanFly for autonomous checks and reuse the player parser. Keep per-object counts of batches, goals, approximate area checks and missing inputs. The generator's initial seed, owner update order, ground walkability and physical movement remain explicit approximations. Verified native Behavior.dll RVA 0x2dd90 by x86 invocation at CW 0x007f, including edge rejection, and captured a matching batch in live AutoDemo. Rust golden vectors match all goals, RNG states and deadlines. Workspace fmt/check/test and licensed patrol, combat and clan-clock regressions pass. All 29 stock missions reach --sim-report 900 with no script interruptions or new missing patrol inputs. Observed the change in a stock AutoDemo window. This does not establish full G7 chronology or G8 movement parity. --- crates/fparkan-runtime/src/lib.rs | 36 +++- crates/fparkan-runtime/src/patrol_points.rs | 277 ++++++++++++++++++++++++++++ crates/fparkan-runtime/src/player_motion.rs | 24 +-- crates/fparkan-runtime/src/unit_chassis.rs | 49 +++++ crates/fparkan-runtime/src/unit_patrol.rs | 107 ++++++++++- 5 files changed, 465 insertions(+), 28 deletions(-) create mode 100644 crates/fparkan-runtime/src/patrol_points.rs create mode 100644 crates/fparkan-runtime/src/unit_chassis.rs (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 917adc2b..0d19d898 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -71,7 +71,9 @@ mod mining; pub use laboratory::LaboratoryReport; mod mission_events; mod ore_transport; +mod patrol_points; mod player_motion; +pub use patrol_points::PatrolPointReport; mod player_status; mod player_weapon; mod production; @@ -80,6 +82,7 @@ mod research; mod script_host; mod script_patrol; mod script_time; +mod unit_chassis; mod unit_combat; mod unit_patrol; mod unit_size_class; @@ -1347,6 +1350,7 @@ pub struct Engine { world: World, loaded: Option, behavior_rng_state: u32, + behavior_point_rng_state: u32, ai_rng_state: u32, autodemo_combat: Option, autodemo_reinforcements: Option, @@ -1363,6 +1367,7 @@ struct LoadedMissionState { mission_assets: MissionAssets, asset_plan: MissionAssetPlan, object_drafts: Vec, + unit_can_fly: std::collections::BTreeMap>, behavior_distributors: Vec, behavior_ore_states: Vec, behavior_profiles: Vec, @@ -1733,6 +1738,7 @@ pub fn create(services: EngineServices) -> Result { world: new_world(WorldConfig), loaded: None, behavior_rng_state: INITIAL_BEHAVIOR_RNG_STATE, + behavior_point_rng_state: 1, ai_rng_state: 1, autodemo_combat: None, autodemo_reinforcements: None, @@ -2683,6 +2689,8 @@ fn load_mission_with_options( engine.autodemo_reinforcements = autodemo_reinforcements; engine.production = production; let free_research = research::FreeResearch::new(&object_drafts[..reserve_start]); + let chassis_resources = CachedResourceRepository::new(vfs.clone()); + let unit_can_fly = unit_chassis::load_flags(&chassis_resources, &object_drafts); engine.loaded = Some(LoadedMissionState { summary: summary.clone(), mission, @@ -2693,6 +2701,7 @@ fn load_mission_with_options( mission_assets, asset_plan: mission_asset_plan, object_drafts, + unit_can_fly, behavior_distributors, behavior_ore_states, behavior_profiles, @@ -2754,7 +2763,11 @@ pub fn step_headless( input: InputSnapshot, queue_time_ms: u32, ) -> Result { - let resources = prepare_loaded_mission_resources(engine, queue_time_ms)?; + let mut resources = prepare_loaded_mission_resources(engine, queue_time_ms)?; + let mut crt = resources + .as_ref() + .map_or(engine.behavior_rng_state, |resources| resources.rng_state); + let mut point_rng = engine.behavior_point_rng_state; let patrol = engine .loaded .as_ref() @@ -2766,7 +2779,15 @@ pub fn step_headless( .map_or_else(Vec::new, |combat| { combat.targets(&engine.world, &mission.mission_assets, Some(patrol)) }); - Some(patrol.prepare(&engine.world, &mission.terrain, queue_time_ms, &targets)) + Some(patrol.prepare( + &engine.world, + &mission.terrain, + queue_time_ms, + &targets, + &mission.unit_can_fly, + &mut crt, + &mut point_rng, + )) }) .transpose()?; if let Some(patrol) = &patrol { @@ -2784,8 +2805,14 @@ pub fn step_headless( if let (Some(patrol), Some(mission)) = (patrol, &mut engine.loaded) { mission.unit_patrol = Some(patrol.state); } + if let Some(resources) = &mut resources { + resources.rng_state = crt; + } + engine.behavior_point_rng_state = point_rng; if let Some(resources) = resources { resources.apply(engine); + } else { + engine.behavior_rng_state = crt; } if let Some(mission) = &mut engine.loaded { control_resources::tick(mission, &engine.world, queue_time_ms); @@ -2952,6 +2979,7 @@ pub struct SimulationReport { pub control_resource_ticks: Vec<(u32, u64)>, pub control_resource_stops: Vec<(u32, u64, String)>, pub weapon_poses: WeaponPoseReport, + pub patrol_points: PatrolPointReport, /// Approximate clan strength queries, grouped by caller and target clan. pub script_strength_queries: Vec<(usize, u16, u64)>, pub script_capture_queries: Vec<(usize, u64)>, @@ -3029,6 +3057,7 @@ pub fn simulate_mission_report( control_resource_ticks: Vec::new(), control_resource_stops: Vec::new(), weapon_poses: WeaponPoseReport::default(), + patrol_points: PatrolPointReport::default(), script_strength_queries: Vec::new(), script_capture_queries: Vec::new(), script_attack_queries: Vec::new(), @@ -3136,6 +3165,9 @@ pub fn simulate_mission_report( .clan_research_catalogs .extend(loaded.clan_ai_events.research_catalogs()); report.laboratory = loaded.laboratories.report(); + if let Some(patrol) = &loaded.unit_patrol { + report.patrol_points = patrol.point_report.clone(); + } report .control_resource_ticks .extend(loaded.control_resources.ticks()); diff --git a/crates/fparkan-runtime/src/patrol_points.rs b/crates/fparkan-runtime/src/patrol_points.rs new file mode 100644 index 00000000..6c7acd21 --- /dev/null +++ b/crates/fparkan-runtime/src/patrol_points.rs @@ -0,0 +1,277 @@ +//! Behavior's cyclic PatrolPlace point list and its refresh timer. + +use crate::{behavior_clock::next_deadline, next_crt_rand, EngineError}; +use std::collections::BTreeMap; + +/// PatrolPlace batches and unresolved inputs, retained after units disappear. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct PatrolPointReport { + /// Object ID -> (batches, generated goals, approximate ground-area checks). + pub batches: BTreeMap, + /// Object ID and missing input -> affected refreshes. + pub stubs: BTreeMap<(u32, &'static str), u64>, +} + +#[derive(Clone, Default)] +pub(super) struct PatrolPoints { + points: Vec<[f32; 2]>, + index: usize, + deadline_ms: u32, +} + +impl PatrolPoints { + pub(super) fn current(&self) -> Option<[f32; 2]> { + self.points.get(self.index).copied() + } + + pub(super) fn advance(&mut self) { + if !self.points.is_empty() { + self.index = (self.index + 1) % self.points.len(); + } + } + + pub(super) fn len(&self) -> usize { + self.points.len() + } + + pub(super) fn refresh( + &mut self, + center: [f32; 2], + radius: f32, + bounds: [[f32; 3]; 2], + now_ms: u32, + crt: &mut u32, + count_rng: &mut u32, + mut allowed: impl FnMut([f32; 2]) -> Result, + ) -> Result { + if !self.points.is_empty() && now_ms <= self.deadline_ms { + return Ok(false); + } + let low = (*count_rng as u16).wrapping_mul(2) ^ (*count_rng >> 16) as u16; + let high = ((*count_rng >> 16) as u16 >> 1) ^ low; + *count_rng = u32::from(low) | (u32::from(high) << 16); + let count = usize::from(high % 5 + 15); + let mut points = Vec::with_capacity(count); + for _ in 0..count { + let mut point = center; + // Native stores the last candidate even if all 350 checks fail. + for _ in 0..350 { + point = center.map(|axis| { + let random = next_crt_rand(crt) as f32 * (1.0 / 32767.0); + (random + random - 1.0) * radius + axis + }); + if (0..2).all(|axis| { + bounds[0][axis] + 100.0 <= point[axis] && point[axis] <= bounds[1][axis] - 100.0 + }) && allowed(point)? + { + break; + } + } + points.push(point); + } + self.points = points; + self.index = 0; + // Default PatrolPlace 20 s + 10 s jitter, quantized by Behavior's timer. + self.deadline_ms = next_deadline(now_ms, 312, 156, crt); + Ok(true) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn native_place_batch_consumes_both_random_streams_and_schedules_its_timer() { + // GOG Behavior.dll RVA 0x2dd90, x87 CW 0x007f. + let mut points = PatrolPoints::default(); + let (mut crt, mut count_rng) = (1, 1); + assert!(points + .refresh( + [500.0, 752.0], + 150.0, + [[0.0; 3], [2000.0, 2000.0, 100.0]], + 1000, + &mut crt, + &mut count_rng, + |_| Ok(true) + ) + .unwrap()); + let expected = [ + [0x43af300c, 0x4440c4d7], + [0x43cbfee2, 0x445327d1], + [0x4403602e, 0x443a7d90], + [0x43e38b31, 0x4459b27a], + [0x44153687, 0x444e7ed0], + [0x43c91dc0, 0x4456ebb7], + [0x440cc9a1, 0x443d03df], + [0x43dc9967, 0x44179fb4], + [0x43bcb5e0, 0x4431d57b], + [0x43c518d0, 0x4422f141], + [0x4421a3ae, 0x4437ed4b], + [0x43c0dccc, 0x4416d9a7], + [0x43b05632, 0x4432d74d], + [0x43febfdb, 0x444156be], + [0x4404a1de, 0x44440995], + [0x43c7ef66, 0x44483a77], + [0x43f29e4b, 0x4430e8c1], + ]; + assert_eq!( + points + .points + .iter() + .map(|p| p.map(f32::to_bits)) + .collect::>(), + expected + ); + assert_eq!( + (crt, count_rng, points.deadline_ms), + (0x074d495a, 0x00020002, 23971) + ); + let first = points.current(); + for _ in 0..17 { + points.advance(); + } + assert_eq!( + points.current(), + first, + "list is cyclic until its timer expires" + ); + assert!(!points + .refresh( + [500.0, 752.0], + 150.0, + [[0.0; 3], [2000.0, 2000.0, 100.0]], + 23971, + &mut crt, + &mut count_rng, + |_| panic!("equality is not due") + ) + .unwrap()); + assert_eq!((crt, count_rng), (0x074d495a, 0x00020002)); + assert!(points + .refresh( + [500.0, 752.0], + 150.0, + [[0.0; 3], [2000.0, 2000.0, 100.0]], + 23972, + &mut crt, + &mut count_rng, + |_| Ok(true) + ) + .unwrap()); + assert_eq!( + (crt, count_rng, points.deadline_ms), + (0xd8782d77, 0x00070006, 48620) + ); + assert_eq!( + points.current().unwrap().map(f32::to_bits), + [0x4405138b, 0x44513fb8] + ); + } + + #[test] + fn live_autodemo_batch_matches_captured_rng_state_and_first_points() { + let mut points = PatrolPoints::default(); + let (mut crt, mut count_rng) = (0xad599c29, 0x66883bac); + let edge = f32::from_bits(0x4494d653); + points + .refresh( + [500.0, 752.0], + 150.0, + [[0.0; 3], [edge, edge, 100.0]], + 42767193, + &mut crt, + &mut count_rng, + |_| Ok(true), + ) + .unwrap(); + assert_eq!(points.len(), 17); + assert_eq!(points.points[0].map(f32::to_bits), [0x43cd89d1, 0x4452e9b5]); + assert_eq!(points.points[1].map(f32::to_bits), [0x44218d6a, 0x44489b26]); + assert_eq!( + (crt, count_rng, points.deadline_ms), + (0x762b0522, 0x229411d0, 42788838) + ); + } + + #[test] + fn native_bounds_reject_candidates_in_the_outer_hundred_units() { + let mut points = PatrolPoints::default(); + let (mut crt, mut count_rng) = (1, 1); + points + .refresh( + [140.0, 140.0], + 150.0, + [[0.0; 3], [2000.0, 2000.0, 100.0]], + 1000, + &mut crt, + &mut count_rng, + |_| Ok(true), + ) + .unwrap(); + assert_eq!(points.points[0].map(f32::to_bits), [0x432580b8, 0x4305f640]); + assert_eq!(points.points[1].map(f32::to_bits), [0x436cda1e, 0x4355fb40]); + assert_eq!( + (crt, count_rng, points.deadline_ms), + (0x5e14f28a, 0x00020002, 21748) + ); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_stock_patrol_keeps_its_batch_before_the_native_refresh_deadline() { + use crate::{ + create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap(); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Autodemo.00/data.tma".into(), + mode: MissionMode::AutoDemo, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + frame(&mut engine, 0).unwrap(); + let report = &engine + .loaded + .as_ref() + .unwrap() + .unit_patrol + .as_ref() + .unwrap() + .point_report; + assert!(!report.batches.is_empty()); + assert!(report.stubs.is_empty(), "{:?}", report.stubs); + for &(batches, points, _) in report.batches.values() { + assert_eq!(batches, 1); + assert!((15..=19).contains(&points)); + } + let first = report.clone(); + for now in (50..=10_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + let report = &engine + .loaded + .as_ref() + .unwrap() + .unit_patrol + .as_ref() + .unwrap() + .point_report; + assert_eq!( + report, &first, + "moving toward a goal must not draw a fresh patrol list" + ); + } +} diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index d9c48102..881b4b5f 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -1,32 +1,12 @@ use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState}; use crate::TerrainWorld; -use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository}; +use fparkan_resource::CachedResourceRepository; pub(super) fn warbot_can_fly(engine: &crate::Engine, object_index: usize) -> bool { let profile = || { let draft = engine.loaded.as_ref()?.object_drafts.get(object_index)?; - let chassis = draft.unit_components.first()?; - let archive = crate::bounded_ascii_c_string(&chassis.archive_raw)?; - let registry_path = crate::normalize_engine_path("chassis archive", &archive).ok()?; let resources = CachedResourceRepository::new(engine.services.vfs.as_ref()?.clone()); - let registry = resources.open_archive(®istry_path).ok()?; - let name = crate::bounded_ascii_c_string(&chassis.resource_raw)?; - let entry = resources - .find(registry, &resource_name(name.as_bytes())) - .ok()??; - let references = - fparkan_prototype::decode_registry_entry(resources.read(entry).ok()?.as_slice()) - .ok()?; - // AniMesh supplies the sixth resource pair to CreateBehaviour. - let profile = references.get(5)?; - let archive = crate::bounded_ascii_c_string(&profile.archive_raw)?; - let path = crate::normalize_engine_path("chassis profile archive", &archive).ok()?; - let name = crate::bounded_ascii_c_string(&profile.resource_raw)?; - let archive = resources.open_archive(&path).ok()?; - let entry = resources - .find(archive, &resource_name(name.as_bytes())) - .ok()??; - fparkan_assets::decode_chassis_can_fly(resources.read(entry).ok()?.as_slice()).ok()? + crate::unit_chassis::can_fly(&resources, draft) }; profile().unwrap_or_else(|| { eprintln!("sim_coverage player_chassis_profile_stub=1 object={object_index}"); diff --git a/crates/fparkan-runtime/src/unit_chassis.rs b/crates/fparkan-runtime/src/unit_chassis.rs new file mode 100644 index 00000000..88b5a3c9 --- /dev/null +++ b/crates/fparkan-runtime/src/unit_chassis.rs @@ -0,0 +1,49 @@ +//! Chassis behavior flags shared by autonomous and player control. +use crate::MissionObjectDraft; +use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository}; +use std::collections::BTreeMap; + +pub(super) fn load_flags( + resources: &CachedResourceRepository, + drafts: &[MissionObjectDraft], +) -> BTreeMap> { + drafts + .iter() + .filter(|draft| { + draft + .unit_dat_header_flags + .is_some_and(|class| class & crate::autodemo::MOBILE_UNIT != 0) + }) + .filter_map(|draft| { + draft + .original_id + .map(|id| (id.0, can_fly(resources, draft))) + }) + .collect() +} + +pub(super) fn can_fly( + resources: &CachedResourceRepository, + draft: &MissionObjectDraft, +) -> Option { + let chassis = draft.unit_components.first()?; + let archive = crate::bounded_ascii_c_string(&chassis.archive_raw)?; + let registry_path = crate::normalize_engine_path("chassis archive", &archive).ok()?; + let registry = resources.open_archive(®istry_path).ok()?; + let name = crate::bounded_ascii_c_string(&chassis.resource_raw)?; + let entry = resources + .find(registry, &resource_name(name.as_bytes())) + .ok()??; + let references = + fparkan_prototype::decode_registry_entry(resources.read(entry).ok()?.as_slice()).ok()?; + // AniMesh supplies the sixth resource pair to CreateBehaviour. + let profile = references.get(5)?; + let archive = crate::bounded_ascii_c_string(&profile.archive_raw)?; + let path = crate::normalize_engine_path("chassis profile archive", &archive).ok()?; + let name = crate::bounded_ascii_c_string(&profile.resource_raw)?; + let archive = resources.open_archive(&path).ok()?; + let entry = resources + .find(archive, &resource_name(name.as_bytes())) + .ok()??; + fparkan_assets::decode_chassis_can_fly(resources.read(entry).ok()?.as_slice()).ok()? +} diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 2471b334..67cd84d6 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -2,13 +2,14 @@ use super::{ autodemo::{MOBILE_UNIT, PLANT_CLASS}, boarding::WARBOT_CLASS, next_crt_rand, + patrol_points::{PatrolPointReport, PatrolPoints}, unit_combat::CombatTarget, EngineError, MissionDocument, MissionObjectDraft, MissionScriptBundle, ObjectHandle, TerrainWorld, TransformState, World, }; use fparkan_script::{Handler15TargetPayload, ScriptDispatchSelector, VarSet}; use fparkan_terrain::{NavigationQuery, RouteRequest, SurfaceQuery}; -use fparkan_world::{set_transform, transform_state}; +use fparkan_world::{identity_metadata, set_transform, transform_state}; use std::collections::{BTreeMap, HashSet, VecDeque}; mod orders; @@ -23,6 +24,7 @@ pub(super) struct UnitPatrol { script_order_counts: BTreeMap<(u32, u32), u64>, clock_ms: u32, rng: u32, + pub(super) point_report: PatrolPointReport, pub(super) factory_raid_clans: usize, } @@ -34,6 +36,7 @@ struct PatrollingUnit { destinations: Option>, radius: f32, points: VecDeque<[f32; 2]>, + place_points: PatrolPoints, retry_ms: u32, attack: Option, attack_target: Option<(ObjectHandle, usize)>, @@ -147,6 +150,7 @@ impl UnitPatrol { script_order_counts: BTreeMap::new(), clock_ms: 0, rng: 1, + point_report: PatrolPointReport::default(), factory_raid_clans: 0, } } @@ -175,6 +179,7 @@ impl UnitPatrol { unit.center = center; unit.radius = radius; unit.points.clear(); + unit.place_points = PatrolPoints::default(); unit.retry_ms = 0; return true; } @@ -187,6 +192,7 @@ impl UnitPatrol { destinations: None, radius, points: VecDeque::new(), + place_points: PatrolPoints::default(), retry_ms: 0, attack: None, attack_target: None, @@ -370,6 +376,7 @@ impl UnitPatrol { destinations: None, radius: 150.0, points: VecDeque::new(), + place_points: PatrolPoints::default(), retry_ms: 0, attack: None, attack_target: None, @@ -384,6 +391,7 @@ impl UnitPatrol { script_order_counts: BTreeMap::new(), clock_ms: 0, rng: 1, + point_report: PatrolPointReport::default(), factory_raid_clans: 0, }) } @@ -394,6 +402,9 @@ impl UnitPatrol { terrain: &TerrainWorld, now_ms: u32, targets: &[(ObjectHandle, CombatTarget)], + chassis: &BTreeMap>, + crt: &mut u32, + count_rng: &mut u32, ) -> Result { let mut state = self.clone(); state.advance_script_orders(world); @@ -406,9 +417,6 @@ impl UnitPatrol { let seconds = now_ms.saturating_sub(state.clock_ms) as f32 * 0.001; state.clock_ms = state.clock_ms.max(now_ms); let mut poses = Vec::new(); - if seconds == 0.0 { - return Ok(PreparedPatrol { state, poses }); - } for unit in &mut state.units { let Ok(before) = transform_state(world, unit.handle) else { continue; @@ -442,6 +450,86 @@ impl UnitPatrol { unit.retry_ms = 0; unit.attack = enemy.map(|target| target.handle); } + let place_patrol = unit.center_object.is_none() + && unit.destinations.is_none() + && unit.attack_target.is_none() + && unit.raid_factory.is_none(); + if place_patrol && (enemy.is_none() || unit.place_points.current().is_none()) { + let original_id = identity_metadata(world, unit.handle) + .ok() + .and_then(|identity| identity.original_id) + .map_or(unit.handle.slot, |id| id.0); + let can_fly = chassis.get(&original_id).copied().flatten(); + if let Some(bounds) = terrain.surface_bounds() { + let mut checks = 0; + let refreshed = unit.place_points.refresh( + center, + unit.radius, + bounds, + now_ms, + crt, + count_rng, + |[x, y]| { + if can_fly == Some(true) { + return Ok(true); + } + checks += 1; + // shortcut: area presence substitutes for native area walkability + // until the native ArealMap flags are projected. + Ok(terrain + .locate_areal([x, y, 0.0]) + .map_err(EngineError::Terrain)? + .is_some()) + }, + )?; + if refreshed { + let totals = state.point_report.batches.entry(original_id).or_default(); + if totals.0 == 0 { + eprintln!("mission_patrol_points_approx object={original_id} (seed, global owner order and ground walkability remain open)"); + } + totals.0 += 1; + totals.1 += unit.place_points.len() as u64; + totals.2 += checks; + if can_fly.is_none() { + let count = state + .point_report + .stubs + .entry((original_id, "chassis_can_fly")) + .or_default(); + if *count == 0 { + eprintln!("mission_patrol_points_stub object={original_id} reason=chassis_can_fly"); + } + *count += 1; + } + unit.points.clear(); + unit.retry_ms = 0; + } + if unit + .place_points + .current() + .is_some_and(|[x, y]| (x - from[0]).hypot(y - from[1]) <= 0.05) + { + unit.place_points.advance(); + unit.points.clear(); + unit.retry_ms = 0; + } + } else { + let count = state + .point_report + .stubs + .entry((original_id, "terrain_bounds")) + .or_default(); + if *count == 0 { + eprintln!( + "mission_patrol_points_stub object={original_id} reason=terrain_bounds" + ); + } + *count += 1; + } + } + if seconds == 0.0 { + continue; + } if let Some(enemy) = enemy.filter(|enemy| enemy.in_range) { let mut after = before; let (yaw, _) = turn_toward( @@ -471,6 +559,17 @@ impl UnitPatrol { .unwrap_or_default() } else if unit.attack_target.is_some() { VecDeque::new() + } else if let Some([x, y]) = unit.place_points.current().filter(|_| place_patrol) { + let route = terrain + .height_at([x, y]) + .map_err(EngineError::Terrain)? + .map(|z| route_points(terrain, from, [x, y, z])) + .transpose()? + .flatten(); + if route.is_none() { + unit.place_points.advance(); + } + route.unwrap_or_default() } else { choose_route(terrain, from, center, unit.radius, &mut state.rng)? }; -- cgit v1.2.3