Esempio n. 1
0
        private bool compareTwoNodes(long node1, long node2)
        {
            double f  = this._nodeGrid.Nodes[ANode.GetGUID_X(node1), ANode.GetGUID_Y(node1)].f;
            double f2 = this._nodeGrid.Nodes[ANode.GetGUID_X(node2), ANode.GetGUID_Y(node2)].f;

            return(f < f2);
        }
Esempio n. 2
0
        /**比较两个节点,返回true则表示第一个节点小于第二个*/
        private Boolean compareTwoNodes(long node1, long node2)
        {
            double f1 = _nodeGrid.Nodes[ANode.GetGUID_X(node1), ANode.GetGUID_Y(node1)].f;
            double f2 = _nodeGrid.Nodes[ANode.GetGUID_X(node2), ANode.GetGUID_Y(node2)].f;

            return(f1 < f2);
        }
Esempio n. 3
0
        public bool isDiagonalWalkable(long node1, long node2)
        {
            int node1x = ANode.GetGUID_X(node1);
            int node1y = ANode.GetGUID_Y(node1);
            int node2x = ANode.GetGUID_X(node2);
            int node2y = ANode.GetGUID_Y(node2);

            return(1 == this._fixedObstruction[node1x, node2y] && 1 == this._fixedObstruction[node2x, node1y]);
        }
Esempio n. 4
0
        /** 判断两个节点的对角线路线是否可走 */
        public bool isDiagonalWalkable(long node1, long node2)
        {
            int node1x = ANode.GetGUID_X(node1);
            int node1y = ANode.GetGUID_Y(node1);

            int node2x = ANode.GetGUID_X(node2);
            int node2y = ANode.GetGUID_Y(node2);

            if (1 == _fixedObstruction[node1x, node2y] && 1 == _fixedObstruction[node2x, node1y])
            {
                return(true);
            }
            return(false);
        }
Esempio n. 5
0
        public string toString()
        {
            string result = "";
            int    len    = this._data.Count;

            for (int i = 0; i < len; i++)
            {
                double f = this._nodeGrid.Nodes[ANode.GetGUID_X(this._data[i]), ANode.GetGUID_Y(this._data[i])].f;
                result += f;
                if (i < len - 1)
                {
                    result += ",";
                }
            }
            return(result);
        }
Esempio n. 6
0
        private void buildPath()
        {
            this._path = new List <ANode>();
            ANode node = new ANode(this._endNodeX, this._endNodeY);

            this._path.Add(node);
            int count = 0;

            while (node.x != this._startNodeX || node.y != this._startNodeY)
            {
                int px = this._grid.Nodes[node.x, node.y].parentX;
                int py = this._grid.Nodes[node.x, node.y].parentY;
                node = new ANode(px, py);
                this._path.Insert(0, node);
                count++;
            }
            Debug.WriteLine(string.Format("Find Path count={0}", count));
        }
Esempio n. 7
0
        private void buildPath()
        {
            _path = new List <ANode>();
            ANode node = new ANode(_endNodeX, _endNodeY);

            _path.Add(node);

            int count = 0;

            while (!(node.x == _startNodeX && node.y == _startNodeY))
            {
                int px = _grid.Nodes[node.x, node.y].parentX;
                int py = _grid.Nodes[node.x, node.y].parentY;
                node = new ANode(px, py);
                _path.Insert(0, node);
                count++;
            }

            System.Diagnostics.Debug.WriteLine(string.Format("Find Path count={0}", count));
        }
Esempio n. 8
0
 public bool search()
 {
     try
     {
         long node    = ANode.GetGUID(this._startNodeX, this._startNodeY);
         long endNode = ANode.GetGUID(this._endNodeX, this._endNodeY);
         int  nodex;
         int  nodey;
         while (node != endNode)
         {
             nodex = ANode.GetGUID_X(node);
             nodey = ANode.GetGUID_Y(node);
             int startX = (0 > nodex - 1) ? 0 : (nodex - 1);
             int endX   = (this._grid.numCols - 1 < nodex + 1) ? (this._grid.numCols - 1) : (nodex + 1);
             int startY = (0 > nodey - 1) ? 0 : (nodey - 1);
             int endY   = (this._grid.numRows - 1 < nodey + 1) ? (this._grid.numRows - 1) : (nodey + 1);
             for (int i = startX; i <= endX; i++)
             {
                 for (int j = startY; j <= endY; j++)
                 {
                     if (this._open.getLength() > AStar.MaxOpenNodeCount)
                     {
                         LogManager.WriteLog(LogTypes.Warning, string.Format("AStar:search()待检测的点太多,没必要再寻路: start({0}, {1}), to({2}, {3}), MaxOpenNodeCount={4}", new object[]
                         {
                             this._startNodeX,
                             this._startNodeY,
                             this._endNodeX,
                             this._endNodeY,
                             AStar.MaxOpenNodeCount
                         }), null, true);
                         return(false);
                     }
                     long test           = ANode.GetGUID(i, j);
                     int  testx          = i;
                     int  testy          = j;
                     bool isTestWalkable = this._grid.isWalkable(testx, testy);
                     if (test != node && isTestWalkable && this._grid.isDiagonalWalkable(node, test))
                     {
                         double cost = this._straightCost;
                         if (nodex != testx && nodey != testy)
                         {
                             cost = this._diagCost;
                         }
                         double nodeg        = this._grid.Nodes[nodex, nodey].g;
                         double g            = nodeg + cost * 1.0;
                         double h            = this.diagonal(testx, testy);
                         double f            = g + h;
                         bool   isInOpen     = this._open.indexOf(test) != -1;
                         int    indexOfClose = this.IndexOfClose(test);
                         if (isInOpen || indexOfClose != -1)
                         {
                             if (this._grid.Nodes[testx, testy].f > f)
                             {
                                 this._grid.Nodes[testx, testy].f       = f;
                                 this._grid.Nodes[testx, testy].g       = g;
                                 this._grid.Nodes[testx, testy].h       = h;
                                 this._grid.Nodes[testx, testy].parentX = nodex;
                                 this._grid.Nodes[testx, testy].parentY = nodey;
                                 if (isInOpen)
                                 {
                                     this._open.updateNode(test);
                                 }
                             }
                         }
                         else
                         {
                             this._grid.Nodes[testx, testy].f       = f;
                             this._grid.Nodes[testx, testy].g       = g;
                             this._grid.Nodes[testx, testy].h       = h;
                             this._grid.Nodes[testx, testy].parentX = nodex;
                             this._grid.Nodes[testx, testy].parentY = nodey;
                             this._open.push(test);
                         }
                     }
                 }
             }
             this._closed[node] = true;
             if (this._open.getLength() == 0)
             {
                 return(false);
             }
             node = this._open.shift();
         }
         nodex          = ANode.GetGUID_X(node);
         nodey          = ANode.GetGUID_Y(node);
         this._endNodeX = nodex;
         this._endNodeY = nodey;
         this.buildPath();
     }
     catch (Exception e)
     {
         Debug.WriteLine(e.Message);
     }
     return(true);
 }
