]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen.cpp
Add Ore generation flags, implement ore absolute height
[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
38 FlagDesc flagdesc_mapgen[] = {
39         {"trees",          MG_TREES},
40         {"caves",          MG_CAVES},
41         {"dungeons",       MG_DUNGEONS},
42         {"v6_jungles",     MGV6_JUNGLES},
43         {"v6_biome_blend", MGV6_BIOME_BLEND},
44         {"flat",           MG_FLAT},
45         {NULL,             0}
46 };
47
48 FlagDesc flagdesc_ore[] = {
49         {"absheight",            OREFLAG_ABSHEIGHT},
50         {"scatter_noisedensity", OREFLAG_DENSITY},
51         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
52         {NULL,                   0}
53 };
54
55
56 ///////////////////////////////////////////////////////////////////////////////
57
58
59 Ore *createOre(OreType type) {
60         switch (type) {
61                 case ORE_SCATTER:
62                         return new OreScatter;
63                 case ORE_SHEET:
64                         return new OreSheet;
65                 //case ORE_CLAYLIKE: //TODO: implement this!
66                 //      return new OreClaylike;
67                 default:
68                         return NULL;
69         }
70 }
71
72
73 void Ore::resolveNodeNames(INodeDefManager *ndef) {
74         if (ore == CONTENT_IGNORE) {
75                 ore = ndef->getId(ore_name);
76                 if (ore == CONTENT_IGNORE) {
77                         errorstream << "Ore::resolveNodeNames: ore node '"
78                                 << ore_name << "' not defined";
79                         ore     = CONTENT_AIR;
80                         wherein = CONTENT_AIR;
81                 }
82         }
83         
84         if (wherein == CONTENT_IGNORE) {
85                 wherein = ndef->getId(wherein_name);
86                 if (wherein == CONTENT_IGNORE) {
87                         errorstream << "Ore::resolveNodeNames: wherein node '"
88                                 << wherein_name << "' not defined";
89                         ore     = CONTENT_AIR;
90                         wherein = CONTENT_AIR;
91                 }
92         }
93 }
94
95
96 void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
97         int in_range = 0;
98
99         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
100         if (flags & OREFLAG_ABSHEIGHT)
101                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
102         if (!in_range)
103                 return;
104
105         resolveNodeNames(mg->ndef);
106         
107         MapNode n_ore(ore);
108         ManualMapVoxelManipulator *vm = mg->vm;
109         PseudoRandom pr(blockseed);
110         int ymin, ymax;
111         
112         if (in_range & ORE_RANGE_MIRROR) {
113                 ymin = MYMAX(nmin.Y, -height_max);
114                 ymax = MYMIN(nmax.Y, -height_min);
115         } else {
116                 ymin = MYMAX(nmin.Y, height_min);
117                 ymax = MYMIN(nmax.Y, height_max);
118         }
119         if (clust_size >= ymax - ymin + 1)
120                 return;
121         
122         int volume = (nmax.X - nmin.X + 1) *
123                                  (nmax.Y - nmin.Y + 1) *
124                                  (nmax.Z - nmin.Z + 1);
125         int csize     = clust_size;
126         int orechance = (csize * csize * csize) / clust_num_ores;
127         int nclusters = volume / clust_scarcity;
128
129         for (int i = 0; i != nclusters; i++) {
130                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
131                 int y0 = pr.range(ymin,   ymax   - csize + 1);
132                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
133                 
134                 if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
135                         continue;
136                 
137                 for (int z1 = 0; z1 != csize; z1++)
138                 for (int y1 = 0; y1 != csize; y1++)
139                 for (int x1 = 0; x1 != csize; x1++) {
140                         if (pr.range(1, orechance) != 1)
141                                 continue;
142                         
143                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
144                         if (vm->m_data[i].getContent() == wherein)
145                                 vm->m_data[i] = n_ore;
146                 }
147         }
148 }
149
150
151 void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
152         int in_range = 0;
153
154         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
155         if (flags & OREFLAG_ABSHEIGHT)
156                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
157         if (!in_range)
158                 return;
159                 
160         resolveNodeNames(mg->ndef);
161
162         MapNode n_ore(ore);
163         ManualMapVoxelManipulator *vm = mg->vm;
164         PseudoRandom pr(blockseed + 4234);
165         int ymin, ymax;
166         
167         if (in_range & ORE_RANGE_MIRROR) {
168                 ymin = MYMAX(nmin.Y, -height_max);
169                 ymax = MYMIN(nmax.Y, -height_min);
170         } else {
171                 ymin = MYMAX(nmin.Y, height_min);
172                 ymax = MYMIN(nmax.Y, height_max);
173         }
174
175         if (clust_size >= ymax - ymin + 1)
176                 return;
177                 
178         int x0 = nmin.X;
179         int z0 = nmin.Z;
180         
181         int x1 = nmax.X;
182         int z1 = nmax.Z;
183         
184         int max_height = clust_size;
185         int y_start = pr.range(ymin, ymax - max_height);
186         
187         if (!noise) {
188                 int sx = nmax.X - nmin.X + 1;
189                 int sz = nmax.Z - nmin.Z + 1;
190                 noise = new Noise(np, 0, sx, sz);
191         }
192         noise->seed = mg->seed + y_start;
193         noise->perlinMap2D(x0, z0);
194         
195         int index = 0;
196         for (int z = z0; z != z1; z++)
197         for (int x = x0; x != x1; x++) {
198                 float noiseval = noise->result[index++];
199                 if (noiseval < nthresh)
200                         continue;
201                         
202                 int height = max_height * (1. / pr.range(1, 3));
203                 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
204                 int y1 = y0 + height;
205                 for (int y = y0; y != y1; y++) {
206                         u32 i = vm->m_area.index(x, y, z);
207                         if (!vm->m_area.contains(i))
208                                 continue;
209                                 
210                         if (vm->m_data[i].getContent() == wherein)
211                                 vm->m_data[i] = n_ore;
212                 }
213         }
214 }
215
216
217 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
218         bool isliquid, wasliquid;
219         v3s16 em  = vm->m_area.getExtent();
220
221         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
222                 for (s16 x = nmin.X; x <= nmax.X; x++) {
223                         wasliquid = true;
224                         
225                         u32 i = vm->m_area.index(x, nmax.Y, z);
226                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
227                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
228                                 
229                                 // there was a change between liquid and nonliquid, add to queue
230                                 if (isliquid != wasliquid)
231                                         trans_liquid->push_back(v3s16(x, y, z));
232
233                                 wasliquid = isliquid;
234                                 vm->m_area.add_y(em, i, -1);
235                         }
236                 }
237         }
238 }
239
240
241 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
242         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
243         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
244                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
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 - v3s16(1,0,1) * MAP_BLOCKSIZE,
281                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
282         bool block_is_underground = (water_level >= nmax.Y);
283
284         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
285         //TimeTaker t("updateLighting");
286
287         // first, send vertical rays of sunshine downward
288         v3s16 em = vm->m_area.getExtent();
289         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
290                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
291                         // see if we can get a light value from the overtop
292                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
293                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
294                                 if (block_is_underground)
295                                         continue;
296                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
297                                 continue;
298                         }
299                         vm->m_area.add_y(em, i, -1);
300
301                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
302                                 MapNode &n = vm->m_data[i];
303                                 if (!ndef->get(n).sunlight_propagates)
304                                         break;
305                                 n.param1 = LIGHT_SUN;
306                                 vm->m_area.add_y(em, i, -1);
307                         }
308                 }
309         }
310         
311         // now spread the sunlight and light up any sources
312         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
313                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
314                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
315                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
316                                 MapNode &n = vm->m_data[i];
317                                 if (n.getContent() == CONTENT_IGNORE ||
318                                         !ndef->get(n).light_propagates)
319                                         continue;
320                                 
321                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
322                                 if (light_produced)
323                                         n.param1 = light_produced;
324                                 
325                                 u8 light = n.param1 & 0x0F;
326                                 if (light) {
327                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
328                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
329                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
330                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
331                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
332                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
333                                 }
334                         }
335                 }
336         }
337         
338         //printf("updateLighting: %dms\n", t.stop());
339 }
340
341
342 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
343         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
344
345         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
346                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
347         bool block_is_underground = (water_level > nmax.Y);
348         bool sunlight = !block_is_underground;
349
350         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
351         
352         for (int i = 0; i < 2; i++) {
353                 enum LightBank bank = banks[i];
354                 std::set<v3s16> light_sources;
355                 std::map<v3s16, u8> unlight_from;
356
357                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
358                                         light_sources, unlight_from);
359                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
360
361                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
362                 vm->spreadLight(bank, light_sources, ndef);
363         }
364 }
365
366
367 //////////////////////// Mapgen V6 parameter read/write
368
369 bool MapgenV6Params::readParams(Settings *settings) {
370         freq_desert = settings->getFloat("mgv6_freq_desert");
371         freq_beach  = settings->getFloat("mgv6_freq_beach");
372
373         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
374         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
375         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
376         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
377         np_mud            = settings->getNoiseParams("mgv6_np_mud");
378         np_beach          = settings->getNoiseParams("mgv6_np_beach");
379         np_biome          = settings->getNoiseParams("mgv6_np_biome");
380         np_cave           = settings->getNoiseParams("mgv6_np_cave");
381         np_humidity       = settings->getNoiseParams("mgv6_np_humidity");
382         np_trees          = settings->getNoiseParams("mgv6_np_trees");
383         np_apple_trees    = settings->getNoiseParams("mgv6_np_apple_trees");
384
385         bool success =
386                 np_terrain_base  && np_terrain_higher && np_steepness &&
387                 np_height_select && np_trees          && np_mud       &&
388                 np_beach         && np_biome          && np_cave      &&
389                 np_humidity      && np_apple_trees;
390         return success;
391 }
392
393
394 void MapgenV6Params::writeParams(Settings *settings) {
395         settings->setFloat("mgv6_freq_desert", freq_desert);
396         settings->setFloat("mgv6_freq_beach",  freq_beach);
397         
398         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
399         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
400         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
401         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
402         settings->setNoiseParams("mgv6_np_mud",            np_mud);
403         settings->setNoiseParams("mgv6_np_beach",          np_beach);
404         settings->setNoiseParams("mgv6_np_biome",          np_biome);
405         settings->setNoiseParams("mgv6_np_cave",           np_cave);
406         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
407         settings->setNoiseParams("mgv6_np_trees",          np_trees);
408         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
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 }