]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen_v7.cpp
af453c2ec6d0a3a4c21441bfed6626ab6aa61d72
[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         calcLighting(node_min, node_max);
190         //setLighting(node_min, node_max, 0xFF);
191
192         this->generating = false;
193 }
194
195
196 void MapgenV7::calculateNoise() {
197         //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
198         int x = node_min.X;
199         int y = node_min.Y;
200         int z = node_min.Z;
201         
202         noise_terrain_mod->perlinMap2D(x, z);
203         
204         noise_height_select->perlinMap2D(x, z);
205         noise_height_select->transformNoiseMap();
206         
207         noise_terrain_persist->perlinMap2D(x, z);
208         float *persistmap = noise_terrain_persist->result;
209         for (int i = 0; i != csize.X * csize.Z; i++)
210                 persistmap[i] = abs(persistmap[i]);
211         
212         noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
213         noise_terrain_base->transformNoiseMap();
214         
215         noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
216         noise_terrain_alt->transformNoiseMap();
217         
218         noise_ridge->perlinMap3D(x, y, z);
219         
220         noise_heat->perlinMap2D(x, z);
221         
222         noise_humidity->perlinMap2D(x, z);
223         
224         //printf("calculateNoise: %dus\n", t.stop());
225 }
226
227
228 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
229         float heat      = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
230         float humidity  = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
231         s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
232         
233         return bmgr->getBiome(heat, humidity, groundlevel);
234 }
235
236
237 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
238         float terrain_mod = NoisePerlin2DNoTxfm(noise_terrain_mod->np, x, z, seed);
239         float hselect     = NoisePerlin2D(noise_height_select->np, x, z, seed);
240         float persist     = abs(NoisePerlin2DNoTxfm(noise_terrain_persist->np, x, z, seed));
241
242         noise_terrain_base->np->persist = persist;
243         float terrain_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
244         float height_base  = terrain_base * terrain_mod;
245
246         noise_terrain_alt->np->persist = persist;
247         float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
248
249         return (height_base * hselect) + (height_alt * (1.0 - hselect));
250 }
251
252
253 float MapgenV7::baseTerrainLevelFromMap(int index) {    
254         float terrain_mod  = noise_terrain_mod->result[index];
255         float hselect      = noise_height_select->result[index];
256         float terrain_base = noise_terrain_base->result[index];
257         float height_base  = terrain_base * terrain_mod;
258         float height_alt   = noise_terrain_alt->result[index];
259
260         return (height_base * hselect) + (height_alt * (1.0 - hselect));
261 }
262
263
264 #if 0
265 // Crap code to test log rivers as a proof-of-concept.  Didn't work out too well.
266 void MapgenV7::carveRivers() {
267         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
268         MapNode n_stone(c_stone);
269         u32 index = 0;
270         
271         int river_depth = 4;
272
273         for (s16 z = node_min.Z; z <= node_max.Z; z++)
274         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
275                 float terrain_mod  = noise_terrain_mod->result[index];
276                 NoiseParams *np = noise_terrain_river->np;
277                 np->persist = noise_terrain_persist->result[index];
278                 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
279                 float height = terrain_river * (1 - abs(terrain_mod)) *
280                                                 noise_terrain_river->np->scale;
281                 height = log(height * height); //log(h^3) is pretty interesting for terrain
282                 
283                 s16 y = heightmap[index];
284                 if (height < 1.0 && y > river_depth &&
285                         y - river_depth >= node_min.Y && y <= node_max.Y) {
286                         
287                         for (s16 ry = y; ry != y - river_depth; ry--) {
288                                 u32 vi = vm->m_area.index(x, ry, z);
289                                 vm->m_data[vi] = n_air;
290                         }
291                         
292                         u32 vi = vm->m_area.index(x, y - river_depth, z);
293                         vm->m_data[vi] = n_water_source;
294                 }
295         }
296 }
297 #endif
298
299
300 int MapgenV7::calcHeightMap() {
301         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
302         u32 index = 0;
303         
304         for (s16 z = node_min.Z; z <= node_max.Z; z++)
305         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
306                 float surface_height = baseTerrainLevelFromMap(index);
307                 s16 surface_y = (s16)surface_height;
308                 
309                 heightmap[index]       = surface_y; 
310                 ridge_heightmap[index] = surface_y;
311                 
312                 if (surface_y > stone_surface_max_y)
313                         stone_surface_max_y = surface_y;
314         }
315                 
316         return stone_surface_max_y;
317 }
318
319
320 void MapgenV7::generateTerrain() {
321         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
322         MapNode n_stone(c_stone);
323
324         v3s16 em = vm->m_area.getExtent();
325         u32 index = 0;
326         
327         for (s16 z = node_min.Z; z <= node_max.Z; z++)
328         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
329                 s16 surface_y = heightmap[index];
330                 Biome *biome = bmgr->biomes[biomemap[index]];
331                 
332                 u32 i = vm->m_area.index(x, node_min.Y, z);
333                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
334                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
335                                 if (y <= surface_y) {
336                                         vm->m_data[i] = (y > water_level + biome->filler_height) ?
337                                                 MapNode(biome->c_filler) : n_stone;
338                                 } else if (y <= water_level) {
339                                         vm->m_data[i] = n_water_source;
340                                 } else {
341                                         vm->m_data[i] = n_air;
342                                 }
343                         }
344                         vm->m_area.add_y(em, i, 1);
345                 }
346         }
347 }
348
349
350 void MapgenV7::carveRidges() {
351         if (node_max.Y <= water_level)
352                 return;
353                 
354         MapNode n_air(CONTENT_AIR);
355         u32 index = 0;
356         
357         for (s16 z = node_min.Z; z <= node_max.Z; z++)
358         for (s16 y = node_min.Y; y <= node_max.Y; y++) {
359                 u32 vi = vm->m_area.index(node_min.X, y, z);
360                 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
361                         // Removing this check will create huge underwater caverns,
362                         // which are interesting but not desirable for gameplay
363                         if (y <= water_level)
364                                 continue;
365                                 
366                         if (noise_ridge->result[index] * (float)(y * y) < 15.0)
367                                 continue;
368
369                         int j = (z - node_min.Z) * csize.Z + (x - node_min.X); //////obviously just temporary
370                         if (y < ridge_heightmap[j])
371                                 ridge_heightmap[j] = y - 1; 
372
373                         vm->m_data[vi] = n_air;
374                 }
375         }
376 }
377
378 /*
379 void MapgenV7::testBiomes() {
380         u32 index = 0;
381         
382         for (s16 z = node_min.Z; z <= node_min.Z; z++)
383         for (s16 x = node_min.X; x <= node_min.X; x++) {;
384                 Biome *b = bmgr->getBiome(heat, humidity, 0);
385         }
386         // make an 80x80 grid with axes heat/humidity as a voroni diagram for biomes
387         // clear out y space for it first with air
388         // use absolute positioning, each chunk will be a +1 height
389 }*/
390
391
392 void MapgenV7::addTopNodes() {
393         v3s16 em = vm->m_area.getExtent();
394         s16 ntopnodes;
395         u32 index = 0;
396         
397         for (s16 z = node_min.Z; z <= node_max.Z; z++)
398         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
399                 // First, add top nodes below the ridge
400                 s16 y = ridge_heightmap[index];
401                 
402                 // This cutoff is good enough, but not perfect.
403                 // It will cut off potentially placed top nodes at chunk boundaries
404                 if (y < node_min.Y)
405                         continue;
406                 if (y > node_max.Y) {
407                         y = node_max.Y; // Let's see if we can still go downward anyway
408                         u32 vi = vm->m_area.index(x, y, z);
409                         if (vm->m_data[vi].getContent() != CONTENT_AIR)
410                                 continue;
411                 }
412                 
413                 // N.B.  It is necessary to search downward since range_heightmap[i]
414                 // might not be the actual height, just the lowest part in the chunk
415                 // where a ridge had been carved
416                 u32 i = vm->m_area.index(x, y, z);
417                 for (; y >= node_min.Y; y--) {
418                         if (vm->m_data[i].getContent() != CONTENT_AIR)
419                                 break;
420                         vm->m_area.add_y(em, i, -1);
421                 }
422
423                 Biome *biome = bmgr->biomes[biomemap[index]];
424
425                 if (y != node_min.Y - 1) {
426                         ridge_heightmap[index] = y; //update ridgeheight
427                         ntopnodes = biome->top_depth;
428                         for (; y <= node_max.Y && ntopnodes; y++) {
429                                 ntopnodes--;
430                                 vm->m_data[i] = MapNode(biome->c_top);
431                                 vm->m_area.add_y(em, i, 1);
432                         }
433                         //heightmap[index] = y;
434                 }
435                 
436                 // Now, add top nodes on top of the ridge
437                 y = heightmap[index];
438
439                 i = vm->m_area.index(x, y, z);
440                 for (; y >= node_min.Y; y--) {
441                         if (vm->m_data[i].getContent() != CONTENT_AIR)
442                                 break;
443                         vm->m_area.add_y(em, i, -1);
444                 }
445
446                 if (y != node_min.Y - 1) {
447                         ntopnodes = biome->top_depth;
448                         // Let's see if we've already added it...
449                         if (y == ridge_heightmap[index] + ntopnodes - 1)
450                                 continue;
451
452                         for (; y <= node_max.Y && ntopnodes; y++) {
453                                 ntopnodes--;
454                                 vm->m_data[i] = MapNode(biome->c_top);
455                                 vm->m_area.add_y(em, i, 1);
456                         }
457                 }
458         }
459 }
460
461
462 void MapgenV7::growGrass() {
463         for (s16 z = node_min.Z; z <= node_max.Z; z++)
464         for (s16 x = node_min.X; x <= node_max.X; x++) {
465                 // Find the lowest surface to which enough light ends up to make
466                 // grass grow.  Basically just wait until not air and not leaves.
467                 s16 surface_y = 0;
468                 {
469                         v3s16 em = vm->m_area.getExtent();
470                         u32 i = vm->m_area.index(x, node_max.Y, z);
471                         s16 y;
472                         // Go to ground level
473                         for (y = node_max.Y; y >= node_min.Y; y--) {
474                                 MapNode &n = vm->m_data[i];
475                                 if (ndef->get(n).param_type != CPT_LIGHT ||
476                                         ndef->get(n).liquid_type != LIQUID_NONE)
477                                         break;
478                                 vm->m_area.add_y(em, i, -1);
479                         }
480                         surface_y = (y >= node_min.Y) ? y : node_min.Y;
481                 }
482
483                 u32 i = vm->m_area.index(x, surface_y, z);
484                 MapNode *n = &vm->m_data[i];
485                 if (n->getContent() == c_dirt && surface_y >= water_level - 20)
486                         n->setContent(c_dirt_with_grass);
487         }
488 }