void CreateMapCities()
        {
            if (mapCountries == null)
            {
                return;
            }

            int countryCount = mapCountries.Count;

            if (mapCities == null)
            {
                mapCities = new List <MapCity>();
            }
            else
            {
                mapCities.Clear();
            }
            int totalCityCount = 0;

            for (int k = 0; k < countryCount; k++)
            {
                int cityCount = UnityEngine.Random.Range(numCitiesPerCountryMin, numCitiesPerCountryMax + 1);

                MapCountry country = mapCountries[k];
                if (!country.visible)
                {
                    continue;
                }
                int provincesCount = country.provinces.Count;
                country.hasCapital = false;
                for (int j = 0; j < provincesCount; j++)
                {
                    country.provinces[j].hasCapital = false;
                }
                Vector2 pos;
                int     countryCityCount = 0;
                for (int q = 0; q < cityCount; q++)
                {
                    if (countryCityCount >= cityCount)
                    {
                        break;
                    }
                    for (int j = 0; j < provincesCount; j++)
                    {
                        if (countryCityCount >= cityCount)
                        {
                            break;
                        }

                        MapProvince prov = country.provinces[j];
                        if (!prov.visible)
                        {
                            continue;
                        }
                        Rect rect = prov.region.rect2D;
                        if (rect.width > 0.00001f && rect.height > 0.00001f)
                        {
                            do
                            {
                                pos.x = rect.xMin + UnityEngine.Random.value * rect.width;
                                pos.y = rect.yMin + UnityEngine.Random.value * rect.height;
                            } while (!prov.region.Contains(pos.x, pos.y));

                            CITY_CLASS cityClass;
                            if (!country.hasCapital)
                            {
                                country.hasCapital = true;
                                cityClass          = CITY_CLASS.COUNTRY_CAPITAL;
                            }
                            else if (!prov.hasCapital)
                            {
                                prov.hasCapital = true;
                                cityClass       = CITY_CLASS.REGION_CAPITAL;
                            }
                            else
                            {
                                cityClass = CITY_CLASS.CITY;
                            }
                            MapCity c = new MapCity("City" + totalCityCount++, j, k, 0, pos, cityClass);
                            mapCities.Add(c);
                            countryCityCount++;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Generate and replace provinces, city and country data
        /// </summary>
        public void GenerateMap()
        {
            try {
                UnityEngine.Random.InitState(seed);
                GenerateHeightMap();
                CreateMapProvinces();
                AssignHeightMapToProvinces(true);
                CreateMapCountries();
                CreateMapCities();

                UnityEngine.Random.InitState(seedNames);
                // Replace countries
                int            mapCountriesCount = mapCountries.Count;
                List <Country> newCountries      = new List <Country>(mapCountriesCount);
                for (int k = 0; k < mapCountriesCount; k++)
                {
                    MapCountry c      = mapCountries[k];
                    Vector2[]  points = c.region.points;
                    if (points == null || points.Length < 3)
                    {
                        continue;
                    }
                    string  name       = GetUniqueRandomName(0, 4, usedNames);
                    Country newCountry = new Country(name, "World", k);
                    Region  region     = new Region(newCountry, newCountry.regions.Count);
                    newCountry.regions.Add(region);
                    region.UpdatePointsAndRect(points);
                    map.RefreshCountryGeometry(newCountry);
                    newCountries.Add(newCountry);
                }
                map.countries  = newCountries.ToArray();
                countryChanges = true;

                // Replace provinces
                usedNames.Clear();
                int             mapProvincesCount = mapProvinces.Count;
                List <Province> newProvinces      = new List <Province>(mapProvincesCount);
                Province[]      provinces         = _map.provinces;
                if (provinces == null)
                {
                    provinces = new Province[0];
                }
                for (int k = 0; k < mapProvincesCount; k++)
                {
                    MapProvince c = mapProvinces[k];
                    if (!c.visible)
                    {
                        continue;
                    }
                    Vector2[] points = c.region.points;
                    if (points == null || points.Length < 3)
                    {
                        continue;
                    }
                    int countryIndex = map.GetCountryIndex(c.center);
                    if (countryIndex < 0)
                    {
                        continue;
                    }

                    string   name        = GetUniqueRandomName(0, 4, usedNames);
                    Province newProvince = new Province(name, countryIndex, k);
                    newProvince.regions = new List <Region>();
                    Region region = new Region(newProvince, newProvince.regions.Count);
                    newProvince.attrib["mapColor"] = c.color;
                    newProvince.regions.Add(region);
                    region.UpdatePointsAndRect(points);
                    map.RefreshProvinceGeometry(newProvince);
                    newProvinces.Add(newProvince);

                    Country         country          = map.countries[countryIndex];
                    List <Province> countryProvinces = country.provinces != null ? new List <Province>(country.provinces) : new List <Province>();
                    countryProvinces.Add(newProvince);
                    country.provinces = countryProvinces.ToArray();
                }
                map.provinces   = newProvinces.ToArray();
                provinceChanges = true;

                // Replace cities
                usedNames.Clear();
                int         mapCitiesCount = mapCities.Count;
                List <City> newCities      = new List <City>(mapCitiesCount);
                string      provinceName   = "";
                for (int k = 0; k < mapCitiesCount; k++)
                {
                    MapCity  c = mapCities[k];
                    City     newCity;
                    Province prov = map.GetProvince(c.unity2DLocation);
                    if (prov == null)
                    {
                        continue;
                    }
                    provinceName = prov != null ? prov.name : "";
                    string name = GetUniqueRandomName(0, 4, usedNames);
                    newCity = new City(name, provinceName, prov.countryIndex, c.population, c.unity2DLocation, c.cityClass, k);
                    newCities.Add(newCity);
                }
                map.cities  = newCities.ToArray();
                cityChanges = true;

                // Generate textures
                GenerateWorldTexture();

                // Apply some complementary style
                if (changeStyle && heightGradientPreset != HeightMapGradientPreset.Custom)
                {
                    map.frontiersColor      = Color.black;
                    map.frontiersColorOuter = Color.black;
                    map.provincesColor      = new Color(0.5f, 0.5f, 0.5f, 0.35f);
                    if (heightGradientPreset == HeightMapGradientPreset.BlackAndWhite || heightGradientPreset == HeightMapGradientPreset.Grayscale)
                    {
                        map.countryLabelsColor       = Color.black;
                        map.countryLabelsShadowColor = new Color(0.9f, 0.9f, 0.9f, 0.75f);
                    }
                    else
                    {
                        map.countryLabelsColor       = Color.white;
                        map.countryLabelsShadowColor = new Color(0.1f, 0.1f, 0.1f, 0.75f);
                    }
                }
                if (_map.earthStyle.isScenicPlus())
                {
                    _map.earthStyle = EARTH_STYLE.Natural;
                }


                // Save map data
                SaveGeneratedMapData();

                map.showFrontiers    = true;
                map.showCountryNames = true;
                map.waterLevel       = seaLevel;
                map.Redraw(true);
            } catch (Exception ex) {
                if (!Application.isPlaying)
                {
                    Debug.LogError("Error generating map: " + ex.ToString());
#if UNITY_EDITOR
                    EditorUtility.DisplayDialog("Error Generating Map", "An error occured while generating map. Try choosing another 'Seed' value, reducing 'Border Curvature' amount or number of provinces.", "Ok");
#endif
                }
            }
        }