]> git.lizzy.rs Git - minetest.git/blob - src/mapgen_v7.cpp
Remove useless recalculation of bounding box (mapblock_mesh)
[minetest.git] / src / mapgen_v7.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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
21 #include "mapgen.h"
22 #include "voxel.h"
23 #include "noise.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
32 #include "profiler.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
35 #include "emerge.h"
36 #include "dungeongen.h"
37 #include "cavegen.h"
38 #include "treegen.h"
39 #include "biome.h"
40 #include "mapgen_v7.h"
41
42
43 /////////////////// Mapgen V7 perlin noise default values
44 NoiseParams nparams_v7_def_terrain_base =
45         {0, 80.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
46 NoiseParams nparams_v7_def_terrain_alt =
47         {0, 20.0, v3f(250.0, 250.0, 250.0), 5934, 5, 0.6};
48 NoiseParams nparams_v7_def_terrain_mod =
49         {0, 1.0, v3f(350.0, 350.0, 350.0), 85039, 5, 0.6};
50 NoiseParams nparams_v7_def_terrain_persist =
51         {0, 1.0, v3f(500.0, 500.0, 500.0), 539, 3, 0.6};
52 NoiseParams nparams_v7_def_height_select =
53         {0.5, 0.5, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
54 NoiseParams nparams_v7_def_ridge =
55         {0, 1.0, v3f(100.0, 100.0, 100.0), 6467, 4, 0.75};
56 /*
57 NoiseParams nparams_v6_def_beach =
58         {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
59 NoiseParams nparams_v6_def_cave =
60         {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
61 NoiseParams nparams_v6_def_humidity =
62         {0.5, 0.5, v3f(500.0, 500.0, 500.0), 72384, 4, 0.66};
63 NoiseParams nparams_v6_def_trees =
64         {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
65 NoiseParams nparams_v6_def_apple_trees =
66         {0.0, 1.0, v3f(100.0, 100.0, 100.0), 342902, 3, 0.45};
67 */
68 ///////////////////////////////////////////////////////////////////////////////
69
70
71 MapgenV7::MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge) {
72         this->generating  = false;
73         this->id     = mapgenid;
74         this->emerge = emerge;
75         this->bmgr   = emerge->biomedef;
76
77         this->seed     = (int)params->seed;
78         this->water_level = params->water_level;
79         this->flags   = params->flags;
80         this->csize   = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
81         this->ystride = csize.X; //////fix this
82
83         this->biomemap  = new u8[csize.X * csize.Z];
84         this->heightmap = new s16[csize.X * csize.Z];
85         this->ridge_heightmap = new s16[csize.X * csize.Z];
86
87         // Terrain noise
88         noise_terrain_base    = new Noise(&params->np_terrain_base,    seed, csize.X, csize.Z);
89         noise_terrain_alt     = new Noise(&params->np_terrain_alt,     seed, csize.X, csize.Z);
90         noise_terrain_mod     = new Noise(&params->np_terrain_mod,     seed, csize.X, csize.Z);
91         noise_terrain_persist = new Noise(&params->np_terrain_persist, seed, csize.X, csize.Z);
92         noise_height_select   = new Noise(&params->np_height_select,   seed, csize.X, csize.Z);
93         noise_ridge           = new Noise(&params->np_ridge, seed, csize.X, csize.Y, csize.Z);
94         
95         // Biome noise
96         noise_heat     = new Noise(bmgr->np_heat,     seed, csize.X, csize.Z);
97         noise_humidity = new Noise(bmgr->np_humidity, seed, csize.X, csize.Z);  
98 }
99
100
101 MapgenV7::~MapgenV7() {
102         delete noise_terrain_base;
103         delete noise_terrain_mod;
104         delete noise_terrain_persist;
105         delete noise_height_select;
106         delete noise_terrain_alt;
107         delete noise_ridge;
108         delete noise_heat;
109         delete noise_humidity;
110         
111         delete[] ridge_heightmap;
112         delete[] heightmap;
113         delete[] biomemap;
114 }
115
116
117 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
118         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Y);
119         float heat      = NoisePerlin2D(bmgr->np_heat, p.X, p.Y, seed);
120         float humidity  = NoisePerlin2D(bmgr->np_humidity, p.X, p.Y, seed);
121         Biome *b = bmgr->getBiome(heat, humidity, groundlevel);
122         
123         s16 y = groundlevel;
124         int iters = 1024; // don't even bother iterating more than 64 times..
125         while (iters--) {
126                 if (y <= water_level)
127                         break;
128                 
129                 float ridgenoise = NoisePerlin3D(noise_ridge->np, p.X, y, p.Y, seed);
130                 if (ridgenoise * (float)(y * y) < 15.0)
131                         break;
132                         
133                 y--;
134         }
135         if (iters == 0)
136                 printf("iters exhausted at %d %d\n", p.X, p.Y);
137
138         return y + b->top_depth;
139 }
140
141
142 void MapgenV7::makeChunk(BlockMakeData *data) {
143         assert(data->vmanip);
144         assert(data->nodedef);
145         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
146                    data->blockpos_requested.Y >= data->blockpos_min.Y &&
147                    data->blockpos_requested.Z >= data->blockpos_min.Z);
148         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
149                    data->blockpos_requested.Y <= data->blockpos_max.Y &&
150                    data->blockpos_requested.Z <= data->blockpos_max.Z);
151                         
152         this->generating = true;
153         this->vm   = data->vmanip;      
154         this->ndef = data->nodedef;
155         //TimeTaker t("makeChunk");
156         
157         v3s16 blockpos_min = data->blockpos_min;
158         v3s16 blockpos_max = data->blockpos_max;
159         node_min = blockpos_min * MAP_BLOCKSIZE;
160         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
161         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
162         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
163
164         blockseed = emerge->getBlockSeed(full_node_min);  //////use getBlockSeed2()!
165
166         // Make some noise
167         calculateNoise();
168
169         // Calculate height map
170         s16 stone_surface_max_y = calcHeightMap();
171         
172         // Calculate biomes
173         BiomeNoiseInput binput;
174         binput.mapsize       = v2s16(csize.X, csize.Z);
175         binput.heat_map      = noise_heat->result;
176         binput.humidity_map  = noise_humidity->result;
177         binput.height_map    = heightmap;
178         bmgr->calcBiomes(&binput, biomemap);
179         
180         c_stone           = ndef->getId("mapgen_stone");
181         c_dirt            = ndef->getId("mapgen_dirt");
182         c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
183         c_sand            = ndef->getId("mapgen_sand");
184         c_water_source    = ndef->getId("mapgen_water_source");
185         c_lava_source     = ndef->getId("mapgen_lava_source");
186         
187         generateTerrain();
188         carveRidges();
189
190         if (flags & MG_CAVES)
191                 generateCaves(stone_surface_max_y);
192         
193         addTopNodes();
194         
195         updateHeightmap(node_min, node_max);
196
197         if (flags & MG_DUNGEONS) {
198                 DungeonGen dgen(ndef, data->seed, water_level);
199                 dgen.generate(vm, blockseed, full_node_min, full_node_max);
200         }
201
202         for (size_t i = 0; i != emerge->decorations.size(); i++) {
203                 Decoration *deco = emerge->decorations[i];
204                 deco->placeDeco(this, blockseed + i, node_min, node_max);
205         }
206
207         for (size_t i = 0; i != emerge->ores.size(); i++) {
208                 Ore *ore = emerge->ores[i];
209                 ore->placeOre(this, blockseed + i, node_min, node_max);
210         }
211         
212         //printf("makeChunk: %dms\n", t.stop());
213         
214         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
215         
216         calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
217                                  node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
218         //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
219         //                      node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
220
221         this->generating = false;
222 }
223
224
225 void MapgenV7::calculateNoise() {
226         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
227         int x = node_min.X;
228         int y = node_min.Y;
229         int z = node_min.Z;
230         
231         noise_terrain_mod->perlinMap2D(x, z);
232         
233         noise_height_select->perlinMap2D(x, z);
234         noise_height_select->transformNoiseMap();
235         
236         noise_terrain_persist->perlinMap2D(x, z);
237         float *persistmap = noise_terrain_persist->result;
238         for (int i = 0; i != csize.X * csize.Z; i++)
239                 persistmap[i] = abs(persistmap[i]);
240         
241         noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
242         noise_terrain_base->transformNoiseMap();
243         
244         noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
245         noise_terrain_alt->transformNoiseMap();
246         
247         noise_ridge->perlinMap3D(x, y, z);
248         
249         noise_heat->perlinMap2D(x, z);
250         
251         noise_humidity->perlinMap2D(x, z);
252         
253         //printf("calculateNoise: %dus\n", t.stop());
254 }
255
256
257 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
258         float heat      = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
259         float humidity  = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
260         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
261         
262         return bmgr->getBiome(heat, humidity, groundlevel);
263 }
264
265
266 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
267         float terrain_mod = NoisePerlin2DNoTxfm(noise_terrain_mod->np, x, z, seed);
268         float hselect     = NoisePerlin2D(noise_height_select->np, x, z, seed);
269         float persist     = abs(NoisePerlin2DNoTxfm(noise_terrain_persist->np, x, z, seed));
270
271         noise_terrain_base->np->persist = persist;
272         float terrain_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
273         float height_base  = terrain_base * terrain_mod;
274
275         noise_terrain_alt->np->persist = persist;
276         float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
277
278         return (height_base * hselect) + (height_alt * (1.0 - hselect));
279 }
280
281
282 float MapgenV7::baseTerrainLevelFromMap(int index) {
283         float terrain_mod  = noise_terrain_mod->result[index];
284         float hselect      = noise_height_select->result[index];
285         float terrain_base = noise_terrain_base->result[index];
286         float height_base  = terrain_base * terrain_mod;
287         float height_alt   = noise_terrain_alt->result[index];
288
289         return (height_base * hselect) + (height_alt * (1.0 - hselect));
290 }
291
292
293 #if 0
294 // Crap code to test log rivers as a proof-of-concept.  Didn't work out too well.
295 void MapgenV7::carveRivers() {
296         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
297         MapNode n_stone(c_stone);
298         u32 index = 0;
299         
300         int river_depth = 4;
301
302         for (s16 z = node_min.Z; z <= node_max.Z; z++)
303         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
304                 float terrain_mod  = noise_terrain_mod->result[index];
305                 NoiseParams *np = noise_terrain_river->np;
306                 np->persist = noise_terrain_persist->result[index];
307                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
308                 float height = terrain_river * (1 - abs(terrain_mod)) *
309                                                 noise_terrain_river->np->scale;
310                 height = log(height * height); //log(h^3) is pretty interesting for terrain
311                 
312                 s16 y = heightmap[index];
313                 if (height < 1.0 && y > river_depth &&
314                         y - river_depth >= node_min.Y && y <= node_max.Y) {
315                         
316                         for (s16 ry = y; ry != y - river_depth; ry--) {
317                                 u32 vi = vm->m_area.index(x, ry, z);
318                                 vm->m_data[vi] = n_air;
319                         }
320                         
321                         u32 vi = vm->m_area.index(x, y - river_depth, z);
322                         vm->m_data[vi] = n_water_source;
323                 }
324         }
325 }
326 #endif
327
328
329 int MapgenV7::calcHeightMap() {
330         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
331         u32 index = 0;
332         
333         for (s16 z = node_min.Z; z <= node_max.Z; z++)
334         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
335                 float surface_height = baseTerrainLevelFromMap(index);
336                 s16 surface_y = (s16)surface_height;
337                 
338                 heightmap[index]       = surface_y; 
339                 ridge_heightmap[index] = surface_y;
340                 
341                 if (surface_y > stone_surface_max_y)
342                         stone_surface_max_y = surface_y;
343         }
344                 
345         return stone_surface_max_y;
346 }
347
348
349 void MapgenV7::generateTerrain() {
350         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
351         MapNode n_stone(c_stone);
352
353         v3s16 em = vm->m_area.getExtent();
354         u32 index = 0;
355         
356         for (s16 z = node_min.Z; z <= node_max.Z; z++)
357         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
358                 s16 surface_y = heightmap[index];
359                 Biome *biome = bmgr->biomes[biomemap[index]];
360                 
361                 u32 i = vm->m_area.index(x, node_min.Y, z);
362                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
363                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
364                                 if (y <= surface_y) {
365                                         vm->m_data[i] = (y > water_level + biome->filler_height) ?
366                                                 MapNode(biome->c_filler) : n_stone;
367                                 } else if (y <= water_level) {
368                                         vm->m_data[i] = n_water_source;
369                                 } else {
370                                         vm->m_data[i] = n_air;
371                                 }
372                         }
373                         vm->m_area.add_y(em, i, 1);
374                 }
375         }
376 }
377
378
379 void MapgenV7::carveRidges() {
380         if (node_max.Y <= water_level)
381                 return;
382                 
383         MapNode n_air(CONTENT_AIR);
384         u32 index = 0;
385         
386         for (s16 z = node_min.Z; z <= node_max.Z; z++)
387         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
388                 u32 vi = vm->m_area.index(node_min.X, y, z);
389                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
390                         // Removing this check will create huge underwater caverns,
391                         // which are interesting but not desirable for gameplay
392                         if (y <= water_level)
393                                 continue;
394                                 
395                         if (noise_ridge->result[index] * (float)(y * y) < 15.0)
396                                 continue;
397
398                         int j = (z - node_min.Z) * csize.Z + (x - node_min.X); //////obviously just temporary
399                         if (y < ridge_heightmap[j])
400                                 ridge_heightmap[j] = y - 1; 
401
402                         vm->m_data[vi] = n_air;
403                 }
404         }
405 }
406
407 /*
408 void MapgenV7::testBiomes() {
409         u32 index = 0;
410         
411         for (s16 z = node_min.Z; z <= node_min.Z; z++)
412         for (s16 x = node_min.X; x <= node_min.X; x++) {;
413                 Biome *b = bmgr->getBiome(heat, humidity, 0);
414         }
415         // make an 80x80 grid with axes heat/humidity as a voroni diagram for biomes
416         // clear out y space for it first with air
417         // use absolute positioning, each chunk will be a +1 height
418 }*/
419
420
421 void MapgenV7::addTopNodes() {
422         v3s16 em = vm->m_area.getExtent();
423         s16 ntopnodes;
424         u32 index = 0;
425
426         for (s16 z = node_min.Z; z <= node_max.Z; z++)
427         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
428                 Biome *biome = bmgr->biomes[biomemap[index]];
429                 
430                 //////////////////// First, add top nodes below the ridge
431                 s16 y = ridge_heightmap[index];
432                 
433                 // This cutoff is good enough, but not perfect.
434                 // It will cut off potentially placed top nodes at chunk boundaries
435                 if (y < node_min.Y)
436                         continue;
437                 if (y > node_max.Y) {
438                         y = node_max.Y; // Let's see if we can still go downward anyway
439                         u32 vi = vm->m_area.index(x, y, z);
440                         content_t c = vm->m_data[vi].getContent();
441                         if (ndef->get(c).walkable)
442                                 continue;
443                 }
444                 
445                 // N.B.  It is necessary to search downward since range_heightmap[i]
446                 // might not be the actual height, just the lowest part in the chunk
447                 // where a ridge had been carved
448                 u32 i = vm->m_area.index(x, y, z);
449                 for (; y >= node_min.Y; y--) {
450                         content_t c = vm->m_data[i].getContent();
451                         if (ndef->get(c).walkable)
452                                 break;
453                         vm->m_area.add_y(em, i, -1);
454                 }
455
456                 if (y != node_min.Y - 1 && y >= water_level) {
457                         ridge_heightmap[index] = y; //update ridgeheight
458                         ntopnodes = biome->top_depth;
459                         for (; y <= node_max.Y && ntopnodes; y++) {
460                                 ntopnodes--;
461                                 vm->m_data[i] = MapNode(biome->c_top);
462                                 vm->m_area.add_y(em, i, 1);
463                         }
464                         // If dirt, grow grass on it.
465                         if (y > water_level - 10 &&
466                                 vm->m_data[i].getContent() == CONTENT_AIR) {
467                                 vm->m_area.add_y(em, i, -1);
468                                 if (vm->m_data[i].getContent() == c_dirt)
469                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
470                         }
471                 }
472                 
473                 //////////////////// Now, add top nodes on top of the ridge
474                 y = heightmap[index];
475                 if (y > node_max.Y) {
476                         y = node_max.Y; // Let's see if we can still go downward anyway
477                         u32 vi = vm->m_area.index(x, y, z);
478                         content_t c = vm->m_data[vi].getContent();
479                         if (ndef->get(c).walkable)
480                                 continue;
481                 }
482
483                 i = vm->m_area.index(x, y, z);
484                 for (; y >= node_min.Y; y--) {
485                         content_t c = vm->m_data[i].getContent();
486                         if (ndef->get(c).walkable)
487                                 break;
488                         vm->m_area.add_y(em, i, -1);
489                 }
490
491                 if (y != node_min.Y - 1) {
492                         ntopnodes = biome->top_depth;
493                         // Let's see if we've already added it...
494                         if (y == ridge_heightmap[index] + ntopnodes - 1)
495                                 continue;
496
497                         for (; y <= node_max.Y && ntopnodes; y++) {
498                                 ntopnodes--;
499                                 vm->m_data[i] = MapNode(biome->c_top);
500                                 vm->m_area.add_y(em, i, 1);
501                         }
502                         // If dirt, grow grass on it.
503                         if (y > water_level - 10 &&
504                                 vm->m_data[i].getContent() == CONTENT_AIR) {
505                                 vm->m_area.add_y(em, i, -1);
506                                 if (vm->m_data[i].getContent() == c_dirt)
507                                         vm->m_data[i] = MapNode(c_dirt_with_grass);
508                         }
509                 }
510         }
511 }
512
513
514 #include "mapgen_v6.h"
515 void MapgenV7::generateCaves(int max_stone_y) {
516         PseudoRandom ps(blockseed + 21343);
517
518         int volume_nodes = (node_max.X - node_min.X + 1) *
519                                            (node_max.Y - node_min.Y + 1) *
520                                            (node_max.Z - node_min.Z + 1);
521         float cave_amount = NoisePerlin2D(&nparams_v6_def_cave,
522                                                                 node_min.X, node_min.Y, seed);
523
524         u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
525         for (u32 i = 0; i < caves_count; i++) {
526                 CaveV7 cave(this, &ps, false);
527                 cave.makeCave(node_min, node_max, max_stone_y);
528         }
529
530         u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
531         for (u32 i = 0; i < bruises_count; i++) {
532                 CaveV7 cave(this, &ps, true);
533                 cave.makeCave(node_min, node_max, max_stone_y);
534         }       
535 }