public float[,] GenerateMap(DiamondSquareGenerationData generationData, CancellationToken token) { if (generationData.PowerOfTwo > MaxPowerOfTwo) { throw new ArgumentException($"n should not be more then {MaxPowerOfTwo} or it can cause an overflow."); } if (generationData.PowerOfTwo < MinValueOfTwo) { throw new ArgumentException($"n should not be less then {MinValueOfTwo}."); } GenerateMapInternal(generationData, token); return(m_Map); }
private void GenerateMapInternal(DiamondSquareGenerationData generationData, CancellationToken token) { m_Random = new Random(generationData.Seed); var size = (int)Math.Pow(2, generationData.PowerOfTwo) + 1; m_Map = new float[size, size]; var startValue = RandRange(MinValue, MaxValue); var maxIndex = size - 1; m_Map[0, 0] = startValue; m_Map[0, maxIndex] = startValue; m_Map[maxIndex, 0] = startValue; m_Map[maxIndex, maxIndex] = startValue; for (var i = maxIndex; i > 1; i /= 2) { var noiseModifier = (MaxValue - MinValue) * generationData.Roughness * ((float)i / maxIndex); for (var y = 0; y < maxIndex; y += i) { for (var x = 0; x < maxIndex; x += i) { DiamondStep(x, y, i, noiseModifier); } } for (var y = 0; y < maxIndex; y += i) { for (var x = 0; x < maxIndex; x += i) { SquareStep(x, y, i, noiseModifier, maxIndex); } } token.ThrowIfCancellationRequested(); } }