]> git.lizzy.rs Git - minetest.git/blob - src/mapgen_indev.cpp
Disable fall bobbing by default; enable using fall_bobbing_amount = 1.0
[minetest.git] / src / mapgen_indev.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mapgen_indev.h"
21 #include "constants.h"
22 #include "map.h"
23 #include "main.h"
24 #include "util/numeric.h"
25 #include "log.h"
26
27 /////////////////// Mapgen Indev perlin noise (default values - not used, from config or defaultsettings)
28
29 NoiseIndevParams nparams_indev_def;
30
31 /*
32 NoiseIndevParams nparams_indev_def_terrain_base;
33 //      (-AVERAGE_MUD_AMOUNT, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6, 1);
34 NoiseIndevParams nparams_indev_def_terrain_higher;
35 //      (20.0, 16.0, v3f(500.0, 500.0, 500.0), 85039, 5, 0.6, 1);
36 NoiseIndevParams nparams_indev_def_steepness;
37 //      (0.85, 0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7, 1);
38 NoiseIndevParams nparams_indev_def_mud;
39 //      (AVERAGE_MUD_AMOUNT, 2.0, v3f(200.0, 200.0, 200.0), 91013, 3, 0.55, 1);
40 NoiseIndevParams nparams_indev_def_float_islands;
41 //      (1, 10.0, v3f(500.0, 500.0, 500.0), 32451, 5, 0.6, 1);
42 NoiseIndevParams nparams_indev_def_biome;
43 */
44
45 ///////////////////////////////////////////////////////////////////////////////
46
47 void NoiseIndev::init(NoiseIndevParams *np, int seed, int sx, int sy, int sz) {
48         Noise::init((NoiseParams*)np, seed, sx, sy, sz);
49         this->npindev   = np;
50 }
51
52 NoiseIndev::NoiseIndev(NoiseIndevParams *np, int seed, int sx, int sy) : Noise(np, seed, sx, sy) {
53         init(np, seed, sx, sy, 1);
54 }
55
56 NoiseIndev::NoiseIndev(NoiseIndevParams *np, int seed, int sx, int sy, int sz) : Noise(np, seed, sx, sy, sz) {
57         init(np, seed, sx, sy, sz);
58 }
59
60 float farscale(float scale, float z) {
61         return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 1 - (fabs(z))                     ) / (MAP_GENERATION_LIMIT * 1) ) * (scale - 1) );
62 }
63
64 float farscale(float scale, float x, float z) {
65         return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 2 - (fabs(x) + fabs(z))           ) / (MAP_GENERATION_LIMIT * 2) ) * (scale - 1) );
66 }
67
68 float farscale(float scale, float x, float y, float z) {
69         return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 3 - (fabs(x) + fabs(y) + fabs(z)) ) / (MAP_GENERATION_LIMIT * 3) ) * (scale - 1) );
70 }
71
72 void NoiseIndev::transformNoiseMapFarScale(float xx, float yy, float zz) {
73         int i = 0;
74         for (int z = 0; z != sz; z++) {
75                 for (int y = 0; y != sy; y++) {
76                         for (int x = 0; x != sx; x++) {
77                                 result[i] = result[i] * npindev->scale * farscale(npindev->farscale, xx, yy, zz) + npindev->offset;
78                                 i++;
79                         }
80                 }
81         }
82 }
83
84 MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge) 
85         : MapgenV6(mapgenid, params, emerge)
86 {
87         noiseindev_terrain_base   = new NoiseIndev(params->npindev_terrain_base,   seed, csize.X, csize.Z);
88         noiseindev_terrain_higher = new NoiseIndev(params->npindev_terrain_higher, seed, csize.X, csize.Z);
89         noiseindev_steepness      = new NoiseIndev(params->npindev_steepness,      seed, csize.X, csize.Z);
90 //        noise_height_select  = new Noise(params->np_height_select,  seed, csize.X, csize.Y);
91 //        noise_trees          = new Noise(params->np_trees,          seed, csize.X, csize.Y);
92         noiseindev_mud            = new NoiseIndev(params->npindev_mud,            seed, csize.X, csize.Z);
93 //        noise_beach          = new Noise(params->np_beach,          seed, csize.X, csize.Y);
94         noiseindev_float_islands1  = new NoiseIndev(params->npindev_float_islands1,  seed, csize.X, csize.Y, csize.Z);
95         noiseindev_float_islands2  = new NoiseIndev(params->npindev_float_islands2,  seed, csize.X, csize.Y, csize.Z);
96         noiseindev_float_islands3  = new NoiseIndev(params->npindev_float_islands3,  seed, csize.X, csize.Z);
97         noiseindev_biome          = new NoiseIndev(params->npindev_biome,          seed, csize.X, csize.Z);
98 }
99
100 MapgenIndev::~MapgenIndev() {
101         delete noiseindev_terrain_base;
102         delete noiseindev_terrain_higher;
103         delete noiseindev_steepness;
104         //delete noise_height_select;
105         //delete noise_trees;
106         delete noiseindev_mud;
107         //delete noise_beach;
108         delete noiseindev_float_islands1;
109         delete noiseindev_float_islands2;
110         delete noiseindev_float_islands3;
111         delete noiseindev_biome;
112 }
113
114 void MapgenIndev::calculateNoise() {
115         int x = node_min.X;
116         int y = node_min.Y;
117         int z = node_min.Z;
118         // Need to adjust for the original implementation's +.5 offset...
119         if (!(flags & MG_FLAT)) {
120                 noiseindev_terrain_base->perlinMap2D(
121                         x + 0.5 * noiseindev_terrain_base->npindev->spread.X * farscale(noiseindev_terrain_base->npindev->farspread, x, z),
122                         z + 0.5 * noiseindev_terrain_base->npindev->spread.Z * farscale(noiseindev_terrain_base->npindev->farspread, x, z));
123                 noiseindev_terrain_base->transformNoiseMapFarScale(x, y, z);
124
125                 noiseindev_terrain_higher->perlinMap2D(
126                         x + 0.5 * noiseindev_terrain_higher->npindev->spread.X * farscale(noiseindev_terrain_higher->npindev->farspread, x, z),
127                         z + 0.5 * noiseindev_terrain_higher->npindev->spread.Z * farscale(noiseindev_terrain_higher->npindev->farspread, x, z));
128                 noiseindev_terrain_higher->transformNoiseMapFarScale(x, y, z);
129
130                 noiseindev_steepness->perlinMap2D(
131                         x + 0.5 * noiseindev_steepness->npindev->spread.X * farscale(noiseindev_steepness->npindev->farspread, x, z),
132                         z + 0.5 * noiseindev_steepness->npindev->spread.Z * farscale(noiseindev_steepness->npindev->farspread, x, z));
133                 noiseindev_steepness->transformNoiseMapFarScale(x, y, z);
134
135                 noise_height_select->perlinMap2D(
136                         x + 0.5 * noise_height_select->np->spread.X,
137                         z + 0.5 * noise_height_select->np->spread.Z);
138
139                 noiseindev_float_islands1->perlinMap3D(
140                         x + 0.33 * noiseindev_float_islands1->npindev->spread.X * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z),
141                         y + 0.33 * noiseindev_float_islands1->npindev->spread.Y * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z),
142                         z + 0.33 * noiseindev_float_islands1->npindev->spread.Z * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z)
143                 );
144                 noiseindev_float_islands1->transformNoiseMapFarScale(x, y, z);
145
146                 noiseindev_float_islands2->perlinMap3D(
147                         x + 0.33 * noiseindev_float_islands2->npindev->spread.X * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z),
148                         y + 0.33 * noiseindev_float_islands2->npindev->spread.Y * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z),
149                         z + 0.33 * noiseindev_float_islands2->npindev->spread.Z * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z)
150                 );
151                 noiseindev_float_islands2->transformNoiseMapFarScale(x, y, z);
152
153                 noiseindev_float_islands3->perlinMap2D(
154                         x + 0.5 * noiseindev_float_islands3->npindev->spread.X * farscale(noiseindev_float_islands3->npindev->farspread, x, z),
155                         z + 0.5 * noiseindev_float_islands3->npindev->spread.Z * farscale(noiseindev_float_islands3->npindev->farspread, x, z));
156                 noiseindev_float_islands3->transformNoiseMapFarScale(x, y, z);
157
158                 noiseindev_mud->perlinMap2D(
159                         x + 0.5 * noiseindev_mud->npindev->spread.X * farscale(noiseindev_mud->npindev->farspread, x, y, z),
160                         z + 0.5 * noiseindev_mud->npindev->spread.Z * farscale(noiseindev_mud->npindev->farspread, x, y, z));
161                 noiseindev_mud->transformNoiseMapFarScale(x, y, z);
162         }
163         noise_beach->perlinMap2D(
164                 x + 0.2 * noise_beach->np->spread.X,
165                 z + 0.7 * noise_beach->np->spread.Z);
166
167         noise_biome->perlinMap2D(
168                 x + 0.6 * noiseindev_biome->npindev->spread.X * farscale(noiseindev_biome->npindev->farspread, x, z),
169                 z + 0.2 * noiseindev_biome->npindev->spread.Z * farscale(noiseindev_biome->npindev->farspread, x, z));
170 }
171
172 bool MapgenIndevParams::readParams(Settings *settings) {
173         freq_desert = settings->getFloat("mgv6_freq_desert");
174         freq_beach  = settings->getFloat("mgv6_freq_beach");
175
176         npindev_terrain_base   = settings->getNoiseIndevParams("mgindev_np_terrain_base");
177         npindev_terrain_higher = settings->getNoiseIndevParams("mgindev_np_terrain_higher");
178         npindev_steepness      = settings->getNoiseIndevParams("mgindev_np_steepness");
179         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
180         np_trees          = settings->getNoiseParams("mgv6_np_trees");
181         npindev_mud            = settings->getNoiseIndevParams("mgindev_np_mud");
182         np_beach          = settings->getNoiseParams("mgv6_np_beach");
183         npindev_biome     = settings->getNoiseIndevParams("mgindev_np_biome");
184         np_cave           = settings->getNoiseParams("mgv6_np_cave");
185         npindev_float_islands1  = settings->getNoiseIndevParams("mgindev_np_float_islands1");
186         npindev_float_islands2  = settings->getNoiseIndevParams("mgindev_np_float_islands2");
187         npindev_float_islands3  = settings->getNoiseIndevParams("mgindev_np_float_islands3");
188
189         bool success =
190                 npindev_terrain_base  && npindev_terrain_higher && npindev_steepness &&
191                 np_height_select && np_trees          && npindev_mud       &&
192                 np_beach         && np_biome          && np_cave &&
193                 npindev_float_islands1 && npindev_float_islands2 && npindev_float_islands3;
194         return success;
195 }
196
197 void MapgenIndevParams::writeParams(Settings *settings) {
198         settings->setFloat("mgv6_freq_desert", freq_desert);
199         settings->setFloat("mgv6_freq_beach",  freq_beach);
200
201         settings->setNoiseIndevParams("mgindev_np_terrain_base",   npindev_terrain_base);
202         settings->setNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);
203         settings->setNoiseIndevParams("mgindev_np_steepness",      npindev_steepness);
204         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
205         settings->setNoiseParams("mgv6_np_trees",          np_trees);
206         settings->setNoiseIndevParams("mgindev_np_mud",            npindev_mud);
207         settings->setNoiseParams("mgv6_np_beach",          np_beach);
208         settings->setNoiseIndevParams("mgindev_np_biome",          npindev_biome);
209         settings->setNoiseParams("mgv6_np_cave",           np_cave);
210         settings->setNoiseIndevParams("mgindev_np_float_islands1",  npindev_float_islands1);
211         settings->setNoiseIndevParams("mgindev_np_float_islands2",  npindev_float_islands2);
212         settings->setNoiseIndevParams("mgindev_np_float_islands3",  npindev_float_islands3);
213 }
214
215
216 float MapgenIndev::baseTerrainLevelFromNoise(v2s16 p) {
217         if (flags & MG_FLAT)
218                 return water_level;
219                 
220         float terrain_base   = NoisePerlin2DPosOffset(noiseindev_terrain_base->npindev,
221                                                         p.X, 0.5, p.Y, 0.5, seed);
222         float terrain_higher = NoisePerlin2DPosOffset(noiseindev_terrain_higher->npindev,
223                                                         p.X, 0.5, p.Y, 0.5, seed);
224         float steepness      = NoisePerlin2DPosOffset(noiseindev_steepness->npindev,
225                                                         p.X, 0.5, p.Y, 0.5, seed);
226         float height_select  = NoisePerlin2DNoTxfmPosOffset(noise_height_select->np,
227                                                         p.X, 0.5, p.Y, 0.5, seed);
228
229         return baseTerrainLevel(terrain_base, terrain_higher,
230                                                         steepness,    height_select);
231 }
232
233 float MapgenIndev::baseTerrainLevelFromMap(int index) {
234         if (flags & MG_FLAT)
235                 return water_level;
236         
237         float terrain_base   = noiseindev_terrain_base->result[index];
238         float terrain_higher = noiseindev_terrain_higher->result[index];
239         float steepness      = noiseindev_steepness->result[index];
240         float height_select  = noise_height_select->result[index];
241         
242         return baseTerrainLevel(terrain_base, terrain_higher,
243                                                         steepness,    height_select);
244 }
245
246 float MapgenIndev::getMudAmount(int index) {
247         if (flags & MG_FLAT)
248                 return AVERAGE_MUD_AMOUNT;
249                 
250         /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
251                         0.5+(float)p.X/200, 0.5+(float)p.Y/200,
252                         seed+91013, 3, 0.55));*/
253         
254         return noiseindev_mud->result[index];
255 }
256
257 void MapgenIndev::generateCaves(int max_stone_y) {
258         float cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, seed);
259         int volume_nodes = (node_max.X - node_min.X + 1) *
260                                            (node_max.Y - node_min.Y + 1) * MAP_BLOCKSIZE;
261         cave_amount = MYMAX(0.0, cave_amount);
262         u32 caves_count = cave_amount * volume_nodes / 50000;
263         u32 bruises_count = 1;
264         PseudoRandom ps(blockseed + 21343);
265         PseudoRandom ps2(blockseed + 1032);
266         
267         if (ps.range(1, 6) == 1)
268                 bruises_count = ps.range(0, ps.range(0, 2));
269         
270         if (getBiome(v2s16(node_min.X, node_min.Z)) == BT_DESERT) {
271                 caves_count   /= 3;
272                 bruises_count /= 3;
273         }
274         
275         for (u32 i = 0; i < caves_count + bruises_count; i++) {
276                 bool large_cave = (i >= caves_count);
277                 CaveIndev cave(this, &ps, &ps2, node_min, large_cave,
278                                                 c_water_source, c_lava_source);
279
280                 cave.makeCave(node_min, node_max, max_stone_y);
281         }
282 }
283
284 CaveIndev::CaveIndev(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2,
285                                 v3s16 node_min, bool is_large_cave,
286                                 content_t c_water, content_t c_lava) {
287         this->vm = mg->vm;
288         this->water_level = mg->water_level;
289         this->large_cave = is_large_cave;
290         this->ps  = ps;
291         this->ps2 = ps2;
292         this->c_water_source = c_water;
293         this->c_lava_source  = c_lava;
294
295         min_tunnel_diameter = 2;
296         max_tunnel_diameter = ps->range(2,6);
297         dswitchint = ps->range(1,14);
298         flooded = large_cave && ps->range(0,4);
299         if (large_cave) {
300                 part_max_length_rs = ps->range(2,4);
301                 float scale = farscale(0.2, node_min.X, node_min.Y, node_min.Z);
302                 if (node_min.Y < -100 && !ps->range(0, scale * 30)) { //huge
303                         flooded = !ps->range(0, 3);
304                         tunnel_routepoints = ps->range(5, 30);
305                         min_tunnel_diameter = 30;
306                         max_tunnel_diameter = ps->range(40, ps->range(80, 200));
307                 } else {
308                         tunnel_routepoints = ps->range(5, ps->range(15,30));
309                         min_tunnel_diameter = 5;
310                         max_tunnel_diameter = ps->range(7, ps->range(8,24));
311                 }
312         } else {
313                 part_max_length_rs = ps->range(2,9);
314                 tunnel_routepoints = ps->range(10, ps->range(15,30));
315         }
316         large_cave_is_flat = (ps->range(0,1) == 0);
317 }
318
319 /*
320 // version with one perlin3d. use with good params like
321 settings->setDefault("mgindev_np_float_islands1",  "-9.5, 10,  (20,  50,  50 ), 45123, 5, 0.6,  1.5, 5");
322 void MapgenIndev::generateFloatIslands(int min_y) {
323         if (node_min.Y < min_y) return;
324         v3s16 p0(node_min.X, node_min.Y, node_min.Z);
325         MapNode n1(c_stone), n2(c_desert_stone);
326         int xl = node_max.X - node_min.X;
327         int yl = node_max.Y - node_min.Y;
328         int zl = node_max.Z - node_min.Z;
329         u32 index = 0;
330         for (int x1 = 0; x1 <= xl; x1++)
331         {
332                 //int x = x1 + node_min.Y;
333                 for (int z1 = 0; z1 <= zl; z1++)
334                 {
335                         //int z = z1 + node_min.Z;
336                         for (int y1 = 0; y1 <= yl; y1++, index++)
337                         {
338                                 //int y = y1 + node_min.Y;
339                                 float noise = noiseindev_float_islands1->result[index];
340                                 //dstream << " y1="<<y1<< " x1="<<x1<<" z1="<<z1<< " noise="<<noise << std::endl;
341                                 if (noise > 0 ) {
342                                         v3s16 p = p0 + v3s16(x1, y1, z1);
343                                         u32 i = vm->m_area.index(p);
344                                         if (!vm->m_area.contains(i))
345                                                 continue;
346                                         // Cancel if not  air
347                                         if (vm->m_data[i].getContent() != CONTENT_AIR)
348                                                 continue;
349                                         vm->m_data[i] = noise > 1 ? n1 : n2;
350                                 }
351                         }
352                 }
353         }
354 }
355 */
356
357 void MapgenIndev::generateFloatIslands(int min_y) {
358         if (node_min.Y < min_y) return;
359         PseudoRandom pr(blockseed + 985);
360         // originally from http://forum.minetest.net/viewtopic.php?id=4776
361         float RAR = 0.8 * farscale(0.4, node_min.Y); // 0.4; // Island rarity in chunk layer. -0.4 = thick layer with holes, 0 = 50%, 0.4 = desert rarity, 0.7 = very rare.
362         float AMPY = 24; // 24; // Amplitude of island centre y variation.
363         float TGRAD = 24; // 24; // Noise gradient to create top surface. Tallness of island top.
364         float BGRAD = 24; // 24; // Noise gradient to create bottom surface. Tallness of island bottom.
365
366         v3s16 p0(node_min.X, node_min.Y, node_min.Z);
367         MapNode n1(c_stone);
368
369         float xl = node_max.X - node_min.X;
370         float yl = node_max.Y - node_min.Y;
371         float zl = node_max.Z - node_min.Z;
372         float midy = node_min.Y + yl * 0.5;
373         u32 index = 0, index2d = 0;
374         for (int x1 = 0; x1 <= xl; ++x1)
375         {
376                 for (int z1 = 0; z1 <= zl; ++z1, ++index2d)
377                 {
378                         float noise3 = noiseindev_float_islands3->result[index2d];
379                         float pmidy = midy + noise3 / 1.5 * AMPY;
380                         for (int y1 = 0; y1 <= yl; ++y1, ++index)
381                         {
382                                 int y = y1 + node_min.Y;
383                                 float noise1 = noiseindev_float_islands1->result[index];
384                                 float offset = y > pmidy ? (y - pmidy) / TGRAD : (pmidy - y) / BGRAD;
385                                 float noise1off = noise1 - offset - RAR;
386                                 if (noise1off > 0 && noise1off < 0.7) {
387                                         float noise2 = noiseindev_float_islands2->result[index];
388                                         if (noise2 - noise1off > -0.7){
389                                                 v3s16 p = p0 + v3s16(x1, y1, z1);
390                                                 u32 i = vm->m_area.index(p);
391                                                 if (!vm->m_area.contains(i))
392                                                         continue;
393                                                 // Cancel if not  air
394                                                 if (vm->m_data[i].getContent() != CONTENT_AIR)
395                                                         continue;
396                                                 vm->m_data[i] = n1;
397                                         }
398                                 }
399                         }
400                 }
401         }
402 }
403
404 void MapgenIndev::generateExperimental() {
405         int float_islands = g_settings->getS16("mgindev_float_islands");
406         if (float_islands)
407                 generateFloatIslands(float_islands);
408 }