bevy_northstar 0.3.0

A Bevy plugin for Hierarchical Pathfinding
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
//! This module contains the `Grid` component which is the main component for the crate.
use std::sync::Arc;

use bevy::{
    log,
    math::{IVec3, UVec3},
    platform::collections::HashMap,
    prelude::{Component, Entity},
};
use ndarray::{s, Array3, ArrayView2, ArrayView3};

use crate::{
    chunk::Chunk,
    dijkstra::*,
    dir::*,
    filter::NeighborFilter,
    graph::Graph,
    nav::{Nav, NavCell},
    neighbor::Neighborhood,
    node::Node,
    path::Path,
    pathfind::{pathfind, pathfind_astar, reroute_path},
    position_in_cubic_window, MovementCost,
};

/// Settings for how the grid is divided into chunks.
#[derive(Copy, Clone, Debug)]
pub struct ChunkSettings {
    /// The square size of each chunk in the grid.
    /// Needs to be at least 3.
    pub size: u32,
    /// The depth of each chunk in the grid when using 3D grids.
    pub depth: u32,
    /// If true, allows corner connections between chunks.
    /// This will increase the time it takes to build the grid.
    /// It generally isn't recommended as the path refinement step should handle corners already unless you have a noisy tilemap.
    pub diagonal_connections: bool,
}

impl Default for ChunkSettings {
    fn default() -> Self {
        ChunkSettings {
            size: 16,
            depth: 1,
            diagonal_connections: false,
        }
    }
}

/// Defaults movement cost and passability for initializing the grid cells.
/// Useful if you're generating your a large map to reduce your initialization time.
#[derive(Copy, Clone, Debug)]
pub struct NavSettings {
    /// The default cost for each cell in the grid.
    pub default_movement_cost: MovementCost,
    /// If true, the default cells will be solid and block movement.
    pub default_impassible: bool,
}

impl Default for NavSettings {
    fn default() -> Self {
        NavSettings {
            default_movement_cost: 1,
            default_impassible: false,
        }
    }
}

/// Settings for collision
#[derive(Copy, Clone, Debug)]
pub struct CollisionSettings {
    /// If true, collision avoidance is enabled.
    pub enabled: bool,
    /// The plugin systems use collision avoidance, this is the look ahead distance for the path to check for blocking entities.
    pub avoidance_distance: u32,
}

impl Default for CollisionSettings {
    fn default() -> Self {
        CollisionSettings {
            enabled: false,
            avoidance_distance: 4,
        }
    }
}

/// Settings for filtering determined neighbors.
#[derive(Clone, Default)]
pub struct NeighborhoodSettings {
    pub filters: Vec<Arc<dyn NeighborFilter + Send + Sync + 'static>>,
}

/// Holder for internal crate settings.
pub struct GridSettings(pub(crate) GridInternalSettings);

/// Builder for `GridSettings`.
///
/// Example usage:
/// ```
/// use bevy_northstar::prelude::*;
///
/// let grid_settings = GridSettingsBuilder::new_2d(128, 128)
///     .chunk_size(16)
///     .default_impassable()
///     .add_neighbor_filter(filter::NoCornerClipping)
///     .enable_collision()
///     .avoidance_distance(5)
///     .build();
///
/// let grid: Grid<OrdinalNeighborhood> = Grid::new(&grid_settings);
/// ```
///
#[derive(Clone)]
pub struct GridSettingsBuilder {
    dimensions: UVec3,
    chunk_settings: ChunkSettings,
    cost_settings: NavSettings,
    collision_settings: CollisionSettings,
    neighborhood_settings: NeighborhoodSettings,
}

impl Default for GridSettingsBuilder {
    fn default() -> Self {
        GridSettingsBuilder {
            dimensions: UVec3::new(64, 64, 1),
            chunk_settings: ChunkSettings::default(),
            cost_settings: NavSettings::default(),
            collision_settings: CollisionSettings::default(),
            neighborhood_settings: NeighborhoodSettings::default(),
        }
    }
}

impl GridSettingsBuilder {
    /// Initalize a 2D [`Grid`] with the given width and height. Returns a [`GridSettingsBuilder`] that can be further configured.
    pub fn new_2d(width: u32, height: u32) -> Self {
        if width < 3 || height < 3 {
            panic!("Width and height must be at least 3");
        }

        let mut grid_settings = GridSettingsBuilder {
            dimensions: UVec3::new(width, height, 1),
            ..Default::default()
        };

        grid_settings.chunk_settings.depth = 1;

        grid_settings
    }

    /// Initalize a 3D [`Grid`] with the given width, height, and depth. Returns a [`GridSettingsBuilder`] that can be further configured.
    pub fn new_3d(width: u32, height: u32, depth: u32) -> Self {
        if width < 3 || height < 3 {
            panic!("Width and height must be at least 3");
        }

        if depth < 1 {
            panic!("Depth must be at least 1");
        }

        GridSettingsBuilder {
            dimensions: UVec3::new(width, height, depth),
            ..Default::default()
        }
    }

    /// Size of each square chunk the grid is divided into.
    /// Must be at least 3.
    pub fn chunk_size(mut self, chunk_size: u32) -> Self {
        if chunk_size < 3 {
            panic!("Chunk size must be at least 3");
        }

        self.chunk_settings.size = chunk_size;
        self
    }

    /// Depth (Z) of each chunk in the grid when using 3D grids.
    /// Must be at least 1 and the grid's depth must be divisible by the chunk depth.
    pub fn chunk_depth(mut self, chunk_depth: u32) -> Self {
        if chunk_depth < 1 {
            panic!("Chunk depth must be at least 1");
        }

        //FIXME: User should be able to set the chunk depth so there's no z chunking but still allow x/y chunking
        // Right now we need dimension.z to be divisible by chunk_depth
        if self.dimensions.z % chunk_depth != 0 {
            panic!("Depth must be divisible by chunk depth");
        }

        self.chunk_settings.depth = chunk_depth;
        self
    }

    /// Enabling this will create extra HPA connections in the corners of the chunks if passable.
    /// This can create better paths in some cases but at the cost of performance.
    /// If you're only using refined HPA* (default) paths then this likely isn't needed.
    pub fn enable_diagonal_connections(mut self) -> Self {
        self.chunk_settings.diagonal_connections = true;
        self
    }

    /// Default movement cost for each cell in the grid.
    pub fn default_movement_cost(mut self, default_movement_cost: MovementCost) -> Self {
        self.cost_settings.default_movement_cost = default_movement_cost;
        self
    }

