Ejemplo n.º 1
0
        //   [StartupInvoke(StartupInvokePriority.Modules)]
        public static void ApplyFix()
        {
            logger.Gray("Fixing Map Placement...");

            List <PlacementPattern> patterns = new List <PlacementPattern>();
            Dictionary <PlacementPattern, string> patternsNames = new Dictionary <PlacementPattern, string>();

            foreach (string file in Directory.EnumerateFiles(PlacementPatternDirectory, "*.xml", SearchOption.AllDirectories))
            {
                try
                {
                    PlacementPattern pattern = (PlacementPattern)File.ReadAllText(file).XMLDeserialize(typeof(PlacementPattern));

                    if (SortByComplexity)
                    {
                        PlacementComplexityCalculator calc = new PlacementComplexityCalculator(pattern.Blues.Concat(pattern.Reds).ToArray <System.Drawing.Point>());
                        pattern.Complexity = calc.Compute();
                    }
                    patterns.Add(pattern);
                    patternsNames.Add(pattern, System.IO.Path.GetFileNameWithoutExtension(file));
                }
                catch
                {
                    logger.Error("Unable to load pattern " + System.IO.Path.GetFileNameWithoutExtension(file));
                }
            }

            Random rand = new Random();

            MapRecord[] maps = MapRecord.Maps.ToArray();

            int succesCount = 0;

            foreach (var map in MapRecord.Maps)
            {
                if (map.BlueCells.Count == 0 || map.RedCells.Count == 0)
                {
                    int[] relativePatternsComplx = (from entry in patterns
                                                    where entry.Relativ
                                                    select entry.Complexity).ToArray <int>();
                    PlacementPattern[] relativPatterns = (from entry in patterns
                                                          where entry.Relativ
                                                          select entry).ShuffleWithProbabilities(relativePatternsComplx).ToArray <PlacementPattern>();
                    Lozenge searchZone = new Lozenge(0, SearchDeep);

                    short[] cells = searchZone.GetCells(300, map);

                    for (int j = 0; j < cells.Length; j++)
                    {
                        short center = cells[j];
                        var   pt     = MapPoint.CellIdToCoord((uint)center);
                        System.Drawing.Point centerPt = new System.Drawing.Point(pt.X, pt.Y);
                        var success = relativPatterns.FirstOrDefault((PlacementPattern entry) => entry.TestPattern(centerPt, map));
                        if (success != null)
                        {
                            map.BlueCells = (from entry in success.Blues
                                             select(short) MapPoint.CoordToCellId(entry.X + centerPt.X, entry.Y + centerPt.Y)).ToList <short>();
                            map.RedCells = (from entry in success.Reds
                                            select(short) MapPoint.CoordToCellId(entry.X + centerPt.X, entry.Y + centerPt.Y)).ToList <short>();

                            map.UpdateInstantElement();
                            succesCount++;
                            break;
                        }
                    }
                }
            }



            logger.Gray(string.Format("{0} on {1} maps fixed ({2:0.0}%)", succesCount, MapRecord.Maps.Count, (double)succesCount / (double)MapRecord.Maps.Count * 100.0));
        }
Ejemplo n.º 2
0
        public static void ApplyFix()
        {
            Console.WriteLine("Apply placements fix ...");

            Console.WriteLine("Override old placements ?(y/n)");
            bool @override = Console.ReadLine() == "y";

            var dir           = PatternsDir;
            var patterns      = new List <PlacementPattern>();
            var patternsNames = new Dictionary <PlacementPattern, string>();

            foreach (var file in Directory.EnumerateFiles(dir, "*.xml", SearchOption.AllDirectories))
            {
                try
                {
                    var pattern = XmlUtils.Deserialize <PlacementPattern>(file);

                    if (SortByComplexity)
                    {
                        var calc = new PlacementComplexityCalculator(pattern.Blues.Concat(pattern.Reds).ToArray());
                        pattern.Complexity = calc.Compute();
                    }

                    patterns.Add(pattern);
                    patternsNames.Add(pattern, Path.GetFileNameWithoutExtension(file));
                }
                catch (Exception)
                {
                    Console.WriteLine("ERROR:Cannot parse pattern {0}", file);
                }
            }

            if (patterns.Count == 0)
            {
                Console.WriteLine("ERROR:no pattern in {0} found", dir);
                return;
            }

            var successCounter = new Dictionary <PlacementPattern, int>();
            var console        = new ConsoleProgress();
            var maps           = Program.Database.Database.Fetch <MapRecord>(MapRelator.FetchQuery);
            int patches        = 0;

            for (int i = 0; i < maps.Count; i++)
            {
                var map = maps[i];
                if (@override || (map.BlueFightCells.Length == 0 ||
                                  map.RedFightCells.Length == 0))
                {
                    var fixPatternsComplx    = patterns.Where(entry => !entry.Relativ).Select(entry => entry.Complexity).ToArray();
                    var fixPatterns          = patterns.Where(entry => !entry.Relativ).ShuffleWithProbabilities(fixPatternsComplx).ToArray();
                    PlacementPattern success = fixPatterns.FirstOrDefault(entry => entry.TestPattern(map));

                    if (success != null)
                    {
                        map.BlueFightCells =
                            success.Blues.Select(entry => (short)MapPoint.CoordToCellId(entry.X, entry.Y)).ToArray();
                        map.RedFightCells =
                            success.Reds.Select(entry => (short)MapPoint.CoordToCellId(entry.X, entry.Y)).ToArray();
                    }
                    else
                    {
                        var relativePatternsComplx = patterns.Where(entry => entry.Relativ).Select(entry => entry.Complexity).ToArray();
                        var relativPatterns        = patterns.Where(entry => entry.Relativ).ShuffleWithProbabilities(relativePatternsComplx).ToArray();

                        // 300 is approx. the middle of the map
                        foreach (var center in GetCellsCircle(map.Cells[300], map, SearchDeep, 0))
                        {
                            var centerPt = MapPoint.CellIdToCoord((uint)center.Id);
                            success = relativPatterns.FirstOrDefault(entry => entry.TestPattern(centerPt, map));

                            if (success != null)
                            {
                                map.BlueFightCells =
                                    success.Blues.Select(
                                        entry =>
                                        (short)MapPoint.CoordToCellId(entry.X + centerPt.X, entry.Y + centerPt.Y)).
                                    ToArray();
                                map.RedFightCells =
                                    success.Reds.Select(
                                        entry =>
                                        (short)MapPoint.CoordToCellId(entry.X + centerPt.X, entry.Y + centerPt.Y)).
                                    ToArray();
                                break;
                            }
                        }
                    }

                    // save it
                    if (success != null)
                    {
                        var database = Program.Database.Database;
                        database.Update(map);
                        patches++;
                    }
                }


                console.Update(string.Format("{0:0.0}% ({1} patchs)", i / (double)maps.Count * 100, patches));
            }

            Console.WriteLine("{0}/{1} ({2:0.0}%) patterns used :", successCounter.Count, patterns.Count, successCounter.Count / (double)patterns.Count * 100);
            foreach (var counter in successCounter)
            {
                Console.WriteLine("{0} :\t{1,12:0.0}%", patternsNames[counter.Key], counter.Value / (double)patches * 100);
            }

            Console.WriteLine("{0} on {1} maps fixed ({2:0.0}%)", patches, maps.Count, patches / (double)maps.Count * 100);

            Console.WriteLine("Maps placements fix applied");
        }