aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/boarding.rs92
-rw-r--r--crates/fparkan-runtime/src/lib.rs35
2 files changed, 97 insertions, 30 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs
index 9a1479f8..e2c4eeb0 100644
--- a/crates/fparkan-runtime/src/boarding.rs
+++ b/crates/fparkan-runtime/src/boarding.rs
@@ -1,7 +1,8 @@
use super::{
- component_cockpit, control_device_provider_value, projectile::unit_sphere, starting_cockpit,
- Engine, EngineError, MissionControlDeviceTableState, MissionControlDeviceTableStatus,
- StartingCockpit, SurfaceQuery, TransformState,
+ component_cockpit, control_device_provider_value,
+ projectile::{projectile_terrain_hit, projectile_unit_hit, unit_sphere},
+ starting_cockpit, Engine, EngineError, MissionControlDeviceTableState,
+ MissionControlDeviceTableStatus, StartingCockpit, SurfaceQuery, TransformState,
};
use fparkan_world::{
handle_by_original_id, identity_metadata, set_transform, transform_state, ObjectHandle,
@@ -22,12 +23,51 @@ pub fn player_vehicle(engine: &Engine) -> Option<StartingCockpit> {
Some(engine.loaded.as_ref()?.boarding.as_ref()?.cockpit)
}
-/// Boards the closest compatible allied warbot.
+/// Selects the friendly warbot under a line of sight supplied by the camera.
+pub fn select_boarding_target(
+ engine: &Engine,
+ hero: StartingCockpit,
+ segment: [[f32; 3]; 2],
+) -> Option<ObjectHandle> {
+ let target = projectile_unit_hit(engine, hero.handle, segment)?;
+ let owner = identity_metadata(&engine.world, hero.handle)
+ .ok()?
+ .owner_id?;
+ let mission = engine.loaded.as_ref()?;
+ // shortcut: use the cockpit sight ray until the original selection ranking is recovered.
+ if projectile_terrain_hit(&mission.terrain, segment)
+ .ok()?
+ .is_some_and(|ground| ground <= target.fraction)
+ {
+ 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)
+ });
+ (warbot
+ && identity_metadata(&engine.world, target.handle)
+ .ok()?
+ .owner_id
+ == Some(owner))
+ .then_some(target.handle)
+}
+
+/// Boards the selected compatible allied warbot within 20 units in XY.
///
/// # Errors
///
/// Returns an error if the hero's world handle is stale.
-pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, EngineError> {
+pub fn board_friendly_warbot(
+ engine: &mut Engine,
+ target: Option<ObjectHandle>,
+) -> Result<Option<StartingCockpit>, EngineError> {
+ let Some(target) = target else {
+ return Ok(None);
+ };
let Some(hero) = starting_cockpit(engine) else {
return Ok(None);
};
@@ -41,7 +81,7 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
return Ok(None);
};
let hero_pose = transform_state(&engine.world, hero.handle)?;
- let position = hero_pose.position.map(f32::from_bits);
+ let [hero_x, hero_y, _] = hero_pose.position.map(f32::from_bits);
let candidate = mission
.object_drafts
.iter()
@@ -51,7 +91,9 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
return None;
}
let handle = handle_by_original_id(&engine.world, draft.original_id?)?;
- if identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) {
+ if handle != target
+ || identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner)
+ {
return None;
}
let table = mission
@@ -62,26 +104,14 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
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 {
+ let [x, y, _] = state.position.map(f32::from_bits);
+ if !within_boarding_range([hero_x, hero_y], [x, y]) {
return None;
}
- Some((
- distance,
- component_cockpit(mission, index, handle)?,
- state.position,
- ))
+ Some((component_cockpit(mission, index, handle)?, state.position))
})
- .min_by(|a, b| a.0.total_cmp(&b.0));
- let Some((_, cockpit, position)) = candidate else {
+ .next();
+ let Some((cockpit, position)) = candidate else {
return Ok(None);
};
let can_fly = crate::player_motion::warbot_can_fly(engine, cockpit.object_index);
@@ -104,6 +134,20 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
Ok(Some(cockpit))
}
+fn within_boarding_range([hero_x, hero_y]: [f32; 2], [x, y]: [f32; 2]) -> bool {
+ (x - hero_x).hypot(y - hero_y) < 20.0
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn boarding_reach_excludes_the_horizontal_boundary() {
+ assert!(super::within_boarding_range([0.0, 0.0], [12.0, 15.99]));
+ assert!(!super::within_boarding_range([0.0, 0.0], [12.0, 16.0]));
+ assert!(!super::within_boarding_range([0.0, 0.0], [f32::NAN, 0.0]));
+ }
+}
+
fn has_working_pilot_control(table: &MissionControlDeviceTableState) -> bool {
let MissionControlDeviceTableStatus::Available(devices) = &table.status else {
return false;
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 81f6e899..a2d24893 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -71,7 +71,9 @@ mod unit_patrol;
pub use autodemo::{
autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView, SpawnedUnit,
};
-pub use boarding::{board_friendly_warbot, disembark_warbot, player_vehicle};
+pub use boarding::{
+ board_friendly_warbot, disembark_warbot, player_vehicle, select_boarding_target,
+};
pub use building_door::{has_proximity_doors, sample_proximity_doors};
pub use fparkan_assets::MissionAssets;
pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, BuildingMeshNode};
@@ -18775,7 +18777,10 @@ mod tests {
panic!("helicopter Control nodes unavailable");
};
nodes[key].current_type1_value_bits = 0.0_f32.to_bits();
- assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None);
+ assert_eq!(
+ board_friendly_warbot(&mut engine, Some(helicopter)).unwrap(),
+ None
+ );
let table = engine
.loaded
.as_mut()
@@ -18790,7 +18795,19 @@ mod tests {
panic!("helicopter Control nodes unavailable");
};
nodes[key].current_type1_value_bits = 1.0_f32.to_bits();
- let vehicle = board_friendly_warbot(&mut engine).unwrap().unwrap();
+ assert_eq!(board_friendly_warbot(&mut engine, None).unwrap(), None);
+ let [hx, hy, hz] = position(&engine, hero);
+ let [tx, ty, tz] = transform_state(&engine.world, helicopter)
+ .unwrap()
+ .position
+ .map(f32::from_bits);
+ assert_eq!(
+ select_boarding_target(&engine, hero, [[hx, hy, hz + 1.0], [tx, ty, tz + 0.5]]),
+ Some(helicopter),
+ );
+ let vehicle = board_friendly_warbot(&mut engine, Some(helicopter))
+ .unwrap()
+ .unwrap();
assert_eq!(vehicle.handle, helicopter);
let [x, y, z] = position(&engine, vehicle);
let step = plan_player_motion(&engine, vehicle, up).unwrap().unwrap();
@@ -19055,11 +19072,14 @@ mod tests {
loaded_mission_script_outcome(&engine),
Some(MissionScriptOutcome::Completed)
);
- assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None);
+ assert_eq!(
+ board_friendly_warbot(&mut engine, Some(robot.handle)).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)
+ let vehicle = board_friendly_warbot(&mut engine, Some(robot.handle))
.unwrap()
.expect("vehicle cockpit");
assert_eq!(vehicle.handle, robot.handle);
@@ -19079,7 +19099,10 @@ mod tests {
})
.collect::<Vec<_>>();
assert_eq!(shots, [(1, "bf_b_01".into())]);
- assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None);
+ assert_eq!(
+ board_friendly_warbot(&mut engine, Some(robot.handle)).unwrap(),
+ None
+ );
let motion = plan_player_motion(
&engine,
vehicle,