use fparkan_assets::PreparedComponentControl; use fparkan_msh::{selected_slot, Group, Lod, ModelAsset, NodeId}; use fparkan_render::LegacyIron3dEulerTransform; pub(super) const DOOR_BLEND_PER_SECOND: f32 = 2.0; /// Whether this controller has a door record with a supported node binding. pub fn has_proximity_doors(control: &PreparedComponentControl) -> bool { door_bindings(control).next().is_some() } fn door_bindings( control: &PreparedComponentControl, ) -> impl Iterator { control .typed_records .iter() .filter(|record| record.tag == 12) .filter_map(|record| { let index = record.fixed_words[1] as usize; control .bindings .iter() .rev() .find(|binding| binding.node_index == index) .filter(|binding| binding.flags == 0) }) } pub(super) fn is_bound_door(control: &PreparedComponentControl, key: u32) -> bool { door_bindings(control).any(|binding| binding.node_index == key as usize) } /// Mixes the active hallway door request into the capture target's pose. pub fn sample_capture_doors( engine: &crate::Engine, handle: crate::ObjectHandle, control: &PreparedComponentControl, blends: &mut Vec, times: &mut [Option], ) { let Some(patrol) = engine .loaded .as_ref() .and_then(|mission| mission.unit_patrol.as_ref()) else { return; }; for (node, requested) in patrol.hallway_doors(handle) { let Some(binding) = door_bindings(control).find(|binding| binding.node_index == node) else { continue; }; blends.resize(control.model_node_count, 0.0); if let (Some(blend), Some(time)) = (blends.get_mut(node), times.get_mut(node)) { *blend = blend.max(requested); *time = Some(binding.frame_a + (binding.frame_b - binding.frame_a) * *blend); } } } /// Updates the authored door tracks next to the player. pub fn sample_proximity_doors( control: &PreparedComponentControl, model: &ModelAsset, root: LegacyIron3dEulerTransform, scale: [f32; 3], player: [f32; 3], delta_seconds: f32, blends: &mut Vec, times: &mut [Option], ) { // shortcut: use the closed model's door bounds and a local proximity trigger // until the original building graph trigger and its timing are recovered. let Some(closed) = fparkan_msh::node38_sampled_hierarchy(model, 0) else { return; }; let previous_len = blends.len(); blends.resize(model.node_count, 0.0); for binding in door_bindings(control) { let index = binding.node_index; let node = NodeId(index as u32); let Some(pose) = closed.poses.get(index) else { continue; }; let Some(slot) = selected_slot(model, node, Lod(0), Group(0)) else { continue; }; let slot = &model.slots[slot.0 as usize]; let center = std::array::from_fn(|axis| (slot.aabb_min[axis] + slot.aabb_max[axis]) * 0.5); let Some(center) = root.try_transform_scaled_point(pose.transform_point(center), scale) else { continue; }; let distance_squared: f32 = player .into_iter() .zip(center) .map(|(p, c)| (p - c).powi(2)) .sum(); let blend = &mut blends[index]; if index >= previous_len { *blend = binding.initial_blend.clamp(0.0, 1.0); } let direction = if distance_squared < 8.0_f32.powi(2) { 1.0 } else { -1.0 }; *blend = (*blend + direction * delta_seconds * DOOR_BLEND_PER_SECOND).clamp(0.0, 1.0); if let Some(time) = times.get_mut(index) { *time = Some(binding.frame_a + (binding.frame_b - binding.frame_a) * *blend); } } }