Exemple #1
0
        public LoadState(DwarfGame game, GameStateManager stateManager, WorldGenerationSettings settings) :
            base(game, "LoadState", stateManager)
        {
            Settings          = settings;
            EnableScreensaver = true;

            Runner = new DwarfRunner(game);
        }
Exemple #2
0
        public WorldLoaderState(DwarfGame Game, GameStateManager StateManager) :
            base(Game, StateManager)
        {
            this.ProceedButtonText = "Load";
            this.NoItemsText       = "No worlds found.";

            this.InvalidItemText = "This world was saved by an earlier version of DwarfCorp and is not compatible.";
            this.ValidateItem    = (item) =>
            {
                return(NewOverworldFile.CheckCompatibility(item) ? "" : "Incompatible save file.");
            };
            this.GetItemName = (item) =>
            {
                return(NewOverworldFile.GetOverworldName(item));
            };

            this.ItemSource = () =>
            {
                System.IO.DirectoryInfo worldDirectory = System.IO.Directory.CreateDirectory(DwarfGame.GetWorldDirectory());
                var dirs = worldDirectory.EnumerateDirectories().ToList();
                dirs.Sort((a, b) => b.LastWriteTime.CompareTo(a.LastWriteTime));
                return(dirs);
            };

            this.ScreenshotSource = (path) =>
            {
                try
                {
                    return(AssetManager.LoadUnbuiltTextureFromAbsolutePath(path + ProgramData.DirChar + "screenshot.png"));
                }
                catch (Exception exception)
                {
                    Console.Error.WriteLine(exception.ToString());
                    return(null);
                }
            };

            this.OnProceedClicked = (path) =>
            {
                var file = new NewOverworldFile(path);
                Overworld.Map            = file.Data.CreateMap();
                Overworld.Name           = file.Data.Name;
                Overworld.NativeFactions = new List <Faction>();
                foreach (var faction in file.Data.FactionList)
                {
                    Overworld.NativeFactions.Add(new Faction(faction));
                }
                var settings = new WorldGenerationSettings();
                settings.Width  = Overworld.Map.GetLength(1);
                settings.Height = Overworld.Map.GetLength(0);
                settings.Name   = System.IO.Path.GetFileName(path);
                StateManager.PopState();
                settings.Natives = Overworld.NativeFactions;
                var genState = new WorldGeneratorState(Game, Game.StateManager, settings, false);
                StateManager.PushState(genState);
            };
        }
 public WorldGenerator(WorldGenerationSettings Settings)
 {
     CurrentState         = GenerationState.NotStarted;
     Seed                 = Settings.Seed;
     this.Settings        = Settings;
     MathFunctions.Random = new ThreadSafeRandom(Seed);
     Overworld.Volcanoes  = new List <Vector2>();
     LandMesh             = null;
     LandIndex            = null;
     NativeCivilizations  = Settings.Natives;
 }
 public WorldGeneratorState(DwarfGame Game, GameStateManager StateManager, WorldGenerationSettings Settings, bool AutoGenerate) :
     base(Game, "NewWorldGeneratorState", StateManager)
 {
     this.AutoGenerate = AutoGenerate;
     this.Settings     = Settings;
     if (this.Settings == null)
     {
         this.Settings = new WorldGenerationSettings()
         {
             Width            = 512,
             Height           = 512,
             Name             = TextGenerator.GenerateRandom(TextGenerator.GetAtoms(ContentPaths.Text.Templates.worlds)),
             NumCivilizations = 5,
             NumFaults        = 3,
             NumRains         = 1000,
             NumVolcanoes     = 3,
             RainfallScale    = 1.0f,
             SeaLevel         = 0.17f,
             TemperatureScale = 1.0f
         }
     }
     ;
 }
