aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-msh/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-msh/src')
-rw-r--r--crates/fparkan-msh/src/hallway.rs130
-rw-r--r--crates/fparkan-msh/src/lib.rs51
2 files changed, 143 insertions, 38 deletions
diff --git a/crates/fparkan-msh/src/hallway.rs b/crates/fparkan-msh/src/hallway.rs
new file mode 100644
index 00000000..d0d646d8
--- /dev/null
+++ b/crates/fparkan-msh/src/hallway.rs
@@ -0,0 +1,130 @@
+use crate::{read_f32, read_u32, MshError, NodeId, RawStream};
+
+/// Model-local navigation points and links from MSH Res17.
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct Hallway {
+ /// Points in native selection order.
+ pub points: Vec<HallwayPoint>,
+ /// Start and end point indexes for each authored link.
+ pub links: Vec<[u32; 2]>,
+}
+
+/// A navigation point attached to a mesh node.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct HallwayPoint {
+ /// Position relative to the attached mesh node.
+ pub position: [f32; 3],
+ /// Raw point flags.
+ pub flags: u32,
+ /// Mesh node used for the world-space projection.
+ pub node: NodeId,
+}
+
+pub(super) fn parse(stream: &RawStream) -> Result<Hallway, MshError> {
+ const POINT_BYTES: usize = 20;
+ const LINK_BYTES: usize = 40;
+ let points = stream.attributes.attr1 as usize;
+ let links = stream.attributes.attr2 as usize;
+ let size = points as u64 * POINT_BYTES as u64 + links as u64 * LINK_BYTES as u64;
+ if size != stream.bytes.len() as u64 {
+ return Err(MshError::InvalidGeometry(
+ "invalid Res17 point table length".into(),
+ ));
+ }
+ let (point_bytes, link_bytes) = stream.bytes.split_at(points * POINT_BYTES);
+ let points = point_bytes
+ .chunks_exact(POINT_BYTES)
+ .map(|point| {
+ Ok(HallwayPoint {
+ position: [
+ read_f32(point, 0)?,
+ read_f32(point, 4)?,
+ read_f32(point, 8)?,
+ ],
+ flags: read_u32(point, 12)?,
+ node: NodeId(read_u32(point, 16)?),
+ })
+ })
+ .collect::<Result<_, MshError>>()?;
+ let links = link_bytes
+ .chunks_exact(LINK_BYTES)
+ .map(|link| Ok([read_u32(link, 0)?, read_u32(link, 4)?]))
+ .collect::<Result<_, MshError>>()?;
+ Ok(Hallway { points, links })
+}
+
+impl Hallway {
+ pub(super) fn hero_exits(
+ &self,
+ node_count: usize,
+ ) -> Result<Vec<(NodeId, [f32; 3])>, MshError> {
+ self.points
+ .iter()
+ .filter(|point| point.flags & 0x1000_4000 == 0x4000)
+ .map(|point| {
+ if point.node.0 as usize >= node_count
+ || !point.position.iter().all(|axis| axis.is_finite())
+ {
+ return Err(MshError::InvalidGeometry(
+ "invalid Res17 hero exit point".into(),
+ ));
+ }
+ Ok((point.node, point.position))
+ })
+ .collect()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::EntryAttributes;
+
+ #[test]
+ fn res17_retains_capture_and_exit_points_in_link_order() {
+ let bytes = [
+ 1.0_f32.to_bits(),
+ 0,
+ 0,
+ 0x40,
+ 4, // Capture point.
+ 0,
+ 0,
+ 0,
+ 0x4000,
+ 2, // Hero exit.
+ 1,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0, // One 40-byte link.
+ ]
+ .into_iter()
+ .flat_map(u32::to_le_bytes)
+ .collect();
+ let hallway = parse(&RawStream {
+ attributes: EntryAttributes {
+ attr1: 2,
+ attr2: 1,
+ attr3: 0,
+ },
+ bytes,
+ })
+ .unwrap();
+ assert_eq!(
+ hallway.points[0],
+ HallwayPoint {
+ position: [1.0, 0.0, 0.0],
+ flags: 0x40,
+ node: NodeId(4),
+ }
+ );
+ assert_eq!(hallway.links, [[1, 0]]);
+ assert_eq!(hallway.hero_exits(5).unwrap(), [(NodeId(2), [0.0; 3])]);
+ }
+}
diff --git a/crates/fparkan-msh/src/lib.rs b/crates/fparkan-msh/src/lib.rs
index 334a6353..7a388e3d 100644
--- a/crates/fparkan-msh/src/lib.rs
+++ b/crates/fparkan-msh/src/lib.rs
@@ -8,6 +8,9 @@ use fparkan_animation::{
};
use fparkan_nres::{EntryMeta, NresDocument, NresError};
+mod hallway;
+pub use hallway::{Hallway, HallwayPoint};
+
/// Node table stream.
pub const STREAM_NODE_TABLE: u32 = 1;
/// Slot stream.
@@ -119,6 +122,8 @@ pub struct ModelAsset {
pub node_names: Option<Vec<Option<String>>>,
/// Node-local points whose flags mark a hero exit.
pub hero_exit_points: Vec<(NodeId, [f32; 3])>,
+ /// Points and links used by the building's indoor routes.
+ pub hallway: Hallway,
/// Optional decoded node-animation streams.
pub animation: Option<ModelAnimation>,
}
@@ -1139,10 +1144,11 @@ fn parse_model_document(document: &NresDocument) -> Result<ModelAsset, MshError>
let node_names = read_optional_stream(document, STREAM_NAMES)?
.map(|raw| parse_res10_names(&raw.bytes, node_count))
.transpose()?;
- let hero_exit_points = read_optional_stream(document, STREAM_GAMEPLAY_POINTS)?
- .map(|raw| parse_hero_exit_points(&raw, node_count))
+ let hallway = read_optional_stream(document, STREAM_GAMEPLAY_POINTS)?
+ .map(|raw| hallway::parse(&raw))
.transpose()?
.unwrap_or_default();
+ let hero_exit_points = hallway.hero_exits(node_count)?;
let animation = parse_optional_animation(document)?;
Ok(ModelAsset {
@@ -1162,46 +1168,11 @@ fn parse_model_document(document: &NresDocument) -> Result<ModelAsset, MshError>
triangle_descriptors,
node_names,
hero_exit_points,
+ hallway,
animation,
})
}
-fn parse_hero_exit_points(
- stream: &RawStream,
- node_count: usize,
-) -> Result<Vec<(NodeId, [f32; 3])>, MshError> {
- const POINT_BYTES: usize = 20;
- let point_count = stream.attributes.attr1 as usize;
- let pairs = stream.attributes.attr2 as usize;
- if !stream.bytes.len().is_multiple_of(POINT_BYTES)
- || (point_count as u64 + 2 * pairs as u64) != (stream.bytes.len() / POINT_BYTES) as u64
- {
- return Err(MshError::InvalidGeometry(
- "invalid Res17 point table length".into(),
- ));
- }
- let mut exits = Vec::new();
- for point in stream.bytes.chunks_exact(POINT_BYTES).take(point_count) {
- let flags = read_u32(point, 12)?;
- if flags & 0x1000_4000 != 0x4000 {
- continue;
- }
- let node = NodeId(read_u32(point, 16)?);
- let position = [
- read_f32(point, 0)?,
- read_f32(point, 4)?,
- read_f32(point, 8)?,
- ];
- if node.0 as usize >= node_count || !position.iter().all(|axis| axis.is_finite()) {
- return Err(MshError::InvalidGeometry(
- "invalid Res17 hero exit point".into(),
- ));
- }
- exits.push((node, position));
- }
- Ok(exits)
-}
-
fn parse_optional_animation(document: &NresDocument) -> Result<Option<ModelAnimation>, MshError> {
let keys = read_optional_stream(document, STREAM_ANIMATION_KEYS)?;
let frame_map = read_optional_stream(document, STREAM_ANIMATION_FRAME_MAP)?;
@@ -2346,6 +2317,7 @@ mod tests {
triangle_descriptors: None,
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: Some(ModelAnimation {
keys: vec![
AnimKey24 {
@@ -2410,6 +2382,7 @@ mod tests {
triangle_descriptors: None,
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: Some(ModelAnimation {
keys: vec![
AnimKey24 {
@@ -2477,6 +2450,7 @@ mod tests {
triangle_descriptors: None,
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: Some(ModelAnimation {
keys: vec![
AnimKey24 {
@@ -3371,6 +3345,7 @@ mod tests {
batches,
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: None,
triangle_descriptors: Some(
tri_flags