From 787279a1f514fbbaf09ca9dd6e5015bdce36635b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 14:16:58 +0400 Subject: feat(ui): control a warbot from the strategic card --- crates/fparkan-assets/src/strategic_roster.rs | 28 +++++ crates/fparkan-runtime/src/boarding.rs | 44 ++++--- .../fparkan-runtime/src/boarding/remote_control.rs | 137 +++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 4 +- crates/fparkan-runtime/src/unit_patrol.rs | 6 +- crates/fparkan-runtime/src/unit_patrol/orders.rs | 7 ++ 6 files changed, 209 insertions(+), 17 deletions(-) create mode 100644 crates/fparkan-runtime/src/boarding/remote_control.rs (limited to 'crates') diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 1cd870a6..86d54b0d 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -28,6 +28,7 @@ const TEXT_LEFT: u32 = 10 + 19 * 2 + 5 + 10; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ListHit { Close, + ManualControl, Row(usize), } @@ -41,6 +42,10 @@ pub fn hit_test([x, y]: [f32; 2], row_count: usize) -> Option { if x >= CLOSE_LEFT as f32 && y < 20.0 { return Some(ListHit::Close); } + // iron3d+0x85c70 expands the icon rectangle by two logical pixels. + if row_count != 0 && (10.0..29.0).contains(&x) && (89.0..108.0).contains(&y) { + return Some(ListHit::ManualControl); + } let y = (y as u32).checked_sub(ROW_TOP)?; let row = (y / ROW_PITCH) as usize; (row < row_count && y % ROW_PITCH < ROW_HEIGHT).then_some(ListHit::Row(row)) @@ -77,6 +82,7 @@ pub struct RosterImages { end: RgbaImage, class_icon: RgbaImage, chassis_icons: [RgbaImage; 4], + manual_control: [RgbaImage; 2], orders: OrderImages, } @@ -164,6 +170,11 @@ impl RosterImages { end, class_icon: icon(49, 110)?, chassis_icons: [icon(97, 94)?, icon(49, 94)?, icon(65, 94)?, icon(81, 94)?], + manual_control: load_ui_sprites( + vfs, + "ui/hq.cfg", + ["short_button_frame_off", "buildscreen_hq_icon"], + )?, orders: OrderImages::load(vfs)?, }) } @@ -173,6 +184,7 @@ impl RosterImages { &self, rows: &[Row<'_>], card: &[Option>], + manual_control_enabled: bool, orders: Option<([&str; 6], Option)>, ) -> Result { let mut panel = self.background.clone(); @@ -182,6 +194,13 @@ impl RosterImages { let name = self.font.centered_line(row.label, width as u16, 16)?; stamp(&mut panel, &name, [49, 25, 49 + width, 41], [255, 255, 0]); warbot_card::paint(&self.font, &mut panel, card)?; + let tint = if manual_control_enabled { + [255; 3] + } else { + [128; 3] + }; + stamp(&mut panel, &self.manual_control[0], [9, 88, 30, 109], tint); + stamp(&mut panel, &self.manual_control[1], [12, 91, 27, 106], tint); } for (index, row) in rows.iter().take(19).enumerate() { let y = ROW_TOP + index as u32 * ROW_PITCH; @@ -290,6 +309,15 @@ mod tests { assert_eq!(hit_test([200.0, 480.0], 20), None); } + #[test] + fn manual_control_uses_the_enlarged_native_hit_box() { + assert_eq!(hit_test([61.0, 89.0], 3), Some(ListHit::ManualControl)); + assert_eq!(hit_test([79.0, 107.0], 3), Some(ListHit::ManualControl)); + assert_eq!(hit_test([80.0, 107.0], 3), None); + assert_eq!(hit_test([61.0, 108.0], 3), None); + assert_eq!(hit_test([61.0, 89.0], 0), None); + } + #[test] fn original_durability_colours_follow_the_rounded_health_percentage() { let durability_fill = |percent| super::durability_fill(percent, TEXT_WIDTH); diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index 9d15fedd..28ed6f61 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -11,6 +11,8 @@ use fparkan_world::{ }; pub(super) const WARBOT_CLASS: u32 = 0x0100_8000; +mod remote_control; +pub use remote_control::{can_control_warbot_remotely, control_warbot_remotely}; /// Ownership and cockpit changes from one Enter command. pub struct VehicleEntry { @@ -32,6 +34,7 @@ impl VehicleEntry { pub(super) struct Boarding { pub(super) cockpit: StartingCockpit, pub(super) hero: StartingCockpit, + pub(super) remote: bool, pub(super) can_fly: bool, pub(super) ground: Option, pub(super) on_building_floor: bool, @@ -156,15 +159,6 @@ pub fn board_friendly_warbot( let Some((cockpit, position)) = candidate else { return Ok(None); }; - let can_fly = crate::player_motion::warbot_can_fly(engine, cockpit.object_index); - let ground = (!can_fly) - .then(|| { - crate::ground_support::GroundSupport::prepare( - &mission.mission_assets, - cockpit.object_index, - ) - }) - .flatten(); set_transform( &mut engine.world, hero.handle, @@ -173,12 +167,31 @@ pub fn board_friendly_warbot( ..hero_pose }, )?; + start_warbot_control(engine, hero, cockpit, false); + Ok(Some(cockpit)) +} + +fn start_warbot_control( + engine: &mut Engine, + hero: StartingCockpit, + cockpit: StartingCockpit, + remote: bool, +) { + let can_fly = crate::player_motion::warbot_can_fly(engine, cockpit.object_index); if let Some(mission) = &mut engine.loaded { mission.boarding = Some(Boarding { cockpit, hero, + remote, can_fly, - ground, + ground: (!can_fly) + .then(|| { + crate::ground_support::GroundSupport::prepare( + &mission.mission_assets, + cockpit.object_index, + ) + }) + .flatten(), on_building_floor: false, flight_velocity: [0.0; 3], }); @@ -186,7 +199,6 @@ pub fn board_friendly_warbot( mission.hero_walk_velocity = [0.0; 2]; mission.hero_look.reset(); } - Ok(Some(cockpit)) } pub(super) fn vehicle_interaction_distance_sq( @@ -285,6 +297,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result, let Some(Boarding { cockpit: vehicle, hero, + remote, can_fly, on_building_floor, .. @@ -293,7 +306,10 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result, return Ok(None); }; let mut pose = transform_state(&engine.world, hero.handle)?; - if let Ok(vehicle_pose) = transform_state(&engine.world, vehicle.handle) { + let vehicle_pose = (!remote) + .then(|| transform_state(&engine.world, vehicle.handle).ok()) + .flatten(); + if let Some(vehicle_pose) = vehicle_pose { let [x, y, _] = vehicle_pose.position.map(f32::from_bits); let yaw = f32::from_bits(vehicle_pose.orientation[2]); let (sin, cos) = yaw.sin_cos(); @@ -322,7 +338,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result, pose.position = exit.map(f32::to_bits); pose.orientation[2] = yaw.to_bits(); set_transform(&mut engine.world, hero.handle, pose)?; - } else if !can_fly && !on_building_floor { + } else if !remote && !can_fly && !on_building_floor { // The passenger still has the destroyed chassis's root height. let [x, y, _] = pose.position.map(f32::from_bits); if let Some(ground) = mission @@ -347,7 +363,7 @@ pub(super) fn move_passenger( vehicle_pose: TransformState, ) -> Option<(ObjectHandle, TransformState)> { let boarding = engine.loaded.as_ref()?.boarding.as_ref()?; - if boarding.cockpit.handle != cockpit.handle { + if boarding.remote || boarding.cockpit.handle != cockpit.handle { return None; } let hero = boarding.hero; diff --git a/crates/fparkan-runtime/src/boarding/remote_control.rs b/crates/fparkan-runtime/src/boarding/remote_control.rs new file mode 100644 index 00000000..960a19d2 --- /dev/null +++ b/crates/fparkan-runtime/src/boarding/remote_control.rs @@ -0,0 +1,137 @@ +//! The command-post link to a friendly warbot's cockpit. + +use super::{has_working_pilot_control, start_warbot_control}; +use crate::{ + component_cockpit, + strategic_roster::warbot_rows, + strategic_view::{command_post, CommandPost}, + Engine, StartingCockpit, +}; +use fparkan_world::{identity_metadata, ObjectHandle}; + +fn remote_cockpit( + engine: &Engine, + hero: StartingCockpit, + target: ObjectHandle, +) -> Option { + let mission = engine.loaded.as_ref()?; + if mission.boarding.is_some() + || !matches!(command_post(engine, hero), Some(CommandPost::Bunker(_))) + || !warbot_rows(engine, hero) + .iter() + .any(|row| row.handle == target && row.selectable) + || mission + .unit_patrol + .as_ref() + .is_some_and(|patrol| patrol.is_leaving_factory(target)) + { + return None; + } + let table = mission + .control_device_tables + .iter() + .find(|table| table.handle == target)?; + if !has_working_pilot_control(table) { + return None; + } + let id = identity_metadata(&engine.world, target).ok()?.original_id?; + component_cockpit(mission, id.0 as usize, target) +} + +/// Whether the selected friendly warbot has a usable manual-control cockpit. +#[must_use] +pub fn can_control_warbot_remotely( + engine: &Engine, + hero: StartingCockpit, + target: ObjectHandle, +) -> bool { + remote_cockpit(engine, hero, target).is_some() +} + +/// Gives the hero manual control of a warbot while the hero remains at the bunker. +pub fn control_warbot_remotely( + engine: &mut Engine, + hero: StartingCockpit, + target: ObjectHandle, +) -> Option { + let cockpit = remote_cockpit(engine, hero, target)?; + if let Some(patrol) = engine.loaded.as_mut()?.unit_patrol.as_mut() { + patrol.release_orders_for_manual_control(target); + } + start_warbot_control(engine, hero, cockpit, true); + Some(cockpit) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + create, disembark_warbot, frame_with_player_motion, load_gameplay_mission, + plan_player_motion, starting_cockpit, strategic_orders, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, PlayerMotion, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::transform_state; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_fortresses_drives_the_warbot_without_moving_the_hero() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let hero = starting_cockpit(&engine).unwrap(); + let actor = warbot_rows(&engine, hero)[0].handle; + let hero_pose = transform_state(&engine.world, hero.handle).unwrap(); + let vehicle_pose = transform_state(&engine.world, actor).unwrap(); + assert!(crate::boarding::vehicle_interaction_distance_sq( + hero_pose.position.map(f32::from_bits), + vehicle_pose.position.map(f32::from_bits) + ) + .is_none()); + assert!(strategic_orders::stand(&mut engine, hero, actor)); + let vehicle = control_warbot_remotely(&mut engine, hero, actor).unwrap(); + assert_eq!(vehicle.handle, actor); + assert_eq!(warbot_rows(&engine, hero)[0].order_label, 0x1824); + for time in [100, 200, 300, 400, 500] { + let motion = plan_player_motion( + &engine, + vehicle, + PlayerMotion { + movement: [0.0, 1.0], + delta_seconds: 0.1, + ..PlayerMotion::default() + }, + ) + .unwrap() + .unwrap(); + frame_with_player_motion(&mut engine, time, vehicle, motion, false).unwrap(); + } + assert_ne!( + transform_state(&engine.world, actor).unwrap().position, + vehicle_pose.position + ); + assert_eq!( + transform_state(&engine.world, hero.handle).unwrap(), + hero_pose + ); + assert_eq!(disembark_warbot(&mut engine).unwrap(), Some(hero)); + assert_eq!( + transform_state(&engine.world, hero.handle).unwrap(), + hero_pose + ); + assert!(crate::player_vehicle(&engine).is_none()); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index ad5709b3..2300adc3 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -184,8 +184,8 @@ pub use wheel_animation::WheelAnimation; pub use aim_convergence::{projectile_sight_target, turret_sight_points}; pub use autodemo::{autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView}; pub use boarding::{ - board_friendly_warbot, disembark_warbot, enter_vehicle, player_vehicle, select_boarding_target, - VehicleEntry, + board_friendly_warbot, can_control_warbot_remotely, control_warbot_remotely, disembark_warbot, + enter_vehicle, player_vehicle, select_boarding_target, VehicleEntry, }; pub use building_door::{has_proximity_doors, sample_capture_doors, sample_proximity_doors}; pub use capture_barrier::sample_capture_barrier; diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 6a230135..1d83eaa8 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -580,7 +580,11 @@ impl UnitPatrol { } } for unit in &mut state.units { - if self.is_leaving_factory(unit.handle) { + if self.is_leaving_factory(unit.handle) + || mission + .boarding + .is_some_and(|control| control.remote && control.cockpit.handle == unit.handle) + { continue; } let Ok(before) = transform_state(world, unit.handle) else { diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index 8198b17d..fd886d80 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -62,6 +62,13 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn release_orders_for_manual_control(&mut self, actor: ObjectHandle) { + self.units.retain(|unit| unit.handle != actor); + self.script_orders.retain(|(handle, _)| *handle != actor); + self.standing.remove(&actor); + self.shut_down.remove(&actor); + } + pub(crate) fn command_order(&mut self, world: &World, actor: ObjectHandle, order: UnitOrder) { self.script_orders.retain(|(unit, _)| *unit != actor); if self.is_leaving_factory(actor) { -- cgit v1.2.3