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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Building one navmesh tile end to end (spec 4, 5, 7).
//!
//! [`tile_config`] derives the Recast configuration of a cell class, and [`build_tile`] runs every
//! step of spec 5.4 on one tile input: terrain, objects, span filters, compact heightfield, area
//! passes, regions, contours, polygon mesh, flags, the extra section and the Detour tile blob.
//!
//! The build raises no warnings: the counts it returns (stray part ids, obstacles) are for the
//! caller to report.

use std::io;

use super::dayz::{
    areas::{dilate_area, extract_tree_obstacles, remap_and_roof},
    filters::{filter_low_height_multi, mode_filter},
    slope::walkable,
};
use super::detour::{
    CreateParams, Obstacle, TileData, TileExtra, TilePart, TilePartPoly, create_tile_data,
    stored_off_mesh,
};
use super::links::{Links, generate_links};
use super::num::{ceil_i32, i32_to_f32, trunc_i32};
use super::recast::{
    compact::{CompactHeightfield, build_compact, erode, mark_box_area},
    contour::{RC_CONTOUR_TESS_WALL_EDGES, build_contours},
    filter::{filter_ledges, filter_low_hanging},
    heightfield::Heightfield,
    mesh::{PolyMesh, build_poly_mesh},
    rasterize::{RasterMode, RasterParams, rasterize_part, rasterize_triangle},
    region::{build_distance_field, build_regions},
};
use super::wire::{CellClass, TileGroup, TileInput, WorldParams};

/// What the door states of the tiles are (spec 5.1): which door parts start disabled.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum DoorPolicy {
    /// Open doors (area 12) are disabled, closed doors (area 13) are not.
    #[default]
    Closed,
    /// Closed doors (area 13) are disabled, open doors are not.
    Open,
    /// No door is disabled.
    Enabled,
}

/// Options of a tile build.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct BuildOptions {
    /// Build jump, climb and ladder links (spec 6).
    pub links: bool,
    /// The initial state of door parts.
    pub doors: DoorPolicy,
    /// Sea level in metres; `None` uses the world's `deepSeaHeight`.
    pub sea_level: Option<f32>,
}

impl Default for BuildOptions {
    fn default() -> Self {
        Self {
            links: true,
            doors: DoorPolicy::Closed,
            sea_level: None,
        }
    }
}

/// The Recast configuration derived from the world and a cell class (spec 4). Sizes in voxels
/// unless noted.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct TileConfig {
    /// Cells across the tile (`tileSize`).
    pub cells: i32,
    /// Cell size in metres.
    pub cs: f32,
    /// Cell height in metres (0.1).
    pub ch: f32,
    /// `walkableHeight`.
    pub walkable_height: i32,
    /// Crouch height.
    pub crouch_height: i32,
    /// Prone height.
    pub prone_height: i32,
    /// `walkableClimb`.
    pub walkable_climb: i32,
    /// `walkableRadius`.
    pub walkable_radius: i32,
    /// Deep water depth.
    pub deep_water: i32,
    /// Sea level.
    pub sea_level: i32,
    /// Border size in cells.
    pub border_size: i32,
    /// Heightfield width and height: `cells + 2 * border_size`.
    pub width: i32,
    /// Padding in metres: `float(border_size) * cs`.
    pub pad: f32,
    /// Contour simplification error in cells.
    pub max_simplification_error: f32,
    /// Longest contour edge in cells (120).
    pub max_edge_len: i32,
    /// Vertical simplification error in voxels (8).
    pub max_vertical_error: f32,
    /// Default minimum region area.
    pub min_region_area: i32,
    /// Region merge area (400).
    pub merge_region_area: i32,
    /// Vertices per polygon (6).
    pub nvp: usize,
    /// Door dilation radius.
    pub door_radius: i32,
    /// Slope threshold: `cos(maxSlope)`.
    pub slope_threshold: f32,
}

