diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-03 18:00:18 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-03 18:00:18 +0300 |
| commit | 099e48fa775b849654d1102a6c1d9e84ded7e1f4 (patch) | |
| tree | 25864aa84bdb465d0ca52f5d28c5a1b20a23e041 /crates/fparkan-runtime/src | |
| parent | 4b9277258224ab846b84cafe1d6a3b91e29a05c5 (diff) | |
| download | fparkan-099e48fa775b849654d1102a6c1d9e84ded7e1f4.tar.xz fparkan-099e48fa775b849654d1102a6c1d9e84ded7e1f4.zip | |
feat(game): cycle warbot cockpit control modes
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 62 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/boarding/remote_control.rs | 87 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_indicators.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_speedometer.rs | 18 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 42 |
8 files changed, 190 insertions, 46 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index c8f586b5..4aaf2cc8 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -14,8 +14,8 @@ pub(super) const WARBOT_CLASS: u32 = 0x0100_8000; pub mod bunker; mod remote_control; pub use remote_control::{ - can_control_warbot_remotely, control_warbot_remotely, remote_automatic_weapons, - remote_warbot_control, RemoteWarbotControl, + can_control_warbot_remotely, control_warbot_remotely, cycle_warbot_control, + remote_automatic_weapons, remote_warbot_control, warbot_control_mode, RemoteWarbotControl, }; /// Ownership and cockpit changes from one Enter command. @@ -38,7 +38,8 @@ impl VehicleEntry { pub(super) struct Boarding { pub(super) cockpit: StartingCockpit, pub(super) hero: StartingCockpit, - pub(super) remote: Option<RemoteWarbotControl>, + pub(super) remote: bool, + pub(super) control_mode: RemoteWarbotControl, pub(super) turret: Option<crate::TurretAim>, pub(super) can_fly: bool, pub(super) ground: Option<crate::ground_support::GroundSupport>, @@ -48,12 +49,11 @@ pub(super) struct Boarding { impl Boarding { pub(super) fn controls_chassis(self) -> bool { - self.turret.is_none() && self.remote != Some(RemoteWarbotControl::Autopilot) + self.turret.is_none() && self.control_mode != RemoteWarbotControl::Autopilot } pub(super) fn controls_weapons(self) -> bool { - self.remote - .is_none_or(|mode| mode == RemoteWarbotControl::Manual) + self.control_mode == RemoteWarbotControl::Manual } } @@ -202,7 +202,8 @@ fn start_player_control( mission.boarding = Some(Boarding { cockpit, hero, - remote, + remote: remote.is_some(), + control_mode: remote.unwrap_or(RemoteWarbotControl::Manual), turret, can_fly, ground: (turret.is_none() && !can_fly) @@ -292,6 +293,27 @@ mod tests { assert_eq!(entry.cockpit.unwrap().handle, target); assert_eq!(entry.capture_notice(), None); assert_eq!(player_vehicle(&engine), entry.cockpit); + + assert_eq!( + super::warbot_control_mode(&engine), + Some(super::RemoteWarbotControl::Manual) + ); + assert_eq!( + super::cycle_warbot_control(&mut engine, None), + Some(super::RemoteWarbotControl::AutoGunner) + ); + assert_eq!( + super::cycle_warbot_control(&mut engine, None), + Some(super::RemoteWarbotControl::Autopilot) + ); + let boarding = engine.loaded.as_ref().unwrap().boarding.unwrap(); + let mut autonomous_pose = transform_state(&engine.world, target).unwrap(); + autonomous_pose.position[0] = (x + 5.0).to_bits(); + let (passenger, _, following) = + super::passenger_pose(&engine.world, boarding, target, autonomous_pose).unwrap(); + assert_eq!(passenger, hero.handle); + assert_eq!(following.position, autonomous_pose.position); + assert!(super::remote_warbot_control(&engine).is_none()); } } @@ -329,8 +351,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, return Ok(None); }; let mut pose = transform_state(&engine.world, hero.handle)?; - let vehicle_pose = remote - .is_none() + let vehicle_pose = (!remote) .then(|| transform_state(&engine.world, vehicle.handle).ok()) .flatten(); if let Some(vehicle_pose) = vehicle_pose { @@ -362,7 +383,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 remote.is_none() && !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 @@ -392,16 +413,27 @@ pub(super) fn move_passenger( cockpit: StartingCockpit, vehicle_pose: TransformState, ) -> Option<(ObjectHandle, TransformState)> { - let boarding = engine.loaded.as_ref()?.boarding.as_ref()?; - if boarding.remote.is_some() || boarding.cockpit.handle != cockpit.handle { + let boarding = engine.loaded.as_ref()?.boarding?; + let (hero, previous, pose) = + passenger_pose(&engine.world, boarding, cockpit.handle, vehicle_pose)?; + set_transform(&mut engine.world, hero, pose).ok()?; + Some((hero, previous)) +} + +pub(super) fn passenger_pose( + world: &fparkan_world::World, + boarding: Boarding, + vehicle: ObjectHandle, + vehicle_pose: TransformState, +) -> Option<(ObjectHandle, TransformState, TransformState)> { + if boarding.remote || boarding.cockpit.handle != vehicle { return None; } let hero = boarding.hero; - let previous = transform_state(&engine.world, hero.handle).ok()?; + let previous = transform_state(world, hero.handle).ok()?; let pose = TransformState { position: vehicle_pose.position, ..previous }; - set_transform(&mut engine.world, hero.handle, pose).ok()?; - Some((hero.handle, previous)) + Some((hero.handle, previous, pose)) } diff --git a/crates/fparkan-runtime/src/boarding/remote_control.rs b/crates/fparkan-runtime/src/boarding/remote_control.rs index 1a881a5d..022ca612 100644 --- a/crates/fparkan-runtime/src/boarding/remote_control.rs +++ b/crates/fparkan-runtime/src/boarding/remote_control.rs @@ -9,7 +9,7 @@ use crate::{ }; use fparkan_world::{identity_metadata, ObjectHandle}; -/// Which systems the player operates from the command post. +/// Which systems the player operates from a warbot cockpit. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum RemoteWarbotControl { /// The player pilots the chassis and fires the weapons. @@ -84,14 +84,49 @@ pub fn control_warbot_remotely( #[must_use] pub fn remote_warbot_control(engine: &Engine) -> Option<RemoteWarbotControl> { let boarding = engine.loaded.as_ref()?.boarding?; - boarding - .turret - .is_none() - .then_some(boarding.remote) - .flatten() + (boarding.remote && boarding.turret.is_none()).then_some(boarding.control_mode) } -/// The active autonomous weapons' ammunition, charge and firing phases. +/// The active control mode of a warbot, aboard or through a remote link. +#[must_use] +pub fn warbot_control_mode(engine: &Engine) -> Option<RemoteWarbotControl> { + let boarding = engine.loaded.as_ref()?.boarding?; + boarding.turret.is_none().then_some(boarding.control_mode) +} + +/// Switches the current warbot without re-entering its cockpit or replacing its order. +pub fn cycle_warbot_control( + engine: &mut Engine, + manual_weapons: Option<&mut PlayerWeapons>, +) -> Option<RemoteWarbotControl> { + use RemoteWarbotControl::{AutoGunner, Autopilot, Manual}; + + let boarding = engine.loaded.as_mut()?.boarding.as_mut()?; + if boarding.turret.is_some() { + return None; + } + let mode = match boarding.control_mode { + Manual => AutoGunner, + AutoGunner => Autopilot, + Autopilot => Manual, + }; + if let Some((manual, automatic)) = manual_weapons.zip( + engine + .autodemo_combat + .as_mut() + .and_then(|combat| combat.weapons_mut(boarding.cockpit.handle)), + ) { + if boarding.control_mode == Manual { + automatic.adopt_cycle_from(manual); + } else if mode == Manual { + manual.adopt_cycle_from(automatic); + } + } + boarding.control_mode = mode; + Some(mode) +} + +/// The active warbot's automatic weapons, ammunition, charge and firing phases. #[must_use] pub fn remote_automatic_weapons(engine: &Engine) -> Option<&PlayerWeapons> { let boarding = engine.loaded.as_ref()?.boarding?; @@ -264,6 +299,44 @@ pub(super) mod tests { }; let current = transform_state(&engine.world, actor).unwrap(); assert!(from_waypoint(current.position) < from_waypoint(start.position)); + let mut manual = crate::prepared_warbot_weapons(&engine) + .find(|(index, _)| *index == cockpit.object_index) + .unwrap() + .1; + let automatic_readout: Vec<_> = remote_automatic_weapons(&engine) + .unwrap() + .indicators() + .collect(); + assert_eq!( + cycle_warbot_control(&mut engine, Some(&mut manual)), + Some(RemoteWarbotControl::Manual) + ); + assert_eq!(manual.indicators().collect::<Vec<_>>(), automatic_readout); + assert!(remote_automatic_weapons(&engine).is_none()); + assert!(plan_player_motion( + &engine, + cockpit, + PlayerMotion { + movement: [0.0, 1.0], + delta_seconds: 0.1, + ..PlayerMotion::default() + }, + ) + .unwrap() + .is_some()); + assert_eq!(warbot_rows(&engine, hero)[0].order_label, 0x1828); + assert_eq!( + cycle_warbot_control(&mut engine, Some(&mut manual)), + Some(RemoteWarbotControl::AutoGunner) + ); + assert_eq!( + cycle_warbot_control(&mut engine, Some(&mut manual)), + Some(RemoteWarbotControl::Autopilot) + ); + assert_eq!( + remote_warbot_control(&engine), + Some(RemoteWarbotControl::Autopilot) + ); assert_eq!(disembark_warbot(&mut engine).unwrap(), Some(hero)); assert_eq!( transform_state(&engine.world, hero.handle).unwrap(), diff --git a/crates/fparkan-runtime/src/cockpit_indicators.rs b/crates/fparkan-runtime/src/cockpit_indicators.rs index 651949dd..b3016a3e 100644 --- a/crates/fparkan-runtime/src/cockpit_indicators.rs +++ b/crates/fparkan-runtime/src/cockpit_indicators.rs @@ -18,10 +18,9 @@ pub struct CockpitIndicators { pub fn cockpit_indicators(engine: &Engine, cockpit: StartingCockpit) -> Option<CockpitIndicators> { let mission = engine.loaded.as_ref()?; let (onsite, control) = match mission.boarding { - Some(board) if board.cockpit.handle == cockpit.handle => ( - board.remote.is_none(), - board.remote.unwrap_or(RemoteWarbotControl::Manual), - ), + Some(board) if board.cockpit.handle == cockpit.handle => { + (!board.remote, board.control_mode) + } None if mission.local_hero == Some(cockpit.handle) => (true, RemoteWarbotControl::Manual), _ => return None, }; diff --git a/crates/fparkan-runtime/src/cockpit_speedometer.rs b/crates/fparkan-runtime/src/cockpit_speedometer.rs index 021fc3ec..5069013b 100644 --- a/crates/fparkan-runtime/src/cockpit_speedometer.rs +++ b/crates/fparkan-runtime/src/cockpit_speedometer.rs @@ -55,13 +55,19 @@ pub(super) fn record_route_speed( mission: &mut LoadedMissionState, patrol: Option<&PreparedPatrol>, ) { - mission.cockpit_speed = mission + let driving = mission .boarding - .filter(|board| board.remote == Some(RemoteWarbotControl::Autopilot)) - .zip(patrol) - .map_or(0.0, |(board, patrol)| { - patrol.speed_of(board.cockpit.handle, board.can_fly) - }); + .as_mut() + .filter(|board| board.control_mode == RemoteWarbotControl::Autopilot) + .zip(patrol); + mission.cockpit_speed = driving.map_or(0.0, |(board, patrol)| { + if board.can_fly { + if let Some(velocity) = patrol.flight_velocity_of(board.cockpit.handle) { + board.flight_velocity = velocity; + } + } + patrol.speed_of(board.cockpit.handle, board.can_fly) + }); } pub(super) fn length(vector: [f32; 3]) -> f32 { diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 2ac3f856..b64b60f8 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -192,9 +192,10 @@ pub use aim_convergence::{projectile_sight_target, turret_sight_points}; pub use autodemo::{autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView}; pub use boarding::bunker as bunker_control; pub use boarding::{ - board_friendly_warbot, can_control_warbot_remotely, control_warbot_remotely, disembark_warbot, - enter_vehicle, player_vehicle, remote_automatic_weapons, remote_warbot_control, - select_boarding_target, RemoteWarbotControl, VehicleEntry, + board_friendly_warbot, can_control_warbot_remotely, control_warbot_remotely, + cycle_warbot_control, disembark_warbot, enter_vehicle, player_vehicle, + remote_automatic_weapons, remote_warbot_control, select_boarding_target, warbot_control_mode, + RemoteWarbotControl, VehicleEntry, }; pub use building_door::{has_proximity_doors, sample_capture_doors, sample_proximity_doors}; pub use camouflage::toggle_camouflage; diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index 4cc35fcc..944ab574 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -110,6 +110,12 @@ enum FirePhase { } impl PlayerWeapons { + pub(super) fn adopt_cycle_from(&mut self, other: &Self) { + let selection = self.selection; + self.clone_from(other); + self.selection = selection; + } + /// Read the same slot, cycle and budget that drive manual firing. pub fn indicators(&self) -> impl Iterator<Item = WeaponIndicator> + '_ { self.weapons.iter().enumerate().map(|(slot, weapon)| { diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index cd90c924..67e1dec3 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -393,6 +393,13 @@ impl AutoDemoCombat { ) } + pub(super) fn weapons_mut(&mut self, handle: ObjectHandle) -> Option<&mut PlayerWeapons> { + self.units + .iter_mut() + .find(|unit| unit.handle == handle) + .map(|unit| &mut unit.weapons) + } + pub(super) fn turret_aim(&self, handle: ObjectHandle, component: usize) -> Option<TurretAim> { self.units .iter() diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index a22543b6..45c45622 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -583,9 +583,7 @@ impl UnitPatrol { for unit in &mut state.units { if self.is_leaving_factory(unit.handle) || mission.boarding.is_some_and(|control| { - control.remote.is_some() - && control.controls_chassis() - && control.cockpit.handle == unit.handle + control.controls_chassis() && control.cockpit.handle == unit.handle }) { continue; @@ -881,6 +879,13 @@ impl UnitPatrol { 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, @@ -1131,21 +1136,36 @@ fn choose_route( } impl PreparedPatrol { - pub(super) fn speed_of(&self, handle: ObjectHandle, can_fly: bool) -> f32 { + fn displacement_of(&self, handle: ObjectHandle) -> [f32; 3] { self.poses .iter() .find(|(moved, _, _)| *moved == handle) - .map_or(0.0, |(_, before, after)| { + .map_or([0.0; 3], |(_, before, after)| { let from = before.position.map(f32::from_bits); - let mut to = after.position.map(f32::from_bits); - if !can_fly { - // Ground support moves the root vertically without driving the chassis. - to[2] = from[2]; - } - crate::cockpit_speedometer::translation_speed(from, to, self.seconds) + 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 { |