Exemple #5
0
        public void MakeDebugWorldMenu()
        {
            GuiRoot.RootItem.Clear();

            var frame = MakeMenuFrame("DEBUG WORLDS");

            MakeMenuItem(frame, "Hills", "Create a hilly world.", (sender, args) =>
            {
                Overworld.CreateHillsLand(Game.GraphicsDevice);
                StateManager.ClearState();
                WorldGenerationSettings settings = new WorldGenerationSettings()
                {
                    ExistingFile = null,
                    ColonySize   = new Point3(8, 1, 8),
                    WorldScale   = 2.0f,
                    WorldOrigin  = new Vector2(Overworld.Map.GetLength(0) / 2.0f,
                                               Overworld.Map.GetLength(1) / 2.0f) * 0.5f
                };
                StateManager.PushState(new LoadState(Game, StateManager, settings));
            });

            MakeMenuItem(frame, "Cliffs", "Create a cliff-y world.", (sender, args) =>
            {
                Overworld.CreateCliffsLand(Game.GraphicsDevice);
                StateManager.ClearState();
                WorldGenerationSettings settings = new WorldGenerationSettings()
                {
                    ExistingFile = null,
                    ColonySize   = new Point3(8, 1, 8),
                    WorldScale   = 2.0f,
                    WorldOrigin  = new Vector2(Overworld.Map.GetLength(0) / 2.0f,
                                               Overworld.Map.GetLength(1) / 2.0f) * 0.5f
                };
                StateManager.PushState(new LoadState(Game, StateManager, settings));
            });

            MakeMenuItem(frame, "Flat", "Create a flat world.", (sender, args) =>
            {
                Overworld.CreateUniformLand(Game.GraphicsDevice);
                StateManager.ClearState();
                WorldGenerationSettings settings = new WorldGenerationSettings()
                {
                    ExistingFile = null,
                    ColonySize   = new Point3(8, 1, 8),
                    WorldScale   = 2.0f,
                    WorldOrigin  = new Vector2(Overworld.Map.GetLength(0) / 2.0f,
                                               Overworld.Map.GetLength(1) / 2.0f) * 0.5f
                };
                StateManager.PushState(new LoadState(Game, StateManager, settings));
            });

            MakeMenuItem(frame, "Ocean", "Create an ocean world", (sender, args) =>
            {
                Overworld.CreateOceanLand(Game.GraphicsDevice, 0.17f);
                StateManager.ClearState();
                WorldGenerationSettings settings = new WorldGenerationSettings()
                {
                    ExistingFile = null,
                    ColonySize   = new Point3(8, 1, 8),
                    WorldScale   = 2.0f,
                    WorldOrigin  = new Vector2(Overworld.Map.GetLength(0) / 2.0f,
                                               Overworld.Map.GetLength(1) / 2.0f) * 0.5f
                };
                StateManager.PushState(new LoadState(Game, StateManager, settings));
            });

            MakeMenuItem(frame, "Back", "Go back to the main menu.", (sender, args) => StateManager.PopState());

            GuiRoot.RootItem.Layout();
        }
        public override void OnEnter()
        {
            // Clear the input queue... cause other states aren't using it and it's been filling up.
            DwarfGame.GumInputMapper.GetInputQueue();

            GuiRoot = new Gui.Root(DwarfGame.GuiSkin);
            GuiRoot.MousePointer = new MousePointer("mouse", 15.0f, 16, 17, 18, 19, 20, 21, 22, 23);

            var mainPanel = GuiRoot.RootItem.AddChild(new Gui.Widget
            {
                Rect           = GuiRoot.RenderData.VirtualScreen,
                Border         = "border-fancy",
                Text           = Settings.Name,
                Font           = "font16",
                TextColor      = new Vector4(0, 0, 0, 1),
                Padding        = new Gui.Margin(4, 4, 4, 4),
                InteriorMargin = new Gui.Margin(24, 0, 0, 0),
            });

            var rightPanel = mainPanel.AddChild(new Gui.Widget
            {
                AutoLayout  = Gui.AutoLayout.DockRight,
                MinimumSize = new Point(256, 0),
                Padding     = new Gui.Margin(2, 2, 2, 2)
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Regenerate",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) => { Settings = new WorldGenerationSettings();  RestartGeneration(); }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Save World",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    if (Generator.CurrentState != WorldGenerator.GenerationState.Finished)
                    {
                        GuiRoot.ShowTooltip(GuiRoot.MousePosition, "Generator is not finished.");
                    }
                    else
                    {
                        System.IO.DirectoryInfo worldDirectory = System.IO.Directory.CreateDirectory(DwarfGame.GetWorldDirectory() + System.IO.Path.DirectorySeparatorChar + Settings.Name);
                        NewOverworldFile file = new NewOverworldFile(Game.GraphicsDevice, Overworld.Map, Settings.Name, Settings.SeaLevel);
                        file.WriteFile(worldDirectory.FullName);
                        file.SaveScreenshot(worldDirectory.FullName + System.IO.Path.DirectorySeparatorChar + "screenshot.png");
                        GuiRoot.ShowModalPopup(GuiRoot.ConstructWidget(new Gui.Widgets.Popup
                        {
                            Text = "File saved."
                        }));
                    }
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Advanced",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    var advancedSettingsEditor = GuiRoot.ConstructWidget(new Gui.Widgets.WorldGenerationSettingsDialog
                    {
                        Settings = Settings,
                        OnClose  = (s) =>
                        {
                            if ((s as Gui.Widgets.WorldGenerationSettingsDialog).Result == Gui.Widgets.WorldGenerationSettingsDialog.DialogResult.Okay)
                            {
                                RestartGeneration();
                            }
                        }
                    });

                    GuiRoot.ShowModalPopup(advancedSettingsEditor);
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Back",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    Generator.Abort();
                    StateManager.PopState();
                }
            });

            StartButton = rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Start Game",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockBottom,
                OnClick            = (sender, args) =>
                {
                    if (Generator.CurrentState != WorldGenerator.GenerationState.Finished)
                    {
                        GuiRoot.ShowTooltip(GuiRoot.MousePosition, "World generation is not finished.");
                    }
                    else
                    {
                        Overworld.Name        = Settings.Name;
                        Settings.ExistingFile = null;
                        Settings.WorldOrigin  = Settings.WorldGenerationOrigin;
                        Settings.SpawnRect    = Generator.GetSpawnRectangle();
                        if (Settings.Natives == null || Settings.Natives.Count == 0)
                        {
                            Settings.Natives = Generator.NativeCivilizations;
                        }
                        //Settings.StartUnderground = StartUnderground.CheckState;
                        //Settings.RevealSurface = RevealSurface.CheckState;

                        foreach (var faction in Settings.Natives)
                        {
                            Vector2 center            = new Vector2(faction.Center.X, faction.Center.Y);
                            Vector2 spawn             = new Vector2(Generator.GetSpawnRectangle().Center.X, Generator.GetSpawnRectangle().Center.Y);
                            faction.DistanceToCapital = (center - spawn).Length();
                            faction.ClaimsColony      = false;
                        }

                        foreach (var faction in Generator.GetFactionsInSpawn())
                        {
                            faction.ClaimsColony = true;
                        }

                        StateManager.ClearState();
                        StateManager.PushState(new LoadState(Game, StateManager, Settings));
                    }
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text       = "Territory size",
                AutoLayout = Gui.AutoLayout.DockTop,
                Font       = "font8",
                TextColor  = new Vector4(0, 0, 0, 1),
            });

            var colonySizeCombo = rightPanel.AddChild(new Gui.Widgets.ComboBox
            {
                AutoLayout             = Gui.AutoLayout.DockTop,
                Items                  = new List <string>(new string[] { "Small", "Medium", "Large" }),
                Font                   = "font8",
                TextColor              = new Vector4(0, 0, 0, 1),
                OnSelectedIndexChanged = (sender) =>
                {
                    switch ((sender as Gui.Widgets.ComboBox).SelectedItem)
                    {
                    case "Small": Settings.ColonySize = new Point3(4, 1, 4); break;

                    case "Medium": Settings.ColonySize = new Point3(8, 1, 8); break;

                    case "Large": Settings.ColonySize = new Point3(10, 1, 10); break;
                    }

                    var worldSize = Settings.ColonySize.ToVector3() * VoxelConstants.ChunkSizeX / Settings.WorldScale;

                    float w = worldSize.X;
                    float h = worldSize.Z;

                    float clickX = System.Math.Max(System.Math.Min(Settings.WorldGenerationOrigin.X, Settings.Width - w), 0);
                    float clickY = System.Math.Max(System.Math.Min(Settings.WorldGenerationOrigin.Y, Settings.Height - h), 0);

                    Settings.WorldGenerationOrigin = new Vector2((int)(clickX), (int)(clickY));
                }
            }) as Gui.Widgets.ComboBox;

            rightPanel.AddChild(new Gui.Widget
            {
                Text       = "Difficulty",
                AutoLayout = Gui.AutoLayout.DockTop,
                Font       = "font8",
                TextColor  = new Vector4(0, 0, 0, 1)
            });

            var difficultySelectorCombo = rightPanel.AddChild(new Gui.Widgets.ComboBox
            {
                AutoLayout             = Gui.AutoLayout.DockTop,
                Items                  = EmbarkmentLibrary.Embarkments.Select(e => e.Key).ToList(),
                TextColor              = new Vector4(0, 0, 0, 1),
                Font                   = "font8",
                OnSelectedIndexChanged = (sender) =>
                {
                    Settings.InitalEmbarkment = EmbarkmentLibrary.Embarkments[(sender as Gui.Widgets.ComboBox).SelectedItem];
                }
            }) as Gui.Widgets.ComboBox;

            /*
             * StartUnderground = rightPanel.AddChild(new Gui.Widgets.CheckBox
             * {
             *  AutoLayout = Gui.AutoLayout.DockTop,
             *  Font = "font8",
             *  Text = "@world-generation-start-underground"
             * }) as Gui.Widgets.CheckBox;
             *
             * RevealSurface = rightPanel.AddChild(new Gui.Widgets.CheckBox
             * {
             *  AutoLayout = Gui.AutoLayout.DockTop,
             *  Font = "font8",
             *  Text = "@world-generation-reveal-surface",
             *  CheckState = true
             * }) as Gui.Widgets.CheckBox;
             */

            rightPanel.AddChild(new Gui.Widget
            {
                Text       = "Cave Layers",
                AutoLayout = Gui.AutoLayout.DockTop,
                Font       = "font8",
                TextColor  = new Vector4(0, 0, 0, 1),
            });

            var layerSetting = rightPanel.AddChild(new Gui.Widgets.ComboBox
            {
                AutoLayout             = AutoLayout.DockTop,
                Items                  = new List <string>(new string[] { "Barely any", "Few", "Normal", "Lots", "Way too many" }),
                Font                   = "font8",
                TextColor              = new Vector4(0, 0, 0, 1),
                OnSelectedIndexChanged = (sender) =>
                {
                    switch ((sender as Gui.Widgets.ComboBox).SelectedItem)
                    {
                    case "Barely any": Settings.NumCaveLayers = 2; break;

                    case "Few": Settings.NumCaveLayers = 3; break;

                    case "Normal": Settings.NumCaveLayers = 4; break;

                    case "Lots": Settings.NumCaveLayers = 6; break;

                    case "Way too many": Settings.NumCaveLayers = 9; break;
                    }
                }
            }) as Gui.Widgets.ComboBox;

            ZoomedPreview = rightPanel.AddChild(new Gui.Widget
            {
                AutoLayout = Gui.AutoLayout.DockBottom,
                OnLayout   = (sender) =>
                {
                    var space = System.Math.Min(
                        layerSetting.Rect.Width, StartButton.Rect.Top - layerSetting.Rect.Bottom - 4);
                    sender.Rect.Height = space;
                    sender.Rect.Width  = space;
                    sender.Rect.Y      = layerSetting.Rect.Bottom + 2;
                    sender.Rect.X      = layerSetting.Rect.X +
                                         ((layerSetting.Rect.Width - space) / 2);
                }
            });

            GenerationProgress = mainPanel.AddChild(new Gui.Widgets.ProgressBar
            {
                AutoLayout          = Gui.AutoLayout.DockBottom,
                TextHorizontalAlign = Gui.HorizontalAlign.Center,
                TextVerticalAlign   = Gui.VerticalAlign.Center,
                Font      = "font10",
                TextColor = new Vector4(1, 1, 1, 1)
            }) as Gui.Widgets.ProgressBar;

            Preview = mainPanel.AddChild(new WorldGeneratorPreview(Game.GraphicsDevice)
            {
                Border     = "border-thin",
                AutoLayout = Gui.AutoLayout.DockFill
            }) as WorldGeneratorPreview;

            GuiRoot.RootItem.Layout();

            difficultySelectorCombo.SelectedIndex = difficultySelectorCombo.Items.IndexOf("Normal");
            colonySizeCombo.SelectedIndex         = colonySizeCombo.Items.IndexOf("Medium");
            layerSetting.SelectedIndex            = layerSetting.Items.IndexOf("Normal");

            IsInitialized = true;

            if (AutoGenerate)
            {
                RestartGeneration();
            }
            else // Setup a dummy generator for now.
            {
                Generator = new WorldGenerator(Settings);
                Generator.LoadDummy(
                    new Color[Overworld.Map.GetLength(0) * Overworld.Map.GetLength(1)],
                    Game.GraphicsDevice);
                Preview.SetGenerator(Generator);
            }

            base.OnEnter();
        }
