Beispiel #1
0
        public MainWindow()
        {
            worldParameters = new WorldParameters();

            InitializeComponent();

            directory = Directory.GetCurrentDirectory();
            Directory.CreateDirectory(directory + "/Worlds");

            textBox_Width.Text         = worldParameters.Data.width.ToString();
            textBox_Height.Text        = worldParameters.Data.height.ToString();
            slider_Water.Value         = worldParameters.Data.waterPercentage;
            slider_WaterBodyBias.Value = worldParameters.Data.waterBodyBias;
            slider_Land.Value          = worldParameters.Data.landPercentage;
            slider_Plain.Value         = worldParameters.Data.plainPercentage;
            slider_Forest.Value        = worldParameters.Data.forestPercentage;
            slider_Mountain.Value      = worldParameters.Data.mountainPercentage;
        }
Beispiel #2
0
        public void Generate(WorldParameters aParemeters)
        {
            int width  = (int)aParemeters.Data.width;
            int height = (int)aParemeters.Data.height;

            myWorldData = new List <List <TileType> >();

            // Fill map with water
            for (int y = 0; y < height; ++y)
            {
                myWorldData.Add(new List <TileType>());
                for (int x = 0; x < width; ++x)
                {
                    myWorldData[y].Add(TileType.Water);
                }
            }

            // Add land
            float  targetLandPercentage  = aParemeters.Data.landPercentage;
            float  landIncrementConstant = 1.0f / (width * height);
            float  islandModifier        = 1.1f - aParemeters.Data.waterBodyBias;
            float  volume     = width * height;
            Random randomizer = new Random();
            List <List <TileType> > buffer = myWorldData;

            // Plain
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    if (randomizer.Next(0, 101) < 40 + 20.0f * targetLandPercentage)
                    {
                        buffer[y][x] = TileType.Plain;
                    }
                }
            }

            for (int i = 0; i < 5; ++i)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        if (buffer[y][x] == TileType.Water && GetAliveNeighbourCount(TileType.Plain, buffer, y, x) >= 5)
                        {
                            buffer[y][x] = TileType.Plain;
                        }
                        if (buffer[y][x] == TileType.Plain && GetAliveNeighbourCount(TileType.Plain, buffer, y, x) < 4)
                        {
                            buffer[y][x] = TileType.Water;
                        }
                    }
                }
            }

            // Mountain
            float targetMountainPercentage = aParemeters.Data.mountainPercentage;

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    if (buffer[y][x] == TileType.Plain && randomizer.Next(0, 101) < 40 + 40.0f * targetMountainPercentage)
                    {
                        buffer[y][x] = TileType.Mountain;
                    }
                }
            }

            for (int i = 0; i < 5; ++i)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        if (buffer[y][x] == TileType.Plain && GetAliveNeighbourCount(TileType.Mountain, buffer, y, x) >= 5 && GetAliveNeighbourCount(TileType.Water, buffer, y, x) == 0)
                        {
                            buffer[y][x] = TileType.Mountain;
                        }
                        if (buffer[y][x] == TileType.Mountain && GetAliveNeighbourCount(TileType.Mountain, buffer, y, x) < 4)
                        {
                            buffer[y][x] = TileType.Plain;
                        }
                    }
                }
            }

            // Forest
            float targetForestPercentage = aParemeters.Data.forestPercentage;

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    if (buffer[y][x] == TileType.Plain && randomizer.Next(0, 101) < 40 + 40.0f * targetForestPercentage)
                    {
                        buffer[y][x] = TileType.Forest;
                    }
                }
            }

            for (int i = 0; i < 5; ++i)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        if (buffer[y][x] == TileType.Plain && GetAliveNeighbourCount(TileType.Forest, buffer, y, x) >= 5)
                        {
                            buffer[y][x] = TileType.Forest;
                        }
                        if (buffer[y][x] == TileType.Forest && GetAliveNeighbourCount(TileType.Forest, buffer, y, x) < 4)
                        {
                            buffer[y][x] = TileType.Plain;
                        }
                    }
                }
            }
        }