/// The configuration of tiles of `cell_class` (spec 4). All arithmetic is in `f32` as the spec
/// writes it. A degenerate cell class (no cells) gives non-finite sizes, which [`build_tile`]
/// rejects.
#[must_use]
pub fn tile_config(
    world: &WorldParams,
    cell_class: &CellClass,
    options: &BuildOptions,
) -> TileConfig {
    let b = &world.world;
    let cs = cell_class.cell_size();
    let walkable_radius = ceil_i32((b.diameter * 0.5_f32) / cs);
    let border_size = 10.max(walkable_radius.saturating_sub(trunc_i32(-3.0_f32 / cs)));
    let sea = options.sea_level.unwrap_or(b.deep_sea_height);
    let min_region = trunc_i32((1.0_f32 / cs) * (1.0_f32 / cs));
    TileConfig {
        cells: cell_class.cells,
        cs,
        ch: 0.1,
        walkable_height: ceil_i32(b.stand_height / 0.1_f32),
        crouch_height: ceil_i32(b.crouch_height / 0.1_f32).saturating_sub(2),
        prone_height: ceil_i32(b.prone_height / 0.1_f32).saturating_sub(2),
        walkable_climb: ceil_i32(b.max_step_height / 0.1_f32),
        walkable_radius,
        deep_water: ceil_i32(b.deep_water_depth / 0.1_f32),
        sea_level: ceil_i32(sea / 0.1_f32),
        border_size,
        width: cell_class
            .cells
            .saturating_add(border_size.saturating_mul(2)),
        pad: i32_to_f32(border_size) * cs,
        max_simplification_error: b.contour_max_error / cs,
        max_edge_len: 120,
        max_vertical_error: 8.0,
        min_region_area: min_region,
        merge_region_area: 400,
        nvp: 6,
        door_radius: trunc_i32(i32_to_f32(walkable_radius) * 0.8_f32),
        slope_threshold: ((b.max_slope / 180.0_f32) * std::f32::consts::PI).cos(),
    }
}

/// The result of building one tile.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
#[expect(
    clippy::large_enum_variant,
    reason = "the tile is returned by value; a box would only move it"
)]
pub enum TileOutcome {
    /// A tile blob.
    #[non_exhaustive]
    Built {
        /// The Detour tile.
        data: TileData,
        /// Polygons whose part id was beyond the extras list (spec 12.3); they were ignored.
        stray_part_ids: usize,
        /// Tree obstacles stored in the tile.
        obstacles: usize,
    },
    /// No walkable surface: no contours or no polygons.
    Empty,
}

/// One entry of the tile extras list (spec 5.1).
struct ExtraPart {
    object_id: u64,
    part_index: u32,
    area: u8,
    /// 0: the polygons of the part get the disabled flag.
    state: u8,
    polys: Vec<u32>,
}

/// The most spans one tile heightfield may hold (hostile input: stacked surfaces could otherwise
/// need gigabytes). Past it the tile fails. A `Span` is 10 bytes, so this bounds the span data of a
/// tile at 2^26 * 10 B = 640 MiB (before the slack of the column vectors); a field added to `Span`
/// raises it.
pub const MAX_SPANS: usize = 1 << 26;

const SEA_FLOOR: f32 = -500.0;
const NULL_AREA: u8 = 0;
/// The terrain area: groups with it are skipped in step 3 (group 0 was rasterised in step 1).
const TERRAIN_AREA: u32 = 1;
const LADDER_AREA: u32 = 15;
/// The hide part group (spec 5.4).
const HIDE_AREA: u32 = 14;

/// The low byte of an area id (the extras list stores the area "as a byte").
fn low_byte(area: u32) -> u8 {
    u8::try_from(area & 0xff).unwrap_or_default()
}

