Ejemplo n.º 1
0
        public void DisplayModeModified(string type)
        {
            if (!GenerationComplete)
            {
                return;
            }

            switch (type)
            {
            case "Height":
                ColorKeys.ColorEntries = Overworld.HeightColors;
                Overworld.TextureFromHeightMap(type, Overworld.Map, Overworld.ScalarFieldType.Height, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Biomes":
                ColorKeys.ColorEntries = BiomeLibrary.CreateBiomeColors();
                Overworld.TextureFromHeightMap(type, Overworld.Map, Overworld.ScalarFieldType.Height, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Temp.":
                ColorKeys.ColorEntries = Overworld.JetColors;
                Overworld.TextureFromHeightMap("Gray", Overworld.Map, Overworld.ScalarFieldType.Temperature, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Rain":
                ColorKeys.ColorEntries = Overworld.JetColors;
                Overworld.TextureFromHeightMap("Gray", Overworld.Map, Overworld.ScalarFieldType.Rainfall, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Erosion":
                ColorKeys.ColorEntries = Overworld.JetColors;
                Overworld.TextureFromHeightMap("Gray", Overworld.Map, Overworld.ScalarFieldType.Erosion, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Faults":
                ColorKeys.ColorEntries = Overworld.JetColors;
                Overworld.TextureFromHeightMap("Gray", Overworld.Map, Overworld.ScalarFieldType.Faults, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;

            case "Factions":
                ColorKeys.ColorEntries   = GenerateFactionColors();
                Overworld.NativeFactions = NativeCivilizations;
                Overworld.TextureFromHeightMap(type, Overworld.Map, Overworld.ScalarFieldType.Factions, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), MapPanel.Lock, worldData, worldMap, Settings.SeaLevel);
                break;
            }
        }
Ejemplo n.º 2
0
        public void PreparePreview(GraphicsDevice device)
        {
            if (Generator.CurrentState != WorldGenerator.GenerationState.Finished)
            {
                KeyMesh = null;
                return;
            }

            if (PreviewRenderTarget == null)
            {
                PreviewRenderTarget = new RenderTarget2D(Device, PreviewPanel.Rect.Width, PreviewPanel.Rect.Height);
            }

            if (PreviewTexture == null || UpdatePreview)
            {
                UpdatePreview = false;
                InitializePreviewRenderTypes();

                if (PreviewTexture == null || PreviewTexture.Width != Overworld.Map.GetLength(0) ||
                    PreviewTexture.Height != Overworld.Map.GetLength(1))
                {
                    PreviewTexture = new Texture2D(Device, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1));
                }

                // Check combo box for style of preview to draw.
                Overworld.NativeFactions = Generator.NativeCivilizations;

                var style = PreviewRenderTypes[PreviewSelector.SelectedItem];
                Overworld.TextureFromHeightMap(style.DisplayType, Overworld.Map,
                                               style.Scalar, Overworld.Map.GetLength(0), Overworld.Map.GetLength(1), null,
                                               Generator.worldData, PreviewTexture, Generator.Settings.SeaLevel);

                var colorKeyEntries = style.GetColorKeys(Generator);
                var font            = Root.GetTileSheet("font8");
                var stringMeshes    = new List <Gui.Mesh>();
                var y        = Rect.Y;
                var maxWidth = 0;

                foreach (var color in colorKeyEntries)
                {
                    Rectangle bounds;
                    var       mesh = Gui.Mesh.CreateStringMesh(color.Key, font, new Vector2(1, 1), out bounds);
                    stringMeshes.Add(mesh.Translate(PreviewPanel.Rect.Right - bounds.Width - (font.TileHeight + 4), y).Colorize(new Vector4(0, 0, 0, 1)));
                    if (bounds.Width > maxWidth)
                    {
                        maxWidth = bounds.Width;
                    }
                    stringMeshes.Add(Gui.Mesh.Quad().Scale(font.TileHeight, font.TileHeight)
                                     .Translate(PreviewPanel.Rect.Right - font.TileHeight + 2, y)
                                     .Texture(Root.GetTileSheet("basic").TileMatrix(1))
                                     .Colorize(color.Value.ToVector4()));
                    y += bounds.Height;
                }


                KeyMesh = Gui.Mesh.Merge(stringMeshes.ToArray());
                var thinBorder = Root.GetTileSheet("border-thin");
                var bgMesh     = Gui.Mesh.CreateScale9Background(
                    new Rectangle(Rect.Right - thinBorder.TileWidth - maxWidth - 8 - font.TileHeight,
                                  Rect.Y, maxWidth + thinBorder.TileWidth + 8 + font.TileHeight, y - Rect.Y + thinBorder.TileHeight),
                    thinBorder, Scale9Corners.Bottom | Scale9Corners.Left);
                KeyMesh = Gui.Mesh.Merge(bgMesh, KeyMesh);
            }

            Device.BlendState        = BlendState.Opaque;
            Device.DepthStencilState = DepthStencilState.Default;
            Device.SetRenderTarget(PreviewRenderTarget);
            PreviewEffect.World = Matrix.Identity;

            PreviewEffect.View            = ViewMatrix;
            PreviewEffect.Projection      = ProjectionMatrix;
            cameraTarget                  = newTarget * 0.1f + cameraTarget * 0.9f;
            PreviewEffect.TextureEnabled  = true;
            PreviewEffect.Texture         = PreviewTexture;
            PreviewEffect.LightingEnabled = true;

            if (Generator.LandMesh == null)
            {
                Generator.CreateMesh(Device);
                UpdatePreview = true;
            }

            if (UpdatePreview || Trees == null)
            {
                Trees = new List <Point3>();
                int       width      = Overworld.Map.GetLength(0);
                int       height     = Overworld.Map.GetLength(1);
                const int resolution = 1;

                for (int x = 0; x < width; x += resolution)
                {
                    for (int y = 0; y < height; y += resolution)
                    {
                        if (!MathFunctions.RandEvent(TreeProbability))
                        {
                            continue;
                        }
                        var h = Overworld.Map[x, y].Height;
                        if (!(h > Generator.Settings.SeaLevel))
                        {
                            continue;
                        }
                        var biome = BiomeLibrary.Biomes[Overworld.Map[x, y].Biome];
                        Trees.Add(new Point3(x, y, biome.Icon));
                    }
                }
            }

            Device.Clear(ClearOptions.Target, Color.Black, 1024.0f, 0);
            Device.DepthStencilState = DepthStencilState.Default;

            if (previewSampler == null)
            {
                previewSampler = new SamplerState
                {
                    Filter = TextureFilter.MinLinearMagPointMipPoint
                };
            }

            Device.SamplerStates[0] = previewSampler;
            foreach (EffectPass pass in PreviewEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                Device.SetVertexBuffer(Generator.LandMesh);
                Device.Indices = Generator.LandIndex;
                Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, Generator.LandMesh.VertexCount, 0,
                                             Generator.LandIndex.IndexCount / 3);
            }
            Device.SetRenderTarget(null);
            Device.Textures[0] = null;
            Device.Indices     = null;
            Device.SetVertexBuffer(null);
        }
Ejemplo n.º 3
0
        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
        }