    /// Sets the default cells in the grid to be impassable.
    pub fn default_impassable(mut self) -> Self {
        self.cost_settings.default_impassible = true;
        self
    }

    /// Enables collision avoidance in the [`crate::plugin::NorthstarPlugin`] pathfinding systems.
    pub fn enable_collision(mut self) -> Self {
        self.collision_settings.enabled = true;
        self
    }

    /// The look ahead distance for collision avoidance.
    /// No need to set this if collision is not enabled.
    pub fn avoidance_distance(mut self, distance: u32) -> Self {
        self.collision_settings.avoidance_distance = distance;
        self
    }

    /// Adds a [`NeighborFilter`] to the grid settings to be applied when computing neighbors.
    /// This lets you apply custom movement rules based on grid cell state.
    /// Multiple filters can be added and will be applied in the order they are added.
    pub fn add_neighbor_filter<F>(mut self, filter: F) -> Self
    where
        F: NeighborFilter + Send + Sync + 'static,
    {
        self.neighborhood_settings.filters.push(Arc::new(filter));
        self
    }

    /// Pass in a [`ChunkSettings`] to configure the grid's chunking behavior.
    /// Or use the individual methods [`GridSettingsBuilder::chunk_size()`] and [`GridSettingsBuilder::chunk_depth()`] to set the chunk size and depth individually.
    pub fn chunk_settings(mut self, chunk_settings: ChunkSettings) -> Self {
        self = self.chunk_size(chunk_settings.size);
        self = self.chunk_depth(chunk_settings.depth);

        if chunk_settings.diagonal_connections {
            self = self.enable_diagonal_connections();
        }

        self
    }

    /// Pass in [`NavSettings`] to configure the grid's default cell navigation data.
    /// You can also use the individual methods [`GridSettingsBuilder::default_movement_cost()`] and [`GridSettingsBuilder::default_impassable()`].
    pub fn nav_settings(mut self, nav_settings: NavSettings) -> Self {
        self = self.default_movement_cost(nav_settings.default_movement_cost);

        if nav_settings.default_impassible {
            self = self.default_impassable();
        }

        self
    }

    /// Pass in a [`CollisionSettings`] to configure the grid's collision avoidance behavior.
    /// You can also use the individual methods [`GridSettingsBuilder::enable_collision()`] and [`GridSettingsBuilder::avoidance_distance()`].
    pub fn collision_settings(mut self, collison_settings: CollisionSettings) -> Self {
        self.collision_settings = collison_settings;
        self
    }

    /// Pass in a [`NeighborhoodSettings`] to configure the grid's neighborhood behavior.
    /// You can also use the individual method [`GridSettingsBuilder::add_neighbor_filter()`] to add a filter.
    pub fn neighborhood_settings(mut self, neighborhood_settings: NeighborhoodSettings) -> Self {
        self.neighborhood_settings = neighborhood_settings;
        self
    }

    /// Builds the [`GridSettings`] from the current builder state.
    /// Call this after you've configured the builder to your liking
    /// and then pass the resulting [`GridSettings`] to the [`Grid::new()`] method.
    pub fn build(self) -> GridSettings {
        GridSettings(GridInternalSettings {
            dimensions: self.dimensions,
            chunk_settings: self.chunk_settings,
            cost_settings: self.cost_settings,
            collision_settings: self.collision_settings,
            neighborhood_settings: self.neighborhood_settings,
        })
    }
}

#[derive(Clone)]
pub(crate) struct GridInternalSettings {
    pub(crate) dimensions: UVec3,
    pub(crate) chunk_settings: ChunkSettings,
    pub(crate) cost_settings: NavSettings,
    pub(crate) collision_settings: CollisionSettings,
    pub(crate) neighborhood_settings: NeighborhoodSettings,
}

impl Default for GridInternalSettings {
    fn default() -> Self {
        GridSettingsBuilder::default().build().0
    }
}

/// `Grid` is the main `Resource` struct for the crate.
///
/// # Example
/// ```rust,no_run
/// use bevy::prelude::*;
/// use bevy_northstar::prelude::*;
///
/// fn main() {
///     App::new()
///        .add_plugins(DefaultPlugins)
///        .add_plugins(NorthstarPlugin::<CardinalNeighborhood>::default())
///        .add_systems(Startup, startup);
/// }
///
/// fn startup(mut commands: Commands) {
///    // Populate the grid with, you'd usually do this after you load your tilemap
///    // and then set the cells in the grid to match the tilemap.
///
///    let grid_settings = GridSettingsBuilder::new_2d(64, 64)
///       .chunk_size(16)
///       .enable_collision()
///       .build();
///
///    let mut grid: Grid<CardinalNeighborhood> = Grid::new(&grid_settings);
///
///    // Set the cell at (0, 0, 0) to be passable with a cost of 1
///    grid.set_nav(UVec3::new(0, 0, 0), Nav::Passable(1));
///    // Set the cell at (1, 0, 0) to be impassable
///    grid.set_nav(UVec3::new(1, 0, 0), Nav::Impassable);
///    // Initialize the grid
///    grid.build();
///
///    // Add the grid to the world
///    commands.spawn(grid);
/// }
/// ```
#[derive(Component)]
pub struct Grid<N: Neighborhood> {
    pub(crate) neighborhood: N,

    dimensions: UVec3,
    chunk_settings: ChunkSettings,
    collision_settings: CollisionSettings,

    grid: Array3<NavCell>,
    chunks: Array3<Chunk>,

    graph: Graph,

    dirty: bool,
}

impl<N: Neighborhood + Default> Grid<N> {
    /// Creates a new [`Grid`] instance with the given [`GridSettings`].
    /// Use the [`GridSettingsBuilder`] to generate the settings.
    pub fn new(settings: &GridSettings) -> Self {
        let GridInternalSettings {
            dimensions,
            chunk_settings,
            cost_settings,
            collision_settings,
            neighborhood_settings: _,
        } = settings.0;

        let UVec3 { x, y, z } = dimensions;

        let x_chunks = x / chunk_settings.size;
        let y_chunks = y / chunk_settings.size;
        let z_chunks = z / chunk_settings.depth;

        let default_navcell = if cost_settings.default_impassible {
            NavCell::new(Nav::Impassable)
        } else {
            NavCell::new(Nav::Passable(cost_settings.default_movement_cost))
        };

        let grid = Array3::from_elem((x as usize, y as usize, z as usize), default_navcell);

        let chunks = Array3::from_shape_fn(
            (x_chunks as usize, y_chunks as usize, z_chunks as usize),
            |(x, y, z)| {
                let min_x = x as u32 * chunk_settings.size;
                let max_x = min_x + chunk_settings.size - 1;
                let min_y = y as u32 * chunk_settings.size;
                let max_y = min_y + chunk_settings.size - 1;
                let min_z = z as u32 * chunk_settings.depth;
                let max_z = min_z + chunk_settings.depth - 1;

                Chunk::new(
                    UVec3::new(min_x, min_y, min_z),
                    UVec3::new(max_x, max_y, max_z),
                )
            },
        );

        Self {
            neighborhood: N::from_settings(&settings.0.neighborhood_settings),
            dimensions,
            chunk_settings,
            collision_settings,

            grid,
            chunks,

            graph: Graph::new(),

            dirty: true,
        }
    }

