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