raasta 1.0.0

Raasta — navigation and pathfinding engine for AGNOS
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
//! Tiled NavMesh — streaming and localized re-baking for open worlds.

use std::collections::HashMap;

use hisab::Vec2;
use serde::{Deserialize, Serialize};

use crate::mesh::{NavMesh, NavPolyId};

#[cfg(feature = "logging")]
use tracing::instrument;

/// Identifies a tile by its grid coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TileCoord {
    pub x: i32,
    pub y: i32,
}

impl TileCoord {
    #[must_use]
    pub fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
}

/// A globally unique polygon ID combining tile coordinate and local polygon index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GlobalPolyId {
    pub tile: TileCoord,
    pub local: NavPolyId,
}

/// A tile containing its own local navmesh.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavTile {
    /// The tile's local navmesh.
    pub mesh: NavMesh,
    /// World-space origin of this tile (bottom-left corner).
    pub origin: Vec2,
}

/// Connection between polygons across tile boundaries.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct TileConnection {
    from: GlobalPolyId,
    to: GlobalPolyId,
    cost: f32,
}

/// A tiled navmesh for open-world navigation.
///
/// Divides the world into rectangular tiles, each with its own `NavMesh`.
/// Supports streaming (load/unload tiles at runtime) and localized
/// re-baking (only rebuild the affected tile).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TiledNavMesh {
    /// Size of each tile in world units.
    tile_size: f32,
    /// Loaded tiles.
    #[serde(
        serialize_with = "serialize_tile_map",
        deserialize_with = "deserialize_tile_map"
    )]
    tiles: HashMap<TileCoord, NavTile>,
    /// Cross-tile connections (border polygon pairs).
    connections: Vec<TileConnection>,
}

fn serialize_tile_map<S>(
    map: &HashMap<TileCoord, NavTile>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    use serde::ser::SerializeSeq;
    let mut seq = serializer.serialize_seq(Some(map.len()))?;
    for (k, v) in map {
        seq.serialize_element(&(*k, v))?;
    }
    seq.end()
}

fn deserialize_tile_map<'de, D>(deserializer: D) -> Result<HashMap<TileCoord, NavTile>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let entries: Vec<(TileCoord, NavTile)> = Vec::deserialize(deserializer)?;
    Ok(entries.into_iter().collect())
}

impl TiledNavMesh {
    /// Create a new tiled navmesh with the given tile size.
    #[cfg_attr(feature = "logging", instrument)]
    #[must_use]
    pub fn new(tile_size: f32) -> Self {
        Self {
            tile_size,
            tiles: HashMap::new(),
            connections: Vec::new(),
        }
    }

    /// Tile size in world units.
    #[must_use]
    pub fn tile_size(&self) -> f32 {
        self.tile_size
    }

    /// Number of loaded tiles.
    #[must_use]
    pub fn tile_count(&self) -> usize {
        self.tiles.len()
    }

    /// Get the tile coordinate for a world position.
    #[must_use]
    pub fn world_to_tile(&self, pos: Vec2) -> TileCoord {
        TileCoord {
            x: (pos.x / self.tile_size).floor() as i32,
            y: (pos.y / self.tile_size).floor() as i32,
        }
    }

    /// Load (insert) a tile. If a tile already exists at this coordinate, it is replaced.
    ///
    /// After loading, call `rebuild_connections()` to update cross-tile connectivity.
    #[cfg_attr(feature = "logging", instrument(skip(self, mesh)))]
    pub fn load_tile(&mut self, coord: TileCoord, mesh: NavMesh) {
        let origin = Vec2::new(
            coord.x as f32 * self.tile_size,
            coord.y as f32 * self.tile_size,
        );
        self.tiles.insert(coord, NavTile { mesh, origin });
    }

    /// Unload (remove) a tile.
    ///
    /// After unloading, call `rebuild_connections()` to update connectivity.
    pub fn unload_tile(&mut self, coord: TileCoord) -> bool {
        let removed = self.tiles.remove(&coord).is_some();
        if removed {
            // Remove connections involving this tile
            self.connections
                .retain(|c| c.from.tile != coord && c.to.tile != coord);
        }
        removed
    }

    /// Check if a tile is loaded.
    #[must_use]
    pub fn has_tile(&self, coord: TileCoord) -> bool {
        self.tiles.contains_key(&coord)
    }

