Skip to content

Commit

Permalink
improved AStar performance a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Mar 8, 2024
1 parent 22e7046 commit c1baa01
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Plugins/MineSharp.Pathfinder/Algorithm/AStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public Path FindPath(Position start, Position end, int maxNodes = 5000)

if (node.Position == end)
{
sw.Stop();
LogManager.GetCurrentClassLogger().Debug($"Checked {nodes.Count} nodes in {sw.ElapsedMilliseconds}ms");
return RetracePath(startNode, endNode);
}

Expand Down Expand Up @@ -96,12 +98,11 @@ private float Distance(Position a, Position b)

foreach (var move in this.Movements.PossibleMoves)
{
if (!move.IsMovePossible(node.Position, world))
var pos = move.Motion.Plus(node.Position);
var neighbor = this.GetNode((Position)pos, end, ref nodes);
if (!neighbor.Walkable || !move.IsMovePossible(node.Position, world))
continue;

var pos = move.Motion.Plus(node.Position);

var neighbor = this.GetNode((Position)pos, end, ref nodes);
neighbors.Add((neighbor, move));
}

Expand Down

0 comments on commit c1baa01

Please sign in to comment.