Exemple #1
0
        public static void Generate()
        {
            //while (true)
            //    Test.Show(RenderNoiseBmp(500, 500));
            while (true)
            {
                int seed = Environment.TickCount;
                seed = new Random().Next(8, 16777216); // 16777216 = 8^8
                DateTime asdf = DateTime.Now;
                seed = asdf.Millisecond + asdf.Minute + asdf.Hour + asdf.Year;
                Random     rand = new Random(seed);
                PolygonMap map  = new PolygonMap(rand.Next());

                Console.Out.WriteLine("Generating map...");
                map.Generate(Size * 15);

                Console.Out.WriteLine("Creating terrain...");
                RealmTile[,] dat = CreateTerrain(rand.Next(), map);

                Console.Out.WriteLine("Computing biomes...");
                new Biome(rand.Next(), map).ComputeBiomes(dat);


                new RealmDisplay(dat).ShowDialog();
                //Test.Show(RenderMoistBmp(dat));
                //Test.Show(RenderEvalBmp(dat));

                map = null;
                dat = null;
                GC.WaitForFullGCComplete(-1);
                GC.Collect();
            }
        }
Exemple #2
0
 public MapFeatures(PolygonMap map, int seed)
 {
     this.map = map;
     rand     = new Random(seed);
 }
Exemple #3
0
        private static RealmTile[,] CreateTerrain(int seed, PolygonMap map)
        {
            Rasterizer <RealmTile> rasterizer = new Rasterizer <RealmTile>(Size, Size);

            //Set all to ocean
            rasterizer.Clear(new RealmTile
            {
                PolygonId = -1,
                Elevation = 0,
                Moisture  = 1,
                TileId    = RealmTileTypes.DeepWater,
                TileObj   = null
            });
            //Render lands poly
            foreach (MapPolygon poly in map.Polygons.Where(_ => !_.IsWater))
            {
                uint color = 0x00ffffff;
                color |= (uint)(poly.DistanceToCoast * 255) << 24;
                rasterizer.FillPolygon(
                    poly.Nodes.SelectMany(_ =>
                {
                    return(new[]
                    {
                        (_.X + 1) / 2 * Size,
                        (_.Y + 1) / 2 * Size
                    });
                }).Concat(new[]
                {
                    (poly.Nodes[0].X + 1) / 2 * Size,
                    (poly.Nodes[0].Y + 1) / 2 * Size
                }).ToArray(),
                    new RealmTile
                {
                    PolygonId = poly.Id,
                    Elevation = (float)poly.DistanceToCoast,
                    Moisture  = -1,
                    TileId    = RealmTileTypes.Grass,
                    TileObj   = null
                });
            }
            MapFeatures fea = new MapFeatures(map, seed);
            //Render roads
            IEnumerable <Coordinate[]> roads = fea.GenerateRoads();

            foreach (Coordinate[] i in roads)
            {
                rasterizer.DrawClosedCurve(i.SelectMany(_ => new[]
                {
                    (_.X + 1) / 2 * Size, (_.Y + 1) / 2 * Size
                }).ToArray(),
                                           1, t =>
                {
                    t.TileId = RealmTileTypes.Road;
                    return(t);
                }, 3);
            }
            //Render waters poly
            foreach (MapPolygon poly in map.Polygons.Where(_ => _.IsWater))
            {
                RealmTile tile = new RealmTile
                {
                    PolygonId = poly.Id,
                    Elevation = (float)poly.DistanceToCoast,
                    TileObj   = null
                };
                if (poly.IsCoast)
                {
                    tile.TileId   = RealmTileTypes.MovingWater;
                    tile.Moisture = 0;
                }
                else
                {
                    tile.TileId   = RealmTileTypes.DeepWater;
                    tile.Moisture = 1;
                }
                rasterizer.FillPolygon(
                    poly.Nodes.SelectMany(_ =>
                {
                    return(new[]
                    {
                        (_.X + 1) / 2 * Size,
                        (_.Y + 1) / 2 * Size
                    });
                }).Concat(new[]
                {
                    (poly.Nodes[0].X + 1) / 2 * Size,
                    (poly.Nodes[0].Y + 1) / 2 * Size
                }).ToArray(), tile);
            }
            //Render rivers
            IEnumerable <MapNode[]> rivers = fea.GenerateRivers();
            Dictionary <Tuple <MapNode, MapNode>, int> edges = new Dictionary <Tuple <MapNode, MapNode>, int>();

            foreach (MapNode[] i in rivers)
            {
                for (int j = 1; j < i.Length; j++)
                {
                    Tuple <MapNode, MapNode> edge = new Tuple <MapNode, MapNode>(i[j - 1], i[j]);
                    if (edges.TryGetValue(edge, out int count))
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    edges[edge] = count;
                }
            }
            foreach (KeyValuePair <Tuple <MapNode, MapNode>, int> i in edges)
            {
                i.Key.Item1.IsWater    = true;
                i.Key.Item1.RiverValue = i.Value + 1;
                i.Key.Item2.IsWater    = true;
                i.Key.Item2.RiverValue = i.Value + 1;
                rasterizer.DrawLineBresenham(
                    (i.Key.Item1.X + 1) / 2 * Size, (i.Key.Item1.Y + 1) / 2 * Size,
                    (i.Key.Item2.X + 1) / 2 * Size, (i.Key.Item2.Y + 1) / 2 * Size,
                    t =>
                {
                    t.TileId    = RealmTileTypes.Water;
                    t.Elevation = (float)(i.Key.Item1.DistanceToCoast + i.Key.Item2.DistanceToCoast) / 2;
                    t.Moisture  = 1;
                    return(t);
                }, 3 * Math.Min(2, i.Value));
            }

            return(rasterizer.Buffer);
        }
Exemple #4
0
 public Biome(int seed, PolygonMap map)
 {
     rand     = new Random(seed);
     this.map = map;
 }