    /// Get a tile's navmesh.
    #[must_use]
    pub fn get_tile(&self, coord: TileCoord) -> Option<&NavTile> {
        self.tiles.get(&coord)
    }

    /// Re-bake a single tile from new boundary geometry.
    ///
    /// Replaces the tile's navmesh with a freshly baked one and rebuilds
    /// only the connections involving this tile and its neighbors.
    /// Much cheaper than `rebuild_connections()` for the full mesh.
    ///
    /// `boundary` is in tile-local coordinates (origin at 0,0).
    #[cfg_attr(feature = "logging", instrument(skip(self, boundary)))]
    pub fn rebake_tile(&mut self, coord: TileCoord, boundary: &[Vec2]) {
        let mesh = NavMesh::bake(boundary);
        self.load_tile(coord, mesh);
        self.rebuild_tile_connections(coord);
    }

    /// Rebuild connections for a single tile and its neighbors.
    ///
    /// Cheaper than full `rebuild_connections()` when only one tile changed.
    #[cfg_attr(feature = "logging", instrument(skip(self)))]
    pub fn rebuild_tile_connections(&mut self, coord: TileCoord) {
        // Remove old connections involving this tile
        self.connections
            .retain(|c| c.from.tile != coord && c.to.tile != coord);

        // Reconnect with all 4 neighbors
        let neighbors = [
            (TileCoord::new(coord.x + 1, coord.y), Border::Right),
            (TileCoord::new(coord.x - 1, coord.y), Border::Left),
            (TileCoord::new(coord.x, coord.y + 1), Border::Top),
            (TileCoord::new(coord.x, coord.y - 1), Border::Bottom),
        ];

        for (neighbor, border) in neighbors {
            if !self.tiles.contains_key(&neighbor) {
                continue;
            }
            match border {
                Border::Right => self.connect_tiles(coord, neighbor, Border::Right),
                Border::Left => self.connect_tiles(neighbor, coord, Border::Right),
                Border::Top => self.connect_tiles(coord, neighbor, Border::Top),
                Border::Bottom => self.connect_tiles(neighbor, coord, Border::Top),
            }
        }
    }

    /// Rebuild cross-tile connections for all loaded tiles.
    ///
    /// Scans border polygons of adjacent tiles and creates connections
    /// between polygons that share vertices near tile boundaries.
    #[cfg_attr(feature = "logging", instrument(skip(self)))]
    pub fn rebuild_connections(&mut self) {
        self.connections.clear();

        let coords: Vec<TileCoord> = self.tiles.keys().copied().collect();

        for &coord in &coords {
            // Check right neighbor
            let right = TileCoord::new(coord.x + 1, coord.y);
            if self.tiles.contains_key(&right) {
                self.connect_tiles(coord, right, Border::Right);
            }
            // Check top neighbor
            let top = TileCoord::new(coord.x, coord.y + 1);
            if self.tiles.contains_key(&top) {
                self.connect_tiles(coord, top, Border::Top);
            }
        }
    }

    /// Find a path across tiles from start to goal (world positions).
    ///
    /// Performs a two-level search:
    /// 1. Find start/goal tiles and local polygons
    /// 2. A* across tiles using connections
    ///
    /// Returns the path as a sequence of world-space waypoints (polygon centroids).
    #[cfg_attr(feature = "logging", instrument(skip(self)))]
    #[must_use]
    pub fn find_path(&self, start: Vec2, goal: Vec2) -> Option<Vec<Vec2>> {
        let start_tile_coord = self.world_to_tile(start);
        let goal_tile_coord = self.world_to_tile(goal);

        let start_tile = self.tiles.get(&start_tile_coord)?;
        let _goal_tile = self.tiles.get(&goal_tile_coord)?;

        // If same tile, use local pathfinding
        if start_tile_coord == goal_tile_coord {
            let local_start = start - start_tile.origin;
            let local_goal = goal - start_tile.origin;
            let poly_path = start_tile.mesh.find_path(local_start, local_goal)?;
            return Some(
                poly_path
                    .iter()
                    .filter_map(|&id| start_tile.mesh.get_poly(id))
                    .map(|p| p.centroid() + start_tile.origin)
                    .collect(),
            );
        }

        // Cross-tile: simple A* on the global graph
        self.cross_tile_path(start, goal, start_tile_coord, goal_tile_coord)
    }

