From e116199498486545a188582a44942c02786aab07 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 10:48:48 +0400 Subject: feat(runtime): capture neutral buildings at communicators --- crates/fparkan-runtime/src/lib.rs | 41 +++++++-- crates/fparkan-runtime/src/mission_events.rs | 127 +++++++++++++++++++++++++++ crates/fparkan-runtime/src/player_motion.rs | 44 ++++++++++ 3 files changed, 207 insertions(+), 5 deletions(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 8f396629..94e61948 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -56,7 +56,8 @@ pub use fparkan_assets::MissionAssets; pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, BuildingMeshNode}; use mission_events::MissionEvents; pub use mission_events::{ - capture_neutral_warbot, MissionEventStop, MissionEventTick, MissionNotice, + capture_neutral_building, capture_neutral_warbot, MissionEventStop, MissionEventTick, + MissionNotice, }; pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep}; pub use player_weapon::{starting_player_weapons, PlayerWeapons}; @@ -17630,8 +17631,8 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] - fn licensed_constructor_checks_current_factory_and_hangar_owners() { - use fparkan_world::{identity_metadata, set_owner, OwnerId}; + fn licensed_constructor_captures_factory_and_hangar_at_communicators() { + use fparkan_world::{identity_metadata, OwnerId}; let vfs: Arc = Arc::new(DirectoryVfs::new(licensed_root("IS"))); let mut engine = create(EngineServices::new(vfs)).unwrap(); @@ -17653,7 +17654,16 @@ mod tests { .notices .iter() .any(|notice| matches!(notice, MissionNotice::ObjectiveComplete(0 | 2)))); - for (time, logic_id, objective_index) in [(2001, 0x8000_0001, 0), (4001, 0x8000_0003, 2)] { + let cockpit = starting_cockpit(&engine).unwrap(); + assert_eq!( + capture_neutral_building(&mut engine, cockpit).unwrap(), + None + ); + let mut player_pose = transform_state(&engine.world, cockpit.handle).unwrap(); + for (time, logic_id, objective_index, plate) in [ + (2001, 0x8000_0001, 0, [391.3_f32, 740.1, 145.12]), + (4001, 0x8000_0003, 2, [1268.1, 790.6, 160.77]), + ] { let slot = engine .loaded .as_ref() @@ -17668,7 +17678,28 @@ mod tests { identity_metadata(&engine.world, handle).unwrap().owner_id, Some(OwnerId(2)) ); - set_owner(&mut engine.world, handle, Some(OwnerId(0))).unwrap(); + for z in [plate[2] + 12.0, plate[2] - 6.0] { + player_pose.position = [plate[0], plate[1], z].map(f32::to_bits); + set_transform(&mut engine.world, cockpit.handle, player_pose).unwrap(); + assert_eq!( + capture_neutral_building(&mut engine, cockpit).unwrap(), + None + ); + } + player_pose.position = plate.map(f32::to_bits); + set_transform(&mut engine.world, cockpit.handle, player_pose).unwrap(); + assert_eq!( + capture_neutral_building(&mut engine, cockpit).unwrap(), + Some(OriginalObjectId(slot as u32)) + ); + assert_eq!( + capture_neutral_building(&mut engine, cockpit).unwrap(), + None + ); + assert_eq!( + identity_metadata(&engine.world, handle).unwrap().owner_id, + Some(OwnerId(0)) + ); let event = frame(&mut engine, time).unwrap().mission_event.unwrap(); assert!(event.stop.is_none(), "{:?}", event.stop); assert!( diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 35d9e7b7..cc78bf6a 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -2,6 +2,8 @@ use super::{ apply_recovered_script_host_callback, Engine, EngineError, LoadedMissionState, MissionObjectDraft, MissionScriptHostEffect, StartingCockpit, LAST_SUPER_AI_TABLE_INDEX, }; +use fparkan_msh::{draw_batches, node38_sampled_hierarchy, selected_slot, Group, Lod, NodeId}; +use fparkan_render::LegacyIron3dEulerTransform; use fparkan_script::{ ScriptDispatchSelector, ScriptInstruction, VarSetDefault, VmHostCallbackCommand, }; @@ -249,6 +251,131 @@ pub fn capture_neutral_warbot( Ok(None) } +/// Transfers the neutral building whose communicator the player is standing on. +/// +/// # Errors +/// +/// Returns an error if the player's world handle or the ownership update fails. +pub fn capture_neutral_building( + engine: &mut Engine, + cockpit: StartingCockpit, +) -> Result, EngineError> { + let Some(mission) = &engine.loaded else { + return Ok(None); + }; + let Some(player_owner) = identity_metadata(&engine.world, cockpit.handle)?.owner_id else { + return Ok(None); + }; + if !mission + .mission + .clans + .get(usize::from(player_owner.0)) + .is_some_and(|clan| clan.mode == 1) + { + return Ok(None); + } + let position = transform_state(&engine.world, cockpit.handle)? + .position + .map(f32::from_bits); + let candidate = mission + .mission_events + .placed_clan_objects + .iter() + .filter_map(|&(handle, _)| { + registration_sequence(&engine.world, handle).ok()??; + let identity = identity_metadata(&engine.world, handle).ok()?; + let clan = mission + .mission + .clans + .get(usize::from(identity.owner_id?.0))?; + (clan.mode == 3).then_some((handle, identity.original_id?)) + }) + .find(|&(handle, id)| { + transform_state(&engine.world, handle).is_ok_and(|pose| { + on_communicator(&mission.mission_assets, id.0 as usize, pose, position) + }) + }); + if let Some((handle, id)) = candidate { + set_owner(&mut engine.world, handle, Some(player_owner))?; + Ok(Some(id)) + } else { + Ok(None) + } +} + +fn on_communicator( + assets: &super::MissionAssets, + object_index: usize, + pose: super::TransformState, + [x, y, z]: [f32; 3], +) -> bool { + let Some(visual) = assets + .visual_for_object(object_index) + .and_then(|id| assets.visual_by_id(id)) + else { + return false; + }; + if visual.source_type != Some(u32::from_le_bytes(*b"FORT")) { + return false; + } + let Some(wear) = assets + .wears + .iter() + .find(|wear| Some(wear.id) == visual.wear_id) + else { + return false; + }; + let Some(model) = visual + .model_id + .and_then(|id| assets.model_by_id(id)) + .map(|model| &model.validated) + else { + return false; + }; + let Some(hierarchy) = node38_sampled_hierarchy(model, 0) else { + return false; + }; + let transform = LegacyIron3dEulerTransform { + translation: pose.position.map(f32::from_bits), + orientation_radians: pose.orientation.map(f32::from_bits), + }; + let scale = pose.scale.map(f32::from_bits); + // shortcut: use the green platform mesh with clearance for the player's + // feet until the original communicator volume is measured. + let platform_height = hierarchy + .poses + .iter() + .enumerate() + .filter_map(|(index, pose)| { + let slot = selected_slot(model, NodeId(index as u32), Lod(0), Group(0))?; + Some((*pose, draw_batches(model, slot).ok()?)) + }) + .flat_map(|(pose, batches)| batches.iter().map(move |batch| (pose, batch))) + .filter(|(_, batch)| { + wear.table + .entries + .get(usize::from(batch.material_index)) + .is_some_and(|entry| entry.material.0.eq_ignore_ascii_case(b"B_COMP_4G")) + }) + .flat_map(|(pose, batch)| { + let first = batch.index_start as usize; + let end = first + usize::from(batch.index_count); + model.indices[first..end] + .chunks_exact(3) + .filter_map(move |indices| { + let [a, b, c] = [indices[0], indices[1], indices[2]].map(|index| { + let local = + model.positions[batch.base_vertex as usize + usize::from(index)]; + transform.try_transform_scaled_point(pose.transform_point(local), scale) + }); + let (a, b, c) = (a?, b?, c?); + in_tactical_polygon(&[a, b, c], x, y).then_some(a[2].max(b[2]).max(c[2])) + }) + }) + .max_by(f32::total_cmp); + platform_height.is_some_and(|height| (z - height).abs() <= 2.0) +} + impl LoadedMissionState { pub(super) fn tick_mission_event( &mut self, diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 7f88d41a..6d9e53d8 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -23,6 +23,50 @@ pub struct PlayerMotionStep { pub sweep: Option<[[f32; 3]; 2]>, } +impl PlayerMotionStep { + /// Vertical segment for finding a nearby building floor at the proposed XY. + #[must_use] + pub fn floor_probe(self) -> Option<[[f32; 3]; 2]> { + let [[_, _, start_z], [x, y, _]] = self.sweep?; + // shortcut: fixed step and drop limits, until the hero collider is wired. + Some([[x, y, start_z + 0.1], [x, y, start_z - 2.5]]) + } + + /// Places the player's feet just above a floor accepted by the scene query. + #[must_use] + pub fn with_building_floor(mut self, height: f32) -> Self { + if let Some(sweep) = &mut self.sweep { + self.pose.position[2] = (height + 0.02).to_bits(); + sweep[1][2] = height + 0.52; + } + self + } +} + +#[cfg(test)] +mod tests { + use super::{PlayerMotionStep, TransformState}; + + #[test] + fn building_floor_probe_uses_proposed_xy_and_previous_height() { + let step = PlayerMotionStep { + pose: TransformState { + position: [4.0_f32, 6.0, 15.0].map(f32::to_bits), + ..TransformState::default() + }, + pitch_bits: 0, + sweep: Some([[3.0, 6.0, 12.5], [4.0, 6.0, 15.5]]), + }; + assert_eq!( + step.floor_probe(), + Some([[4.0, 6.0, 12.6], [4.0, 6.0, 10.0]]) + ); + let lowered = step.with_building_floor(11.5); + assert_eq!(lowered.pose.position.map(f32::from_bits), [4.0, 6.0, 11.52]); + assert_eq!(lowered.sweep.unwrap()[1], [4.0, 6.0, 12.02]); + } +} + /// Proposes a hero step from the world pose and the active terrain. /// /// # Errors -- cgit v1.2.3