private void BuildRoads() { var topology = new Topology(this); var roads = new List <List <Vector2> > (); var streets = new List <List <Vector2> > (); foreach (var gate in Gates.ToList()) { var endPoint = Market.Shape.Vertices.OrderBy(v => (gate - v).magnitude).First(); var street = topology.BuildPath(gate, endPoint, topology.Outer); if (street != null) { //roads.Add(street); streets.Add(street); if (CityWall.Gates.Contains(gate)) { var direction = GeometryHelpers.Scale(gate - Center, 100000); var start = topology.Node2V.Values.OrderBy(v => (v - direction).magnitude).First(); var road = topology.BuildPath(start, gate, topology.Inner); if (road == null) { CityWall.Gates.Remove(gate); CityWall.Towers.Add(gate); Gates.Remove(gate); } else { roads.Add(road); } } } } if (!roads.Any()) { throw new InvalidOperationException("No roads into the town"); } Roads.AddRange(TidyUpRoads(roads)); Streets.AddRange(TidyUpRoads(streets)); foreach (var road in Roads) { var insideVertices = road.Where(rv => Patches.Where(p => p.Shape.ContainsPoint(rv)).All(p => p.WithinCity)).Except(Gates).ToList(); foreach (var insideVertex in insideVertices) { road.Remove(insideVertex); } } }