예제 #1
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            AStarSearchNode <T> objNode = (AStarSearchNode <T>)obj;

            return(node.Equals(objNode.node));
        }
예제 #2
0
        public T[] PerformSearch(T origin,
                                 T destination,
                                 AStarMapFeeder <T> feeder)
        {
            ClearSearch();
            AddOpenNode(origin);

            while (searchOpenNodes.Count > 0 && !feeder.SameNode(searchOpenNodes[0].node, destination))
            {
                AStarSearchNode <T> current = searchOpenNodes[0];
                RemoveOpenNode(current);
                AddClosedNode(current);

                visitedNodes.Add(current.node);

                T[] neighbours = feeder.GetNeighbours(current.node);
                foreach (T neighbour in neighbours)
                {
                    float cost = current.pathCost + feeder.GetMoveCost(current.node, neighbour);

                    AStarSearchNode <T> neighbourSearch = searchNodes.ContainsKey(neighbour)?
                                                          searchNodes[neighbour] :
                                                          new AStarSearchNode <T>(neighbour,
                                                                                  cost,
                                                                                  cost + feeder.GetDistanceEstimation(neighbour, destination),
                                                                                  current.node);
                    bool inOpen      = openNodes.Contains(neighbour);
                    bool inClosed    = closedNodes.Contains(neighbour);
                    bool smallerCost = cost < neighbourSearch.pathCost;

                    if (inOpen && smallerCost)
                    {
                        RemoveOpenNode(neighbourSearch);
                    }
                    else if (inClosed && smallerCost)
                    {
                        RemoveClosedNode(neighbourSearch);
                    }
                    else if (!inOpen && !inClosed)
                    {
                        AddOpenNode(neighbourSearch);
                    }
                }
            }

            if (searchOpenNodes.Count > 0)
            {
                path = GetParentPathFrom(searchOpenNodes[0], origin, feeder);
            }

            return(path);
        }
예제 #3
0
        private void AddOpenNode(AStarSearchNode <T> searchNode)
        {
            if (openNodes.Contains(searchNode.node) || searchNodes.ContainsKey(searchNode.node))
            {
                return;
            }

            searchOpenNodes.Add(searchNode);
            searchNodes.Add(searchNode.node, searchNode);
            openNodes.Add(searchNode.node);


            searchOpenNodes.Sort((AStarSearchNode <T> searchNode1,
                                  AStarSearchNode <T> searchNode2) =>
            {
                return(searchNode1.queueRank.CompareTo(searchNode2.queueRank));
            });
        }
예제 #4
0
        private T[] GetParentPathFrom(AStarSearchNode <T> searchNode, T origin, AStarMapFeeder <T> feeder)
        {
            List <T> parentList = new List <T>();

            do
            {
                parentList.Add(searchNode.node);

                if (searchNodes.ContainsKey(searchNode.parent))
                {
                    searchNode = searchNodes[searchNode.parent];
                }
            }while(!feeder.SameNode(searchNode.node, origin) &&
                   !feeder.SameNode(searchNode.node, searchNode.parent));

            if (feeder.SameNode(searchNode.node, origin))
            {
                parentList.Add(searchNode.node);
            }

            parentList.Reverse();
            return(parentList.ToArray());
        }
예제 #5
0
 private void RemoveClosedNode(AStarSearchNode <T> searchNode)
 {
     searchClosedNodes.Remove(searchNode);
     closedNodes.Remove(searchNode.node);
 }
예제 #6
0
 private void AddClosedNode(AStarSearchNode <T> searchNode)
 {
     searchClosedNodes.Add(searchNode);
     closedNodes.Add(searchNode.node);
 }
예제 #7
0
 private void RemoveOpenNode(AStarSearchNode <T> searchNode)
 {
     searchOpenNodes.Remove(searchNode);
     openNodes.Remove(searchNode.node);
 }
예제 #8
0
        private void AddOpenNode(T node)
        {
            AStarSearchNode <T> searchNode = new AStarSearchNode <T>(node);

            AddOpenNode(searchNode);
        }