Example #1
0
 /// <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)
 {
     m_X = AX;
     m_Y = AY;
 }
Example #2
0
 /// <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);
 }
Example #3
0
        /// <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)
        {
            m_StartNode = startNode;
            m_GoalNode  = goalNode;

            int loopBreak = 0;

            m_OpenList.Add(m_StartNode);
            while (m_OpenList.Count > 0 && loopBreak < LOOPBREAKER)
            {
                // Get the node with the lowest TotalCost
                AStarNode NodeCurrent = (AStarNode)m_OpenList.Pop();

                // If the node is the goal copy the path to the solution array
                if (NodeCurrent.IsGoal())
                {
                    while (NodeCurrent != null)
                    {
                        m_Solution.Insert(0, NodeCurrent);
                        NodeCurrent = NodeCurrent.parent;
                    }
                    break;
                }

                // Get successors to the current node
                NodeCurrent.GetSuccessors(m_Successors);
                foreach (AStarNode NodeSuccessor in 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 (m_OpenList.Contains(NodeSuccessor))
                    {
                        NodeOpen = (AStarNode)m_OpenList[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 (m_ClosedList.Contains(NodeSuccessor))
                    {
                        NodeClosed = (AStarNode)m_ClosedList[m_ClosedList.IndexOf(NodeSuccessor)];
                    }
                    if ((NodeClosed != null) && (NodeSuccessor.totalCost > NodeClosed.totalCost))
                    {
                        continue;
                    }

                    // Remove the old successor from the open list
                    m_OpenList.Remove(NodeOpen);

                    // Remove the old successor from the closed list
                    m_ClosedList.Remove(NodeClosed);

                    // Add the current successor to the open list
                    m_OpenList.Push(NodeSuccessor);
                }
                // Add the current node to the closed list
                m_ClosedList.Add(NodeCurrent);
                loopBreak += 1;
            }
        }
Example #4
0
 /// <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)
 {
     m_Parent = parentRef;
     m_Cost   = costRef;
     goalNode = goalRef;
 }