private static List <DistrictGenerator> CreateDistrictsWithAdjacency(IList <TriangleF> triangles) { // Prepare a list of the district for each triangle, and vice versa, so we can pass on adjacency information. var trianglesFromDistricts = new Dictionary <DistrictGenerator, TriangleF>(triangles.Count); var districtsFromTriangles = new Dictionary <TriangleF, DistrictGenerator>(triangles.Count); foreach (var triangle in triangles) { var district = new DistrictGenerator(triangle.Vertices); trianglesFromDistricts.Add(district, triangle); districtsFromTriangles.Add(triangle, district); } foreach (var kvp in trianglesFromDistricts) { var district = kvp.Key; var triangle = kvp.Value; var toAdd = triangle.AdjacentTriangles .Select(t => districtsFromTriangles[t]); district.AdjacentDistricts.UnionWith(toAdd); } return(trianglesFromDistricts.Keys.ToList()); }
public static float AccumulateValues(this IList <DistrictEffect> effects, DistrictGenerator district) { var value = 0f; foreach (var effect in effects) { value = effect.AccumulateValue(value, district); } return(value); }
public void MergeWithDistrict(DistrictGenerator other, AdjacencyInfo adjacency) { MergeWith(other, adjacency); foreach (var yetAnother in other.AdjacentDistricts) { yetAnother.AdjacentDistricts.Remove(other); if (yetAnother != this && !yetAnother.AdjacentDistricts.Contains(this)) { yetAnother.AdjacentDistricts.Add(this); } } var toAdd = other.AdjacentDistricts .Where(d => d != this && !AdjacentDistricts.Contains(d)); foreach (var district in toAdd) { AdjacentDistricts.Add(district); } }
public override float AccumulateValue(float prevValue, DistrictGenerator district) => Effect(prevValue, district);
private bool MergeDistricts(List <DistrictGenerator> districts, Func <DistrictGenerator, bool> districtFilter, Func <DistrictGenerator, bool> targetFilter) { bool addedAny = false; for (int iPolygon = 0; iPolygon < districts.Count; iPolygon++) { var testPolygon = districts[iPolygon]; if (!districtFilter(testPolygon)) { continue; } // This polygon should be merged onto the adjacent polygon that shares its longest edge AdjacencyInfo bestAdjacency = null; DistrictGenerator bestPolygon = null; foreach (var polygon in districts) { if (polygon == testPolygon) { continue; // don't merge with self } if (!targetFilter(polygon)) { continue; } if (!testPolygon.AdjacentDistricts.Contains(polygon)) { continue; // filter out non-adjacent districts quickly } var adjacency = testPolygon.GetAdjacencyInfo(polygon); if (adjacency == null) { continue; // don't merge if not adjacent } if (bestAdjacency != null && adjacency.Length < bestAdjacency.Length) { continue; // don't merge if we already have a polygon we share longer edge(s) with } bestAdjacency = adjacency; bestPolygon = polygon; } if (bestPolygon == null) { continue; } bestPolygon.MergeWithDistrict(testPolygon, bestAdjacency); districts.RemoveAt(iPolygon); iPolygon--; addedAny = true; } return(addedAny); }
public override float AccumulateValue(float prevValue, DistrictGenerator district) { return(prevValue + GetValue(district.GetCenter())); }
public void RemoveDistrict(DistrictGenerator district) { districts.Remove(district); district.Region = null; Population -= district.Population; }
public void AddDistrict(DistrictGenerator district) { districts.Add(district); district.Region = this; Population += district.Population; }
public abstract float AccumulateValue(float prevValue, DistrictGenerator district);