    /// Returns the neighborhood used by this grid.
    /// Useful if you want to call [`Neighborhood::is_ordinal()`] or similar methods.
    pub fn neighborhood(&self) -> &N {
        &self.neighborhood
    }

    /// Returns an [`ndarray::ArrayView3<NavCell>`] for read-only access to the grid data.
    pub fn view(&self) -> ArrayView3<NavCell> {
        self.grid.view()
    }

    /// Returns an [`ndarray::ArrayView3<NavCell>`] for read-only access to the data within a given [`Chunk`].
    pub(crate) fn chunk_view(&self, chunk: &Chunk) -> ArrayView3<NavCell> {
        chunk.view(&self.grid)
    }

    /// Returns the [`Graph`] associated with this grid.
    pub(crate) fn graph(&self) -> &Graph {
        &self.graph
    }

    /// Test if a grid cell is passable at a given [`bevy::math::UVec3`] position.
    pub fn is_passable(&self, pos: UVec3) -> bool {
        if !self.in_bounds(pos) {
            return false;
        }

        self.grid[[pos.x as usize, pos.y as usize, pos.z as usize]].is_passable()
    }

    /// Test if a grid cell is a ramp at a given [`bevy::math::UVec3`] position.
    pub fn is_ramp(&self, pos: UVec3) -> bool {
        if !self.in_bounds(pos) {
            return false;
        }

        self.grid[[pos.x as usize, pos.y as usize, pos.z as usize]].is_ramp()
    }

    /// Set the [`Nav`] settings at a given [`bevy::math::UVec3`] position in the grid.
    pub fn set_nav(&mut self, pos: UVec3, nav: Nav) {
        if !self.in_bounds(pos) {
            panic!("Attempted to set nav at out-of-bounds position");
        }

        if !self.dirty {
            log::warn!("The Grid state is dirty, you either forgot to call `build()` on grid or you need to call `build()` after modifying the grid. In the future partial rebuilding will be supported.");
        }

        let navcell = NavCell::new(nav);
        self.grid[[pos.x as usize, pos.y as usize, pos.z as usize]] = navcell;
    }

    /// Gets the [`Nav`] settings at a given [`bevy::math::UVec3`] position in the grid.
    pub fn nav(&self, pos: UVec3) -> Option<Nav> {
        if self.in_bounds(pos) {
            Some(self.grid[[pos.x as usize, pos.y as usize, pos.z as usize]].nav())
        } else {
            None
        }
    }

    /// Gets the [`NavCell`] at a given [`bevy::math::UVec3`] position in the grid.
    pub(crate) fn navcell(&self, pos: UVec3) -> &NavCell {
        &self.grid[[pos.x as usize, pos.y as usize, pos.z as usize]]
    }

    /// Returns the dimensions of the grid.
    pub fn dimensions(&self) -> UVec3 {
        self.dimensions
    }

    /// Returns the width of the grid.
    pub fn width(&self) -> u32 {
        self.dimensions.x
    }

    /// Returns the height of the grid.
    pub fn height(&self) -> u32 {
        self.dimensions.y
    }

    /// Returns the depth of the grid.
    pub fn depth(&self) -> u32 {
        self.dimensions.z
    }

    /// Returns the square width/height dimensions of the chunks in the grid.
    pub fn chunk_size(&self) -> u32 {
        self.chunk_settings.size
    }

    /// Returns the chunk settings of the grid.
    pub fn chunk_settings(&self) -> ChunkSettings {
        self.chunk_settings
    }

    /// Test if collision is enabled.
    pub fn collision(&self) -> bool {
        self.collision_settings.enabled
    }

    /// Set the collision settings for the grid.
    pub fn set_collision(&mut self, collision: bool) {
        self.collision_settings.enabled = collision;
    }

    /// Returns the avoidance distance for collision avoidance
    pub fn avoidance_distance(&self) -> u32 {
        self.collision_settings.avoidance_distance
    }

    /// Set the avoidance distance for collision avoidance
    pub fn set_avoidance_distance(&mut self, distance: u32) {
        self.collision_settings.avoidance_distance = distance;
    }

    /// Checks if a position is within the bounds of the grid.
    pub fn in_bounds(&self, pos: UVec3) -> bool {
        pos.x < self.dimensions.x && pos.y < self.dimensions.y && pos.z < self.dimensions.z
    }

    /// Builds the entire grid. This includes precomputing neighbors, creating nodes for each edge of each chunk,
    /// caching paths between internal nodes within each chunk, and connecting adjacent nodes between chunks.
    /// This method needs to be called after the grid has been initialized.
    pub fn build(&mut self) {
        self.precompute_neighbors();
        self.build_nodes();
        self.connect_internal_chunk_nodes();
        self.connect_adjacent_chunk_nodes();
        self.dirty = false;
    }

    pub(crate) fn precompute_neighbors(&mut self) {
        let grid_view = self.grid.view();

        // Collect positions and neighbor bits first to avoid borrow checker issues
        let mut neighbor_bits_vec = Vec::with_capacity(self.grid.len());
        for ((x, y, z), _cell) in self.grid.indexed_iter() {
            let pos = UVec3::new(x as u32, y as u32, z as u32);
            let bits = self.neighborhood.neighbors(&grid_view, pos);
            neighbor_bits_vec.push(((x, y, z), bits));
        }

        // Now apply the neighbor bits with mutable access
        for &((x, y, z), bits) in &neighbor_bits_vec {
            self.grid[[x, y, z]].neighbor_bits = bits;
        }
    }

