/// <summary> /// Finds the shortest path from the start node to the goal node /// </summary> /// <param name="AStartNode">Start node</param> /// <param name="AGoalNode">Goal node</param> public void FindPath(AStarNode AStartNode, AStarNode AGoalNode) { int totalCost = (AStarHabbo.Map.GetLength(0) * AStarHabbo.Map.GetLength(1)) ^ 2; FStartNode = AStartNode; FGoalNode = AGoalNode; int i = 0; FOpenList.Add(FStartNode); while (FOpenList.Count > 0) { i++; if (i > totalCost) { break; } else { // Get the node with the lowest TotalCost AStarNode NodeCurrent = (AStarNode)FOpenList.Pop(); // If the node is the goal copy the path to the solution array if (NodeCurrent.IsGoal()) { while (NodeCurrent != null) { FSolution.Insert(0, NodeCurrent); NodeCurrent = NodeCurrent.Parent; } break; } // Get successors to the current node NodeCurrent.GetSuccessors(FSuccessors); foreach (AStarNode NodeSuccessor in FSuccessors) { // Test if the currect successor node is on the open list, if it is and // the TotalCost is higher, we will throw away the current successor. AStarNode NodeOpen = null; if (FOpenList.Contains(NodeSuccessor)) { NodeOpen = (AStarNode)FOpenList[FOpenList.IndexOf(NodeSuccessor)]; } if ((NodeOpen != null) && (NodeSuccessor.TotalCost > NodeOpen.TotalCost)) { continue; } // Test if the currect successor node is on the closed list, if it is and // the TotalCost is higher, we will throw away the current successor. AStarNode NodeClosed = null; if (FClosedList.Contains(NodeSuccessor)) { NodeClosed = (AStarNode)FClosedList[FClosedList.IndexOf(NodeSuccessor)]; } if ((NodeClosed != null) && (NodeSuccessor.TotalCost > NodeClosed.TotalCost)) { continue; } // Remove the old successor from the open list FOpenList.Remove(NodeOpen); // Remove the old successor from the closed list FClosedList.Remove(NodeClosed); // Add the current successor to the open list FOpenList.Push(NodeSuccessor); } // Add the current node to the closed list FClosedList.Add(NodeCurrent); } } }
public Coord getNext(int X, int Y, int goalX, int goalY) { if (X == goalX && Y == goalY) { return(new Coord(-1, 0)); } int maxCycles = (maxX * maxY) ^ 2; int Cycles = 0; ArrayList Solution = new ArrayList(); this.Goal = new mapNode(goalX, goalY, 0, null, null, null); this.Start = new mapNode(X, Y, 0, null, Goal, null); Goal.Start = Goal; Start.Start = Start; Open.Add(Start); while (Open.Count > 0) { if (Cycles >= maxCycles) { return(new Coord(-1, 0)); } else { Cycles++; } mapNode Current = (mapNode)Open.Pop(); if (Current.X == Goal.X && Current.Y == Goal.Y) { while (Current != null) { Solution.Insert(0, Current); Current = Current.Parent; } break; } else { foreach (mapNode Successor in Current.Successors(this)) { mapNode openNode = null; if (Open.Contains(Successor)) { openNode = (mapNode)Open[Open.IndexOf(Successor)]; if (Successor.totalCost > openNode.totalCost) { continue; } } mapNode closedNode = null; if (Closed.Contains(Successor)) { closedNode = (mapNode)Closed[Closed.IndexOf(Successor)]; if (Successor.totalCost > closedNode.totalCost) { continue; } } Open.Remove(Successor); Closed.Remove(Successor); Open.Push(Successor); } Closed.Add(Current); } } if (Solution.Count == 0) { return(new Coord(-1, 0)); } else { mapNode Next = (mapNode)Solution[1]; return(new Coord(Next.X, Next.Y)); } }