コード例 #1
0
ファイル: AStar.cs プロジェクト: pampersrocker/STAR
        public void FindWay(Node start, Node aim)
        {
            //Only run a new PathSearch if we don't running currently one.
            if (myThread.ThreadState != ThreadState.Running)
            {
                this.start = start;
                this.aim   = aim;
                aimglobal  = aim.ToPathNode();

                //openList.Clear();
                openLinkedList.Clear();
                //If thread is never Started, start it
                if (myThread.ThreadState == ThreadState.Unstarted)
                {
                    myThread.Start();
                }
                //If thread is Suspended from an older search, resume it
                //with the new parameters specified above.
                if (myThread.ThreadState == ThreadState.Suspended)
                {
                    myThread.Resume();
                }

                abort = false;
            }
            //FindWay();
        }
コード例 #2
0
ファイル: AStar.cs プロジェクト: pampersrocker/STAR
        private void FindWay()
        {
            while (!stop)
            {
                if (aim.Walkable == Walkable.Walkable && start.Walkable == Walkable.Walkable)
                {
                    PathNode aimNode;
                    PathNode currentNode;
                    searching = true;
                    //searchMap = MapToArray();
                    starttime = DateTime.Now;
                    List <PathNode> similarNodes = new List <PathNode>();
                    MapToArray(ref searchMap);

                    //openList.Add(start.ToPathNode());
                    openLinkedList.AddFirst(start.ToPathNode());
                    PathNodeComparer comparer = new PathNodeComparer();

                    aimNode = searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition];

                    while (openLinkedList.Count > 0 && searching && !abort)
                    {
                        if (openLinkedList.First.Value == aimNode)
                        {
                            searching = false;
                        }
                        if (searching)
                        {
                            currentNode = openLinkedList.First.Value;
                            try
                            {
                                //    openList.Remove(openList[0]);
                                //    openList.Sort(comparer);
                                openLinkedList.RemoveFirst();
                            }
                            catch (Exception)
                            {
                                abort = true;
                                //throw;
                            }
                            try
                            {
                                while (openLinkedList.First.Value.FCost == currentNode.FCost)
                                {
                                    similarNodes.Add(openLinkedList.First.Value);
                                    openLinkedList.RemoveFirst();
                                }
                            }
                            catch (Exception)
                            {
                            }
                            if (currentNode.State != NodeState.Closed && searching)
                            {
                                //ExploreNode(openList[0], searchMap);
                                ExploreNode(currentNode, searchMap);
                            }
                            for (int i = 0; i < similarNodes.Count; i++)
                            {
                                ExploreNode(similarNodes[i], searchMap);
                            }
                            similarNodes.Clear();
                            //openList.AddRange(newOpenList);



                            //if (openList.Contains(aimNode))
                            //    if (aimNode.State == NodeState.Closed)
                            //        searching = false;
                            if (openLinkedList.Contains(aimNode))
                            {
                                if (aimNode.State == NodeState.Closed)
                                {
                                    searching = false;
                                }
                            }
                            //myThread.Suspend();
                        }
                    }
                    if (!searching)
                    {
                        PathNode lastNode;
                        LinkedList <PathNode> pathnodes = new LinkedList <PathNode>();
                        pathnodes.AddLast(searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition]);
                        lastNode = searchMap[aimglobal.MapYPosition, aimglobal.MapXPosition];
                        while (lastNode.RootNode != null)
                        {
                            pathnodes.AddBefore(pathnodes.First, lastNode.RootNode);
                            lastNode = lastNode.RootNode;
                        }
                        path = new Path(pathnodes.ToList());
                        PathFound(path, (DateTime.Now - starttime).TotalMilliseconds);
                    }
                    else
                    {
                        PathFound(path, -2);
                    }
                }
                else
                {
                    PathFound(path, -1);
                }
                if (!stop)
                {
                    myThread.Suspend();
                }
            }
        }
コード例 #3
0
ファイル: AStar.cs プロジェクト: pampersrocker/STAR
        public void FindWay(Node start, Node aim)
        {
            //Only run a new PathSearch if we don't running currently one.
            if (myThread.ThreadState != ThreadState.Running)
            {
                this.start = start;
                this.aim = aim;
                aimglobal = aim.ToPathNode();

                //openList.Clear();
                openLinkedList.Clear();
                //If thread is never Started, start it
                if (myThread.ThreadState == ThreadState.Unstarted)
                    myThread.Start();
                //If thread is Suspended from an older search, resume it
                //with the new parameters specified above.
                if (myThread.ThreadState == ThreadState.Suspended)
                    myThread.Resume();

                abort = false;
            }
            //FindWay();
        }