diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 10:16:24 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 10:16:24 +0300 |
| commit | 3f14308af536f4ad04c004f02f5ed799c5ae443c (patch) | |
| tree | b196d8814ee9b23e69cc96811d0538d8748b8a75 /crates | |
| parent | e8e55549d6bd984a01c09eb49882bf3f70090633 (diff) | |
| download | fparkan-3f14308af536f4ad04c004f02f5ed799c5ae443c.tar.xz fparkan-3f14308af536f4ad04c004f02f5ed799c5ae443c.zip | |
feat(runtime): board and drive allied warbots
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 160 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 45 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile.rs | 3 |
3 files changed, 207 insertions, 1 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs new file mode 100644 index 00000000..50fa5919 --- /dev/null +++ b/crates/fparkan-runtime/src/boarding.rs @@ -0,0 +1,160 @@ +use super::{ + component_cockpit, projectile::unit_sphere, starting_cockpit, Engine, EngineError, + StartingCockpit, SurfaceQuery, TransformState, +}; +use fparkan_world::{ + handle_by_original_id, identity_metadata, set_transform, transform_state, ObjectHandle, +}; + +#[derive(Clone, Copy)] +pub(super) struct Boarding { + pub(super) cockpit: StartingCockpit, + pub(super) hero: StartingCockpit, +} + +/// The vehicle cockpit while the player is aboard. +#[must_use] +pub fn player_vehicle(engine: &Engine) -> Option<StartingCockpit> { + Some(engine.loaded.as_ref()?.boarding.as_ref()?.cockpit) +} + +/// Boards the closest compatible allied warbot. +/// +/// # Errors +/// +/// Returns an error if the hero's world handle is stale. +pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, EngineError> { + let Some(hero) = starting_cockpit(engine) else { + return Ok(None); + }; + let Some(mission) = &engine.loaded else { + return Ok(None); + }; + if mission.boarding.is_some() { + return Ok(None); + } + let Some(owner) = identity_metadata(&engine.world, hero.handle)?.owner_id else { + return Ok(None); + }; + let hero_pose = transform_state(&engine.world, hero.handle)?; + let position = hero_pose.position.map(f32::from_bits); + let candidate = mission + .object_drafts + .iter() + .enumerate() + .filter_map(|(index, draft)| { + if draft.unit_dat_header_flags != Some(0x0100_8000) { + return None; + } + let handle = handle_by_original_id(&engine.world, draft.original_id?)?; + if identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) { + return None; + } + let state = transform_state(&engine.world, handle).ok()?; + let (center, radius) = unit_sphere(&mission.mission_assets, index, state)?; + let distance = center + .into_iter() + .zip(position) + .map(|(center, player)| (center - player).powi(2)) + .sum::<f32>() + .sqrt() + - radius; + // shortcut: same-clan proximity replaces native selection and boarding admission. + if !distance.is_finite() || distance > 6.0 { + return None; + } + Some(( + distance, + component_cockpit(mission, index, handle)?, + state.position, + )) + }) + .min_by(|a, b| a.0.total_cmp(&b.0)); + let Some((_, cockpit, position)) = candidate else { + return Ok(None); + }; + set_transform( + &mut engine.world, + hero.handle, + TransformState { + position, + ..hero_pose + }, + )?; + if let Some(mission) = &mut engine.loaded { + mission.boarding = Some(Boarding { cockpit, hero }); + mission.player_pitch_bits = 0; + } + Ok(Some(cockpit)) +} + +/// Leaves the vehicle and restores the hero's cockpit. +/// +/// # Errors +/// +/// Returns an error when the hero pose or terrain query cannot be read. +pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, EngineError> { + let Some(mission) = &mut engine.loaded else { + return Ok(None); + }; + let Some(Boarding { + cockpit: vehicle, + hero, + }) = mission.boarding + else { + return Ok(None); + }; + let mut pose = transform_state(&engine.world, hero.handle)?; + if let Ok(vehicle_pose) = transform_state(&engine.world, vehicle.handle) { + 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(); + let radius = unit_sphere(&mission.mission_assets, vehicle.object_index, vehicle_pose) + .map_or(1.0, |(_, radius)| radius); + // shortcut: use the sides and terrain surface until the exit + // point and collision checks are recovered. + let mut exit = None; + for side in [1.0, -1.0] { + let xy = [ + x + cos * (radius + 1.0) * side, + y + sin * (radius + 1.0) * side, + ]; + if let Some(ground) = mission + .terrain + .height_at(xy) + .map_err(EngineError::Terrain)? + { + exit = Some([xy[0], xy[1], ground + 0.02]); + break; + } + } + let Some(exit) = exit else { + return Ok(None); + }; + pose.position = exit.map(f32::to_bits); + pose.orientation[2] = yaw.to_bits(); + set_transform(&mut engine.world, hero.handle, pose)?; + } + mission.boarding = None; + mission.player_pitch_bits = 0; + Ok(Some(hero)) +} + +pub(super) fn move_passenger( + engine: &mut Engine, + cockpit: StartingCockpit, + vehicle_pose: TransformState, +) -> Option<(ObjectHandle, TransformState)> { + let boarding = engine.loaded.as_ref()?.boarding.as_ref()?; + if boarding.cockpit.handle != cockpit.handle { + return None; + } + let hero = boarding.hero; + let previous = transform_state(&engine.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)) +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index af069b33..c71c5619 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -43,6 +43,7 @@ use std::sync::Arc; mod autodemo; mod autodemo_combat; +mod boarding; mod mesh_collision; mod mission_events; mod player_motion; @@ -55,6 +56,7 @@ pub use autodemo::{ autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView, SpawnedUnit, }; pub use autodemo_combat::{AutoDemoCombatEvent, AutoDemoProjectileType}; +pub use boarding::{board_friendly_warbot, disembark_warbot, player_vehicle}; pub use fparkan_assets::MissionAssets; pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, BuildingMeshNode}; use mission_events::MissionEvents; @@ -1334,6 +1336,7 @@ struct LoadedMissionState { mission_events: MissionEvents, unit_patrol: Option<unit_patrol::UnitPatrol>, player_pitch_bits: u32, + boarding: Option<boarding::Boarding>, script_host_mode: MissionMode, script_host_status: u32, script_outcome: MissionScriptOutcome, @@ -2434,6 +2437,7 @@ fn load_mission_with_options( mission_events, unit_patrol, player_pitch_bits: 0, + boarding: None, script_host_mode: request.mode, // Fresh IGame construction seeds this field with 5; this is not the // later status-4 transition performed after native game initialization. @@ -2592,6 +2596,7 @@ pub fn frame_with_player_motion( pose.position = previous.position; } set_transform(&mut engine.world, cockpit.handle, pose)?; + let passenger = boarding::move_passenger(engine, cockpit, pose); match frame(engine, queue_time_ms) { Ok(mut frame) => { if let Some(mission) = &mut engine.loaded { @@ -2602,6 +2607,9 @@ pub fn frame_with_player_motion( } Err(error) => { set_transform(&mut engine.world, cockpit.handle, previous)?; + if let Some((handle, previous)) = passenger { + set_transform(&mut engine.world, handle, previous)?; + } Err(error) } } @@ -17820,6 +17828,43 @@ mod tests { loaded_mission_script_outcome(&engine), Some(MissionScriptOutcome::Completed) ); + assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None); + let robot_pose = transform_state(&engine.world, robot.handle).unwrap(); + player_pose.position = robot_pose.position; + set_transform(&mut engine.world, cockpit.handle, player_pose).unwrap(); + let vehicle = board_friendly_warbot(&mut engine) + .unwrap() + .expect("vehicle cockpit"); + assert_eq!(vehicle.handle, robot.handle); + assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None); + 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, 66_101, vehicle, motion, false).unwrap(); + let driven = transform_state(&engine.world, vehicle.handle).unwrap(); + assert_ne!(driven.position, robot_pose.position); + assert_eq!( + transform_state(&engine.world, cockpit.handle) + .unwrap() + .position, + driven.position + ); + assert_eq!(disembark_warbot(&mut engine).unwrap(), Some(cockpit)); + assert_eq!(player_vehicle(&engine), None); + assert_ne!( + transform_state(&engine.world, cockpit.handle) + .unwrap() + .position, + driven.position + ); } #[test] diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index d88eed50..af9e075e 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -178,6 +178,7 @@ pub fn projectile_unit_hit( segment: [[f32; 3]; 2], ) -> Option<ProjectileContact> { let mission = engine.loaded.as_ref()?; + let passenger = mission.boarding.map(|boarding| boarding.hero.handle); // shortcut: root MSH sphere until combat uses the per-node hull query. mission .object_drafts @@ -189,7 +190,7 @@ pub fn projectile_unit_hit( return None; } let handle = handle_by_original_id(&engine.world, draft.original_id?)?; - if handle == shooter { + if handle == shooter || passenger == Some(handle) { return None; } let state = transform_state(&engine.world, handle).ok()?; |
