Exemple #1
0
        private void Erode(int width, int height, float seaLevel, Overworld.MapData[,] heightMap, int numRains, int rainLength, int numRainSamples, float[,] buffer)
        {
            float remaining = 1.0f - Progress.Value - 0.2f;
            float orig      = Progress.Value;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    buffer[x, y] = heightMap[x, y].Height;
                }
            }

            for (int i = 0; i < numRains; i++)
            {
                LoadingMessage = "Erosion " + i + "/" + numRains;
                Progress.Value = orig + remaining * ((float)i / (float)numRains);
                Vector2 currentPos = new Vector2(0, 0);
                Vector2 bestPos    = currentPos;
                float   bestHeight = 0.0f;
                for (int k = 0; k < numRainSamples; k++)
                {
                    int randX = PlayState.Random.Next(1, width - 1);
                    int randY = PlayState.Random.Next(1, height - 1);

                    currentPos = new Vector2(randX, randY);
                    float h = Overworld.GetHeight(buffer, currentPos);

                    if (h > bestHeight)
                    {
                        bestHeight = h;
                        bestPos    = currentPos;
                    }
                }

                currentPos = bestPos;

                const float erosionRate = 0.99f;
                Vector2     velocity    = Vector2.Zero;
                for (int j = 0; j < rainLength; j++)
                {
                    Vector2 g = Overworld.GetMinNeighbor(buffer, currentPos);

                    float h = Overworld.GetHeight(buffer, currentPos);

                    if (h < seaLevel || g.LengthSquared() < 1e-12)
                    {
                        break;
                    }

                    Overworld.MinBlend(Overworld.Map, currentPos, erosionRate * Overworld.GetValue(Overworld.Map, currentPos, Overworld.ScalarFieldType.Erosion), Overworld.ScalarFieldType.Erosion);

                    velocity    = 0.1f * g + 0.7f * velocity + 0.2f * MathFunctions.RandVector2Circle();
                    currentPos += velocity;
                }
            }
        }