Esempio n. 9
0
        public bool search()
        {
            try
            {
                //异步运算。当上一次遍历超出最大允许值后停止遍历,下一次从
                //上次暂停处开始继续遍历
                long node    = ANode.GetGUID(_startNodeX, _startNodeY);
                long endNode = ANode.GetGUID(_endNodeX, _endNodeY);

                int nodex = 0;
                int nodey = 0;

                while (node != endNode)
                {
                    nodex = ANode.GetGUID_X(node);
                    nodey = ANode.GetGUID_Y(node);

                    int startX = 0 > nodex - 1 ? 0 : nodex - 1;
                    int endX   = _grid.numCols - 1 < nodex + 1 ? _grid.numCols - 1 : nodex + 1;
                    int startY = 0 > nodey - 1 ? 0 : nodey - 1;
                    int endY   = _grid.numRows - 1 < nodey + 1 ? _grid.numRows - 1 : nodey + 1;

                    for (int i = startX; i <= endX; i++)
                    {
                        for (int j = startY; j <= endY; j++)
                        {
                            //待检测的点太多,没必要再寻路了
                            if (_open.getLength() > MaxOpenNodeCount)
                            {
                                string warning = String.Format("AStar:search()待检测的点太多,没必要再寻路: start({0}, {1}), to({2}, {3}), MaxOpenNodeCount={4}",
                                                               _startNodeX, _startNodeY, _endNodeX, _endNodeY, MaxOpenNodeCount);
                                LogManager.WriteLog(LogTypes.Warning, warning);
                                return(false);
                            }

                            long test = ANode.GetGUID(i, j);

                            int testx = i;
                            int testy = j;

                            bool isTestWalkable = _grid.isWalkable(testx, testy);
                            if (test == node || !isTestWalkable ||
                                !_grid.isDiagonalWalkable(node, test))
                            {
                                continue;
                            }

                            double cost = _straightCost;

                            if (!((nodex == testx) || (nodey == testy)))
                            {
                                cost = _diagCost;
                            }

                            double nodeg = _grid.Nodes[nodex, nodey].g;
                            double g     = nodeg + cost * costMultiplier;
                            double h     = diagonal(testx, testy);
                            double f     = g + h;

                            Boolean isInOpen     = _open.indexOf(test) != -1;
                            int     indexOfClose = IndexOfClose(test);

                            if (isInOpen || indexOfClose != -1)
                            {
                                if (_grid.Nodes[testx, testy].f > f)
                                {
                                    _grid.Nodes[testx, testy].f       = f;
                                    _grid.Nodes[testx, testy].g       = g;
                                    _grid.Nodes[testx, testy].h       = h;
                                    _grid.Nodes[testx, testy].parentX = nodex;
                                    _grid.Nodes[testx, testy].parentY = nodey;

                                    if (isInOpen)
                                    {
                                        _open.updateNode(test);
                                    }
                                }
                            }
                            else
                            {
                                _grid.Nodes[testx, testy].f       = f;
                                _grid.Nodes[testx, testy].g       = g;
                                _grid.Nodes[testx, testy].h       = h;
                                _grid.Nodes[testx, testy].parentX = nodex;
                                _grid.Nodes[testx, testy].parentY = nodey;
                                _open.push(test);
                            }
                        }
                    }

                    _closed[node] = true;
                    if (_open.getLength() == 0)
                    {
                        return(false);
                    }

                    node = _open.shift();
                }

                //所有的node,都由很多副本,所以,很多时候,必须生成确定的副本
                nodex = ANode.GetGUID_X(node);
                nodey = ANode.GetGUID_Y(node);

                _endNodeX = nodex;
                _endNodeY = nodey;

                buildPath();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }

            return(true);
        }