├── .gitignore ├── .vscode └── launch.json ├── Cargo.lock ├── Cargo.toml ├── README.md ├── assets ├── demo.tiled-project ├── demo.tiled-session ├── demo.tmx ├── demo.tsx ├── demo_128.tmx ├── demo_16.tmx ├── demo_256.tmx ├── tile_0018.png ├── tile_0018_edit.png ├── tile_0019.png ├── tile_0023.png ├── tile_0024.png └── tiles │ ├── tile_0000.png │ ├── tile_0001.png │ ├── tile_0002.png │ ├── tile_0003.png │ ├── tile_0004.png │ ├── tile_0005.png │ ├── tile_0006.png │ ├── tile_0007.png │ ├── tile_0008.png │ ├── tile_0009.png │ ├── tile_0010.png │ ├── tile_0011.png │ ├── tile_0012.png │ ├── tile_0013.png │ ├── tile_0014.png │ ├── tile_0015.png │ ├── tile_0016.png │ ├── tile_0017.png │ ├── tile_0018.png │ ├── tile_0019.png │ ├── tile_0020.png │ ├── tile_0021.png │ ├── tile_0022.png │ ├── tile_0023.png │ └── tile_0024.png ├── benches ├── benches.rs └── profiler.rs ├── examples ├── collision.rs └── demo.rs └── src ├── astar.rs ├── chunk.rs ├── components.rs ├── debug.rs ├── dijkstra.rs ├── dir.rs ├── graph.rs ├── grid.rs ├── lib.rs ├── neighbor.rs ├── node.rs ├── path.rs ├── plugin.rs ├── raycast.rs └── theta.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | _old/ 3 | .DS_Store -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "lldb", 9 | "request": "launch", 10 | "name": "Debug unit tests in library 'bevy_northstar'", 11 | "cargo": { 12 | "args": [ 13 | "test", 14 | "--no-run", 15 | "--lib", 16 | "--package=bevy_northstar" 17 | ], 18 | "filter": { 19 | "name": "bevy_northstar", 20 | "kind": "lib" 21 | } 22 | }, 23 | "args": [], 24 | "cwd": "${workspaceFolder}" 25 | }, 26 | { 27 | "type": "lldb", 28 | "request": "launch", 29 | "name": "Debug demo example", 30 | "cargo": { 31 | "args": [ 32 | "build", 33 | "--example", 34 | "demo", 35 | "--features", 36 | "stats" 37 | ] 38 | }, 39 | "args": [], 40 | "env": { 41 | "CARGO_MANIFEST_DIR": "${workspaceFolder}", 42 | "RUST_BACKTRACE": "1" 43 | }, 44 | "cwd": "${workspaceFolder}" 45 | }, 46 | { 47 | "type": "lldb", 48 | "request": "launch", 49 | "name": "Debug collision example", 50 | "cargo": { 51 | "args": [ 52 | "build", 53 | "--example", 54 | "collision", 55 | "--features", 56 | "stats" 57 | ] 58 | }, 59 | "args": [], 60 | "env": { 61 | "CARGO_MANIFEST_DIR": "${workspaceFolder}", 62 | "RUST_BACKTRACE": "1" 63 | }, 64 | "cwd": "${workspaceFolder}" 65 | } 66 | ] 67 | } -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bevy_northstar" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | authors = ["John Mullins"] 7 | description = "A Bevy plugin for Hierarchical Pathfinding" 8 | homepage = "https://github.com/jtothethree/bevy_northstar" 9 | license = "MIT" 10 | readme = "README.md" 11 | repository = "https://github.com/jtothethree/bevy_northstar" 12 | 13 | categories = ["game-development"] 14 | keywords = ["bevy", "pathfinding", "astar", "hpa", "game"] 15 | exclude = ["assets/*", "res/*"] 16 | 17 | [features] 18 | stats = [] 19 | 20 | [dependencies] 21 | thiserror = "2.0.12" 22 | indexmap = "2.7.1" 23 | ndarray = { version = "0.16.1", features=["rayon"] } 24 | rustc-hash = "2.1.1" 25 | strum = "0.27.1" 26 | slab = "0.4.9" 27 | 28 | [dependencies.bevy] 29 | version = "0.15" 30 | default-features = false 31 | features=["bevy_render", "bevy_gizmos"] 32 | 33 | [dev-dependencies] 34 | bevy = { version = "0.15.3", features=["bevy_dev_tools"] } 35 | bevy_ecs_tilemap = { version = "0.15" } 36 | bevy_ecs_tiled = { version = "0.5.1" } 37 | 38 | [target.'cfg(not(target_os = "windows"))'.dev-dependencies] 39 | criterion = { version = "0.5.1", features = ["html_reports"] } 40 | pprof = { version = "0.14.0", features = ["flamegraph"] } 41 | 42 | rand = "0.8.5" 43 | 44 | [target.'cfg(not(target_os = "windows"))'] 45 | [[bench]] 46 | name = "benches" 47 | harness = false 48 | 49 | [[examples]] 50 | name = "demo" 51 | required-features = ["stats"] 52 | 53 | [[examples]] 54 | name = "collision" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bevy_northstar 2 | ## A 2d/3d hierarchical pathfinding crate for Bevy. 3 | 4 | `bevy_northstar` works by dividing the map into chunks and then calculates nodes based on the entrances between chunks. The nodes are used in pathfinding to get a higher level path that is significantly faster to calculate over long distances. Once the high level path is determined between a start and goal point it's refined to get a more accurate path. 5 | 6 | ### This crate is still a work in progress. 7 | 8 | The crate is currently opinionated in the sense that it's not bring-your-own-grid. That may change in the future. 9 | 10 | ## Demo 11 | cargo run --example demo --features stats --release 12 | 13 | Press P to switch between HPA* and traditional A* 14 | Press C to disable/enable collision 15 | 16 | ![Screenshot 2025-03-30 at 9 34 05 AM](https://github.com/user-attachments/assets/e1ec3d27-3c64-4955-a8d0-afbad95c4107) 17 | 18 | 19 | ## Flags 20 | This crate has the following Cargo features: 21 | 22 | - `stats`: Enables pathfinding benchmarks. Useful to get an idea of how much time it's using per frame. 23 | 24 | ## Features 25 | - **Supports 2D and 3D Tilemaps** – Supports 2d and 3d tilemaps. 26 | 27 | - **Optimized Performance** – Algorithms are heavily benchmarked for efficiency. 28 | 29 | - **Gizmo Debug View** – Debug visuals for verifying the built HPA graph. Pathing debug components to visualize an entities path. 30 | 31 | - **Stress Tests** – 128x128 map with 128 entities to stress test HPA vs A* and collision. A collision example is provided to stress test narrow pathing. 32 | 33 | - **Dynamic Collision & Avoidance** – For moving colliders attaching a simple Blocking marker component is all that's needed. If you use the built in systems the pathing will do a configurable look ahead to see if it can do a fast local A* reroute. 34 | 35 | - **Bevy Systems Integration** – Bevy systems and components for pathfinding as well as collision markers when avoidance paths fail. 36 | 37 | ## Roadmap / TODO 38 | - **Code & Documentation Cleanup** – Refine and document the API. 39 | - **Basic Example** - Create a minimal example demonstrating only the basics usage for the crate. Move existing examples to stress tests. 40 | 41 | 42 | - **Modify & Rebuild Grid Chunks Dynamically** – Support updates to the grid after it’s been built. 43 | - **Pseudo-3D Tilemap Support** – Add support for features like stairs and ramps without full 3D calculations. 44 | - **Parallelized Graph Building** – Speed up grid/graph construction using parallelism. 45 | - **Add Support For Multiple HPA Levels** – Implement multiple hierarchical levels for improved efficiency. 46 | - **Optimize 3D Performance** – 3d grids appear to take a performance hit higher than expected currently. 47 | 48 | ## Assets credits 49 | - [kenny-minimap-pack](https://kenney.nl/assets/minimap-pack): an 8x8 tileset from [Kenney](https://kenney.nl/), licensed under [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) 50 | 51 | 52 | ## Thanks 53 | Thanks to the following crates and blogs that have been used as references 54 | * https://github.com/evenfurther/pathfinding 55 | * https://github.com/mich101mich/hierarchical_pathfinding 56 | * https://alexmelenchon.github.io/Hierarchial-Pathfinding-Research/ 57 | -------------------------------------------------------------------------------- /assets/demo.tiled-project: -------------------------------------------------------------------------------- 1 | { 2 | "automappingRulesFile": "", 3 | "commands": [ 4 | ], 5 | "compatibilityVersion": 1100, 6 | "extensionsPath": "extensions", 7 | "folders": [ 8 | "." 9 | ], 10 | "properties": [ 11 | ], 12 | "propertyTypes": [ 13 | { 14 | "color": "#ffa0a0a4", 15 | "drawFill": true, 16 | "id": 1, 17 | "members": [ 18 | { 19 | "name": "ty", 20 | "propertyType": "TileType", 21 | "type": "string", 22 | "value": "Floor" 23 | } 24 | ], 25 | "name": "TileInfos", 26 | "type": "class", 27 | "useAs": [ 28 | "property", 29 | "map", 30 | "layer", 31 | "object", 32 | "tile", 33 | "tileset", 34 | "wangcolor", 35 | "wangset", 36 | "project" 37 | ] 38 | }, 39 | { 40 | "id": 2, 41 | "name": "TileType", 42 | "storageType": "string", 43 | "type": "enum", 44 | "values": [ 45 | "Floor", 46 | "Wall" 47 | ], 48 | "valuesAsFlags": false 49 | } 50 | ] 51 | } 52 | -------------------------------------------------------------------------------- /assets/demo.tiled-session: -------------------------------------------------------------------------------- 1 | { 2 | "Map/SizeTest": { 3 | "height": 4300, 4 | "width": 2 5 | }, 6 | "activeFile": "", 7 | "expandedProjectPaths": [ 8 | ], 9 | "fileStates": { 10 | "demo.tmx": { 11 | "scale": 0.19, 12 | "selectedLayer": 0, 13 | "viewCenter": { 14 | "x": 2047.3684210526312, 15 | "y": 2047.368421052632 16 | } 17 | }, 18 | "demo.tsx": { 19 | "dynamicWrapping": true, 20 | "scaleInDock": 6.4479, 21 | "scaleInEditor": 5.78 22 | }, 23 | "demo_128.tmx": { 24 | "scale": 1.4861, 25 | "selectedLayer": 0, 26 | "viewCenter": { 27 | "x": 110.3559652782451, 28 | "y": 275.8899131956127 29 | } 30 | } 31 | }, 32 | "last.imagePath": "/Users/john/Projects/bevy_northstar/assets/tiles", 33 | "last.worldFilePath": "/Users/john/Projects/bevy_northstar/assets", 34 | "loadedWorlds": [ 35 | "/Users/john/Projects/bevy_northstar/assets/world.world" 36 | ], 37 | "map.fixedSize": false, 38 | "map.height": 128, 39 | "map.lastUsedFormat": "tmx", 40 | "map.tileHeight": 8, 41 | "map.tileWidth": 8, 42 | "map.width": 128, 43 | "openFiles": [ 44 | ], 45 | "project": "demo.tiled-project", 46 | "property.type": "TileInfos", 47 | "recentFiles": [ 48 | "demo.tsx", 49 | "demo.tmx", 50 | "demo_128.tmx" 51 | ], 52 | "tileset.lastUsedFormat": "tsx", 53 | "tileset.type": 1 54 | } 55 | -------------------------------------------------------------------------------- /assets/demo.tsx: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /assets/demo_16.tmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3, 7 | 6,15,15,15,15,15,15,15,15,15,15,15,15,15,15,8, 8 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 9 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 10 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 11 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 12 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 13 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 14 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 15 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 16 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 17 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 18 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 19 | 6,15,23,15,23,15,23,15,15,23,15,23,15,23,15,8, 20 | 6,15,15,15,15,15,15,15,15,15,15,15,15,15,15,8, 21 | 11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /assets/tile_0018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tile_0018.png -------------------------------------------------------------------------------- /assets/tile_0018_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tile_0018_edit.png -------------------------------------------------------------------------------- /assets/tile_0019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tile_0019.png -------------------------------------------------------------------------------- /assets/tile_0023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tile_0023.png -------------------------------------------------------------------------------- /assets/tile_0024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tile_0024.png -------------------------------------------------------------------------------- /assets/tiles/tile_0000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0000.png -------------------------------------------------------------------------------- /assets/tiles/tile_0001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0001.png -------------------------------------------------------------------------------- /assets/tiles/tile_0002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0002.png -------------------------------------------------------------------------------- /assets/tiles/tile_0003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0003.png -------------------------------------------------------------------------------- /assets/tiles/tile_0004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0004.png -------------------------------------------------------------------------------- /assets/tiles/tile_0005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0005.png -------------------------------------------------------------------------------- /assets/tiles/tile_0006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0006.png -------------------------------------------------------------------------------- /assets/tiles/tile_0007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0007.png -------------------------------------------------------------------------------- /assets/tiles/tile_0008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0008.png -------------------------------------------------------------------------------- /assets/tiles/tile_0009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0009.png -------------------------------------------------------------------------------- /assets/tiles/tile_0010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0010.png -------------------------------------------------------------------------------- /assets/tiles/tile_0011.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0011.png -------------------------------------------------------------------------------- /assets/tiles/tile_0012.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0012.png -------------------------------------------------------------------------------- /assets/tiles/tile_0013.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0013.png -------------------------------------------------------------------------------- /assets/tiles/tile_0014.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0014.png -------------------------------------------------------------------------------- /assets/tiles/tile_0015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0015.png -------------------------------------------------------------------------------- /assets/tiles/tile_0016.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0016.png -------------------------------------------------------------------------------- /assets/tiles/tile_0017.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0017.png -------------------------------------------------------------------------------- /assets/tiles/tile_0018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0018.png -------------------------------------------------------------------------------- /assets/tiles/tile_0019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0019.png -------------------------------------------------------------------------------- /assets/tiles/tile_0020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0020.png -------------------------------------------------------------------------------- /assets/tiles/tile_0021.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0021.png -------------------------------------------------------------------------------- /assets/tiles/tile_0022.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0022.png -------------------------------------------------------------------------------- /assets/tiles/tile_0023.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0023.png -------------------------------------------------------------------------------- /assets/tiles/tile_0024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JtotheThree/bevy_northstar/128dca03dcb569c2454b444d5852a425a4e9b395/assets/tiles/tile_0024.png -------------------------------------------------------------------------------- /benches/benches.rs: -------------------------------------------------------------------------------- 1 | use bevy::{math::UVec3, utils::hashbrown::HashMap}; 2 | use criterion::{criterion_group, criterion_main, Criterion}; 3 | 4 | use bevy_northstar::{grid::{Grid, GridSettings}, prelude::{OrdinalNeighborhood, OrdinalNeighborhood3d}}; 5 | 6 | mod profiler; 7 | 8 | 9 | fn benchmarks(c: &mut Criterion) { 10 | let mut group = c.benchmark_group("pathfinding"); 11 | 12 | let grid_settings = GridSettings { 13 | width: 64, 14 | height: 64, 15 | depth: 1, 16 | chunk_size: 32, 17 | chunk_depth: 1, 18 | chunk_ordinal: false, 19 | default_cost: 1, 20 | default_wall: false, 21 | }; 22 | 23 | let mut grid: Grid = Grid::new(&grid_settings); 24 | 25 | group.sample_size(10); 26 | 27 | group.bench_function("build_grid_64x64", |b| b.iter(|| 28 | grid.build() 29 | )); 30 | 31 | group.bench_function("pathfind_64x64", |b| b.iter(|| 32 | grid.get_path(UVec3::new(0, 0, 0), UVec3::new(63, 63, 0), &HashMap::new(), false) 33 | )); 34 | 35 | group.bench_function("raw_pathfind_64x64", |b| b.iter(|| 36 | grid.get_astar_path(UVec3::new(0, 0, 0), UVec3::new(63, 63, 0), &HashMap::new(), false) 37 | )); 38 | 39 | let grid_settings = GridSettings { 40 | width: 512, 41 | height: 512, 42 | depth: 1, 43 | chunk_depth: 1, 44 | chunk_size: 32, 45 | chunk_ordinal: false, 46 | default_cost: 1, 47 | default_wall: false, 48 | }; 49 | 50 | let mut grid: Grid = Grid::new(&grid_settings); 51 | 52 | group.bench_function("build_grid_512x512", |b| b.iter(|| 53 | grid.build() 54 | )); 55 | 56 | group.bench_function("pathfind_512x512", |b| b.iter(|| 57 | grid.get_path(UVec3::new(0, 0, 0), UVec3::new(511, 511, 0), &HashMap::new(), false) 58 | )); 59 | 60 | group.bench_function("raw_pathfind_512x512", |b| b.iter(|| 61 | grid.get_astar_path(UVec3::new(0, 0, 0), UVec3::new(511, 511, 0), &HashMap::new(), false) 62 | )); 63 | 64 | 65 | let grid_settings = GridSettings { 66 | width: 128, 67 | height: 128, 68 | depth: 4, 69 | chunk_depth: 1, 70 | chunk_size: 16, 71 | chunk_ordinal: false, 72 | default_cost: 1, 73 | default_wall: false, 74 | }; 75 | 76 | let mut grid: Grid = Grid::new(&grid_settings); 77 | 78 | group.bench_function("build_grid_128x128x4", |b| b.iter(|| 79 | grid.build() 80 | )); 81 | 82 | group.bench_function("pathfind_128x128x4", |b| b.iter(|| 83 | grid.get_path(UVec3::new(0, 0, 0), UVec3::new(127, 127, 3), &HashMap::new(), false) 84 | )); 85 | 86 | group.bench_function("raw_pathfind_128x128x4", |b| b.iter(|| 87 | grid.get_astar_path(UVec3::new(0, 0, 0), UVec3::new(127, 127, 3), &HashMap::new(), false) 88 | )); 89 | 90 | group.finish(); 91 | } 92 | 93 | criterion_group!{ 94 | name = benches; 95 | config = Criterion::default().with_profiler(profiler::FlamegraphProfiler::new(100)).sample_size(10); 96 | targets = benchmarks 97 | } 98 | 99 | criterion_main!(benches); 100 | 101 | -------------------------------------------------------------------------------- /benches/profiler.rs: -------------------------------------------------------------------------------- 1 | 2 | 3 | use std::{fs::File, os::raw::c_int, path::Path}; 4 | 5 | use criterion::profiler::Profiler; 6 | use pprof::ProfilerGuard; 7 | 8 | 9 | /// Small custom profiler that can be used with Criterion to create a flamegraph for benchmarks. 10 | /// Also see [the Criterion documentation on this][custom-profiler]. 11 | /// 12 | /// ## Example on how to enable the custom profiler: 13 | /// 14 | /// ``` 15 | /// mod perf; 16 | /// use perf::FlamegraphProfiler; 17 | /// 18 | /// fn fibonacci_profiled(criterion: &mut Criterion) { 19 | /// // Use the criterion struct as normal here. 20 | /// } 21 | /// 22 | /// fn custom() -> Criterion { 23 | /// Criterion::default().with_profiler(FlamegraphProfiler::new()) 24 | /// } 25 | /// 26 | /// criterion_group! { 27 | /// name = benches; 28 | /// config = custom(); 29 | /// targets = fibonacci_profiled 30 | /// } 31 | /// ``` 32 | /// 33 | /// The neat thing about this is that it will sample _only_ the benchmark, and not other stuff like 34 | /// the setup process. 35 | /// 36 | /// Further, it will only kick in if `--profile-time