public void CalculateRain(int width, int height)
        {
            for (int y = 0; y < height; y++)
            {
                float currentMoisture = Settings.RainfallScale * 10;
                for (int x = 0; x < width; x++)
                {
                    float h       = Overworld.Map[x, y].Height;
                    bool  isWater = h < Settings.SeaLevel;

                    if (isWater)
                    {
                        currentMoisture += MathFunctions.Rand(0.1f, 0.3f);
                        currentMoisture  = Math.Min(currentMoisture, Settings.RainfallScale * 20);
                        Overworld.Map[x, y].Rainfall = 0.5f;
                    }
                    else
                    {
                        float rainAmount = currentMoisture * 0.017f * h + currentMoisture * 0.0006f;
                        currentMoisture -= rainAmount;
                        float evapAmount = MathFunctions.Rand(0.01f, 0.02f);
                        currentMoisture += evapAmount;
                        Overworld.Map[x, y].Rainfall = rainAmount * Settings.RainfallScale * Settings.Width * 0.015f;
                    }
                }
            }

            Overworld.Distort(width, height, 5.0f, 0.03f, Overworld.ScalarFieldType.Rainfall);
        }
        private void Voronoi(int width, int height, int numVoronoiPoints)
        {
            List <List <Vector2> > vPoints = new List <List <Vector2> >();
            List <float>           rands   = new List <float>();

            /*
             * List<Vector2> edge = new List<Vector2>
             * {
             *  new Vector2(0, 0),
             *  new Vector2(width, 0),
             *  new Vector2(width, height),
             *  new Vector2(0, height),
             *  new Vector2(0, 0)
             * };
             *
             * List<Vector2> randEdge = new List<Vector2>();
             * for (int i = 1; i < edge.Count; i++)
             * {
             *  if (MathFunctions.RandEvent(0.5f))
             *  {
             *      randEdge.Add(edge[i]);
             *      randEdge.Add(edge[i - 1]);
             *  }
             * }
             *
             * vPoints.Add(randEdge);
             */
            for (int i = 0; i < numVoronoiPoints; i++)
            {
                Vector2 v = GetEdgePoint(width, height);

                for (int j = 0; j < 4; j++)
                {
                    List <Vector2> line = new List <Vector2>();
                    rands.Add(1.0f);

                    line.Add(v);
                    v += new Vector2(MathFunctions.Rand() - 0.5f, MathFunctions.Rand() - 0.5f) * Settings.Width * 0.5f;
                    line.Add(v);
                    vPoints.Add(line);
                }
            }


            List <VoronoiNode> nodes = new List <VoronoiNode>();

            foreach (List <Vector2> pts in vPoints)
            {
                for (int j = 0; j < pts.Count - 1; j++)
                {
                    VoronoiNode node = new VoronoiNode
                    {
                        pointA = pts[j],
                        pointB = pts[j + 1]
                    };
                    nodes.Add(node);
                }
            }

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Overworld.Map[x, y].Faults = GetVoronoiValue(nodes, x, y);
                }
            }

            ScaleMap(Overworld.Map, width, height, Overworld.ScalarFieldType.Faults);
            Overworld.Distort(width, height, 20, 0.01f, Overworld.ScalarFieldType.Faults);
        }
        public void GenerateWorld(int seed, int width, int height)
        {
#if CREATE_CRASH_LOGS
            try
#endif
            {
                Seed = seed;
                MathFunctions.Random       = new ThreadSafeRandom(Seed);
                Overworld.heightNoise.Seed = seed;
                CurrentState = GenerationState.Generating;

                if (Overworld.Name == null)
                {
                    Overworld.Name = WorldGenerationSettings.GetRandomWorldName();
                }

                LoadingMessage             = "Init..";
                Overworld.heightNoise.Seed = Seed;
                worldData     = new Color[width * height];
                Overworld.Map = new Overworld.MapData[width, height];

                Progress = 0.01f;

                LoadingMessage           = "Height Map ...";
                float[,] heightMapLookup = null;
                heightMapLookup          = Overworld.GenerateHeightMapLookup(width, height);
                Overworld.GenerateHeightMapFromLookup(heightMapLookup, width, height, 1.0f, false);

                Progress = 0.05f;

                int numRains       = (int)Settings.NumRains;
                int rainLength     = 250;
                int numRainSamples = 3;

                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Erosion    = 1.0f;
                        Overworld.Map[x, y].Weathering = 0;
                        Overworld.Map[x, y].Faults     = 1.0f;
                    }
                }

                LoadingMessage = "Climate";
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Temperature = ((float)(y) / (float)(height)) * Settings.TemperatureScale;
                        //Overworld.Map[x, y].Rainfall = Math.Max(Math.Min(Overworld.noise(x, y, 1000.0f, 0.01f) + Overworld.noise(x, y, 100.0f, 0.1f) * 0.05f, 1.0f), 0.0f) * RainfallScale;
                    }
                }

                //Overworld.Distort(width, height, 60.0f, 0.005f, Overworld.ScalarFieldType.Rainfall);
                Overworld.Distort(width, height, 30.0f, 0.005f, Overworld.ScalarFieldType.Temperature);
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Temperature = Math.Max(Math.Min(Overworld.Map[x, y].Temperature, 1.0f), 0.0f);
                    }
                }

                int numVoronoiPoints = (int)Settings.NumFaults;

                if (UpdatePreview != null)
                {
                    UpdatePreview();
                }

                Progress       = 0.1f;
                LoadingMessage = "Faults ...";

                #region voronoi

                Voronoi(width, height, numVoronoiPoints);

                #endregion

                Overworld.GenerateHeightMapFromLookup(heightMapLookup, width, height, 1.0f, true);

                Progress = 0.2f;

                Overworld.GenerateHeightMapFromLookup(heightMapLookup, width, height, 1.0f, true);

                Progress = 0.25f;
                if (UpdatePreview != null)
                {
                    UpdatePreview();
                }
                LoadingMessage = "Erosion...";

                #region erosion

                float[,] buffer = new float[width, height];
                Erode(width, height, Settings.SeaLevel, Overworld.Map, numRains, rainLength, numRainSamples, buffer);
                Overworld.GenerateHeightMapFromLookup(heightMapLookup, width, height, 1.0f, true);

                #endregion

                Progress = 0.9f;


                LoadingMessage = "Blur.";
                Overworld.Blur(Overworld.Map, width, height, Overworld.ScalarFieldType.Erosion);

                LoadingMessage = "Generate height.";
                Overworld.GenerateHeightMapFromLookup(heightMapLookup, width, height, 1.0f, true);


                LoadingMessage = "Rain";
                CalculateRain(width, height);

                LoadingMessage = "Biome";
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Biome = Overworld.GetBiome(Overworld.Map[x, y].Temperature, Overworld.Map[x, y].Rainfall, Overworld.Map[x, y].Height).Biome;
                    }
                }

                LoadingMessage = "Volcanoes";

                GenerateVolcanoes(width, height);

                if (UpdatePreview != null)
                {
                    UpdatePreview();
                }


                LoadingMessage = "Factions";
                FactionLibrary library = new FactionLibrary();
                library.Initialize(null, new CompanyInformation());

                if (Settings.Natives == null || Settings.Natives.Count == 0)
                {
                    NativeCivilizations = new List <Faction>();
                    for (int i = 0; i < Settings.NumCivilizations; i++)
                    {
                        NativeCivilizations.Add(library.GenerateFaction(null, i, Settings.NumCivilizations));
                    }
                    Settings.Natives = NativeCivilizations;
                }
                else
                {
                    NativeCivilizations = Settings.Natives;

                    if (Settings.NumCivilizations > Settings.Natives.Count)
                    {
                        int count = Settings.Natives.Count;
                        for (int i = count; i < Settings.NumCivilizations; i++)
                        {
                            NativeCivilizations.Add(library.GenerateFaction(null, i, Settings.NumCivilizations));
                        }
                    }
                }
                SeedCivs(Overworld.Map, Settings.NumCivilizations, NativeCivilizations);
                GrowCivs(Overworld.Map, 200, NativeCivilizations);


                for (int x = 0; x < width; x++)
                {
                    Overworld.Map[x, 0]          = Overworld.Map[x, 1];
                    Overworld.Map[x, height - 1] = Overworld.Map[x, height - 2];
                }

                for (int y = 0; y < height; y++)
                {
                    Overworld.Map[0, y]         = Overworld.Map[1, y];
                    Overworld.Map[width - 1, y] = Overworld.Map[width - 2, y];
                }

                LoadingMessage = "Selecting Spawn Point";
                AutoSelectSpawnRegion();

                CurrentState   = GenerationState.Finished;
                LoadingMessage = "";
                Progress       = 1.0f;
            }
