dayzmake-core 0.3.1

Readers and writers for DayZ file formats: PBO, rapified config, P3D, PAA, BI keys and signatures, and navmesh generation
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Ported to Rust and altered for DayZ (see the dayzmake navmesh spec); this is not the original software.

//! `rcCompactHeightfield`, `rcBuildCompactHeightfield` (with the DayZ `subAreas`), `rcMarkBoxArea`
//! and `rcErodeWalkableArea` (spec 5.4 steps 5 and 6, 10.1 item 1), plus the chamfer distance
//! transform they share with the DayZ dilation.

use std::io;

use super::heightfield::Heightfield;
use crate::navmesh::num::{i32_to_f32, trunc_i32};

/// `RC_NULL_AREA`.
const NULL_AREA: u8 = 0;

/// `RC_NOT_CONNECTED`: the 6-bit "no neighbour" value of a direction.
pub const NOT_CONNECTED: u32 = 0x3f;

/// The largest span index of a column start (24 bits, `rcCompactCell::index`).
const MAX_SPAN_INDEX: usize = 0x00ff_ffff;

/// `rcGetDirOffsetX`.
#[must_use]
pub fn dir_offset_x(direction: i32) -> i32 {
    match direction & 3 {
        0 => -1,
        2 => 1,
        _ => 0,
    }
}

/// `rcGetDirOffsetY` (the second grid coordinate, z).
#[must_use]
pub fn dir_offset_y(direction: i32) -> i32 {
    match direction & 3 {
        1 => 1,
        3 => -1,
        _ => 0,
    }
}

/// `rcCompactSpan`: `con` packs six bits per direction, `h` is the free height above `y`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CompactSpan {
    /// Lower extent of the span.
    pub y: u16,
    /// Region id (0 = none).
    pub reg: u16,
    /// Packed neighbour connections (24 bits).
    pub con: u32,
    /// Height of the span above `y`.
    pub h: u8,
}

impl CompactSpan {
    /// `rcGetCon`: the neighbour layer of `direction`, or [`NOT_CONNECTED`].
    #[must_use]
    pub fn get_con(&self, direction: i32) -> u32 {
        let shift = u32::try_from(direction & 3).unwrap_or(0) * 6;
        (self.con >> shift) & 0x3f
    }

    /// `rcSetCon`.
    pub fn set_con(&mut self, direction: i32, neighbor_index: u32) {
        let shift = u32::try_from(direction & 3).unwrap_or(0) * 6;
        self.con = (self.con & !(0x3f << shift)) | ((neighbor_index & 0x3f) << shift);
    }
}

/// `rcCompactHeightfield` with the DayZ `subAreas`. Fields past `areas` are filled by later stages
/// (distance field, regions, contours).
#[derive(Clone, Debug, Default)]
pub struct CompactHeightfield {
    /// Width of the field (x).
    pub width: i32,
    /// Height of the field (z).
    pub height: i32,
    /// Border size in cells.
    pub border_size: i32,
    /// Agent height in voxels.
    pub walkable_height: i32,
    /// Agent climb in voxels.
    pub walkable_climb: i32,
    /// Minimum bounds.
    pub bmin: [f32; 3],
    /// Maximum bounds.
    pub bmax: [f32; 3],
    /// Cell size.
    pub cs: f32,
    /// Cell height.
    pub ch: f32,
    /// Per column `(index of the first span, span count)`, `x + z * width` order.
    pub cells: Vec<(u32, u8)>,
    /// The spans of all columns.
    pub spans: Vec<CompactSpan>,
    /// Area id per span.
    pub areas: Vec<u8>,
    /// DayZ part id per span.
    pub sub_areas: Vec<u16>,
    /// Distance field per span (filled by the region builder).
    pub dist: Vec<u16>,
    /// Maximum of `dist`.
    pub max_distance: u16,
    /// Number of regions.
    pub max_regions: u16,
}

