Пример #1
0
        private void BuildWalls()
        {
            var circumference         = FindCircumference(Patches.Where(p => p.WithinCity)).Vertices;
            var allowedTowerPositions = circumference.ToList();
            var castleVertices        = Castle.Patch.Shape.Vertices.ToList();

            foreach (var position in allowedTowerPositions.Intersect(WaterBorder).ToList())
            {
                var neighbours = Patches.Where(p => p.Shape.Vertices.Contains(position));
                if (!neighbours.Any(n => !n.WithinCity && !n.Water))
                {
                    allowedTowerPositions.Remove(position);
                }
            }

            // Make sure the wall starts and ends at the sea
            if (Options.Water)
            {
                var seaPoints = allowedTowerPositions.Where(t => Patches.Any(p => p.Water && p.Shape.Vertices.Contains(t))).ToList();

                var lastSeaPoint  = seaPoints[1];
                var firstSeaPoint = seaPoints[0];

                var minIndex = Math.Min(allowedTowerPositions.IndexOf(lastSeaPoint), allowedTowerPositions.IndexOf(firstSeaPoint));
                var maxIndex = Math.Max(allowedTowerPositions.IndexOf(lastSeaPoint), allowedTowerPositions.IndexOf(firstSeaPoint));

                if (minIndex != 0 && maxIndex != allowedTowerPositions.Count - 1)
                {
                    var seaPointIndex = allowedTowerPositions.IndexOf(lastSeaPoint);

                    while (seaPointIndex > 0)
                    {
                        var point = allowedTowerPositions[0];
                        allowedTowerPositions.RemoveAt(0);
                        allowedTowerPositions.Add(point);
                        seaPointIndex = allowedTowerPositions.IndexOf(lastSeaPoint);
                    }
                }
            }

            CityWall = new Wall(allowedTowerPositions, this, 2, 10, castleVertices);

            var allowedCastleWallPositions = castleVertices.Except(allowedTowerPositions).ToList();
            var firstIndex = castleVertices.IndexOf(allowedCastleWallPositions.First()) - 1;

            if (firstIndex < 0)
            {
                firstIndex = castleVertices.Count - 1;
            }
            var lastIndex = castleVertices.IndexOf(allowedCastleWallPositions.Last()) + 1;

            if (lastIndex > castleVertices.Count - 1)
            {
                lastIndex = 0;
            }
            allowedCastleWallPositions.Insert(0, castleVertices[firstIndex]);
            allowedCastleWallPositions.Add(castleVertices[lastIndex]);

            Castle.Wall = new Wall(allowedCastleWallPositions, this, 1, 1, circumference);
        }