Britbot
static void Britbot.Commander.MergeSimilar ( ) [inline, static, private]

Merges similar groups.

Definition at line 407 of file Commander.cs.

        {
            //setup merging lists
            List<List<Group>> allMerges = new List<List<Group>>();

            //iterate over all groups
            foreach (Group group in Commander.Groups)
            {
                //create a merging list and add the current group to it
                List<Group> toMerge = new List<Group> {group};

                //check if there are any mering list that should contain the current group
                List<List<Group>> sameTarget =
                    allMerges.Where(
                        gList =>
                            //Basically join all group sharing the same target and in reasonable distance from each other
                            gList.Any(g => g.Target.Equals(group.Target) && g.MinDistance(group) < Magic.MaxJoinDistance))
                        .ToList();

                //if the sameTarget list constains some merging lists
                if (sameTarget.Count > 0)
                {
                    //if there are, remove these merging lists
                    allMerges.RemoveAll(
                        gList =>
                            gList.Any(g => g.Target.Equals(group.Target) && g.MinDistance(group) < Magic.MaxJoinDistance));
                    
                    //merge these lists
                    foreach (List<Group> gList in sameTarget)
                        toMerge.AddRange(gList);
                }

                //add the new group to the list of merges
                allMerges.Add(toMerge);
            }

            //actually merge the group according to the merging lists
            foreach (List<Group> mergeList in allMerges)
            {
                Group first = mergeList.First();

                for (int i = 1; i < mergeList.Count; i++)
                    first.Join(mergeList[i], false);
            }
        }