public static Backtrace ( |
||
iNode | ||
Результат | List |
public static List <GridPos> FindPath(JumpPointParam iParam) { List <Node> tOpenList = iParam.openList; Node tStartNode = iParam.StartNode; Node tEndNode = iParam.EndNode; Node tNode; bool revertEndNodeWalkable = false; // set the `g` and `f` value of the start node to be 0 tStartNode.startToCurNodeLen = 0; tStartNode.heuristicStartToEndLen = 0; // push the start node into the open list tOpenList.Add(tStartNode); tStartNode.isOpened = true; if (iParam.AllowEndNodeUnWalkable && !iParam.SearchGrid.IsWalkableAt(tEndNode.x, tEndNode.y)) { iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, true); revertEndNodeWalkable = true; } // while the open list is not empty while (tOpenList.Count > 0) { // pop the position of node which has the minimum `f` value. tOpenList.Sort(); tNode = (Node)tOpenList[tOpenList.Count - 1]; tOpenList.RemoveAt(tOpenList.Count - 1); tNode.isClosed = true; if (tNode.Equals(tEndNode)) { if (revertEndNodeWalkable) { iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, false); } return(Node.Backtrace(tNode)); // rebuilding path } identifySuccessors(iParam, tNode); } if (revertEndNodeWalkable) { iParam.SearchGrid.SetWalkableAt(tEndNode.x, tEndNode.y, false); } // fail to find the path return(new List <GridPos>()); }
public static List <GridPos> FindPath(JumpPointParam jpParam) { var tOpenList = jpParam.OpenList; var tStartNode = jpParam.StartNode; var tEndNode = jpParam.EndNode; Node tNode; var revertEndNodeWalkable = false; // set the `g` and `f` value of the start node to be 0 tStartNode.StartToCurNodeLen = 0; tStartNode.HeuristicStartToEndLen = 0; // push the start node into the open list _ = tOpenList.Add(tStartNode); tStartNode.IsOpened = true; if (jpParam.CurEndNodeUnWalkableTreatment == EndNodeUnWalkableTreatment.Allow && !jpParam.SearchGrid.IsWalkableAt(tEndNode.X, tEndNode.Y)) { _ = jpParam.SearchGrid.SetWalkableAt(tEndNode.X, tEndNode.Y, true); revertEndNodeWalkable = true; } // while the open list is not empty while (tOpenList.Count > 0) { // pop the position of node which has the minimum `f` value. tNode = tOpenList.DeleteMin(); tNode.IsClosed = true; if (tNode.Equals(tEndNode)) { if (revertEndNodeWalkable) { _ = jpParam.SearchGrid.SetWalkableAt(tEndNode.X, tEndNode.Y, false); } return(Node.Backtrace(tNode)); // rebuilding path } IdentifySuccessors(jpParam, tNode); } if (revertEndNodeWalkable) { _ = jpParam.SearchGrid.SetWalkableAt(tEndNode.X, tEndNode.Y, false); } // fail to find the path return(new List <GridPos>()); }
/* private class NodeComparer : IComparer<Node> { public int Compare(Node x, Node y) { var result = (x.heuristicStartToEndLen - y.heuristicStartToEndLen); if (result < 0) return -1; else if (result > 0) return 1; else { return 0; } } } */ public static List<GridPos> FindPath(AStarParam iParam) { object lo = new object(); //var openList = new IntervalHeap<Node>(new NodeComparer()); var openList = new IntervalHeap<Node>(); var startNode = iParam.StartNode; var endNode = iParam.EndNode; var heuristic = iParam.HeuristicFunc; var grid = iParam.SearchGrid; var diagonalMovement = iParam.DiagonalMovement; var weight = iParam.Weight; startNode.startToCurNodeLen = 0; startNode.heuristicStartToEndLen = 0; openList.Add(startNode); startNode.isOpened = true; while (openList.Count != 0) { var node = openList.DeleteMin(); node.isClosed = true; if (node == endNode) { return Node.Backtrace(endNode); } var neighbors = grid.GetNeighbors(node, diagonalMovement); #if (UNITY) foreach(var neighbor in neighbors) #else Parallel.ForEach(neighbors, neighbor => #endif { #if (UNITY) if (neighbor.isClosed) continue; #else if (neighbor.isClosed) return; #endif var x = neighbor.x; var y = neighbor.y; float ng = node.startToCurNodeLen + (float)((x - node.x == 0 || y - node.y == 0) ? 1 : Math.Sqrt(2)); if (!neighbor.isOpened || ng < neighbor.startToCurNodeLen) { neighbor.startToCurNodeLen = ng; if (neighbor.heuristicCurNodeToEndLen == null) neighbor.heuristicCurNodeToEndLen = weight * heuristic(Math.Abs(x - endNode.x), Math.Abs(y - endNode.y)); neighbor.heuristicStartToEndLen = neighbor.startToCurNodeLen + neighbor.heuristicCurNodeToEndLen.Value; neighbor.parent = node; if (!neighbor.isOpened) { lock (lo) { openList.Add(neighbor); } neighbor.isOpened = true; } else { } } } #if (!UNITY) ); #endif } return new List<GridPos>(); }