Britbot
bool Britbot.Group.IsFormed ( bool  checkCasualties = true) [inline, private]

Checks if the group is formed.

Returns:

Definition at line 374 of file Group.cs.

        {
            Logger.BeginTime("IsFormed at group " + this.Id);

            if (checkCasualties)
                if (this.CasualtiesPercent() > Magic.casualtiesThresholdPercent) //if there are many casualties
                {
                    Logger.StopTime("IsFormed at group " + this.Id);
                    return false;
                }

            //the offsets from the original location.
            //this test assumes that the group has moves but *relatively* everyone is in place.
            int deltaCol = 0, deltaRow = 0;

            //a flag to indicate of the current 
            bool deltaFlag = true;

            //bool to flag if there is need for another formation test (see below)
            bool confirmUnstructured = false;

            //iterate over the forming instructions 
            foreach (KeyValuePair<int, Location> formOrder in this.FormOrders)
            {
                //get the actual pirate from its ID
                Pirate pete = Bot.Game.GetMyPirate(formOrder.Key);

                if (pete != null)
                {
                    //ignore dead pirates
                    if (pete.IsLost)
                        continue;

                    //we calculate the deltas for the first pirate only
                    if (deltaFlag)
                    {
                        deltaCol = formOrder.Value.Col - pete.Loc.Col;
                        deltaRow = formOrder.Value.Row - pete.Loc.Row;
                        deltaFlag = false;

                        //skip to the next pirate
                        continue;
                    }

                    //check if the pirate is in its right spot relatively to the group
                    if (pete.Loc.Col + deltaCol != formOrder.Value.Col || pete.Loc.Row + deltaRow != formOrder.Value.Row)
                    {
                        //if the pirate is not in place, proceed to the next test. 
                        //because there's slight chance one of the pirates got stuck for a little bit but it is still in OK location
                        confirmUnstructured = true;
                        break;
                    }
                }
            }

            //if the group passed the previous test, return true (meaning that the group is formed)
            if (!confirmUnstructured)
            {
                Logger.Write(string.Format("Group {0} is formed", this.Id));
                Logger.StopTime("IsFormed at group " + this.Id);
                return true;
            }

            //else, proceed to the next test

            //find the central pirate in the group
            Location pivot = this.FindCenter(true);

            //the group's new structure (formation)
            Location[][] structureFull = null;

            //try to get a new formation
            try
            {
                structureFull = this.GenerateGroupStructure(pivot, false);
            }
            catch //if there's an exception (such as InvalidLocationException) return false
            {
                Logger.Write(String.Format("Group {0} is not formed yet", this.Id));
                Logger.StopTime("IsFormed at group " + this.Id);
                return false;
            }

            //if we still failed to get a new structure for whatever reason...
            if (structureFull == null)
            {
                //...return false
                Logger.Write(String.Format("Group {0} is not formed yet", this.Id));
                Logger.StopTime("IsFormed at group " + this.Id);
                return false;
            }

            //if we got a new structure successfully...
            //this is the number of empty location in the group's structure
            int emptyCells = 0;

            //iterate over all the locations in the new structure
            for (int i = 0; i < structureFull.Length; i++)
            {
                for (int k = 0; k < structureFull[i].Length; k++)
                {
                    //try to find a pirate in the location
                    Pirate p = Bot.Game.GetPirateOn(structureFull[i][k]);

                    //if there's not pirate of ours on the location
                    if (!(p != null && p.Owner == Consts.ME))
                    {
                        //check if this is the last ring, where some empty spots are OK
                        if (i == structureFull.Length - 1)
                            //advance the counter
                            emptyCells++;
                        else
                        {
                            //quit the iterations because the group is not formed for sure
                            //I know goto is bad but this is a legitimate case for this (rare one!)
                            goto ReturnFalse;
                        }
                    }
                }
            }

            //check if the empty cells is what it should be
            //(the structure array might be larger than the # of pirates in the group,
            //like if we have 4 pirates we need the 2nd ring (index 1) but but there will be on free spot)
            if (emptyCells == structureFull.Length - this.Pirates.Count)
            {
                Logger.Write(String.Format("Group {0} is formed", this.Id));
                Logger.StopTime("IsFormed at group " + this.Id);
                return true;
            }

            ReturnFalse:
            //if we are still not formed, return the right answer
            Logger.Write(String.Format("Group {0} is not formed yet", this.Id));

            Logger.StopTime("IsFormed at group " + this.Id);
            return false;
        }