Пример #1
0
        /// <summary>
        /// Creates a country with a name and list of points.
        /// </summary>
        /// <returns>The country index.</returns>
        int CreateCountry(string name, Vector2[] points)
        {
            // 1) Initialize a new country
            Country country = new Country(name, "Continent", 1);

            // 2) Define the land region for this country with a list of points with coordinates from -0.5, -0.5 (bottom/left edge of map) to 0.5, 0.5 (top/right edge of map)
            // Note: the list of points should be expressed in clock-wise order
            Region region = new Region(country, 0);

            region.UpdatePointsAndRect(points);

            // 3) Add the region to the country (a country can have multiple regions, like islands)
            country.regions.Add(region);

            // 4) Add the new country to the map
            int countryIndex = map.CountryAdd(country);

            return(countryIndex);
        }
Пример #2
0
        /// <summary>
        /// Creates a province with a name and list of points.
        /// </summary>
        /// <returns>The country index.</returns>
        int CreateProvince(string name, int countryIndex, Vector2[] points)
        {
            // 1) Initialize a new province
            Province province = new Province(name, countryIndex, 0);

            // 2) Define the land region for this country with a list of points with coordinates from -0.5, -0.5 (bottom/left edge of map) to 0.5, 0.5 (top/right edge of map)
            // Note: the list of points should be expressed in clock-wise order
            Region region = new Region(province, 0);

            region.UpdatePointsAndRect(points);

            // 3) Add the region to the province (a province can also have multiple regions, like islands)
            province.regions = new List <Region>();
            province.regions.Add(region);

            // 4) Add the new country to the map
            int provinceIndex = map.ProvinceAdd(province);

            return(provinceIndex);
        }
Пример #3
0
        void RegionMergeTest()
        {
            ClearAll();


            Country country = new Country("", "", 1);
            Region  r1      = new Region(country, 0);

            showGrid = true;
            //								Vector2[] pp1 = new Vector2[] { new Vector2(2,1), new Vector2(3,1), new Vector2(4,2), new Vector2(1,2) };
            //								r1.UpdatePointsAndRect(pp1);
            Cell cell1 = GetCell(10, 10);

            r1.UpdatePointsAndRect(cell1.points);
            Region r2 = new Region(country, 1);
            //								Vector2[] pp2 = new Vector2[] { new Vector2(2,1), new Vector2(1,0), new Vector2(4,0), new Vector2(3,1) };
            //								r2.UpdatePointsAndRect(pp2);
            Cell cell2 = GetCell(10, 11);

            r2.UpdatePointsAndRect(cell2.points);

//												PolygonClipper pc = new PolygonClipper (r1, r2);
//												pc.Compute (PolygonOp.UNION, null);

            Clipper clipper = new Clipper();

            clipper.AddPath(r1, PolyType.ptSubject);
            clipper.AddPath(r2, PolyType.ptClip);
            clipper.Execute(ClipType.ctUnion);

            country.regions.Add(r1);
            int countryIndex = CountryAdd(country);

            RefreshCountryDefinition(countryIndex, null);
            Redraw();
            GenerateCountryRegionSurface(countryIndex, 0, GetColoredTexturedMaterial(Color.red, null));
            Debug.Log("ok");
        }
Пример #4
0
        /// <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
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Adjusts all countries frontiers to match the hexagonal grid
        /// </summary>
        public IEnumerator HexifyCountries(HexifyOpContext context)
        {
            Cell[] cells = _map.cells;
            if (cells == null)
            {
                yield break;
            }


            // Initialization
            cancelled     = false;
            hexifyContext = context;

            if (procCells == null || procCells.Length < cells.Length)
            {
                procCells = new RegionCell[cells.Length];
            }
            for (int k = 0; k < cells.Length; k++)
            {
                procCells [k].entityIndex = -1;
            }

            // Compute area of a single cell; for optimization purposes we'll ignore all regions whose surface is smaller than a 20% the size of a cell
            float  minArea            = 0;
            Region templateCellRegion = null;

            for (int k = 0; k < cells.Length; k++)
            {
                if (cells [k] != null)
                {
                    templateCellRegion = new Region(null, 0);
                    templateCellRegion.UpdatePointsAndRect(cells [k].points, true);
                    minArea = templateCellRegion.rect2DArea * 0.2f;
                    break;
                }
            }

            if (templateCellRegion == null)
            {
                yield break;
            }

            if (hexagonPoints == null || hexagonPoints.Length != 6)
            {
                hexagonPoints = new Vector2[6];
            }
            for (int k = 0; k < 6; k++)
            {
                hexagonPoints [k] = templateCellRegion.points [k] - templateCellRegion.center;
            }

            // Pass 1: remove minor regions
            yield return(RemoveSmallRegions(minArea, _map.countries));

            // Pass 2: assign all region centers to each country from biggest country to smallest country
            if (!cancelled)
            {
                yield return(AssignRegionCenters(_map.countries));
            }

            // Pass 3: add cells to target countries
            if (!cancelled)
            {
                yield return(AddHexagons(_map.countries));
            }

            // Pass 4: merge adjacent regions
            if (!cancelled)
            {
                yield return
                    (MergeAdjacentRegions(_map.countries));
            }

            // Pass 5: remove cells from other countries
            if (!cancelled)
            {
                yield return(RemoveHexagons(_map.countries));
            }

            // Pass 6: update geometry of resulting countries
            if (!cancelled)
            {
                yield return(UpdateCountries());
            }

            if (!cancelled)
            {
                _map.OptimizeFrontiers();
                _map.Redraw(true);
            }

            hexifyContext.progress(1f, hexifyContext.title, "");               // hide progress bar
            yield return(null);

            if (hexifyContext.finish != null)
            {
                hexifyContext.finish(cancelled);
            }
        }
