Ejemplo n.º 1
0
Archivo: Town.cs Proyecto: twobob/Town
        public Town(TownOptions options)
        {
            Options  = options;
            Rnd.Seed = options.Seed.Value;

            var ok = false;

            while (!ok)
            {
                NumPatches  = options.Patches;
                Roads       = new List <List <Vector2> >();
                Streets     = new List <List <Vector2> >();
                Gates       = new List <Vector2>();
                Center      = new Vector2(Width / 2f * (1 + 0.1 * Rnd.NextDouble() - 0.1 * Rnd.NextDouble()), Height / 2f * (1 + 0.1 * Rnd.NextDouble() - 0.1 * Rnd.NextDouble()));
                Patches     = new List <Patch>();
                WaterBorder = new List <Vector2>();

                try
                {
                    BuildPatches();
                    OptimizePatches();
                    BuildWalls();
                    BuildRoads();
                    PopulateTown();

                    ok = Roads.Count > 1;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
        }
Ejemplo n.º 2
0
 protected override void GetBuildingSpecs(out float gridChaos, out float sizeChaos, out Func <Polygon, float> emptyProbabilityFunc, out float minArea)
 {
     minArea              = 60 + 160 * (float)Rnd.NextDouble() * (float)Rnd.NextDouble();
     gridChaos            = 0.05f + (float)Rnd.NextDouble() * 0.02f;
     sizeChaos            = 0.2f;
     emptyProbabilityFunc = p => 0.2f;
 }
Ejemplo n.º 3
0
Archivo: Town.cs Proyecto: twobob/Town
        private IEnumerable <Vector2> GetVoronoiPoints(int nPatches, Vector2 center)
        {
            var sa = Rnd.NextDouble() * 2 * Math.PI;

            for (var i = 0; i < nPatches * PatchMultiplier; i++)
            {
                var a = sa + Math.Sqrt(i) * 5;
                var r = (i == 0 ? 0 : 10 + i * (2 + Rnd.NextDouble()));
                yield return(new Vector2(Math.Cos(a) * r, Math.Sin(a) * r) + center);
            }
        }
Ejemplo n.º 4
0
        protected override void GetBuildingSpecs(out float gridChaos, out float sizeChaos, out Func <Polygon, float> emptyProbabilityFunc, out float minArea)
        {
            minArea   = 5 + 30 * (float)Rnd.NextDouble() * (float)Rnd.NextDouble();
            gridChaos = 0.4f + (float)Rnd.NextDouble() * 0.02f;
            sizeChaos = 1f;
            var closestWallPoint = Patch.Town.CityWall.Circumference.OrderBy(p => (p - Patch.Center).Length).First();
            var centerDistance   = (Patch.Center - closestWallPoint).Length;

            emptyProbabilityFunc = p =>
            {
                var distanceToWall       = (p.Center - closestWallPoint).Length;
                var normalizedDistanceSq = (float)Math.Pow(distanceToWall / (1.5f * centerDistance), 3);
                return(Math.Max(Math.Min(normalizedDistanceSq, 0.95f), 0.05f));
            };
        }
Ejemplo n.º 5
0
        private static IEnumerable <Polygon> CreateAlleys(Polygon block, float minArea, float gridChaos, float sizeChaos, Func <Polygon, float> emptyProbabilityFunc, bool split, int levels = 0)
        {
            Vector2 point  = Vector2.Zero;
            var     length = float.MinValue;

            block.ForEachEdge((p1, p2, index) =>
            {
                var len = (p1 - p2).Length;
                if (len > length)
                {
                    length = len;
                    point  = p1;
                }
            });

            var spread = 0.8f * gridChaos;
            var ratio  = (1 - spread) / 2 + (float)Rnd.NextDouble() * spread;

            // Trying to keep buildings rectangular even in chaotic wards
            var angleSpread = (float)(Math.PI / 6 * gridChaos * (block.Area() < minArea * 4 ? 0 : 1));
            var angle       = ((float)Rnd.NextDouble() - 0.5f) * angleSpread;

            var halves = block.Bisect(point, ratio, angle, split ? Alley : 0f);

            var buildings = new List <Polygon>();

            foreach (var half in halves)
            {
                if (half.Area() < minArea * Math.Pow(2, 4 * sizeChaos * (Rnd.NextDouble() - 0.5f)) || levels > 5)
                {
                    if (!Rnd.NextBool(emptyProbabilityFunc(half)))
                    {
                        buildings.Add(half);
                    }
                }
                else
                {
                    buildings.AddRange(CreateAlleys(half, minArea, gridChaos, sizeChaos, emptyProbabilityFunc, half.Area() > minArea / (Rnd.NextDouble() * Rnd.NextDouble()), levels + 1));
                }
            }

            return(buildings);
        }
Ejemplo n.º 6
0
Archivo: Town.cs Proyecto: twobob/Town
        private void BuildPatches()
        {
            var points = GetVoronoiPoints(NumPatches, Center).ToList();

            var v = Voronoi.Build(points);

            for (var i = 0; i < 3; i++)
            {
                var toRelax = v.Points.Take(3).ToList();
                toRelax.Add(v.Points[NumPatches]);
                v = Voronoi.Relax(v, toRelax);
            }

            v = Voronoi.Relax(v);

            var comparer = new Vector2LengthComparer();

            v.Points.Sort(comparer);

            var regions = v.Partitioning();

            Patches.Clear();
            foreach (var region in regions)
            {
                Patches.Add(Patch.FromRegion(this, region));
            }

            var patchesInTown = Patches.OrderBy(p => (Center - p.Center).Length).Take(NumPatches).ToList();

            // Find random patch at the outside of town to place water
            if (Options.Water)
            {
                var firstWaterPatch = patchesInTown.Where(p => p.GetAllNeighbours().Any(n => !n.WithinCity))
                                      .OrderBy(p => Rnd.NextDouble()).First();
                firstWaterPatch.Water = true;

                var waterDirection = Vector2.Normalize(firstWaterPatch.Center - Center);

                var toCheck = new List <Patch> {
                    firstWaterPatch
                };
                while (toCheck.Any())
                {
                    var checking = toCheck[0];
                    toCheck.RemoveAt(0);

                    var waterPatches = checking.GetAllNeighbours().Except(patchesInTown)
                                       .Where(n => Math.Abs((Center - n.Center).AngleComparedTo(waterDirection)) < Math.PI / 4)
                                       .Where(n => !n.Water).ToList();
                    foreach (var waterPatch in waterPatches)
                    {
                        waterPatch.Water = true;
                        toCheck.Add(waterPatch);
                    }
                }
            }

            patchesInTown = Patches.Where(p => !p.Water).OrderBy(p => (Center - p.Center).Length).Take(NumPatches)
                            .ToList();

            foreach (var patch in patchesInTown)
            {
                patch.WithinCity  = true;
                patch.WithinWalls = true;
            }

            Castle = new Castle(patchesInTown.Last());

            Market = patchesInTown.First();

            var circumference = FindCircumference(patchesInTown);
            var smoothAmount  = Math.Min(1f, 40f / circumference.Vertices.Count);

            if (Options.Walls)
            {
                SmoothPatches(circumference, smoothAmount);
            }

            if (Options.Water)
            {
                var waterCircumference = FindCircumference(Patches.Where(p => p.Water));
                SmoothPatches(waterCircumference, 0.2f);
                WaterBorder.Clear();
                WaterBorder.AddRange(waterCircumference.Vertices);
            }
        }