]> git.lizzy.rs Git - minetest.git/blob - src/mg_ore.cpp
Ores: Add stratum ore (#6352)
[minetest.git] / src / mg_ore.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
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 <algorithm>
28
29
30 FlagDesc flagdesc_ore[] = {
31         {"absheight",                 OREFLAG_ABSHEIGHT}, // Non-functional
32         {"puff_cliffs",               OREFLAG_PUFF_CLIFFS},
33         {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
34         {NULL,                        0}
35 };
36
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40
41 OreManager::OreManager(IGameDef *gamedef) :
42         ObjDefManager(gamedef, OBJDEF_ORE)
43 {
44 }
45
46
47 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed,
48         v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
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, ore_zero_level);
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 ///////////////////////////////////////////////////////////////////////////////
76
77
78 Ore::~Ore()
79 {
80         delete noise;
81 }
82
83
84 void Ore::resolveNodeNames()
85 {
86         getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
87         getIdsFromNrBacklog(&c_wherein);
88 }
89
90
91 size_t Ore::placeOre(Mapgen *mg, u32 blockseed,
92         v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
93 {
94         // Ore y_min / y_max is displaced by ore_zero_level or remains unchanged.
95         // Any ore with a limit at +-MAX_MAP_GENERATION_LIMIT is considered to have
96         // that limit at +-infinity, so we do not alter that limit.
97         s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ?
98                 -MAX_MAP_GENERATION_LIMIT : y_min + ore_zero_level;
99
100         s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ?
101                 MAX_MAP_GENERATION_LIMIT : y_max + ore_zero_level;
102
103         if (nmin.Y > y_max_disp || nmax.Y < y_min_disp)
104                 return 0;
105
106         int actual_ymin = MYMAX(nmin.Y, y_min_disp);
107         int actual_ymax = MYMIN(nmax.Y, y_max_disp);
108         if (clust_size >= actual_ymax - actual_ymin + 1)
109                 return 0;
110
111         nmin.Y = actual_ymin;
112         nmax.Y = actual_ymax;
113         generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
114
115         return 1;
116 }
117
118
119 ///////////////////////////////////////////////////////////////////////////////
120
121
122 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
123         v3s16 nmin, v3s16 nmax, u8 *biomemap)
124 {
125         PcgRandom pr(blockseed);
126         MapNode n_ore(c_ore, 0, ore_param2);
127
128         u32 sizex  = (nmax.X - nmin.X + 1);
129         u32 volume = (nmax.X - nmin.X + 1) *
130                                  (nmax.Y - nmin.Y + 1) *
131                                  (nmax.Z - nmin.Z + 1);
132         u32 csize     = clust_size;
133         u32 cvolume    = csize * csize * csize;
134         u32 nclusters = volume / clust_scarcity;
135
136         for (u32 i = 0; i != nclusters; i++) {
137                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
138                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
139                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
140
141                 if ((flags & OREFLAG_USE_NOISE) &&
142                         (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
143                         continue;
144
145                 if (biomemap && !biomes.empty()) {
146                         u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
147                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
148                         if (it == biomes.end())
149                                 continue;
150                 }
151
152                 for (u32 z1 = 0; z1 != csize; z1++)
153                 for (u32 y1 = 0; y1 != csize; y1++)
154                 for (u32 x1 = 0; x1 != csize; x1++) {
155                         if (pr.range(1, cvolume) > clust_num_ores)
156                                 continue;
157
158                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
159                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
160                                 continue;
161
162                         vm->m_data[i] = n_ore;
163                 }
164         }
165 }
166
167
168 ///////////////////////////////////////////////////////////////////////////////
169
170
171 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
172         v3s16 nmin, v3s16 nmax, u8 *biomemap)
173 {
174         PcgRandom pr(blockseed + 4234);
175         MapNode n_ore(c_ore, 0, ore_param2);
176
177         u16 max_height = column_height_max;
178         int y_start_min = nmin.Y + max_height;
179         int y_start_max = nmax.Y - max_height;
180
181         int y_start = y_start_min < y_start_max ?
182                 pr.range(y_start_min, y_start_max) :
183                 (y_start_min + y_start_max) / 2;
184
185         if (!noise) {
186                 int sx = nmax.X - nmin.X + 1;
187                 int sz = nmax.Z - nmin.Z + 1;
188                 noise = new Noise(&np, 0, sx, sz);
189         }
190         noise->seed = mapseed + y_start;
191         noise->perlinMap2D(nmin.X, nmin.Z);
192
193         size_t index = 0;
194         for (int z = nmin.Z; z <= nmax.Z; z++)
195         for (int x = nmin.X; x <= nmax.X; x++, index++) {
196                 float noiseval = noise->result[index];
197                 if (noiseval < nthresh)
198                         continue;
199
200                 if (biomemap && !biomes.empty()) {
201                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
202                         if (it == biomes.end())
203                                 continue;
204                 }
205
206                 u16 height = pr.range(column_height_min, column_height_max);
207                 int ymidpoint = y_start + noiseval;
208                 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
209                 int y1 = MYMIN(nmax.Y, y0 + height - 1);
210
211                 for (int y = y0; y <= y1; y++) {
212                         u32 i = vm->m_area.index(x, y, z);
213                         if (!vm->m_area.contains(i))
214                                 continue;
215                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
216                                 continue;
217
218                         vm->m_data[i] = n_ore;
219                 }
220         }
221 }
222
223
224 ///////////////////////////////////////////////////////////////////////////////
225
226
227 OrePuff::~OrePuff()
228 {
229         delete noise_puff_top;
230         delete noise_puff_bottom;
231 }
232
233
234 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
235         v3s16 nmin, v3s16 nmax, u8 *biomemap)
236 {
237         PcgRandom pr(blockseed + 4234);
238         MapNode n_ore(c_ore, 0, ore_param2);
239
240         int y_start = pr.range(nmin.Y, nmax.Y);
241
242         if (!noise) {
243                 int sx = nmax.X - nmin.X + 1;
244                 int sz = nmax.Z - nmin.Z + 1;
245                 noise = new Noise(&np, 0, sx, sz);
246                 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
247                 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
248         }
249
250         noise->seed = mapseed + y_start;
251         noise->perlinMap2D(nmin.X, nmin.Z);
252         bool noise_generated = false;
253
254         size_t index = 0;
255         for (int z = nmin.Z; z <= nmax.Z; z++)
256         for (int x = nmin.X; x <= nmax.X; x++, index++) {
257                 float noiseval = noise->result[index];
258                 if (noiseval < nthresh)
259                         continue;
260
261                 if (biomemap && !biomes.empty()) {
262                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
263                         if (it == biomes.end())
264                                 continue;
265                 }
266
267                 if (!noise_generated) {
268                         noise_generated = true;
269                         noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
270                         noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
271                 }
272
273                 float ntop    = noise_puff_top->result[index];
274                 float nbottom = noise_puff_bottom->result[index];
275
276                 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
277                         float ndiff = noiseval - nthresh;
278                         if (ndiff < 1.0f) {
279                                 ntop *= ndiff;
280                                 nbottom *= ndiff;
281                         }
282                 }
283
284                 int ymid = y_start;
285                 int y0 = ymid - nbottom;
286                 int y1 = ymid + ntop;
287
288                 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
289                         SWAP(int, y0, y1);
290
291                 for (int y = y0; y <= y1; y++) {
292                         u32 i = vm->m_area.index(x, y, z);
293                         if (!vm->m_area.contains(i))
294                                 continue;
295                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
296                                 continue;
297
298                         vm->m_data[i] = n_ore;
299                 }
300         }
301 }
302
303
304 ///////////////////////////////////////////////////////////////////////////////
305
306
307 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
308         v3s16 nmin, v3s16 nmax, u8 *biomemap)
309 {
310         PcgRandom pr(blockseed + 2404);
311         MapNode n_ore(c_ore, 0, ore_param2);
312
313         u32 sizex  = (nmax.X - nmin.X + 1);
314         u32 volume = (nmax.X - nmin.X + 1) *
315                                  (nmax.Y - nmin.Y + 1) *
316                                  (nmax.Z - nmin.Z + 1);
317         u32 csize  = clust_size;
318         u32 nblobs = volume / clust_scarcity;
319
320         if (!noise)
321                 noise = new Noise(&np, mapseed, csize, csize, csize);
322
323         for (u32 i = 0; i != nblobs; i++) {
324                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
325                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
326                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
327
328                 if (biomemap && !biomes.empty()) {
329                         u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
330                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
331                         if (it == biomes.end())
332                                 continue;
333                 }
334
335                 bool noise_generated = false;
336                 noise->seed = blockseed + i;
337
338                 size_t index = 0;
339                 for (u32 z1 = 0; z1 != csize; z1++)
340                 for (u32 y1 = 0; y1 != csize; y1++)
341                 for (u32 x1 = 0; x1 != csize; x1++, index++) {
342                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
343                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
344                                 continue;
345
346                         // Lazily generate noise only if there's a chance of ore being placed
347                         // This simple optimization makes calls 6x faster on average
348                         if (!noise_generated) {
349                                 noise_generated = true;
350                                 noise->perlinMap3D(x0, y0, z0);
351                         }
352
353                         float noiseval = noise->result[index];
354
355                         float xdist = (s32)x1 - (s32)csize / 2;
356                         float ydist = (s32)y1 - (s32)csize / 2;
357                         float zdist = (s32)z1 - (s32)csize / 2;
358
359                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
360
361                         if (noiseval < nthresh)
362                                 continue;
363
364                         vm->m_data[i] = n_ore;
365                 }
366         }
367 }
368
369
370 ///////////////////////////////////////////////////////////////////////////////
371
372
373 OreVein::~OreVein()
374 {
375         delete noise2;
376 }
377
378
379 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
380         v3s16 nmin, v3s16 nmax, u8 *biomemap)
381 {
382         PcgRandom pr(blockseed + 520);
383         MapNode n_ore(c_ore, 0, ore_param2);
384
385         u32 sizex = (nmax.X - nmin.X + 1);
386
387         if (!noise) {
388                 int sx = nmax.X - nmin.X + 1;
389                 int sy = nmax.Y - nmin.Y + 1;
390                 int sz = nmax.Z - nmin.Z + 1;
391                 noise  = new Noise(&np, mapseed, sx, sy, sz);
392                 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
393         }
394         bool noise_generated = false;
395
396         size_t index = 0;
397         for (int z = nmin.Z; z <= nmax.Z; z++)
398         for (int y = nmin.Y; y <= nmax.Y; y++)
399         for (int x = nmin.X; x <= nmax.X; x++, index++) {
400                 u32 i = vm->m_area.index(x, y, z);
401                 if (!vm->m_area.contains(i))
402                         continue;
403                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
404                         continue;
405
406                 if (biomemap && !biomes.empty()) {
407                         u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
408                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
409                         if (it == biomes.end())
410                                 continue;
411                 }
412
413                 // Same lazy generation optimization as in OreBlob
414                 if (!noise_generated) {
415                         noise_generated = true;
416                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
417                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
418                 }
419
420                 // randval ranges from -1..1
421                 float randval   = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
422                 float noiseval  = contour(noise->result[index]);
423                 float noiseval2 = contour(noise2->result[index]);
424                 if (noiseval * noiseval2 + randval * random_factor < nthresh)
425                         continue;
426
427                 vm->m_data[i] = n_ore;
428         }
429 }
430
431
432 ///////////////////////////////////////////////////////////////////////////////
433
434
435 OreStratum::~OreStratum()
436 {
437         delete noise_stratum_thickness;
438 }
439
440
441 void OreStratum::generate(MMVManip *vm, int mapseed, u32 blockseed,
442         v3s16 nmin, v3s16 nmax, u8 *biomemap)
443 {
444         PcgRandom pr(blockseed + 4234);
445         MapNode n_ore(c_ore, 0, ore_param2);
446
447         if (!noise) {
448                 int sx = nmax.X - nmin.X + 1;
449                 int sz = nmax.Z - nmin.Z + 1;
450                 noise = new Noise(&np, 0, sx, sz);
451                 noise_stratum_thickness = new Noise(&np_stratum_thickness, 0, sx, sz);
452         }
453         noise->perlinMap2D(nmin.X, nmin.Z);
454         noise_stratum_thickness->perlinMap2D(nmin.X, nmin.Z);
455
456         size_t index = 0;
457
458         for (int z = nmin.Z; z <= nmax.Z; z++)
459         for (int x = nmin.X; x <= nmax.X; x++, index++) {
460                 if (biomemap && !biomes.empty()) {
461                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
462                         if (it == biomes.end())
463                                 continue;
464                 }
465
466                 float nmid = noise->result[index];
467                 float nhalfthick = noise_stratum_thickness->result[index] / 2.0f;
468                 int y0 = MYMAX(nmin.Y, nmid - nhalfthick);
469                 int y1 = MYMIN(nmax.Y, nmid + nhalfthick);
470
471                 for (int y = y0; y <= y1; y++) {
472                         if (pr.range(1, clust_scarcity) != 1)
473                                 continue;
474
475                         u32 i = vm->m_area.index(x, y, z);
476                         if (!vm->m_area.contains(i))
477                                 continue;
478                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
479                                 continue;
480
481                         vm->m_data[i] = n_ore;
482                 }
483         }
484 }