aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/designer_sockets.rs
blob: 44ccf7c8c9c6c25f2f124e8ec4619d477c43acc2 (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
//! Named model sockets exposed by the original constructor.

use crate::{designer_categories::DesignerCategory, MissionAssets};
use fparkan_prototype::{unit_component_tree, UnitComponentRecord};

/// One external mounting point, identified by its parent record and MSH node.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DesignerSocket {
    /// Category of part the socket accepts.
    pub category: DesignerCategory,
    /// Parent construction record.
    pub parent: usize,
    /// Socket node in the parent's model.
    pub node: usize,
    /// Original socket label from MSH Res10.
    pub family: String,
    /// Installed Unit DAT record, if occupied.
    pub part: Option<usize>,
}

/// Finds the chassis's turret mounts and every installed turret's weapon mounts.
pub fn designer_sockets(
    assets: &MissionAssets,
    object: usize,
    records: &[UnitComponentRecord],
) -> Vec<DesignerSocket> {
    let Ok(tree) = unit_component_tree(records) else {
        return Vec::new();
    };
    let mut sockets = Vec::new();
    for (parent, visual) in assets.visuals_for_object(object).iter().enumerate() {
        let Some(record) = records.get(parent).filter(|record| record.kind != 0) else {
            continue;
        };
        let category = match DesignerCategory::from_record(record) {
            Some(DesignerCategory::Chassis) => DesignerCategory::Turret,
            Some(DesignerCategory::Turret) => DesignerCategory::Weapon,
            _ => continue,
        };
        let Some(names) = assets
            .visual_by_id(*visual)
            .and_then(|visual| assets.model_by_id(visual.model_id?))
            .and_then(|model| model.validated.node_names.as_ref())
        else {
            continue;
        };
        for (node, family) in names.iter().enumerate() {
            let Some(family) = family.as_ref() else {
                continue;
            };
            let canonical = family.to_ascii_lowercase();
            let usable = match category {
                DesignerCategory::Turret => canonical.starts_with("e_tur_"),
                _ => canonical.starts_with("universal_") || canonical.starts_with("central_"),
            };
            if !usable {
                continue;
            }
            let part = tree[parent].children.iter().copied().find(|&index| {
                records[index].kind != 0
                    && records[index].parent_or_link == node as i32
                    && DesignerCategory::from_record(&records[index]) == Some(category)
            });
            sockets.push(DesignerSocket {
                category,
                parent,
                node,
                family: canonical,
                part,
            });
        }
    }
    sockets
}