/// <summary> /// Constructor for a node in a 2-dimensional map /// </summary> /// <param name="parentRef">Parent of the node</param> /// <param name="goalRef">Goal node</param> /// <param name="costRef">Accumulative cost</param> /// <param name="AX">x-coordinate</param> /// <param name="AY">y-coordinate</param> public AStarNode2D(AStarNode parentRef, AStarNode goalRef, double costRef, int AX, int AY) : base(parentRef, goalRef, costRef) { this.m_X = AX; this.m_Y = AY; }
/// <summary> /// Determines wheather the current node is the same state as the on passed. /// </summary> /// <param name="node">AStarNode to compare the current node to</param> /// <returns>Returns true if they are the same state</returns> public virtual bool IsSameState(AStarNode node) { return(false); }
/// <summary> /// Finds the shortest path from the start node to the goal node /// </summary> /// <param name="startNode">Start node</param> /// <param name="goalNode">Goal node</param> public void FindPath(AStarNode startNode, AStarNode goalNode) { this.m_StartNode = startNode; this.m_GoalNode = goalNode; int loopBreak = 0; this.m_OpenList.Add(this.m_StartNode); while (this.m_OpenList.Count > 0 && loopBreak < LOOPBREAKER) { // Get the node with the lowest TotalCost AStarNode NodeCurrent = (AStarNode)this.m_OpenList.Pop(); // If the node is the goal copy the path to the solution array if (NodeCurrent.IsGoal()) { while (NodeCurrent != null) { this.m_Solution.Insert(0, NodeCurrent); NodeCurrent = NodeCurrent.parent; } break; } // Get successors to the current node NodeCurrent.GetSuccessors(this.m_Successors); foreach (AStarNode NodeSuccessor in this.m_Successors) { // 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 (this.m_OpenList.Contains(NodeSuccessor)) { NodeOpen = (AStarNode)this.m_OpenList[this.m_OpenList.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 (this.m_ClosedList.Contains(NodeSuccessor)) { NodeClosed = (AStarNode)this.m_ClosedList[this.m_ClosedList.IndexOf(NodeSuccessor)]; } if ((NodeClosed != null) && (NodeSuccessor.totalCost > NodeClosed.totalCost)) { continue; } // Remove the old successor from the open list this.m_OpenList.Remove(NodeOpen); // Remove the old successor from the closed list this.m_ClosedList.Remove(NodeClosed); // Add the current successor to the open list this.m_OpenList.Push(NodeSuccessor); } // Add the current node to the closed list this.m_ClosedList.Add(NodeCurrent); loopBreak += 1; } }
/// <summary> /// Constructor. /// </summary> /// <param name="parentRef">The node's parent</param> /// <param name="goalRef">The goal node</param> /// <param name="costRef">The accumulative cost until now</param> public AStarNode(AStarNode parentRef, AStarNode goalRef, double costRef) { this.m_Parent = parentRef; this.m_Cost = costRef; this.goalNode = goalRef; }