]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen.cpp
Fix black tree leaves, reduce above-ground cave shadows
[dragonfireclient.git] / src / mapgen.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.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "biome.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 "treegen.h"
36 #include "mapgen_v6.h"
37 #include "mapgen_v7.h"
38
39 FlagDesc flagdesc_mapgen[] = {
40         {"trees",          MG_TREES},
41         {"caves",          MG_CAVES},
42         {"dungeons",       MG_DUNGEONS},
43         {"v6_jungles",     MGV6_JUNGLES},
44         {"v6_biome_blend", MGV6_BIOME_BLEND},
45         {"flat",           MG_FLAT},
46         {NULL,             0}
47 };
48
49 FlagDesc flagdesc_ore[] = {
50         {"absheight",            OREFLAG_ABSHEIGHT},
51         {"scatter_noisedensity", OREFLAG_DENSITY},
52         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
53         {NULL,                   0}
54 };
55
56
57 ///////////////////////////////////////////////////////////////////////////////
58
59
60 Ore *createOre(OreType type) {
61         switch (type) {
62                 case ORE_SCATTER:
63                         return new OreScatter;
64                 case ORE_SHEET:
65                         return new OreSheet;
66                 //case ORE_CLAYLIKE: //TODO: implement this!
67                 //      return new OreClaylike;
68                 default:
69                         return NULL;
70         }
71 }
72
73
74 void Ore::resolveNodeNames(INodeDefManager *ndef) {
75         if (ore == CONTENT_IGNORE) {
76                 ore = ndef->getId(ore_name);
77                 if (ore == CONTENT_IGNORE) {
78                         errorstream << "Ore::resolveNodeNames: ore node '"
79                                 << ore_name << "' not defined";
80                         ore     = CONTENT_AIR;
81                         wherein = CONTENT_AIR;
82                 }
83         }
84         
85         if (wherein == CONTENT_IGNORE) {
86                 wherein = ndef->getId(wherein_name);
87                 if (wherein == CONTENT_IGNORE) {
88                         errorstream << "Ore::resolveNodeNames: wherein node '"
89                                 << wherein_name << "' not defined";
90                         ore     = CONTENT_AIR;
91                         wherein = CONTENT_AIR;
92                 }
93         }
94 }
95
96
97 void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
98         int in_range = 0;
99
100         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
101         if (flags & OREFLAG_ABSHEIGHT)
102                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
103         if (!in_range)
104                 return;
105
106         resolveNodeNames(mg->ndef);
107         
108         MapNode n_ore(ore);
109         ManualMapVoxelManipulator *vm = mg->vm;
110         PseudoRandom pr(blockseed);
111         int ymin, ymax;
112         
113         if (in_range & ORE_RANGE_MIRROR) {
114                 ymin = MYMAX(nmin.Y, -height_max);
115                 ymax = MYMIN(nmax.Y, -height_min);
116         } else {
117                 ymin = MYMAX(nmin.Y, height_min);
118                 ymax = MYMIN(nmax.Y, height_max);
119         }
120         if (clust_size >= ymax - ymin + 1)
121                 return;
122         
123         int volume = (nmax.X - nmin.X + 1) *
124                                  (nmax.Y - nmin.Y + 1) *
125                                  (nmax.Z - nmin.Z + 1);
126         int csize     = clust_size;
127         int orechance = (csize * csize * csize) / clust_num_ores;
128         int nclusters = volume / clust_scarcity;
129
130         for (int i = 0; i != nclusters; i++) {
131                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
132                 int y0 = pr.range(ymin,   ymax   - csize + 1);
133                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
134                 
135                 if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
136                         continue;
137                 
138                 for (int z1 = 0; z1 != csize; z1++)
139                 for (int y1 = 0; y1 != csize; y1++)
140                 for (int x1 = 0; x1 != csize; x1++) {
141                         if (pr.range(1, orechance) != 1)
142                                 continue;
143                         
144                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
145                         if (vm->m_data[i].getContent() == wherein)
146                                 vm->m_data[i] = n_ore;
147                 }
148         }
149 }
150
151
152 void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
153         int in_range = 0;
154
155         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
156         if (flags & OREFLAG_ABSHEIGHT)
157                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
158         if (!in_range)
159                 return;
160                 
161         resolveNodeNames(mg->ndef);
162
163         MapNode n_ore(ore);
164         ManualMapVoxelManipulator *vm = mg->vm;
165         PseudoRandom pr(blockseed + 4234);
166         int ymin, ymax;
167         
168         if (in_range & ORE_RANGE_MIRROR) {
169                 ymin = MYMAX(nmin.Y, -height_max);
170                 ymax = MYMIN(nmax.Y, -height_min);
171         } else {
172                 ymin = MYMAX(nmin.Y, height_min);
173                 ymax = MYMIN(nmax.Y, height_max);
174         }
175
176         if (clust_size >= ymax - ymin + 1)
177                 return;
178                 
179         int x0 = nmin.X;
180         int z0 = nmin.Z;
181         
182         int x1 = nmax.X;
183         int z1 = nmax.Z;
184         
185         int max_height = clust_size;
186         int y_start = pr.range(ymin, ymax - max_height);
187         
188         if (!noise) {
189                 int sx = nmax.X - nmin.X + 1;
190                 int sz = nmax.Z - nmin.Z + 1;
191                 noise = new Noise(np, 0, sx, sz);
192         }
193         noise->seed = mg->seed + y_start;
194         noise->perlinMap2D(x0, z0);
195         
196         int index = 0;
197         for (int z = z0; z != z1; z++)
198         for (int x = x0; x != x1; x++) {
199                 float noiseval = noise->result[index++];
200                 if (noiseval < nthresh)
201                         continue;
202                         
203                 int height = max_height * (1. / pr.range(1, 3));
204                 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
205                 int y1 = y0 + height;
206                 for (int y = y0; y != y1; y++) {
207                         u32 i = vm->m_area.index(x, y, z);
208                         if (!vm->m_area.contains(i))
209                                 continue;
210                                 
211                         if (vm->m_data[i].getContent() == wherein)
212                                 vm->m_data[i] = n_ore;
213                 }
214         }
215 }
216
217
218 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
219         bool isliquid, wasliquid;
220         v3s16 em  = vm->m_area.getExtent();
221
222         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
223                 for (s16 x = nmin.X; x <= nmax.X; x++) {
224                         wasliquid = true;
225                         
226                         u32 i = vm->m_area.index(x, nmax.Y, z);
227                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
228                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
229                                 
230                                 // there was a change between liquid and nonliquid, add to queue
231                                 if (isliquid != wasliquid)
232                                         trans_liquid->push_back(v3s16(x, y, z));
233
234                                 wasliquid = isliquid;
235                                 vm->m_area.add_y(em, i, -1);
236                         }
237                 }
238         }
239 }
240
241
242 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
243         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
244         VoxelArea a(nmin, nmax);
245
246         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
247                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
248                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
249                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
250                                 vm->m_data[i].param1 = light;
251                 }
252         }
253 }
254
255
256 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
257         if (light <= 1 || !a.contains(p))
258                 return;
259                 
260         u32 vi = vm->m_area.index(p);
261         MapNode &nn = vm->m_data[vi];
262
263         light--;
264         // should probably compare masked, but doesn't seem to make a difference
265         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
266                 return;
267         
268         nn.param1 = light;
269         
270         lightSpread(a, p + v3s16(0, 0, 1), light);
271         lightSpread(a, p + v3s16(0, 1, 0), light);
272         lightSpread(a, p + v3s16(1, 0, 0), light);
273         lightSpread(a, p - v3s16(0, 0, 1), light);
274         lightSpread(a, p - v3s16(0, 1, 0), light);
275         lightSpread(a, p - v3s16(1, 0, 0), light);
276 }
277
278
279 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
280         VoxelArea a(nmin, nmax);
281         bool block_is_underground = (water_level >= nmax.Y);
282
283         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
284         //TimeTaker t("updateLighting");
285
286         // first, send vertical rays of sunshine downward
287         v3s16 em = vm->m_area.getExtent();
288         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
289                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
290                         // see if we can get a light value from the overtop
291                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
292                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
293                                 if (block_is_underground)
294                                         continue;
295                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
296                                 continue;
297                         }
298                         vm->m_area.add_y(em, i, -1);
299
300                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
301                                 MapNode &n = vm->m_data[i];
302                                 if (!ndef->get(n).sunlight_propagates)
303                                         break;
304                                 n.param1 = LIGHT_SUN;
305                                 vm->m_area.add_y(em, i, -1);
306                         }
307                 }
308         }
309         
310         // now spread the sunlight and light up any sources
311         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
312                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
313                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
314                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
315                                 MapNode &n = vm->m_data[i];
316                                 if (n.getContent() == CONTENT_IGNORE ||
317                                         !ndef->get(n).light_propagates)
318                                         continue;
319                                 
320                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
321                                 if (light_produced)
322                                         n.param1 = light_produced;
323                                 
324                                 u8 light = n.param1 & 0x0F;
325                                 if (light) {
326                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
327                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
328                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
329                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
330                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
331                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
332                                 }
333                         }
334                 }
335         }
336         
337         //printf("updateLighting: %dms\n", t.stop());
338 }
339
340
341 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
342         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
343         VoxelArea a(nmin, nmax);
344         bool block_is_underground = (water_level > nmax.Y);
345         bool sunlight = !block_is_underground;
346
347         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
348         
349         for (int i = 0; i < 2; i++) {
350                 enum LightBank bank = banks[i];
351                 std::set<v3s16> light_sources;
352                 std::map<v3s16, u8> unlight_from;
353
354                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
355                                         light_sources, unlight_from);
356                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
357
358                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
359                 vm->spreadLight(bank, light_sources, ndef);
360         }
361 }
362
363
364 //////////////////////// Mapgen V6 parameter read/write
365
366 bool MapgenV6Params::readParams(Settings *settings) {
367         freq_desert = settings->getFloat("mgv6_freq_desert");
368         freq_beach  = settings->getFloat("mgv6_freq_beach");
369
370         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
371         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
372         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
373         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
374         np_mud            = settings->getNoiseParams("mgv6_np_mud");
375         np_beach          = settings->getNoiseParams("mgv6_np_beach");
376         np_biome          = settings->getNoiseParams("mgv6_np_biome");
377         np_cave           = settings->getNoiseParams("mgv6_np_cave");
378         np_humidity       = settings->getNoiseParams("mgv6_np_humidity");
379         np_trees          = settings->getNoiseParams("mgv6_np_trees");
380         np_apple_trees    = settings->getNoiseParams("mgv6_np_apple_trees");
381
382         bool success =
383                 np_terrain_base  && np_terrain_higher && np_steepness &&
384                 np_height_select && np_trees          && np_mud       &&
385                 np_beach         && np_biome          && np_cave      &&
386                 np_humidity      && np_apple_trees;
387         return success;
388 }
389
390
391 void MapgenV6Params::writeParams(Settings *settings) {
392         settings->setFloat("mgv6_freq_desert", freq_desert);
393         settings->setFloat("mgv6_freq_beach",  freq_beach);
394         
395         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
396         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
397         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
398         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
399         settings->setNoiseParams("mgv6_np_mud",            np_mud);
400         settings->setNoiseParams("mgv6_np_beach",          np_beach);
401         settings->setNoiseParams("mgv6_np_biome",          np_biome);
402         settings->setNoiseParams("mgv6_np_cave",           np_cave);
403         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
404         settings->setNoiseParams("mgv6_np_trees",          np_trees);
405         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
406 }
407
408
409 bool MapgenV7Params::readParams(Settings *settings) {
410         np_terrain_base    = settings->getNoiseParams("mgv7_np_terrain_base");
411         np_terrain_alt     = settings->getNoiseParams("mgv7_np_terrain_alt");
412         np_terrain_mod     = settings->getNoiseParams("mgv7_np_terrain_mod");
413         np_terrain_persist = settings->getNoiseParams("mgv7_np_terrain_persist");
414         np_height_select   = settings->getNoiseParams("mgv7_np_height_select");
415         np_ridge           = settings->getNoiseParams("mgv7_np_ridge");
416         
417         bool success =
418                 np_terrain_base    && np_terrain_alt   && np_terrain_mod &&
419                 np_terrain_persist && np_height_select && np_ridge;
420         return success;
421 }
422
423
424 void MapgenV7Params::writeParams(Settings *settings) {
425         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
426         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
427         settings->setNoiseParams("mgv7_np_terrain_mod",     np_terrain_mod);
428         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
429         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
430         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
431 }
432
433
434 /////////////////////////////////// legacy static functions for farmesh
435
436
437 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
438         //just need to return something
439         s16 level = 5;
440         return level;
441 }
442
443
444 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
445         double sandnoise = noise2d_perlin(
446                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
447                         seed+59420, 3, 0.50);
448
449         return (sandnoise > 0.15);
450 }
451
452
453 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
454         double noise = noise2d_perlin(
455                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
456                         seed+2, 4, 0.66);
457         double zeroval = -0.39;
458         if(noise < zeroval)
459                 return 0;
460         else
461                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
462 }