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