use super::{ animal_migration::{flight_clearance, AnimalMigration}, autodemo::{MOBILE_UNIT, PLANT_CLASS}, boarding::WARBOT_CLASS, building_passage::BuildingPassage, factory_release::FactoryDeparture, flight_altitude::FlightClearance, ground_support::GroundSupport, next_crt_rand, patrol_points::{PatrolPointReport, PatrolPoints}, route_motion::{follow_route, route_points, turn_toward, RouteClearance}, route_obstacles::BuildingFootprints, route_traffic::{body_radius, FrameTraffic}, unit_combat::CombatTarget, unit_speed::{order_speed, UnitSpeedKind, UnitSpeedReport}, EngineError, LoadedMissionState, MissionDocument, MissionObjectDraft, MissionScriptBundle, ObjectHandle, TerrainWorld, TransformState, World, }; use fparkan_script::{Handler15TargetPayload, ScriptDispatchSelector, VarSet}; use fparkan_terrain::{NavigationQuery, SurfaceQuery}; use fparkan_world::{set_transform, transform_state}; use std::collections::{BTreeMap, HashSet, VecDeque}; mod orders; mod recharge; mod search_and_destroy; pub(super) use orders::supports_order; pub(super) use orders::UnitOrder; #[derive(Clone)] pub(super) struct UnitPatrol { center: Option<[f32; 2]>, units: Vec, factory_departures: Vec, animal_migration: Vec, migration_rng: u32, shut_down: HashSet, standing: HashSet, script_orders: Vec<(ObjectHandle, VecDeque)>, script_order_counts: BTreeMap<(u32, u32), u64>, clock_ms: u32, rng: u32, pub(super) point_report: PatrolPointReport, pub(super) speed_report: UnitSpeedReport, pub(super) motion_report: crate::UnitMotionReport, pub(super) factory_raid_clans: usize, buildings: BuildingFootprints, } #[derive(Clone)] pub(super) struct PatrollingUnit { handle: ObjectHandle, center: [f32; 2], center_object: Option, destinations: Option>, radius: f32, points: VecDeque<[f32; 2]>, place_points: PatrolPoints, retry_ms: u32, attack: Option, search: Option, recharge: Option, attack_target: Option<(ObjectHandle, usize)>, raid_factory: Option<(ObjectHandle, usize)>, ground_support: Option>, capture_target: Option, building_passage: Option, animal_clearance: Option, } pub(super) struct PreparedPatrol { pub(super) state: UnitPatrol, poses: Vec<(ObjectHandle, TransformState, TransformState)>, seconds: f32, } #[derive(Clone, Copy)] pub(super) struct PatrolAttackLimit { center: [f32; 2], radius: f32, current_target: Option, } impl PatrolAttackLimit { pub(super) fn allows_attack(self, position: [f32; 3]) -> bool { distance_to_patrol(self.center, position) <= self.radius + 60.0 } pub(super) fn allows_target(self, target: ObjectHandle, position: [f32; 3]) -> bool { self.current_target == Some(target) || distance_to_patrol(self.center, position) < self.radius } } impl UnitPatrol { pub(super) fn approximate_factory_raid( &mut self, world: &World, mission: &MissionDocument, drafts: &[MissionObjectDraft], handles: &[ObjectHandle], bundle: &MissionScriptBundle, declarations: &VarSet, ) { let attacks_factory = bundle .package .events .iter() .filter(|event| event.name_raw == b"PBM_BUILDING_ATTACK_Start\0") .flat_map(|event| &event.instructions) .filter(|instruction| { instruction.dispatch_selector() == ScriptDispatchSelector::Handler(28) }) .any(|instruction| { declarations .resolve_handler15(instruction) .is_ok_and(|order| { order.word_1 == 3 && order.word_2 == 3 && order.word_3 == PLANT_CLASS && matches!(order.target, Handler15TargetPayload::ByLogicId(_)) }) }); if !attacks_factory { return; } let clan = &mission.clans[bundle.clan_index]; let factories = drafts.iter().enumerate().filter(|(_, draft)| { draft.unit_dat_header_flags == Some(PLANT_CLASS) && mission .clans .get(draft.raw_after_resource as usize) .is_some_and(|owner| { clan.relations.iter().any(|relation| { relation.relation_value == 0 && relation.other_clan_name.raw == owner.name.raw }) }) }); // shortcut: route placed battle robots toward the nearest hostile factory until // the problem scheduler can select and dispatch a native attack group. let mut ordered = false; for (draft, &handle) in drafts.iter().zip(handles).filter(|(draft, _)| { draft.raw_after_resource as usize == bundle.clan_index && draft.unit_dat_header_flags == Some(WARBOT_CLASS) }) { let distance = |target: &MissionObjectDraft| { (draft.position[0] - target.position[0]) .hypot(draft.position[1] - target.position[1]) }; if let Some((index, factory)) = factories .clone() .min_by(|(_, a), (_, b)| distance(a).total_cmp(&distance(b))) { ordered |= self.order( world, handle, [factory.position[0], factory.position[1]], 60.0, ); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.raid_factory = Some((handles[index], index)); } } } self.factory_raid_clans += usize::from(ordered); } pub(super) fn scripted() -> Self { Self { center: None, units: Vec::new(), factory_departures: Vec::new(), animal_migration: Vec::new(), migration_rng: 1, shut_down: HashSet::new(), standing: HashSet::new(), script_orders: Vec::new(), script_order_counts: BTreeMap::new(), clock_ms: 0, rng: 1, point_report: PatrolPointReport::default(), speed_report: UnitSpeedReport::default(), motion_report: crate::UnitMotionReport::default(), factory_raid_clans: 0, buildings: BuildingFootprints::default(), } } pub(super) fn order( &mut self, world: &World, handle: ObjectHandle, center: [f32; 2], radius: f32, ) -> bool { self.script_orders.retain(|(unit, _)| *unit != handle); self.shut_down.remove(&handle); self.standing.remove(&handle); self.units .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.building_passage = None; let was_attacking = unit.attack_target.take().is_some(); let was_moving = unit.destinations.take().is_some(); let was_searching = unit.search.take().is_some(); let was_recharging = unit.recharge.take().is_some(); if unit.center_object.take().is_some() || was_moving || was_attacking || was_searching || was_recharging || unit.center != center || unit.radius != radius { unit.center = center; unit.radius = radius; unit.points.clear(); unit.place_points = PatrolPoints::default(); unit.retry_ms = 0; return true; } return false; } self.units.push(PatrollingUnit { handle, center, center_object: None, destinations: None, radius, points: VecDeque::new(), place_points: PatrolPoints::default(), retry_ms: 0, attack: None, search: None, recharge: None, attack_target: None, raid_factory: None, ground_support: None, capture_target: None, building_passage: None, animal_clearance: None, }); true } pub(super) fn move_to( &mut self, world: &World, handle: ObjectHandle, destination: [f32; 2], append: bool, ) { if append { if let Some(destinations) = self .units .iter_mut() .find(|unit| unit.handle == handle) .and_then(|unit| unit.destinations.as_mut()) { destinations.push_back(destination); return; } } self.order(world, handle, destination, 60.0); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.destinations = Some(VecDeque::from([destination])); unit.points.clear(); unit.retry_ms = 0; } } pub(super) fn move_to_capture( &mut self, world: &World, handle: ObjectHandle, building: ObjectHandle, destination: [f32; 2], passage: Option, ) { 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.building_passage = passage; } } pub(super) fn set_animal_migration(&mut self, migration: Vec) { for clan in &migration { eprintln!( "animal_migration_approx clan={} (animal route, arrival and RNG order remain open)", clan.owner.0 ); } self.animal_migration = migration; } fn advance_animal_migration(&mut self, world: &World, now_ms: u32, crt: &mut u32) { let mut migrations = std::mem::take(&mut self.animal_migration); for migration in &mut migrations { migration .animals .retain(|&handle| transform_state(world, handle).is_ok()); if migration.animals.is_empty() { continue; } let Some(site) = migration.select(now_ms, &mut self.migration_rng) else { continue; }; for &handle in &migration.animals { self.order(world, handle, site.center, site.radius); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.animal_clearance = Some(flight_clearance(crt)); } } } migrations.retain(|migration| !migration.animals.is_empty()); self.animal_migration = migrations; } pub(super) fn hallway_doors( &self, building: ObjectHandle, ) -> impl Iterator + '_ { self.units .iter() .filter_map(|unit| unit.building_passage.as_ref()) .filter(move |route| route.building == building) .filter_map(|route| route.opening) } 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( &mut self, world: &World, handle: ObjectHandle, target: ObjectHandle, center: [f32; 2], radius: f32, ) -> bool { if self.units.iter().any(|unit| { unit.handle == handle && unit.attack_target.is_none() && unit.center_object == Some(target) && unit.radius == radius }) { return false; } self.order(world, handle, center, radius); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.center_object = Some(target); unit.points.clear(); unit.retry_ms = 0; } true } pub(super) fn is_patrolling(&self, handle: ObjectHandle) -> bool { self.units.iter().any(|unit| unit.handle == handle) } pub(super) fn has_point_order(&self, handle: ObjectHandle, point: [f32; 2]) -> bool { self.units .iter() .any(|unit| unit.handle == handle && unit.patrols_point(point)) } pub(super) fn clear_point_order(&mut self, handle: ObjectHandle, point: [f32; 2]) { self.units .retain(|unit| unit.handle != handle || !unit.patrols_point(point)); } pub(super) fn shut_down(&mut self, handle: ObjectHandle) -> bool { self.script_orders.retain(|(unit, _)| *unit != handle); self.standing.remove(&handle); // shortcut: stop autonomous movement and fire until the native shutdown task is modeled. self.units.retain(|unit| unit.handle != handle); self.shut_down.insert(handle) } pub(super) fn is_shut_down(&self, handle: ObjectHandle) -> bool { self.shut_down.contains(&handle) } pub(super) fn begin_factory_departure(&mut self, departure: FactoryDeparture, now_ms: u32) { if self.units.is_empty() && self.factory_departures.is_empty() { self.clock_ms = now_ms; } self.factory_departures.push(departure); } pub(super) fn is_leaving_factory(&self, unit: ObjectHandle) -> bool { self.factory_departures .iter() .any(|departure| departure.handle == unit) } pub(super) fn has_factory_departure(&self, plant: ObjectHandle) -> bool { self.factory_departures .iter() .any(|departure| departure.plant == plant) } pub(super) fn is_moving_to_destination(&self, handle: ObjectHandle) -> bool { self.units .iter() .any(|unit| unit.handle == handle && unit.destinations.is_some()) } pub(super) fn clear_move(&mut self, handle: ObjectHandle) { self.units .retain(|unit| unit.handle != handle || unit.destinations.is_none()); } pub(super) fn raid_factory(&self, handle: ObjectHandle) -> Option<(ObjectHandle, usize)> { self.units .iter() .find(|unit| unit.handle == handle)? .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, handle: ObjectHandle, ) -> Option { // A raid starts with an explicit attack order. It never receives PatrolPlace limits. let unit = self.units.iter().find(|unit| { unit.handle == handle && unit.raid_factory.is_none() && unit.search.is_none() })?; Some(PatrolAttackLimit { center: unit.center(world)?, radius: unit.radius, current_target: unit.attack, }) } pub(super) fn activate(&mut self, world: &World, handle: ObjectHandle) { if let Some(center) = self.center { self.order(world, handle, center, 150.0); } } pub(super) fn new( mission: &MissionDocument, drafts: &[MissionObjectDraft], handles: &[ObjectHandle], ) -> Option { // shortcut: use the observed AutoDemo meeting area from the first clan anchor // until we can execute the native producer of the patrol order. let center = mission.clans.first()?.anchor.map(f32::round); let units = drafts .iter() .zip(handles) .filter(|(draft, _)| { draft .unit_dat_header_flags .is_some_and(|class| class & MOBILE_UNIT != 0) && (draft.raw_after_resource as usize) < mission.clans.len() }) .map(|(_, &handle)| PatrollingUnit { handle, center, center_object: None, destinations: None, radius: 150.0, points: VecDeque::new(), place_points: PatrolPoints::default(), retry_ms: 0, attack: None, search: None, recharge: None, attack_target: None, raid_factory: None, ground_support: None, capture_target: None, building_passage: None, animal_clearance: None, }) .collect(); Some(Self { center: Some(center), units, factory_departures: Vec::new(), animal_migration: Vec::new(), migration_rng: 1, shut_down: HashSet::new(), standing: HashSet::new(), script_orders: Vec::new(), script_order_counts: BTreeMap::new(), clock_ms: 0, rng: 1, point_report: PatrolPointReport::default(), speed_report: UnitSpeedReport::default(), motion_report: crate::UnitMotionReport::default(), factory_raid_clans: 0, buildings: BuildingFootprints::default(), }) } pub(super) fn prepare( &self, world: &World, mission: &LoadedMissionState, now_ms: u32, targets: &[(ObjectHandle, CombatTarget)], crt: &mut u32, count_rng: &mut u32, ) -> Result { let terrain = &mission.terrain; let chassis = &mission.unit_chassis_flags; let mut state = self.clone(); state.advance_script_orders(world, &mission.control_device_tables); state.advance_animal_migration(world, now_ms, crt); state.units.retain(|unit| { (unit .destinations .as_ref() .is_none_or(|items| !items.is_empty()) || unit .building_passage .as_ref() .is_some_and(|route| !route.legs.is_empty()) || unit.recharge.is_some()) && unit.center(world).is_some() }); let seconds = now_ms.saturating_sub(state.clock_ms) as f32 * 0.001; state.clock_ms = state.clock_ms.max(now_ms); let mut traffic = if seconds > 0.0 && (!state.units.is_empty() || !state.factory_departures.is_empty()) { state.buildings.refresh(world, mission); FrameTraffic::read(world, mission) } else { FrameTraffic::default() }; let mut poses = Vec::new(); if seconds > 0.0 { for mut departure in std::mem::take(&mut state.factory_departures) { let handle = departure.handle; let Ok(before) = transform_state(world, handle) else { continue; }; let after = departure.move_out(before, mission, &mut traffic, &state.buildings, seconds)?; if after != before { poses.push((handle, before, after)); } if !departure.finished(after) { state.factory_departures.push(departure); } } } for unit in &mut state.units { if self.is_leaving_factory(unit.handle) || mission.boarding.is_some_and(|control| { control.controls_chassis() && control.cockpit.handle == unit.handle }) { continue; } let Ok(before) = transform_state(world, unit.handle) else { continue; }; let from = before.position.map(f32::from_bits); let motion_object = crate::CombatObject::read(world, unit.handle); let original_id = motion_object.original_id.unwrap_or(unit.handle.slot); let can_fly = unit .animal_clearance .map(|_| true) .or_else(|| chassis.get(&original_id).and_then(|flags| flags.can_fly)); if unit.recharge.is_some() && (seconds == 0.0 || !unit.advance_recharge( world, mission, now_ms, from, traffic.radius(unit.handle), )) { continue; } if let Some(destinations) = &mut unit.destinations { if destinations .front() .is_some_and(|[x, y]| (x - from[0]).hypot(y - from[1]) <= 0.05) { destinations.pop_front(); unit.points.clear(); unit.retry_ms = 0; } if destinations.is_empty() { if let Some(route) = &mut unit.building_passage { if transform_state(world, route.building).is_err() { route.legs.clear(); continue; } let radius = traffic.radius(unit.handle); let Some(neighbors) = traffic.blockers(unit.handle, [from[0], from[1]], radius) else { continue; }; let speed = movement_speed( mission, unit.handle, original_id, UnitSpeedKind::Go, &mut state.speed_report, ); let (after, swept) = route.advance(before, speed, seconds, &neighbors); traffic.reserve(unit.handle, swept); if after != before { poses.push((unit.handle, before, after)); } } continue; } } let center = unit.center(world).expect("retained patrol center"); if (center[0] - unit.center[0]).hypot(center[1] - unit.center[1]) >= unit.radius { unit.center = center; unit.points.clear(); unit.retry_ms = 0; } let enemy = targets .iter() .find(|(handle, _)| *handle == unit.handle) .map(|(_, target)| *target) .filter(|target| { unit.search.is_none() || !super::attack_target::complete( world, &mission.control_device_tables, target.handle, ) }); if unit.attack != enemy.map(|target| target.handle) { unit.points.clear(); unit.retry_ms = 0; unit.attack = enemy.map(|target| target.handle); } if let Some(search) = &mut unit.search { if search.forget_depleted(world, &mission.control_device_tables) { unit.points.clear(); unit.retry_ms = 0; } } let place_patrol = unit.center_object.is_none() && unit.destinations.is_none() && unit.attack_target.is_none() && unit.raid_factory.is_none() && unit.search.is_none(); if place_patrol && (enemy.is_none() || unit.place_points.current().is_none()) { 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; } state.motion_report.update(motion_object, now_ms); let radius = if unit.animal_clearance.is_some() { body_radius(&mission.mission_assets, original_id as usize, before).unwrap_or(1.0) } else { traffic.radius(unit.handle) }; let flight = (can_fly == Some(true) && radius > 0.0).then_some(FlightClearance { body_radius: radius, height: unit.animal_clearance.unwrap_or(2.0 * radius), vertical_limit: unit.animal_clearance.map_or(f32::INFINITY, |_| { super::animal_migration::vertical_limit(mission, unit.handle) }), }); let ground = if can_fly == Some(false) { *unit.ground_support.get_or_insert_with(|| { GroundSupport::prepare(&mission.mission_assets, original_id as usize) }) } else { None }; let height_at = |xy| terrain.height_at(xy).map_err(EngineError::Terrain); if let Some(enemy) = enemy.filter(|enemy| enemy.in_range) { let mut after = before; let (yaw, _) = turn_toward( f32::from_bits(before.orientation[2]), [enemy.center[0] - from[0], enemy.center[1] - from[1]], seconds, ); after.orientation[2] = yaw.to_bits(); if let Some(flight) = flight { if let Some(floor) = terrain .height_at([from[0], from[1]]) .map_err(EngineError::Terrain)? { after.position[2] = flight.raised(from[2], floor, 9.0, seconds).0.to_bits(); } } if let Some(ground) = ground { ground.reanchor(before, &mut after, height_at)?; } if after != before { poses.push((unit.handle, before, after)); } continue; } if (unit.points.is_empty() || enemy.is_some()) && now_ms >= unit.retry_ms { unit.points = if let Some(enemy) = enemy { route_points(terrain, from, enemy.center)?.unwrap_or_default() } else if let Some(&[x, y]) = unit.destinations.as_ref().and_then(|items| items.front()) { // shortcut: route to the same XY using the terrain height until native Go arrival is wired. terrain .height_at([x, y]) .map_err(EngineError::Terrain)? .map(|z| route_points(terrain, from, [x, y, z])) .transpose()? .flatten() .unwrap_or_default() } else if unit.attack_target.is_some() { VecDeque::new() } else if let Some(search) = &mut unit.search { search.route(world, mission, unit.handle, from, crt)? } 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)? }; unit.retry_ms = now_ms.saturating_add(1_000); } if unit.points.is_empty() && flight.is_none() { if let Some(ground) = ground { let mut after = before; ground.reanchor(before, &mut after, height_at)?; if after != before { poses.push((unit.handle, before, after)); } } continue; } let speed = if (place_patrol && enemy.is_none()) || unit.destinations.is_some() { let kind = if place_patrol { UnitSpeedKind::PatrolPlace } else { UnitSpeedKind::Go }; movement_speed( mission, unit.handle, original_id, kind, &mut state.speed_report, ) } else { 9.0 }; let Some(neighbors) = traffic.blockers(unit.handle, [from[0], from[1]], radius) else { continue; }; let buildings = state.buildings.with_clearance(radius); let (after, swept) = follow_route( before, &mut unit.points, speed, seconds, RouteClearance { buildings: &buildings, traffic: &neighbors, flight, ground, }, height_at, )?; traffic.reserve(unit.handle, swept); if after != before { poses.push((unit.handle, before, after)); } } if let Some(boarding) = mission.boarding.filter(|board| !board.controls_chassis()) { if let Some(passenger) = poses.iter().find_map(|&(handle, _, pose)| { crate::boarding::passenger_pose(world, boarding, handle, pose) }) { poses.push(passenger); } } Ok(PreparedPatrol { state, poses, seconds, }) } } fn movement_speed( mission: &LoadedMissionState, handle: ObjectHandle, original_id: u32, kind: UnitSpeedKind, report: &mut UnitSpeedReport, ) -> f32 { let table = mission .control_device_tables .iter() .find(|table| table.handle == handle); let behavior = mission .behavior_profiles .iter() .find(|profile| profile.handle == handle); order_speed(table, behavior, original_id, kind, report) } #[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 has_object_order(&self, target: ObjectHandle) -> bool { self.center_object == Some(target) || self .attack_target .is_some_and(|(handle, _)| handle == target) } fn patrols_point(&self, point: [f32; 2]) -> bool { self.center_object.is_none() && self.attack_target.is_none() && self.destinations.is_none() && self.search.is_none() && self.center == point } fn center(&self, world: &World) -> Option<[f32; 2]> { // shortcut: use the target's transform as the patrol center until native building approaches are wired. match self.center_object { None => Some(self.center), Some(handle) => { let [x, y, _] = transform_state(world, handle) .ok()? .position .map(f32::from_bits); Some([x, y]) } } } } #[cfg(test)] #[test] fn attack_order_remains_assigned_and_cleanup_preserves_a_new_target() { let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); let [actor, first, second] = [0; 3].map(|_| { let unit = fparkan_world::construct_object( &mut world, fparkan_world::ObjectDraft { original_id: None }, ) .unwrap(); fparkan_world::register_object(&mut world, unit).unwrap(); unit }); let mut patrol = UnitPatrol::scripted(); patrol.set_attack_target(&world, actor, first, 1, [50.0, 60.0]); assert!(patrol.has_object_order(actor, first)); patrol.clear_object_order(actor, second); assert_eq!(patrol.attack_target(actor), Some((first, 1))); patrol.set_attack_target(&world, actor, second, 2, [70.0, 80.0]); patrol.clear_object_order(actor, first); assert_eq!(patrol.attack_target(actor), Some((second, 2))); patrol.clear_object_order(actor, second); assert!(!patrol.is_patrolling(actor)); } #[cfg(test)] #[test] fn point_patrol_cleanup_preserves_reassigned_orders() { let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); let actor = fparkan_world::construct_object( &mut world, fparkan_world::ObjectDraft { original_id: None }, ) .unwrap(); fparkan_world::register_object(&mut world, actor).unwrap(); let mut patrol = UnitPatrol::scripted(); let point = [1124.0, 783.0]; patrol.order(&world, actor, point, 60.0); assert!(patrol.has_point_order(actor, point)); patrol.clear_point_order(actor, [1125.0, 783.0]); assert!(patrol.has_point_order(actor, point)); patrol.move_to(&world, actor, point, false); assert!(!patrol.has_point_order(actor, point)); patrol.clear_point_order(actor, point); assert!(patrol.is_patrolling(actor)); patrol.order(&world, actor, point, 90.0); patrol.clear_point_order(actor, point); assert!(!patrol.is_patrolling(actor)); } #[cfg(test)] #[test] fn shutdown_clears_patrol_until_a_new_order_arrives() { let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); let handle = fparkan_world::construct_object( &mut world, fparkan_world::ObjectDraft { original_id: None }, ) .unwrap(); fparkan_world::register_object(&mut world, handle).unwrap(); let mut patrol = UnitPatrol::scripted(); assert!(patrol.order(&world, handle, [20.0, 30.0], 60.0)); assert!(patrol.shut_down(handle)); assert!(patrol.is_shut_down(handle)); assert!(!patrol.is_patrolling(handle)); assert!(patrol.order(&world, handle, [35.0, 45.0], 60.0)); assert!(!patrol.is_shut_down(handle)); assert!(patrol.is_patrolling(handle)); } #[cfg(test)] #[test] fn object_patrol_tracks_its_center_and_ignores_repeated_orders() { let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); let mut make_actor = || { let actor = fparkan_world::construct_object( &mut world, fparkan_world::ObjectDraft { original_id: None }, ) .unwrap(); fparkan_world::register_object(&mut world, actor).unwrap(); actor }; let actor = make_actor(); let target = make_actor(); let mut patrol = UnitPatrol::scripted(); assert!(patrol.order_around_object(&world, actor, target, [0.0; 2], 60.0)); assert!(!patrol.order_around_object(&world, actor, target, [0.0; 2], 60.0)); let mut position = transform_state(&world, target).unwrap(); position.position = [20.0_f32, 35.0, 0.0].map(f32::to_bits); set_transform(&mut world, target, position).unwrap(); assert_eq!( patrol.attack_limit(&world, actor).unwrap().center, [20.0, 35.0] ); patrol.order(&world, actor, [0.0; 2], 60.0); assert_eq!(patrol.attack_limit(&world, actor).unwrap().center, [0.0; 2]); } #[cfg(test)] #[test] fn move_orders_append_and_replace_destinations() { let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); let actor = fparkan_world::construct_object( &mut world, fparkan_world::ObjectDraft { original_id: None }, ) .unwrap(); fparkan_world::register_object(&mut world, actor).unwrap(); let mut patrol = UnitPatrol::scripted(); patrol.move_to(&world, actor, [10.0, 20.0], false); patrol.move_to(&world, actor, [30.0, 40.0], true); assert_eq!( patrol.units[0].destinations, Some(VecDeque::from([[10.0, 20.0], [30.0, 40.0]])) ); patrol.move_to(&world, actor, [45.0, 50.0], false); assert_eq!( patrol.units[0].destinations, Some(VecDeque::from([[45.0, 50.0]])) ); } fn distance_to_patrol([x, y]: [f32; 2], [px, py, _]: [f32; 3]) -> f32 { // shortcut: use the horizontal distance until patrol orders carry their native Z. (px - x).hypot(py - y) } #[cfg(test)] #[test] fn patrol_limits_new_targets_and_ends_pursuit_at_the_outer_boundary() { let tracked = ObjectHandle { slot: 1, generation: 1, }; let candidate = ObjectHandle { slot: 2, generation: 1, }; let patrol = PatrolAttackLimit { center: [500.0, 752.0], radius: 150.0, current_target: Some(tracked), }; assert!(patrol.allows_target(candidate, [649.0, 752.0, 0.0])); assert!(!patrol.allows_target(candidate, [650.0, 752.0, 0.0])); assert!(patrol.allows_target(tracked, [650.0, 752.0, 0.0])); assert!(patrol.allows_attack([710.0, 752.0, 0.0])); assert!(!patrol.allows_attack([711.0, 752.0, 0.0])); assert!(!patrol.allows_attack([650.0, 902.0, 0.0])); } fn choose_route( terrain: &TerrainWorld, from: [f32; 3], center: [f32; 2], radius: f32, rng: &mut u32, ) -> Result, EngineError> { // shortcut: choose one routed patrol point at a time with separate RNG state. for _ in 0..16 { let [x, y] = center.map(|axis| axis + (next_crt_rand(rng) as f32 / 32767.0 * 2.0 - 1.0) * radius); let Some(z) = terrain.height_at([x, y]).map_err(EngineError::Terrain)? else { continue; }; if let Some(points) = route_points(terrain, from, [x, y, z])? { return Ok(points); } } Ok(VecDeque::new()) } impl PreparedPatrol { fn displacement_of(&self, handle: ObjectHandle) -> [f32; 3] { self.poses .iter() .find(|(moved, _, _)| *moved == handle) .map_or([0.0; 3], |(_, before, after)| { let from = before.position.map(f32::from_bits); let to = after.position.map(f32::from_bits); std::array::from_fn(|axis| to[axis] - from[axis]) }) } pub(super) fn speed_of(&self, handle: ObjectHandle, can_fly: bool) -> f32 { if self.seconds <= 0.0 { return 0.0; } let mut displacement = self.displacement_of(handle); if !can_fly { // Ground support moves the root vertically without driving the chassis. displacement[2] = 0.0; } crate::cockpit_speedometer::length(displacement) / self.seconds } pub(super) fn flight_velocity_of(&self, handle: ObjectHandle) -> Option<[f32; 3]> { (self.seconds > 0.0).then(|| { self.displacement_of(handle) .map(|distance| distance / self.seconds) }) } pub(super) fn apply_poses(&self, world: &mut World) -> Result<(), EngineError> { // These handles were read during preparation and the world has yet to step. for &(handle, _, after) in &self.poses { set_transform(world, handle, after)?; } Ok(()) } pub(super) fn restore_poses(&self, world: &mut World) -> Result<(), EngineError> { for &(handle, before, _) in &self.poses { set_transform(world, handle, before)?; } Ok(()) } }