Sorry for the double-post, but here's a demo of what happens if the a-star heuristic is too low:
This example uses a real cost per tile of 2, and a heuristic in the pathfinder of only 1 per tile. It's also only 2D, and based around 4-direction movement, instead of 8. Despite that, its point is valid.
In the squares in the images the first number is the distance travelled, and the second is the heuristic estimate to the end of the path. At each step, the tile with the lowest total of both numbers added together is expanded. In the case of a tie, the tile with the lowest heuristic to the end is expanded.
| - Start 5 squares away from end. 0 travelled, 5 heuristic
- Expand start square. Three of the squares around it increase the total cost (8), one is closer to the end, but still at a higher total cost than the original heuristic (6)
- Expand the 2,4 square. The squares around it have total costs of 7 and 9
- Expand the 4,3 square. The squares around it have total costs of 8 and 10
- Expand the 6,2 square (it ties total cost with the 2,6 squares, but has a lower heuristic, so takes priority). The squares around it have total costs of 9 and 11
- Here's where it starts going wrong. The 8,1 square is the obvious next step in the path to us, but to the algorithm, the 2,6 squares have the lowest total cost, so they're all tried next.
- Then 8,1
- And again mistakes, as the 4,5 squares have a total cost of 9, vs the 10 of the end itself. If the squares to the right of the 4,5 squares had costs of 1, it would indeed be a better path. But they don't.
- Finally, we get to the end. The end path is a perfect straight line, but even in such a small example, we search twice as many squares as we need to.
- This is a 10-square example, starting at green and ending at red. In this case the number of tiles searched balloons to over 4x what's necessary. And it only gets worse at longer distances and when obstacles are added...
|
So there you go. An explanation of why setting the normal path cost to 1 would help, assuming DF uses 1 as its heuristic.