From bd7811533a348e56d2be9c7281d0533fc8c46024 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 18:33:49 +0400 Subject: feat(ui): follow mobile command posts with the strategic camera Connect captured command vehicles and follow their native orbit. Verify the sweep and acceleration against the original DLL. In a Teleport debug copy, verify Enter, edge rotation and Esc with Vulkan validation enabled. The stock start opens cleanly. The stock route and the original window comparison remain pending. --- crates/fparkan-runtime/src/boarding.rs | 18 +-- crates/fparkan-runtime/src/mission_events.rs | 2 +- crates/fparkan-runtime/src/strategic_view.rs | 109 +++++++++++---- .../src/strategic_view/vehicle_camera.rs | 153 +++++++++++++++++++++ 4 files changed, 249 insertions(+), 33 deletions(-) create mode 100644 crates/fparkan-runtime/src/strategic_view/vehicle_camera.rs (limited to 'crates') diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index c453b74e..95acc7e0 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -61,7 +61,7 @@ pub fn player_vehicle(engine: &Engine) -> Option { Some(engine.loaded.as_ref()?.boarding.as_ref()?.cockpit) } -/// Selects the friendly warbot under a line of sight supplied by the camera. +/// Selects an allied vehicle for boarding or a mobile command link. pub fn select_boarding_target( engine: &Engine, hero: StartingCockpit, @@ -79,14 +79,16 @@ pub fn select_boarding_target( { return None; } - let warbot = mission.object_drafts.iter().any(|draft| { - draft.unit_dat_header_flags == Some(WARBOT_CLASS) - && draft - .original_id - .and_then(|id| handle_by_original_id(&engine.world, id)) - == Some(target.handle) + let vehicle = mission.object_drafts.iter().any(|draft| { + matches!( + draft.unit_dat_header_flags, + Some(WARBOT_CLASS | crate::mission_events::MOBILE_HQ_CLASS) + ) && draft + .original_id + .and_then(|id| handle_by_original_id(&engine.world, id)) + == Some(target.handle) }); - (warbot + (vehicle && identity_metadata(&engine.world, target.handle) .ok()? .owner_id diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 8a6f3592..4721a258 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -15,7 +15,7 @@ use fparkan_world::{ }; use std::collections::{HashMap, HashSet}; -const MOBILE_HQ_CLASS: u32 = 0x0101_0000; +pub(super) const MOBILE_HQ_CLASS: u32 = 0x0101_0000; pub(super) const MAIN_TELEPORT_CLASS: u32 = 0x8000_0200; /// First encounter with an uncovered periodic script instruction. diff --git a/crates/fparkan-runtime/src/strategic_view.rs b/crates/fparkan-runtime/src/strategic_view.rs index 250f943e..daf6f17f 100644 --- a/crates/fparkan-runtime/src/strategic_view.rs +++ b/crates/fparkan-runtime/src/strategic_view.rs @@ -5,6 +5,8 @@ use fparkan_render::{LegacyIron3dEulerTransform, RawCameraTransform}; use fparkan_world::{identity_metadata, registration_sequence, transform_state, ObjectHandle}; use std::f32::consts::FRAC_PI_2; +mod vehicle_camera; + /// Horizontal field of view of the strategic camera. pub const COMMAND_FOV: f32 = 1.04; @@ -13,7 +15,7 @@ pub const COMMAND_FOV: f32 = 1.04; pub enum CommandPost { /// Hero at their own clan's bunker communicator. Bunker(ObjectHandle), - /// The vehicle the hero is currently piloting. + /// A vehicle providing a mobile command link. Warbot(ObjectHandle), } @@ -25,13 +27,8 @@ pub fn command_post(engine: &Engine, cockpit: StartingCockpit) -> Option Option Option { + if crate::player_vehicle(engine).is_some() { + return None; + } + let hero_owner = identity_metadata(&engine.world, cockpit.handle) + .ok()? + .owner_id?; + let metadata = identity_metadata(&engine.world, target).ok()?; + let draft = engine + .loaded + .as_ref()? + .object_drafts + .get(metadata.original_id?.0 as usize)?; + if metadata.owner_id != Some(hero_owner) + || draft.unit_dat_header_flags != Some(crate::mission_events::MOBILE_HQ_CLASS) + || !vehicle_command_link(engine, target) + { + return None; + } + let hero = post_position(engine, cockpit.handle)?; + let position = post_position(engine, target)?; + crate::boarding::vehicle_interaction_distance_sq(hero, position)?; + Some(CommandPost::Warbot(target)) +} + +fn vehicle_command_link(engine: &Engine, handle: ObjectHandle) -> bool { + engine.loaded.as_ref().is_some_and(|mission| { + mission.control_device_tables.iter().any(|table| { + table.handle == handle + && !table.node_provider_depletion_marked + && matches!(&table.status, MissionControlDeviceTableStatus::Available(devices) + if has_command_link(devices)) + }) + }) +} + fn has_command_link(devices: &[crate::EffectiveControlDevice]) -> bool { // iron3d+0x773a0 and Control+0x2c3c0 search the current tags from index zero. let index = devices.iter().position(|device| device.descriptor.tag == 1); @@ -88,7 +126,7 @@ fn post_position(engine: &Engine, handle: ObjectHandle) -> Option<[f32; 3]> { ) } -/// Held directions of the bunker camera during the current frame. +/// Held command camera directions during the current frame. #[derive(Clone, Copy, Default)] pub struct CommandCameraInput { /// Motion along the camera's right, forward and vertical axes. @@ -142,6 +180,7 @@ pub struct CommandCamera { position: [f32; 3], yaw: f32, tilt: f32, + follow_distance: f32, axes: [CameraAxis; 5], } @@ -157,6 +196,7 @@ impl CommandCamera { position: post_position(engine, handle)?, yaw: FRAC_PI_2, tilt: 1.0, + follow_distance: 0.0, axes: [CameraAxis::default(); 5], }) } @@ -170,28 +210,45 @@ impl CommandCamera { input: CommandCameraInput, building_height: impl FnOnce([[f32; 3]; 2]) -> Option, ) -> Option { - let terrain = &engine.loaded.as_ref()?.terrain; + let mission = engine.loaded.as_ref()?; + let terrain = &mission.terrain; let [min, max] = terrain.surface_bounds()?; - let clearance = match self.post { + match self.post { CommandPost::Bunker(_) => { self.advance_bunker(dt, input, max); - 36.0 } CommandPost::Warbot(handle) => { - self.position = post_position(engine, handle)?; - 2.5 + if !vehicle_command_link(engine, handle) { + return None; + } + let target = post_position(engine, handle)?; + let state = transform_state(&engine.world, handle).ok()?; + let index = identity_metadata(&engine.world, handle) + .ok()? + .original_id? + .0 as usize; + let (_, radius) = + crate::projectile::unit_sphere(&mission.mission_assets, index, state)?; + self.advance_vehicle(dt, input.rotation, max, target, radius); } - }; + } let [x, y, z] = self.position; let ground = terrain.height_at([x, y]).ok()?; - // iron3d+0xa1470 casts from the map's upper plane and includes roofs. - let surface = building_height([[x, y, max[2]], [x, y, ground.unwrap_or(min[2])]]) - .or(ground) - .unwrap_or(0.0); - self.position[2] = z.max(surface + clearance); - if let CommandPost::Bunker(handle) = self.post { - self.position[2] = self.position[2].min(surface + clearance + 200.0); - self.limit_to_bunker(post_position(engine, handle)?); + let surface = match self.post { + CommandPost::Bunker(handle) => { + // Only the bunker passes the roof flags to iron3d+0xa1470. + let surface = + building_height([[x, y, max[2]], [x, y, ground.unwrap_or(min[2])]]).or(ground); + self.limit_to_bunker(post_position(engine, handle)?); + surface + } + CommandPost::Warbot(_) => ground, + } + .unwrap_or(0.0); + // The last camera update at iron3d+0x37720 applies this band in both modes. + self.position[2] = z.clamp(surface + 36.0, surface + 236.0); + if !self.position.into_iter().all(f32::is_finite) { + return None; } let matrix = LegacyIron3dEulerTransform { translation: self.position, @@ -222,7 +279,6 @@ impl CommandCamera { fn move_bunker(&mut self, dt: f32, input: CommandCameraInput, map_max: [f32; 3]) { let [right, forward, up] = input.translation; - let [yaw, tilt] = input.rotation; let right = self.axes[0].step(right, 125.0, dt); let forward = self.axes[1].step(forward, 125.0, dt); let up = self.axes[2].step(up, 62.5, dt); @@ -238,6 +294,10 @@ impl CommandCamera { } } self.position[2] += up; + self.rotate(dt, input.rotation); + } + + fn rotate(&mut self, dt: f32, [yaw, tilt]: [i8; 2]) { self.yaw += self.axes[3].step(yaw, 1.5, dt); let next_tilt = self.tilt + self.axes[4].step(tilt, 1.5, dt); if 0.0 < next_tilt && next_tilt < FRAC_PI_2 { @@ -294,6 +354,7 @@ mod tests { position: origin, yaw: FRAC_PI_2, tilt: 1.0, + follow_distance: 0.0, axes: [CameraAxis::default(); 5], }; let input = CommandCameraInput { diff --git a/crates/fparkan-runtime/src/strategic_view/vehicle_camera.rs b/crates/fparkan-runtime/src/strategic_view/vehicle_camera.rs new file mode 100644 index 00000000..424d065b --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_view/vehicle_camera.rs @@ -0,0 +1,153 @@ +use super::{CameraAxis, CommandCamera, COMMAND_FOV}; + +impl CommandCamera { + pub(super) fn advance_vehicle( + &mut self, + dt: f32, + rotation: [i8; 2], + map_max: [f32; 3], + target: [f32; 3], + radius: f32, + ) { + if dt <= 0.0 { + return; + } + // iron3d+0x37f70 follows an orbit point in world axes. + let (sin_yaw, cos_yaw) = self.yaw.sin_cos(); + let (sin_tilt, cos_tilt) = self.tilt.sin_cos(); + let horizontal = sin_tilt * self.follow_distance; + let goal = [ + target[0] - cos_yaw * horizontal, + target[1] - sin_yaw * horizontal, + target[2] + cos_tilt * self.follow_distance, + ]; + let margin = (0.5 * COMMAND_FOV).tan() * self.position[2]; + for (axis, (position, goal)) in self.position.iter_mut().zip(goal).enumerate() { + let (snap, speed) = if axis < 2 { (3.0, 125.0) } else { (2.0, 62.5) }; + let offset = goal - *position; + if offset.abs() <= snap { + *position = goal; + self.axes[axis] = CameraAxis::default(); + continue; + } + let direction = if offset > 0.0 { 1 } else { -1 }; + let next = *position + self.axes[axis].step(direction, speed, dt); + if axis == 2 + || (offset < 0.0 && next > margin) + || (offset > 0.0 && next < map_max[axis] - margin) + { + *position = next; + } + } + self.rotate(dt, rotation); + + // iron3d+0x38250 adjusts the orbit distance once per active frame. + if self.follow_distance > radius * 8.0 { + self.follow_distance -= 1.0; + } + if self.follow_distance < 200.0 { + self.follow_distance += 1.0; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::strategic_view::{mobile_command_post, CommandCameraInput, CommandPost}; + use crate::{ + create, enter_vehicle, frame, load_gameplay_mission, mission_object_handle, player_vehicle, + starting_cockpit, EngineServices, MissionMode, MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{set_transform, transform_state, ObjectHandle, OriginalObjectId}; + use std::f32::consts::FRAC_PI_2; + use std::sync::Arc; + + #[test] + fn vehicle_camera_tracks_the_native_orbit_in_world_axes() { + let mut camera = CommandCamera { + post: CommandPost::Warbot(ObjectHandle { + slot: 0, + generation: 0, + }), + position: [402.9, 430.0, 92.0], + yaw: FRAC_PI_2, + tilt: 1.0, + follow_distance: 25.0, + axes: [CameraAxis::default(); 5], + }; + let max = [1024.0; 3]; + let target = [400.0, 450.0, 80.0]; + camera.advance_vehicle(0.01, [0, 0], max, target, 3.0); + for (actual, expected) in camera + .position + .into_iter() + .zip([400.0, 428.96323, 93.50756]) + { + assert!((actual - expected).abs() < 0.00005); + } + assert_eq!(camera.follow_distance, 25.0); + camera.position = [420.0, 400.0, 80.0]; + camera.advance_vehicle(0.01, [1, 0], max, target, 3.0); + assert_eq!(camera.position, [420.0, 400.0, 80.0]); + camera.advance_vehicle(0.25, [1, 0], max, target, 3.0); + assert_eq!(camera.position, [404.375, 415.625, 87.8125]); + assert!((camera.yaw - FRAC_PI_2 - 0.1875).abs() < 1e-6); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn captured_teleport_hq_supplies_mobile_camera_without_boarding() { + 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/CAMPAIGN/CAMPAIGN.00/Mission.04/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + frame(&mut engine, 1).unwrap(); + let hero = starting_cockpit(&engine).unwrap(); + let hq = mission_object_handle(&engine, OriginalObjectId(0)).unwrap(); + let original_pose = transform_state(&engine.world, hero.handle).unwrap(); + let mut near_hq = original_pose; + near_hq.position = transform_state(&engine.world, hq).unwrap().position; + set_transform(&mut engine.world, hero.handle, near_hq).unwrap(); + assert_eq!(mobile_command_post(&engine, hero, hq), None); + assert_eq!( + enter_vehicle(&mut engine, hero, None).unwrap().captured, + Some(OriginalObjectId(0)) + ); + let post = mobile_command_post(&engine, hero, hq); + assert_eq!(post, Some(CommandPost::Warbot(hq))); + set_transform(&mut engine.world, hero.handle, original_pose).unwrap(); + assert_eq!(mobile_command_post(&engine, hero, hq), None); + + let mut camera = CommandCamera::new(&engine, post.unwrap()).unwrap(); + let terrain_only = |_| panic!("mobile camera should skip the roof query"); + let start = camera + .sample(&engine, 0.0, CommandCameraInput::default(), terrain_only) + .unwrap(); + let input = CommandCameraInput { + rotation: [1, 0], + ..Default::default() + }; + let mut moved = start; + for _ in 0..30 { + moved = camera + .sample(&engine, 1.0 / 60.0, input, terrain_only) + .unwrap(); + } + assert_ne!(moved.translation(), start.translation()); + assert_ne!(moved.words[0], start.words[0]); + assert_eq!(player_vehicle(&engine), None); + } +} -- cgit v1.2.3