예제 #1
0
        private List <MapNode> findPath(MapNode start, MapNode end, bool eightDirection = false, bool useBinaryHeap = true)
        {
            if (useBinaryHeap)
            {
                _open = new BinaryHeapOpenList <MapNode>();
            }
            else
            {
                _open = new ListOpenList <MapNode>();
            }
            _closed = new List <MapNode>();
            List <MapNode> result = new List <MapNode>();

            start.parentNode = null;
            //设置或者不设置为0,问题不大。唯一的可能问题是寻路多次之后有可能溢出
            start.g = 0;
            start.h = 0;
            start.f = 0;
            //-----------------------
            if (end == null)
            {
                return(result);
            }
            if (!end.isPassAble())
            {
                return(result);
            }
            end.parentNode = null;
            _open.push(start);
            while ((_open.getCount() > 0) && (_closed.IndexOf(end) == -1))
            {
                //if (_open is ListOpenList<MapNode>)
                //{
                //    (_open as ListOpenList<MapNode>).printSelf();
                //}
                _current = _open.getSmallest();
                checkAround(_current, end, eightDirection);
                _closed.Add(_current);
            }

            MapNode c = end;

            while (c != null)
            {
                result.Insert(0, c);
                c = c.parentNode;
            }
            if (result.Count == 1)
            {
                result.Clear();
                result.Add(start);
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 对比测试用的非启发算法,不要使用
        /// </summary>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        /// <param name="endX"></param>
        /// <param name="endY"></param>
        /// <param name="eightDirection"></param>
        /// <param name="useBinaryHeap"></param>
        /// <returns></returns>
        public List <MapNode> findPathByXYNoInspire(int startX, int startY, int endX, int endY, bool eightDirection = false)
        {
            //Debug.Log(startX + " " + startY + " " + endX + " " + endY);
            List <MapNode> result = new List <MapNode>();

            if (startX == endX && startY == endY)
            {
                result.Add(_mapdata[startY, startX]);
                return(result);
            }
            MapNode start = _mapdata[startY, startX];
            MapNode end   = _mapdata[endY, endX];

            _open = new ListOpenList <MapNode>();
            _open.push(start);
            start.parentNode = null;
            start.g          = start.h = start.f = 0;
            end.parentNode   = null;
            _closed          = new List <MapNode>();
            while (_open.getCount() > 0 && (_closed.IndexOf(end) == -1))
            {
                //不去是用启发算法去找一个可能最优的,而是随便找一个来计算
                MapNode mn = (_open as ListOpenList <MapNode>).pop();
                //检查周围
                if (!eightDirection)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        int _x = mn.x + _xd[i];
                        int _y = mn.y + _yd[i];
                        if (_x < 0 || _y < 0 || (_x >= _width) || (_y >= _height))
                        {
                            continue;
                        }
                        MapNode testN = _mapdata[_y, _x];
                        if (_closed.IndexOf(testN) != -1)
                        {
                            continue;
                        }
                        if (!testN.isPassAble())
                        {
                            _closed.Add(testN);
                            testN.parentNode = mn;
                            continue;
                        }
                        if (_open.indexOf(testN) != -1)
                        {
                            if (testN.g > mn.g + testN.weight)
                            {
                                testN.g          = mn.g + testN.weight;
                                testN.parentNode = mn;
                            }
                            continue;
                        }
                        else
                        {
                            _open.push(testN);
                            testN.parentNode = mn;
                            testN.g          = mn.g + testN.weight;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 8; i++)
                    {
                        int _x = mn.x + _xde[i];
                        int _y = mn.y + _yde[i];
                        if (_x < 0 || _y < 0 || (_x >= _width) || (_y >= _height))
                        {
                            continue;
                        }
                        MapNode testN = _mapdata[_y, _x];
                        if (_closed.IndexOf(testN) != -1)
                        {
                            continue;
                        }
                        if (!testN.isPassAble())
                        {
                            _closed.Add(testN);
                            testN.parentNode = mn;
                            continue;
                        }
                        if (_open.indexOf(testN) != -1)
                        {
                            if (testN.g > mn.g + testN.weight)
                            {
                                testN.g          = mn.g + testN.weight;
                                testN.parentNode = mn;
                            }
                            continue;
                        }
                        else
                        {
                            _open.push(testN);
                            testN.parentNode = mn;
                            testN.g          = mn.g + testN.weight;
                        }
                    }
                }
                //加入closed
                _closed.Add(mn);
            }

            MapNode c = end;

            while (c != null)
            {
                result.Insert(0, c);
                c = c.parentNode;
            }
            if (result.Count == 1)
            {
                result.Clear();
                result.Add(start);
            }

            if (result.Count < 2)
            {
                //找不到路
            }

            return(result);
        }