#if CREATE_CRASH_LOGS
            catch (Exception exception)
            {
                ProgramData.WriteExceptionLog(exception);
                throw;
            }
#endif
        }
        public void GenerateWorld(int seed, int width, int height)
        {
#if CREATE_CRASH_LOGS
            try
#endif
            {
                GUI.MouseMode = GUISkin.MousePointer.Wait;

                PlayState.Random   = new ThreadSafeRandom(Seed);
                GenerationComplete = false;

                LoadingMessage             = "Init..";
                Overworld.heightNoise.Seed = Seed;
                worldMap      = new Texture2D(Game.GraphicsDevice, width, height);
                worldData     = new Color[width * height];
                Overworld.Map = new Overworld.MapData[width, height];

                Progress.Value = 0.01f;

                LoadingMessage = "Height Map ...";
                Overworld.GenerateHeightMap(width, height, 1.0f, false);

                Progress.Value = 0.05f;

                int numRains       = (int)Settings.NumRains;
                int rainLength     = 250;
                int numRainSamples = 3;

                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Erosion    = 1.0f;
                        Overworld.Map[x, y].Weathering = 0;
                        Overworld.Map[x, y].Faults     = 1.0f;
                    }
                }

                LoadingMessage = "Climate";
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Temperature = ((float)(y) / (float)(height)) * Settings.TemperatureScale;
                        //Overworld.Map[x, y].Rainfall = Math.Max(Math.Min(Overworld.noise(x, y, 1000.0f, 0.01f) + Overworld.noise(x, y, 100.0f, 0.1f) * 0.05f, 1.0f), 0.0f) * RainfallScale;
                    }
                }

                //Overworld.Distort(width, height, 60.0f, 0.005f, Overworld.ScalarFieldType.Rainfall);
                Overworld.Distort(width, height, 30.0f, 0.005f, Overworld.ScalarFieldType.Temperature);
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Temperature = Math.Max(Math.Min(Overworld.Map[x, y].Temperature, 1.0f), 0.0f);
                    }
                }

                Overworld.TextureFromHeightMap("Height", Overworld.Map, Overworld.ScalarFieldType.Height, width, height, MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);

                int numVoronoiPoints = (int)Settings.NumFaults;


                Progress.Value = 0.1f;
                LoadingMessage = "Faults ...";

                #region voronoi

                Voronoi(width, height, numVoronoiPoints);

                #endregion

                Overworld.GenerateHeightMap(width, height, 1.0f, true);

                Progress.Value = 0.2f;

                Overworld.GenerateHeightMap(width, height, 1.0f, true);

                Progress.Value = 0.25f;
                Overworld.TextureFromHeightMap("Height", Overworld.Map, Overworld.ScalarFieldType.Height, width, height, MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                LoadingMessage = "Erosion...";

                #region erosion

                float[,] buffer = new float[width, height];
                Erode(width, height, Settings.SeaLevel, Overworld.Map, numRains, rainLength, numRainSamples, buffer);
                Overworld.GenerateHeightMap(width, height, 1.0f, true);

                #endregion

                Progress.Value = 0.9f;


                LoadingMessage = "Blur.";
                Overworld.Blur(Overworld.Map, width, height, Overworld.ScalarFieldType.Erosion);

                LoadingMessage = "Generate height.";
                Overworld.GenerateHeightMap(width, height, 1.0f, true);


                LoadingMessage = "Rain";
                CalculateRain(width, height);

                LoadingMessage = "Biome";
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Overworld.Map[x, y].Biome = Overworld.GetBiome(Overworld.Map[x, y].Temperature, Overworld.Map[x, y].Rainfall, Overworld.Map[x, y].Height);
                    }
                }

                LoadingMessage = "Volcanoes";

                GenerateVolcanoes(width, height);

                Overworld.TextureFromHeightMap("Height", Overworld.Map, Overworld.ScalarFieldType.Height, width, height, MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);

                LoadingMessage      = "Factions";
                NativeCivilizations = new List <Faction>();
                FactionLibrary library = new FactionLibrary();
                library.Initialize(null, "fake", "fake", null, Color.Blue);
                for (int i = 0; i < Settings.NumCivilizations; i++)
                {
                    NativeCivilizations.Add(library.GenerateFaction(i, Settings.NumCivilizations));
                }
                SeedCivs(Overworld.Map, Settings.NumCivilizations, NativeCivilizations);
                GrowCivs(Overworld.Map, 200, NativeCivilizations);


                for (int x = 0; x < width; x++)
                {
                    Overworld.Map[x, 0]          = Overworld.Map[x, 1];
                    Overworld.Map[x, height - 1] = Overworld.Map[x, height - 2];
                }

                for (int y = 0; y < height; y++)
                {
                    Overworld.Map[0, y]         = Overworld.Map[1, y];
                    Overworld.Map[width - 1, y] = Overworld.Map[width - 2, y];
                }

                GenerationComplete = true;

                MapPanel.Image       = new ImageFrame(worldMap, new Rectangle(0, 0, worldMap.Width, worldMap.Height));
                MapPanel.LocalBounds = new Rectangle(300, 30, worldMap.Width, worldMap.Height);


                Progress.Value = 1.0f;
                IsGenerating   = false;
                GUI.MouseMode  = GUISkin.MousePointer.Pointer;
                DoneGenerating = true;
            }
#if CREATE_CRASH_LOGS
            catch (Exception exception)
            {
                ProgramData.WriteExceptionLog(exception);
                throw;
            }
#endif
        }