我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。

在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。

有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?


当前回答

在游戏开始前保存地图上的节点(交叉点) 当怪物死亡时,取点(坐标)并找到 节点列表中最近的节点 计算从该节点到洞的所有路径 按长度取最短路径 将该点与最近节点之间的空间长度相加 绘制并在路径上移动

享受吧!

其他回答

下面是ammoQ的洪水填充想法的模拟和伪代码。

queue q
enqueue q, ghost_origin
set visited

while q has squares
   p <= dequeue q
   for each square s adjacent to p
      if ( s not in visited ) then
         add s to visited
         s.returndirection <= direction from s to p
         enqueue q, s
      end if
   next
 next

它的思想是宽度优先搜索,所以每次你遇到一个新的相邻正方形s,最好的路径是经过p。我相信是O(N)。

我的方法有点内存密集型(从《吃豆人》时代的角度来看),但你只需要计算一次,它适用于任何关卡设计(包括跳跃)。

一次标记节点

当你第一次加载一个关卡时,将所有怪物巢穴节点标记为0(代表与巢穴的距离)。继续向外标记已连接的节点1,连接到它们的节点2,依此类推,直到所有节点都被标记。(注意:如果巢穴有多个入口,这也是有效的)

我假设您已经有了表示每个节点和到它们的邻居的连接的对象。伪代码可能看起来像这样:

public void fillMap(List<Node> nodes) { // call passing lairNodes
    int i = 0;

    while(nodes.count > 0) {
        // Label with distance from lair
        nodes.labelAll(i++);

        // Find connected unlabelled nodes
        nodes = nodes
            .flatMap(n -> n.neighbours)
            .filter(!n.isDistanceAssigned());
    }
}

眼睛移动到距离标签最小的邻居

一旦所有节点都标记好了,路由眼睛就变得很简单了……只需要选择距离标签最小的相邻节点(注意:如果多个节点的距离相等,那么选择哪个节点并不重要)。伪代码:

public Node moveEyes(final Node current) {
    return current.neighbours.min((n1, n2) -> n1.distance - n2.distance);
}

全标记示例

这是一个寻径问题。有关流行的算法,请参见http://wiki.gamedev.net/index.php/A*。

最初的《吃豆人》并没有使用寻径或花哨的AI。它只是让玩家觉得游戏比实际更有深度,但实际上它是随机的。正如Ian Millington和John Funge在《ai Intelligence for Games》中所述。

Not sure if it's true or not, but it makes a lot of sense to me. Honestly, I don't see these behaviors that people are talking about. Red/Blinky for ex is not following the player at all times, as they say. Nobody seems to be consistently following the player, on purpose. The chance that they will follow you looks random to me. And it's just very tempting to see behavior in randomness, especially when the chances of getting chased are very high, with 4 enemies and very limited turning options, in a small space. At least in its initial implementation, the game was extremely simple. Check out the book, it's in one of the first chapters.

对于我的《吃豆人》游戏,我创造了一个“最短多条回家路径”算法,它适用于我所提供的任何迷宫(在我的规则集内)。它也适用于隧道。

当关卡被加载时,每个十字路口的所有归途数据都是空的(默认),一旦幽灵开始探索迷宫,它们每次遇到“新的”十字路口或从不同的路径再次遇到已知的十字路口时,它们的归途路径信息就会不断更新。