    // Populates the graph with nodes for each edge of each chunk.
    fn build_nodes(&mut self) {
        let chunk_size = self.chunk_settings.size as usize;
        let chunk_depth = self.chunk_settings.depth as usize;

        let x_chunks = self.dimensions.x as usize / chunk_size;
        let y_chunks = self.dimensions.y as usize / chunk_size;
        let z_chunks = self.dimensions.z as usize / chunk_depth;

        for x in 0..x_chunks {
            for y in 0..y_chunks {
                for z in 0..z_chunks {
                    let current_chunk = &self.chunks[[x, y, z]];

                    let directions = Dir::cardinal();

                    for dir in directions {
                        let dir_vec = dir.vector();

                        let nx = x as i32 + dir_vec.0;
                        let ny = y as i32 + dir_vec.1;
                        let nz = z as i32 + dir_vec.2;

                        // Ensure neighbor is within bounds
                        if nx >= 0
                            && nx < x_chunks as i32
                            && ny >= 0
                            && ny < y_chunks as i32
                            && nz >= 0
                            && nz < z_chunks as i32
                        {
                            let neighbor_chunk =
                                &self.chunks[[nx as usize, ny as usize, nz as usize]];

                            let current_edge = current_chunk.edge(&self.grid, dir);
                            let neighbor_edge = neighbor_chunk.edge(&self.grid, dir.opposite());

                            let mut nodes = self.calculate_edge_nodes(
                                current_edge,
                                neighbor_edge,
                                current_chunk.clone(),
                                dir,
                            );

                            // Position the nodes in world space using the fixed axis to realign the nodes,
                            // they also need to be adjusted by the position of the chunk in the grid
                            for node in nodes.iter_mut() {
                                node.pos = match dir {
                                    Dir::NORTH => UVec3::new(
                                        node.pos.x + current_chunk.min().x,
                                        node.pos.y + current_chunk.max().y,
                                        node.pos.z + current_chunk.min().z,
                                    ),
                                    Dir::EAST => UVec3::new(
                                        node.pos.y + current_chunk.max().x,
                                        node.pos.x + current_chunk.min().y,
                                        node.pos.z + current_chunk.min().z,
                                    ),
                                    Dir::SOUTH => UVec3::new(
                                        node.pos.x + current_chunk.min().x,
                                        node.pos.y + current_chunk.min().y,
                                        node.pos.z + current_chunk.min().z,
                                    ),
                                    Dir::WEST => UVec3::new(
                                        node.pos.y + current_chunk.min().x,
                                        node.pos.x + current_chunk.min().y,
                                        node.pos.z + current_chunk.min().z,
                                    ),
                                    // TODO: WE NEED TO FIX UP AND DOWN NODE STUFFS
                                    Dir::UP => UVec3::new(
                                        node.pos.x + current_chunk.min().x,
                                        node.pos.y + current_chunk.min().y,
                                        node.pos.z + current_chunk.max().z,
                                    ),
                                    Dir::DOWN => UVec3::new(
                                        node.pos.x + current_chunk.min().x,
                                        node.pos.y + current_chunk.min().y,
                                        node.pos.z + current_chunk.min().z,
                                    ),
                                    _ => panic!("Invalid direction"),
                                }
                            }

                            self.graph.add_nodes(&nodes);
                        }
                    }

                    // Handle ordinal connections if enabled
                    if self.chunk_settings.diagonal_connections {
                        let ordinal_directions = Dir::ordinal();

                        for dir in ordinal_directions {
                            let dir_vec = dir.vector();

                            let nx = x as i32 + dir_vec.0;
                            let ny = y as i32 + dir_vec.1;
                            let nz = z as i32 + dir_vec.2;

                            // Ensure neighbor is within bounds
                            if nx >= 0
                                && nx < x_chunks as i32
                                && ny >= 0
                                && ny < y_chunks as i32
                                && nz >= 0
                                && nz < z_chunks as i32
                            {
                                let neighbor_chunk =
                                    &self.chunks[[nx as usize, ny as usize, nz as usize]];

                                let current_corner = current_chunk.corner(&self.grid, dir);
                                let neighbor_corner =
                                    neighbor_chunk.corner(&self.grid, dir.opposite());

                                if current_corner.is_impassable() || neighbor_corner.is_impassable()
                                {
                                    continue;
                                }

                                let pos = match dir {
                                    Dir::NORTHEAST => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.max().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::SOUTHEAST => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.min().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::SOUTHWEST => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.min().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::NORTHWEST => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.max().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::NORTHEASTUP => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.max().y,
                                        current_chunk.max().z,
                                    ),
                                    Dir::SOUTHEASTUP => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.min().y,
                                        current_chunk.max().z,
                                    ),
                                    Dir::SOUTHWESTUP => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.min().y,
                                        current_chunk.max().z,
                                    ),
                                    Dir::NORTHWESTUP => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.max().y,
                                        current_chunk.max().z,
                                    ),
                                    Dir::NORTHEASTDOWN => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.max().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::SOUTHEASTDOWN => UVec3::new(
                                        current_chunk.max().x,
                                        current_chunk.min().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::SOUTHWESTDOWN => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.min().y,
                                        current_chunk.min().z,
                                    ),
                                    Dir::NORTHWESTDOWN => UVec3::new(
                                        current_chunk.min().x,
                                        current_chunk.max().y,
                                        current_chunk.min().z,
                                    ),
                                    _ => panic!("Invalid direction"),
                                };

                                self.graph.add_node(pos, current_chunk.clone(), Some(dir));
                            }
                        }
                    }
                }
            }
        }
    }

    // Calculates the edge nodes for a given edge in the grid.
    fn calculate_edge_nodes(
        &self,
        start_edge: ArrayView2<NavCell>,
        end_edge: ArrayView2<NavCell>,
        chunk: Chunk,
        dir: Dir,
    ) -> Vec<Node> {
        let mut nodes = Vec::new();

        // Iterate over the start edge and find connections that are walkable
        for ((start_x, start_y), start_cell) in start_edge.indexed_iter() {
            if start_cell.is_impassable() {
                continue;
            }

            let end_x = start_x;
            let mut end_y = start_y;

            if start_cell.is_ramp() {
                end_y = start_y + 1;
            }

            let end_cell = end_edge[[end_x, end_y]].clone();

            // If the end cell is impassable, we can't connect the nodes
            if !end_cell.is_impassable() {
                let pos = UVec3::new(start_x as u32, start_y as u32, 0);

                let node = Node::new(pos, chunk.clone(), Some(dir));
                nodes.push(node);
            }
        }

        // Split nodes into groups of continous nodes
        let continous_with = |start: UVec3, end: UVec3| {
            let x_diff = (start.x as i32 - end.x as i32).abs();
            let y_diff = (start.y as i32 - end.y as i32).abs();

            x_diff <= 1 && y_diff <= 1
        };

        let mut split_nodes = Vec::new();
        let mut current_group: Vec<Node> = Vec::new();

        for node in nodes {
            if let Some(last_node) = current_group.last() {
                if !continous_with(last_node.pos, node.pos) {
                    split_nodes.push(current_group);
                    current_group = Vec::new();
                }
            }
            current_group.push(node);
        }

        if !current_group.is_empty() {
            split_nodes.push(current_group);
        }

        // Find the center of the split nodes
        let mut final_nodes = Vec::new();

        for group in &split_nodes {
            /*if group.len() > 8 {
                final_nodes.push(group.first().unwrap().clone());
                final_nodes.push(group.last().unwrap().clone());
            } else {*/

            let middle = group.len() / 2;
            final_nodes.push(group[middle].clone());
            //}
        }

        if !final_nodes.is_empty() {
            return final_nodes;
        }

        if !self.neighborhood.is_ordinal() {
            return final_nodes;
        }

        let mut ordinal_nodes = Vec::new();

        // If we made it here that means no connection nodes were found, but we allow ordinal connections so let's build those if possible
        for ((start_x, start_y), start_cell) in start_edge.indexed_iter() {
            if start_cell.is_impassable() {
                continue;
            }

            let end_x = start_x;
            let mut end_y = start_y;

            if start_cell.is_ramp() {
                end_y = start_y + 1;
            }

            if end_x > 0 {
                let left = end_edge[[end_x - 1, end_y]].clone();
                if !left.is_impassable() {
                    let pos = UVec3::new(start_x as u32, start_y as u32, 0);

                    let node = Node::new(pos, chunk.clone(), Some(dir));
                    ordinal_nodes.push(node);
                }
            }

            if end_x < end_edge.shape()[0] - 1 {
                let right = end_edge[[end_x + 1, end_y]].clone();
                if !right.is_impassable() {
                    let pos = UVec3::new(start_x as u32, start_y as u32, 0);

                    let node = Node::new(pos, chunk.clone(), Some(dir));
                    ordinal_nodes.push(node);
                }
            }
        }

        ordinal_nodes
    }

    // Connects the internal nodes of each chunk to each other.
    fn connect_internal_chunk_nodes(&mut self) {
        let chunk_size = self.chunk_settings.size as usize;
        let chunk_depth = self.chunk_settings.depth as usize;

        let x_chunks = self.dimensions.x as usize / chunk_size;
        let y_chunks = self.dimensions.y as usize / chunk_size;
        let z_chunks = self.dimensions.z as usize / chunk_depth;

        for x in 0..x_chunks {
            for y in 0..y_chunks {
                for z in 0..z_chunks {
                    // Connect internal nodes

                    let chunk_grid = self.chunks[[x, y, z]].view(&self.grid);
                    let chunk = &self.chunks[[x, y, z]];

                    let nodes = self.graph.nodes_in_chunk(chunk.clone());

                    let mut connections = Vec::new();

                    for node in nodes.iter() {
                        // Collect other nodes positions into an array
                        let other_nodes = nodes
                            .iter()
                            .filter(|other_node| other_node.pos != node.pos)
                            .map(|other_node| other_node.pos)
                            .collect::<Vec<_>>();

                        // Adjust node.pos by the chunk position
                        let start_pos = node.pos - chunk.min();

                        let goals = other_nodes
                            .iter()
                            .map(|pos| *pos - chunk.min())
                            .collect::<Vec<_>>();

                        let paths = dijkstra_grid(
                            &chunk_grid,
                            start_pos,
                            &goals,
                            false,
                            100,
                            &HashMap::new(),
                        );

                        // Readjust position to world space and then connect the nodes
                        for (goal_pos, path) in paths {
                            let start = node.pos;
                            let goal = goal_pos + chunk.min();

                            // Readjust path to world space
                            let path_vec = path
                                .path()
                                .iter()
                                .map(|pos| *pos + chunk.min())
                                .collect::<Vec<_>>();

                            connections.push((
                                start,
                                goal,
                                Path::new(path_vec.clone(), path_vec.len() as u32),
                            ));
                        }
                    }

                    for (node_pos, other_node_pos, path) in connections {
                        self.graph.connect_node(node_pos, other_node_pos, path);
                    }
                }
            }
        }
    }

    // Connects the nodes of adjacent chunks to each other.
    fn connect_adjacent_chunk_nodes(&mut self) {
        let nodes = self.graph.nodes();

        let mut connections = Vec::new();

        for node in nodes {
            // Check all the adjacent positions of the node, taking into account cardinal/ordinal settings
            let directions = if self.chunk_settings.diagonal_connections {
                Dir::all()
            } else {
                Dir::cardinal()
            };

            for dir in directions {
                let dir_vec = dir.vector();

                let nx = node.pos.x as i32 + dir_vec.0;
                let ny = node.pos.y as i32 + dir_vec.1;
                let nz = node.pos.z as i32 + dir_vec.2;

                if let Some(neighbor) = self
                    .graph
                    .node_at(UVec3::new(nx as u32, ny as u32, nz as u32))
                {
                    // Check if neighbor is in a different chunk
                    if node.chunk != neighbor.chunk {
                        let path = Path::from_slice(&[node.pos, neighbor.pos], 1);

                        connections.push((node.pos, neighbor.pos, path));
                    }
                }
            }
        }

        for (node_pos, neighbor_pos, path) in connections {
            self.graph.connect_node(node_pos, neighbor_pos, path);
        }
    }

    /// Returns the `Chunk` for the given position `UVec3` in the grid.
    pub(crate) fn chunk_at_position(&self, pos: UVec3) -> Option<&Chunk> {
        self.chunks.iter().find(|&chunk| {
            pos.x >= chunk.min().x
                && pos.x <= chunk.max().x
                && pos.y >= chunk.min().y
                && pos.y <= chunk.max().y
                && pos.z >= chunk.min().z
                && pos.z <= chunk.max().z
        })
    }

    /// Recursively reroutes a path using astar pathing to further away chunks until a path can be found.
    ///
    /// Useful if local collision avoidance is failing.
    ///
    /// If you're using the plugin pathing systems, you shouldn't need to call this directly.
    ///
    /// # Arguments
    /// * `path` - The [`Path`] to reroute.
    /// * `start` - The start position of the path.
    /// * `goal` - The goal position of the path.
    /// * `blocking` - A map of positions to entities that are blocking the path. Pass `&HashMap::new()` if you're not concerned with collision.
    ///   Pass `&HasMap::new()` if you're not concerned with collision. If using [`crate::plugin::NorthstarPlugin`] you can pass it the [`crate::plugin::BlockingMap`] resource.
    ///   If not, build a [`HashMap<UVec3, Entity>`] with the positions of entities that should be blocking paths.
    /// * `refined` - Whether to use refined pathing or not.
    ///
    /// # Returns
    /// A new rerouted [`Path`] if successful, or `None` if no viable path could be found.
    ///
    pub fn reroute_path(
        &self,
        path: &Path,
        start: UVec3,
        goal: UVec3,
        blocking: &HashMap<UVec3, Entity>,
        refined: bool,
    ) -> Option<Path> {
        reroute_path(self, path, start, goal, blocking, refined)
    }

    /// Checks if a path exists from `start` to `goal` using the fastest algorithm.
    /// Ignores any blocking entities.
    ///
    /// # Arguments
    /// * `start` - The starting position in the grid.
    /// * `goal` - The goal position in the grid.
    /// # Returns
    /// `true` if a path exists, `false` otherwise.
    ///
    pub fn is_path_viable(&self, start: UVec3, goal: UVec3) -> bool {
        pathfind(self, start, goal, &HashMap::new(), false, false).is_some()
    }

    /// Generate an HPA* path from `start` to `goal`.
    ///
    /// # Arguments
    /// * `start` - The starting position in the grid.
    /// * `goal` - The goal position in the grid.
    /// * `blocking` - A map of positions to entities that are blocking the path. Pass `&HashMap::new()` if you're not concerned with collision.
    ///   Pass `&HasMap::new()` if you're not concerned with collision. If using [`crate::plugin::NorthstarPlugin`] you can pass it the [`crate::plugin::BlockingMap`] resource.
    ///   If not, build a [`HashMap<UVec3, Entity>`] with the positions of entities that should be blocking paths.
    /// * `partial` - Whether to allow partial paths (i.e., if the goal is unreachable, return the closest reachable point).
    /// # Returns
    /// A [`Path`] if successful, or `None` if no viable path could be found.
    ///
    pub fn pathfind(
        &self,
        start: UVec3,
        goal: UVec3,
        blocking: &HashMap<UVec3, Entity>,
        partial: bool,
    ) -> Option<Path> {
        pathfind(self, start, goal, blocking, partial, true)
    }

    /// Generate a coarse (unrefined) HPA* path from `start` to `goal`.
    ///
    /// This method is useful for generating paths when the shortest viable path is not required.
    ///
    /// # Arguments
    /// * `start` - The starting position in the grid.
    /// * `goal` - The goal position in the grid.
    /// * `blocking` - A map of positions to entities that are blocking the path. Pass `&HashMap::new()` if you're not concerned with collision.
    ///   Pass `&HasMap::new()` if you're not concerned with collision. If using [`crate::plugin::NorthstarPlugin`] you can pass it the [`crate::plugin::BlockingMap`] resource.
    ///   If not, build a [`HashMap<UVec3, Entity>`] with the positions of entities that should be blocking paths.
    /// * `partial` - Whether to allow partial paths (i.e., if the goal is unreachable, return the closest reachable point).
    /// # Returns
    /// A [`Path`] if successful, or `None` if no viable path could be found.
    ///
    pub fn pathfind_coarse(
        &self,
        start: UVec3,
        goal: UVec3,
        blocking: &HashMap<UVec3, Entity>,
        partial: bool,
    ) -> Option<Path> {
        pathfind(self, start, goal, blocking, partial, false)
    }

    /// Generate a traditional A* path from `start` to `goal`.
    /// This method is useful for generating paths that require precise navigation and CPU cost isn't a concern.
    /// Great for a turn based game where movment cost is important.
    ///
    /// # Arguments
    /// * `start` - The starting position in the grid.
    /// * `goal` - The goal position in the grid.
    /// * `blocking` - A map of positions to entities that are blocking the path. Pass `&HashMap::new()` if you're not concerned with collision.
    ///   Pass `&HasMap::new()` if you're not concerned with collision. If using [`crate::plugin::NorthstarPlugin`] you can pass it the [`crate::plugin::BlockingMap`] resource.
    ///   If not, build a [`HashMap<UVec3, Entity>`] with the positions of entities that should be blocking paths.
    /// * `partial` - Whether to allow partial paths (i.e., if the goal is unreachable, return the closest reachable point).
    /// # Returns
    /// A [`Path`] if successful, or `None` if no viable path could be found.
    ///
    pub fn pathfind_astar(
        &self,
        start: UVec3,
        goal: UVec3,
        blocking: &HashMap<UVec3, Entity>,
        partial: bool,
    ) -> Option<Path> {
        pathfind_astar(
            &self.neighborhood,
            &self.grid.view(),
            start,
            goal,
            blocking,
            partial,
        )
    }

    /// Generate an A* path within a cubic radius around the `start` position.
    /// This can be used to limit an A* search to a confined search area.
    /// You'll want to ensure your radius at least covers the distance to the goal.
    ///
    /// # Arguments
    /// * `start` - The starting position in the grid.
    /// * `goal` - The goal position in the grid.
    /// * `radius` - The radius around the start position to search for a path.
    /// * `blocking` - A map of positions to entities that are blocking the path.
    ///   Pass `&HasMap::new()` if you're not concerned with collision. If using [`crate::plugin::NorthstarPlugin`] you can pass it the [`crate::plugin::BlockingMap`] resource.
    ///   If not, build a [`HashMap<UVec3, Entity>`] with the positions of entities that should be blocking paths.
    /// * `partial` - Whether to allow partial paths (i.e., if the goal is unreachable, return the closest reachable point).
    /// # Returns
    /// A [`Path`] if successful, or `None` if no viable path could be found.
    ///
    pub fn pathfind_astar_radius(
        &self,
        start: UVec3,
        goal: UVec3,
        radius: u32,
        blocking: &HashMap<UVec3, Entity>,
        partial: bool,
    ) -> Option<Path> {
        let min = start.as_ivec3().saturating_sub(IVec3::splat(radius as i32));
        let max = start
            .as_ivec3()
            .saturating_add(IVec3::splat(radius as i32) + IVec3::ONE);

        let grid_shape = self.grid.shape();
        let min = min.max(IVec3::ZERO);
        let max = max.min(IVec3::new(
            grid_shape[0] as i32,
            grid_shape[1] as i32,
            grid_shape[2] as i32,
        ));

        let grid_shape_vec = IVec3::new(
            grid_shape[0] as i32,
            grid_shape[1] as i32,
            grid_shape[2] as i32,
        );
        if !position_in_cubic_window(goal, start.as_ivec3(), radius as i32, grid_shape_vec) {
            return None;
        }

        // Create a subview of the grid
        let view = self.grid.slice(s![
            min.x as usize..max.x as usize,
            min.y as usize..max.y as usize,
            min.z as usize..max.z as usize
        ]);

        // Remap start/goal into local view
        let start_local = (start.as_ivec3() - min).as_uvec3();
        let goal_local = (goal.as_ivec3() - min).as_uvec3();

        // Remap blocking positions into local view
        let blocking_local: HashMap<UVec3, Entity> = blocking
            .iter()
            .filter_map(|(pos, &ent)| {
                let pos_i = pos.as_ivec3();
                if pos_i.cmplt(min).any() || pos_i.cmpge(max).any() {
                    return None;
                }
                Some(((pos_i - min).as_uvec3(), ent))
            })
            .collect();

        // Run pathfinding on the subview
        let result = pathfind_astar(
            &self.neighborhood,
            &view,
            start_local,
            goal_local,
            &blocking_local,
            partial,
        );

        // Convert path result back to global positions
        result.map(|mut path| {
            path.translate_by(min.as_uvec3());
            path
        })
    }
}