/// A part id as the 16-bit span field holds it: wrapping like C's `(unsigned short)counter++`
/// (spec 5.4, part ids), so 65536 parts wrap to 0 instead of saturating.
fn part_id(counter: usize) -> u16 {
    u16::try_from(counter & 0xffff).unwrap_or_default()
}

fn area_u8(area: u32) -> Option<u8> {
    u8::try_from(area).ok().filter(|a| *a < 64)
}

/// The extras list: all parts of all non-tree groups, and the part count of the last tree group.
fn extras_list(input: &TileInput, doors: DoorPolicy) -> (Vec<ExtraPart>, usize) {
    let mut extras = Vec::new();
    let mut tree_items = 0;
    for g in &input.groups {
        if g.area == 10 {
            tree_items = g.parts.len();
            continue;
        }
        let state = match doors {
            DoorPolicy::Closed if g.area == 12 => 0,
            DoorPolicy::Open if g.area == 13 => 0,
            _ => 1,
        };
        for p in &g.parts {
            extras.push(ExtraPart {
                object_id: p.object_id,
                part_index: p.part_index,
                area: low_byte(g.area),
                state,
                polys: Vec::new(),
            });
        }
    }
    (extras, tree_items)
}

/// Rasterises the triangles of `verts` one by one in rasteriser order `(v0, v2, v1)`. With
/// `slope` set, triangles that fail the slope test get area 0.
fn raster_triangles(
    hf: &mut Heightfield,
    verts: &[[f32; 3]],
    params: &RasterParams,
    slope: Option<f32>,
) {
    for [v0, v1, v2] in verts.as_chunks::<3>().0.iter().copied() {
        let mut p = *params;
        if let Some(threshold) = slope
            && !walkable(threshold, v0, v1, v2)
        {
            p.area = NULL_AREA;
        }
        rasterize_triangle(hf, v0, v2, v1, &p);
    }
}

/// The vertices of a part (server order); empty when the range is invalid.
fn part_verts(input: &TileInput, group: usize, part: usize) -> &[[f32; 3]] {
    input
        .part_vertex_range(group, part)
        .and_then(|range| input.groups.get(group)?.verts.get(range))
        .unwrap_or(&[])
}

/// The triangles of a part in rasteriser order, as one flat vertex list.
fn part_triangles(input: &TileInput, group: usize, part: usize) -> Vec<[f32; 3]> {
    let verts = part_verts(input, group, part);
    let mut out = Vec::with_capacity(verts.len());
    for [v0, v1, v2] in verts.as_chunks::<3>().0.iter().copied() {
        out.extend([v0, v2, v1]);
    }
    out
}

