Пример #1
0
        private List <ANode> GetNeighborNodes(ANode n)
        {
            List <ANode> temp = new List <ANode>();

            int col = (int)n.Position.X;
            int row = (int)n.Position.Y;

            if (row + 1 < GridRows)
            {
                //temp.Add(Grid[col][row + 1]);
                temp.Add(Grid[row + 1][col]);
            }
            if (row - 1 >= 0)
            {
                //temp.Add(Grid[col][row - 1]);
                temp.Add(Grid[row - 1][col]);
            }
            if (col - 1 >= 0)
            {
                //temp.Add(Grid[col - 1][row]);
                temp.Add(Grid[row][col - 1]);
            }
            if (col + 1 < GridCols)
            {
                //temp.Add(Grid[col + 1][row]);
                temp.Add(Grid[row][col + 1]);
            }

            return(temp);
        }
Пример #2
0
 public ANode(Vector2 pos, bool walkable)
 {
     Parent           = null;
     Position         = pos;
     DistanceToTarget = -1;
     Cost             = 1;
     isWall           = walkable;
 }
Пример #3
0
        private int AImove(Character c)
        {
            int dir      = -1;
            int distance = calcDistance(c.I, c.J, player.I, player.J);

            //kiểm tra có nằm trên đường đạn không
            if (checkInBombsArea(c.I, c.J))
            {
                //tìm điểm an toàn trong phạm vi 3 nước tính từ vị trí hiện tại
                switch (RoomSetting.Instance.MapSize)
                {
                case 21:
                    return(findSafePlace(c, 10));

                case 31:
                    return(findSafePlace(c, 11));

                case 51:
                    return(findSafePlace(c, 12));
                }
            }

            //khi khoảng cách 2 bên quá xa thì thu hẹp khoảng cách
            int d = rand.Next(1, 3);

            if (distance > d)
            {
                if (RoomSetting.Instance.MapSize <= 31)
                {
                    ANode pos = astar.FindPath(this, new Vector2(c.J, c.I), new Vector2(player.J, player.I));
                    if (pos != null)
                    {
                        dir = nextCell(pos, c);
                    }
                    else
                    {
                        int[] possibleMove = findPossibleMove(c);
                        dir = findNextMove(possibleMove, c, player.I, player.J);
                    }
                }
                else
                {
                    int[] possibleMove = findPossibleMove(c);
                    dir = findNextMove(possibleMove, c, player.I, player.J);
                }
            }
            //khi gần rồi thì tấn công
            else
            {
                if (player.I == c.I || player.J == c.J)
                {
                    BotSetBomb(c);
                }
            }

            return(dir);
        }
Пример #4
0
        private int findSafePlace(Character c, int length)
        {
            int i, j, dir, tempI, tempJ, distance, shortest;

            distance = 0;
            dir      = tempI = tempJ = -1;
            shortest = 99999;
            for (i = c.I - length; i < c.I + length; i++)
            {
                if (i < 0 || i >= mapGenerator.LogicMap.Length)
                {
                    continue;
                }
                for (j = c.J - length; j < c.J + length; j++)
                {
                    if (j < 0 || j >= mapGenerator.LogicMap[0].Length)
                    {
                        continue;
                    }
                    if (mapGenerator.IsValidLocation(i, j) && !checkInBombsArea(i, j))
                    {
                        distance = calcDistance(i, j, c.I, c.J);
                        if (distance < shortest)
                        {
                            shortest = distance;
                            tempI    = i;
                            tempJ    = j;
                        }
                    }
                }
            }

            if (tempI != -1 || tempJ != -1)
            {
                if (RoomSetting.Instance.MapSize <= 31)
                {
                    ANode pos = astar.FindPath(this, new Vector2(c.J, c.I), new Vector2(tempJ, tempI));
                    if (pos != null)
                    {
                        return(nextCell(pos, c));
                    }
                    return(findNextMove(findPossibleMove(c, true), c, tempI, tempJ));
                }
                return(findNextMove(findPossibleMove(c, true), c, tempI, tempJ));
            }
            return(-1);
        }
Пример #5
0
 public int nextCell(ANode pos, Character c)
 {
     if (pos.Position.X - c.J > 0)
     {
         return(3);
     }
     else if (pos.Position.X - c.J < 0)
     {
         return(2);
     }
     else if (pos.Position.Y - c.I > 0)
     {
         return(1);
     }
     else if (pos.Position.Y - c.I < 0)
     {
         return(0);
     }
     else
     {
         return(-1);
     }
 }
Пример #6
0
        public ANode FindPath(GameOperator go, Vector2 Start, Vector2 End)
        {
            Grid = UpdateGrid(go);
            ANode start = new ANode(new Vector2((int)(Start.X), (int)(Start.Y)), true);
            ANode end   = new ANode(new Vector2((int)(End.X), (int)(End.Y)), true);

            ANode res = null;

            List <ANode> Path       = new List <ANode>();
            List <ANode> OpenList   = new List <ANode>();
            List <ANode> ClosedList = new List <ANode>();
            List <ANode> neighbors;
            ANode        current = start;

            // add start node to Open List
            OpenList.Add(start);

            while (OpenList.Count != 0 && !ClosedList.Exists(x => x.Position == end.Position))
            {
                current = OpenList[0];
                OpenList.Remove(current);
                ClosedList.Add(current);
                neighbors = GetNeighborNodes(current);


                foreach (ANode aN in neighbors)
                {
                    if (!ClosedList.Contains(aN) && !aN.isWall)
                    {
                        if (!OpenList.Contains(aN))
                        {
                            if (aN.Position.X == end.Position.X && aN.Position.Y == end.Position.Y)
                            {
                                res = current;
                                break;
                            }
                            aN.Parent           = current;
                            aN.DistanceToTarget = Math.Abs(aN.Position.X - end.Position.X) + Math.Abs(aN.Position.Y - end.Position.Y);
                            aN.Cost             = 1 + aN.Parent.Cost;
                            OpenList.Add(aN);
                            OpenList = OpenList.OrderBy(node => node.F).ToList <ANode>();
                        }
                    }
                }
                if (res != null)
                {
                    break;
                }
            }

            // can't find destination
            if (res == null)
            {
                return(null);
            }

            while (res != null && res != start)
            {
                Path.Add(res);
                res = res.Parent;
            }

            if (Path.Count > 0)
            {
                return(Path[Path.Count - 1]);
            }
            return(null);
        }