impl CompactHeightfield {
    /// The span index range of column `(x, z)`; empty when out of range.
    #[must_use]
    pub fn column_range(&self, x: i32, z: i32) -> std::ops::Range<usize> {
        if x < 0 || z < 0 || x >= self.width || z >= self.height {
            return 0..0;
        }
        let Some(i) = usize::try_from(x)
            .ok()
            .zip(
                usize::try_from(z)
                    .ok()
                    .zip(usize::try_from(self.width).ok()),
            )
            .and_then(|(x, (z, w))| z.checked_mul(w)?.checked_add(x))
        else {
            return 0..0;
        };
        let Some(&(index, count)) = self.cells.get(i) else {
            return 0..0;
        };
        let start = usize::try_from(index).unwrap_or(0);
        start..start.saturating_add(usize::from(count))
    }

    /// The neighbour of span `i` in column `(x, z)` through `direction`: `(nx, nz, index)`, or
    /// `None` when not connected.
    #[must_use]
    pub fn neighbor(&self, x: i32, z: i32, i: usize, direction: i32) -> Option<(i32, i32, usize)> {
        let con = self.spans.get(i)?.get_con(direction);
        if con == NOT_CONNECTED {
            return None;
        }
        let nx = x + dir_offset_x(direction);
        let nz = z + dir_offset_y(direction);
        let start = self.column_range(nx, nz).start;
        Some((nx, nz, start.checked_add(usize::try_from(con).ok()?)?))
    }
}

/// `rcGetHeightFieldSpanCount`: the number of walkable spans.
fn span_count(hf: &Heightfield) -> usize {
    hf.columns()
        .iter()
        .flatten()
        .filter(|s| s.area != NULL_AREA)
        .count()
}

/// `rcBuildCompactHeightfield`. The compact field takes `walkable_height * ch` on top of `bmax.y`
/// (the pipeline passes `proneHeight`), copies each solid span's `subArea` and packs the neighbour
/// connections, six bits per direction; a neighbour layer above 62 stays `RC_NOT_CONNECTED`.
///
/// # Errors
/// Returns an error when the walkable spans do not fit the 24-bit span index, or a column has more
/// than 255 walkable spans.
pub fn build_compact(
    walkable_height: i32,
    walkable_climb: i32,
    hf: &Heightfield,
) -> io::Result<CompactHeightfield> {
    const MAX_HEIGHT: i32 = 0xffff;
    const MAX_LAYERS: i32 = 0x3f - 1;
    let x_size = hf.width;
    let z_size = hf.height;
    let count = span_count(hf);
    if count > MAX_SPAN_INDEX {
        return Err(error!(
            "compact heightfield has too many spans ({count}, max {MAX_SPAN_INDEX})"
        ));
    }
    let mut bmax = hf.bmax;
    bmax[1] += i32_to_f32(walkable_height) * hf.ch;
    let mut chf = CompactHeightfield {
        width: x_size,
        height: z_size,
        border_size: 0,
        walkable_height,
        walkable_climb,
        bmin: hf.bmin,
        bmax,
        cs: hf.cs,
        ch: hf.ch,
        cells: vec![(0, 0); hf.columns().len()],
        spans: Vec::with_capacity(count),
        areas: Vec::with_capacity(count),
        sub_areas: Vec::with_capacity(count),
        dist: Vec::new(),
        max_distance: 0,
        max_regions: 0,
    };

    // Fill in cells and spans.
    for (column, cell) in hf.columns().iter().zip(chf.cells.iter_mut()) {
        // A column without spans keeps index 0, count 0.
        if column.is_empty() {
            continue;
        }
        let first = chf.spans.len();
        cell.0 = u32::try_from(first).map_err(|_| error!("compact span index overflow"))?;
        for (k, span) in column.iter().enumerate() {
            if span.area == NULL_AREA {
                continue;
            }
            let bot = i32::from(span.smax);
            let top = column.get(k + 1).map_or(MAX_HEIGHT, |n| i32::from(n.smin));
            chf.spans.push(CompactSpan {
                y: u16::try_from(bot.clamp(0, 0xffff)).unwrap_or(0),
                reg: 0,
                con: 0,
                h: u8::try_from((top - bot).clamp(0, 0xff)).unwrap_or(0),
            });
            chf.areas.push(span.area);
            chf.sub_areas.push(span.sub_area);
        }
        cell.1 = u8::try_from(chf.spans.len() - first)
            .map_err(|_| error!("a column has more than 255 walkable spans"))?;
    }

    // Find the neighbour connections.
    for z in 0..z_size {
        for x in 0..x_size {
            for i in chf.column_range(x, z) {
                let Some(&span) = chf.spans.get(i) else {
                    continue;
                };
                let mut new = span;
                for dir in 0..4 {
                    new.set_con(dir, NOT_CONNECTED);
                    let nx = x + dir_offset_x(dir);
                    let nz = z + dir_offset_y(dir);
                    // First check that the neighbour cell is in bounds.
                    if nx < 0 || nz < 0 || nx >= x_size || nz >= z_size {
                        continue;
                    }
                    // Iterate over all neighbour spans and check if any of them is accessible.
                    let range = chf.column_range(nx, nz);
                    let start = range.start;
                    for k in range {
                        let Some(n) = chf.spans.get(k) else {
                            continue;
                        };
                        let bot = i32::from(span.y).max(i32::from(n.y));
                        let top = (i32::from(span.y) + i32::from(span.h))
                            .min(i32::from(n.y) + i32::from(n.h));
                        // The gap must be walkable and the climb not too high.
                        if (top - bot) >= walkable_height
                            && (i32::from(n.y) - i32::from(span.y)).abs() <= walkable_climb
                        {
                            let layer = i32::try_from(k - start).unwrap_or(i32::MAX);
                            if layer > MAX_LAYERS {
                                continue;
                            }
                            new.set_con(dir, u32::try_from(layer).unwrap_or(NOT_CONNECTED));
                            break;
                        }
                    }
                }
                if let Some(s) = chf.spans.get_mut(i) {
                    *s = new;
                }
            }
        }
    }
    Ok(chf)
}