/// Spec 5.4 step 3: the objects of every group but the terrain. Returns the ladder groups, which
/// the link stage uses.
fn rasterize_objects<'a>(
    hf: &mut Heightfield,
    input: &'a TileInput,
    cfg: &TileConfig,
    obstacles: bool,
) -> Vec<&'a TileGroup> {
    let mut ladders = Vec::new();
    let mut part_counter = 0_usize;
    let slope = Some(cfg.slope_threshold);
    for (gi, g) in input.groups.iter().enumerate() {
        if g.area == TERRAIN_AREA {
            continue;
        }
        if g.area == LADDER_AREA {
            ladders.push(g);
            continue;
        }
        let Some(area) = area_u8(g.area) else {
            continue;
        };
        let mut params = RasterParams {
            area,
            walkable_climb: cfg.walkable_climb,
            walkable_height: cfg.walkable_height,
            ..RasterParams::default()
        };
        match g.area {
            6 | 7 | 8 | 9 | 11 => raster_triangles(hf, &g.verts, &params, slope),
            2 => {
                params.mode = RasterMode::Water;
                // Ruling R14: shallow water starts a step (walkableClimb) under the surface.
                params.water_depth = cfg.walkable_climb;
                raster_triangles(hf, &g.verts, &params, None);
                params.area = 3;
                params.water_depth = cfg.deep_water;
                raster_triangles(hf, &g.verts, &params, None);
            }
            10 => {
                if g.parts.is_empty() {
                    params.mode = RasterMode::Tree;
                    raster_triangles(hf, &g.verts, &params, None);
                    continue;
                }
                for k in 0..g.parts.len() {
                    if obstacles {
                        params.mode = RasterMode::Tree;
                        params.sub_area = part_id(k);
                    } else {
                        params.mode = RasterMode::Add;
                        params.area = NULL_AREA;
                        params.sub_area = 0;
                    }
                    raster_triangles(hf, part_verts(input, gi, k), &params, None);
                }
            }
            12..=14 => {
                params.mode = if g.area == 14 {
                    RasterMode::PaintOrAdd
                } else {
                    RasterMode::PaintBelow
                };
                if g.area == 14 {
                    params.flag_merge_threshold = 1;
                } else {
                    params.flag_merge_threshold = cfg.walkable_height;
                }
                if g.parts.is_empty() {
                    raster_triangles(hf, &g.verts, &params, None);
                    continue;
                }
                for k in 0..g.parts.len() {
                    params.sub_area = part_id(part_counter);
                    part_counter = part_counter.saturating_add(1);
                    rasterize_part(hf, &part_triangles(input, gi, k), &params);
                }
            }
            18 => raster_triangles(hf, &g.verts, &params, None),
            _ => {}
        }
    }
    ladders
}

/// The area table of spec 4: the default minimum region area, except for areas 10 and 12 to 14.
pub(crate) fn min_region_table(cfg: &TileConfig) -> [i32; 64] {
    let mut table = [cfg.min_region_area; 64];
    for id in [10, 12, 13, 14] {
        if let Some(e) = table.get_mut(id) {
            *e = i32::MAX;
        }
    }
    table
}

fn areas_flag(mask: &mut [bool; 64], ids: &[usize], value: bool) {
    for &id in ids {
        if let Some(m) = mask.get_mut(id) {
            *m = value;
        }
    }
}

/// Spec 5.4 step 6 (`postCompact`): sea box, dilations, tree obstacles, erosion, remap and roof.
fn post_compact(
    chf: &mut CompactHeightfield,
    cfg: &TileConfig,
    pbmin: [f32; 3],
    pbmax: [f32; 3],
    tree_items: usize,
    obstacles: bool,
) -> io::Result<Vec<Obstacle>> {
    // 6.1 The sea box.
    mark_box_area(
        chf,
        [pbmin[0], SEA_FLOOR, pbmin[2]],
        [pbmax[0], i32_to_f32(cfg.sea_level) * cfg.ch, pbmax[2]],
        5,
    );
    // 6.2 Dilations: every area but the door and hide areas takes part.
    let mut mask = [true; 64];
    areas_flag(&mut mask, &[12, 13, 14], false);
    dilate_area(chf, cfg.door_radius, 13, &mask);
    dilate_area(chf, cfg.door_radius, 12, &mask);
    dilate_area(chf, cfg.walkable_radius, 14, &mask);
    dilate_area(chf, cfg.walkable_radius, 16, &mask);
    // 6.3 Tree obstacles.
    let mut found = Vec::new();
    if obstacles && tree_items > 0 {
        let mut seeds = [false; 64];
        areas_flag(&mut seeds, &[0, 10, 12, 13, 14], true);
        found = extract_tree_obstacles(chf, 10, 1, tree_items, &seeds, 2.0, 3.0);
    }
    // 6.4 to 6.6.
    erode(chf, cfg.walkable_radius).map_err(|e| error!("could not erode: {e}"))?;
    remap_and_roof(chf);
    Ok(found)
}

/// The poly flags of an area (spec 7.1).
fn area_flags(area: u8) -> u16 {
    match area {
        1 | 6 | 7 | 9 | 14 => 0x01,
        2 | 4 => 0x11,
        3 => 0x10,
        5 => 0x20,
        8 => 0x09,
        12 | 13 => 0x05,
        16 => 0x0400,
        17 => 0x0800,
        _ => 0,
    }
}

