Advertisement
JeWe37

Untitled

Apr 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. struct Layer {
  2.     int64 baseSeed;  // Generator seed (depends only on hierarchy of generator)
  3.     int64 worldSeed; // based on the seed of the world
  4.  
  5.     int64 chunkSeed; // randomiser seed
  6.  
  7.     int scale;      // map scale of this layer (map entry = scale x scale blocks)
  8.  
  9.     void (*getMap)(Layer *uniform l, uniform int out[], uniform int areaX, uniform int areaZ, uniform int areaWidth, uniform int areaHeight);
  10.  
  11.     Layer *p, *p2;
  12. };
  13.  
  14. static inline void setChunkSeed(Layer *layer, int64 chunkX, int64 chunkZ)
  15. {
  16.     layer->chunkSeed =  layer->worldSeed;
  17.     layer->chunkSeed *= layer->chunkSeed * 6364136223846793005L + 1442695040888963407L;
  18.     layer->chunkSeed += chunkX;
  19.     layer->chunkSeed *= layer->chunkSeed * 6364136223846793005L + 1442695040888963407L;
  20.     layer->chunkSeed += chunkZ;
  21.     layer->chunkSeed *= layer->chunkSeed * 6364136223846793005L + 1442695040888963407L;
  22.     layer->chunkSeed += chunkX;
  23.     layer->chunkSeed *= layer->chunkSeed * 6364136223846793005L + 1442695040888963407L;
  24.     layer->chunkSeed += chunkZ;
  25. }
  26.  
  27. static inline int mcNextInt(Layer *layer, int mod)
  28. {
  29.     int ret = (int)((layer->chunkSeed >> 24) & (int64)mod);
  30.  
  31.     if (ret < 0)
  32.     {
  33.         ret += mod;
  34.     }
  35.  
  36.     layer->chunkSeed *= layer->chunkSeed * 6364136223846793005L + 1442695040888963407L;
  37.     layer->chunkSeed += layer->worldSeed;
  38.     return ret;
  39. }
  40.  
  41. static inline int selectRandom2(Layer *l, int a1, int a2)
  42. {
  43.     int i = mcNextInt(l, 0x1);
  44.     return i == 0 ? a1 : a2;
  45. }
  46.  
  47. static inline int selectRandom4(Layer *l, int a1, int a2, int a3, int a4)
  48. {
  49.     int i = mcNextInt(l, 0x3);
  50.     return i == 0 ? a1 : i == 1 ? a2 : i == 2 ? a3 : a4;
  51. }
  52.  
  53. static inline int selectModeOrRandom(Layer *l, int a1, int a2, int a3, int a4)
  54. {
  55.     int rndarg = selectRandom4(l, a1, a2, a3, a4);
  56.  
  57.     if(a2 == a3 && a3 == a4) return a2;
  58.     if(a1 == a2 && a1 == a3) return a1;
  59.     if(a1 == a2 && a1 == a4) return a1;
  60.     if(a1 == a3 && a1 == a4) return a1;
  61.     if(a1 == a2 && a3 != a4) return a1;
  62.     if(a1 == a3 && a2 != a4) return a1;
  63.     if(a1 == a4 && a2 != a3) return a1;
  64.     if(a2 == a3 && a1 != a4) return a2;
  65.     if(a2 == a4 && a1 != a3) return a2;
  66.     if(a3 == a4 && a1 != a2) return a3;
  67.  
  68.     return rndarg;
  69. }
  70.  
  71. extern "C" void mapIsland(Layer *uniform l, uniform int out[], uniform int areaX, uniform int areaZ, uniform int areaWidth, uniform int areaHeight);
  72.  
  73. export void mapZoom(Layer *uniform l, uniform int out[], uniform int areaX, uniform int areaZ, uniform int areaWidth, uniform int areaHeight) {
  74.     uniform int pX = areaX >> 1;
  75.     uniform int pZ = areaZ >> 1;
  76.     uniform int pWidth =  (areaWidth >> 1) + 2;
  77.     uniform int pHeight = (areaHeight >> 1) + 2;
  78.     uniform int z;
  79.  
  80.     Layer* pre = l->p;
  81.  
  82.     pre->getMap(l->p, out, pX, pZ, pWidth, pHeight);
  83.  
  84.     int (*selectRand)(Layer *l, int a1, int a2, int a3, int a4) = ((void*)pre->getMap == (void*)mapIsland) ? selectRandom4 : selectModeOrRandom;
  85.  
  86.     uniform int newWidth = (pWidth-1) << 1;
  87.     uniform int newHeight = (pHeight-1) << 1;
  88.     int idx, a, b, a1, b1;
  89.     uniform int *buf = new int[(newWidth+1)*(newHeight+1)*sizeof(int)];
  90.  
  91.     for(z = 0; z < pHeight - 1; z++)
  92.     {
  93.         idx = (z << 1) * newWidth;
  94.         a = out[(z+0)*pWidth];
  95.         b = out[(z+1)*pWidth];
  96.  
  97.         foreach (x = 0 ... pWidth-1)
  98.         {
  99.             setChunkSeed(l, (int64)((x + pX) << 1), (int64)((z + pZ) << 1));
  100.             a1 = out[x+1 + (z+0)*pWidth];
  101.             b1 = out[x+1 + (z+1)*pWidth];
  102.             buf[idx] = a;
  103.             buf[idx + newWidth] = selectRandom2(l, a, b);
  104.             idx++;
  105.             buf[idx] = selectRandom2(l, a, a1);
  106.             buf[idx + newWidth] = selectRand(l, a, a1, b, b1);
  107.             idx++;
  108.             a = a1;
  109.             b = b1;
  110.         }
  111.     }
  112.  
  113.     for(z = 0; z < areaHeight; z++)
  114.     {
  115.         memcpy(&out[z*areaWidth], &buf[(z + (areaZ & 1))*newWidth + (areaX & 1)], areaWidth*sizeof(int));
  116.     }
  117.  
  118.     delete[] buf;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement