]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mg_ore.cpp
5af8e31e4f6ee2d48c591bb9a3b9771ccb08e484
[dragonfireclient.git] / src / mapgen / mg_ore.cpp
1 /*
2 Minetest
3 Copyright (C) 2015-2020 paramat
4 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "mg_ore.h"
22 #include "mapgen.h"
23 #include "noise.h"
24 #include "map.h"
25 #include "log.h"
26 #include "util/numeric.h"
27 #include <cmath>
28 #include <algorithm>
29
30 FlagDesc flagdesc_ore[] = {{"absheight", OREFLAG_ABSHEIGHT}, // Non-functional
31                 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
32                 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE}, {NULL, 0}};
33
34 ///////////////////////////////////////////////////////////////////////////////
35
36 OreManager::OreManager(IGameDef *gamedef) : ObjDefManager(gamedef, OBJDEF_ORE)
37 {
38 }
39
40 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
41 {
42         size_t nplaced = 0;
43
44         for (size_t i = 0; i != m_objects.size(); i++) {
45                 Ore *ore = (Ore *)m_objects[i];
46                 if (!ore)
47                         continue;
48
49                 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
50                 blockseed++;
51         }
52
53         return nplaced;
54 }
55
56 void OreManager::clear()
57 {
58         for (ObjDef *object : m_objects) {
59                 Ore *ore = (Ore *)object;
60                 delete ore;
61         }
62         m_objects.clear();
63 }
64
65 OreManager *OreManager::clone() const
66 {
67         auto mgr = new OreManager();
68         ObjDefManager::cloneTo(mgr);
69         return mgr;
70 }
71
72 ///////////////////////////////////////////////////////////////////////////////
73
74 Ore::~Ore()
75 {
76         delete noise;
77 }
78
79 void Ore::resolveNodeNames()
80 {
81         getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
82         getIdsFromNrBacklog(&c_wherein);
83 }
84
85 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
86 {
87         if (nmin.Y > y_max || nmax.Y < y_min)
88                 return 0;
89
90         int actual_ymin = MYMAX(nmin.Y, y_min);
91         int actual_ymax = MYMIN(nmax.Y, y_max);
92         if (clust_size >= actual_ymax - actual_ymin + 1)
93                 return 0;
94
95         nmin.Y = actual_ymin;
96         nmax.Y = actual_ymax;
97         generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
98
99         return 1;
100 }
101
102 void Ore::cloneTo(Ore *def) const
103 {
104         ObjDef::cloneTo(def);
105         NodeResolver::cloneTo(def);
106         def->c_ore = c_ore;
107         def->c_wherein = c_wherein;
108         def->clust_scarcity = clust_scarcity;
109         def->clust_num_ores = clust_num_ores;
110         def->clust_size = clust_size;
111         def->y_min = y_min;
112         def->y_max = y_max;
113         def->ore_param2 = ore_param2;
114         def->flags = flags;
115         def->nthresh = nthresh;
116         def->np = np;
117         def->noise = nullptr; // cannot be shared! so created on demand
118         def->biomes = biomes;
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////
122
123 ObjDef *OreScatter::clone() const
124 {
125         auto def = new OreScatter();
126         Ore::cloneTo(def);
127         return def;
128 }
129
130 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin,
131                 v3s16 nmax, biome_t *biomemap)
132 {
133         PcgRandom pr(blockseed);
134         MapNode n_ore(c_ore, 0, ore_param2);
135
136         u32 sizex = (nmax.X - nmin.X + 1);
137         u32 volume = (nmax.X - nmin.X + 1) * (nmax.Y - nmin.Y + 1) *
138                      (nmax.Z - nmin.Z + 1);
139         u32 csize = clust_size;
140         u32 cvolume = csize * csize * csize;
141         u32 nclusters = volume / clust_scarcity;
142
143         for (u32 i = 0; i != nclusters; i++) {
144                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
145                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
146                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
147
148                 if ((flags & OREFLAG_USE_NOISE) &&
149                                 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
150                         continue;
151
152                 if (biomemap && !biomes.empty()) {
153                         u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
154                         auto it = biomes.find(biomemap[index]);
155                         if (it == biomes.end())
156                                 continue;
157                 }
158
159                 for (u32 z1 = 0; z1 != csize; z1++)
160                         for (u32 y1 = 0; y1 != csize; y1++)
161                                 for (u32 x1 = 0; x1 != csize; x1++) {
162                                         if (pr.range(1, cvolume) > clust_num_ores)
163                                                 continue;
164
165                                         u32 i = vm->m_area.index(
166                                                         x0 + x1, y0 + y1, z0 + z1);
167                                         if (!CONTAINS(c_wherein,
168                                                             vm->m_data[i].getContent()))
169                                                 continue;
170
171                                         vm->m_data[i] = n_ore;
172                                 }
173         }
174 }
175
176 ///////////////////////////////////////////////////////////////////////////////
177
178 ObjDef *OreSheet::clone() const
179 {
180         auto def = new OreSheet();
181         Ore::cloneTo(def);
182
183         def->column_height_max = column_height_max;
184         def->column_height_min = column_height_min;
185         def->column_midpoint_factor = column_midpoint_factor;
186
187         return def;
188 }
189
190 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
191                 biome_t *biomemap)
192 {
193         PcgRandom pr(blockseed + 4234);
194         MapNode n_ore(c_ore, 0, ore_param2);
195
196         u16 max_height = column_height_max;
197         int y_start_min = nmin.Y + max_height;
198         int y_start_max = nmax.Y - max_height;
199
200         int y_start = y_start_min < y_start_max ? pr.range(y_start_min, y_start_max)
201                                                 : (y_start_min + y_start_max) / 2;
202
203         if (!noise) {
204                 int sx = nmax.X - nmin.X + 1;
205                 int sz = nmax.Z - nmin.Z + 1;
206                 noise = new Noise(&np, 0, sx, sz);
207         }
208         noise->seed = mapseed + y_start;
209         noise->perlinMap2D(nmin.X, nmin.Z);
210
211         size_t index = 0;
212         for (int z = nmin.Z; z <= nmax.Z; z++)
213                 for (int x = nmin.X; x <= nmax.X; x++, index++) {
214                         float noiseval = noise->result[index];
215                         if (noiseval < nthresh)
216                                 continue;
217
218                         if (biomemap && !biomes.empty()) {
219                                 auto it = biomes.find(biomemap[index]);
220                                 if (it == biomes.end())
221                                         continue;
222                         }
223
224                         u16 height = pr.range(column_height_min, column_height_max);
225                         int ymidpoint = y_start + noiseval;
226                         int y0 = MYMAX(nmin.Y,
227                                         ymidpoint - height * (1 - column_midpoint_factor));
228                         int y1 = MYMIN(nmax.Y, y0 + height - 1);
229
230                         for (int y = y0; y <= y1; y++) {
231                                 u32 i = vm->m_area.index(x, y, z);
232                                 if (!vm->m_area.contains(i))
233                                         continue;
234                                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
235                                         continue;
236
237                                 vm->m_data[i] = n_ore;
238                         }
239                 }
240 }
241
242 ///////////////////////////////////////////////////////////////////////////////
243
244 OrePuff::~OrePuff()
245 {
246         delete noise_puff_top;
247         delete noise_puff_bottom;
248 }
249
250 ObjDef *OrePuff::clone() const
251 {
252         auto def = new OrePuff();
253         Ore::cloneTo(def);
254
255         def->np_puff_top = np_puff_top;
256         def->np_puff_bottom = np_puff_bottom;
257         def->noise_puff_top = nullptr; // cannot be shared, on-demand
258         def->noise_puff_bottom = nullptr;
259
260         return def;
261 }
262
263 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
264                 biome_t *biomemap)
265 {
266         PcgRandom pr(blockseed + 4234);
267         MapNode n_ore(c_ore, 0, ore_param2);
268
269         int y_start = pr.range(nmin.Y, nmax.Y);
270
271         if (!noise) {
272                 int sx = nmax.X - nmin.X + 1;
273                 int sz = nmax.Z - nmin.Z + 1;
274                 noise = new Noise(&np, 0, sx, sz);
275                 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
276                 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
277         }
278
279         noise->seed = mapseed + y_start;
280         noise->perlinMap2D(nmin.X, nmin.Z);
281         bool noise_generated = false;
282
283         size_t index = 0;
284         for (int z = nmin.Z; z <= nmax.Z; z++)
285                 for (int x = nmin.X; x <= nmax.X; x++, index++) {
286                         float noiseval = noise->result[index];
287                         if (noiseval < nthresh)
288                                 continue;
289
290                         if (biomemap && !biomes.empty()) {
291                                 auto it = biomes.find(biomemap[index]);
292                                 if (it == biomes.end())
293                                         continue;
294                         }
295
296                         if (!noise_generated) {
297                                 noise_generated = true;
298                                 noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
299                                 noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
300                         }
301
302                         float ntop = noise_puff_top->result[index];
303                         float nbottom = noise_puff_bottom->result[index];
304
305                         if (!(flags & OREFLAG_PUFF_CLIFFS)) {
306                                 float ndiff = noiseval - nthresh;
307                                 if (ndiff < 1.0f) {
308                                         ntop *= ndiff;
309                                         nbottom *= ndiff;
310                                 }
311                         }
312
313                         int ymid = y_start;
314                         int y0 = ymid - nbottom;
315                         int y1 = ymid + ntop;
316
317                         if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
318                                 SWAP(int, y0, y1);
319
320                         for (int y = y0; y <= y1; y++) {
321                                 u32 i = vm->m_area.index(x, y, z);
322                                 if (!vm->m_area.contains(i))
323                                         continue;
324                                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
325                                         continue;
326
327                                 vm->m_data[i] = n_ore;
328                         }
329                 }
330 }
331
332 ///////////////////////////////////////////////////////////////////////////////
333
334 ObjDef *OreBlob::clone() const
335 {
336         auto def = new OreBlob();
337         Ore::cloneTo(def);
338         return def;
339 }
340
341 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
342                 biome_t *biomemap)
343 {
344         PcgRandom pr(blockseed + 2404);
345         MapNode n_ore(c_ore, 0, ore_param2);
346
347         u32 sizex = (nmax.X - nmin.X + 1);
348         u32 volume = (nmax.X - nmin.X + 1) * (nmax.Y - nmin.Y + 1) *
349                      (nmax.Z - nmin.Z + 1);
350         u32 csize = clust_size;
351         u32 nblobs = volume / clust_scarcity;
352
353         if (!noise)
354                 noise = new Noise(&np, mapseed, csize, csize, csize);
355
356         for (u32 i = 0; i != nblobs; i++) {
357                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
358                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
359                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
360
361                 if (biomemap && !biomes.empty()) {
362                         u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
363                         auto it = biomes.find(biomemap[bmapidx]);
364                         if (it == biomes.end())
365                                 continue;
366                 }
367
368                 bool noise_generated = false;
369                 noise->seed = blockseed + i;
370
371                 size_t index = 0;
372                 for (u32 z1 = 0; z1 != csize; z1++)
373                         for (u32 y1 = 0; y1 != csize; y1++)
374                                 for (u32 x1 = 0; x1 != csize; x1++, index++) {
375                                         u32 i = vm->m_area.index(
376                                                         x0 + x1, y0 + y1, z0 + z1);
377                                         if (!CONTAINS(c_wherein,
378                                                             vm->m_data[i].getContent()))
379                                                 continue;
380
381                                         // Lazily generate noise only if there's a chance
382                                         // of ore being placed This simple optimization
383                                         // makes calls 6x faster on average
384                                         if (!noise_generated) {
385                                                 noise_generated = true;
386                                                 noise->perlinMap3D(x0, y0, z0);
387                                         }
388
389                                         float noiseval = noise->result[index];
390
391                                         float xdist = (s32)x1 - (s32)csize / 2;
392                                         float ydist = (s32)y1 - (s32)csize / 2;
393                                         float zdist = (s32)z1 - (s32)csize / 2;
394
395                                         noiseval -= std::sqrt(xdist * xdist +
396                                                                     ydist * ydist +
397                                                                     zdist * zdist) /
398                                                     csize;
399
400                                         if (noiseval < nthresh)
401                                                 continue;
402
403                                         vm->m_data[i] = n_ore;
404                                 }
405         }
406 }
407
408 ///////////////////////////////////////////////////////////////////////////////
409
410 OreVein::~OreVein()
411 {
412         delete noise2;
413 }
414
415 ObjDef *OreVein::clone() const
416 {
417         auto def = new OreVein();
418         Ore::cloneTo(def);
419
420         def->random_factor = random_factor;
421         def->noise2 = nullptr; // cannot be shared, on-demand
422         def->sizey_prev = sizey_prev;
423
424         return def;
425 }
426
427 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
428                 biome_t *biomemap)
429 {
430         PcgRandom pr(blockseed + 520);
431         MapNode n_ore(c_ore, 0, ore_param2);
432
433         int sizex = nmax.X - nmin.X + 1;
434         int sizey = nmax.Y - nmin.Y + 1;
435         // Because this ore uses 3D noise the perlinmap Y size can be different in
436         // different mapchunks due to ore Y limits. So recreate the noise objects
437         // if Y size has changed.
438         // Because these noise objects are created multiple times for this ore type
439         // it is necessary to 'delete' them here.
440         if (!noise || sizey != sizey_prev) {
441                 delete noise;
442                 delete noise2;
443                 int sizez = nmax.Z - nmin.Z + 1;
444                 noise = new Noise(&np, mapseed, sizex, sizey, sizez);
445                 noise2 = new Noise(&np, mapseed + 436, sizex, sizey, sizez);
446                 sizey_prev = sizey;
447         }
448
449         bool noise_generated = false;
450         size_t index = 0;
451         for (int z = nmin.Z; z <= nmax.Z; z++)
452                 for (int y = nmin.Y; y <= nmax.Y; y++)
453                         for (int x = nmin.X; x <= nmax.X; x++, index++) {
454                                 u32 i = vm->m_area.index(x, y, z);
455                                 if (!vm->m_area.contains(i))
456                                         continue;
457                                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
458                                         continue;
459
460                                 if (biomemap && !biomes.empty()) {
461                                         u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
462                                         auto it = biomes.find(biomemap[bmapidx]);
463                                         if (it == biomes.end())
464                                                 continue;
465                                 }
466
467                                 // Same lazy generation optimization as in OreBlob
468                                 if (!noise_generated) {
469                                         noise_generated = true;
470                                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
471                                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
472                                 }
473
474                                 // randval ranges from -1..1
475                                 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) -
476                                                 1.f;
477                                 float noiseval = contour(noise->result[index]);
478                                 float noiseval2 = contour(noise2->result[index]);
479                                 if (noiseval * noiseval2 + randval * random_factor <
480                                                 nthresh)
481                                         continue;
482
483                                 vm->m_data[i] = n_ore;
484                         }
485 }
486
487 ///////////////////////////////////////////////////////////////////////////////
488
489 OreStratum::~OreStratum()
490 {
491         delete noise_stratum_thickness;
492 }
493
494 ObjDef *OreStratum::clone() const
495 {
496         auto def = new OreStratum();
497         Ore::cloneTo(def);
498
499         def->np_stratum_thickness = np_stratum_thickness;
500         def->noise_stratum_thickness = nullptr; // cannot be shared, on-demand
501         def->stratum_thickness = stratum_thickness;
502
503         return def;
504 }
505
506 void OreStratum::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin,
507                 v3s16 nmax, biome_t *biomemap)
508 {
509         PcgRandom pr(blockseed + 4234);
510         MapNode n_ore(c_ore, 0, ore_param2);
511
512         if (flags & OREFLAG_USE_NOISE) {
513                 if (!noise) {
514                         int sx = nmax.X - nmin.X + 1;
515                         int sz = nmax.Z - nmin.Z + 1;
516                         noise = new Noise(&np, 0, sx, sz);
517                 }
518                 noise->perlinMap2D(nmin.X, nmin.Z);
519         }
520
521         if (flags & OREFLAG_USE_NOISE2) {
522                 if (!noise_stratum_thickness) {
523                         int sx = nmax.X - nmin.X + 1;
524                         int sz = nmax.Z - nmin.Z + 1;
525                         noise_stratum_thickness =
526                                         new Noise(&np_stratum_thickness, 0, sx, sz);
527                 }
528                 noise_stratum_thickness->perlinMap2D(nmin.X, nmin.Z);
529         }
530
531         size_t index = 0;
532
533         for (int z = nmin.Z; z <= nmax.Z; z++)
534                 for (int x = nmin.X; x <= nmax.X; x++, index++) {
535                         if (biomemap && !biomes.empty()) {
536                                 auto it = biomes.find(biomemap[index]);
537                                 if (it == biomes.end())
538                                         continue;
539                         }
540
541                         int y0;
542                         int y1;
543
544                         if (flags & OREFLAG_USE_NOISE) {
545                                 float nhalfthick =
546                                                 ((flags & OREFLAG_USE_NOISE2) ? noise_stratum_thickness
547                                                                                                 ->result[index]
548                                                                               : (float)stratum_thickness) /
549                                                 2.0f;
550                                 float nmid = noise->result[index];
551                                 y0 = MYMAX(nmin.Y, std::ceil(nmid - nhalfthick));
552                                 y1 = MYMIN(nmax.Y, nmid + nhalfthick);
553                         } else { // Simple horizontal stratum
554                                 y0 = nmin.Y;
555                                 y1 = nmax.Y;
556                         }
557
558                         for (int y = y0; y <= y1; y++) {
559                                 if (pr.range(1, clust_scarcity) != 1)
560                                         continue;
561
562                                 u32 i = vm->m_area.index(x, y, z);
563                                 if (!vm->m_area.contains(i))
564                                         continue;
565                                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
566                                         continue;
567
568                                 vm->m_data[i] = n_ore;
569                         }
570                 }
571 }