我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
当前回答
对于更传统的寻路算法的替代方案,您可以看看(名称很合适!)吃豆人气味反对象模式。
你可以在启动时在迷宫中弥漫怪物洞的气味,然后让眼睛跟着它回家。
气味设置好后,运行成本非常低。
编辑:很遗憾维基百科上的文章已经被删除了,所以WayBack Machine来拯救…
其他回答
如果每个正方形都有一个到中心的距离值呢?这样,对于每个给定的正方形,你可以在所有可能的方向上得到相邻正方形的值。你选择最小值的正方形,然后移动到那个正方形。
数值将使用任何可用的算法预先计算出来。
我认为你的解决方案是正确的,比这更简单,就是制作一个更“现实”的新版本,鬼魂的眼睛可以穿过墙壁=)
我不太清楚你是如何执行游戏的,但你可以这么做:
Determine the eyes location relative position to the gate. i.e. Is it left above? Right below? Then move the eyes opposite one of the two directions (such as make it move left if it is right of the gate, and below the gate) and check if there are and walls preventing you from doing so. If there are walls preventing you from doing so then make it move opposite the other direction (for example, if the coordinates of the eyes relative to the pin is right north and it was currently moving left but there is a wall in the way make it move south. Remember to keep checking each time to move to keep checking where the eyes are in relative to the gate and check to see when there is no latitudinal coordinate. i.e. it is only above the gate. In the case it is only above the gate move down if there is a wall, move either left or right and keep doing this number 1 - 4 until the eyes are in the den. I've never seen a dead end in Pacman this code will not account for dead ends. Also, I have included a solution to when the eyes would "wobble" between a wall that spans across the origin in my pseudocode.
一些伪代码:
x = getRelativeOppositeLatitudinalCoord()
y
origX = x
while(eyesNotInPen())
x = getRelativeOppositeLatitudinalCoordofGate()
y = getRelativeOppositeLongitudinalCoordofGate()
if (getRelativeOppositeLatitudinalCoordofGate() == 0 && move(y) == false/*assume zero is neither left or right of the the gate and false means wall is in the way */)
while (move(y) == false)
move(origX)
x = getRelativeOppositeLatitudinalCoordofGate()
else if (move(x) == false) {
move(y)
endWhile
最初的《吃豆人》并没有使用寻径或花哨的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.
我的方法有点内存密集型(从《吃豆人》时代的角度来看),但你只需要计算一次,它适用于任何关卡设计(包括跳跃)。
一次标记节点
当你第一次加载一个关卡时,将所有怪物巢穴节点标记为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);
}
全标记示例