diff options
Diffstat (limited to 'crates/fparkan-assets/src/designer_sockets.rs')
| -rw-r--r-- | crates/fparkan-assets/src/designer_sockets.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/designer_sockets.rs b/crates/fparkan-assets/src/designer_sockets.rs new file mode 100644 index 00000000..44ccf7c8 --- /dev/null +++ b/crates/fparkan-assets/src/designer_sockets.rs @@ -0,0 +1,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 +} |
