r/howdidtheycodeit • • Apr 22 '23
Answered How did dwarf fortress do pathfinding with z levels
I know A* is the base algorithm but I wonder how they made it work with going up/down
51
Upvotes
r/howdidtheycodeit • • Apr 22 '23
I know A* is the base algorithm but I wonder how they made it work with going up/down
77
u/Putnam3145 IndieDev Apr 22 '23 edited Aug 03 '23
Hi, DF's second programmer. All the other answers are pretty basic but you're probably wondering what the heuristic function is and I'll just tell you: it's octile distance, which you can prooobably find on google (sorry, on mobile). You can generalize this to any n-dimensional grid, it is simply max(dx,dy,dz,...)+0.414*min(dx,dy,dz,...), which always underestimates (thus admissible) and closer to the true distance than simple chebyshev (that's max of all), so reasonably greedy
88
u/jonatansan Apr 22 '23
It's rather simple. If you are used to A* in 2D, you simply add a third dimension. So instead of a grid
[x][y], you have a grid[x][y][z]. Exploring neighbors simply add a couple of checks for "up" and "down" to the classic "north, east, west, south".Actually, A* can be generalize to any number of dimension and be used for way more complicated problems than pathfinding in a grid.
13
u/Soundless_Pr Apr 22 '23
A* can be generalized to work with any shape graph, not just a 2d grid. Just add connections between nodes on different z levels as long as they're not obstructed
20
u/Craptastic19 Apr 22 '23
Stairs probably add a link between nodes on different levels, which is then evaluated like any other node. Just a hyper casual guess though.
7
u/Kejalol Apr 22 '23
Being in 3 dimensions (or any number of dimensions really) doesn't change the math, it just makes it harder for humans to visualize. Once you start writing pathfinding algorithms yourself it'll be easier to see why adding more dimensions doesn't really change anything.
1
u/Tekuzo Apr 22 '23
https://zackbellgames.com/2015/10/02/faraway-dynamic-way-point-trails/
1
u/Blecki Apr 22 '23
Dwarfcorp does a* on a network of generic nodes that include not just the grid but jumping, climbing ladders, riding mine carts, and more.
0
u/MtBoaty Apr 22 '23
I sometimes wonder if a version of djikstra could work better
-2
u/icebeat Apr 22 '23
Do they have path finder?