#[cfg(test)]
mod tests {
    use bevy::{math::UVec3, platform::collections::HashMap};

    use crate::{
        dir::Dir,
        grid::{
            ChunkSettings, CollisionSettings, Grid, GridInternalSettings, GridSettings,
            GridSettingsBuilder, NavCell, NavSettings, NeighborhoodSettings,
        },
        nav::Nav,
        neighbor::OrdinalNeighborhood3d,
    };

    const GRID_SETTINGS: GridSettings = GridSettings(GridInternalSettings {
        dimensions: UVec3::new(12, 12, 1),
        chunk_settings: ChunkSettings {
            size: 4,
            depth: 1,
            diagonal_connections: false,
        },
        cost_settings: NavSettings {
            default_movement_cost: 1,
            default_impassible: false,
        },
        collision_settings: CollisionSettings {
            enabled: false,
            avoidance_distance: 4,
        },
        neighborhood_settings: NeighborhoodSettings {
            filters: Vec::new(),
        },
    });

    #[test]
    pub fn test_new() {
        let grid = Grid::<OrdinalNeighborhood3d>::new(&GRID_SETTINGS);
        assert_eq!(grid.grid.shape(), [12, 12, 1]);
    }

    #[test]
    pub fn test_edges() {
        let grid_settings = GridSettingsBuilder::new_2d(4, 4)
            .chunk_size(4)
            .enable_diagonal_connections()
            .build();

        let mut grid = Grid::<OrdinalNeighborhood3d>::new(&grid_settings);

        // Fill grid edges with walls
        for x in 0..4 {
            for y in 0..4 {
                if x == 0 || x == 3 || y == 0 || y == 3 {
                    grid.grid[[x, y, 0]] = NavCell::new(Nav::Impassable);
                }
            }
        }

        let chunk = grid.chunks[[0, 0, 0]].clone();

        let mut edges = Vec::new();

        edges.push(chunk.edge(&grid.grid, Dir::NORTH));
        edges.push(chunk.edge(&grid.grid, Dir::EAST));
        edges.push(chunk.edge(&grid.grid, Dir::SOUTH));
        edges.push(chunk.edge(&grid.grid, Dir::WEST));

        for edge in edges {
            for cell in edge.iter() {
                assert!(cell.is_impassable());
            }
        }
    }