/// `rcMarkBoxArea`: every walkable span whose `y` lies within the box (world units, truncated to
/// voxels like upstream) gets `area`. A span's part id is untouched.
pub fn mark_box_area(chf: &mut CompactHeightfield, bmin: [f32; 3], bmax: [f32; 3], area: u8) {
    let x_lo = trunc_i32((bmin[0] - chf.bmin[0]) / chf.cs);
    let y_lo = trunc_i32((bmin[1] - chf.bmin[1]) / chf.ch);
    let z_lo = trunc_i32((bmin[2] - chf.bmin[2]) / chf.cs);
    let x_hi = trunc_i32((bmax[0] - chf.bmin[0]) / chf.cs);
    let y_hi = trunc_i32((bmax[1] - chf.bmin[1]) / chf.ch);
    let z_hi = trunc_i32((bmax[2] - chf.bmin[2]) / chf.cs);

    if x_hi < 0 || x_lo >= chf.width || z_hi < 0 || z_lo >= chf.height {
        return;
    }
    let x_lo = x_lo.max(0);
    let z_lo = z_lo.max(0);
    let x_hi = x_hi.min(chf.width - 1);
    let z_hi = z_hi.min(chf.height - 1);
    for z in z_lo..=z_hi {
        for x in x_lo..=x_hi {
            for i in chf.column_range(x, z) {
                let y = chf.spans.get(i).map_or(i32::MIN, |s| i32::from(s.y));
                if y >= y_lo
                    && y <= y_hi
                    && let Some(a) = chf.areas.get_mut(i)
                    && *a != NULL_AREA
                {
                    *a = area;
                }
            }
        }
    }
}

