hedgewars/uLandGenPerlin.pas
author unc0rr
Fri, 07 Mar 2014 23:36:56 +0400
changeset 10181 4708343d5963
child 10182 9d34898b22f7
permissions -rw-r--r--
Perlin noise generator untweaked, temporarily replacing maze generator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10181
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     1
{$INCLUDE "options.inc"}
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     2
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     3
unit uLandGenPerlin;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     4
interface
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     5
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     6
procedure GenPerlin;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     7
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     8
implementation
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
     9
uses uVariables, uConsts, math; // for min()
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    10
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    11
var fadear: array[byte] of LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    12
    p: array[0..511] of LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    13
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    14
function fade(t: LongInt) : LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    15
var t0, t1: LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    16
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    17
    t0:= fadear[t shr 8];
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    18
    t1:= fadear[min(255, t shr 8 + 1)];
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    19
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    20
    fade:= t0 + ((t and 255) * (t1 - t0) shr 8)
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    21
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    22
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    23
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    24
function lerp(t, a, b: LongInt) : LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    25
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    26
    lerp:= a + (t * (b - a) shr 12)
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    27
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    28
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    29
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    30
function grad(hash, x, y, z: LongInt) : LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    31
var h, v, u: LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    32
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    33
    h:= hash and 15;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    34
    if h < 8 then u:= x else u:= y;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    35
    if h < 4 then v:= y else
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    36
        if (h = 12) or (h = 14) then v:= x else v:= z;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    37
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    38
    if odd(h) then u:= -u;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    39
    if odd(h shr 1) then v:= -v;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    40
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    41
    grad:= u + v
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    42
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    43
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    44
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    45
function inoise(x, y, z: LongInt) : LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    46
const N = $10000;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    47
var xx, yy, zz, u, v, w, A, AA, AB, B, BA, BB: LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    48
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    49
    xx:= (x shr 16) and 255;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    50
    yy:= (y shr 16) and 255;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    51
    zz:= (z shr 16) and 255;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    52
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    53
    x:= x and $FFFF;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    54
    y:= y and $FFFF;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    55
    z:= z and $FFFF;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    56
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    57
    u:= fade(x);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    58
    v:= fade(y);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    59
    w:= fade(z);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    60
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    61
    A:= p[xx    ] + yy; AA:= p[A] + zz; AB:= p[A + 1] + zz;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    62
    B:= p[xx + 1] + yy; BA:= p[B] + zz; BB:= p[B + 1] + zz;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    63
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    64
    inoise:=
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    65
 lerp(w, lerp(v, lerp(u, grad(p[AA  ], x   , y   , z   ),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    66
                                     grad(p[BA  ], x-N , y   , z   )),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    67
                             lerp(u, grad(p[AB  ], x   , y-N , z   ),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    68
                                     grad(p[BB  ], x-N , y-N , z   ))),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    69
                     lerp(v, lerp(u, grad(p[AA+1], x   , y   , z-N ),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    70
                                     grad(p[BA+1], x-N , y   , z-N )),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    71
                             lerp(u, grad(p[AB+1], x   , y-N , z-N ),
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    72
                                     grad(p[BB+1], x-N , y-N , z-N ))));
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    73
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    74
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    75
function f(t: double): double;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    76
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    77
    f:= t * t * t * (t * (t * 6 - 15) + 10);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    78
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    79
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    80
const permutation: array[byte] of LongInt = ( 151,160,137,91,90,15,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    81
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    82
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    83
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    84
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    85
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    86
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    87
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    88
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    89
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    90
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    91
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    92
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    93
   );
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    94
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    95
procedure inoise_setup();
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    96
var i: LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    97
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    98
    for i:= 0 to 255 do
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
    99
        begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   100
        p[256 + i]:= permutation[i];
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   101
        p[i]:= permutation[i]
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   102
        end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   103
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   104
    for i:= 0 to 255 do
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   105
        fadear[i]:= trunc($1000 * f(i / 256));
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   106
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   107
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   108
const detail = 120000*3;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   109
    field = 3;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   110
    width = 4096;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   111
    height = 2048;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   112
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   113
procedure GenPerlin;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   114
var y, x, di, dj, r: LongInt;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   115
begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   116
    inoise_setup();
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   117
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   118
    for y:= 0 to pred(height) do
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   119
    begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   120
        di:= detail * field * y div height;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   121
        for x:= 0 to pred(width) do
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   122
        begin
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   123
            dj:= detail * field * x div width;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   124
            r:= (abs(inoise(di, dj, detail*field)) + y*4) mod 65536 div 256;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   125
            r:= r - max(0, abs(x - width div 2) - width * 45 div 100);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   126
            //r:= r - max(0, - abs(x - width div 2) + width * 2 div 100);
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   127
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   128
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   129
            r:= r + (trunc(1000 - sqrt(sqr(x - (width div 2)) * 4 + sqr(y - height * 5 div 4) * 22))) div 600 * 20;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   130
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   131
            if r < 0 then Land[y, x]:= 0 else Land[y, x]:= lfBasic;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   132
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   133
        end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   134
    end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   135
end;
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   136
4708343d5963 Perlin noise generator untweaked, temporarily replacing maze generator
unc0rr
parents:
diff changeset
   137
end.