Ejemplo n.º 1
0
 public Node GetNode(GridPos iPos)
 {
     if (this.m_nodes.ContainsKey(iPos))
     {
         return this.m_nodes[iPos];
     }
     return null;
 }
        public JumpPointParam(
            IBaseGrid iGrid,
            GridPos iStartPos,
            GridPos iEndPos,
            bool iAllowEndNodeUnWalkable = true,
            bool iCrossCorner = true,
            bool iCrossAdjacentPoint = true,
            HeuristicMode iMode = HeuristicMode.EUCLIDEAN)
        {
            switch (iMode)
            {
                case HeuristicMode.MANHATTAN:
                    this.m_heuristic = new HeuristicDelegate(Heuristic.Manhattan);
                    break;
                case HeuristicMode.EUCLIDEAN:
                    this.m_heuristic = new HeuristicDelegate(Heuristic.Euclidean);
                    break;
                case HeuristicMode.CHEBYSHEV:
                    this.m_heuristic = new HeuristicDelegate(Heuristic.Chebyshev);
                    break;
                default:
                    this.m_heuristic = new HeuristicDelegate(Heuristic.Euclidean);
                    break;
            }
            this.m_allowEndNodeUnWalkable = iAllowEndNodeUnWalkable;
            this.m_crossAdjacentPoint = iCrossAdjacentPoint;
            this.m_crossCorner = iCrossCorner;
            this.openList = new List<Node>();

            this.m_searchGrid = iGrid;
            this.m_startNode = this.m_searchGrid.GetNodeAt(iStartPos.x, iStartPos.y);
            this.m_endNode = this.m_searchGrid.GetNodeAt(iEndPos.x, iEndPos.y);
            if (this.m_startNode == null)
            {
                this.m_startNode = new Node(iStartPos.x, iStartPos.y, true);
            }
            if (this.m_endNode == null)
            {
                this.m_endNode = new Node(iEndPos.x, iEndPos.y, true);
            }
            this.m_useRecursive = false;
        }
Ejemplo n.º 3
0
 public Node SetNode(GridPos iPos, bool? iWalkable = null)
 {
     if (iWalkable.HasValue)
     {
         if (iWalkable.Value == true)
         {
             if (this.m_nodes.ContainsKey(iPos))
             {
                 return this.m_nodes[iPos];
             }
             var newNode = new Node(iPos.x, iPos.y, iWalkable);
             this.m_nodes.Add(iPos, newNode);
             return newNode;
         }
         else
         {
             this.removeNode(iPos);
         }
     }
     else
     {
         var newNode = new Node(iPos.x, iPos.y, true);
         this.m_nodes.Add(iPos, newNode);
         return newNode;
     }
     return null;
 }
Ejemplo n.º 4
0
 public Node SetNode(int iX, int iY, bool? iWalkable = null)
 {
     var pos = new GridPos(iX, iY);
     return SetNode(pos, iWalkable);
 }
Ejemplo n.º 5
0
 public Node GetNode(int iX, int iY)
 {
     var pos = new GridPos(iX, iY);
     return this.GetNode(pos);
 }
Ejemplo n.º 6
0
 protected void removeNode(GridPos iPos)
 {
     if (this.m_nodes.ContainsKey(iPos))
     {
         this.m_nodes.Remove(iPos);
     }
 }