    #[test]
    pub fn test_calculate_edge_nodes() {
        let grid = Grid::<OrdinalNeighborhood3d>::new(&GRID_SETTINGS);

        let chunk = grid.chunks.iter().next().unwrap().clone();
        let neighbor_chunk = grid.chunks.iter().nth(1).unwrap().clone();

        let edges = grid.calculate_edge_nodes(
            chunk.edge(&grid.grid, Dir::NORTH),
            neighbor_chunk.edge(&grid.grid, Dir::SOUTH),
            chunk.clone(),
            Dir::NORTH,
        );

        assert_eq!(edges.len(), 1);
    }

    #[test]
    pub fn test_calculate_edge_nodes_3d() {
        let grid_settings = GridSettingsBuilder::new_3d(8, 8, 8)
            .chunk_size(4)
            .chunk_depth(4)
            .enable_diagonal_connections()
            .build();

        let grid = Grid::<OrdinalNeighborhood3d>::new(&grid_settings);

        let chunk = grid.chunks.iter().next().unwrap().clone();
        let neighbor_chunk = grid.chunks.iter().nth(1).unwrap().clone();

        let edges = grid.calculate_edge_nodes(
            chunk.edge(&grid.grid, Dir::NORTH),
            neighbor_chunk.edge(&grid.grid, Dir::SOUTH),
            chunk.clone(),
            Dir::NORTH,
        );

        assert_eq!(edges.len(), 4);
    }

