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