Exemple #1
0
        public static void RenumberOceans(this Map map, List <Tile> oceanTiles)
        {
            var allTiles = new HashSet <Tile>(oceanTiles);

            var oceans = new List <IslandDetails>();

            while (allTiles.Count > 0)
            {
                var aTile  = allTiles.FirstOrDefault();
                var island = new IslandDetails
                {
                    Tiles = { aTile }
                };
                oceans.Add(island);


                var edgeSet = new HashSet <Tile> {
                    aTile
                };
                allTiles.Remove(aTile);
                while (edgeSet.Count > 0)
                {
                    var current = edgeSet.FirstOrDefault();
                    edgeSet.Remove(current);
                    foreach (var tile in map.Neighbours(current).Where(t => allTiles.Contains(t)))
                    {
                        edgeSet.Add(tile);
                        allTiles.Remove(tile);
                        island.Tiles.Add(tile);
                    }
                }
            }
            map.RenumberOceans(oceans);
        }
Exemple #2
0
        public static void NormalizeIslands(this Map map)
        {
            var allTiles = new HashSet <Tile>();

            for (var y = 0; y < map.Tile.GetLength(1); y++)
            {
                for (var x = 0; x < map.Tile.GetLength(0); x++)
                {
                    allTiles.Add(map.Tile[x, y]);
                }
            }

            var oceans = new List <IslandDetails>();
            var lands  = new List <IslandDetails>();

            while (allTiles.Count > 0)
            {
                var aTile  = allTiles.FirstOrDefault();
                var island = new IslandDetails
                {
                    Tiles = { aTile }
                };
                Func <Tile, bool> comparator;
                if (aTile.Type == TerrainType.Ocean)
                {
                    oceans.Add(island);
                    comparator = t => t.Type == TerrainType.Ocean;
                }
                else
                {
                    lands.Add(island);
                    comparator = t => t.Type != TerrainType.Ocean;
                }

                var edgeSet = new HashSet <Tile> {
                    aTile
                };
                allTiles.Remove(aTile);
                while (edgeSet.Count > 0)
                {
                    var current = edgeSet.FirstOrDefault();
                    edgeSet.Remove(current);
                    foreach (var tile in map.Neighbours(current).Where(t => comparator(t) && allTiles.Contains(t)))
                    {
                        edgeSet.Add(tile);
                        allTiles.Remove(tile);
                        island.Tiles.Add(tile);
                    }
                }
            }

            map.Islands = lands;
            map.RenumberIslands();
            map.RenumberOceans(oceans);
        }