hedgewars/uLandGenPerlin.pas
changeset 10188 e8f2dbabd01b
parent 10187 0d506346c1f0
child 10189 875607ce793d
equal deleted inserted replaced
10187:0d506346c1f0 10188:e8f2dbabd01b
     6 procedure GenPerlin;
     6 procedure GenPerlin;
     7 
     7 
     8 implementation
     8 implementation
     9 uses uVariables, uConsts, uRandom, math; // for min()
     9 uses uVariables, uConsts, uRandom, math; // for min()
    10 
    10 
    11 var fadear: array[byte] of LongInt;
    11 var p: array[0..511] of LongInt;
    12     p: array[0..511] of LongInt;
    12 
       
    13 const fadear: array[byte] of LongInt = 
       
    14 (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 6, 7, 9, 10, 12,
       
    15 14, 17, 19, 22, 25, 29, 32, 36, 40, 45, 49, 54, 60, 65, 71,
       
    16 77, 84, 91, 98, 105, 113, 121, 130, 139, 148, 158, 167, 178,
       
    17 188, 199, 211, 222, 234, 247, 259, 273, 286, 300, 314, 329, 344,
       
    18 359, 374, 390, 407, 424, 441, 458, 476, 494, 512, 531, 550,
       
    19 570, 589, 609, 630, 651, 672, 693, 715, 737, 759, 782, 805, 828,
       
    20 851, 875, 899, 923, 948, 973, 998, 1023, 1049, 1074, 1100, 1127,
       
    21 1153, 1180, 1207, 1234, 1261, 1289, 1316, 1344, 1372, 1400, 1429,
       
    22 1457, 1486, 1515, 1543, 1572, 1602, 1631, 1660, 1690, 1719, 1749,
       
    23 1778, 1808, 1838, 1868, 1898, 1928, 1958, 1988, 2018, 2048, 2077,
       
    24 2107, 2137, 2167, 2197, 2227, 2257, 2287, 2317, 2346, 2376, 2405,
       
    25 2435, 2464, 2493, 2523, 2552, 2580, 2609, 2638, 2666, 2695, 2723,
       
    26 2751, 2779, 2806, 2834, 2861, 2888, 2915, 2942, 2968, 2995, 3021,
       
    27 3046, 3072, 3097, 3122, 3147, 3172, 3196, 3220, 3244, 3267, 3290,
       
    28 3313, 3336, 3358, 3380, 3402, 3423, 3444, 3465, 3486, 3506, 3525,
       
    29 3545, 3564, 3583, 3601, 3619, 3637, 3654, 3672, 3688, 3705, 3721,
       
    30 3736, 3751, 3766, 3781, 3795, 3809, 3822, 3836, 3848, 3861, 3873,
       
    31 3884, 3896, 3907, 3917, 3928, 3937, 3947, 3956, 3965, 3974, 3982,
       
    32 3990, 3997, 4004, 4011, 4018, 4024, 4030, 4035, 4041, 4046, 4050,
       
    33 4055, 4059, 4063, 4066, 4070, 4073, 4076, 4078, 4081, 4083, 4085,
       
    34 4086, 4088, 4089, 4091, 4092, 4092, 4093, 4094, 4094, 4095, 4095,
       
    35 4095, 4095, 4095, 4095, 4095);
    13 
    36 
    14 function fade(t: LongInt) : LongInt; inline;
    37 function fade(t: LongInt) : LongInt; inline;
    15 var t0, t1: LongInt;
    38 var t0, t1: LongInt;
    16 begin
    39 begin
    17     t0:= fadear[t shr 8];
    40     t0:= fadear[t shr 8];
    25 begin
    48 begin
    26     lerp:= a + (t * (b - a) shr 12)
    49     lerp:= a + (t * (b - a) shr 12)
    27 end;
    50 end;
    28 
    51 
    29 
    52 
    30 function grad(hash, x, y: LongInt) : LongInt;
    53 function grad(hash, x, y: LongInt) : LongInt; inline;
    31 var h, v, u: LongInt;
    54 var h, v, u: LongInt;
    32 begin
    55 begin
    33     h:= hash and 15;
    56     h:= hash and 15;
    34     if h < 8 then u:= x else u:= y;
    57     if h < 8 then u:= x else u:= y;
    35     if h < 4 then v:= y else
    58     if h < 4 then v:= y else
    40 
    63 
    41     grad:= u + v
    64     grad:= u + v
    42 end;
    65 end;
    43 
    66 
    44 
    67 
    45 function inoise(x, y: LongInt) : LongInt;
    68 function inoise(x, y: LongInt) : LongInt; inline;
    46 const N = $10000;
    69 const N = $10000;
    47 var xx, yy, u, v, A, AA, AB, B, BA, BB: LongInt;
    70 var xx, yy, u, v, A, AA, AB, B, BA, BB: LongInt;
    48 begin
    71 begin
    49     xx:= (x shr 16) and 255;
    72     xx:= (x shr 16) and 255;
    50     yy:= (y shr 16) and 255;
    73     yy:= (y shr 16) and 255;
    63                             grad(p[BA  ], x-N , y  )),
    86                             grad(p[BA  ], x-N , y  )),
    64                     lerp(u, grad(p[AB  ], x   , y-N),
    87                     lerp(u, grad(p[AB  ], x   , y-N),
    65                             grad(p[BB  ], x-N , y-N)));
    88                             grad(p[BB  ], x-N , y-N)));
    66 end;
    89 end;
    67 
    90 
    68 function f(t: double): double;
    91 function f(t: double): double; inline;
    69 begin
    92 begin
    70     f:= t * t * t * (t * (t * 6 - 15) + 10);
    93     f:= t * t * t * (t * (t * 6 - 15) + 10);
    71 end;
    94 end;
    72 
    95 
    73 procedure inoise_setup();
    96 procedure inoise_setup();
    85         p[ii]:= t
   108         p[ii]:= t
    86     end;
   109     end;
    87 
   110 
    88     for i:= 0 to 255 do
   111     for i:= 0 to 255 do
    89         p[256 + i]:= p[i];
   112         p[256 + i]:= p[i];
    90 
       
    91     for i:= 0 to 255 do
       
    92         fadear[i]:= trunc($1000 * f(i / 256));
       
    93 end;
   113 end;
    94 
   114 
    95 const detail = 120000*3;
   115 const detail = 120000*3;
    96     field = 3;
   116     field = 3;
       
   117     df = detail * field;
    97     width = 4096;
   118     width = 4096;
    98     height = 2048;
   119     height = 2048;
    99     bottomPlateHeight = 90;
   120     bottomPlateHeight = 90;
   100     bottomPlateMargin = 1200;
   121     bottomPlateMargin = 1200;
   101     plateFactor = 1;
   122     plateFactor = 1;
   105 begin
   126 begin
   106     inoise_setup();
   127     inoise_setup();
   107 
   128 
   108     for y:= 1024 to pred(height) do
   129     for y:= 1024 to pred(height) do
   109     begin
   130     begin
   110         di:= detail * field * y div height;
   131         di:= df * y div height;
   111         for x:= 0 to pred(width) do
   132         for x:= 0 to pred(width) do
   112         begin
   133         begin
   113             dj:= detail * field * x div width;
   134             dj:= df * x div width;
   114             r:= (abs(inoise(di, dj))) shr 8 and $ff;
   135             r:= (abs(inoise(di, dj))) shr 8 and $ff;
   115             //r:= r - max(0, abs(x - width div 2) - width * 55 div 128); // fade on edges
   136             //r:= r - max(0, abs(x - width div 2) - width * 55 div 128); // fade on edges
   116             //r:= r - max(0, - abs(x - width div 2) + width * 2 div 100); // split vertically in the middle
   137             //r:= r - max(0, - abs(x - width div 2) + width * 2 div 100); // split vertically in the middle
   117 
   138 
   118 
   139