public Bitmap DrawMap(IMap map, DrawSettings settings)
        {
            int width = (int)map.Width;
            int height = (int)map.Height;
            var bitmap = new Bitmap(width, height);

            Graphics graphics = Graphics.FromImage(bitmap);
            Pen blackPen = new Pen(Color.Black);
            Pen redPen = new Pen(Color.Red);
            Brush whiteBrush = new SolidBrush(Color.White);

            Brush oceanBrush = new SolidBrush(m_Palette.OceanColor);
            Brush lakeBrush = new SolidBrush(m_Palette.LakeColor);

            Pen riverPen = new Pen(m_Palette.RiverColor);

            Brush landBrush = new SolidBrush(Color.BurlyWood);
            Brush coastBrush = new SolidBrush(Color.Bisque);

            graphics.FillRectangle(whiteBrush, 0, 0, width, height);

            var maxDistFromWater = 1; // map.Polygons.Max(p => p.DistanceForMoisture);

            if (settings.DisplayPolygons)
            {
                foreach (var polygon in map.Polygons.Where(p => p.Corners.Count > 2))
                {
                    if (polygon.IsLand)
                    {
                        if (polygon.IsOceanCoast && settings.DisplayCoast)
                        {
                            graphics.FillPolygon(coastBrush, polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                            //graphics.FillPolygon(coastBrush, polygon.CornersToDraw.Select(p => (PointF)p).Reverse().ToArray());
                        }
                        else
                        {
                            if (settings.DisplayElevation)
                            {
                                var lowLandColor = new MyColor(Color.Green);
                                var highLandColor = new MyColor(Color.Red);

                                Color color = (Color)(polygon.Elevation / maxDistFromWater * highLandColor +
                                                      (1 - polygon.Elevation / maxDistFromWater) * lowLandColor);

                                graphics.FillPolygon(new SolidBrush(color), polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                            }
                            else
                            {
                                if (settings.DisplayMoisture)
                                {

                                    var dryLandColor = new MyColor(Color.White);
                                    var wetLandColor = new MyColor(Color.Blue);

                                    Color color = (Color)(polygon.Moisture * wetLandColor + (1 - polygon.Moisture) * dryLandColor);

                                    graphics.FillPolygon(new SolidBrush(color), polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                                }
                                else
                                {
                                    var brush = new SolidBrush(m_Palette.GetPolygonColor(polygon));
                                    graphics.FillPolygon(
                                        brush, polygon.CornersToDraw.Select(p => (PointF)p).Reverse().ToArray());
                                    //graphics.FillPolygon(landBrush, polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                                }
                            }
                        }
                    }
                    else
                    {
                        if (polygon.IsOcean)
                        {
                            if (settings.DisplayElevation)
                            {
                                var shallowWaterColor = new MyColor(Color.LightBlue);
                                var deepWaterLandColor = new MyColor(Color.DarkBlue);

                                Color color = (Color)(-polygon.Elevation / maxDistFromWater * deepWaterLandColor +
                                                      (1 + polygon.Elevation / maxDistFromWater) * shallowWaterColor);

                                graphics.FillPolygon(new SolidBrush(color), polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                            }
                            else
                            {
                                var newOceanBrush = new SolidBrush(m_Palette.GetPolygonColor(polygon));
                                graphics.FillPolygon(newOceanBrush, polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                            }
                        }
                        if (polygon.IsLake)
                        {
                            if (settings.DisplayLakes)
                            {
                                graphics.FillPolygon(lakeBrush, polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                            }
                            else
                            {
                                if (settings.DisplayElevation)
                                {
                                    var lowLandColor = new MyColor(Color.Green);
                                    var highLandColor = new MyColor(Color.Red);

                                    Color color =
                                        (Color)
                                        (polygon.Elevation / maxDistFromWater * highLandColor
                                         + (1 - polygon.Elevation / maxDistFromWater) * lowLandColor);

                                    graphics.FillPolygon(
                                        new SolidBrush(color), polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                                }
                            }
                        }
                    }

                    /*graphics.FillEllipse(new SolidBrush(Color.Red), (float) polygon.Center.X, (float) polygon.Center.Y, 5, 5);

                    for (int i = 0; i < polygon.Borders.Count - 2; i++)
                    {
                        graphics.DrawLine(new Pen(Color.Green), (PointF) polygon.Borders[i].Center, (PointF) polygon.Borders[i + 1].Center);
                    }*/
                }
            }

            if (settings.DisplayRivers)
            {
                foreach (var border in map.Borders)
                {
                    if (border.RiverCapacity > 0 && (!border.IsLake || !settings.DisplayLakes))
                    {
                        var riverPenWidth = (float)Math.Sqrt(30 * border.RiverCapacity / Math.Sqrt(map.Polygons.Count));
                        riverPen.Width = riverPenWidth;
                        graphics.DrawLines(riverPen, border.BorderToDraw.Select(p => (PointF)p).ToArray());
                    }
                }
            }

            if (settings.DisplayLinealBorders)
            {
                foreach (var polygon in map.Polygons.Where(p => p.Corners.Count > 2))
                {
                    graphics.DrawPolygon(blackPen, polygon.Corners.Select(p => (PointF)p).ToArray());
                }
            }

            if (settings.DisplayNoiseBorders)
            {
                foreach (var polygon in map.Polygons.Where(p => p.Corners.Count > 2))
                {
                    graphics.DrawPolygon(new Pen(Color.Black), polygon.CornersToDraw.Select(p => (PointF)p).ToArray());
                }
            }

            if (settings.ApplyNoise)
            {
                //var noise = this.m_PerlinNoiseGenerator.GenerateNoise(width, height, this.m_Random.Next(), Math.Max(width, height));
                var noise = this.m_PerlinNoiseGenerator.GenerateNoise(width, height, this.m_Random.Next(), 100);

                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        bitmap.SetPixel(i, j, Color.FromArgb((int)(BaseAlfa + (256 - BaseAlfa) * noise[i, j]), bitmap.GetPixel(i, j)));
                    }
                }
            }

            if (settings.DisplayCoastline)
            {
                foreach (var border in map.Borders)
                {
                    if (border.IsCoast)
                    {
                        Pen pen;
                        if (border.IsOceanCoast)
                        {
                            pen = new Pen(Color.Brown, 3);
                        }
                        else
                        {
                            pen = new Pen(Color.RoyalBlue, 2);
                        }

                        for (int i = 0; i < border.BorderToDraw.Count - 1; i++)
                        {
                            graphics.DrawLine(pen, (PointF)border.BorderToDraw[i], (PointF)border.BorderToDraw[i + 1]);
                        }
                    }
                }
            }

            /*foreach (var polygon in map.Polygons)
            {
                graphics.DrawString(polygon.DistanceFromEdge.ToString(CultureInfo.InvariantCulture), new Font("Thaoma", 10), Brushes.Black, (PointF)polygon.Center);
            }*/

            /*if (this.checkBoxShowTimes.Checked)
            {
                graphics.DrawString(
                    this.m_Structure.LastActionLength.ToString(CultureInfo.InvariantCulture),
                    new Font("Thaoma", 80),
                    Brushes.Black,
                    10,
                    10);

                graphics.DrawString(
                    this.m_Structure.LandCreation.ToString(CultureInfo.InvariantCulture),
                    new Font("Thaoma", 80),
                    Brushes.Black,
                    10,
                    100);

                graphics.DrawString(
                    this.m_Structure.MapBuilding.ToString(CultureInfo.InvariantCulture),
                    new Font("Thaoma", 80),
                    Brushes.Black,
                    10,
                    190);
            }*/
            /*if (checkBoxPoints.Checked)
            {
                int r = 2;
                foreach (var corner in map.Corners)
                {
                    graphics.DrawEllipse(blackPen, (float) corner.X - r, (float) corner.Y - r, 2 * r, 2 * r);
                }
            }

            if (checkBoxBorders.Checked)
            {
                foreach (var edge in map.Borders)
                {
                    graphics.DrawLine(blackPen, (float) edge.Corners[0].X, (float) edge.Corners[0].Y,
                                      (float) edge.Corners[1].X,
                                      (float) edge.Corners[1].Y);
                }
            }

            if (checkBoxTriangles.Checked)
            {
                foreach (var edge in map.Borders)
                {

                    graphics.DrawLine(redPen, (float) edge.Polygons[0].BasePoint.X,
                                      (float) edge.Polygons[0].BasePoint.Y,
                                      (float) edge.Polygons[1].BasePoint.X,
                                      (float) edge.Polygons[1].BasePoint.Y);
                }
            }*/

            /*int r = 2;

            if (checkBoxPoints.Checked)
            {
                foreach (var point in m_Structure.Points)
                {
                    graphics.DrawEllipse(blackPen, (float) point.X - r, (float) point.Y - r, 2 * r, 2 * r);
                }
            }*/

            /*foreach (var triangle in m_Structure.Triangles)
            {
                if (checkBoxTriangles.Checked)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Edge edge = triangle.Edge(i);
                        graphics.DrawLine(blackPen, (float) edge.First.X, (float) edge.First.Y, (float) edge.Second.X,
                                          (float) edge.Second.Y);
                    }
                }

                if (checkBoxBorders.Checked)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (triangle.Triangles[i] != null)
                        {
                            Point2D a = triangle.Center;
                            Point2D b = triangle.Triangles[i].Center;

                            graphics.DrawLine(redPen, (float) a.X, (float) a.Y, (float) b.X, (float) b.Y);
                        }
                    }
                }
            }*/
            return bitmap;
        }
Exemple #2
0
        private async Task CreateTestNoise()
        {
            double start;
            int count;
            if (int.TryParse(textBoxSeed.Text, out m_Seed) && double.TryParse(textBoxStart.Text, out start) && int.TryParse(textBoxCount.Text, out count))
            {
                //INoiseGenerator generator = new PerlinNoiseGeneratorOld();
                INoiseGenerator generator = new PerlinNoiseGenerator(new StandardComponentGenerator(), new MersenneTwisterRandomGeneratorFactory());

                if (cbNormalize.Checked)
                {
                    generator = new NormNoiseDecorator(generator);
                }

                if (cbPolarize.Checked)
                {
                    generator = new SlowedHeightIncreaseNoiseDecorator(generator);
                }

                var noise = await Task.Run(() => generator.GenerateNoise(m_Width, m_Height, m_Seed, start, count));

                var values = new List<double>();
                foreach (var value in noise)
                {
                    values.Add(value);
                }
                values.Sort();

                for (int i = 0; i < values.Count - 1; i++)
                {
                    if (values[i + 1] - values[i] > 0.001)
                    {
                    }
                }

                this.m_Bitmap = new Bitmap(m_Width, m_Height);
                for (int i = 0; i < m_Width; i++)
                {
                    for (int j = 0; j < m_Height; j++)
                    {
                        //var botColor = new MyColor(Color.FromArgb(255, 0, 255, 0));
                        var botColor = new MyColor(Color.White);
                        var topColor = new MyColor(Color.Black);
                        MyColor color = topColor * noise[i, j] + botColor * (1 - noise[i, j]);
                        /*MyColor color;// = topColor * noise[i, j] + botColor * (1 - noise[i, j]);
                        if (noise[i, j] > 0.65)
                        {
                            color = new MyColor(Color.Wheat);
                        }
                        else
                        {
                            color = new MyColor(Color.White);
                        }*/
                        this.m_Bitmap.SetPixel(i, j, (Color)color);
                    }
                }
            }

            this.Refresh();
        }