Britbot
Location Britbot.EnemyGroup.GetLocation ( bool  forcePirate) [inline, private]

Returns the average location for this group.

/

Parameters:
forcePirateif you ant the function to strictly return a location of a pirate or just the average location
Returns:
Returns the average location for this group or the pirate closest to the average

Definition at line 131 of file EnemyGroup.cs.

        {
            //Get a list of all location of the enemy pirates in this group
            List<Location> locs = new List<Location>();

            if (this.EnemyPirates == null)
                return new Location(0, 0);

            foreach (int e in this.EnemyPirates)
            {
                Pirate enemyPirate = Bot.Game.GetEnemyPirate(e);

                if (enemyPirate != null)
                    locs.Add(enemyPirate.Loc);
            }

            //sum all the locations!
            int totalCol = locs.Sum(loc => loc.Col);
            int totalRow = locs.Sum(loc => loc.Row);

            Location averageLocation = new Location(0, 0);

            //return the average location
            if (locs.Count != 0)
                averageLocation = new Location(totalRow / locs.Count, totalCol / locs.Count);

            if (forcePirate)
            {
                int minDistance = Bot.Game.GetCols() + Bot.Game.GetCols();
                Pirate pete = null;

                //iterate over all the pirate and find the one with the minimun distance to the average location
                foreach (Pirate pirate in this.EnemyPirates.ConvertAll(p => Bot.Game.GetEnemyPirate(p)))
                {
                    if (pirate.IsLost)
                        continue;

                    int currDistance = Bot.Game.Distance(averageLocation, pirate.Loc);
                    if (currDistance < minDistance)
                    {
                        minDistance = currDistance;
                        pete = pirate;
                    }
                }

                //set the returned location to the central pirate location
                if (pete != null)
                    averageLocation = pete.Loc;
            }
            return averageLocation;
        }