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); }
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; }