示例#1
0
文件: AI.cs 项目: Jecral/Football
        /// <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;
        }
示例#2
0
文件: AI.cs 项目: Jecral/Football
        /* Returns a player if he will stand in the way of the ball
         * Returns null if no player stands in the way. */
        public Player PlayerInBallWay(Point shotPoint, Point targetPoint)
        {
            Pathfinding pathfinding = new Pathfinding();
            Point tmpBallExactLocation = pathfinding.GetExactLocation(shotPoint);
            Point tmpBallGridLocation = pathfinding.GetGridLocation(tmpBallExactLocation);

            for(int i = 0; i < 3; i++)
            {
                if (!tmpBallGridLocation.Equals(shotPoint))
                {
                    Player playerOnPosition = pathfinding.StandsPlayerOnPosition(tmpBallGridLocation, 1);
                    if (playerOnPosition != null)
                    {
                        return playerOnPosition;
                    }
                }
                double[] direction = pathfinding.GetExactDirection(tmpBallExactLocation, pathfinding.GetExactLocation(targetPoint));
                tmpBallExactLocation.X += (int)(direction[0] * 20);
                tmpBallExactLocation.Y += (int)(direction[1] * 20);
                tmpBallGridLocation = pathfinding.GetGridLocation(tmpBallExactLocation);
            }

            return null;
        }