diff options
author | kwolekr <kwolekr@minetest.net> | 2015-04-21 12:59:09 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2015-04-21 13:05:14 -0400 |
commit | 943c6e523e64dd879b7974b7adc35153095fd80e (patch) | |
tree | 688f9e08ee967fafb3ffcbb6b7cca7f0a8e1654a /src/noise.cpp | |
parent | c197e039226db9f154703a424c012615c223a7f6 (diff) | |
download | hax-minetest-server-943c6e523e64dd879b7974b7adc35153095fd80e.tar.gz hax-minetest-server-943c6e523e64dd879b7974b7adc35153095fd80e.zip |
Noise: Add noise unittests
Fix buffer size calculation for lacunarity < 1.0
Add guard against absurd noise parameters
Diffstat (limited to 'src/noise.cpp')
-rw-r--r-- | src/noise.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/noise.cpp b/src/noise.cpp index 9852a1524..4bfc46f15 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -538,19 +538,28 @@ void Noise::setOctaves(int octaves) void Noise::resizeNoiseBuf(bool is3d) { - int nlx, nly, nlz; - float ofactor; - //maximum possible spread value factor - ofactor = pow(np.lacunarity, np.octaves - 1); + float ofactor = (np.lacunarity > 1.0) ? + pow(np.lacunarity, np.octaves - 1) : + np.lacunarity; + + // noise lattice point count + // (int)(sz * spread * ofactor) is # of lattice points crossed due to length + float num_noise_points_x = sx * ofactor / np.spread.X; + float num_noise_points_y = sy * ofactor / np.spread.Y; + float num_noise_points_z = sz * ofactor / np.spread.Z; + + // protect against obviously invalid parameters + if (num_noise_points_x > 1000000000.f || + num_noise_points_y > 1000000000.f || + num_noise_points_z > 1000000000.f) + throw InvalidNoiseParamsException(); - //noise lattice point count - //(int)(sz * spread * ofactor) is # of lattice points crossed due to length // + 2 for the two initial endpoints // + 1 for potentially crossing a boundary due to offset - nlx = (int)ceil(sx * ofactor / np.spread.X) + 3; - nly = (int)ceil(sy * ofactor / np.spread.Y) + 3; - nlz = is3d ? (int)ceil(sz * ofactor / np.spread.Z) + 3 : 1; + size_t nlx = (size_t)ceil(num_noise_points_x) + 3; + size_t nly = (size_t)ceil(num_noise_points_y) + 3; + size_t nlz = is3d ? (size_t)ceil(num_noise_points_z) + 3 : 1; delete[] noise_buf; try { |