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