Translation to Portuguese of Brazil for Minetest
[oweals/minetest.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 Ore::placeOre(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         int ymin, ymax;
109         
110         if (in_range & ORE_RANGE_MIRROR) {
111                 ymin = MYMAX(nmin.Y, -height_max);
112                 ymax = MYMIN(nmax.Y, -height_min);
113         } else {
114                 ymin = MYMAX(nmin.Y, height_min);
115                 ymax = MYMIN(nmax.Y, height_max);
116         }
117         if (clust_size >= ymax - ymin + 1)
118                 return;
119         
120         nmin.Y = ymin;
121         nmax.Y = ymax;
122         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
123 }
124
125
126 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
127                                                 u32 blockseed, v3s16 nmin, v3s16 nmax) {
128         PseudoRandom pr(blockseed);
129         MapNode n_ore(ore, 0, ore_param2);
130
131         int volume = (nmax.X - nmin.X + 1) *
132                                  (nmax.Y - nmin.Y + 1) *
133                                  (nmax.Z - nmin.Z + 1);
134         int csize     = clust_size;
135         int orechance = (csize * csize * csize) / clust_num_ores;
136         int nclusters = volume / clust_scarcity;
137
138         for (int i = 0; i != nclusters; i++) {
139                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
140                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
141                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
142                 
143                 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
144                         continue;
145                 
146                 for (int z1 = 0; z1 != csize; z1++)
147                 for (int y1 = 0; y1 != csize; y1++)
148                 for (int x1 = 0; x1 != csize; x1++) {
149                         if (pr.range(1, orechance) != 1)
150                                 continue;
151                         
152                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
153                         if (vm->m_data[i].getContent() == wherein)
154                                 vm->m_data[i] = n_ore;
155                 }
156         }
157 }
158
159
160 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
161                                                 u32 blockseed, v3s16 nmin, v3s16 nmax) {
162         PseudoRandom pr(blockseed + 4234);
163         MapNode n_ore(ore, 0, ore_param2);
164         
165         int max_height = clust_size;
166         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
167         
168         if (!noise) {
169                 int sx = nmax.X - nmin.X + 1;
170                 int sz = nmax.Z - nmin.Z + 1;
171                 noise = new Noise(np, 0, sx, sz);
172         }
173         noise->seed = seed + y_start;
174         noise->perlinMap2D(nmin.X, nmin.Z);
175         
176         int index = 0;
177         for (int z = nmin.Z; z <= nmax.Z; z++)
178         for (int x = nmin.X; x <= nmax.X; x++) {
179                 float noiseval = noise->result[index++];
180                 if (noiseval < nthresh)
181                         continue;
182                         
183                 int height = max_height * (1. / pr.range(1, 3));
184                 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
185                 int y1 = y0 + height;
186                 for (int y = y0; y != y1; y++) {
187                         u32 i = vm->m_area.index(x, y, z);
188                         if (!vm->m_area.contains(i))
189                                 continue;
190                                 
191                         if (vm->m_data[i].getContent() == wherein)
192                                 vm->m_data[i] = n_ore;
193                 }
194         }
195 }
196
197
198 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
199         bool isliquid, wasliquid;
200         v3s16 em  = vm->m_area.getExtent();
201
202         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
203                 for (s16 x = nmin.X; x <= nmax.X; x++) {
204                         wasliquid = true;
205                         
206                         u32 i = vm->m_area.index(x, nmax.Y, z);
207                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
208                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
209                                 
210                                 // there was a change between liquid and nonliquid, add to queue
211                                 if (isliquid != wasliquid)
212                                         trans_liquid->push_back(v3s16(x, y, z));
213
214                                 wasliquid = isliquid;
215                                 vm->m_area.add_y(em, i, -1);
216                         }
217                 }
218         }
219 }
220
221
222 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
223         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
224         VoxelArea a(nmin, nmax);
225
226         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
227                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
228                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
229                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
230                                 vm->m_data[i].param1 = light;
231                 }
232         }
233 }
234
235
236 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
237         if (light <= 1 || !a.contains(p))
238                 return;
239                 
240         u32 vi = vm->m_area.index(p);
241         MapNode &nn = vm->m_data[vi];
242
243         light--;
244         // should probably compare masked, but doesn't seem to make a difference
245         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
246                 return;
247         
248         nn.param1 = light;
249         
250         lightSpread(a, p + v3s16(0, 0, 1), light);
251         lightSpread(a, p + v3s16(0, 1, 0), light);
252         lightSpread(a, p + v3s16(1, 0, 0), light);
253         lightSpread(a, p - v3s16(0, 0, 1), light);
254         lightSpread(a, p - v3s16(0, 1, 0), light);
255         lightSpread(a, p - v3s16(1, 0, 0), light);
256 }
257
258
259 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
260         VoxelArea a(nmin, nmax);
261         bool block_is_underground = (water_level >= nmax.Y);
262
263         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
264         //TimeTaker t("updateLighting");
265
266         // first, send vertical rays of sunshine downward
267         v3s16 em = vm->m_area.getExtent();
268         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
269                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
270                         // see if we can get a light value from the overtop
271                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
272                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
273                                 if (block_is_underground)
274                                         continue;
275                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
276                                 continue;
277                         }
278                         vm->m_area.add_y(em, i, -1);
279
280                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
281                                 MapNode &n = vm->m_data[i];
282                                 if (!ndef->get(n).sunlight_propagates)
283                                         break;
284                                 n.param1 = LIGHT_SUN;
285                                 vm->m_area.add_y(em, i, -1);
286                         }
287                 }
288         }
289         
290         // now spread the sunlight and light up any sources
291         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
292                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
293                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
294                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
295                                 MapNode &n = vm->m_data[i];
296                                 if (n.getContent() == CONTENT_IGNORE ||
297                                         !ndef->get(n).light_propagates)
298                                         continue;
299                                 
300                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
301                                 if (light_produced)
302                                         n.param1 = light_produced;
303                                 
304                                 u8 light = n.param1 & 0x0F;
305                                 if (light) {
306                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
307                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
308                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
309                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
310                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
311                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
312                                 }
313                         }
314                 }
315         }
316         
317         //printf("updateLighting: %dms\n", t.stop());
318 }
319
320
321 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
322         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
323         VoxelArea a(nmin, nmax);
324         bool block_is_underground = (water_level > nmax.Y);
325         bool sunlight = !block_is_underground;
326
327         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
328         
329         for (int i = 0; i < 2; i++) {
330                 enum LightBank bank = banks[i];
331                 std::set<v3s16> light_sources;
332                 std::map<v3s16, u8> unlight_from;
333
334                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
335                                         light_sources, unlight_from);
336                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
337
338                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
339                 vm->spreadLight(bank, light_sources, ndef);
340         }
341 }
342
343
344 //////////////////////// Mapgen V6 parameter read/write
345
346 bool MapgenV6Params::readParams(Settings *settings) {
347         freq_desert = settings->getFloat("mgv6_freq_desert");
348         freq_beach  = settings->getFloat("mgv6_freq_beach");
349
350         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
351         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
352         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
353         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
354         np_mud            = settings->getNoiseParams("mgv6_np_mud");
355         np_beach          = settings->getNoiseParams("mgv6_np_beach");
356         np_biome          = settings->getNoiseParams("mgv6_np_biome");
357         np_cave           = settings->getNoiseParams("mgv6_np_cave");
358         np_humidity       = settings->getNoiseParams("mgv6_np_humidity");
359         np_trees          = settings->getNoiseParams("mgv6_np_trees");
360         np_apple_trees    = settings->getNoiseParams("mgv6_np_apple_trees");
361
362         bool success =
363                 np_terrain_base  && np_terrain_higher && np_steepness &&
364                 np_height_select && np_trees          && np_mud       &&
365                 np_beach         && np_biome          && np_cave      &&
366                 np_humidity      && np_apple_trees;
367         return success;
368 }
369
370
371 void MapgenV6Params::writeParams(Settings *settings) {
372         settings->setFloat("mgv6_freq_desert", freq_desert);
373         settings->setFloat("mgv6_freq_beach",  freq_beach);
374         
375         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
376         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
377         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
378         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
379         settings->setNoiseParams("mgv6_np_mud",            np_mud);
380         settings->setNoiseParams("mgv6_np_beach",          np_beach);
381         settings->setNoiseParams("mgv6_np_biome",          np_biome);
382         settings->setNoiseParams("mgv6_np_cave",           np_cave);
383         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
384         settings->setNoiseParams("mgv6_np_trees",          np_trees);
385         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
386 }
387
388
389 bool MapgenV7Params::readParams(Settings *settings) {
390         np_terrain_base    = settings->getNoiseParams("mgv7_np_terrain_base");
391         np_terrain_alt     = settings->getNoiseParams("mgv7_np_terrain_alt");
392         np_terrain_mod     = settings->getNoiseParams("mgv7_np_terrain_mod");
393         np_terrain_persist = settings->getNoiseParams("mgv7_np_terrain_persist");
394         np_height_select   = settings->getNoiseParams("mgv7_np_height_select");
395         np_ridge           = settings->getNoiseParams("mgv7_np_ridge");
396         
397         bool success =
398                 np_terrain_base    && np_terrain_alt   && np_terrain_mod &&
399                 np_terrain_persist && np_height_select && np_ridge;
400         return success;
401 }
402
403
404 void MapgenV7Params::writeParams(Settings *settings) {
405         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
406         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
407         settings->setNoiseParams("mgv7_np_terrain_mod",     np_terrain_mod);
408         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
409         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
410         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
411 }
412
413
414 /////////////////////////////////// legacy static functions for farmesh
415
416
417 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
418         //just need to return something
419         s16 level = 5;
420         return level;
421 }
422
423
424 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
425         double sandnoise = noise2d_perlin(
426                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
427                         seed+59420, 3, 0.50);
428
429         return (sandnoise > 0.15);
430 }
431
432
433 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
434         double noise = noise2d_perlin(
435                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
436                         seed+2, 4, 0.66);
437         double zeroval = -0.39;
438         if(noise < zeroval)
439                 return 0;
440         else
441                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
442 }