Beispiel #1
0
        public List <Node> AStar()
        {
            // Pop off lowest F
            int? min     = OpenList.Min(n => n.F);
            Node current = OpenList.Where(n => n.F == min).First();

            OpenList.Remove(current);

            // Check for goal
            if (current.Equals(Goal))
            {
                ClosedList.Push(current);
                return(ClosedList.Reverse().ToList());
            }

            else
            {
                //possibleMoves = GetPassibleNodes(current);
                OpenList = GetPassibleNodes(current);
                List <Node> toRemove = new List <Node>();

                OpenList.ForEach(n =>
                {
                    if (ClosedList.Contains(n))
                    {
                        toRemove.Add(n);
                    }
                    else
                    {
                        GetF(n);
                    }
                });

                toRemove.ForEach(n => OpenList.Remove(n));

                //possibleMoves.ForEach(n =>
                //{
                //    if (!ClosedList.Contains(n))        // Dont move to an already visited Node
                //    {
                //        GetF(n);
                //        if (!OpenList.Contains(n))
                //        {
                //            n.Parent = current;
                //            OpenList.Add(n);
                //        }
                //    }
                //});
            }

            ClosedList.Push(current);


            return(null);
        }
        public bool FindTheShortesPath(Node Start, Node Target)
        {
            Node CurrentNode = Start;

            OpenList.Add(CurrentNode);

            for (; OpenList.Count > 0;)
            {
                CurrentNode = OpenList[0];

                if (CurrentNode.Equals(Target))
                {
                    VisitedNodes.Add(Target);

                    for (int Index = 0; Index < VisitedNodes.Count - 1; Index++)
                    {
                        ShortesPath.Add(InnerGraph.FindEdge(VisitedNodes[Index], VisitedNodes[Index + 1]));
                    }

                    return(true);
                }

                OpenList.Remove(CurrentNode);

                ClosedList.Add(CurrentNode);

                foreach (Node Inheritor in CurrentNode.Inheritors.Where(Node => Node != null && Node.Index != Node.Inheritors.Length - 1))
                {
                    if (!ClosedList.Contains(Inheritor))
                    {
                        if (!OpenList.Contains(Inheritor))
                        {
                            Inheritor[Inheritor.Index] = CurrentNode;

                            Inheritor.HeuristicPathWeight = CalculateHeuristic(Inheritor, Target);

                            Inheritor.GainedPathWeight = InnerGraph.FindEdge(CurrentNode, Inheritor).Weight;

                            Inheritor.TotalPathWeight = Inheritor.GainedPathWeight + Inheritor.HeuristicPathWeight;

                            OpenList.Add(Inheritor);

                            OpenList = OpenList.OrderBy(Node => Node.TotalPathWeight).ToList <Node>();
                        }
                    }
                }

                VisitedNodes.Add(CurrentNode);
            }

            return(true);
        }
Beispiel #3
0
    public override ArrayList FindPaths(int StartPos, int InEndPos, int InID)
    {
        ArrayList Paths = new ArrayList();

        if (!IsValidPosIndex(StartPos) || !IsValidPosIndex(InEndPos))
        {
            return(Paths);
        }

        CTimeCheck.Start(InID);
        EndPos = InEndPos;

        ClosedList.Clear();
        OpenList.Clear();
        foreach (Tile tile in Tiles)
        {
            if (tile.bBlock)
            {
                ClosedList.Add(tile.Index);
            }

            tile.Reset();
        }

        ClosedList.Add(StartPos);
        int FindNextPath         = StartPos;
        int path_make_start_time = Environment.TickCount;

        while (true)
        {
            //if (100 <= Environment.TickCount - path_make_start_time)
            //    return Paths;

            ArrayList MakedOpenList = MakeOpenList(FindNextPath);

            if (OpenList.Count == 0)
            {
                CDebugLog.Log(ELogType.AStar, "OpenList.Count == 0");
            }

            //FindNextPath = FindNextPathIndex(ref MakedOpenList);
            ////FindNextPath = FindNextPathIndexFromOpenList();

            //if (-1 == FindNextPath)
            //{
            FindNextPath = FindNextPathIndexFromOpenList();
            //}

            if (-1 == FindNextPath)
            {
                CTimeCheck.End(ELogType.AStar, "Failed FindPaths");
                return(Paths);
            }

            if (FindNextPath == EndPos)
            {
                break;
            }

            OpenList.Remove(FindNextPath);
            ClosedList.Add(FindNextPath);
        }

        int path_index = EndPos;

        Paths.Add(EndPos);
        while (true)
        {
            if (path_index == StartPos)
            {
                break;
            }

            Paths.Add(((Tile)Tiles[path_index]).ParentTile);
            path_index = ((Tile)Tiles[path_index]).ParentTile;
        }
        Paths.Reverse();


        CTimeCheck.End(ELogType.AStar, "Success FindPaths");
        return(Paths);
    }