This article has been translated into Spanish and French. Other translations are welcome.
While it is easy once you get the hang of it, the A* (pronounced A-star) algorithm can be complicated for beginners. There are plenty of articles on the web that explain A*, but most are written for people who understand the basics already. This one is for the true beginner.
虽然掌握了A*(读作A-star)算法就认为它很容易,对于初学者来说,它却是复杂的。网上有很多解释A*的文章,不过大多数是写给理解了基础知识的人。本文是给初学者的。
This article does not try to be the definitive work on the subject. Instead it describes the fundamentals and prepares you to go out and read all of those other materials and understand what they are talking about. Links to some of the best are provided at the end of this article, under Further Reading.
本文并不想成为关于这个主题的权威论文。实际上它讨论了基础知识并为你做一些准备,以便进一步阅读其他资料和理解它们讨论的内容。本文的后面列出了几个最好的文章,在进阶阅读中。
Finally, this article is not program-specific. You should be able to adapt what's here to any computer language. As you might expect, however, I have included a link to a sample program at the end of this article. The package contains two versions: one in C++ and one in Blitz Basic. It also contains executables if you just want to see A* in action.
最后,本文不是编程规范的。你应该能够改写这里的东西到任何计算机语言上。如你所期望的,同时,我包含了一个示例程序的链接,在本文后面结束的地方。这个程序包有两个版本:一个是C++,另一个用Blitz Basic语言编写。如果你只是想看看A*的行为,里面也含有可执行exe文件。
But we are getting ahead of ourselves. Let's start at the beginning ...
但我们要超越自己。让我们从头开始 ...
介绍:搜索区域Introduction: The Search Area
Let's assume we have someone who wants to get from point A to point B and that a wall separates the two points. This is illustrated in the graphic found below, with green being the starting point A, red being the ending point B, and the blue filled squares being the wall in between.
我们假设某人想从A点到达B点,一堵墙把它们分开了。如下图所示,绿色是开始点A,红色是结束点B,而蓝色填充的方块是中间的墙。
[图 1][Figure 1]
The first thing you should notice is that we have divided our search area into a square grid. Simplifying the search area, as we have done here, is the first step in pathfinding. This particular method reduces our search area to a simple two dimensional array. Each item in the array represents one of the squares on the grid, and its status is recorded as walkable or unwalkable. The path is found by figuring out which squares we should take to get from A to B. Once the path is found, our person moves from the center of one square to the center of the next until the target is reached.
你应该注意的第一件事是,我们把搜索区域分割成了方块的格子。简化搜索区域,如你目前完成的那样,这是寻路的第一步。这个特殊方法把搜索区域简化成了一个二维数组。数组的每一个项目代表了格子里的一个方块,它的状态记录成可行走和不可行走。通过计算出从A到达B应该走哪些方块,就找到了路径。一旦路径找到,我们的人从一个方块的中心移动到下一个方块的中心,直到抵达目标。
These center points are called "nodes". When you read about pathfinding elsewhere, you will often see people discussing nodes. Why not just refer to them as squares? Because it is possible to divide up your pathfinding area into something other than squares. They could be rectangular, hexagons, or any shape, really. And the nodes could be placed anywhere within the shapes ? in the center or along the edges, or anywhere else. We are using this system, however, because it is the simplest.
这些中心点称作“节点”。当你在其它地方阅读关于寻路时,你将经常发现人们讨论节点。为什么不直接把它们认为是方块呢?因为有可能你要把你的寻路区域以非方块的东西来分割。它们可能是矩形,六角形,或任何形状,真的。而节点可以放到形状内的任何位置。在中心,或者沿着边缘,或其它地方。然而我们使用这个系统,因为它最简单。
开始搜索Starting the Search
Once we have simplified our search area into a manageable number of nodes, as we have done with the grid layout above, the next step is to conduct a search to find the shortest path. In A* pathfinding, we do this by starting at point A, checking the adjacent squares, and generally searching outward until we find our target.
一旦我们把搜索区域简化成了可以管理的大量节点,就象我们上面所做的那样采用格子的布局,下一步就是引导一个搜索来找出最短路径。在A*寻路的做法,我们从开始点A做起,检查它周围的方块,并且向外普通的搜索,直到找到目标。
We begin the search by doing the following:
我们这样开始搜索:
Begin at the starting point A and add it to an "open list" of squares to be considered. The open list is kind of like a shopping list. Right now there is just one item on the list, but we will have more later. It contains squares that might fall along the path you want to take, but maybe not. Basically, this is a list of squares that need to be checked out.
从开始点A起,添加它到待考虑的方块的“开放列表”。开放列表有点象购物列表。此时只有一个项目在里面,但很快我们会得到更多。它包含了你可能取用的沿途的方块,也可能不用它。基本上,这是需要检查的方块的列表。
Look at all the reachable or walkable squares adjacent to the starting point, ignoring squares with walls, water, or other illegal terrain. Add them to the open list, too. For each of these squares, save point A as its "parent square". This parent square stuff is important when we want to trace our path. It will be explained more later.
观察开始点邻近的所有可到达或可行走的方块,忽略有墙,水或其他非法地形的方块。也把它们添加到开放列表。对每一个方块,保存A 点作为它们的“父亲”。这个父亲方块在跟踪路径时非常重要。后面会更多的解释。
Drop the starting square A from your open list, and add it to a "closed list" of squares that you don't need to look at again for now.
把开始方块A从开放列表中取出,并放到“封闭列表”内,它是所有现在不需要再关注的方块的列表。
At this point, you should have something like the following illustration. In this diagram, the dark green square in the center is your starting square. It is outlined in light blue to indicate that the square has been added to the closed list. All of the adjacent squares are now on the open list of squares to be checked, and they are outlined in light green. Each has a gray pointer that points back to its parent, which is the starting square.
在此,你应该有了类似下图的东西。在这个图中,中间的深绿色的方块就是开始方块。它有浅蓝色的外框,表示它被添加到封闭列表了。所有的相邻方块现在都进入要检查的方块的开放列表中了,它们有浅绿的外框。每一个都有灰色的指针指回它的父亲,它就是开始方块。
[图 2][Figure 2]
Next, we choose one of the adjacent squares on the open list and more or less repeat the earlier process, as described below. But which square do we choose? The one with the lowest F cost.
下一步,我们从开放列表中,选出一个相邻的方块,然后多多少少重复早先的过程,下面会说到。但是我们选择哪一个呢?具有最小F值的那个。
路径排序Path Scoring
The key to determining which squares to use when figuring out the path is the following equation:
找到形成路径的方块的关键是下面的等式:
F = G + H
where
这里
G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there.
G = 从开始 点A到格子中给定方块的移动代价,沿着到达该方块而生成的那个路径。
H = the estimated movement cost to move from that given square on the grid to the final destination, point B. This is often referred to as the heuristic, which can be a bit confusing. The reason why it is called that is because it is a guess. We really don't know the actual distance until we find the path, because all kinds of stuff can be in the way (walls, water, etc.). You are given one way to calculate H in this tutorial, but there are many others that you can find in other articles on the web.
H = 从格子中给定 的方块到最终目标 B点的评估移动代价。这种方式通常称作试探法,有点让人混乱。因为这是一个猜测,所以得到这个称谓。在找到路径之前,我们真的不知道实际的距离,因为途中有各种东西(墙,水,等等)。在本教程里给出了一种计算H的方法,但在网上你能找到很多其他的文章。
Our path is generated by repeatedly going through our open list and choosing the square with the lowest F score. This process will be described in more detail a bit further in the article. First let's look more closely at how we calculate the equation.
我们需要的路径是这样生成的:反复的遍历开放列表,选择具有最小F值的方块。这个过程在本文稍后会详细描述。先让我们看看如何计算前面提到的等式。
As described above, G is the movement cost to move from the starting point to the given square using the path generated to get there. In this example, we will assign a cost of 10 to each horizontal or vertical square moved, and a cost of 14 for a diagonal move. We use these numbers because the actual distance to move diagonally is the square root of 2 (don't be scared), or roughly 1.414 times the cost of moving horizontally or vertically. We use 10 and 14 for simplicity's sake. The ratio is about right, and we avoid having to calculate square roots and we avoid decimals. This isn't just because we are dumb and don't like math. Using whole numbers like these is a lot faster for the computer, too. As you will soon find out, pathfinding can be very slow if you don't use short cuts like these.
如上所述,G是经由到达它的路径,从开始点到给定方块的移动代价。在本例中,我们为每个水平/垂直的移动指定代价为10,而斜角的移动代价为14。我们使用这些值,因为斜角移动的实际距离是2的平方根(别害怕),或者大概1.414倍的水平/垂直的移动代价。出于简化的目的使用了10和14。比例大致是正确的,而我们却避免了方根和小数的计算。倒不是我们没有能力做或者不喜欢数学。使用这些数字也能让计算更快一些。以后你就会发现,如果不使用这些技巧,寻路的计算非常慢。
Since we are calculating the G cost along a specific path to a given square, the way to figure out the G cost of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square. The need for this method will become apparent a little further on in this example, as we get more than one square away from the starting square.
既然我们沿着到达给定方块的路径来计算G的值,找出那个方块的G值的方法就是找到其父亲的G值,再加上10或者14而得,这依赖于他处于其父亲的斜角或者直角(非斜角)而定。这在本例后面会更加清晰,随着我们从开始点离开而得到更多的方块。
H can be estimated in a variety of ways. The method we use here is called the Manhattan method, where you calculate the total number of squares moved horizontally and vertically to reach the target square from the current square, ignoring diagonal movement. We then multiply the total by 10. This is called the Manhattan method because it's like calculating the number of city blocks from one place to another, where you can't cut across the block diagonally. Importantly, when calculating H, we ignore any intervening obstacles. This is an estimate of the remaining distance, not the actual distance, which is why it's called the heuristic. Want to know more? You can find equations and additional notes on heuristics here.
H能通过多种方法估算。我们这里用到的方法叫做Manhattan方法,计算从当前方块经过水平/垂直移动而到达目标方块的方块总数。然后将总数乘以 10。这种方法之所以叫做Manhattan方法,因为他很象计算从一个地点到达另一个地点的城市街区数量计算,此时你不能斜向的穿越街区。重要的是,当计算H的时候,要忽略任何路径中的障碍。这是一个对剩余距离的 估算值,而不是实际值,这就是试探法的称谓由来。想知道更多?关于试探法的更多说明在这里。
F is calculated by adding G and H. The results of the first step in our search can be seen in the illustration below. The F, G, and H scores are written in each square. As is indicated in the square to the immediate right of the starting square, F is printed in the top left, G is printed in the bottom left, and H is printed in the bottom right.
G和H相加就算出了F。第一步搜索的结果见下图的描述。F,G,和H值都写入了每个方块。如开始方块相邻右边的方块,F显示在左上方,G显示在左下方,而 H显示在右下方。
[图 3][Figure 3]
So let's look at some of these squares. In the square with the letters in it, G = 10. This is because it is just one square from the starting square in a horizontal direction. The squares immediately above, below, and to the left of the starting square all have the same G score of 10. The diagonal squares have G scores of 14.
好,让我们来观察某些方块。在有字母的方块中,G = 10。这是由于在水平方向上从开始点(到那里)只有一个方块(的距离)。开始点相邻上方,下方和左边的方块都具有同样的G值:10。斜角的方块G值为 14。
The H scores are calculated by estimating the Manhattan distance to the red target square, moving only horizontally and vertically and ignoring the wall that is in the way. Using this method, the square to the immediate right of the start is 3 squares from the red square, for a H score of 30. The square just above this square is 4 squares away (remember, only move horizontally and vertically) for an H score of 40. You can probably see how the H scores are calculated for the other squares.
H的计算通过估算Manhattan距离而得,即:水平/垂直移动,忽略途中的障碍,到达红色的目标方块的距离。用这种方法,开始点相邻右边的方块和红色方块相距3个方块,那么H值就是30。其上的方块距离为4(记住,只能水平或者垂直移动),H就是40。你也许可以看看其他方块的H值是如何算出的。
The F score for each square, again, is simply calculated by adding G and H together.
每个方块的F值,再说一下,不过就是G和H相加。
持续的搜索Continuing the Search
To continue the search, we simply choose the lowest F score square from all those that are on the open list. We then do the following with the selected square:
为了继续搜索,我们简单的选择开放列表里具有最小F值的方块。然后对选定的方块做如下操作:
Drop it from the open list and add it to the closed list.
将他从开放列表取出,并加入封闭列表。
Check all of the adjacent squares. Ignoring those that are on the closed list or unwalkable (terrain with walls, water, or other illegal terrain), add squares to the open list if they are not on the open list already. Make the selected square the "parent" of the new squares.
测试所有的相邻方块。忽略封闭列表内的和不可行走的(墙,水及其它非法地形)方块,如果方块不在开放列表中,则添加进去。将选定方块作为这些新加入方块的父亲。
If an adjacent square is already on the open list, check to see if this path to that square is a better one. In other words, check to see if the G score for that square is lower if we use the current square to get there. If not, don't do anything.
On the other hand, if the G cost of the new path is lower, change the parent of the adjacent square to the selected square (in the diagram above, change the direction of the pointer to point at the selected square). Finally, recalculate both the F and G scores of that square. If this seems confusing, you will see it illustrated below.
如果一个相邻方块已经存在于开放列表,检查到达那个方块的路径是否更优。换句话说,检查经由当前方块到达那里是否具有更小的G 值。如果没有,不做任何事。
相反,如果新路径的G值更小,把这个相邻方块的父亲改为当前选定的方块(在上图中,修改其指针方向指向选定方块)。最后,重新计算那个方块的F和G值。如果这样还是很迷惑的话,后面还会有图解说明。
Okay, so let's see how this works. Of our initial 9 squares, we have 8 left on the open list after the starting square was switched to the closed list. Of these, the one with the lowest F cost is the one to the immediate right of the starting square, with an F score of 40. So we select this square as our next square. It is highlight in blue in the following illustration.
好了,让我们看看它是怎样工作的。在初始的9个方块中,当开始方块被纳入封闭列表后,我们的开放列表就只有8个方块了。在这些块中,具有最小F值的是开始方块相邻右边的那个,其F值为40。所以我们选定这个块作为下一个方块。在随后的图例中,它以高亮的蓝色表示。
[图 4][Figure 4]
First, we drop it from our open list and add it to our closed list (that's why it's now highlighted in blue). Then we check the adjacent squares. Well, the ones to the immediate right of this square are wall squares, so we ignore those. The one to the immediate left is the starting square. That's on the closed list, so we ignore that, too.
首先,我们把它从开放列表取出,并加入到封闭列表(这就是它现在是高亮的蓝色的原因)。然后我们检查相邻的方块。然而,这个方块相邻右边的是代表墙的方块,所以忽略它们。其相邻左边是开始方块。它处于封闭列表内,所以也忽略它
The other four squares are already on the open list, so we need to check if the paths to those squares are any better using this square to get there, using G scores as our point of reference. Let's look at the square right above our selected square. Its current G score is 14. If we instead went through the current square to get there, the G score would be equal to 20 (10, which is the G score to get to the current square, plus 10 more to go vertically to the one just above it). A G score of 20 is higher than 14, so this is not a better path. That should make sense if you look at the diagram. It's more direct to get to that square from the starting square by simply moving one square diagonally to get there, rather than moving horizontally one square, and then vertically one square.
其它4个已经在开放列表中了,所以我们需要检查经由当前方块到达他们是否是更优的路径,使用G值为参考点。我们来看看这个选定方块上面右边的那个方块。它的当前G值是14。如果我们经由当前方块到达那里,G值将是20(10,到达当前方块的G值,再加上10垂直移动到它上面的方块)。20 > 14,所以这不是一个好的路径。看看图解能更好的理解这些。从开始方块斜向移动到那个方块更直接,而不是水平移动一个方块,再垂直移动一个方块。
When we repeat this process for all 4 of the adjacent squares already on the open list, we find that none of the paths are improved by going through the current square, so we don't change anything. So now that we looked at all of the adjacent squares, we are done with this square, and ready to move to the next square.
当我们对已经存在于开放列表的所有4个相邻方块都重复这个过程,我们发现经由当前方块没有更佳的路径,所以什么也不用改变。现在看看所有的相邻方块,我们已经处理完毕,并准备移动到下一个方块。
So we go through the list of squares on our open list, which is now down to 7 squares, and we pick the one with the lowest F cost. Interestingly, in this case, there are two squares with a score of 54. So which do we choose? It doesn't really matter. For the purposes of speed, it can be faster to choose the last one you added to the open list. This biases the search in favor of squares that get found later on in the search, when you have gotten closer to the target. But it doesn't really matter. (Differing treatment of ties is why two versions of A* may find different paths of equal length.)
现在,我们再遍历开放列表,它只有7个方块了,选择具有最小F值的那个。有趣的是,此时有两个方块都有值54。那么我们选择哪个?实际上这不算什么。为了速度的目的,选择你最后加入到开放列表的那个方块更快。当你更接近目标的时候,它倾向于后发现的方块。但这真的没什么关系。(不同的处理造成了两个版本的A*可能找到不同的等长路径。)
So let's choose the one just below, and to the right of the starting square, as is shown in the following illustration.
我们选择下面的那个,位于开始方块的右边,如下图所示。
[图 5][Figure 5]
This time, when we check the adjacent squares we find that the one to the immediate right is a wall square, so we ignore that. The same goes for the one just above that. We also ignore the square just below the wall. Why? Because you can't get to that square directly from the current square without cutting across the corner of the nearby wall. You really need to go down first and then move over to that square, moving around the corner in the process. (Note: This rule on cutting corners is optional. Its use depends on how your nodes are placed.)
这一次,当检查相邻的方块时,我们相邻右边的是一个墙方块,所以忽略它。对那个方块上面的块同样忽略。我们也忽略墙下面的方块。为什么?因为你不把临近墙的角切开就无法直接到达那个方块。实际上你需要先向下走,然后越过那个方块,在这个过程中都是围绕角在移动。(说明:切开角的规则是可选的。它的使用依赖于你的节点如何放置。)
That leaves five other squares. The other two squares below the current square aren't already on the open list, so we add them and the current square becomes their parent. Of the other three squares, two are already on the closed list (the starting square, and the one just above the current square, both highlighted in blue in the diagram), so we ignore them. And the last square, to the immediate left of the current square, is checked to see if the G score is any lower if you go through the current square to get there. No dice. So we're done and ready to check the next square on our open list.
这样就剩下5个方块了。当前方块下的两个方块不在开放列表中,所以要添加他们,并把当前方块作为它们的父亲。在另外三个方块中,有两个已经在封闭列表中了(开始方块,和当前方块上面的那个,它们都用高亮的蓝色在图中标出来了),所以忽略它们。最后一个方块,当前方块相邻左边的那个,检查经由当前方块到达那里是否得到更小的G值。没有。所以处理完毕,并准备检查开放列表中的下一个方块。
We repeat this process until we add the target square to the open list, at which point it looks something like the illustration below.
我们重复这个过程,直到把目标点添加到开放列表,此时的情形如下图所示。
[图 6][Figure 6]
Note that the parent square for the square two squares below the starting square has changed from the previous illustration. Before it had a G score of 28 and pointed back to the square above it and to the right. Now it has a score of 20 and points to the square just above it. This happened somewhere along the way on our search, where the G score was checked and it turned out to be lower using a new path ? so the parent was switched and the G and F scores were recalculated. While this change doesn't seem too important in this example, there are plenty of possible situations where this constant checking will make all the difference in determining the best path to your target.
注意开始方块向下的第二个方块,在前面的描述中其父亲已经发生改变。开始它的G值为28,指向其右上角的方块。现在它的值是20,指向其上方的方块。这是在搜索方法中某处发生的吗?在那里G值被检查,而且使用新的路径后,它得到了更小的值。所以它的父亲切换了,G和F也重新计算。而这个改变在本例中不见得非常重要,还有足够多的可能位置,在决定最佳路径的时候,持续的检查会产生各种差别。
So how do we determine the actual path itself? Simple, just start at the red target square, and work backwards moving from one square to its parent, following the arrows. This will eventually take you back to the starting square, and that's your path! It should look like the following illustration. Moving from the starting square A to the destination square B is simply a matter of moving from the center of each square (the node) to the center of the next square on the path, until you reach the target. Simple!
那么我们怎样决定实际的路径呢?简单,从红色目标方块开始,向后移动到它的父亲,跟从箭头的指示。最终你会回到开始方块,这就是路径!它应该如下图所示。从方块A移动到目标方块B就是从每一个方块(节点)的中心移动到路径上的下一个方块的中心的简单过程,直到到达目标。简单!
A*方法总结
好,现在你已经看完了整个说明,让我们把每一步的操作写在一起:
把起始格添加到开启列表。
重复如下的工作:
a) 寻找开启列表中F值最低的格子。我们称它为当前格。
b) 把它切换到关闭列表。
c) 对相邻的8格中的每一个?
如果它不可通过或者已经在关闭列表中,略过它。反之如下。
如果它不在开启列表中,把它添加进去。把当前格作为这一格的父节点。记录这一格的F,G,和H值。
如果它已经在开启列表中,用G值为参考检查新的路径是否更好。更低的G值意味着更好的路径。如果是这样,就把这一格的父节点改成当前格,并且重新计算这一格的G和F值。如果你保持你的开启列表按F值排序,改变之后你可能需要重新对开启列表排序。
d) 停止,当你
把目标格添加进了开启列表,这时候路径被找到,或者
没有找到目标格,开启列表已经空了。这时候,路径不存在。
保存路径。从目标格开始,沿着每一格的父节点移动直到回到起始格。这就是你的路径。
题外话
离题一下,见谅,值得一提的是,当你在网上或者相关论坛看到关于A*的不同的探讨,你有时会看到一些被当作A*算法的代码而实际上他们不是。要使用A*,你必须包含上面讨论的所有元素--特定的开启和关闭列表,用F,G和H作路径评价。有很多其他的寻路算法,但他们并不是A*,A*被认为是他们当中最好的。Bryan Stout在这篇文章后面的参考文档中论述了一部分,包括他们的一些优点和缺点。有时候特定的场合其他算法会更好,但你必须很明确你在作什么。好了,够多的了。回到文章。
实现的注解
现在你已经明白了基本原理,写你的程序的时候还得考虑一些额外的东西。下面这些材料中的一些引用了我用C++和Blitz Basic写的程序,但对其他语言写的代码同样有效。
维护开启列表:这是A*寻路算法最重要的组成部分。每次你访问开启列表,你都需要寻找F值最低的方格。有几种不同的方法实现这一点。你可以把路径元素随意保存,当需要寻找F值最低的元素的时候,遍历开启列表。这很简单,但是太慢了,尤其是对长路径来说。这可以通过维护一格排好序的列表来改善,每次寻找F值最低的方格只需要选取列表的首元素。当我自己实现的时候,这种方法是我的首选。
在小地图。这种方法工作的很好,但它并不是最快的解决方案。更苛求速度的A*程序员使用叫做“binary heap”的方法,这也是我在代码中使用的方法。凭我的经验,这种方法在大多数场合会快2~3倍,并且在长路经上速度呈几何级数提升(10倍以上速度)。如果你想了解更多关于binary heap的内容,查阅我的文章:Using Binary Heaps in A* Pathfinding。
其他单位:如果你恰好看了我的例子代码,你会发现它完全忽略了其他单位。我的寻路者事实上可以相互穿越。取决于具体的游戏,这也许可以,也许不行。如果你打算考虑其他单位,希望他们能互相绕过,我建议在寻路算法中忽略其他单位,写一些新的代码作碰撞检测。当碰撞发生,你可以生成一条新路径或者使用一些标准的移动规则(比如总是向右,等等)直到路上没有了障碍,然后再生成新路径。为什么在最初的路径计算中不考虑其他单位呢?那是因为其他单位会移动,当你到达他们原来的位置的时候,他们可能已经离开了。这有可能会导致奇怪的结果,一个单位突然转向,躲避一个已经不在那里的单位,并且会撞到计算完路径后,冲进它的路径中的单位。
然而,在寻路算法中忽略其他对象,意味着你必须编写单独的碰撞检测代码。这因游戏而异,所以我把这个决定权留给你。参考文献列表中,Bryan Stout的文章值得研究,里面有一些可能的解决方案(像鲁棒追踪,等等)。
一些速度方面的提示:当你开发你自己的A*程序,或者改写我的,你会发现寻路占据了大量的CPU时间,尤其是在大地图上有大量对象在寻路的时候。如果你阅读过网上的其他材料,你会明白,即使是开发了星际争霸或帝国时代的专家,这也无可奈何。如果你觉得寻路太过缓慢,这里有一些建议也许有效:
使用更小的地图或者更少的寻路者。
不要同时给多个对象寻路。取而代之的是把他们加入一个队列,把寻路过程分散在几个游戏周期中。如果你的游戏以40周期每秒的速度运行,没人能察觉。但是他们会发觉游戏速度突然变慢,当大量寻路者计算自己路径的时候。
尽量使用更大的地图网格。这降低了寻路中搜索的总网格数。如果你有志气,你可以设计两个或者更多寻路系统以便使用在不同场合,取决于路径的长度。这也正是专业人士的做法,用大的区域计算长的路径,然后在接近目标的时候切换到使用小格子/区域的精细寻路。如果你对这个观点感兴趣,查阅我的文章 :Two-Tiered A* Pathfinding。
使用路径点系统计算长路径,或者预先计算好路径并加入到游戏中。
预处理你的地图,表明地图中哪些区域是不可到达的。我把这些区域称作“孤岛”。事实上,他们可以是岛屿或其他被墙壁包围等无法到达的任意区域。A*的下限是,当你告诉它要寻找通往那些区域的路径时,它会搜索整个地图,直到所有可到达的方格/节点都被通过开启列表和关闭列表的计算。这会浪费大量的CPU时间。可以通过预先确定这些区域(比如通过flood-fill或类似的方法)来避免这种情况的发生,用某些种类的数组记录这些信息,在开始寻路前检查它。在我Blitz版本的代码中,我建立了一个地图预处理器来作这个工作。它也标明了寻路算法可以忽略的死端,这进一步提高了寻路速度。
不同的地形损耗:在这个教程和我附带的程序中,地形只有两种-可通过的和不可通过的。但是你可能会需要一些可通过的地形,但是移动耗费更高-沼泽,小山,地牢的楼梯,等等。这些都是可通过但是比平坦的开阔地移动耗费更高的地形。类似的,道路应该比自然地形移动耗费更低。
这个问题很容易解决,只要在计算任何地形的G值的时候增加地形损耗就可以了。简单的给它增加一些额外的损耗就可以了。由于A*算法已经按照寻找最低耗费的路径来设计,所以很容易处理这种情况。在我提供的这个简单的例子里,地形只有可通过和不可通过两种,A*会找到最短,最直接的路径。但是在地形耗费不同的场合,耗费最低的路径也许会包含很长的移动距离-就像沿着路绕过沼泽而不是直接穿过它。
一种需额外考虑的情况是被专家称之为“influence mapping”的东西(暂译为影响映射图)。就像上面描述的不同地形耗费一样,你可以创建一格额外的分数系统,并把它应用到寻路的AI中。假设你有一张有大批寻路者的地图,他们都要通过某个山区。每次电脑生成一条通过那个关口的路径,它就会变得更拥挤。如果你愿意,你可以创建一个影响映射图对有大量屠杀事件的格子施以不利影响。这会让计算机更倾向安全些的路径,并且帮助它避免总是仅仅因为路径短(但可能更危险)而持续把队伍和寻路者送到某一特定路径。
处理未知区域:你是否玩过这样的PC游戏,电脑总是知道哪条路是正确的,即使它还没有侦察过地图?对于游戏,寻路太好会显得不真实。幸运的是,这是一格可以轻易解决的问题。
答案就是为每个不同的玩家和电脑(每个玩家,而不是每个单位--那样的话会耗费大量的内存)创建一个独立的“knownWalkability”数组,每个数组包含玩家已经探索过的区域,以及被当作可通过区域的其他区域,直到被证实。用这种方法,单位会在路的死端徘徊并且导致错误的选择直到他们在周围找到路。一旦地图被探索了,寻路就像往常那样进行。
平滑路径:尽管A*提供了最短,最低代价的路径,它无法自动提供看起来平滑的路径。看一下我们的例子最终形成的路径(在图7)。最初的一步是起始格的右下方,如果这一步是直接往下的话,路径不是会更平滑一些吗?
有几种方法来解决这个问题。当计算路径的时候可以对改变方向的格子施加不利影响,对G值增加额外的数值。也可以换种方法,你可以在路径计算完之后沿着它跑一遍,找那些用相邻格替换会让路径看起来更平滑的地方。想知道完整的结果,查看 Marco Pinter 发表在 Gamasutra.com 的 一篇文章:Toward More Realistic Pathfinding (免费,但是需要注册)。
非方形搜索区域:在我们的例子里,我们使用简单的2D方形图。你可以不使用这种方式。你可以使用不规则形状的区域。想想冒险棋的游戏,和游戏中那些国家。你可以设计一个像那样的寻路关卡。为此,你可能需要建立一个国家相邻关系的表格,和从一个国家移动到另一个的G值。你也需要估算H值的方法。其他的事情就和例子中完全一样了。当你需要向开启列表中添加新元素的时候,不需使用相邻的格子,取而代之的是从表格中寻找相邻的国家。
类似的,你可以为一张确定的地形图创建路径点系统,路径点一般是路上,或者地牢通道的转折点。作为游戏设计者,你可以预设这些路径点。两个路径点被认为是相邻的如果他们之间的直线上没有障碍的话。在冒险棋的例子里,你可以保存这些相邻信息在某个表格里,当需要在开启列表中添加元素的时候使用它。然后你就可以记录关联的G值(可能使用两点间的直线距离),H值(可以使用到目标点的直线距离),其他都按原先的做就可以了。
另一个在非方形区域搜索RPG地图的例子:
In my main article, A* Pathfinding for Beginners, I described A* in very general terms, and described how to create a single all-purpose pathfinding function. Creating only one pathfinding function, however, can be needlessly limiting.
Consider the following RPG situation, and a swordsman who wants to pathfind around a nearby wall:
Given this kind of map, you could place nodes in a variety of ways, and use a variety of densities. In this example, let's use a high-density node network, as is shown below.
In this graphic, the white nodes are walkable. Spots where there are no nodes are unwalkable. We also do some things a little differently in this example. In this example you are allowed to "cut corners" around unwalkable squares. Also the "squares" themselves aren't square anymore. Because this is an isometric example, we have decided to pack twice as many nodes on the Y (vertical axis) as there are on the X (horizontal) axis. This allows proper isometric paths.
As you can see, using this tightly packed node network, we can pathfind not only around the nearby wall but also between the wall and the nearby barrel in the process. Our dense node network also allows pathfinding around the standing torch, where the node at the very base is unwalkable. This, in sum, is precision pathfinding.
Well, that is pretty cool in short-distance situations, but what do we do if we need to pathfind across the entire map? Using a densely packed node network like this could easily leave you searching through 10,000+ nodes on just a single pass through the A* loop. That will bring just about any PC to a grinding halt.
So let's look at an alternative. What if we chose to create a much less dense node network, like the one you see below?
In this example, the nodes are in the center of the large isometric diamonds. Data about walkability between the diamonds is stored in the same array along the edges, and is represented in this graphic with small red dots. To move from one isometric tile to one of its 8 adjacent macro-tiles, the adjacent tile must itself be walkable, and the path between must not be blocked.
This node network is 72 times less dense than the earlier one. Instead of dealing with as many as 10,000+ nodes at any given time, here we will only be dealing with 1/72 of that, or less than 200. Our computer can definitely handle that. Pathfinding across the map is no longer a problem.
Putting the Two Together
So, which method do we choose? Both!
On any given search, you would first pathfind across the map, using the macro-level pathfinder. You would then switch to the micro-pathfinder and search for a path from the current location to the node two macro-nodes away on the path. Once you entered each new macro-level diamond "square," you would use the micro-level pathfinder to pathfind to the macro-node two macro nodes ahead. This ends up wasting the second half of each micro-path, but you need to do this to make sure it looks good -- and it isn't much of a waste, since you are micro-pathfinding across a relatively short distance. Once you get 2-3 macro nodes away from the target, you would switch over completely to the micro-pathfinder, and pathfind to the final location.
Using this approach, you will get speed pathfinding across the entire map and be able to negotiate around corners, barrels, etc. in a realistic way, as in the earlier micro-pathfinding example. Pretty cool, huh!