Example #1
0
        /// <summary>
        /// Connects any orphaned sections of the map together.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static T ConnectOrphanedSections <T>(T map) where T : class, IMap, new()
        {
            var floodFillAnalyzer         = new FloodFillAnalyzer(map);
            var returnMap                 = map.Clone <T>();
            List <MapSection> mapSections = floodFillAnalyzer.GetMapSections();
            var unionFind                 = new UnionFind(mapSections.Count);

            while (unionFind.Count > 1)
            {
                for (int i = 0; i < mapSections.Count; i++)
                {
                    int                 closestMapSectionIndex = FindNearestMapSection(mapSections, i, unionFind);
                    MapSection          closestMapSection      = mapSections[closestMapSectionIndex];
                    IEnumerable <ICell> tunnelCells            = map.GetCellsAlongLine(mapSections[i].Bounds.Center.X, mapSections[i].Bounds.Center.Y,
                                                                                       closestMapSection.Bounds.Center.X, closestMapSection.Bounds.Center.Y);
                    ICell previousCell = null;
                    foreach (ICell cell in tunnelCells)
                    {
                        returnMap.SetCellProperties(cell.X, cell.Y, true, true);
                        if (previousCell != null)
                        {
                            if (cell.X != previousCell.X || cell.Y != previousCell.Y)
                            {
                                returnMap.SetCellProperties(cell.X + 1, cell.Y, true, true);
                            }
                        }
                        previousCell = cell;
                    }
                    unionFind.Union(i, closestMapSectionIndex);
                }
            }
            return(returnMap);
        }
Example #2
0
        private void ConnectCaves()
        {
            var floodFillAnalyzer         = new FloodFillAnalyzer(_map);
            List <MapSection> mapSections = floodFillAnalyzer.GetMapSections();
            var unionFind = new UnionFind(mapSections.Count);

            while (unionFind.Count > 1)
            {
                for (int i = 0; i < mapSections.Count; i++)
                {
                    int                 closestMapSectionIndex = FindNearestMapSection(mapSections, i, unionFind);
                    MapSection          closestMapSection      = mapSections[closestMapSectionIndex];
                    IEnumerable <ICell> tunnelCells            = _map.GetCellsAlongLine(mapSections[i].Bounds.Center.X, mapSections[i].Bounds.Center.Y,
                                                                                        closestMapSection.Bounds.Center.X, closestMapSection.Bounds.Center.Y);
                    ICell previousCell = null;
                    foreach (ICell cell in tunnelCells)
                    {
                        _map.SetCellProperties(cell.X, cell.Y, true, true);
                        if (previousCell != null)
                        {
                            if (cell.X != previousCell.X || cell.Y != previousCell.Y)
                            {
                                _map.SetCellProperties(cell.X + 1, cell.Y, true, true);
                            }
                        }
                        previousCell = cell;
                    }
                    unionFind.Union(i, closestMapSectionIndex);
                }
            }
        }