diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 44 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/boarding/remote_control.rs | 137 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 7 |
5 files changed, 181 insertions, 17 deletions
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<crate::ground_support::GroundSupport>, 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<Option<StartingCockpit>, let Some(Boarding { cockpit: vehicle, hero, + remote, can_fly, on_building_floor, .. @@ -293,7 +306,10 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, 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<Option<StartingCockpit>, 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<StartingCockpit> { + 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<StartingCockpit> { + 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) { |
