4 void fractalnoise(byte *noise, int size, int startgrid)
6 int x, y, g, g2, amplitude, min, max, size1 = size - 1, sizepower, gridpower;
8 #define n(x,y) noisebuf[((y)&size1)*size+((x)&size1)]
10 for (sizepower = 0;(1 << sizepower) < size;sizepower++);
11 if (size != (1 << sizepower))
12 Sys_Error("fractalnoise: size must be power of 2\n");
14 for (gridpower = 0;(1 << gridpower) < startgrid;gridpower++);
15 if (startgrid != (1 << gridpower))
16 Sys_Error("fractalnoise: grid must be power of 2\n");
18 startgrid = bound(0, startgrid, size);
20 amplitude = 0xFFFF; // this gets halved before use
21 noisebuf = Mem_Alloc(tempmempool, size*size*sizeof(int));
22 memset(noisebuf, 0, size*size*sizeof(int));
24 for (g2 = startgrid;g2;g2 >>= 1)
26 // brownian motion (at every smaller level there is random behavior)
28 for (y = 0;y < size;y += g2)
29 for (x = 0;x < size;x += g2)
30 n(x,y) += (rand()&litude);
35 // subdivide, diamond-square algorythm (really this has little to do with squares)
37 for (y = 0;y < size;y += g2)
38 for (x = 0;x < size;x += g2)
39 n(x+g,y+g) = (n(x,y) + n(x+g2,y) + n(x,y+g2) + n(x+g2,y+g2)) >> 2;
41 for (y = 0;y < size;y += g2)
42 for (x = 0;x < size;x += g2)
44 n(x+g,y) = (n(x,y) + n(x+g2,y) + n(x+g,y-g) + n(x+g,y+g)) >> 2;
45 n(x,y+g) = (n(x,y) + n(x,y+g2) + n(x-g,y+g) + n(x+g,y+g)) >> 2;
49 // find range of noise values
51 for (y = 0;y < size;y++)
52 for (x = 0;x < size;x++)
54 if (n(x,y) < min) min = n(x,y);
55 if (n(x,y) > max) max = n(x,y);
59 // normalize noise and copy to output
60 for (y = 0;y < size;y++)
61 for (x = 0;x < size;x++)
62 *noise++ = (byte) (((n(x,y) - min) * 256) / max);