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