/// Spec 7.2: attaches polygons to parts, sets or clears the disabled flag and counts the stray part
/// ids. Returns the stray count.
fn attach_parts(
    extras: &mut [ExtraPart],
    sub_areas: &[u16],
    areas: &[u8],
    flags: &mut [u16],
) -> usize {
    let mut stray = 0;
    if extras.is_empty() {
        return stray;
    }
    for (i, (&sub, &area)) in sub_areas.iter().zip(areas).enumerate() {
        let Some(part) = extras.get_mut(usize::from(sub)) else {
            stray += 1;
            continue;
        };
        if part.area != area {
            continue;
        }
        if let Some(f) = flags.get_mut(i) {
            if part.state == 0 {
                *f |= 0x2;
            } else {
                *f &= !0x2;
            }
        }
        part.polys.push(u32::try_from(i).unwrap_or(u32::MAX));
    }
    stray
}

/// Spec 7.2, ruling R17: the off-mesh polygons of the connections that belong to a hide part join the
/// part's polygon list. `stored[i]` tells whether connection `i` is in the tile (Detour keeps those
/// that start inside it, in order), and `ground` is the number of ground polygons that the off-mesh
/// polygons follow. The lists stay ascending: the ground polygons come first.
fn attach_off_mesh(extras: &mut [ExtraPart], links: &Links, stored: &[bool], ground: usize) {
    let mut poly = ground;
    for (part, &keep) in links.parts.iter().zip(stored) {
        if !keep {
            continue;
        }
        if let Some(part) = part.and_then(|p| extras.get_mut(usize::from(p))) {
            part.polys.push(u32::try_from(poly).unwrap_or(u32::MAX));
        }
        poly += 1;
    }
}

/// The extra section from the extras list (spec 7.2).
fn extra_section(extras: &[ExtraPart], obstacles: Vec<Obstacle>) -> io::Result<TileExtra> {
    let mut extra = TileExtra::default();
    for part in extras {
        let first = if part.polys.is_empty() {
            -1
        } else {
            i32::try_from(extra.part_polys.len()).map_err(|_| error!("too many part polygons"))?
        };
        extra.parts.push(TilePart {
            object_id: part.object_id,
            part_index: part.part_index,
            first_poly: first,
        });
        let base = extra.part_polys.len();
        for (k, poly) in part.polys.iter().enumerate() {
            let next = if k + 1 < part.polys.len() {
                i32::try_from(base + k + 1).map_err(|_| error!("too many part polygons"))?
            } else {
                -1
            };
            extra.part_polys.push(TilePartPoly { poly: *poly, next });
        }
    }
    extra.obstacles = obstacles;
    Ok(extra)
}

/// Builds the Detour tile of one tile input (spec 5).
///
/// # Errors
///
/// Returns an error for an unknown cell class ("unknown cell class N"; the caller names the tile), an invalid
/// grid, or a failure of one of the pipeline stages.
pub fn build_tile(
    world: &WorldParams,
    input: &TileInput,
    options: &BuildOptions,
) -> io::Result<TileOutcome> {
    build_tile_with_span_limit(world, input, options, MAX_SPANS)
}