    #[test]
    pub fn test_build_nodes() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        grid.build_nodes();

        let nodes = grid.graph.nodes();

        assert_eq!(nodes[0].pos, UVec3::new(2, 3, 0));
        assert_eq!(nodes[1].pos, UVec3::new(3, 2, 0));

        assert_eq!(nodes[2].pos, UVec3::new(2, 7, 0));
        assert_eq!(nodes[3].pos, UVec3::new(3, 6, 0));
        assert_eq!(nodes[4].pos, UVec3::new(2, 4, 0));

        assert_eq!(nodes[5].pos, UVec3::new(3, 10, 0));
        assert_eq!(nodes[6].pos, UVec3::new(2, 8, 0));

        assert_eq!(grid.graph.node_count(), 24);

        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        let chunk_size = GRID_SETTINGS.0.chunk_settings.size as usize;
        let half_chunk_size = chunk_size / 2;

        for x in 0..(GRID_SETTINGS.0.dimensions.x as usize) {
            for y in 0..(GRID_SETTINGS.0.dimensions.y as usize) {
                if x % chunk_size == 0 && y % half_chunk_size == 0 {
                    grid.grid[[x, y, 0]] = NavCell::new(Nav::Impassable);
                } else {
                    grid.grid[[x, y, 0]] = NavCell::new(Nav::Passable(1));
                }
            }
        }

        grid.build_nodes();

        assert_eq!(grid.graph.node_count(), 36);