Ejemplo n.º 7
0
 protected void removeNode(int iX, int iY)
 {
     var pos = new GridPos(iX, iY);
     this.removeNode(pos);
 }
        private static GridPos? jumpLoop(JumpPointParam iParam, int iX, int iY, int iPx, int iPy)
        {
            GridPos? retVal = null;
            Stack<JumpSnapshot> stack = new Stack<JumpSnapshot>();

            JumpSnapshot currentSnapshot = new JumpSnapshot();
            JumpSnapshot newSnapshot = null;
            currentSnapshot.iX = iX;
            currentSnapshot.iY = iY;
            currentSnapshot.iPx = iPx;
            currentSnapshot.iPy = iPy;
            currentSnapshot.stage = 0;

            stack.Push(currentSnapshot);
            while (stack.Count != 0)
            {
                currentSnapshot = stack.Pop();
                switch (currentSnapshot.stage)
                {
                    case 0:
                        if (!iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX, currentSnapshot.iY))
                        {
                            retVal = null;
                            continue;
                        }
                        else if (
                            iParam.SearchGrid.GetNodeAt(currentSnapshot.iX, currentSnapshot.iY)
                                .Equals(iParam.EndNode))
                        {
                            retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                            continue;
                        }

                        currentSnapshot.tDx = currentSnapshot.iX - currentSnapshot.iPx;
                        currentSnapshot.tDy = currentSnapshot.iY - currentSnapshot.iPy;
                        currentSnapshot.jx = null;
                        currentSnapshot.jy = null;
                        if (iParam.CrossCorner)
                        {
                            // check for forced neighbors
                            // along the diagonal
                            if (currentSnapshot.tDx != 0 && currentSnapshot.tDy != 0)
                            {
                                if ((iParam.SearchGrid.IsWalkableAt(
                                    currentSnapshot.iX - currentSnapshot.tDx,
                                    currentSnapshot.iY + currentSnapshot.tDy)
                                     && !iParam.SearchGrid.IsWalkableAt(
                                         currentSnapshot.iX - currentSnapshot.tDx,
                                         currentSnapshot.iY))
                                    || (iParam.SearchGrid.IsWalkableAt(
                                        currentSnapshot.iX + currentSnapshot.tDx,
                                        currentSnapshot.iY - currentSnapshot.tDy)
                                        && !iParam.SearchGrid.IsWalkableAt(
                                            currentSnapshot.iX,
                                            currentSnapshot.iY - currentSnapshot.tDy)))
                                {
                                    retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                    continue;
                                }
                            }
                                // horizontally/vertically
                            else
                            {
                                if (currentSnapshot.tDx != 0)
                                {
                                    // moving along x
                                    if ((iParam.SearchGrid.IsWalkableAt(
                                        currentSnapshot.iX + currentSnapshot.tDx,
                                        currentSnapshot.iY + 1)
                                         && !iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX, currentSnapshot.iY + 1))
                                        || (iParam.SearchGrid.IsWalkableAt(
                                            currentSnapshot.iX + currentSnapshot.tDx,
                                            currentSnapshot.iY - 1)
                                            && !iParam.SearchGrid.IsWalkableAt(
                                                currentSnapshot.iX,
                                                currentSnapshot.iY - 1)))
                                    {
                                        retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                        continue;
                                    }
                                }
                                else
                                {
                                    if ((iParam.SearchGrid.IsWalkableAt(
                                        currentSnapshot.iX + 1,
                                        currentSnapshot.iY + currentSnapshot.tDy)
                                         && !iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX + 1, currentSnapshot.iY))
                                        || (iParam.SearchGrid.IsWalkableAt(
                                            currentSnapshot.iX - 1,
                                            currentSnapshot.iY + currentSnapshot.tDy)
                                            && !iParam.SearchGrid.IsWalkableAt(
                                                currentSnapshot.iX - 1,
                                                currentSnapshot.iY)))
                                    {
                                        retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                        continue;
                                    }
                                }
                            }
                            // when moving diagonally, must check for vertical/horizontal jump points
                            if (currentSnapshot.tDx != 0 && currentSnapshot.tDy != 0)
                            {
                                currentSnapshot.stage = 1;
                                stack.Push(currentSnapshot);

                                newSnapshot = new JumpSnapshot();
                                newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                                newSnapshot.iY = currentSnapshot.iY;
                                newSnapshot.iPx = currentSnapshot.iX;
                                newSnapshot.iPy = currentSnapshot.iY;
                                newSnapshot.stage = 0;
                                stack.Push(newSnapshot);
                                continue;
                            }

                            // moving diagonally, must make sure one of the vertical/horizontal
                            // neighbors is open to allow the path

                            // moving diagonally, must make sure one of the vertical/horizontal
                            // neighbors is open to allow the path
                            if (iParam.SearchGrid.IsWalkableAt(
                                currentSnapshot.iX + currentSnapshot.tDx,
                                currentSnapshot.iY)
                                || iParam.SearchGrid.IsWalkableAt(
                                    currentSnapshot.iX,
                                    currentSnapshot.iY + currentSnapshot.tDy))
                            {
                                newSnapshot = new JumpSnapshot();
                                newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                                newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                                newSnapshot.iPx = currentSnapshot.iX;
                                newSnapshot.iPy = currentSnapshot.iY;
                                newSnapshot.stage = 0;
                                stack.Push(newSnapshot);
                                continue;
                            }
                            else if (iParam.CrossAdjacentPoint)
                            {
                                newSnapshot = new JumpSnapshot();
                                newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                                newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                                newSnapshot.iPx = currentSnapshot.iX;
                                newSnapshot.iPy = currentSnapshot.iY;
                                newSnapshot.stage = 0;
                                stack.Push(newSnapshot);
                                continue;
                            }
                        }
                        else //if (!iParam.CrossCorner)
                        {
                            // check for forced neighbors
                            // along the diagonal
                            if (currentSnapshot.tDx != 0 && currentSnapshot.tDy != 0)
                            {
                                if ((iParam.SearchGrid.IsWalkableAt(
                                    currentSnapshot.iX + currentSnapshot.tDx,
                                    currentSnapshot.iY + currentSnapshot.tDy)
                                     && iParam.SearchGrid.IsWalkableAt(
                                         currentSnapshot.iX,
                                         currentSnapshot.iY + currentSnapshot.tDy)
                                     && !iParam.SearchGrid.IsWalkableAt(
                                         currentSnapshot.iX + currentSnapshot.tDx,
                                         currentSnapshot.iY))
                                    || (iParam.SearchGrid.IsWalkableAt(
                                        currentSnapshot.iX + currentSnapshot.tDx,
                                        currentSnapshot.iY + currentSnapshot.tDy)
                                        && iParam.SearchGrid.IsWalkableAt(
                                            currentSnapshot.iX + currentSnapshot.tDx,
                                            currentSnapshot.iY)
                                        && !iParam.SearchGrid.IsWalkableAt(
                                            currentSnapshot.iX,
                                            currentSnapshot.iY + currentSnapshot.tDy)))
                                {
                                    retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                    continue;
                                }
                            }
                                // horizontally/vertically
                            else
                            {
                                if (currentSnapshot.tDx != 0)
                                {
                                    // moving along x
                                    if ((iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX, currentSnapshot.iY + 1)
                                         && !iParam.SearchGrid.IsWalkableAt(
                                             currentSnapshot.iX - currentSnapshot.tDx,
                                             currentSnapshot.iY + 1))
                                        || (iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX, currentSnapshot.iY - 1)
                                            && !iParam.SearchGrid.IsWalkableAt(
                                                currentSnapshot.iX - currentSnapshot.tDx,
                                                currentSnapshot.iY - 1)))
                                    {
                                        retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                        continue;
                                    }
                                }
                                else
                                {
                                    if ((iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX + 1, currentSnapshot.iY)
                                         && !iParam.SearchGrid.IsWalkableAt(
                                             currentSnapshot.iX + 1,
                                             currentSnapshot.iY - currentSnapshot.tDy))
                                        || (iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX - 1, currentSnapshot.iY)
                                            && !iParam.SearchGrid.IsWalkableAt(
                                                currentSnapshot.iX - 1,
                                                currentSnapshot.iY - currentSnapshot.tDy)))
                                    {
                                        retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                                        continue;
                                    }
                                }
                            }

                            // when moving diagonally, must check for vertical/horizontal jump points
                            if (currentSnapshot.tDx != 0 && currentSnapshot.tDy != 0)
                            {
                                currentSnapshot.stage = 3;
                                stack.Push(currentSnapshot);

                                newSnapshot = new JumpSnapshot();
                                newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                                newSnapshot.iY = currentSnapshot.iY;
                                newSnapshot.iPx = currentSnapshot.iX;
                                newSnapshot.iPy = currentSnapshot.iY;
                                newSnapshot.stage = 0;
                                stack.Push(newSnapshot);
                                continue;
                            }

                            // moving diagonally, must make sure both of the vertical/horizontal
                            // neighbors is open to allow the path
                            if (iParam.SearchGrid.IsWalkableAt(
                                currentSnapshot.iX + currentSnapshot.tDx,
                                currentSnapshot.iY)
                                && iParam.SearchGrid.IsWalkableAt(
                                    currentSnapshot.iX,
                                    currentSnapshot.iY + currentSnapshot.tDy))
                            {
                                newSnapshot = new JumpSnapshot();
                                newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                                newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                                newSnapshot.iPx = currentSnapshot.iX;
                                newSnapshot.iPy = currentSnapshot.iY;
                                newSnapshot.stage = 0;
                                stack.Push(newSnapshot);
                                continue;
                            }
                        }
                        retVal = null;
                        break;
                    case 1:
                        currentSnapshot.jx = retVal;

                        currentSnapshot.stage = 2;
                        stack.Push(currentSnapshot);

                        newSnapshot = new JumpSnapshot();
                        newSnapshot.iX = currentSnapshot.iX;
                        newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                        newSnapshot.iPx = currentSnapshot.iX;
                        newSnapshot.iPy = currentSnapshot.iY;
                        newSnapshot.stage = 0;
                        stack.Push(newSnapshot);
                        break;
                    case 2:
                        currentSnapshot.jy = retVal;
                        if (currentSnapshot.jx != null || currentSnapshot.jy != null)
                        {
                            retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                            continue;
                        }

                        // moving diagonally, must make sure one of the vertical/horizontal
                        // neighbors is open to allow the path
                        if (iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX + currentSnapshot.tDx, currentSnapshot.iY)
                            || iParam.SearchGrid.IsWalkableAt(
                                currentSnapshot.iX,
                                currentSnapshot.iY + currentSnapshot.tDy))
                        {
                            newSnapshot = new JumpSnapshot();
                            newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                            newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                            newSnapshot.iPx = currentSnapshot.iX;
                            newSnapshot.iPy = currentSnapshot.iY;
                            newSnapshot.stage = 0;
                            stack.Push(newSnapshot);
                            continue;
                        }
                        else if (iParam.CrossAdjacentPoint)
                        {
                            newSnapshot = new JumpSnapshot();
                            newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                            newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                            newSnapshot.iPx = currentSnapshot.iX;
                            newSnapshot.iPy = currentSnapshot.iY;
                            newSnapshot.stage = 0;
                            stack.Push(newSnapshot);
                            continue;
                        }
                        retVal = null;
                        break;
                    case 3:
                        currentSnapshot.jx = retVal;

                        currentSnapshot.stage = 4;
                        stack.Push(currentSnapshot);

                        newSnapshot = new JumpSnapshot();
                        newSnapshot.iX = currentSnapshot.iX;
                        newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                        newSnapshot.iPx = currentSnapshot.iX;
                        newSnapshot.iPy = currentSnapshot.iY;
                        newSnapshot.stage = 0;
                        stack.Push(newSnapshot);
                        break;
                    case 4:
                        currentSnapshot.jy = retVal;
                        if (currentSnapshot.jx != null || currentSnapshot.jy != null)
                        {
                            retVal = new GridPos(currentSnapshot.iX, currentSnapshot.iY);
                            continue;
                        }

                        // moving diagonally, must make sure both of the vertical/horizontal
                        // neighbors is open to allow the path
                        if (iParam.SearchGrid.IsWalkableAt(currentSnapshot.iX + currentSnapshot.tDx, currentSnapshot.iY)
                            && iParam.SearchGrid.IsWalkableAt(
                                currentSnapshot.iX,
                                currentSnapshot.iY + currentSnapshot.tDy))
                        {
                            newSnapshot = new JumpSnapshot();
                            newSnapshot.iX = currentSnapshot.iX + currentSnapshot.tDx;
                            newSnapshot.iY = currentSnapshot.iY + currentSnapshot.tDy;
                            newSnapshot.iPx = currentSnapshot.iX;
                            newSnapshot.iPy = currentSnapshot.iY;
                            newSnapshot.stage = 0;
                            stack.Push(newSnapshot);
                            continue;
                        }
                        retVal = null;
                        break;
                }
            }

            return retVal;
        }
        public void Reset(GridPos iStartPos, GridPos iEndPos, IBaseGrid iSearchGrid = null)
        {
            this.openList.Clear();
            this.m_startNode = null;
            this.m_endNode = null;

            if (iSearchGrid != null)
            {
                this.m_searchGrid = iSearchGrid;
            }
            this.m_searchGrid.Reset();
            this.m_startNode = this.m_searchGrid.GetNodeAt(iStartPos.x, iStartPos.y);
            this.m_endNode = this.m_searchGrid.GetNodeAt(iEndPos.x, iEndPos.y);
            if (this.m_startNode == null)
            {
                this.m_startNode = new Node(iStartPos.x, iStartPos.y, true);
            }
            if (this.m_endNode == null)
            {
                this.m_endNode = new Node(iEndPos.x, iEndPos.y, true);
            }
        }
Ejemplo n.º 10
0
 public bool Equals(GridPos p)
 {
     // Return true if the fields match:
     return (this.x == p.x) && (this.y == p.y);
 }
Ejemplo n.º 11
0
        public Node SetNode(int iX, int iY, bool?iWalkable = null)
        {
            var pos = new GridPos(iX, iY);

            return(SetNode(pos, iWalkable));
        }
Ejemplo n.º 12
0
        public Node GetNode(int iX, int iY)
        {
            var pos = new GridPos(iX, iY);

            return(this.GetNode(pos));
        }
Ejemplo n.º 13
0
        protected void removeNode(int iX, int iY)
        {
            var pos = new GridPos(iX, iY);

            this.removeNode(pos);
        }
Ejemplo n.º 14
0
 public bool Equals(GridPos p)
 {
     // Return true if the fields match:
     return((this.x == p.x) && (this.y == p.y));
 }