Britbot
IEnumerable<KeyValuePair<Pirate, Direction> > Britbot.Group.GetStructureMoves ( CancellationToken  cancellationToken) [inline, private]

Get the correct moves to get into structure.

Parameters:
cancellationToken
Returns:

Definition at line 298 of file Group.cs.

        {
            Logger.BeginTime("GetStructureMoves");
            //check if we are not stuck try to get into formation for too long
            if (this._formTurnsAttempt > this.Pirates.Count * 1)
                //if we are stuck, request new instructions. This will reset the _formTurnsAttempt counter
                this.GenerateFormationInstructions();

            //advance the counter
            this._formTurnsAttempt++;

            //iterate over all the structure (each pirate in the group has a reserved location in the structure)
            foreach (KeyValuePair<int, Location> formOrder in this.FormOrders)
            {
                //Throwing an exception if cancellation was requested.
                cancellationToken.ThrowIfCancellationRequested();

                //Get the actual pirate object by its ID
                Pirate pete = Bot.Game.GetMyPirate(formOrder.Key);

                //if the pirate is already in place, make it stay in place
                if (pete.Loc.Col == formOrder.Value.Col && pete.Loc.Row == formOrder.Value.Row)
                {
                    yield return new KeyValuePair<Pirate, Direction>(pete, Direction.NOTHING);
                }
                else //if the pirate need to move into position
                {
                    //All the possible direction from the pirate to its position the the structure
                    List<Direction> possibleDirections = Bot.Game.GetDirections(pete, formOrder.Value);

                    List<Direction> filteredDirections = new List<Direction>(possibleDirections.Count);

                    //iterate over the possible directions
                    foreach (Direction t in possibleDirections)
                    {
                        //check if the direction is NOTHING - it means that the pirate is in place (double check with the previous if)
                        if (t == Direction.NOTHING)
                        {
                            filteredDirections.Add(t);
                            break;
                        }

                        //if the direction is actually passable add it to the list
                        if (Bot.Game.Destination(pete.Loc, t).IsActuallyPassable())
                            filteredDirections.Add(t);
                    }

                    //and return it
                    if (filteredDirections.Count == 0)
                        yield return new KeyValuePair<Pirate, Direction>(pete, Direction.NOTHING);
                    else if (Bot.Game.Distance(pete.Loc, formOrder.Value) <= 15)
                        yield return new KeyValuePair<Pirate, Direction>(pete, filteredDirections.First());
                    else if (filteredDirections.Count >= Magic.tryAlternate + 1)
                        yield return new KeyValuePair<Pirate, Direction>(pete, filteredDirections[Magic.tryAlternate]);
                    else
                        yield return new KeyValuePair<Pirate, Direction>(pete, filteredDirections.First());
                }
            }
            Logger.StopTime("GetStructureMoves");
        }