Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            Console.WriteLine("Generating heightmap");
            var heightmap = new Heightmap2(new Size(128, 128));
            heightmap.Run();

            //            DrawHeightmap(heightmap);

            var basePath = DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss");
            Directory.CreateDirectory(basePath);

            int seed;

            if (args.Length == 0)
            {
                var seedRandom = new Random();
                seed = seedRandom.Next();
            }
            else
            {
                seed = int.Parse(args[0]);
            }

            var settings = new VoronoiSettings
            {
                Seed = seed,
                SliceSize = 256,
                SlicesWidth = 16,
                SlicesHeight = 8,
                SiteCount = 2000
            };

            var jsonSerializer = JsonSerializer.CreateDefault();
            using (var writer = new StreamWriter(new FileStream(Path.Combine(basePath, "settings.json"), FileMode.CreateNew)))
            {
                jsonSerializer.Serialize(writer, settings);
                writer.Flush();
            }

            var rnd = new Random(settings.Seed);
            var points = new HashSet<Point>();

            Console.WriteLine("Generating random points");
            LoadPoints(rnd, settings, points);
            using (var writer = new StreamWriter(new FileStream(Path.Combine(basePath, "points.json"), FileMode.CreateNew)))
            {
                jsonSerializer.Serialize(writer, points);
                writer.Flush();
            }

            var mapHelper = new MapHelper();
            var centerFinder = new CenterFinder(mapHelper);
            var faultyEdgeFixer = new FaultyEdgeFixer(mapHelper);
            var mapExtender = new MapExtender();

            Console.WriteLine("Duplicating points for wraparound map");
            var pointsForWrapAroundMap = mapExtender.MakePointsForWrapAroundMap(points, settings.Width);

            var voronoi = new Voronoi();
            Console.WriteLine("Computing wraparound voronoi");
            voronoi.Compute(pointsForWrapAroundMap, settings.Width * 2, settings.Height);
            //            DrawWrapAroundMap(voronoi, pointsForWrapAroundMap, "VoronoiWrapAround.png", width * 2, height);

            // do y times:
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Fixing faulty edges");
                faultyEdgeFixer.FixFaultyEdges(voronoi, settings);

                Console.WriteLine("Looking for center of cells");
                var centerOfRealCells = centerFinder.FindAllCenters(voronoi, settings);

                Console.WriteLine("Setting new start point");
                points = new HashSet<Point>(centerOfRealCells);

                // create new wraparound for these points
                Console.WriteLine("Duplicating points for wraparound map");
                pointsForWrapAroundMap = mapExtender.MakePointsForWrapAroundMap(points, settings.Width);

                // generate voronoi
                Console.WriteLine("Computing wraparound voronoi");
                voronoi.Compute(pointsForWrapAroundMap, settings.Width * 2, settings.Height);
            //                DrawWrapAroundMap(voronoi, pointsForWrapAroundMap, string.Format("VoronoiWrapAround{0}.png", i + 1), width * 2, height);
            }

            Console.WriteLine("Fixing faulty edges");
            faultyEdgeFixer.FixFaultyEdges(voronoi, settings);

            Console.WriteLine("Looking for final cells");
            var finalCells = voronoi.Cells
                // filter the "real" cells
                .Where(c => c.Site.RealPoint).ToList();
            var ratioX = (settings.Width * 2) / 128d;
            var ratioY = settings.Height / 128d;

            Console.WriteLine("Applying heightmap to final cells");
            foreach (var finalCell in finalCells)
            {
                if (!finalCell.Vertices.Any())
                {
                    continue;
                }

                if (finalCell.Vertices.Any(p => p.Y <= 0d || p.Y >= settings.Height))
                {
                    finalCell.Height = -1;
                    continue;
                }

                finalCell.Height = heightmap.GetCell((int) (finalCell.Site.X / ratioX) - 1, (int) (finalCell.Site.Y / ratioY) - 1);
            }

            Console.WriteLine("Generating heightmap image");
            var filename = Path.Combine(basePath, string.Format("VoronoiHeightmap_{0}.png", DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")));
            DrawHeightmap2(voronoi, finalCells, filename, settings.Width * 2, settings.Height);
        }
Exemplo n.º 2
0
 public CenterFinder(MapHelper mapHelper)
 {
     _mapHelper = mapHelper;
 }
Exemplo n.º 3
0
 public FaultyEdgeFixer(MapHelper mapHelper)
 {
     _mapHelper = mapHelper;
 }