aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/building_door.rs
blob: 9ac892067c271583244203bf00696d32f0b5ece1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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<Item = &fparkan_assets::PreparedControlNodeBinding> {
    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<f32>,
    times: &mut [Option<f32>],
) {
    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<f32>,
    times: &mut [Option<f32>],
) {
    // 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);
        }
    }
}