private void mapImage_MouseMove(object sender, MouseEventArgs e) { Color color = GetColorFromImage(e); MapCountry mapCountry = GetCountryByColor(color); if (mapCountry != null) { this.mapImage.Cursor = Cursors.Hand; this.mapImage.Source = mapCountry.GetMapImage(); this.txtSelectedCountry.Text = mapCountry.GetDescription(); this.refColor.Fill = new SolidColorBrush(mapCountry.GetSecondaryColor()); } else { this.mapImage.Cursor = Cursors.Arrow; if (_selectedCountry != null) { this.mapImage.Source = _selectedCountry.GetMapImage(); this.txtSelectedCountry.Text = _selectedCountry.GetDescription(); this.refColor.Fill = new SolidColorBrush(_selectedCountry.GetSecondaryColor()); } else { this.mapImage.Source = _worldMapImage; this.txtSelectedCountry.Text = _defaultText; } } }
private void mapImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { try { Color color = GetColorFromImage(e); MapCountry mapCountry = GetCountryByColor(color); if (mapCountry != null) { _selectedCountry = mapCountry; this.nextButton.IsEnabled = true; this.txtSelectedCountry.Text = mapCountry.GetDescription(); this.mapImage.Source = mapCountry.GetMapImage(); this.refColor.Visibility = System.Windows.Visibility.Visible; this.refColor.Fill = new SolidColorBrush(mapCountry.GetSecondaryColor()); } if (_selectedCountry != null) { if (_selectedCountry.IsOwnCode("AR")) //HACK, Como siempre es argentina, hardcodeo las regiones y sus Ids { _allRegionsForSelectedCountry = new List <Region>(); //Inicializacion de variables var selectRegionOption = new Region(0, Labels.SelectRegion, "AR"); var saltaRegionOption = new Region(1, "Salta", "AR"); var chacoRegionOption = new Region(2, "Chaco", "AR"); _allRegionsForSelectedCountry.Add(selectRegionOption); _allRegionsForSelectedCountry.Add(saltaRegionOption); _allRegionsForSelectedCountry.Add(chacoRegionOption); this.regionsCombo.ItemsSource = _allRegionsForSelectedCountry; this.regionsCombo.SelectedValue = selectRegionOption.Id; this.Regions.Visibility = Visibility.Visible; this.nextButton.IsEnabled = true; } else { regionRequests.GetRegionsByCountryCode(_selectedCountry.GetCode()); this.nextButton.IsEnabled = false; } } } catch (Exception ex) { throw ex; } }
public CountrySelector(string currentPlayingCountry, int currentPlayingRegion) { InitializeComponent(); _worldMapImage = Earthwatchers.UI.Resources.ResourceHelper.GetBitmap("/Resources/Images/world-map.png"); _defaultText = Labels.SelectCountryFromMap; _selectedCountry = null; _mapCoutries = new List <MapCountry>(); _mapCoutries.Add(new MapCountry("AR", Labels.ARGENTINA, Color.FromArgb(255, 0, 73, 126), Color.FromArgb(255, 0, 147, 254))); _mapCoutries.Add(new MapCountry("CA", Labels.CANADA, Color.FromArgb(255, 255, 216, 0), Color.FromArgb(255, 255, 246, 0))); _mapCoutries.Add(new MapCountry("CN", Labels.CHINA, Color.FromArgb(255, 127, 0, 0), Color.FromArgb(255, 224, 0, 0))); var currentCountry = GetCountryByCode(currentPlayingCountry); if (currentCountry == null) { this.mapImage.Source = _worldMapImage; this.txtSelectedCountry.Text = _defaultText; } else { this.mapImage.Source = currentCountry.GetMapImage(); this.txtSelectedCountry.Text = currentCountry.GetDescription(); } this.nextButton.IsEnabled = false; earthwatcherRequests = new EarthwatcherRequests(Constants.BaseApiUrl); landRequests = new LandRequests(Constants.BaseApiUrl); scoreRequests = new ScoreRequests(Constants.BaseApiUrl); regionRequests = new RegionRequests(Constants.BaseApiUrl); earthwatcherRequests.PlayingRegionChanged += earthwatcherRequests_PlayingRegionChanged; earthwatcherRequests.LandReassignedByPlayingRegion += earthwatcherRequest_LandReassignedByPlayingRegion; landRequests.LandsReceived += landRequests_LandsReceived; regionRequests.RegionsReceived += regionRequests_RegionsReceived; }
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 } } }
void UpdateCountryBoundaries() { if (mapCountries == null) { return; } // Update territory region int terrCount = mapCountries.Count; for (int k = 0; k < terrCount; k++) { MapCountry territory = mapCountries[k]; MapRegion territoryRegion = territory.region; if (territoryRegion.polygon == null) { continue; } territoryRegion.points = territoryRegion.polygon.contours[0].GetVector2Points(); List <Point> points = territoryRegion.polygon.contours[0].points; int pointCount = points.Count; for (int j = 0; j < pointCount; j++) { Point p0 = points[j]; Point p1; if (j == pointCount - 1) { p1 = points[0]; } else { p1 = points[j + 1]; } territoryRegion.segments.Add(new Segment(p0, p1)); } // Update bounding rect float minx, miny, maxx, maxy; minx = miny = float.MaxValue; maxx = maxy = float.MinValue; int terrPointCount = territoryRegion.points.Length; for (int p = 0; p < terrPointCount; p++) { Vector2 point = territoryRegion.points[p]; if (point.x < minx) { minx = point.x; } if (point.x > maxx) { maxx = point.x; } if (point.y < miny) { miny = point.y; } if (point.y > maxy) { maxy = point.y; } } float rectWidth = maxx - minx; float rectHeight = maxy - miny; territoryRegion.rect2D = new Rect(minx, miny, rectWidth, rectHeight); territoryRegion.rect2DArea = rectWidth * rectHeight; } }
void CreateMapCountries() { numCountries = Mathf.Clamp(numCountries, 1, Mathf.Min(numProvinces, MAX_TERRITORIES)); if (mapCountries == null) { mapCountries = new List <MapCountry>(numCountries); } else { mapCountries.Clear(); } for (int k = 0; k < mapProvinces.Count; k++) { mapProvinces[k].countryIndex = -1; } int cellsCount = mapProvinces.Count; for (int c = 0; c < numCountries; c++) { MapCountry territory = new MapCountry(c.ToString()); int territoryIndex = mapCountries.Count; int p = UnityEngine.Random.Range(0, cellsCount); int z = 0; while ((mapProvinces[p].countryIndex != -1 || !mapProvinces[p].visible || mapProvinces[p].ignoreTerritories) && z++ <= mapProvinces.Count) { p++; if (p >= mapProvinces.Count) { p = 0; } } if (z > mapProvinces.Count) { break; // no more territories can be found - this should not happen } MapProvince prov = mapProvinces[p]; prov.countryIndex = territoryIndex; territory.capitalCenter = prov.center; territory.provinces.Add(prov); mapCountries.Add(territory); } // Continue conquering cells int[] territoryCellIndex = new int[mapCountries.Count]; // Iterate one cell per country (this is not efficient but ensures balanced distribution) bool remainingCells = true; int territoriesCount = mapCountries.Count; while (remainingCells) { while (remainingCells) { remainingCells = false; for (int k = 0; k < territoriesCount; k++) { MapCountry territory = mapCountries[k]; int territoryCellsCount = territory.provinces.Count; for (int p = territoryCellIndex[k]; p < territoryCellsCount; p++) { MapRegion cellRegion = territory.provinces[p].region; int neighboursCount = cellRegion.neighbours.Count; for (int n = 0; n < neighboursCount; n++) { MapRegion otherRegion = cellRegion.neighbours[n]; MapProvince otherProv = (MapProvince)otherRegion.entity; if (otherProv.countryIndex == -1 && otherProv.visible && !otherProv.ignoreTerritories) { otherProv.countryIndex = k; territory.provinces.Add(otherProv); remainingCells = true; p = territory.provinces.Count; break; } } if (p < territoryCellsCount) // no free neighbours left for this cell { territoryCellIndex[k]++; } } } } // Check if there's any other cell without territory for (int k = 0; k < cellsCount; k++) { MapProvince cell = mapProvinces[k]; if (cell.countryIndex == -1 && cell.visible && !cell.ignoreTerritories) { int territoryIndex = UnityEngine.Random.Range(0, territoriesCount); cell.countryIndex = territoryIndex; mapCountries[territoryIndex].provinces.Add(cell); remainingCells = true; break; } } } FindCountryFrontiers(); UpdateCountryBoundaries(); }
void FindCountryFrontiers() { if (mapCountries == null || mapCountries.Count == 0) { return; } if (territoryFrontiers == null) { territoryFrontiers = new List <Segment>(cellNeighbourHit.Count); } else { territoryFrontiers.Clear(); } if (territoryNeighbourHit == null) { territoryNeighbourHit = new Dictionary <Segment, MapRegion>(50000); } else { territoryNeighbourHit.Clear(); } int terrCount = mapCountries.Count; Connector[] connectors = new Connector[terrCount]; for (int k = 0; k < terrCount; k++) { connectors[k] = new Connector(); MapCountry territory = mapCountries[k]; territory.provinces.Clear(); if (territory.region == null) { MapRegion territoryRegion = new MapRegion(territory); territory.region = territoryRegion; } mapCountries[k].region.neighbours.Clear(); } int cellCount = mapProvinces.Count; for (int k = 0; k < cellCount; k++) { MapProvince cell = mapProvinces[k]; if (cell.countryIndex >= terrCount) { continue; } bool validCell = cell.visible && cell.countryIndex >= 0; if (validCell) { mapCountries[cell.countryIndex].provinces.Add(cell); } MapRegion region = cell.region; int numSegments = region.segments.Count; for (int i = 0; i < numSegments; i++) { Segment seg = region.segments[i]; if (seg.border) { if (validCell) { territoryFrontiers.Add(seg); int territory1 = cell.countryIndex; connectors[territory1].Add(seg); seg.territoryIndex = territory1; } continue; } if (territoryNeighbourHit.ContainsKey(seg)) { MapRegion neighbour = territoryNeighbourHit[seg]; MapProvince neighbourCell = (MapProvince)neighbour.entity; int territory1 = cell.countryIndex; int territory2 = neighbourCell.countryIndex; if (territory2 != territory1) { territoryFrontiers.Add(seg); if (validCell) { connectors[territory1].Add(seg); seg.territoryIndex = (territory2 >= 0) ? -1 : territory1; // if segment belongs to a visible cell and valid territory2, mark this segment as disputed. Otherwise make it part of territory1 if (seg.territoryIndex < 0) { // add territory neigbhours MapRegion territory1Region = mapCountries[territory1].region; MapRegion territory2Region = mapCountries[territory2].region; if (!territory1Region.neighbours.Contains(territory2Region)) { territory1Region.neighbours.Add(territory2Region); } if (!territory2Region.neighbours.Contains(territory1Region)) { territory2Region.neighbours.Add(territory1Region); } } } if (territory2 >= 0) { connectors[territory2].Add(seg); } } } else { territoryNeighbourHit.Add(seg, region); seg.territoryIndex = cell.countryIndex; } } } for (int k = 0; k < terrCount; k++) { mapCountries[k].region = new MapRegion(mapCountries[k]); if (mapCountries[k].provinces.Count > 0) { mapCountries[k].region.polygon = connectors[k].ToPolygonFromLargestLineStrip(); } else { mapCountries[k].region.polygon = null; } } }