Posted October 27, 2024 by deuxb
Pathfinding problem in math is almost 100 years old, so you'd expect everything related to it to be perfected by now. Indeed, there are dozens of very efficient algorithms for finding the shortest path from A to B, but what if you don't need the shortest one?
Octohill is a ski resort simulator where my NPCs do enjoy the ride, so they should take longer routes and small detours to get the satisfaction they're looking for. While this sounds like a small addition, this actually makes the problem NP-hard (meaning that's impossible to solve it fast unlike the shortest path thing). These detours are called negative cycles in graph theory and are a real pain to deal with.
My initial approach was to ignore the problem and try to use the classical algorithms hoping it would work, and up to some moment it did (that's how the game version 0.2 makes the pathfinding, or what is still used for vehicles as in the cover image):
But when the ski resort gets big enough AI starts to make some very stupid decisions because of the good (cyclic) paths not being considered, so in the end I've decided to do a major rework.
After some experiments with making this algorithms more complex I realised a fundamental truth: real people don't test millions of paths analyzing all the mountain surface in their head when they decide which piste to try next. They only quickly check the high level touristy trails map (often inaccurate...) and what's right ahead of them.
Now I'm using a more natural approach:
As a result, a resort with about 800 skiers on slopes runs on x5 speed without any delays and the flow looks quite natural without them getting lost. The new pathfinding will be a part of the next major Octohill update which is coming soon, together with the dynamical construction.