问题描述
很久以前,我尝试使用修改后的客户端和简单的广度优先搜索算法来实现带有寻路功能的 minecraft Bot,该算法有效,但性能极差。尽管如此,我实际上已经能够决定打破某些块或放置它们以达到目标是否更聪明。
所以我开始实现我自己的 minecraft 客户端,我猜它可以正常工作 :D 另外我想使用 A* Pathfinding 来获得更好的性能。不幸的是,在实施它之后,我开始发现它在大多数情况下都很好用,但是一旦路径变得更复杂,它就会直接计算几分钟而找不到通往目标的路径。似乎它要么朝错误的方向搜索,要么以某种方式完全跳过目标。无论哪种方式,它都找不到它。再加上实现跳跃后可以越过间隙,不管他是否必须,他都会继续跳跃。看起来像一个快乐的 minecraft 玩家跳来跳去,但没有任何意义。即使将跳跃成本设置为极大值,它仍然会到处跳跃,而不是正常行走。
我开始认为这种方法肯定存在根本性错误,但无法找出可能是什么。
这是我的寻路逻辑
class pathfinder
{
private const int WalkCost = 10;
private const int DiagonalWalkCost = 14;
private const int JumpCost = 5;
private const int Maxdistance = 100;
private int Getdistance(Node a,Node b)
{
return Math.Abs(b.X - a.X) + Math.Abs(b.Y - a.Y) + Math.Abs(b.Z - a.Z);
}
public Path Findpath(Node start,Node goal)
{
start.H = Getdistance(start,goal) * WalkCost;
var open = new List<Node>();
open.Add(start);
var closed = new List<Node>();
var cameFrom = new Dictionary<Node,Node>();
var action = new Dictionary<Node,Movement>();
var done = false;
var considered = 0;
var world = Client.Instance.World;
Node current;
while (open.Count > 0)
{
current = open.OrderBy(n => n.F).First();
open.Remove(current);
closed.Add(current);
if (current == goal)
{
done = true;
break;
}
foreach (var neighbour in GetNeighbours(current))
{
if (Getdistance(neighbour,start) > Maxdistance)
continue;
if (closed.Contains(neighbour))
continue;
if (neighbour.Y > current.Y) //Jump up
{
if (!IsWalkable(neighbour)
|| world.IsSolid(current.Up().Up()))
continue;
neighbour.G = current.G + WalkCost + JumpCost;
action[neighbour] = Movement.Jumpup;
}
else if(neighbour.Y < current.Y) //Jump Down
{
if (!IsWalkable(neighbour)
|| world.IsSolid(neighbour.Up().Up()))
continue;
neighbour.G = current.G + WalkCost + JumpCost;
action[neighbour] = Movement.Fall;
}
else //Walk
{
if (!IsWalkable(neighbour))
continue;
if (neighbour.X != current.X && neighbour.Z != current.Z) //Diagonal
{
if (world.IsSolid(new Node(neighbour.X,current.Y,current.Z))
|| world.IsSolid(new Node(neighbour.X,current.Z).Up())
|| world.IsSolid(new Node(current.X,neighbour.Z))
|| world.IsSolid(new Node(current.X,neighbour.Z).Up()))
continue;
neighbour.G = current.G + DiagonalWalkCost;
action[neighbour] = Movement.Walk;
}
else //Straight
{
if(Getdistance(current,neighbour) >= 2) //Straight Jump
{
var nodeBetween = new Node(current.X + (neighbour.X - current.X) / 2,current.Z + (neighbour.Z - current.Z) / 2);
if (world.IsSolid(current.Up().Up())
|| world.IsSolid(neighbour.Up().Up())
|| world.IsSolid(nodeBetween)
|| world.IsSolid(nodeBetween.Up())
|| world.IsSolid(nodeBetween.Up().Up()))
continue;
neighbour.G = current.G + 2 * WalkCost + JumpCost;
action[neighbour] = Movement.JumpGap;
}
else //Straight Walk
{
neighbour.G = current.G + WalkCost;
action[neighbour] = Movement.Walk;
}
}
}
considered++;
var prevIoUsEntry = open.FirstOrDefault(n => n == neighbour);
if (prevIoUsEntry == null || neighbour.G < prevIoUsEntry.G)
{
neighbour.H = Getdistance(neighbour,goal) * WalkCost;
cameFrom[neighbour] = current;
open.Add(neighbour);
}
}
}
Client.Instance.SendChatMessage(String.Format("{0} possible moves considered",considered));
if (!done)
return null;
var currentNode = goal;
var path = new List<Node>();
var moves = new List<Movement>();
while (currentNode != start)
{
path.Add(currentNode);
moves.Add(action[action.Keys.First(k => k == currentNode)]);
currentNode = cameFrom[cameFrom.Keys.First(k => k == currentNode)];
}
path.Add(start);
path.Reverse();
moves.Add(Movement.Walk);
moves.Reverse();
return new Path(path,moves);
}
private bool IsWalkable(Node node)
{
var world = Client.Instance.World;
var block = world.GetBlock(node.X,node.Y,node.Z);
return world.IsSolid(node.Down())
&& !world.IsSolid(node)
&& !world.IsSolid(node.Up())
&& !block.BlockName.Contains("water")
&& !block.BlockName.Contains("lava");
}
private Node[] GetNeighbours(Node node)
{
var neighbours = new List<Node>();
//neighbours.Add(node.Up()); //pillar up
//neighbours.Add(node.Down()); //dig down
neighbours.Add(node.north()); //simple walk
neighbours.Add(node.East());
neighbours.Add(node.south());
neighbours.Add(node.West());
neighbours.Add(node.north().East()); //diagonal walk
neighbours.Add(node.north().West());
neighbours.Add(node.south().East());
neighbours.Add(node.south().West());
neighbours.Add(node.north().Up()); //jump up
neighbours.Add(node.East().Up());
neighbours.Add(node.south().Up());
neighbours.Add(node.West().Up());
neighbours.Add(node.north().Down()); //fall down
neighbours.Add(node.East().Down());
neighbours.Add(node.south().Down());
neighbours.Add(node.West().Down());
neighbours.Add(node.north().north()); //jump 1 wide gap
neighbours.Add(node.East().East());
neighbours.Add(node.south().south());
neighbours.Add(node.West().West());
return neighbours.ToArray();
}
}
这是我的 Node 类
public class Node/* : IEquatable<Node>*/
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public int G { get; set; }
public int H { get; set; }
public int F { get => G + H; }
public Node CameFrom { get; set; }
public Node(int x,int y,int z)
{
X = x;
Y = y;
Z = z;
}
public Node()
{
}
public Node Down()
{
return new Node(X,Y - 1,Z);
}
public Node Up()
{
return new Node(X,Y + 1,Z);
}
public Node East()
{
return new Node(X + 1,Y,Z);
}
public Node West()
{
return new Node(X - 1,Z);
}
public Node South()
{
return new Node(X,Z + 1);
}
public Node north()
{
return new Node(X,Z - 1);
}
//public bool Equals(Node other)
//{
// return other.X == X && other.Y == Y && other.Z == Z;
//}
public override bool Equals(object obj)
{
if (!(obj is Node))
return false;
return this == (Node)obj;
}
public static bool operator ==(Node a,Node b)
{
if (a is null && b is object || b is null && a is object)
return false;
if (a is null && b is null)
return true;
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
public static bool operator !=(Node a,Node b)
{
if (a is null && b is object || b is null && a is object)
return true;
if (a is null && b is null)
return false;
return a.X != b.X || a.Y != b.Y || a.Z != b.Z;
}
//public static bool operator ==(Node a,Node b)
//{
// return a.GetHashCode() == b.GetHashCode();
//}
//public static bool operator !=(Node a,Node b)
//{
// return a.GetHashCode() != b.GetHashCode();
//}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
hash = hash * 23 + Z.GetHashCode();
return hash;
}
}
忽略混乱的相等性检查。我在想这可能与我检查节点相等性的方式有关,但也没有成功,除了我的代码比 xD 之前更混乱
有人对这里可能出现的问题有什么建议吗?任何关于类似事物的文章,这意味着在 3D 环境中寻找路径并跳过间隙,也将不胜感激。
这是一个运行过程的可视化结果,它花费了 16.5 秒,查看了 62.700 个可能的邻居,最终总共走了 48 步。
机器人试图从右边的橙色方块移动到梯子末端的蓝色方块,这种东西在空中,这使得寻路更难公平:
这是可视化:
绿色:采用的路径,蓝色:封闭列表,浅蓝色:开放列表
据我所知,寻路似乎是有效的,但我未受过教育的假设是,它非常无效,因为它在最终找到目标之前在封闭列表中有很多块。
随着路径越长,这个时间似乎呈指数增长,因为有时需要半个小时左右才能找到目标,我认为这是行不通的。
解决方法
我还没有看到所有的代码,但 GetDistance 似乎是错误的。 假设我们忘记了 Y。 (1,1) 和 (2,2) 与您的方法的距离为 2,因此与 (1,1) 和 (1,3) 的距离相同,这是错误的,您可以绘制以查看。
对于 3D 距离,计算是
float deltaX = b.x - a.x;
float deltaY = b.y - a.y;
float deltaZ = b.z - a.z;
float distance = (float) Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
,
再试一次: 您可以尝试提高性能的方法:
- 由于您已经实现了 get HashCode,您可以更改 HastSet 中的列表,尤其是关闭的,因为您在其上使用了 contains 并且 List 在 O(n) 中,HashSet 在 O(1) 中
- OrderBy.First() 不是最好的方法,因为它必须对列表进行排序,这也需要很多时间。也许你可以用 Min() 和 Select() 做同样的事情