    /// Cross-tile A* pathfinding on the global polygon graph.
    fn cross_tile_path(
        &self,
        start: Vec2,
        goal: Vec2,
        start_tile: TileCoord,
        goal_tile: TileCoord,
    ) -> Option<Vec<Vec2>> {
        use std::cmp::Ordering;
        use std::collections::BinaryHeap;

        // Build a simple global node list: (tile, local_poly_id) -> global index
        let mut nodes: Vec<GlobalPolyId> = Vec::new();
        let mut node_positions: Vec<Vec2> = Vec::new();
        let mut tile_node_map: HashMap<GlobalPolyId, usize> = HashMap::new();

        for (&coord, tile) in &self.tiles {
            for poly in tile.mesh.polys() {
                let gid = GlobalPolyId {
                    tile: coord,
                    local: poly.id,
                };
                let idx = nodes.len();
                tile_node_map.insert(gid, idx);
                nodes.push(gid);
                node_positions.push(poly.centroid() + tile.origin);
            }
        }

        if nodes.is_empty() {
            return None;
        }

        // Find start and goal nodes
        let st = self.tiles.get(&start_tile)?;
        let local_start = start - st.origin;
        let start_poly = st.mesh.find_poly_at(local_start)?;
        let start_gid = GlobalPolyId {
            tile: start_tile,
            local: start_poly,
        };
        let start_idx = *tile_node_map.get(&start_gid)?;

        let gt = self.tiles.get(&goal_tile)?;
        let local_goal = goal - gt.origin;
        let goal_poly = gt.mesh.find_poly_at(local_goal)?;
        let goal_gid = GlobalPolyId {
            tile: goal_tile,
            local: goal_poly,
        };
        let goal_idx = *tile_node_map.get(&goal_gid)?;

        // Build adjacency: local neighbors + cross-tile connections
        let n = nodes.len();
        let mut adj: Vec<Vec<(usize, f32)>> = vec![Vec::new(); n];

        // Local neighbors
        for (&coord, tile) in &self.tiles {
            for poly in tile.mesh.polys() {
                let gid = GlobalPolyId {
                    tile: coord,
                    local: poly.id,
                };
                let from_idx = tile_node_map[&gid];
                for &neighbor_id in &poly.neighbors {
                    let ngid = GlobalPolyId {
                        tile: coord,
                        local: neighbor_id,
                    };
                    if let Some(&to_idx) = tile_node_map.get(&ngid) {
                        let cost = node_positions[from_idx].distance(node_positions[to_idx]);
                        adj[from_idx].push((to_idx, cost));
                    }
                }
            }
        }

        // Cross-tile connections
        for conn in &self.connections {
            if let (Some(&from_idx), Some(&to_idx)) =
                (tile_node_map.get(&conn.from), tile_node_map.get(&conn.to))
            {
                adj[from_idx].push((to_idx, conn.cost));
                adj[to_idx].push((from_idx, conn.cost));
            }
        }

        // A*
        #[derive(Clone, Copy)]
        struct AStarNode {
            idx: usize,
            f: f32,
        }

        impl PartialEq for AStarNode {
            fn eq(&self, other: &Self) -> bool {
                self.f == other.f
            }
        }

        impl Eq for AStarNode {}

        impl PartialOrd for AStarNode {
            fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for AStarNode {
            fn cmp(&self, other: &Self) -> Ordering {
                other.f.partial_cmp(&self.f).unwrap_or(Ordering::Equal)
            }
        }

        let mut g = vec![f32::INFINITY; n];
        let mut came_from: Vec<Option<usize>> = vec![None; n];
        let mut closed = vec![false; n];
        let mut open = BinaryHeap::new();

        g[start_idx] = 0.0;
        let h = node_positions[start_idx].distance(node_positions[goal_idx]);
        open.push(AStarNode {
            idx: start_idx,
            f: h,
        });

        while let Some(current) = open.pop() {
            if current.idx == goal_idx {
                // Reconstruct path
                let mut path = Vec::new();
                let mut cur = goal_idx;
                loop {
                    path.push(node_positions[cur]);
                    match came_from[cur] {
                        Some(prev) => cur = prev,
                        None => break,
                    }
                }
                path.reverse();
                return Some(path);
            }

            if closed[current.idx] {
                continue;
            }
            closed[current.idx] = true;

            for &(ni, cost) in &adj[current.idx] {
                if closed[ni] {
                    continue;
                }
                let tg = g[current.idx] + cost;
                if tg < g[ni] {
                    g[ni] = tg;
                    came_from[ni] = Some(current.idx);
                    let h = node_positions[ni].distance(node_positions[goal_idx]);
                    open.push(AStarNode { idx: ni, f: tg + h });
                }
            }
        }

        None
    }

    /// Connect border polygons between two adjacent tiles.
    fn connect_tiles(&mut self, a: TileCoord, b: TileCoord, border: Border) {
        let tile_a = match self.tiles.get(&a) {
            Some(t) => t,
            None => return,
        };
        let tile_b = match self.tiles.get(&b) {
            Some(t) => t,
            None => return,
        };

        let threshold = self.tile_size * 0.05; // 5% tolerance for border matching

        // Collect polygon data to avoid borrow conflicts
        let polys_a: Vec<(NavPolyId, Vec<Vec2>, Vec2)> = tile_a
            .mesh
            .polys()
            .iter()
            .map(|p| (p.id, p.vertices.clone(), p.centroid() + tile_a.origin))
            .collect();
        let polys_b: Vec<(NavPolyId, Vec<Vec2>, Vec2)> = tile_b
            .mesh
            .polys()
            .iter()
            .map(|p| (p.id, p.vertices.clone(), p.centroid() + tile_b.origin))
            .collect();

        let origin_a = tile_a.origin;
        let origin_b = tile_b.origin;
        let tile_size = self.tile_size;

        let opposite = match border {
            Border::Right => Border::Left,
            Border::Top => Border::Bottom,
            _ => return,
        };

        for (id_a, verts_a, centroid_a) in &polys_a {
            if !is_near_border(verts_a, tile_size, border, threshold) {
                continue;
            }

            for (id_b, verts_b, centroid_b) in &polys_b {
                if !is_near_border(verts_b, tile_size, opposite, threshold) {
                    continue;
                }

                if has_shared_border_vertices(verts_a, origin_a, verts_b, origin_b, threshold) {
                    let cost = centroid_a.distance(*centroid_b);
                    self.connections.push(TileConnection {
                        from: GlobalPolyId {
                            tile: a,
                            local: *id_a,
                        },
                        to: GlobalPolyId {
                            tile: b,
                            local: *id_b,
                        },
                        cost,
                    });
                }
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum Border {
    Right,
    Top,
    Left,
    Bottom,
}

/// Check if any vertex of a polygon is near a tile border.
fn is_near_border(vertices: &[Vec2], tile_size: f32, border: Border, threshold: f32) -> bool {
    for v in vertices {
        match border {
            Border::Right => {
                if (v.x - tile_size).abs() < threshold {
                    return true;
                }
            }
            Border::Left => {
                if v.x.abs() < threshold {
                    return true;
                }
            }
            Border::Top => {
                if (v.y - tile_size).abs() < threshold {
                    return true;
                }
            }
            Border::Bottom => {
                if v.y.abs() < threshold {
                    return true;
                }
            }
        }
    }
    false
}

/// Check if two polygons share vertices near the tile boundary.
fn has_shared_border_vertices(
    verts_a: &[Vec2],
    origin_a: Vec2,
    verts_b: &[Vec2],
    origin_b: Vec2,
    threshold: f32,
) -> bool {
    for va in verts_a {
        let world_a = *va + origin_a;
        for vb in verts_b {
            let world_b = *vb + origin_b;
            if world_a.distance(world_b) < threshold {
                return true;
            }
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mesh::NavPoly;

    fn make_tile_mesh() -> NavMesh {
        // Simple square polygon from (0,0) to (10,10)
        let mut mesh = NavMesh::new();
        mesh.add_poly(NavPoly {
            id: NavPolyId(0),
            vertices: vec![
                Vec2::ZERO,
                Vec2::new(10.0, 0.0),
                Vec2::new(10.0, 10.0),
                Vec2::new(0.0, 10.0),
            ],
            neighbors: vec![],
            cost: 1.0,
            layer: 0,
        });
        mesh
    }

    #[test]
    fn tiled_basic() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        assert_eq!(tiled.tile_count(), 1);
        assert!(tiled.has_tile(TileCoord::new(0, 0)));
    }

    #[test]
    fn tiled_world_to_tile() {
        let tiled = TiledNavMesh::new(10.0);
        assert_eq!(
            tiled.world_to_tile(Vec2::new(5.0, 5.0)),
            TileCoord::new(0, 0)
        );
        assert_eq!(
            tiled.world_to_tile(Vec2::new(15.0, 5.0)),
            TileCoord::new(1, 0)
        );
        assert_eq!(
            tiled.world_to_tile(Vec2::new(-5.0, -5.0)),
            TileCoord::new(-1, -1)
        );
    }

    #[test]
    fn tiled_same_tile_path() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        let path = tiled.find_path(Vec2::new(2.0, 2.0), Vec2::new(8.0, 8.0));
        assert!(path.is_some());
    }

    #[test]
    fn tiled_unload() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        assert!(tiled.unload_tile(TileCoord::new(0, 0)));
        assert!(!tiled.has_tile(TileCoord::new(0, 0)));
        assert_eq!(tiled.tile_count(), 0);
    }

    #[test]
    fn tiled_unload_nonexistent() {
        let mut tiled = TiledNavMesh::new(10.0);
        assert!(!tiled.unload_tile(TileCoord::new(0, 0)));
    }

    #[test]
    fn tiled_cross_tile_path() {
        let mut tiled = TiledNavMesh::new(10.0);

        // Two tiles side by side, each with a square polygon
        // Tile (0,0): polygon with vertex at (10,0)-(10,10) on right border
        // Tile (1,0): polygon with vertex at (0,0)-(0,10) on left border (= world 10,0-10,10)
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        tiled.load_tile(TileCoord::new(1, 0), make_tile_mesh());
        tiled.rebuild_connections();

        let path = tiled.find_path(Vec2::new(5.0, 5.0), Vec2::new(15.0, 5.0));
        assert!(path.is_some());
        let path = path.unwrap();
        assert!(path.len() >= 2);
    }

    #[test]
    fn tiled_no_path_unloaded_tile() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        // Goal is in unloaded tile
        assert!(
            tiled
                .find_path(Vec2::new(5.0, 5.0), Vec2::new(25.0, 5.0))
                .is_none()
        );
    }

    #[test]
    fn tiled_serde_roundtrip() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        let json = serde_json::to_string(&tiled).unwrap();
        let deserialized: TiledNavMesh = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.tile_count(), 1);
    }

    #[test]
    fn tile_coord_new() {
        let tc = TileCoord::new(3, -2);
        assert_eq!(tc.x, 3);
        assert_eq!(tc.y, -2);
    }

    #[test]
    fn rebake_tile() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        assert_eq!(
            tiled
                .get_tile(TileCoord::new(0, 0))
                .unwrap()
                .mesh
                .poly_count(),
            1
        );

        // Rebake with a triangle
        tiled.rebake_tile(
            TileCoord::new(0, 0),
            &[Vec2::ZERO, Vec2::new(10.0, 0.0), Vec2::new(5.0, 10.0)],
        );
        assert!(tiled.has_tile(TileCoord::new(0, 0)));
        // Poly count may differ after rebake
        assert!(
            tiled
                .get_tile(TileCoord::new(0, 0))
                .unwrap()
                .mesh
                .poly_count()
                >= 1
        );
    }

    #[test]
    fn rebuild_tile_connections_local() {
        let mut tiled = TiledNavMesh::new(10.0);
        tiled.load_tile(TileCoord::new(0, 0), make_tile_mesh());
        tiled.load_tile(TileCoord::new(1, 0), make_tile_mesh());
        tiled.rebuild_connections();

        // Rebake one tile — connections should still work
        tiled.rebake_tile(
            TileCoord::new(0, 0),
            &[
                Vec2::ZERO,
                Vec2::new(10.0, 0.0),
                Vec2::new(10.0, 10.0),
                Vec2::new(0.0, 10.0),
            ],
        );

        let path = tiled.find_path(Vec2::new(5.0, 5.0), Vec2::new(15.0, 5.0));
        assert!(path.is_some());
    }
}