Ejemplo n.º 1
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();
        }
        public override void Build(IMap map, MapSettings settings)
        {
            Comparison<Corner> comparison = (a, b) => -a.Elevation.CompareTo(b.Elevation);
            var queue = new PriorityQueue<Corner>(comparison);

            foreach (var corner in map.Corners)
            {
                if (!corner.IsOcean || corner.IsOceanCoast)
                {
                    if (corner.IsOceanCoast)
                    {
                        corner.Elevation = 0;
                        queue.Enqueue(corner);
                    }
                    else
                    {
                        corner.Elevation = 1e10;
                    }
                }
                else
                {
                    corner.Elevation = 0;
                }
            }

            /*while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                foreach (var corner in current.Corners)
                {
                    // TODO: check why can corner be null
                    if (corner != null && corner.IsLand && corner.Elevation < 0)
                    {
                        corner.Elevation = current.Elevation + Geometry.Dist(current, corner) ;
                        queue.Enqueue(corner);
                    }
                }
            }*/

            var noise = new NormNoiseDecorator(m_NoiseGenerator).GenerateNoise((int)map.Width, (int)map.Height, m_Random.Next(), 4);

            /*foreach (var corner in map.Corners)
            {
                if (corner.IsLand)
                {
                    corner.Elevation = (corner.Elevation + 1) * noise[(int) corner.X, (int) corner.Y];
                }
            }

            NormalizeElevation(map);*/

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                foreach (var corner in current.Corners.Where(map.ContainsPointInside))
                {
                    var midPoint = (current + corner) / 2;
                    var newDist = current.Elevation + Geometry.Dist(current, corner) * noise[(int)midPoint.X, (int)midPoint.Y];
                    if (corner.Elevation > newDist)
                    {
                        corner.Elevation = newDist;
                        queue.Enqueue(corner);

                        if (corner.IsLake)
                        {
                            double bigStep = 2 * map.Corners.Count * this.SmallStep;

                            var lakeQueue = new Queue<Corner>();
                            lakeQueue.Enqueue(corner);
                            while (lakeQueue.Count > 0)
                            {
                                var lakeCorner = lakeQueue.Dequeue();
                                foreach (var c in lakeCorner.Corners)
                                {
                                    if (c.IsLake && c.Elevation > lakeCorner.Elevation + bigStep)
                                    {
                                        c.Elevation = lakeCorner.Elevation + this.SmallStep;
                                        lakeQueue.Enqueue(c);
                                        queue.Enqueue(c);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            this.NormalizeCornerElevation(map);

            //TODO: obsolete?
            /*foreach (var polygon in map.Polygons)
            {
                polygon.Elevation = polygon.Corners.Average(p => p.Elevation);
                //polygon.Elevation = polygon.IsLand ? polygon.Corners.Average(p => p.Elevation) : 0;
            } */
        }