Advertisement
timeshifter

Untitled

Apr 29th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 KB | None | 0 0
  1.         static double[] cos = new double[]{
  2.             1,0.99992,0.99968,0.99928,0.99873,0.99801,0.99714,0.9961,0.99491,0.99356,0.99205,0.99038,0.98856,0.98658,0.98444,0.98215,0.97969,0.97709,0.97432,0.9714,0.96833,0.9651,0.96172,0.95819,0.9545,0.95067,
  3.             0.94668,0.94254,0.93825,0.93381,0.92922,0.92448,0.9196,0.91457,0.90939,0.90407,0.89861,0.893,0.88725,0.88136,0.87533,0.86916,0.86285,0.85641,0.84983,0.84311,0.83626,0.82927,0.82216,0.81491,0.80753,
  4.             0.80003,0.79239,0.78463,0.77675,0.76874,0.76061,0.75236,0.74399,0.7355,0.72689,0.71817,0.70933,0.70038,0.69132,0.68215,0.67287,0.66349,0.65399,0.6444,0.6347,0.6249,0.615,0.605,0.59491,0.58472,
  5.             0.57444,0.56406,0.5536,0.54305,0.53241,0.52169,0.51089,0.5,0.48903,0.47799,0.46687,0.45568,0.44441,0.43307,0.42167,0.41019,0.39865,0.38705,0.37539,0.36366,0.35188,0.34004,0.32815,0.31621,0.30421,
  6.             0.29217,0.28008,0.26795,0.25577,0.24355,0.2313,0.21901,0.20668,0.19432,0.18193,0.1695,0.15706,0.14459,0.13209,0.11957,0.10704,0.09449,0.08192,0.06934,0.05675,0.04414,0.03154,0.01892,0.00631,-0.00631,
  7.             -0.01892,-0.03154,-0.04414,-0.05675,-0.06934,-0.08192,-0.09449,-0.10704,-0.11957,-0.13209,-0.14459,-0.15706,-0.1695,-0.18193,-0.19432,-0.20668,-0.21901,-0.2313,-0.24355,-0.25577,-0.26795,-0.28008,-0.29217,-0.30421,-0.31621,
  8.             -0.32815,-0.34004,-0.35188,-0.36366,-0.37539,-0.38705,-0.39865,-0.41019,-0.42167,-0.43307,-0.44441,-0.45568,-0.46687,-0.47799,-0.48903,-0.5,-0.51089,-0.52169,-0.53241,-0.54305,-0.5536,-0.56406,-0.57444,-0.58472,-0.59491,
  9.             -0.605,-0.615,-0.6249,-0.6347,-0.6444,-0.65399,-0.66349,-0.67287,-0.68215,-0.69132,-0.70038,-0.70933,-0.71817,-0.72689,-0.7355,-0.74399,-0.75236,-0.76061,-0.76874,-0.77675,-0.78463,-0.79239,-0.80003,-0.80753,-0.81491,
  10.             -0.82216,-0.82927,-0.83626,-0.84311,-0.84983,-0.85641,-0.86285,-0.86916,-0.87533,-0.88136,-0.88725,-0.893,-0.89861,-0.90407,-0.90939,-0.91457,-0.9196,-0.92448,-0.92922,-0.93381,-0.93825,-0.94254,-0.94668,-0.95067,-0.9545,
  11.             -0.95819,-0.96172,-0.9651,-0.96833,-0.9714,-0.97432,-0.97709,-0.97969,-0.98215,-0.98444,-0.98658,-0.98856,-0.99038,-0.99205,-0.99356,-0.99491,-0.9961,-0.99714,-0.99801,-0.99873,-0.99928,-0.99968,-0.99992,-1};
  12.  
  13.  
  14.         public static byte[, ,] GetChunk(int worldChunkX, int worldChunkZ) {
  15.             double
  16.                 min = 0.0,
  17.                 max = 1.0,
  18.                 fx, fy, fz, fxinv, fyinv, xinterp1, xinterp2, xinterp3, xinterp4, yinterp1, yinterp2, incdiv;
  19.             int
  20.                 maxOctaves = 4,
  21.                 seed = 62476,
  22.                 worldStartX = worldChunkX * 16,
  23.                 worldStartZ = worldChunkZ * 16,
  24.                 zoneStartX = 0,
  25.                 zoneStartZ = 0,
  26.                 count = 0,
  27.                 x, y, z, z0, z1, xBound, yBound,  depth, noisex, noisey, noisez, xSize, ySize, xySize, n_x, n_y, n1_x, n1_y, m_x, m_y, n1_z, zoneDiffX, zoneDiffZ, m_z;
  28.  
  29.             //double[] yGuide = new double[] { 1.0, -1, -1, -1, 1.0 };
  30.             //double[] yGuide = new double[] { 0.0, -1, -1, 0.2, 1.0 };
  31.             //double[] yGuide = new double[] { 1.0, -1, -1, -1, 0.0, -1, -1, -1, 1.0 };
  32.             double[] yGuide = new double[] { 1.0, -1, 0.25, -1, 1.0 };
  33.             //double[] yGuide = new double[] { 0.0, -1, 0.2, -1, 0.0 };
  34.             //double[] yGuide = new double[] { 1.0, -1, -1, 0.4, -1, 0.3, -1, -1, 1.0 };
  35.  
  36.             //double[] cos = new double[250];
  37.             //for (int i = 0; i <= cos.GetUpperBound(0); i++)
  38.             //    cos[i] = Math.Cos(((double)i / (double)cos.GetUpperBound(0)) * Math.PI);
  39.  
  40.  
  41.             double[] noise;
  42.             double[] map = new double[16 * 512 * 16];
  43.             byte[, ,] ret = new byte[16, 256, 16];
  44.  
  45.             int frequency = yGuide.GetUpperBound(0) / 2,
  46.                 increment = 256 / frequency;
  47.             double amplitude = (max - min) / 2.0,
  48.                 midpoint = min + amplitude,
  49.                 noiseMod = midpoint;
  50.  
  51.             bool first = true, hasGrass = false; ;
  52.  
  53.             Random R;
  54.  
  55.             //TimeSpan timeNoise = new TimeSpan(0);
  56.  
  57.             while (increment > 1 && count < maxOctaves) {
  58.  
  59.                 //calculate start coords for the zone covering the requested chunk at the current iteration
  60.                 zoneStartX = (int)Math.Floor((double)worldStartX / (double)increment) * increment;
  61.                 zoneStartZ = (int)Math.Floor((double)worldStartZ / (double)increment) * increment;
  62.  
  63.  
  64.                 xSize = (int)Math.Ceiling(16.0 / increment) + 1;
  65.                 ySize = (frequency * 2) + 1;
  66.  
  67.                 xBound = xSize - 1;
  68.                 yBound = ySize - 1;
  69.                 xySize = (xSize * ySize);
  70.                 noise = new double[xSize * xySize];
  71.                 m_y = increment * 721;
  72.                 //generate noise map
  73.                 for (x = 0; x <= xBound; x++) {
  74.                     n_x = x * xySize;
  75.                     m_x = (zoneStartX + (x * increment)) * 13758;
  76.                     for (z = 0; z <= xBound; z++) {
  77.                         m_z = (zoneStartZ + (z * increment)) * 6124773;
  78.                         for (y = 0; y <= yBound; y++) {
  79.                             n_y = y * xSize;
  80.                             R = new Random(seed + m_x  + (y * m_y) + m_z);
  81.                             noise[n_x + n_y + z] = (R.NextDouble() * amplitude * (R.Next(2) == 0 ? -1 : 1)) + noiseMod;
  82.                         }
  83.                     }
  84.                 }
  85.  
  86.                 //apply specified y guide rules
  87.                 if (first) {
  88.                     for (y = 0; y <= yBound; y++) {
  89.                         n_y = y * xSize;
  90.                         if (yGuide[y] >= 0) {
  91.                             for (x = 0; x <= xBound; x++) {
  92.                                 n_x = x * xySize;
  93.                                 m_z = n_x + n_y;
  94.                                 for (z = 0; z <= xBound; z++) {
  95.                                     noise[m_z + z] = yGuide[y];
  96.                                 }
  97.                             }
  98.                         }
  99.                     }
  100.                     first = false;
  101.                 }
  102.                 zoneDiffX = worldStartX - zoneStartX;
  103.                 zoneDiffZ = worldStartZ - zoneStartZ;
  104.                 incdiv = (double)cos.Length / increment;
  105.                 for (x = 0; x < 16; x++) {
  106.                     noisex = x / increment;
  107.                     fx = (1.0 - cos[(int)(((double)((x % increment) + zoneDiffX)) * incdiv)]) * 0.5;
  108.                     fxinv = 1.0 - fx;
  109.                     n_x = noisex * xySize;
  110.                     n1_x = n_x + xySize;
  111.                     m_x = x * 8192;
  112.                     for (y = 0; y < 256; y ++) {
  113.                         noisey = y*2 / increment;
  114.                         fy = (1.0 - cos[(int)(((double)(y*2 % increment)) * incdiv)]) * 0.5;
  115.                         fyinv = 1.0 - fy;
  116.                         n_y = noisey * xSize;
  117.                         n1_y = n_y + xSize;
  118.                         m_z = m_x + y*32;
  119.                         for (noisez = 0; noisez < xBound; noisez++) {
  120.                             n1_z = noisez + 1;
  121.                             xinterp1 = noise[n_x + n_y + noisez] * fxinv + noise[n1_x + n_y + noisez] * fx;
  122.                             xinterp2 = noise[n_x + n1_y + noisez] * fxinv + noise[n1_x + n1_y + noisez] * fx;
  123.                             xinterp3 = noise[n_x + n_y + n1_z] * fxinv + noise[n1_x + n_y + n1_z] * fx;
  124.                             xinterp4 = noise[n_x + n1_y + n1_z] * fxinv + noise[n1_x + n1_y + n1_z] * fx;
  125.  
  126.                             yinterp1 = xinterp1 * fyinv + xinterp2 * fy;
  127.                             yinterp2 = xinterp3 * fyinv + xinterp4 * fy;
  128.  
  129.                             z0 = noisez * increment;
  130.                             z1 = Math.Min(16, z0 + increment);
  131.  
  132.                             for (z = z0; z < z1; z++) {
  133.                                 fz = (1.0 - cos[(int)((double)((z % increment) + zoneDiffZ) * incdiv)]) * 0.5;
  134.                                 map[m_z + z] += yinterp1 * (1.0 - fz) + yinterp2 * fz;
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.  
  140.                 amplitude *= 0.5; //persistence assumed to be 1.0
  141.                 count++;
  142.                 frequency *= 2;
  143.                 increment /= 2;
  144.                 noiseMod = 0;
  145.             }
  146.  
  147.  
  148.             //determine block type based on threshhold and depth
  149.             for (x = 0; x < 16; x++) {
  150.                 m_x = x * 8192;
  151.                 for (z = 0; z < 16; z++) {
  152.                     m_z = m_x + z;
  153.                     depth = 0;
  154.                     hasGrass = false;
  155.                     for (y = 255; y >= 0; y--) {
  156.                         m_y = (y * 2) * 16;
  157.                         if (map[m_z + m_y] > 0.25) { //threshhold
  158.                             ret[x, y, z] = 0;
  159.                             depth = 0;
  160.                         }
  161.                         else {
  162.                             if (++depth == 1 && !hasGrass) {
  163.                                 ret[x, y, z] = 2;
  164.                                 hasGrass = true;
  165.                             }
  166.                             else if (depth < 5)
  167.                                 ret[x, y, z] = 3;
  168.                             else
  169.                                 ret[x, y, z] = 1;
  170.  
  171.                         }
  172.  
  173.                     }
  174.                 }
  175.             }
  176.  
  177.             return ret;
  178.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement