void CountryMergeAdjacentRegions(Country targetCountry) { // Merges adjacent regions of target country int numRegions = targetCountry.regions.Count; WPM.Geom.Polygon[] countryPolys = new WPM.Geom.Polygon[numRegions]; for (int k = 0; k < numRegions; k++) { countryPolys[k] = GetGeomPolygon(targetCountry.regions[k].latlon); } for (int k = 0; k < targetCountry.regions.Count; k++) { if (countryPolys[k] != null) { Region region1 = targetCountry.regions[k]; for (int j = k + 1; j < targetCountry.regions.Count; j++) { if (countryPolys[j] != null) { PolygonClipper pc = new PolygonClipper(countryPolys[k], countryPolys[j]); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); ReplacePointsFromPolygon(region1, pc.subject); countryPolys[k] = GetGeomPolygon(targetCountry.regions[k].latlon); targetCountry.regions.RemoveAt(j); if (countryIndex >= 0 && targetCountry == _map.countries[countryIndex] && countryRegionIndex >= j) { countryRegionIndex--; } countryPolys[j] = null; } } } } } }
/// <summary> /// Makes one country region to annex another country /// </summary> public void CountryRegionTransferTo() { if (countryIndex < 0 || GUICountryTransferToCountryIndex < 0 || GUICountryTransferToCountryIndex >= countryNames.Length || countryRegionIndex < 0) { return; } // Get target country // recover GUI country index selection int targetCountryIndex = -1; string[] s = countryNames [GUICountryTransferToCountryIndex].Split(new char[] { '(', ')' }, System.StringSplitOptions.RemoveEmptyEntries); if (s.Length >= 2) { if (!int.TryParse(s [1], out targetCountryIndex)) { return; } } Country sourceCountry = map.countries [countryIndex]; Country targetCountry = map.countries [targetCountryIndex]; Region sourceRegion = sourceCountry.regions [countryRegionIndex]; Region targetRegion = targetCountry.regions [targetCountry.mainRegionIndex]; // Transfer all provinces records to target country if (targetCountry.provinces == null && !map.showProvinces) { map.showProvinces = true; // Forces loading of provinces map.showProvinces = false; } if (sourceCountry.provinces != null) { List <Province> destProvinces; if (targetCountry.provinces != null) { destProvinces = new List <Province> (targetCountry.provinces); } else { destProvinces = new List <Province> (); } List <Province> sourceProvinces = new List <Province> (sourceCountry.provinces); for (int k = 0; k < sourceCountry.provinces.Length; k++) { Province province = sourceCountry.provinces [k]; if (sourceRegion.Contains(province.center)) { province.countryIndex = targetCountryIndex; destProvinces.Add(province); sourceProvinces.Remove(province); provinceChanges = true; } } sourceCountry.provinces = sourceProvinces.ToArray(); targetCountry.provinces = destProvinces.ToArray(); } // Add region to target country's polygon - only if the province is touching or crossing target country frontier PolygonClipper pc = new PolygonClipper(targetRegion, sourceRegion); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); } else { // Add new region to country Region newCountryRegion = new Region(targetCountry, targetCountry.regions.Count); newCountryRegion.points = new List <Vector3> (sourceRegion.points).ToArray(); targetCountry.regions.Add(newCountryRegion); } // Transfer cities & mount points TransferRegionCities(countryIndex, sourceRegion, targetCountryIndex); TransferRegionMountPoints(countryIndex, sourceRegion, targetCountryIndex); // Remove region from source country sourceCountry.regions.Remove(sourceRegion); // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.RefreshCountryDefinition(countryIndex, null); map.RefreshCountryDefinition(targetCountryIndex, null); countryChanges = true; countryIndex = targetCountryIndex; countryRegionIndex = targetCountry.mainRegionIndex; CountryRegionSelect(); map.RedrawMapLabels(); }
/// <summary> /// Changes province's owner to specified country /// </summary> public void ProvinceTransferTo() { if (provinceIndex < 0 || GUIProvinceTransferToCountryIndex < 0 || GUIProvinceTransferToCountryIndex >= countryNames.Length) { return; } // Get target country // recover GUI country index selection int targetCountryIndex = -1; string[] s = countryNames [GUIProvinceTransferToCountryIndex].Split(new char[] { '(', ')' }, System.StringSplitOptions.RemoveEmptyEntries); if (s.Length >= 2) { if (!int.TryParse(s [1], out targetCountryIndex)) { return; } } // Remove province form source country Province province = map.provinces[provinceIndex]; Country sourceCountry = map.countries[countryIndex]; if (map.countries[countryIndex].provinces != null) { List <Province> sourceProvinces = new List <Province>(sourceCountry.provinces); int provIndex = -1; for (int k = 0; k < sourceCountry.provinces.Length; k++) { if (sourceCountry.provinces[k].name.Equals(province.name)) { provIndex = k; } } if (provIndex >= 0) { sourceProvinces.RemoveAt(provIndex); sourceCountry.provinces = sourceProvinces.ToArray(); } } // Adds province to target country province.countryIndex = targetCountryIndex; Country targetCountry = map.countries[targetCountryIndex]; List <Province> destProvinces = new List <Province>(targetCountry.provinces); destProvinces.Add(province); targetCountry.provinces = destProvinces.ToArray(); // Apply boolean operations on country polygons Region provinceRegion = province.regions[provinceRegionIndex]; Region sourceRegion = sourceCountry.regions[sourceCountry.mainRegionIndex]; Region targetRegion = targetCountry.regions[targetCountry.mainRegionIndex]; // Extract from source country - only if province is in the frontier or is crossing the country RegionSanitize(sourceRegion); RegionSanitize(provinceRegion); WPM.Geom.Polygon provincePolygon = GetGeomPolygon(provinceRegion.latlon); PolygonClipper pc = new PolygonClipper(GetGeomPolygon(sourceRegion.latlon), provincePolygon); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.DIFFERENCE); ReplacePointsFromPolygon(sourceRegion, pc.subject); } // Add region to target country's polygon - only if the province is touching or crossing target country frontier RegionSanitize(targetRegion); pc = new PolygonClipper(GetGeomPolygon(targetRegion.latlon), provincePolygon); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); ReplacePointsFromPolygon(targetRegion, pc.subject); } else { // Add new region to country Region newCountryRegion = new Region(targetCountry, targetCountry.regions.Count); newCountryRegion.points = new List <Vector3>(provinceRegion.points).ToArray(); UpdateLatLonFromPoints(newCountryRegion); targetCountry.regions.Add(newCountryRegion); } // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); // Refresh definition of old country map.RefreshCountryDefinition(countryIndex, null); // Link province to target country and refresh definition of target country map.RefreshCountryDefinition(targetCountryIndex, null); map.RefreshProvinceDefinition(provinceIndex); countryChanges = true; provinceChanges = true; countryIndex = targetCountryIndex; countryRegionIndex = targetCountry.mainRegionIndex; ProvinceRegionSelect(); }
/// <summary> /// Makes one country to annex another /// </summary> public void CountryTransferTo() { if (countryIndex < 0 || GUICountryTransferToCountryIndex < 0 || GUICountryTransferToCountryIndex >= countryNames.Length) { return; } // Get target country // recover GUI country index selection int targetCountryIndex = -1; string[] s = countryNames [GUICountryTransferToCountryIndex].Split(new char[] { '(', ')' }, System.StringSplitOptions.RemoveEmptyEntries); if (s.Length >= 2) { if (!int.TryParse(s [1], out targetCountryIndex)) { return; } } // Transfer all provinces records to target country Country sourceCountry = map.countries [countryIndex]; Country targetCountry = map.countries [targetCountryIndex]; //MAX! mergeCountryData(sourceCountry, targetCountry); dataChanges = true; ndChanges = true; if (targetCountry.provinces == null && !map.showProvinces) { map.showProvinces = true; // Forces loading of provinces map.showProvinces = false; } if (sourceCountry.provinces != null) { List <Province> destProvinces; if (targetCountry.provinces != null) { destProvinces = new List <Province> (targetCountry.provinces); } else { destProvinces = new List <Province> (); } for (int k = 0; k < sourceCountry.provinces.Length; k++) { Province province = sourceCountry.provinces [k]; province.countryIndex = targetCountryIndex; destProvinces.Add(province); } targetCountry.provinces = destProvinces.ToArray(); } // Add main region of the source country to target if they are joint Region sourceRegion = sourceCountry.regions [sourceCountry.mainRegionIndex]; Region targetRegion = targetCountry.regions [targetCountry.mainRegionIndex]; //MAX! if (sourceRegion.neighbours.Contains(targetRegion)) { // Add region to target country's polygon - only if the province is touching or crossing target country frontier PolygonClipper pc = new PolygonClipper(targetRegion, sourceRegion); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); } //MAX! } else { // Add new region to country Region newCountryRegion = new Region(targetCountry, targetCountry.regions.Count); newCountryRegion.points = new List <Vector3> (sourceRegion.points).ToArray(); targetCountry.regions.Add(newCountryRegion); } // Transfer additional regions if (sourceCountry.regions.Count > 1) { List <Region> targetRegions = new List <Region> (targetCountry.regions); for (int k = 0; k < sourceCountry.regions.Count; k++) { if (k != sourceCountry.mainRegionIndex) { targetRegions.Add(sourceCountry.regions [k]); } } targetCountry.regions = targetRegions; } //MAX! if (!map.showCities && map.cities == null) { map.ReadCitiesPackedString(); } // Transfer cities & mount points TransferCities(countryIndex, targetCountryIndex); TransferMountPoints(countryIndex, targetCountryIndex); // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.CountryDelete(countryIndex, false); map.RefreshCountryDefinition(targetCountryIndex, null); countryChanges = true; provinceChanges = true; countryIndex = targetCountryIndex; countryRegionIndex = targetCountry.mainRegionIndex; CountryRegionSelect(); map.RedrawMapLabels(); }
/// <summary> /// Makes one country to annex another /// </summary> public void CountryTransferTo() { if (countryIndex < 0 || GUICountryTransferToCountryIndex < 0 || GUICountryTransferToCountryIndex >= countryNames.Length) { return; } // Get target country // recover GUI country index selection int targetCountryIndex = -1; string[] s = countryNames [GUICountryTransferToCountryIndex].Split(new char[] { '(', ')' }, System.StringSplitOptions.RemoveEmptyEntries); if (s.Length >= 2) { if (!int.TryParse(s [1], out targetCountryIndex)) { return; } } // Transfer all provinces records to target country Country sourceCountry = map.countries[countryIndex]; Country targetCountry = map.countries[targetCountryIndex]; if (targetCountry.provinces == null && !map.showProvinces) { map.showProvinces = true; // Forces loading of provinces map.showProvinces = false; } List <Province> destProvinces = new List <Province>(targetCountry.provinces); for (int k = 0; k < sourceCountry.provinces.Length; k++) { Province province = sourceCountry.provinces[k]; province.countryIndex = targetCountryIndex; destProvinces.Add(province); } targetCountry.provinces = destProvinces.ToArray(); // Add main region of the source country to target if they are joint Region sourceRegion = sourceCountry.regions[countryRegionIndex]; // sourceCountry.mainRegionIndex]; Region targetRegion = targetCountry.regions[targetCountry.mainRegionIndex]; // Add region to target country's polygon - only if the province is touching or crossing target country frontier // RegionSanitize(targetRegion); // RegionSanitize(sourceRegion); PolygonClipper pc = new PolygonClipper(GetGeomPolygon(targetRegion.latlon), GetGeomPolygon(sourceRegion.latlon)); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); ReplacePointsFromPolygon(targetRegion, pc.subject); } else { // Add new region to country sourceRegion.entity = targetCountry; sourceRegion.regionIndex = targetCountry.regions.Count; targetCountry.regions.Add(sourceRegion); } // Transfer additional regions if (sourceCountry.regions.Count > 1) { List <Region> targetRegions = new List <Region>(targetCountry.regions); for (int k = 0; k < sourceCountry.regions.Count; k++) { if (k != countryRegionIndex) //sourceCountry.mainRegionIndex) { { targetRegions.Add(sourceCountry.regions[k]); } } targetCountry.regions = targetRegions; } CountryMergeAdjacentRegions(targetCountry); // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.CountryDelete(countryIndex, false); if (countryIndex < targetCountryIndex) { targetCountryIndex--; // source country has been deleted moving up remaining countries in the list } map.RefreshCountryDefinition(targetCountryIndex, null); countryChanges = true; provinceChanges = true; countryIndex = targetCountryIndex; countryRegionIndex = targetCountry.mainRegionIndex; CountryRegionSelect(); map.RedrawMapLabels(); }
/// <summary> /// Separates a province from its current country producing a new country /// </summary> public void ProvinceSeparate(string newCountryName) { if (provinceIndex < 0 || provinceIndex >= map.provinces.Length) { return; } // Remove province form source country Province province = map.provinces [provinceIndex]; Country sourceCountry = map.countries [countryIndex]; if (map.countries [countryIndex].provinces != null) { List <Province> sourceProvinces = new List <Province> (sourceCountry.provinces); int provIndex = -1; for (int k = 0; k < sourceCountry.provinces.Length; k++) { if (sourceCountry.provinces [k].name.Equals(province.name)) { provIndex = k; } } if (provIndex >= 0) { sourceProvinces.RemoveAt(provIndex); sourceCountry.provinces = sourceProvinces.ToArray(); } } // Adds province region to a new country Region regionProvince = province.regions [provinceRegionIndex]; Country targetCountry = new Country(newCountryName, sourceCountry.continent); Region region = new Region(targetCountry, 0); region.points = new List <Vector3> (regionProvince.points).ToArray(); targetCountry.regions.Add(region); map.CountryAdd(targetCountry); int targetCountryIndex = map.countries.Length - 1; map.RefreshCountryDefinition(targetCountryIndex, null); lastCountryCount = -1; // Add province to the new country if (targetCountry.provinces == null) { targetCountry.provinces = new Province[0]; } List <Province> destProvinces = new List <Province> (targetCountry.provinces); destProvinces.Add(province); targetCountry.provinces = destProvinces.ToArray(); // Apply boolean operations on country polygons Region provinceRegion = province.regions [provinceRegionIndex]; Region sourceRegion = sourceCountry.regions [sourceCountry.mainRegionIndex]; // Extract from source country - only if province is in the frontier or is crossing the country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; otherSourceRegion.sanitized = true; } PolygonClipper pc = new PolygonClipper(sourceRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { sourceRegion.sanitized = false; pc.Compute(PolygonOp.DIFFERENCE); } else { // Look for other regions to substract for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; pc = new PolygonClipper(otherSourceRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { otherSourceRegion.sanitized = false; pc.Compute(PolygonOp.DIFFERENCE); } } } // Remove invalid regions from source country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; if (!otherSourceRegion.sanitized && otherSourceRegion.points.Length < 5) { sourceCountry.regions.RemoveAt(k); k--; } } // Transfer cities int cityCount = map.cities.Count; for (int k = 0; k < cityCount; k++) { City city = map.cities [k]; if (city.countryIndex == countryIndex && city.province.Equals(province.name)) { city.countryIndex = targetCountryIndex; } } // Transfer mount points int mountPointCount = map.mountPoints.Count; for (int k = 0; k < mountPointCount; k++) { MountPoint mp = map.mountPoints [k]; if (mp.countryIndex == countryIndex && mp.provinceIndex == provinceIndex) { mp.countryIndex = targetCountryIndex; } } // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.RefreshCountryDefinition(province.countryIndex, null); province.countryIndex = targetCountryIndex; map.RefreshProvinceDefinition(provinceIndex); map.RefreshCountryDefinition(targetCountryIndex, null); countryChanges = true; provinceChanges = true; cityChanges = true; mountPointChanges = true; ProvinceRegionSelect(); }
/// <summary> /// Deletes current region or province if this was the last region /// </summary> public void ProvinceDelete() { if (provinceIndex < 0 || provinceIndex >= map.provinces.Length) { return; } // Apply boolean operations on country polygons Province province = map.provinces [provinceIndex]; Region provinceRegion = province.regions [provinceRegionIndex]; Country sourceCountry = map.countries [countryIndex]; // Extract from source country - only if province is in the frontier or is crossing the country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; otherSourceRegion.sanitized = true; } for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; PolygonClipper pc = new PolygonClipper(otherSourceRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { otherSourceRegion.sanitized = false; pc.Compute(PolygonOp.DIFFERENCE); } } // Remove invalid regions from source country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; if (!otherSourceRegion.sanitized && otherSourceRegion.points.Length < 5) { sourceCountry.regions.RemoveAt(k); k--; } } // Remove it from the country array List <Province> newProvinces = new List <Province> (map.countries [countryIndex].provinces.Length - 1); for (int k = 0; k < map.countries [countryIndex].provinces.Length; k++) { if (!map.countries [countryIndex].provinces [k].name.Equals(GUIProvinceName)) { newProvinces.Add(map.countries [countryIndex].provinces [k]); } } map.countries [countryIndex].provinces = newProvinces.ToArray(); // Remove from the global array newProvinces = new List <Province> (map.provinces.Length - 1); for (int k = 0; k < map.provinces.Length; k++) { if (k != provinceIndex) { newProvinces.Add(map.provinces [k]); } } map.provinces = newProvinces.ToArray(); // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.RefreshCountryDefinition(countryIndex, null); ClearProvinceSelection(); map.OptimizeFrontiers(); map.Redraw(); countryChanges = true; provinceChanges = true; CountryRegionSelect(); }
public void ProvinceTransferTo(int targetCountryIndex, int provinceIndex) { // Remove province form source country Province province = map.provinces [provinceIndex]; Country sourceCountry = map.countries [countryIndex]; if (map.countries [countryIndex].provinces != null) { List <Province> sourceProvinces = new List <Province> (sourceCountry.provinces); int provIndex = -1; for (int k = 0; k < sourceCountry.provinces.Length; k++) { if (sourceCountry.provinces [k].name.Equals(province.name)) { provIndex = k; } } if (provIndex >= 0) { sourceProvinces.RemoveAt(provIndex); sourceCountry.provinces = sourceProvinces.ToArray(); } } // Adds province to target country Country targetCountry = map.countries [targetCountryIndex]; if (targetCountry.provinces == null) { targetCountry.provinces = new Province[0]; } List <Province> destProvinces = new List <Province> (targetCountry.provinces); destProvinces.Add(province); targetCountry.provinces = destProvinces.ToArray(); // Apply boolean operations on country polygons Region provinceRegion = province.regions [provinceRegionIndex]; Region sourceRegion = sourceCountry.regions [sourceCountry.mainRegionIndex]; Region targetRegion = targetCountry.regions [targetCountry.mainRegionIndex]; // Extract from source country - only if province is in the frontier or is crossing the country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; otherSourceRegion.sanitized = true; } PolygonClipper pc = new PolygonClipper(sourceRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { sourceRegion.sanitized = false; pc.Compute(PolygonOp.DIFFERENCE); } else { // Look for other regions to substract for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; pc = new PolygonClipper(otherSourceRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { otherSourceRegion.sanitized = false; pc.Compute(PolygonOp.DIFFERENCE); } } } // Remove invalid regions from source country for (int k = 0; k < sourceCountry.regions.Count; k++) { Region otherSourceRegion = sourceCountry.regions [k]; if (!otherSourceRegion.sanitized && otherSourceRegion.points.Length < 5) { sourceCountry.regions.RemoveAt(k); k--; } } // Add region to target country's polygon - only if the province is touching or crossing target country frontier pc = new PolygonClipper(targetRegion, provinceRegion); if (pc.OverlapsSubjectAndClipping()) { pc.Compute(PolygonOp.UNION); } else { // Add new region to country Region newCountryRegion = new Region(targetCountry, targetCountry.regions.Count); newCountryRegion.points = new List <Vector3> (provinceRegion.points).ToArray(); targetCountry.regions.Add(newCountryRegion); } //Max! if (!map.showCities && map.cities == null) { map.ReadCitiesPackedString(); } // Transfer cities int cityCount = map.cities.Count; for (int k = 0; k < cityCount; k++) { City city = map.cities [k]; if (city.countryIndex == countryIndex && city.province.Equals(province.name)) { city.countryIndex = targetCountryIndex; } } // Transfer mount points int mountPointCount = map.mountPoints.Count; for (int k = 0; k < mountPointCount; k++) { MountPoint mp = map.mountPoints [k]; if (mp.countryIndex == countryIndex && mp.provinceIndex == provinceIndex) { mp.countryIndex = targetCountryIndex; } } // Finish operation map.HideCountryRegionHighlights(true); map.HideProvinceRegionHighlights(true); map.RefreshCountryDefinition(province.countryIndex, null); province.countryIndex = targetCountryIndex; map.RefreshProvinceDefinition(provinceIndex); map.RefreshCountryDefinition(targetCountryIndex, null); countryChanges = true; provinceChanges = true; cityChanges = true; mountPointChanges = true; }