Пример #1
0
        public Movement GetSafeMovementDirection(HashSet <Direction> dangerDirections)
        {
            _moveType = MoveType.SafeMovement;
            var player = _client.GetPlayerTank();

            var possibleDirections = MapUtility.IterationMap
                                     .ToDictionary(x => x.Key, x => x.Value(player))
                                     .Where(x => !_client.IsObstacleAt(x.Value) && !_client.IsOutOf(x.Value))
                                     .Select(x => x.Key)
                                     .ToArray();

            var safeDirections = possibleDirections.Where(x => !dangerDirections.Contains(x)).ToArray();

            switch (safeDirections.Length)
            {
            case 1:
                return(safeDirections[0].ToMovement());

            case 2:
                return(safeDirections[_randomValueGenerator.Next(1)].ToMovement());

            case 3:
                return(dangerDirections.First().IsVerticalDirection()
                            ? DirectionUility.HorisontalDirections[
                           _randomValueGenerator.Next(DirectionUility.HorisontalDirections.Length - 1)]
                       .ToMovement()
                            : DirectionUility.VerticalDirections[
                           _randomValueGenerator.Next(DirectionUility.VerticalDirections.Length - 1)]
                       .ToMovement()
                       );

            default:
                return(possibleDirections[_randomValueGenerator.Next(possibleDirections.Length - 1)].ToMovement());
            }
        }
        private static Point GetClosestEnemyPosition(BattleNetClient client, List <List <Point> > checkedPoints,
                                                     List <List <Movement> > movements, List <Point> currentRangePoints, out List <Movement> road)
        {
            if (client.IsAnyOfEnemyAt(currentRangePoints))
            {
                for (int index = 0; index < currentRangePoints.Count; index++)
                {
                    if (client.IsEnemyAt(currentRangePoints[index]))
                    {
                        road = new List <Movement>(movements[index]);
                        return(currentRangePoints[index]);
                    }
                }
            }

            var iterationMap      = MapUtility.IterationMap;
            var nextRangePoints   = new List <Point>();
            var nextMovements     = new List <List <Movement> >();
            var nextCheckedPoints = new List <List <Point> >();

            for (int index = 0; index < movements.Count; index++)
            {
                foreach (var iteration in iterationMap)
                {
                    var nextPosition = iteration.Value(currentRangePoints[index]);
                    var movement     = iteration.Key.ToMovement();

                    if (checkedPoints[index].Exists(x => x.X == nextPosition.X && x.Y == nextPosition.Y))
                    {
                        continue;
                    }

                    if (client.IsOutOf(nextPosition) || client.IsObstacleAt(nextPosition))
                    {
                        continue;
                    }

                    nextRangePoints.Add(nextPosition);

                    var move = new List <Movement>(movements[index]);
                    move.Add(iteration.Key.ToMovement());
                    nextMovements.Add(move);

                    var nextChecked = new List <Point>(checkedPoints[index]);
                    nextChecked.Add(currentRangePoints[index]);
                    nextCheckedPoints.Add(nextChecked);
                }
            }

            checkedPoints.Clear();
            checkedPoints.AddRange(nextCheckedPoints);
            movements.Clear();
            movements.AddRange(nextMovements);
            currentRangePoints.Clear();
            currentRangePoints.AddRange(nextRangePoints);
            road = new List <Movement>();
            return(PointUtility.CreateNegativePoint());
        }
        private static List <Movement> GetRoad(BattleNetClient client, Point targetPoint, List <List <Point> > checkedPoints,
                                               List <List <Movement> > movements, List <Point> currentRangePoints)
        {
            if (currentRangePoints.Exists(x => x.X == targetPoint.X && x.Y == targetPoint.Y))
            {
                for (int index = 0; index < currentRangePoints.Count; index++)
                {
                    if (currentRangePoints[index].X == targetPoint.X && currentRangePoints[index].Y == targetPoint.Y)
                    {
                        return(movements[index]);
                    }
                }
            }

            var iterationMap      = MapUtility.IterationMap;
            var nextRangePoints   = new List <Point>();
            var nextMovements     = new List <List <Movement> >();
            var nextCheckedPoints = new List <List <Point> >();

            for (int index = 0; index < movements.Count; index++)
            {
                foreach (var iteration in iterationMap)
                {
                    var nextPosition = iteration.Value(currentRangePoints[index]);
                    var movement     = iteration.Key.ToMovement();

                    if (checkedPoints[index].Exists(x => x.X == nextPosition.X && x.Y == nextPosition.Y))
                    {
                        continue;
                    }

                    if (client.IsOutOf(nextPosition) || client.IsObstacleAt(nextPosition))
                    {
                        continue;
                    }

                    nextRangePoints.Add(nextPosition);

                    var move = new List <Movement>(movements[index]);
                    move.Add(iteration.Key.ToMovement());
                    nextMovements.Add(move);

                    var nextChecked = new List <Point>(checkedPoints[index]);
                    nextChecked.Add(currentRangePoints[index]);
                    nextCheckedPoints.Add(nextChecked);
                }
            }

            checkedPoints.Clear();
            checkedPoints.AddRange(nextCheckedPoints);
            movements.Clear();
            movements.AddRange(nextMovements);
            currentRangePoints.Clear();
            currentRangePoints.AddRange(nextRangePoints);

            return(new List <Movement>());
        }
        private bool IsConstructionsOnLine(Point zeroPosition, Direction requestedDirection, Region area)
        {
            var testPosition = new Point(zeroPosition.X, zeroPosition.Y);
            var iterator     = MapUtility.IterationMap[requestedDirection];

            while (!_client.IsOutOf(testPosition) && testPosition.IsOutOf(area))
            {
                testPosition = iterator(testPosition);

                if (_client.IsConstructionAt(testPosition))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #5
0
        private bool IsEnemiesOnLine(Point zeroPosition, Direction requestedDirection, List <MapPoint> enemiesInArea)
        {
            var testPosition = new Point(zeroPosition.X, zeroPosition.Y);
            var iterator     = MapUtility.IterationMap[requestedDirection];

            while (!_client.IsOutOf(testPosition.X, testPosition.Y))
            {
                testPosition = iterator(testPosition);

                if (enemiesInArea.Exists(enemy => enemy.X == testPosition.X && enemy.Y == testPosition.Y))
                {
                    return(true);
                }
            }

            return(false);
        }
        private static bool IsInClosureArea(BattleNetClient client, Point position, Region area, List <Point> checkedPoints)
        {
            if (checkedPoints.Exists(x => x.X == position.X && x.Y == position.Y))
            {
                return(true);//skip checking. As far as we checking closure area by "&&" true equivalent to skipping
            }

            if (client.IsOutOf(position))
            {
                return(true);
            }

            var iterationMap = MapUtility.IterationMap;

            var topPosition    = iterationMap[Direction.Up](position);
            var bottomPosition = iterationMap[Direction.Down](position);
            var leftPosition   = iterationMap[Direction.Left](position);
            var rightPosition  = iterationMap[Direction.Right](position);

            checkedPoints.Add(position);

            if (client.IsObstacleAt(position))
            {
                return(true);
            }

            if (position.IsOutOf(area))
            {
                return(false);
            }

            return(IsInClosureArea(client, topPosition, area, checkedPoints) &&
                   IsInClosureArea(client, bottomPosition, area, checkedPoints) &&
                   IsInClosureArea(client, leftPosition, area, checkedPoints) &&
                   IsInClosureArea(client, rightPosition, area, checkedPoints));
        }
        private static Point GetClosestPosition(BattleNetClient client, Point position, Func <Point, bool> successCheck,
                                                List <Point> checkedPoints, List <Movement> movements)
        {
            if (checkedPoints.Exists(x => x.X == position.X && x.Y == position.Y))
            {
                return(PointUtility.CreateNegativePoint());//skip checking.
            }

            if (client.IsOutOf(position) || client.IsObstacleAt(position))
            {
                return(PointUtility.CreateNegativePoint());
            }

            var iterationMap = MapUtility.IterationMap;

            var topPosition    = iterationMap[Direction.Up](position);
            var bottomPosition = iterationMap[Direction.Down](position);
            var leftPosition   = iterationMap[Direction.Left](position);
            var rightPosition  = iterationMap[Direction.Right](position);

            checkedPoints.Add(position);

            if (successCheck(position))
            {
                return(position);
            }

            var topChildMovements    = new List <Movement>(movements);
            var bottomChildMovements = new List <Movement>(movements);
            var rightChildMovements  = new List <Movement>(movements);
            var leftChildMovements   = new List <Movement>(movements);

            var topEnemyPosition    = GetClosestPosition(client, topPosition, successCheck, new List <Point>(checkedPoints), topChildMovements);
            var bottomEnemyPosition = GetClosestPosition(client, bottomPosition, successCheck, new List <Point>(checkedPoints), bottomChildMovements);
            var leftEnemyPosition   = GetClosestPosition(client, leftPosition, successCheck, new List <Point>(checkedPoints), leftChildMovements);
            var rightEnemyPosition  = GetClosestPosition(client, rightPosition, successCheck, new List <Point>(checkedPoints), rightChildMovements);

            var topMovements    = topChildMovements.Count;
            var bottomMovements = bottomChildMovements.Count;
            var leftMovements   = leftChildMovements.Count;
            var rightMovements  = rightChildMovements.Count;

            if (topMovements < bottomMovements &&
                topMovements < leftMovements &&
                topMovements < rightMovements)
            {
                movements.Add(Movement.Up);
                movements.AddRange(topChildMovements);
                return(topEnemyPosition);
            }

            if (bottomMovements < topMovements &&
                bottomMovements < leftMovements &&
                bottomMovements < rightMovements)
            {
                movements.Add(Movement.Down);
                movements.AddRange(bottomChildMovements);
                return(bottomEnemyPosition);
            }

            if (leftMovements < topMovements &&
                leftMovements < bottomMovements &&
                leftMovements < rightMovements)
            {
                movements.Add(Movement.Left);
                movements.AddRange(leftChildMovements);
                return(leftEnemyPosition);
            }

            if (rightMovements < topMovements &&
                rightMovements < bottomMovements &&
                rightMovements < leftMovements)
            {
                movements.Add(Movement.Right);
                movements.AddRange(rightChildMovements);
                return(rightEnemyPosition);
            }

            return(PointUtility.CreateNegativePoint());
        }