        let grid_settings = GridSettingsBuilder::new_2d(12, 12)
            .chunk_size(4)
            .enable_diagonal_connections()
            .build();

        // Test ordinal
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&grid_settings);

        grid.build_nodes();

        assert_eq!(grid.graph.node_count(), 40);
    }

    #[test]
    pub fn test_connect_internal_nodes() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        grid.precompute_neighbors();
        grid.build_nodes();
        grid.connect_internal_chunk_nodes();

        assert_eq!(grid.graph.node_count(), 24);
        assert_eq!(grid.graph.edge_count(), 44);
    }

    #[test]
    pub fn test_connect_adjacent_nodes() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        grid.precompute_neighbors();
        grid.build_nodes();
        grid.connect_adjacent_chunk_nodes();

        let edges = grid
            .graph
            .node_at(UVec3::new(2, 3, 0))
            .unwrap()
            .edges
            .clone();

        assert!(edges.contains_key(&UVec3::new(2, 4, 0)));

        assert_eq!(edges[&UVec3::new(2, 4, 0)].path().len(), 2);
        assert_eq!(edges[&UVec3::new(2, 4, 0)].cost(), 1);

        assert_eq!(grid.graph.edge_count(), 24);
    }

    #[test]
    pub fn test_get_chunk_for_position() {
        let grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);
        let chunk = grid.chunk_at_position(UVec3::new(0, 0, 0)).unwrap();

        assert_eq!(chunk.min(), UVec3::new(0, 0, 0));
        assert_eq!(
            chunk.max(),
            UVec3::new(
                GRID_SETTINGS.0.chunk_settings.size - 1,
                GRID_SETTINGS.0.chunk_settings.size - 1,
                GRID_SETTINGS.0.chunk_settings.depth - 1
            )
        );
    }

    #[test]
    pub fn test_get_all_nodes_in_chunk() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);
        grid.build_nodes();

        let nodes = grid.graph.nodes_in_chunk(grid.chunks[[0, 0, 0]].clone());

        assert_eq!(nodes.len(), 2);
    }

    #[test]
    pub fn test_get_path() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        grid.build();

        let path = grid.pathfind(
            UVec3::new(10, 10, 0),
            UVec3::new(4, 4, 0),
            &HashMap::new(),
            false,
        );
        let raw_path = grid.pathfind_astar(
            UVec3::new(10, 10, 0),
            UVec3::new(4, 4, 0),
            &HashMap::new(),
            false,
        );

        assert!(path.is_some());
        // Ensure start cell is the first cell in the path
        assert_ne!(path.clone().unwrap().path()[0], UVec3::new(10, 10, 0));
        // Ensure end cell is the last cell in the path
        assert_eq!(
            path.clone().unwrap().path().last().unwrap(),
            &UVec3::new(4, 4, 0)
        );

        assert_eq!(path.clone().unwrap().len(), raw_path.unwrap().len());
        assert_eq!(path.unwrap().len(), 6);
    }

    #[test]
    fn test_calculate_edge_nodes_returns_center() {
        let grid_settings = GridSettingsBuilder::new_2d(64, 64).chunk_size(32).build();

        let grid: Grid<OrdinalNeighborhood3d> = Grid::new(&grid_settings);

        let start_edge = grid.chunks[[0, 0, 0]].edge(&grid.grid, Dir::NORTH);
        let end_edge = grid.chunks[[0, 1, 0]].edge(&grid.grid, Dir::SOUTH);

        let nodes = grid.calculate_edge_nodes(
            start_edge,
            end_edge,
            grid.chunks[[0, 0, 0]].clone(),
            Dir::NORTH,
        );

        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].pos, UVec3::new(16, 0, 0));
    }

    #[test]
    fn test_random_grid_path() {
        let width = 128;
        let height = 128;
        let chunk_size = 32;

        // Test a grid with randomized walls, the grid must be solvable
        let grid_settings = GridSettingsBuilder::new_2d(width, height)
            .chunk_size(chunk_size)
            .enable_diagonal_connections()
            .build();

        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&grid_settings);

        for x in 0..width {
            for y in 0..height {
                if (x % 4 == 0)
                    && (y % chunk_size == 0 || y / (chunk_size + chunk_size) == chunk_size)
                {
                    grid.grid[[x as usize, y as usize, 0]] = NavCell::new(Nav::Impassable);
                }

                if (y % 4 == 0)
                    && (x % chunk_size == 0 || x & (chunk_size + chunk_size) == chunk_size)
                {
                    grid.grid[[x as usize, y as usize, 0]] = NavCell::new(Nav::Impassable);
                }
            }
        }

        grid.build();

        let path = grid.pathfind(
            UVec3::new(7, 7, 0),
            UVec3::new(121, 121, 0),
            &HashMap::new(),
            false,
        );

        assert!(path.is_some());
        assert!(!path.unwrap().is_empty());
    }

    #[test]
    fn test_large_3d_path() {
        let grid_settings = GridSettingsBuilder::new_3d(128, 128, 4)
            .chunk_size(16)
            .chunk_depth(2)
            .build();

        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&grid_settings);

        grid.build();
        let path = grid.pathfind(
            UVec3::new(0, 0, 0),
            UVec3::new(31, 31, 3),
            &HashMap::new(),
            false,
        );

        assert!(path.is_some());
    }

    #[test]
    pub fn test_get_astar_path() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        grid.build();

        let path = grid.pathfind_astar(
            UVec3::new(0, 0, 0),
            UVec3::new(10, 10, 0),
            &HashMap::new(),
            false,
        );

        assert!(path.is_some());
        assert_eq!(path.unwrap().len(), 10);
    }

    #[test]
    pub fn test_is_path_viable() {
        let mut grid: Grid<OrdinalNeighborhood3d> = Grid::new(&GRID_SETTINGS);

        // Block off a section of the grid to make sure the path is not viable
        for x in 0..12 {
            grid.set_nav(
                UVec3::new(x, 5, 0),
                Nav::Impassable, // Set as wall
            );
        }

        grid.build();

        let viable = grid.is_path_viable(UVec3::new(0, 0, 0), UVec3::new(10, 0, 0));
        assert!(viable);

        let not_viable = grid.is_path_viable(UVec3::new(0, 0, 0), UVec3::new(8, 8, 0));
        assert!(!not_viable);

        let out_of_bounds_not_viable =
            grid.is_path_viable(UVec3::new(100, 100, 0), UVec3::new(200, 200, 0));
        assert!(!out_of_bounds_not_viable);
    }
}