/// [`build_tile`] with an explicit span budget (tests use a small one to prove the refusal without
/// allocating the real limit).
///
/// # Errors
///
/// As [`build_tile`], plus "too many spans (N, limit L)" when the rasterised heightfield needs
/// more than `span_limit` spans.
#[doc(hidden)]
pub fn build_tile_with_span_limit(
    world: &WorldParams,
    input: &TileInput,
    options: &BuildOptions,
    span_limit: usize,
) -> io::Result<TileOutcome> {
    let class = usize::try_from(input.cell_class)
        .ok()
        .and_then(|i| world.cell_classes.get(i))
        .ok_or_else(|| error!("unknown cell class {}", input.cell_class))?;
    let cfg = tile_config(world, class, options);
    if cfg.cells <= 0 || !cfg.cs.is_finite() || cfg.cs <= 0.0 {
        return Err(error!("cell class has {} cells of {} m", cfg.cells, cfg.cs));
    }
    let obstacles_on = world.world.obstacles;

    // 5.1 setup.
    let (mut extras, tree_items) = extras_list(input, options.doors);
    let pad = cfg.pad;
    let pbmin = [input.bmin[0] - pad, input.bmin[1], input.bmin[2] - pad];
    let pbmax = [input.bmax[0] + pad, input.bmax[1], input.bmax[2] + pad];
    let mut hf = Heightfield::new(cfg.width, cfg.width, pbmin, pbmax, cfg.cs, cfg.ch)
        .map_err(|e| error!("could not create solid heightfield: {e}"))?;
    hf.set_span_limit(span_limit);

    // 5.4 step 1: terrain.
    if let Some(g) = input.groups.first()
        && let Some(area) = area_u8(g.area)
    {
        let p = RasterParams {
            area,
            ..RasterParams::default()
        };
        raster_triangles(&mut hf, &g.verts, &p, Some(cfg.slope_threshold));
    }
    hf.check_span_budget()?;
    // Step 2: mode filter (it only runs for a border above 10).
    mode_filter(&mut hf, cfg.border_size);
    // Step 3: objects.
    let ladders = rasterize_objects(&mut hf, input, &cfg, obstacles_on);
    hf.check_span_budget()?;
    // Step 4: span filters.
    filter_low_height_multi(
        &mut hf,
        [cfg.prone_height, cfg.crouch_height, cfg.walkable_height],
        [0, 16, 17],
        18,
    );
    filter_low_hanging(&mut hf, cfg.walkable_climb);
    let mut skip = [false; 64];
    areas_flag(&mut skip, &[0, 1], true);
    filter_ledges(&mut hf, cfg.walkable_height, cfg.walkable_climb, &skip);
    // Step 5: compact heightfield.
    let mut chf = build_compact(cfg.prone_height, cfg.walkable_climb, &hf)
        .map_err(|e| error!("could not build compact data: {e}"))?;
    // Step 6: area passes.
    let obstacles = post_compact(&mut chf, &cfg, pbmin, pbmax, tree_items, obstacles_on)?;
    // Step 7: links.
    let links = if options.links {
        let hide: Vec<bool> = extras
            .iter()
            .map(|e| u32::from(e.area) == HIDE_AREA)
            .collect();
        generate_links(&mut chf, &hf, &ladders, &cfg, world, &hide)?
    } else {
        Links::default()
    };
    drop(hf);
    // Step 8: distance field and regions.
    build_distance_field(&mut chf).map_err(|e| error!("could not build distance field: {e}"))?;
    build_regions(
        &mut chf,
        cfg.border_size,
        &min_region_table(&cfg),
        cfg.merge_region_area,
    )
    .map_err(|e| error!("could not build regions: {e}"))?;
    // Step 9: contours.
    let cset = build_contours(
        &chf,
        cfg.max_simplification_error,
        cfg.max_vertical_error,
        cfg.max_edge_len,
        RC_CONTOUR_TESS_WALL_EDGES,
    )
    .map_err(|e| error!("could not create contours: {e}"))?;
    if cset.contours.is_empty() {
        return Ok(TileOutcome::Empty);
    }
    // Step 10: polygon mesh.
    let pm = build_poly_mesh(&cset, cfg.nvp)
        .map_err(|e| error!("could not triangulate contours: {e}"))?;
    if pm.areas.is_empty() {
        return Ok(TileOutcome::Empty);
    }

    finish_tile(world, input, &cfg, &pm, &mut extras, obstacles, &links)
}

