示例#1
0
        //public bool Stopped
        //{
        //    get { return mStopped; }
        //}

        //public int Formula
        //{
        //    get { return hFormula; }
        //    set { hFormula = value; }
        //}

        //public bool Diagonals
        //{
        //    get { return mDiagonals; }
        //    set { mDiagonals = value; }
        //}



        //public int HeuristicEstimate
        //{
        //    get { return mHEstimate; }
        //    set { mHEstimate = value; }
        //}

        //public int SearchLimit
        //{
        //    get { return mSearchLimit; }
        //    set { mSearchLimit = value; }
        //}

        //public double CompletedTime
        //{
        //    get { return mCompletedTime; }
        //    set { mCompletedTime = value; }
        //}

        //public void FindPathStop()
        //{
        //    mStop = true;
        //}

        public List <PathFinderNode> FindPath(Point start, Point end, int num)
        {
            PathFinderNode parentNode;
            bool           found = false;
            int            gridX = mGrid.GetUpperBound(0);
            int            gridY = mGrid.GetUpperBound(1);

            mStop    = false;
            mStopped = false;
            mOpen.Clear();
            mClose.Clear();

            parentNode.g  = 0;
            parentNode.h  = mHEstimate;
            parentNode.f  = parentNode.g + parentNode.h;
            parentNode.x  = start.X;
            parentNode.y  = start.Y;
            parentNode.px = parentNode.x;
            parentNode.py = parentNode.y;
            mOpen.Push(parentNode);
            while (mOpen.Count > 0 && !mStop)
            {
                parentNode = mOpen.Pop();
                if (parentNode.x == end.X && parentNode.y == end.Y)
                {
                    mClose.Add(parentNode);
                    found = true;
                    break;
                }
                if (mClose.Count > mSearchLimit)
                {
                    mStopped = true;
                    return(null);
                }
                sbyte[,] direction = new sbyte[4, 2] {
                    { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
                };
                for (int i = 0; i < 4; i++)
                {
                    PathFinderNode newNode = new PathFinderNode();
                    newNode.x = parentNode.x + direction[i, 0];
                    newNode.y = parentNode.y + direction[i, 1];
                    if (newNode.x < 0 || newNode.y < 0 || newNode.x > num - 1 || newNode.y > num - 1)
                    {
                        continue;
                    }
                    if (isHurdle[newNode.x, newNode.y])
                    {
                        continue;
                    }
                    int newG = parentNode.g + mGrid[newNode.x, newNode.y];

                    if (newG == parentNode.g)
                    {
                        continue;
                    }

                    int foundInOpenIndex = -1;
                    for (int j = 0; j < mOpen.Count; j++)
                    {
                        if (mOpen[j].x == newNode.x && mOpen[j].y == newNode.y)
                        {
                            foundInOpenIndex = j;
                            break;
                        }
                    }

                    if (foundInOpenIndex != -1 && mOpen[foundInOpenIndex].g <= newG)
                    {
                        continue;
                    }

                    int foundInCloseIndex = -1;
                    for (int j = 0; j < mClose.Count; j++)
                    {
                        if (mClose[j].x == newNode.x && mClose[j].y == newNode.y)
                        {
                            foundInCloseIndex = j;
                            break;
                        }
                    }
                    if (foundInCloseIndex != -1 && mClose[foundInCloseIndex].g <= newG)
                    {
                        continue;
                    }

                    newNode.px = parentNode.x;
                    newNode.py = parentNode.y;
                    newNode.f  = newG;

                    newNode.f = mHEstimate * (Math.Abs(newNode.x - end.X) + Math.Abs(newNode.y - end.Y));
                    newNode.f = newNode.f + newNode.f;
                    mOpen.Push(newNode);
                }
                mClose.Add(parentNode);
            }
            if (found)
            {
                PathFinderNode fNode = mClose[mClose.Count - 1];
                for (int i = mClose.Count - 1; i >= 0; i--)
                {
                    if (fNode.px == mClose[i].x && fNode.py == mClose[i].y || i == mClose.Count - 1)
                    {
                        fNode = mClose[i];
                    }
                    else
                    {
                        mClose.RemoveAt(i);
                    }
                }
                mStopped = true;
                return(mClose);
            }
            mStopped = true;
            return(null);
        }
        public List <PathFinderNode> FindPath(Point start, Point end)
        {
            PathFinderNode parentNode;
            bool           found = false;
            int            gridX = mGrid.GetUpperBound(0);
            int            gridY = mGrid.GetUpperBound(1);

            mStop    = false;
            mStopped = false;
            mOpen.Clear();
            mClose.Clear();

            sbyte[,] direction;
            if (mDiagonals)
            {
                direction = new sbyte[8, 2] {
                    { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 }
                }
            }
            ;
            else
            {
                direction = new sbyte[4, 2] {
                    { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
                }
            };


            parentNode.G  = 0;
            parentNode.H  = mHEstimate;
            parentNode.F  = parentNode.G + parentNode.H;
            parentNode.x  = start.X;
            parentNode.y  = start.Y;
            parentNode.px = parentNode.x;
            parentNode.py = parentNode.y;
            mOpen.Push(parentNode);
            while (mOpen.Count > 0 && !mStop)
            {
                parentNode = mOpen.Pop();

                if (parentNode.x == end.X && parentNode.x == end.Y)
                {
                    mClose.Add(parentNode);
                    found = true;
                    break;
                }

                if (mClose.Count > mSearchLimit)
                {
                    mStopped = true;
                    return(null);
                }
                for (int i = 0; i < (mDiagonals ? 8 : 4); i++)
                {
                    PathFinderNode newNode;
                    newNode.x = parentNode.x + direction[i, 0];
                    newNode.y = parentNode.y + direction[i, 1];

                    if (newNode.x < 0 || newNode.y < 0 || newNode.x >= gridX || newNode.y >= gridY)
                    {
                        continue;
                    }

                    int newG = parentNode.G + mGrid[newNode.x, newNode.y];


                    if (newG == parentNode.G)
                    {
                        continue;
                    }

                    int foundInOpenIndex = -1;
                    for (int j = 0; j < mOpen.Count; j++)
                    {
                        if (mOpen[j].x == newNode.x && mOpen[j].y == newNode.y)
                        {
                            foundInOpenIndex = j;
                            break;
                        }
                    }
                    if (foundInOpenIndex != -1 && mOpen[foundInOpenIndex].G <= newG)
                    {
                        continue;
                    }

                    int foundInCloseIndex = -1;
                    for (int j = 0; j < mClose.Count; j++)
                    {
                        if (mClose[j].x == newNode.x && mClose[j].y == newNode.y)
                        {
                            foundInCloseIndex = j;
                            break;
                        }
                    }
                    if (foundInCloseIndex != -1 && mClose[foundInCloseIndex].G <= newG)
                    {
                        continue;
                    }

                    newNode.px = parentNode.x;
                    newNode.py = parentNode.y;
                    newNode.G  = newG;

                    newNode.H = mHEstimate * (Math.Abs(newNode.x - end.X) + Math.Abs(newNode.y - end.Y));
                    newNode.F = newNode.G + newNode.H;
                    mOpen.Push(newNode);
                }

                mClose.Add(parentNode);
            }

            // mCompletedTime = HighResolutionTime.GetTime();
            if (found)
            {
                PathFinderNode fNode = mClose[mClose.Count - 1];
                for (int i = mClose.Count - 1; i >= 0; i--)
                {
                    if (fNode.px == mClose[i].x && fNode.py == mClose[i].y || i == mClose.Count - 1)
                    {
                        fNode = mClose[i];
                    }
                    else
                    {
                        mClose.RemoveAt(i);
                    }
                }
                mStopped = true;
                return(mClose);
            }
            mStopped = true;
            return(null);
        }