Why Do StarCraft’s Dragoons Move So Stupidly?

In this blog post, we’ll examine the pathfinding algorithm used for unit movement in StarCraft and, in particular, explore why Dragoons don’t follow the shortest path.

 

There are probably very few people who haven’t played “StarCraft,” the national game that was first released by Blizzard and became a massive sensation in South Korea. Although many popular games have emerged since then and the official StarLeague has ended, StarCraft will forever remain in the hearts of many. However, there is one aspect of StarCraft that’s particularly hard to overlook: the Dragoon, which simply won’t move the way you want it to. Any player who has ever played as the Protoss has likely experienced a Dragoon suddenly turning around and heading in the opposite direction while descending a narrow slope. While Dragoons are particularly clueless, other ground units also often fail to follow the shortest path. Most people would nod in agreement that StarCraft’s pathfinding system is rather unreliable.
Surprisingly, however, StarCraft’s pathfinding algorithm—which only appears to be sloppy—isn’t actually that bad. In this article, let’s explore the clever algorithm StarCraft uses to determine unit paths and find out why Dragoons are so clueless.
One of the most famous pathfinding algorithms in computer science is Dijkstra’s algorithm. Although it’s one of the most famous algorithms, its principle is simple: repeatedly visit all nearby tiles until you reach the destination! The Dijkstra algorithm begins with a territory consisting of a single starting tile. In the first image, you can see that only the starting tile is marked yellow. From there, it sequentially visits—that is, captures—tiles near the territory, gradually expanding its area.
More specifically, the algorithm proceeds by storing a value (let’s call it d) representing the shortest distance from the starting point to each tile. Before running the algorithm, initialize the d value of the starting point to 0 and the d values of all other points to ∞. Our ultimate goal is to determine the d value of the destination tile—that is, the shortest distance to the destination tile.
d(v) = the shortest distance from the starting tile to tile v
The d value for each tile is updated as the yellow occupied area expands. Since the occupied area expands by capturing neighboring tiles, all tiles adjacent to the yellow tiles are potential candidates for capture. In the second figure, the candidate tiles for occupation are marked in blue. The d-value for each candidate tile is updated based on its distance from the occupied area. The Dijkstra algorithm expands the occupied area by occupying the candidate tile with the smallest d-value among those updated in this way. If there are multiple candidate tiles with the smallest d-value, all of them are occupied.
Since the `d` value of an occupied candidate is the shortest distance from the starting point, if the destination is occupied, we have successfully determined the shortest distance to the destination. If we also record the path taken while occupying the candidates, we can determine the shortest path as well.
However, the speed at which a path is found cannot be considered particularly fast. This is because Dijkstra’s algorithm captures all surrounding tiles without considering the destination. As shown in the final figure, by the time the destination is captured, all other points have already been captured as well. Repeating the process of capturing all tiles closer than the destination every time a path is sought becomes increasingly time-consuming as the number of tiles increases. For example, a StarCraft map is typically a square measuring 128 units in height and 128 units in width, consisting of a total of 16,384 tiles. If a path must be found for a unit to travel from one end of the map to the other, all 16,384 tiles must be searched. When multiple units—rather than just one—are moving, the path must be re-searched every time units collide, which could be a significant burden for real-time computation on computers of that era. This inefficiency arises because Dijkstra’s algorithm searches all possible paths. However, if you have a rough idea of where the destination is, there is a way to significantly reduce the number of paths that need to be searched.
The A’ (A-star) algorithm addresses this inefficiency of Dijkstra’s algorithm. While the original Dijkstra’s algorithm used only the distance d from the starting tile as the criterion for selecting the next tile to occupy, the A’ algorithm additionally uses a function h to estimate the remaining distance, selecting the tile where d(v) + h(v) is minimized.

h(v) = Estimated distance from tile v to the destination tile

Typically, the function h is defined as one that can be calculated easily and quickly, even if it is not entirely accurate. It is most commonly used because the straight-line distance to the destination is easy to determine. As in the example mentioned earlier, when all neighboring tiles are at the same distance, h(v) can be calculated using an expression such as h(v) = max(|vₓ − destinationₓ|, |vᵧ − destinationᵧ|).
Both figures show the d and h values for each tile. First, in the left figure, the Dijkstra algorithm visits all 16 points, whereas the A’ algorithm visits only four tiles! Even when obstacles are present—which slightly increases the number of possible paths to explore—the A’ algorithm still examines a significantly smaller number of paths compared to the Dijkstra algorithm. Even with only approximate location information about the destination, the efficiency of the search can be dramatically improved.
If it is guaranteed that the estimated remaining distance, h(v), is always equal to or less than the actual remaining distance, then the path found by the A’ algorithm is guaranteed to be the shortest. This is because the value of d(v) + h(v) is always equal to or less than the actual path length. Therefore, the length of the path to the destination found by the A’ algorithm is shorter than or equal to the shortest path found by Dijkstra’s algorithm, which uses only d(v). However, since it cannot be shorter than the shortest path, the A’ algorithm can find a path of the same length as the shortest path.
However, when obstacles extensively block the straight-line path from the start tile to the destination tile, the search range of the A’ algorithm also becomes as wide as that of the Dijkstra algorithm. The first of the three figures illustrates the search range of the Dijkstra algorithm, while the second shows that of the A’ algorithm. Although the search range is smaller than that of the Dijkstra algorithm, it is disappointing to see how much larger it has become compared to when there are no obstacles. This is because the algorithm must explore all plausible paths to guarantee that the found path is the shortest one. If we were willing to forgo the guarantee that the path is the shortest, but could find plausible paths much faster, this would be far more appealing for game development.
To achieve this, one approach is to select the tile with the smallest value of `d(v) + k · h(v)`—where `h(v)` is the estimated path function multiplied by a constant—as the criterion for choosing a cell to occupy. In other words, during the pathfinding process, the estimated remaining distance is considered k times more important than the actual distance. It is known that, in the worst-case scenario, the path found this way may be k times longer than the shortest path. However, the search range is significantly reduced. The third figure illustrates the search range of the A’ algorithm when k = 5. While the A’ algorithm visited 198 points, when the estimated remaining distance was weighted 5 times more heavily, it searched only 42 points; yet, the resulting path lengths showed little difference.
Ultimately, the fact that units in StarCraft do not take the shortest path is a trade-off made to speed up path calculation. Additionally, since a Dragoon is slightly larger than the tile size used to calculate movement paths in StarCraft, it cannot move within a single tile and must occupy at least four tiles. Because any tile even slightly occupied by a unit is considered an obstacle and is excluded from pathfinding, the Dragoon is inevitably forced to take a longer route.
In the future, if you see a Dragoon wandering aimlessly while moving, rather than getting frustrated, try to approach the game with a more understanding mindset based on the knowledge that “since the path was calculated using the A’ algorithm with a higher weight on estimated distance, it cannot follow the shortest path.”

 

About the author

Writer

I'm a "Cat Detective" I help reunite lost cats with their families.
I recharge over a cup of café latte, enjoy walking and traveling, and expand my thoughts through writing. By observing the world closely and following my intellectual curiosity as a blog writer, I hope my words can offer help and comfort to others.