public static SortedSet <__AStarNode> getPathDrawnBetween(Level level, IntVec from, IntVec to, int count) { int actions = 0; int[,] solid = level.getIntSolid(); SortedSet <__AStarNode> nodes = new SortedSet <__AStarNode>(new __AStarNode.__AStarNodeComparer()); __AStarNode recentNode = new __AStarNode(from, 0, 10 * calculateHeuristic(from, to)); __AStarNode bestNode = recentNode; nodes.Add(recentNode); while (recentNode != null && !recentNode.position.Equals(to) && !recentNode.expanded && (actions < count)) { recentNode = nodes.Min; if (recentNode != null) { //recentNode.expanded = true; Direction[] closest = closestDirections(to - recentNode.position); if (recentNode.heuristic.CompareTo(bestNode.heuristic) <= 0) { //Console.Write("test"); bestNode = recentNode; } for (int i = 0; i < closest.Length; i++)//foreach (Direction dir in Direction.Values) { Direction dir = closest[i]; IntVec newLocation = recentNode.position + dir; if (solid[newLocation.X, newLocation.Y] < -(recentNode.actualCost + 10) && (actions++ < count)) { solid[newLocation.X, newLocation.Y] = -(recentNode.actualCost + 10); nodes.Add(new __AStarNode(newLocation, recentNode.actualCost + 10, 10 * calculateHeuristic(newLocation, to), i, recentNode, dir)); } } if (actions++ < count) { nodes.Remove(recentNode); } } } return(nodes); }
public static Direction[] getPathBetween(Level level, IntVec from, IntVec to, ref bool isPossible) { int[,] solid = level.getIntSolid(); SortedSet <__AStarNode> nodes = new SortedSet <__AStarNode>(new __AStarNode.__AStarNodeComparer()); __AStarNode recentNode = new __AStarNode(from, 0, 10 * calculateHeuristic(from, to)); __AStarNode bestNode = recentNode; nodes.Add(recentNode); while (recentNode != null && !recentNode.position.Equals(to) && !recentNode.expanded) { recentNode = nodes.Min; if (recentNode != null) { //recentNode.expanded = true; Direction[] closest = closestDirections(to - recentNode.position); if (recentNode.heuristic.CompareTo(bestNode.heuristic) <= 0) { //Console.Write("test"); bestNode = recentNode; } for (int i = 0; i < closest.Length; i++) //foreach (Direction dir in Direction.Values) { Direction dir = closest[i]; IntVec newLocation = recentNode.position + dir; if (solid[newLocation.X, newLocation.Y] < -(recentNode.actualCost + 10) || (solid[newLocation.X, newLocation.Y] == int.MaxValue && newLocation.Equals(to))) { solid[newLocation.X, newLocation.Y] = -(recentNode.actualCost + 10); nodes.Add(new __AStarNode(newLocation, recentNode.actualCost + 10, 10 * calculateHeuristic(newLocation, to), i, recentNode, dir)); } } nodes.Remove(recentNode); } } if (bestNode != null) { isPossible = bestNode.position.Equals(to); } List <Direction> path = new List <Direction>(); recentNode = recentNode ?? bestNode; while (recentNode != null && !recentNode.position.Equals(from)) { path.Add(recentNode.directionTaken); recentNode = recentNode.reachedFrom; } path.Reverse(); return(path.ToArray()); }