/// The two chamfer passes of `rcErodeWalkableArea` over the 4-connected spans, with a `label`
/// copied along from the neighbour whenever a span's distance strictly improves (DayZ: the area
/// and part id of the nearest seed travel with the distance). `dist` is a byte per span.
pub(crate) fn chamfer(chf: &CompactHeightfield, dist: &mut [u8], label: &mut [(u8, u16)]) {
    let w = chf.width;
    let h = chf.height;

    // Improve span `i` from span `m` with the step cost `k`.
    let relax = |dist: &mut [u8], label: &mut [(u8, u16)], i: usize, m: usize, k: i32| {
        let Some(&dm) = dist.get(m) else {
            return;
        };
        let nd = (i32::from(dm) + k).min(255);
        if dist.get(i).is_some_and(|&di| nd < i32::from(di))
            && let (Some(d), Some(&l)) = (dist.get_mut(i), label.get(m))
        {
            *d = u8::try_from(nd).unwrap_or(255);
            if let Some(slot) = label.get_mut(i) {
                *slot = l;
            }
        }
    };

    // Pass 1
    for y in 0..h {
        for x in 0..w {
            for i in chf.column_range(x, y) {
                // (-1,0) then (-1,-1) and (0,-1) then (1,-1)
                for (d1, d2) in [(0, 3), (3, 2)] {
                    if let Some((ax, ay, ai)) = chf.neighbor(x, y, i, d1) {
                        relax(dist, label, i, ai, 2);
                        if let Some((_, _, aai)) = chf.neighbor(ax, ay, ai, d2) {
                            relax(dist, label, i, aai, 3);
                        }
                    }
                }
            }
        }
    }

    // Pass 2
    for y in (0..h).rev() {
        for x in (0..w).rev() {
            for i in chf.column_range(x, y) {
                for (d1, d2) in [(2, 1), (1, 0)] {
                    if let Some((ax, ay, ai)) = chf.neighbor(x, y, i, d1) {
                        relax(dist, label, i, ai, 2);
                        if let Some((_, _, aai)) = chf.neighbor(ax, ay, ai, d2) {
                            relax(dist, label, i, aai, 3);
                        }
                    }
                }
            }
        }
    }
}

/// `rcErodeWalkableArea`: spans with a chamfer distance to the nearest unwalkable span or missing
/// neighbour below `2 * radius` become `RC_NULL_AREA`.
///
/// # Errors
/// Returns an error when the field is inconsistent (arrays shorter than the span list).
pub fn erode(chf: &mut CompactHeightfield, radius: i32) -> io::Result<()> {
    let n = chf.spans.len();
    if chf.areas.len() != n {
        return Err(error!("compact heightfield areas do not match its spans"));
    }
    let mut dist = vec![0xffu8; n];

    // Mark boundary cells.
    for y in 0..chf.height {
        for x in 0..chf.width {
            for i in chf.column_range(x, y) {
                let area = chf.areas.get(i).copied().unwrap_or(NULL_AREA);
                let boundary = if area == NULL_AREA {
                    true
                } else {
                    let mut nc = 0;
                    for dir in 0..4 {
                        if let Some((_, _, ni)) = chf.neighbor(x, y, i, dir)
                            && chf.areas.get(ni).is_some_and(|&a| a != NULL_AREA)
                        {
                            nc += 1;
                        }
                    }
                    // At least one missing neighbour.
                    nc != 4
                };
                if boundary && let Some(d) = dist.get_mut(i) {
                    *d = 0;
                }
            }
        }
    }

    // The labels are not needed here (erosion only looks at distances).
    let mut label = vec![(0u8, 0u16); n];
    chamfer(chf, &mut dist, &mut label);

    // `(unsigned char)(radius*2)`
    let thr = u8::try_from((radius.wrapping_mul(2)) & 0xff).unwrap_or(0);
    for (a, &d) in chf.areas.iter_mut().zip(&dist) {
        if d < thr {
            *a = NULL_AREA;
        }
    }
    Ok(())
}