Advertisement
BlackJar72

Spatial Random / Spatial Noise (4D Hash)

Feb 6th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. /*
  2.  * A code snippet (not the whole thing) or a system for
  3.  * generating sequences of random number at points in
  4.  * space and "time" (sequence order) by hashing 4-dimensional
  5.  * coordinates.  The purpose of this is for use in
  6.  * procedural content generation, as an alternative to
  7.  * complex systems of region / chunk / area seed calculations.
  8.  * This allows for any of the needed pseudorandom numbers
  9.  * to be generated on demand, in any order, as many times
  10.  * as required -- easily adapting to worlds generating in
  11.  * unpredictable order based on player actions and location.
  12.  *
  13.  * As a side benefit and instance (class static or global)
  14.  * with a hardcoded magic number for a seed can be used
  15.  * to has location specific data for storage in a hashmap
  16.  * style cache.
  17.  *
  18.  * For fewer dimension, of course, it can be simplified.
  19.  *
  20.  * Other data types are easily derived, of course.  For
  21.  * example, taking only lower order bits for smaller
  22.  * integral types or dividing by 0xffffffffffffffff to
  23.  * get floating point types.
  24.  *
  25.  * An simpler but flawed version was previously posted,
  26.  * but it had serious flaws when used with random coordinates;
  27.  * this is bigger and slower but fixes those problems.
  28.  *
  29.  * Fuller implementations can be found at:
  30.  * https://github.com/BlackJar72/MyLibraries/blob/master/GameMath/src/SpatialRandom.cpp
  31.  *
  32.  * ...and for a Java version at:
  33.  * https://github.com/BlackJar72/ProcGenLab/blob/master/src/jaredbgreat/procgenlab/api/util/SpatialNoise.java
  34.  *
  35.  */
  36.  
  37. inline long rrotate(const long &in, const int &by) const {
  38.     return ((in >> by) | (in << (64 - by)));
  39. }
  40.  
  41. inline long lrotate(const long &in, const int &by) const {
  42.     return ((in << by) | (in >> (64 - by)));
  43. }
  44.  
  45.  
  46. unsigned long long SpatialNoise::longFor(const int &x, const int &y, const int &z, const int &t) const {
  47.     long long out = seed1 + (15485077L  * (long long)t)
  48.                           + (12338621L  * (long long)x)
  49.                           + (15485863L  * (long long)y)
  50.                           + (14416417L  * (long long)z);
  51.     long long alt = seed2 + (179424743L * (long long)t)
  52.                           + (154858637L * (long long)y)
  53.                           + (179426003L * (long long)x)
  54.                           + (179425819L * (long long)z);
  55.     alt ^= lrotate(alt, (x % 29) + 13);
  56.     alt ^= rrotate(alt, (y % 31) + 7);
  57.     alt ^= lrotate(alt, (z % 23) + 19);
  58.     alt ^= rrotate(alt, (t % 43) + 11);
  59.     out ^= lrotate(out, ((x & 0x7fffffff) % 13) + 5);
  60.     out ^= rrotate(out, ((y & 0x7fffffff) % 11) + 28);
  61.     out ^= lrotate(out, ((z & 0x7fffffff) % 7) + 13);
  62.     out ^= rrotate(out, ((t & 0x7ffffff)% 17) + 45);
  63.     return (out ^ alt);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement