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 = System.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..."); var dat = CreateTerrain(rand.Next(), map); Console.Out.WriteLine("Computing biomes..."); new Biome(rand.Next(), map).ComputeBiomes(dat); new TerrainDisplay(dat).ShowDialog(); //Test.Show(RenderMoistBmp(dat)); //Test.Show(RenderEvalBmp(dat)); map = null; dat = null; GC.WaitForFullGCComplete(-1); GC.Collect(); } }
public const int Size = 1024; //Size of world maps #endregion Fields #region Methods 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 = System.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..."); var dat = CreateTerrain(rand.Next(), map); Console.Out.WriteLine("Computing biomes..."); new Biome(rand.Next(), map).ComputeBiomes(dat); new TerrainDisplay(dat).ShowDialog(); //Test.Show(RenderMoistBmp(dat)); //Test.Show(RenderEvalBmp(dat)); map = null; dat = null; GC.WaitForFullGCComplete(-1); GC.Collect(); } }
public Biome(int seed, PolygonMap map) { rand = new Random(seed); this.map = map; }
static TerrainTile[,] CreateTerrain(int seed, PolygonMap map) { Rasterizer <TerrainTile> rasterizer = new Rasterizer <TerrainTile>(Size, Size); //Set all to ocean rasterizer.Clear(new TerrainTile() { PolygonId = -1, Elevation = 0, Moisture = 1, TileId = TileTypes.DeepWater, TileObj = null }); //Render lands poly foreach (var 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 TerrainTile() { PolygonId = poly.Id, Elevation = (float)poly.DistanceToCoast, Moisture = -1, TileId = TileTypes.Grass, TileObj = null }); } MapFeatures fea = new MapFeatures(map, seed); //Render roads var roads = fea.GenerateRoads(); foreach (var i in roads) { rasterizer.DrawClosedCurve(i.SelectMany(_ => new[] { (_.X + 1) / 2 * Size, (_.Y + 1) / 2 * Size }).ToArray(), 1, t => { t.TileId = TileTypes.Road; return(t); }, 3); } //Render waters poly foreach (var poly in map.Polygons.Where(_ => _.IsWater)) { var tile = new TerrainTile() { PolygonId = poly.Id, Elevation = (float)poly.DistanceToCoast, TileObj = null }; if (poly.IsCoast) { tile.TileId = TileTypes.MovingWater; tile.Moisture = 0; } else { tile.TileId = TileTypes.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 var rivers = fea.GenerateRivers(); Dictionary <Tuple <MapNode, MapNode>, int> edges = new Dictionary <Tuple <MapNode, MapNode>, int>(); foreach (var i in rivers) { for (int j = 1; j < i.Length; j++) { Tuple <MapNode, MapNode> edge = new Tuple <MapNode, MapNode>(i[j - 1], i[j]); int count; if (edges.TryGetValue(edge, out count)) { count++; } else { count = 1; } edges[edge] = count; } } foreach (var 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 = TileTypes.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); }
public MapFeatures(PolygonMap map, int seed) { this.map = map; rand = new Random(seed); }
public MapFeatures(PolygonMap map, int seed) { this.map = map; rand = new Random(seed); }
public Biome(int seed, PolygonMap map) { rand = new Random(seed); this.map = map; }
static TerrainTile[,] CreateTerrain(int seed, PolygonMap map) { Rasterizer<TerrainTile> rasterizer = new Rasterizer<TerrainTile>(Size, Size); //Set all to ocean rasterizer.Clear(new TerrainTile() { PolygonId = -1, Elevation = 0, Moisture = 1, TileId = TileTypes.DeepWater, TileObj = null }); //Render lands poly foreach (var 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 TerrainTile() { PolygonId = poly.Id, Elevation = (float)poly.DistanceToCoast, Moisture = -1, TileId = TileTypes.Grass, TileObj = null }); } MapFeatures fea = new MapFeatures(map, seed); //Render roads var roads = fea.GenerateRoads(); foreach (var i in roads) { rasterizer.DrawClosedCurve(i.SelectMany(_ => new[] { (_.X + 1) / 2 * Size, (_.Y + 1) / 2 * Size }).ToArray(), 1, t => { t.TileId = TileTypes.Road; return t; }, 3); } //Render waters poly foreach (var poly in map.Polygons.Where(_ => _.IsWater)) { var tile = new TerrainTile() { PolygonId = poly.Id, Elevation = (float)poly.DistanceToCoast, TileObj = null }; if (poly.IsCoast) { tile.TileId = TileTypes.MovingWater; tile.Moisture = 0; } else { tile.TileId = TileTypes.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 var rivers = fea.GenerateRivers(); Dictionary<Tuple<MapNode, MapNode>, int> edges = new Dictionary<Tuple<MapNode, MapNode>, int>(); foreach (var i in rivers) { for (int j = 1; j < i.Length; j++) { Tuple<MapNode, MapNode> edge = new Tuple<MapNode, MapNode>(i[j - 1], i[j]); int count; if (edges.TryGetValue(edge, out count)) count++; else count = 1; edges[edge] = count; } } foreach (var 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 = TileTypes.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; }