bool AnyRegionContainsPoint(Vector2 p)
        {
            int entitiesCount = entities.Count;

            for (int k = 0; k < entitiesCount; k++)
            {
                IAdminEntity entity = entities [k];
                if (entity.regions == null)
                {
                    continue;
                }
                int entityRegionsCount = entity.regions.Count;
                for (int r = 0; r < entityRegionsCount; r++)
                {
                    Region region = entity.regions [r];
                    if (region.Contains(p))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        void PopulateRegion(int countryIndex, int provinceIndex, Region region)
        {
            int mountPoints = UnityEngine.Random.Range(GUIMountPointMassPopulationMin, GUIMountPointMassPopulationMax + 1);

            while (mountPoints > 0)
            {
                // Get a random position inside region
                Vector2 point;;
                for (int k = 0; k < 100; k++)
                {
                    float rx = UnityEngine.Random.value * region.rect2D.width;
                    float ry = UnityEngine.Random.value * region.rect2D.height;
                    point = new Vector2(region.rect2D.xMin + rx, region.rect2D.yMin + ry);
                    float r = Mathf.Min(rx, ry) * GUIMountPointMassPopulationSeparation * 0.01f;
                    if (region.Contains(point) && map.GetMountPointNearPoint(point, r) < 0)
                    {
                        map.MountPointAdd(point, map.mountPoints.Count.ToString(), countryIndex, provinceIndex, GUIMountPointMassPopulationTypeInteger);
                        break;
                    }
                }
                mountPoints--;
            }
        }
        /// <summary>
        /// Calculates correct province for cities
        /// </summary>
        public void FixOrphanCities()
        {
            if (_map.provinces == null)
            {
                return;
            }

            for (int c = 0; c < _map.cities.Length; c++)
            {
                City city = _map.cities[c];
                if (city.countryIndex == -1)
                {
                    for (int k = 0; k < _map.countries.Length; k++)
                    {
                        Country co = _map.countries[k];
                        for (int kr = 0; kr < co.regions.Count; kr++)
                        {
                            if (co.regions[kr].Contains(city.unity2DLocation))
                            {
                                city.countryIndex = k;
                                cityChanges       = true;
                                k = 100000;
                                break;
                            }
                        }
                    }
                }
                if (city.countryIndex == -1)
                {
                    float minDist = float.MaxValue;
                    for (int k = 0; k < _map.countries.Length; k++)
                    {
                        Country co = _map.countries[k];
                        for (int kr = 0; kr < co.regions.Count; kr++)
                        {
                            float dist = FastVector.SqrDistance(ref co.regions[kr].center, ref city.unity2DLocation);
                            if (dist < minDist)
                            {
                                minDist           = dist;
                                city.countryIndex = k;
                                cityChanges       = true;
                            }
                        }
                    }
                }

                if (city.province.Length == 0)
                {
                    Country country = _map.countries[city.countryIndex];
                    if (country.provinces == null)
                    {
                        continue;
                    }
                    for (int p = 0; p < country.provinces.Length; p++)
                    {
                        Province province = country.provinces[p];
                        if (province.regions == null)
                        {
                            _map.ReadProvincePackedString(province);
                        }
                        for (int pr = 0; pr < province.regions.Count; pr++)
                        {
                            Region reg = province.regions[pr];
                            if (reg.Contains(city.unity2DLocation))
                            {
                                city.province = province.name;
                                cityChanges   = true;
                                p             = 100000;
                                break;
                            }
                        }
                    }
                }
            }

            for (int c = 0; c < _map.cities.Length; c++)
            {
                City city = _map.cities[c];
                if (city.province.Length == 0)
                {
                    float minDist = float.MaxValue;
                    int   pg      = -1;
                    for (int p = 0; p < _map.provinces.Length; p++)
                    {
                        Province province = _map.provinces[p];
                        if (province.regions == null)
                        {
                            _map.ReadProvincePackedString(province);
                        }
                        for (int pr = 0; pr < province.regions.Count; pr++)
                        {
                            Region pregion = province.regions[pr];
                            for (int prp = 0; prp < pregion.points.Length; prp++)
                            {
                                float dist = FastVector.SqrDistance(ref city.unity2DLocation, ref pregion.points[prp]);
                                if (dist < minDist)
                                {
                                    minDist = dist;
                                    pg      = p;
                                }
                            }
                        }
                    }
                    if (pg >= 0)
                    {
                        city.province = _map.provinces[pg].name;
                        cityChanges   = true;
                    }
                }
            }
        }
        void ProvinceSubstractProvinceEnclaves(int provinceIndex, Region region, Poly2Tri.Polygon poly)
        {
            List <Region> negativeRegions = new List <Region> ();

            for (int oc = 0; oc < _countries.Length; oc++)
            {
                Country ocCountry = _countries [oc];
                if (ocCountry.hidden || ocCountry.provinces == null)
                {
                    continue;
                }
                Region mainCountryRegion = ocCountry.regions [ocCountry.mainRegionIndex];
                if (!mainCountryRegion.rect2D.Overlaps(region.rect2D))
                {
                    continue;
                }
                for (int op = 0; op < ocCountry.provinces.Length; op++)
                {
                    Province opProvince = ocCountry.provinces [op];
                    if (opProvince == provinces [provinceIndex])
                    {
                        continue;
                    }
                    if (opProvince.regions == null)
                    {
                        ReadProvincePackedString(opProvince);
                    }
                    if (opProvince.regions == null)
                    {
                        continue;
                    }
                    if (opProvince.regionsRect2D.Overlaps(region.rect2D, true))
                    {
                        Region oProvRegion = opProvince.regions [opProvince.mainRegionIndex];
                        if (region.Contains(oProvRegion))                               // just check main region of province for speed purposes
                        {
                            negativeRegions.Add(oProvRegion.Clone());
                        }
                    }
                }
            }
            // Collapse negative regions in big holes
            for (int nr = 0; nr < negativeRegions.Count - 1; nr++)
            {
                for (int nr2 = nr + 1; nr2 < negativeRegions.Count; nr2++)
                {
                    if (negativeRegions [nr].Intersects(negativeRegions [nr2]))
                    {
                        Clipper clipper = new Clipper();
                        int     control = negativeRegions [nr].points.Length;
                        clipper.AddPath(negativeRegions [nr], PolyType.ptSubject);
                        clipper.AddPath(negativeRegions [nr2], PolyType.ptClip);
                        clipper.Execute(ClipType.ctUnion);
                        negativeRegions.RemoveAt(nr2);
                        nr = -1;
                        break;
                    }
                }
            }

            // Substract holes
            int negativeRegionsCount = negativeRegions.Count;

            for (int r = 0; r < negativeRegionsCount; r++)
            {
                int       pointCount = negativeRegions [r].points.Length;
                Vector2[] pp         = new Vector2[pointCount];
                for (int p = 0; p < pointCount; p++)
                {
                    Vector2 point    = negativeRegions [r].points [p];
                    Vector2 midPoint = negativeRegions [r].center;
                    pp [p] = point + (midPoint - point) * 0.0001f;                     // prevents Poly2Tri issues when enclave boarders are to near from region borders
                }
                Poly2Tri.Polygon polyHole = new Poly2Tri.Polygon(pp);
                poly.AddHole(polyHole);
            }
        }
        public Country[] GetCountries()
        {
            if (mode != TerritoriesImporterMode.Countries)
            {
                return(null);
            }
            int entitiesCount = entities.Count;

            for (int k = 0; k < entitiesCount; k++)
            {
                if (entities [k].regions.Count == 0)
                {
                    entities.RemoveAt(k);
                    entitiesCount--;
                    if (k > 0)
                    {
                        k--;
                    }
                }
            }
            entitiesCount = entities.Count;

            // Check for holes
            for (int c = 0; c < entitiesCount; c++)
            {
                Country country = (Country)entities [c];
                if (country.name.Equals(BACKGROUND_COUNTRY))
                {
                    continue;
                }
                WMSK.instance.RefreshCountryGeometry(country);
                for (int r0 = 0; r0 < country.regions.Count; r0++)
                {
                    Region region0 = country.regions [r0];
                    for (int r1 = 0; r1 < country.regions.Count; r1++)
                    {
                        if (r1 == r0)
                        {
                            continue;
                        }
                        Region region1 = country.regions [r1];
                        if (region0.Contains(region1))
                        {
                            // region1 is a hole
                            if (!CountryBordersTooNearRegion(country, region1))
                            {
                                Country bgCountry = GetBackgroundCountry();
                                bgCountry.regions.Add(region1);
                            }
                            country.regions.Remove(region1);
                            r0 = -1;
                            break;
                        }
                    }
                }
            }

            entitiesCount = entities.Count;
            Country[] newCountries = new Country[entitiesCount];
            for (int k = 0; k < entitiesCount; k++)
            {
                newCountries [k] = (Country)entities [k];
            }
            return(newCountries);
        }