some meshgen and map rendering updates
[voxelands-alt.git] / src / map / mapgen_noise.c
blob7445e909d5318b23af123d9fb9450ac119c4f28b
1 /************************************************************************
2 * mapgen_noise.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
22 #include <math.h>
24 static int seed = 12345;
26 static float noise(int x, int z)
28 int a = (x*43695)+(z*43721)+seed;
29 int b = (a>>13)^a;
31 float d = ((float)(b%1000)/1000.0);
34 d = (d*2.0)-1.0;
36 return d;
39 static float smooth_noise(int x, int z)
41 float sides;
42 float centre;
44 sides = noise(x+1,z);
45 sides += noise(x-1,z);
46 sides += noise(x,z+1);
47 sides += noise(x,z-1);
48 sides /= 8.0;
50 centre = noise(x,z);
51 centre /= 4.0;
53 return (sides+centre);
56 static float interpolated_noise(int x, int z, float devisor)
58 float v1;
59 float v2;
60 float v3;
61 float v4;
62 float i1;
63 float i2;
64 float fx = (float)x/devisor;
65 float fz = (float)z/devisor;
66 int ix = fx;
67 int iz = fz;
68 if (x<1)
69 ix--;
70 if (z<1)
71 iz--;
72 fx -= ix;
73 fz -= iz;
75 v1 = smooth_noise(ix,iz);
76 v2 = smooth_noise(ix+1,iz);
77 v3 = smooth_noise(ix,iz+1);
78 v4 = smooth_noise(ix+1,iz+1);
80 i1 = (v1*(1.0-fx))+(v2*fx);
81 i2 = (v3*(1.0-fx))+(v4*fx);
83 return (i1*(1.0-fz))+(i2*fz);
86 void mapgen_seed(int s)
88 seed = s;
91 float noise_height(int x, int z)
93 float n = interpolated_noise(x,z,60.0)*70.0;
95 n += interpolated_noise(x,z,20.0)*20.0;
96 n += interpolated_noise(x,z,10.0)*4.0;
98 return n;