Example #1
0
        /// <summary>
        /// Calculates the distances to the enemy of every point around the target and returns the points ordered in a list.
        /// </summary>
        /// <param name="player"></param>
        /// <param name="playerTeam"></param>
        /// <param name="targetPoint"></param>
        /// <param name="range">The range around the target point</param>
        /// <param name="amountOfPlayers">The amount of players which are allowed to stand around the calculated point </param>
        /// <param name="rangeRectangle"></param>
        /// <returns></returns>
        public List<Point> SearchRunPoints(Player player, Team playerTeam, Point targetPoint, int range, int amountOfPlayers, Rectangle rangeRectangle)
        {
            Pathfinding pathfinding = new Pathfinding();

            Point rootPoint = targetPoint;
            List<Point> neighbors = new List<Point>();
            List<double> distancesToTarget = new List<double>();

            double rootDistanceToTarget = Pathfinding.DistanceBetweenPoints(player.Location, targetPoint);

            Point? bestNeighbor = null;
            int bestSurroundingEnemies = -1;

            for (int v = -2; v <= 2; v++)
            {
                for (int h = -2; h <= 2; h++)
                {
                    Point neighbor = new Point(rootPoint.X + h, rootPoint.Y + v);
                    int surroundingEnemies = SurroundingPlayers(GetEnemyTeam(playerTeam), range, neighbor, 1).Count;
                    double tmpDistanceToTarget = Pathfinding.DistanceBetweenPoints(neighbor, targetPoint);
                    if (pathfinding.IsWithinField(neighbor) && !neighbor.Equals(player.Location) && (!Pathfinding.CurrentBlockedRoom.Room.Contains(neighbor) || Pathfinding.CurrentBlockedRoom.AllowedTeam.Equals(playerTeam)))
                    {
                        if (tmpDistanceToTarget < rootDistanceToTarget && (bestSurroundingEnemies == -1 || surroundingEnemies < bestSurroundingEnemies))
                        {
                            bestNeighbor = neighbor;
                            bestSurroundingEnemies = surroundingEnemies;
                        }
                        if (rangeRectangle.Contains(neighbor) && pathfinding.StandsPlayerOnPosition(neighbor, 1) == null && surroundingEnemies <= amountOfPlayers)
                        {

                            if (neighbors.Count == 0)
                            {
                                neighbors.Add(neighbor);
                                distancesToTarget.Add(tmpDistanceToTarget);
                            }
                            else
                            {
                                for (int i = 0; i <= distancesToTarget.Count; i++)
                                {
                                    if (i == distancesToTarget.Count || tmpDistanceToTarget < distancesToTarget[i])
                                    {
                                        neighbors.Insert(i, neighbor);
                                        distancesToTarget.Insert(i, tmpDistanceToTarget);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (neighbors.Count == 0)
            {
                if (bestNeighbor.HasValue)
                {
                    neighbors.Add(bestNeighbor.Value);
                }
                else
                {
                    neighbors.Add(targetPoint);
                }

            }
            return neighbors;
        }
Example #2
0
        /// <summary>
        /// Returns the best unmarked location within the rectangle
        /// </summary>
        /// <param name="player"></param>
        /// <param name="rectangle"></param>
        /// <returns></returns>
        public Point SearchFreePointInRectangle(Player player, Rectangle rectangle)
        {
            Pathfinding pathfinding = new Pathfinding();
            Team playerTeam = PlayersTeam(player);
            Team enemyTeam = GetEnemyTeam(playerTeam);
            Point centerOfRectangle= new Point(rectangle.X + (rectangle.Width/2), rectangle.Y + (rectangle.Height/2));

            Point bestPoint = centerOfRectangle;
            int amountOfPlayers = -1;
            for (int x = rectangle.X; x < rectangle.X + rectangle.Width; x++)
            {
                for (int y = rectangle.Y; y < rectangle.Y + rectangle.Height; y++)
                {
                    int tmpAmount = SurroundingPlayers(playerTeam,2, new Point(x, y), 5).Count + SurroundingPlayers(enemyTeam, 2, new Point(x, y), 5).Count;
                    if (pathfinding.IsWithinField(new Point(x, y)))
                    {
                        if (amountOfPlayers == -1 || tmpAmount < amountOfPlayers)
                        {
                            bestPoint = new Point(x, y);
                        }
                    }
                }
            }

            return bestPoint;
        }