aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 07:50:33 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 07:50:33 +0300
commite50b53e7a08d8b5e3393e477b890da95c4c8ef2a (patch)
tree1506b2d9685b562b4b22966c5e16bc574f69fbbc /crates/fparkan-assets/src
parent20491de7c29f56fda9be16d3b56a9633cb71d386 (diff)
downloadfparkan-e50b53e7a08d8b5e3393e477b890da95c4c8ef2a.tar.xz
fparkan-e50b53e7a08d8b5e3393e477b890da95c4c8ef2a.zip
feat(ui): install turrets and weapons in designer sockets
Read the chassis and turret mount families from MSH Res10 and the attached devices from CTLD. Preserve the parent and mount node in Unit DAT, including separate ammunition subtrees for matching weapons. Verified from the stock Field Camp start: install a turret, mount two identical guns, remove one with its ammunition, and order the edited project through the factory panel.
Diffstat (limited to 'crates/fparkan-assets/src')
-rw-r--r--crates/fparkan-assets/src/designer_screen.rs100
-rw-r--r--crates/fparkan-assets/src/designer_sockets.rs74
-rw-r--r--crates/fparkan-assets/src/lib.rs1
3 files changed, 157 insertions, 18 deletions
diff --git a/crates/fparkan-assets/src/designer_screen.rs b/crates/fparkan-assets/src/designer_screen.rs
index bdbdeda2..2aa09e32 100644
--- a/crates/fparkan-assets/src/designer_screen.rs
+++ b/crates/fparkan-assets/src/designer_screen.rs
@@ -14,6 +14,7 @@ use fparkan_prototype::UnitComponentRecord;
use fparkan_vfs::Vfs;
pub use crate::designer_categories::{DesignerCategory, DesignerPartsPane};
+pub use crate::designer_sockets::{designer_sockets, DesignerSocket};
const SEND: [u32; 4] = [194, 437, 229, 480];
const EXIT: [u32; 4] = [405, 437, 446, 480];
@@ -38,6 +39,8 @@ pub enum DesignerAction {
Category(DesignerCategory),
/// Choose the installed mount to inspect.
Part(usize),
+ /// Choose a named mount, including a socket without an installed part.
+ Socket(usize),
/// Install a researched variant at the inspected mount.
Variant(usize),
/// Move a list by one row.
@@ -116,7 +119,9 @@ pub struct DesignerPicture<'a> {
/// Installed part category displayed in the model pane.
pub category: DesignerCategory,
/// Selected record and the available equipment for it.
- pub variants: Option<(usize, &'a [String])>,
+ pub variants: Option<(DesignerAction, &'a [String])>,
+ /// External model mounts, including empty ones.
+ pub sockets: &'a [DesignerSocket],
/// Action whose original tooltip is shown.
pub hover: Option<DesignerAction>,
/// Active project file control.
@@ -308,7 +313,7 @@ impl DesignerScreen {
for (index, category) in DesignerCategory::ALL.into_iter().enumerate() {
let enabled = match pane {
Parts => category == view.category && !variants.is_empty(),
- Model => category.mounts(records).next().is_some(),
+ Model => model_rows(category, records, view.sockets).next().is_some(),
};
let selected = enabled && category == view.category;
let (frame, icon, _) = category.tab();
@@ -333,27 +338,29 @@ impl DesignerScreen {
);
}
}
- let mounts = view.category.mounts(records).collect::<Vec<_>>();
- for (row, index) in mounts
+ let mounts = model_rows(view.category, records, view.sockets).collect::<Vec<_>>();
+ for (row, &mount) in mounts
.iter()
.skip(view.first_part)
.take(PART_ROWS)
.enumerate()
{
- let record = &records[*index];
- let key = field(&record.resource_raw);
- let label = self.part_label(key, field(&record.description_raw));
+ let label = mounted_record(mount, view.sockets)
+ .and_then(|index| records.get(index))
+ .map_or(b"".as_slice(), |record| {
+ self.part_label(field(&record.resource_raw), field(&record.description_raw))
+ });
self.paint_row(
image,
label,
Model,
row,
- view.variants.is_some_and(|(mount, _)| mount == *index),
+ view.variants.is_some_and(|(selected, _)| selected == mount),
)?;
}
let installed = view
.variants
- .and_then(|(mount, _)| records.get(mount))
+ .and_then(|(mount, _)| records.get(mounted_record(mount, view.sockets)?))
.map(|part| field(&part.resource_raw));
for (row, name) in variants
.iter()
@@ -515,15 +522,16 @@ pub fn equipment_hit(
records: &[UnitComponentRecord],
first_variant: usize,
variants: usize,
+ sockets: &[DesignerSocket],
) -> Option<DesignerAction> {
use DesignerPartsPane::{Model, Parts};
- if let Some(category) = category_hit(cursor, records) {
+ if let Some(category) = category_hit(cursor, records, sockets) {
return Some(DesignerAction::Category(category));
}
let pane = DesignerPartsPane::at(cursor)?;
let count = match pane {
Parts => variants,
- Model => category.mounts(records).count(),
+ Model => model_rows(category, records, sockets).count(),
};
if count > PART_ROWS {
for forward in [false, true] {
@@ -537,15 +545,16 @@ pub fn equipment_hit(
Parts => {
(first_variant + row < variants).then_some(DesignerAction::Variant(first_variant + row))
}
- Model => category
- .mounts(records)
- .nth(first_part + row)
- .map(DesignerAction::Part),
+ Model => model_rows(category, records, sockets).nth(first_part + row),
}
}
/// Finds an enabled category tab in the model panel.
-pub fn category_hit(cursor: [f32; 2], records: &[UnitComponentRecord]) -> Option<DesignerCategory> {
+pub fn category_hit(
+ cursor: [f32; 2],
+ records: &[UnitComponentRecord],
+ sockets: &[DesignerSocket],
+) -> Option<DesignerCategory> {
DesignerCategory::ALL.into_iter().find(|&category| {
let (left, _, _) = category.tab();
let left = left + DesignerPartsPane::Model.left();
@@ -555,10 +564,36 @@ pub fn category_hit(cursor: [f32; 2], records: &[UnitComponentRecord]) -> Option
DesignerCategory::Chassis | DesignerCategory::Armour
));
contains([left, 16, left + hit_width, 53], cursor)
- && category.mounts(records).next().is_some()
+ && model_rows(category, records, sockets).next().is_some()
})
}
+/// Rows of one equipment category in native mount order.
+pub fn model_rows<'a>(
+ category: DesignerCategory,
+ records: &'a [UnitComponentRecord],
+ sockets: &'a [DesignerSocket],
+) -> impl Iterator<Item = DesignerAction> + 'a {
+ sockets
+ .iter()
+ .enumerate()
+ .filter_map(move |(index, socket)| {
+ (socket.category == category).then_some(DesignerAction::Socket(index))
+ })
+ .chain(category.mounts(records).filter_map(|part| {
+ (!sockets.iter().any(|socket| socket.part == Some(part)))
+ .then_some(DesignerAction::Part(part))
+ }))
+}
+
+fn mounted_record(mount: DesignerAction, sockets: &[DesignerSocket]) -> Option<usize> {
+ match mount {
+ DesignerAction::Part(part) => Some(part),
+ DesignerAction::Socket(socket) => sockets.get(socket)?.part,
+ _ => None,
+ }
+}
+
fn recent_slot(index: usize) -> [u32; 4] {
let left = 200 + 30 * index as u32;
[left, 270, left + 24, 294]
@@ -606,7 +641,8 @@ mod tests {
immediate_child_count: 0,
};
let records = [0, 3, 4, 3, 4, 3, 3, 3, 3, 3].map(make_part);
- let action = |cursor| equipment_hit(cursor, DesignerCategory::Systems, 1, &records, 2, 8);
+ let action =
+ |cursor| equipment_hit(cursor, DesignerCategory::Systems, 1, &records, 2, 8, &[]);
assert_eq!(
action([491.0, 24.0]),
Some(DesignerAction::Category(DesignerCategory::Chassis))
@@ -627,4 +663,32 @@ mod tests {
[2, 4]
);
}
+
+ #[test]
+ fn an_empty_socket_enables_its_category_and_has_its_own_row() {
+ let sockets = [
+ DesignerSocket {
+ category: DesignerCategory::Weapon,
+ parent: 1,
+ node: 6,
+ family: "universal_lr".into(),
+ part: None,
+ },
+ DesignerSocket {
+ category: DesignerCategory::Weapon,
+ parent: 1,
+ node: 7,
+ family: "universal_lr".into(),
+ part: None,
+ },
+ ];
+ let action =
+ |cursor| equipment_hit(cursor, DesignerCategory::Weapon, 0, &[], 0, 0, &sockets);
+ assert_eq!(
+ action([526.0, 28.0]),
+ Some(DesignerAction::Category(DesignerCategory::Weapon))
+ );
+ assert_eq!(action([521.0, 73.0]), Some(DesignerAction::Socket(0)));
+ assert_eq!(action([521.0, 96.0]), Some(DesignerAction::Socket(1)));
+ }
}
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
+}
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index fe99b109..6900953c 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -36,6 +36,7 @@ mod control_drive;
mod control_weapon_sound;
mod designer_categories;
pub mod designer_screen;
+mod designer_sockets;
mod explosion_profile;
pub mod factory_projects;
pub mod game_font;