]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/noise.cpp
Fix MSVC compatibility
[dragonfireclient.git] / src / noise.cpp
index 9852a1524612c3b6cd21c1787f82362f3e722efa..7a23819c8c9e121bda325f8d77e1b8edca6c2182 100644 (file)
@@ -115,7 +115,9 @@ u32 PcgRandom::range(u32 bound)
 
 s32 PcgRandom::range(s32 min, s32 max)
 {
-       assert(max >= min);
+       if (max < min)
+               throw PrngException("Invalid range (max < min)");
+
        u32 bound = max - min + 1;
        return range(bound) + min;
 }
@@ -146,7 +148,7 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)
        s32 accum = 0;
        for (int i = 0; i != num_trials; i++)
                accum += range(min, max);
-       return ((float)accum / num_trials) + 0.5f;
+       return myround((float)accum / num_trials);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -538,19 +540,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 {