private int[] CalculateBiomes(ChunkCoordinates coordinates, ChunkLandscape landscape)
        {
            // var biomes = BiomeProvider.GetBiomes().ToArray();

            int worldX = coordinates.X << 4;
            int worldZ = coordinates.Z << 4;

            float[] weightedBiomes = new float[256];
            var     biomeData      = new int[SampleArraySize * SampleArraySize];

            for (int x = -SampleSize; x < SampleSize + 5; x++)
            {
                for (int z = -SampleSize; z < SampleSize + 5; z++)
                {
                    var xx = worldX + ((x * 8f));
                    var zz = worldZ + ((z * 8f));

                    var temp = TemperatureNoise.GetValue(xx, zz);

                    //Note for self: The temperature noise returns a value between -1 & 1 however we need the value to be between 0 & 2.
                    //We do this by getting the absolute value (0 to 1) and multiplying it by 2.
                    temp = MathF.Abs(temp) * 2f;

                    var rain = MathF.Abs(RainfallNoise.GetValue(xx, zz));

                    if (temp < _lowestTemp)
                    {
                        _lowestTemp = temp;
                    }

                    if (temp > _highestTemp)
                    {
                        _highestTemp = temp;
                    }

                    if (rain < _lowestHumidity)
                    {
                        _lowestHumidity = rain;
                    }

                    if (rain > _highestHumidity)
                    {
                        _highestHumidity = rain;
                    }

                    _totalTemp     += temp;
                    _totalHumidity += rain;
                    _totalLookups++;

                    if (_totalLookups % 1024 == 0)
                    {
                        /*  Console.Clear();
                         *
                         * Console.WriteLine(
                         *    $"Average temperature: {_totalTemp / _totalLookups} Highest: {_highestTemp} Lowest: {_lowestTemp}");
                         *
                         * Console.WriteLine(
                         *    $"Average humidity: {_totalHumidity / _totalLookups} Highest: {_highestHumidity} Lowest: {_lowestHumidity}");
                         *
                         * Console.WriteLine($"Unique biomes: {_uniqueBiomes.Count}");*/
                    }

                    biomeData[(x + SampleSize) * SampleArraySize + (z + SampleSize)] = (BiomeProvider.GetBiome(
                                                                                            (float)temp, (float)rain).Id);
                }
            }

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    int index = NoiseMap.GetIndex(x, z);

                    float totalWeight = 0;
                    for (int mapX = 0; mapX < SampleArraySize; mapX++)
                    {
                        for (int mapZ = 0; mapZ < SampleArraySize; mapZ++)
                        {
                            float weight = _weightings[mapX * SampleArraySize + mapZ][(x << 4) + z];
                            if (weight > 0)
                            {
                                totalWeight += weight;
                                weightedBiomes[biomeData[mapX * SampleArraySize + mapZ]] += weight;
                            }
                        }
                    }

                    // normalize biome weights
                    for (int biomeIndex = 0; biomeIndex < weightedBiomes.Length; biomeIndex++)
                    {
                        weightedBiomes[biomeIndex] /= totalWeight;
                    }

                    // combine mesa biomes
                    // mesaCombiner.adjust(weightedBiomes);
                    landscape.Noise[index] = 0f;

                    float river = TerrainBase.GetRiverStrength(new BlockCoordinates(worldX + x, 0, worldZ + z), this);
                    landscape.River[index] = -river;

                    float maxWeight = 0f;
                    for (int i = 0; i < weightedBiomes.Length; i++)
                    {
                        var value = weightedBiomes[i];

                        if (value > 0f)
                        {
                            var biome = BiomeProvider.GetBiome(i);

                            landscape.Noise[index] += biome.RNoise(
                                this, worldX + x, worldZ + z, value, river + 1f) * value;

                            weightedBiomes[i] = 0f;

                            if (value > maxWeight)
                            {
                                maxWeight = value;
                                landscape.Biome[index] = biome;

                                if (!_uniqueBiomes.Contains(biome.Id))
                                {
                                    _uniqueBiomes.TryAdd(biome.Id);
                                    // Console.WriteLine($"Unique biomes: {uniqueBiomes.Count}");
                                }
                            }
                        }
                    }

                    //landscape.Biome[index] = weightedBiomes.;

                    // landscape.Biome[index] = BiomeProvider.GetBiome(i);
                    //landscape.Biome[index] = BiomeProvider.GetBiome();
                }
            }

            return(biomeData);
        }
Esempio n. 2
0
        private int[] CalculateBiomes(ChunkCoordinates coordinates, ChunkLandscape landscape)
        {
            int worldX = coordinates.X * 16;
            int worldZ = coordinates.Z * 16;

            float[] weightedBiomes = new float[256];
            var     biomeData      = new int[SampleArraySize * SampleArraySize];

            for (int x = -SampleSize; x < SampleSize + 5; x++)
            {
                for (int z = -SampleSize; z < SampleSize + 5; z++)
                {
                    var temp = TemperatureNoise.GetValue(worldX + ((x * 8)), worldZ + ((z * 8)));
                    var rain = RainfallNoise.GetValue(worldX + ((x * 8)), worldZ + ((z * 8)));

                    biomeData[(x + SampleSize) * SampleArraySize + (z + SampleSize)] = (BiomeProvider.GetBiome((float)temp, (float)rain).Id);
                }
            }

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    int index = NoiseMap.GetIndex(x, z);

                    float totalWeight = 0;
                    for (int mapX = 0; mapX < SampleArraySize; mapX++)
                    {
                        for (int mapZ = 0; mapZ < SampleArraySize; mapZ++)
                        {
                            float weight = _weightings[mapX * SampleArraySize + mapZ][x * 16 + z];
                            if (weight > 0)
                            {
                                totalWeight += weight;
                                weightedBiomes[biomeData[mapX * SampleArraySize + mapZ]] += weight;
                            }
                        }
                    }

                    // normalize biome weights
                    for (int biomeIndex = 0; biomeIndex < weightedBiomes.Length; biomeIndex++)
                    {
                        weightedBiomes[biomeIndex] /= totalWeight;
                    }

                    // combine mesa biomes
                    // mesaCombiner.adjust(weightedBiomes);

                    landscape.Noise[index] = 0f;

                    float river = TerrainBase.GetRiverStrength(new BlockCoordinates(worldX + x, 0, worldZ + z), this);
                    landscape.River[index] = -river;

                    for (int i = 0; i < 256; i++)
                    {
                        var value = weightedBiomes[i];
                        if (value > 0f)
                        {
                            var biome = BiomeProvider.GetBiome(i);
                            landscape.Noise[index] += biome.RNoise(this, worldX + x, worldZ + z, value, river + 1f) * value;
                            landscape.Biome[index]  = biome;
                            // 0 for the next column
                            weightedBiomes[i] = 0f;
                        }
                    }

                    // landscape.Biome[index] = BiomeProvider.GetBiome(i);
                    //landscape.Biome[index] = BiomeProvider.GetBiome();
                }
            }

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    BlockCoordinates pos = new BlockCoordinates(worldX + (x - 7) * 8 + 4, 0, worldZ + (z - 7) * 8 + 4);
                    //  landscape.Biome[x * 16 + z] = BiomeProvider.GetBiome(pos.X, pos.Z);
                }
            }

            return(biomeData);
        }