Exemple #7
0
        public override void Update(DwarfTime gameTime)
        {
            if (DoneLoading)
            {
                // Todo: Decouple gui/input from world.
                // Copy important bits to PlayState - This is a hack; decouple world from gui and input instead.
                PlayState.Input = Input;
                StateManager.PopState(false);
                StateManager.PushState(new PlayState(Game, StateManager, World));

                World.OnSetLoadingMessage = null;
                Overworld.NativeFactions  = World.Natives;
            }
            else
            {
                if (Settings.GenerateFromScratch && Generator.CurrentState == WorldGenerator.GenerationState.Finished && World == null)
                {
                    Settings = Generator.Settings;
                    CreateWorld();
                }
                else if (Settings.GenerateFromScratch)
                {
                    if (!LoadTicker.HasMesssage(Generator.LoadingMessage))
                    {
                        LoadTicker.AddMessage(Generator.LoadingMessage);
                    }
                }

                foreach (var item in DwarfGame.GumInputMapper.GetInputQueue())
                {
                    GuiRoot.HandleInput(item.Message, item.Args);
                    if (item.Message == Gui.InputEvents.KeyPress)
                    {
                        Runner.Jump();
                    }
                }

                GuiRoot.Update(gameTime.ToRealTime());
                Runner.Update(gameTime);

                if (World != null && World.LoadStatus == WorldManager.LoadingStatus.Failure && !DisplayException)
                {
                    DisplayException = true;
                    string exceptionText = World.LoadingException == null
                        ? "Unknown exception."
                        : World.LoadingException.ToString();
                    GuiRoot.MouseVisible        = true;
                    GuiRoot.MousePointer        = new Gui.MousePointer("mouse", 4, 0);
                    DwarfTime.LastTime.IsPaused = false;
                    DwarfTime.LastTime.Speed    = 1.0f;
                    World = null;

                    GuiRoot.ShowModalPopup(new Gui.Widgets.Confirm()
                    {
                        CancelText = "",
                        Text       = "Oh no! Loading failed :( This crash has been automatically reported to Completely Fair Games: " + exceptionText,
                        OnClick    = (s, a) =>
                        {
                            StateManager.PopState();
                            StateManager.ClearState();
                            StateManager.PushState(new MainMenuState(Game, StateManager));
                        },
                        OnClose = (s) =>
                        {
                            StateManager.PopState();
                            StateManager.ClearState();
                            StateManager.PushState(new MainMenuState(Game, StateManager));
                        },
                        Rect = GuiRoot.RenderData.VirtualScreen
                    });
                }
            }

            base.Update(gameTime);
        }
        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 override void OnEnter()
        {
            // Clear the input queue... cause other states aren't using it and it's been filling up.
            DwarfGame.GumInputMapper.GetInputQueue();

            GuiRoot = new Gui.Root(DwarfGame.GumSkin);
            GuiRoot.MousePointer = new MousePointer("mouse", 15.0f, 16, 17, 18, 19, 20, 21, 22, 23);

            var mainPanel = GuiRoot.RootItem.AddChild(new Gui.Widget
            {
                Rect           = GuiRoot.RenderData.VirtualScreen,
                Border         = "border-fancy",
                Text           = Settings.Name,
                Font           = "font16",
                TextColor      = new Vector4(0, 0, 0, 1),
                Padding        = new Gui.Margin(4, 4, 4, 4),
                InteriorMargin = new Gui.Margin(24, 0, 0, 0),
            });

            var rightPanel = mainPanel.AddChild(new Gui.Widget
            {
                AutoLayout  = Gui.AutoLayout.DockRight,
                MinimumSize = new Point(256, 0),
                Padding     = new Gui.Margin(2, 2, 2, 2)
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Regenerate",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) => { Settings = new WorldGenerationSettings();  RestartGeneration(); }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Save World",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    if (Generator.CurrentState != WorldGenerator.GenerationState.Finished)
                    {
                        GuiRoot.ShowTooltip(GuiRoot.MousePosition, "Generator is not finished.");
                    }
                    else
                    {
                        System.IO.DirectoryInfo worldDirectory = System.IO.Directory.CreateDirectory(DwarfGame.GetWorldDirectory() + ProgramData.DirChar + Settings.Name);
                        NewOverworldFile file = new NewOverworldFile(Game.GraphicsDevice, Overworld.Map, Settings.Name, Settings.SeaLevel);
                        file.WriteFile(worldDirectory.FullName);
                        file.SaveScreenshot(worldDirectory.FullName + ProgramData.DirChar + "screenshot.png");
                        GuiRoot.ShowModalPopup(GuiRoot.ConstructWidget(new Gui.Widgets.Popup
                        {
                            Text = "File saved."
                        }));
                    }
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Advanced",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    var advancedSettingsEditor = GuiRoot.ConstructWidget(new Gui.Widgets.WorldGenerationSettingsDialog
                    {
                        Settings = Settings,
                        OnClose  = (s) => RestartGeneration()
                    });

                    GuiRoot.ShowModalPopup(advancedSettingsEditor);
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Back",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockTop,
                OnClick            = (sender, args) =>
                {
                    Generator.Abort();
                    StateManager.PopState();
                }
            });

            StartButton = rightPanel.AddChild(new Gui.Widget
            {
                Text               = "Start Game",
                Border             = "border-button",
                ChangeColorOnHover = true,
                TextColor          = new Vector4(0, 0, 0, 1),
                Font               = "font16",
                AutoLayout         = Gui.AutoLayout.DockBottom,
                OnClick            = (sender, args) =>
                {
                    if (Generator.CurrentState != WorldGenerator.GenerationState.Finished)
                    {
                        GuiRoot.ShowTooltip(GuiRoot.MousePosition, "World generation is not finished.");
                    }
                    else
                    {
                        Overworld.Name        = Settings.Name;
                        Settings.ExistingFile = null;
                        Settings.WorldOrigin  = Settings.WorldGenerationOrigin;
                        if (Settings.Natives == null || Settings.Natives.Count == 0)
                        {
                            Settings.Natives = Generator.NativeCivilizations;
                        }

                        StateManager.ClearState();
                        StateManager.PushState(new LoadState(Game, StateManager, Settings));
                    }
                }
            });

            rightPanel.AddChild(new Gui.Widget
            {
                Text       = "Colony size",
                AutoLayout = Gui.AutoLayout.DockTop,
                Font       = "font8",
                TextColor  = new Vector4(0, 0, 0, 1)
            });

            var colonySizeCombo = rightPanel.AddChild(new Gui.Widgets.ComboBox
            {
                AutoLayout             = Gui.AutoLayout.DockTop,
                Items                  = new List <string>(new string[] { "Small", "Medium", "Large" }),
                Font                   = "font8",
                TextColor              = new Vector4(0, 0, 0, 1),
                OnSelectedIndexChanged = (sender) =>
                {
                    switch ((sender as Gui.Widgets.ComboBox).SelectedItem)
                    {
                    case "Small": Settings.ColonySize = new Point3(4, 1, 4); break;

                    case "Medium": Settings.ColonySize = new Point3(8, 1, 8); break;

                    case "Large": Settings.ColonySize = new Point3(10, 1, 10); break;
                    }

                    var worldSize = Settings.ColonySize.ToVector3() * VoxelConstants.ChunkSizeX / Settings.WorldScale;

                    float w = worldSize.X / 2;
                    float h = worldSize.Z / 2;

                    float clickX = System.Math.Max(System.Math.Min(Settings.WorldGenerationOrigin.X, Settings.Width - w), w);
                    float clickY = System.Math.Max(System.Math.Min(Settings.WorldGenerationOrigin.Y, Settings.Height - h), h);

                    Settings.WorldGenerationOrigin = new Vector2((int)(clickX), (int)(clickY));
                }
            }) as Gui.Widgets.ComboBox;

            rightPanel.AddChild(new Gui.Widget
            {
                Text       = "Difficulty",
                AutoLayout = Gui.AutoLayout.DockTop,
                Font       = "font8",
                TextColor  = new Vector4(0, 0, 0, 1)
            });

            var difficultySelectorCombo = rightPanel.AddChild(new Gui.Widgets.ComboBox
            {
                AutoLayout             = Gui.AutoLayout.DockTop,
                Items                  = Embarkment.EmbarkmentLibrary.Select(e => e.Key).ToList(),
                TextColor              = new Vector4(0, 0, 0, 1),
                Font                   = "font8",
                OnSelectedIndexChanged = (sender) =>
                {
                    Settings.InitalEmbarkment = Embarkment.EmbarkmentLibrary[(sender as Gui.Widgets.ComboBox).SelectedItem];
                }
            }) as Gui.Widgets.ComboBox;

            ZoomedPreview = rightPanel.AddChild(new Gui.Widget
            {
                AutoLayout = Gui.AutoLayout.DockBottom,
                OnLayout   = (sender) =>
                {
                    var space = System.Math.Min(
                        difficultySelectorCombo.Rect.Width, StartButton.Rect.Top - difficultySelectorCombo.Rect.Bottom - 4);
                    sender.Rect.Height = space;
                    sender.Rect.Width  = space;
                    sender.Rect.Y      = difficultySelectorCombo.Rect.Bottom + 2;
                    sender.Rect.X      = difficultySelectorCombo.Rect.X +
                                         ((difficultySelectorCombo.Rect.Width - space) / 2);
                }
            });


            StatsLabel = rightPanel.AddChild(new Gui.Widget()
            {
                AutoLayout          = Gui.AutoLayout.DockBottom,
                Font                = "font8",
                TextColor           = new Vector4(1, 1, 1, 1),
                Background          = new TileReference("sbasic", 0),
                BackgroundColor     = new Vector4(0.0f, 0.0f, 0.0f, 0.2f),
                TextHorizontalAlign = HorizontalAlign.Left,
                MinimumSize         = new Point(128, 64),
                WrapText            = true,
                Padding             = new Margin(10, 10, 10, 0)
            });

            GenerationProgress = mainPanel.AddChild(new Gui.Widgets.ProgressBar
            {
                AutoLayout          = Gui.AutoLayout.DockBottom,
                TextHorizontalAlign = Gui.HorizontalAlign.Center,
                TextVerticalAlign   = Gui.VerticalAlign.Center,
                Font      = "font10",
                TextColor = new Vector4(1, 1, 1, 1)
            }) as Gui.Widgets.ProgressBar;

            Preview = mainPanel.AddChild(new WorldGeneratorPreview(Game.GraphicsDevice)
            {
                Border     = "border-thin",
                AutoLayout = Gui.AutoLayout.DockFill
            }) as WorldGeneratorPreview;

            GuiRoot.RootItem.Layout();
            Preview.PreviewPanel.OnClick += (widget, args) =>
            {
                StatsLabel.Text = Generator.GetSpawnStats();

                StatsLabel.Invalidate();
            };

            difficultySelectorCombo.SelectedIndex = difficultySelectorCombo.Items.IndexOf("Normal");
            colonySizeCombo.SelectedIndex         = colonySizeCombo.Items.IndexOf("Medium");

            IsInitialized = true;

            if (AutoGenerate)
            {
                RestartGeneration();
            }
            else // Setup a dummy generator for now.
            {
                Generator = new WorldGenerator(Settings);
                Generator.LoadDummy(
                    new Color[Overworld.Map.GetLength(0) * Overworld.Map.GetLength(1)],
                    Game.GraphicsDevice);
                Preview.SetGenerator(Generator);
            }

            base.OnEnter();
        }