Пример #6
0
        /// <summary>
        /// Creates a new province with the given region
        /// </summary>
        public void ProvinceCreate(Region region)
        {
            if (region == null)
            {
                return;
            }

            // Remove region from source entity
            IAdminEntity entity = region.entity;

            if (entity != null)
            {
                entity.regions.Remove(region);
                Country country;
                // Refresh entity definition
                if (region.entity is Country)
                {
                    int countryIndex = _map.GetCountryIndex((Country)region.entity);
                    country = _map.countries [countryIndex];
                    _map.RefreshCountryGeometry(country);
                }
                else
                {
                    int provinceIndex = map.GetProvinceIndex((Province)region.entity);
                    country = _map.countries [_map.provinces [provinceIndex].countryIndex];
                    _map.RefreshProvinceGeometry(provinceIndex);
                }
            }

            provinceIndex       = map.provinces.Length;
            provinceRegionIndex = 0;
            string   newProvinceName = GetProvinceUniqueName("New Province");
            Province newProvince     = new Province(newProvinceName, countryIndex, map.GetUniqueId(new List <IExtendableAttribute> (map.provinces)));
            Region   newRegion       = new Region(newProvince, 0);

            newRegion.UpdatePointsAndRect(region.points);
            newProvince.regions = new List <Region> ();
            newProvince.regions.Add(newRegion);
            map.ProvinceAdd(newProvince);
            map.RefreshProvinceDefinition(provinceIndex, false);

            // Update cities
            List <City> cities      = _map.GetCities(region);
            int         citiesCount = cities.Count;

            if (citiesCount > 0)
            {
                for (int k = 0; k < citiesCount; k++)
                {
                    if (cities [k].province != newProvinceName)
                    {
                        cities [k].province = newProvinceName;
                        cityChanges         = true;
                    }
                }
            }

            lastProvinceCount = -1;
            GUIProvinceName   = newProvince.name;
            SyncGUIProvinceSelection();
            ProvinceRegionSelect();
            provinceChanges = true;
        }
        /// <summary>
        /// Creates a new country based on a given region. Existing region is removed from its source entity.
        /// </summary>
        /// <param name="region">Region.</param>
        public void CountryCreate(Region region)
        {
            // Remove region from source entity
            IAdminEntity entity = region.entity;

            entity.regions.Remove(region);
            Country country;

            // Refresh entity definition
            if (region.entity is Country)
            {
                int countryIndex = _map.GetCountryIndex((Country)region.entity);
                country = _map.countries[countryIndex];
                _map.RefreshCountryGeometry(country);
            }
            else
            {
                int provinceIndex = map.GetProvinceIndex((Province)region.entity);
                country = _map.countries[_map.provinces[provinceIndex].countryIndex];
                _map.RefreshProvinceGeometry(provinceIndex);
            }

            // Create the new country
            string  uniqueName = GetCountryUniqueName(country.name);
            Country newCountry = new Country(uniqueName, country.continent, map.GetUniqueId(new List <IExtendableAttribute>(map.countries)));

            if (entity is Country)
            {
                newCountry.regions.Add(region);
            }
            else
            {
                Region newRegion = new Region(newCountry, 0);
                newRegion.UpdatePointsAndRect(region.points);
                newCountry.regions.Add(newRegion);
            }
            countryIndex       = map.CountryAdd(newCountry);
            countryRegionIndex = 0;
            lastCountryCount   = -1;
            GUICountryName     = "";
            ReloadCountryNames();
            countryChanges = true;

            // Update cities
            List <City> cities = _map.GetCities(region);

            if (cities.Count > 0)
            {
                for (int k = 0; k < cities.Count; k++)
                {
                    if (cities [k].countryIndex != countryIndex)
                    {
                        cities [k].countryIndex = countryIndex;
                        cityChanges             = true;
                    }
                }
            }

            // Update mount points
            List <MountPoint> mp = _map.GetMountPoints(region);

            if (mp.Count > 0)
            {
                for (int k = 0; k < mp.Count; k++)
                {
                    if (mp [k].countryIndex != countryIndex)
                    {
                        mp [k].countryIndex = countryIndex;
                        mountPointChanges   = true;
                    }
                }
            }

            // Transfer any contained province
            if (entity is Country)
            {
                List <Province> provinces = _map.GetProvinces(region);
                for (int k = 0; k < provinces.Count; k++)
                {
                    Province prov = provinces[k];
                    if (prov.regions == null)
                    {
                        _map.ReadProvincePackedString(prov);
                    }
                    if (prov.regions == null)
                    {
                        continue;
                    }
                    if (_map.CountryTransferProvinceRegion(countryIndex, prov.mainRegion, false))
                    {
                        provinceChanges = true;
                    }
                }
            }

            map.Redraw();
            CountryRegionSelect();
        }