/// Spec 5.4 step 11 and 7: flags, extras and the Detour tile.
fn finish_tile(
    world: &WorldParams,
    input: &TileInput,
    cfg: &TileConfig,
    pm: &PolyMesh,
    extras: &mut [ExtraPart],
    obstacles: Vec<Obstacle>,
    links: &Links,
) -> io::Result<TileOutcome> {
    let pad = cfg.pad;
    let mut flags: Vec<u16> = pm.areas.iter().map(|a| area_flags(*a)).collect();
    let stray = attach_parts(extras, &pm.sub_areas, &pm.areas, &mut flags);
    let obstacle_count = obstacles.len();
    // The extra section comes after the scalars: which connections Detour keeps depends on them.
    let no_extra = TileExtra::default();
    let mut params = CreateParams::new(
        &pm.verts,
        &pm.polys,
        &flags,
        &pm.areas,
        cfg.nvp,
        &links.off_mesh,
        &no_extra,
    );
    params.tile_x = input.x;
    params.tile_y = input.z;
    params.bmin = [
        (input.bmin[0] - pad) + pad,
        input.bmin[1],
        (input.bmin[2] - pad) + pad,
    ];
    params.bmax = [
        (input.bmax[0] + pad) - pad,
        input.bmax[1] + i32_to_f32(cfg.prone_height) * 0.1_f32,
        (input.bmax[2] + pad) - pad,
    ];
    params.walkable_height = world.world.stand_height;
    params.walkable_radius = world.world.diameter * 0.5_f32;
    params.walkable_climb = world.world.max_step_height;
    params.cs = cfg.cs;
    params.ch = cfg.ch;
    attach_off_mesh(extras, links, &stored_off_mesh(&params), pm.areas.len());
    let extra = extra_section(extras, obstacles)?;
    params.extra = &extra;
    let data =
        create_tile_data(&params).map_err(|e| error!("could not build Detour navmesh: {e}"))?;
    Ok(TileOutcome::Built {
        data,
        stray_part_ids: stray,
        obstacles: obstacle_count,
    })
}

#[cfg(test)]
mod tests {
    use super::{ExtraPart, Links, attach_off_mesh, part_id};
    use crate::navmesh::detour::OffMeshInput;

    fn extra(area: u8) -> ExtraPart {
        ExtraPart {
            object_id: 0,
            part_index: 0,
            area,
            state: 1,
            polys: Vec::new(),
        }
    }

    /// Ruling R17: the off-mesh polygon numbers follow the ground polygons in the order of the stored
    /// connections; connections that Detour drops take no number and no part.
    #[test]
    fn off_mesh_polygons_join_their_hide_part() {
        let mut extras = vec![extra(12), extra(14), extra(14)];
        // The ground polygons 5 and 6 are already in part 1.
        extras[1].polys = vec![5, 6];
        let links = Links {
            off_mesh: vec![OffMeshInput::default(); 6],
            parts: vec![Some(1), Some(2), None, Some(1), Some(9), Some(2)],
        };
        // Connection 1 is not stored, so the polygons are 10, (none), 11, 12, 13, 14.
        let stored = [true, false, true, true, true, true];
        attach_off_mesh(&mut extras, &links, &stored, 10);
        assert_eq!(extras[1].polys, [5, 6, 10, 12]);
        // Part 9 does not exist: ignored, and its polygon (13) is still counted.
        assert_eq!(extras[2].polys, [14]);
        assert_eq!(extras[0].polys, Vec::<u32>::new());
    }

    /// Spec 5.4: part ids wrap like C's `(unsigned short)counter++`.
    #[test]
    fn part_ids_wrap_at_65536() {
        assert_eq!(part_id(0), 0);
        assert_eq!(part_id(65535), 65535);
        assert_eq!(part_id(65536), 0);
        assert_eq!(part_id(65537), 1);
    }
}