wownav_parser/
mmtile.rs

1use bytemuck::AnyBitPattern;
2use core::ffi::{c_float, c_int, c_uint};
3use log::error;
4
5/// Hi
6
7const MMAP_MAGIC: u32 = 0x4d4d4150; // 'MMAP'
8const MMAP_VERSION: u32 = 15;
9#[derive(Debug, AnyBitPattern, Copy, Clone)]
10#[repr(C)]
11pub struct DtMeshHeader {
12    magic: c_int,
13    /// Tile magic number. (Used to identify the data format.)
14    version: c_int,
15    /// Tile data format version number.
16    pub x: c_int,
17    /// The x-position of the tile within the dtNavMesh tile grid. (x, y, layer)
18    pub y: c_int,
19    /// The y-position of the tile within the dtNavMesh tile grid. (x, y, layer)
20    pub layer: c_int,
21    /// The layer of the tile within the dtNavMesh tile grid. (x, y, layer)
22    pub user_id: c_uint,
23    /// The user defined id of the tile.
24    pub poly_count: c_int,
25    /// The number of polygons in the tile.
26    pub vert_count: c_int,
27    /// The number of vertices in the tile.
28    pub max_link_count: c_int,
29    /// The number of allocated links.
30    pub detail_mesh_count: c_int,
31    /// The number of sub-meshes in the detail mesh.
32
33    /// The number of unique vertices in the detail mesh. (In addition to the polygon vertices.)
34    pub detail_vert_count: c_int,
35
36    pub detail_tri_count: c_int,
37    /// The number of triangles in the detail mesh.
38    pub bv_node_count: c_int,
39    /// The number of bounding volume nodes. (Zero if bounding volumes are disabled.)
40    pub off_mesh_con_count: c_int,
41    /// The number of off-mesh connections.
42    pub off_mesh_base: c_int,
43    /// The index of the first polygon which is an off-mesh connection.
44    pub walkable_height: c_float,
45    /// The height of the agents using the tile.
46    pub walkable_radius: c_float,
47    /// The radius of the agents using the tile.
48    pub walkable_climb: c_float,
49    /// The maximum climb height of the agents using the tile.
50    pub bmin: [c_float; 3],
51    /// The minimum bounds of the tile's AABB. [(x, y, z)]
52    pub bmax: [c_float; 3],
53    /// The maximum bounds of the tile's AABB. [(x, y, z)]
54
55    /// The bounding volume quantization factor.
56    pub bv_quant_factor: c_float,
57}
58
59#[derive(Debug, AnyBitPattern, Copy, Clone)]
60#[repr(C)]
61pub struct MmapTileHeader {
62    mmap_magic: u32,
63    dt_version: u32,
64    mmap_version: u32,
65    size: u32,
66    uses_liquids: u32,
67}
68
69impl MmapTileHeader {
70    pub fn from_bytes(buffer: &[u8]) -> Result<Self, ()> {
71        let header = *bytemuck::from_bytes::<MmapTileHeader>(&buffer);
72
73        if header.mmap_magic != MMAP_MAGIC {
74            return Err(error!(
75                "Invalid MMAP_MAGIC: {}, current: {}",
76                header.mmap_magic, MMAP_MAGIC
77            ));
78        }
79
80        if header.mmap_version != MMAP_VERSION {
81            return Err(error!(
82                "Invalid MMAP_VERSION: {}, current: {}",
83                header.mmap_version, MMAP_VERSION
84            ));
85        }
86
87        /* TODO: check for header.size */
88
89        Ok(header)
90    }
91}
92
93impl DtMeshHeader {
94    pub fn from_bytes(buffer: &[u8]) -> Result<Self, ()> {
95        Ok(*bytemuck::from_bytes::<DtMeshHeader>(&buffer))
96    }
97}