Performance fix + SAO factorization
[oweals/minetest.git] / src / mg_ore.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mg_ore.h"
21 #include "mapgen.h"
22 #include "noise.h"
23 #include "util/numeric.h"
24 #include "map.h"
25 #include "log.h"
26
27 FlagDesc flagdesc_ore[] = {
28         {"absheight",                 OREFLAG_ABSHEIGHT},
29         {"puff_cliffs",               OREFLAG_PUFF_CLIFFS},
30         {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
31         {NULL,                        0}
32 };
33
34
35 ///////////////////////////////////////////////////////////////////////////////
36
37
38 OreManager::OreManager(IGameDef *gamedef) :
39         ObjDefManager(gamedef, OBJDEF_ORE)
40 {
41 }
42
43
44 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
45 {
46         size_t nplaced = 0;
47
48         for (size_t i = 0; i != m_objects.size(); i++) {
49                 Ore *ore = (Ore *)m_objects[i];
50                 if (!ore)
51                         continue;
52
53                 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
54                 blockseed++;
55         }
56
57         return nplaced;
58 }
59
60
61 void OreManager::clear()
62 {
63         for (size_t i = 0; i < m_objects.size(); i++) {
64                 Ore *ore = (Ore *)m_objects[i];
65                 delete ore;
66         }
67         m_objects.clear();
68 }
69
70
71 ///////////////////////////////////////////////////////////////////////////////
72
73
74 Ore::Ore()
75 {
76         flags = 0;
77         noise = NULL;
78 }
79
80
81 Ore::~Ore()
82 {
83         delete noise;
84 }
85
86
87 void Ore::resolveNodeNames()
88 {
89         getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
90         getIdsFromNrBacklog(&c_wherein);
91 }
92
93
94 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
95 {
96         int in_range = 0;
97
98         in_range |= (nmin.Y <= y_max && nmax.Y >= y_min);
99         if (flags & OREFLAG_ABSHEIGHT)
100                 in_range |= (nmin.Y >= -y_max && nmax.Y <= -y_min) << 1;
101         if (!in_range)
102                 return 0;
103
104         int actual_ymin, actual_ymax;
105         if (in_range & ORE_RANGE_MIRROR) {
106                 actual_ymin = MYMAX(nmin.Y, -y_max);
107                 actual_ymax = MYMIN(nmax.Y, -y_min);
108         } else {
109                 actual_ymin = MYMAX(nmin.Y, y_min);
110                 actual_ymax = MYMIN(nmax.Y, y_max);
111         }
112         if (clust_size >= actual_ymax - actual_ymin + 1)
113                 return 0;
114
115         nmin.Y = actual_ymin;
116         nmax.Y = actual_ymax;
117         generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
118
119         return 1;
120 }
121
122
123 ///////////////////////////////////////////////////////////////////////////////
124
125
126 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
127         v3s16 nmin, v3s16 nmax, u8 *biomemap)
128 {
129         PcgRandom pr(blockseed);
130         MapNode n_ore(c_ore, 0, ore_param2);
131
132         u32 sizex  = (nmax.X - nmin.X + 1);
133         u32 volume = (nmax.X - nmin.X + 1) *
134                                  (nmax.Y - nmin.Y + 1) *
135                                  (nmax.Z - nmin.Z + 1);
136         u32 csize     = clust_size;
137         u32 cvolume    = csize * csize * csize;
138         u32 nclusters = volume / clust_scarcity;
139
140         for (u32 i = 0; i != nclusters; i++) {
141                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
142                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
143                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
144
145                 if ((flags & OREFLAG_USE_NOISE) &&
146                         (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
147                         continue;
148
149                 if (biomemap && !biomes.empty()) {
150                         u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
151                         UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
152                         if (it == biomes.end())
153                                 continue;
154                 }
155
156                 for (u32 z1 = 0; z1 != csize; z1++)
157                 for (u32 y1 = 0; y1 != csize; y1++)
158                 for (u32 x1 = 0; x1 != csize; x1++) {
159                         if (pr.range(1, cvolume) > clust_num_ores)
160                                 continue;
161
162                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
163                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
164                                 continue;
165
166                         vm->m_data[i] = n_ore;
167                 }
168         }
169 }
170
171
172 ///////////////////////////////////////////////////////////////////////////////
173
174
175 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
176         v3s16 nmin, v3s16 nmax, u8 *biomemap)
177 {
178         PcgRandom pr(blockseed + 4234);
179         MapNode n_ore(c_ore, 0, ore_param2);
180
181         u16 max_height = column_height_max;
182         int y_start_min = nmin.Y + max_height;
183         int y_start_max = nmax.Y - max_height;
184
185         int y_start = y_start_min < y_start_max ?
186                 pr.range(y_start_min, y_start_max) :
187                 (y_start_min + y_start_max) / 2;
188
189         if (!noise) {
190                 int sx = nmax.X - nmin.X + 1;
191                 int sz = nmax.Z - nmin.Z + 1;
192                 noise = new Noise(&np, 0, sx, sz);
193         }
194         noise->seed = mapseed + y_start;
195         noise->perlinMap2D(nmin.X, nmin.Z);
196
197         size_t index = 0;
198         for (int z = nmin.Z; z <= nmax.Z; z++)
199         for (int x = nmin.X; x <= nmax.X; x++, index++) {
200                 float noiseval = noise->result[index];
201                 if (noiseval < nthresh)
202                         continue;
203
204                 if (biomemap && !biomes.empty()) {
205                         UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
206                         if (it == biomes.end())
207                                 continue;
208                 }
209
210                 u16 height = pr.range(column_height_min, column_height_max);
211                 int ymidpoint = y_start + noiseval;
212                 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
213                 int y1 = MYMIN(nmax.Y, y0 + height - 1);
214
215                 for (int y = y0; y <= y1; y++) {
216                         u32 i = vm->m_area.index(x, y, z);
217                         if (!vm->m_area.contains(i))
218                                 continue;
219                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
220                                 continue;
221
222                         vm->m_data[i] = n_ore;
223                 }
224         }
225 }
226
227
228 ///////////////////////////////////////////////////////////////////////////////
229
230 OrePuff::OrePuff() :
231         Ore()
232 {
233         noise_puff_top    = NULL;
234         noise_puff_bottom = NULL;
235 }
236
237
238 OrePuff::~OrePuff()
239 {
240         delete noise_puff_top;
241         delete noise_puff_bottom;
242 }
243
244
245 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
246         v3s16 nmin, v3s16 nmax, u8 *biomemap)
247 {
248         PcgRandom pr(blockseed + 4234);
249         MapNode n_ore(c_ore, 0, ore_param2);
250
251         int y_start = pr.range(nmin.Y, nmax.Y);
252
253         if (!noise) {
254                 int sx = nmax.X - nmin.X + 1;
255                 int sz = nmax.Z - nmin.Z + 1;
256                 noise = new Noise(&np, 0, sx, sz);
257                 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
258                 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
259         }
260
261         noise->seed = mapseed + y_start;
262         noise->perlinMap2D(nmin.X, nmin.Z);
263         bool noise_generated = false;
264
265         size_t index = 0;
266         for (int z = nmin.Z; z <= nmax.Z; z++)
267         for (int x = nmin.X; x <= nmax.X; x++, index++) {
268                 float noiseval = noise->result[index];
269                 if (noiseval < nthresh)
270                         continue;
271
272                 if (biomemap && !biomes.empty()) {
273                         UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
274                         if (it == biomes.end())
275                                 continue;
276                 }
277
278                 if (!noise_generated) {
279                         noise_generated = true;
280                         noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
281                         noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
282                 }
283
284                 float ntop    = noise_puff_top->result[index];
285                 float nbottom = noise_puff_bottom->result[index];
286
287                 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
288                         float ndiff = noiseval - nthresh;
289                         if (ndiff < 1.0f) {
290                                 ntop *= ndiff;
291                                 nbottom *= ndiff;
292                         }
293                 }
294
295                 int ymid = y_start;
296                 int y0 = ymid - nbottom;
297                 int y1 = ymid + ntop;
298
299                 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
300                         SWAP(int, y0, y1);
301
302                 for (int y = y0; y <= y1; y++) {
303                         u32 i = vm->m_area.index(x, y, z);
304                         if (!vm->m_area.contains(i))
305                                 continue;
306                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
307                                 continue;
308
309                         vm->m_data[i] = n_ore;
310                 }
311         }
312 }
313
314
315 ///////////////////////////////////////////////////////////////////////////////
316
317
318 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
319         v3s16 nmin, v3s16 nmax, u8 *biomemap)
320 {
321         PcgRandom pr(blockseed + 2404);
322         MapNode n_ore(c_ore, 0, ore_param2);
323
324         u32 sizex  = (nmax.X - nmin.X + 1);
325         u32 volume = (nmax.X - nmin.X + 1) *
326                                  (nmax.Y - nmin.Y + 1) *
327                                  (nmax.Z - nmin.Z + 1);
328         u32 csize  = clust_size;
329         u32 nblobs = volume / clust_scarcity;
330
331         if (!noise)
332                 noise = new Noise(&np, mapseed, csize, csize, csize);
333
334         for (u32 i = 0; i != nblobs; i++) {
335                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
336                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
337                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
338
339                 if (biomemap && !biomes.empty()) {
340                         u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
341                         UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
342                         if (it == biomes.end())
343                                 continue;
344                 }
345
346                 bool noise_generated = false;
347                 noise->seed = blockseed + i;
348
349                 size_t index = 0;
350                 for (u32 z1 = 0; z1 != csize; z1++)
351                 for (u32 y1 = 0; y1 != csize; y1++)
352                 for (u32 x1 = 0; x1 != csize; x1++, index++) {
353                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
354                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
355                                 continue;
356
357                         // Lazily generate noise only if there's a chance of ore being placed
358                         // This simple optimization makes calls 6x faster on average
359                         if (!noise_generated) {
360                                 noise_generated = true;
361                                 noise->perlinMap3D(x0, y0, z0);
362                         }
363
364                         float noiseval = noise->result[index];
365
366                         float xdist = (s32)x1 - (s32)csize / 2;
367                         float ydist = (s32)y1 - (s32)csize / 2;
368                         float zdist = (s32)z1 - (s32)csize / 2;
369
370                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
371
372                         if (noiseval < nthresh)
373                                 continue;
374
375                         vm->m_data[i] = n_ore;
376                 }
377         }
378 }
379
380
381 ///////////////////////////////////////////////////////////////////////////////
382
383 OreVein::OreVein() :
384         Ore()
385 {
386         noise2 = NULL;
387 }
388
389
390 OreVein::~OreVein()
391 {
392         delete noise2;
393 }
394
395
396 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
397         v3s16 nmin, v3s16 nmax, u8 *biomemap)
398 {
399         PcgRandom pr(blockseed + 520);
400         MapNode n_ore(c_ore, 0, ore_param2);
401
402         u32 sizex = (nmax.X - nmin.X + 1);
403
404         if (!noise) {
405                 int sx = nmax.X - nmin.X + 1;
406                 int sy = nmax.Y - nmin.Y + 1;
407                 int sz = nmax.Z - nmin.Z + 1;
408                 noise  = new Noise(&np, mapseed, sx, sy, sz);
409                 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
410         }
411         bool noise_generated = false;
412
413         size_t index = 0;
414         for (int z = nmin.Z; z <= nmax.Z; z++)
415         for (int y = nmin.Y; y <= nmax.Y; y++)
416         for (int x = nmin.X; x <= nmax.X; x++, index++) {
417                 u32 i = vm->m_area.index(x, y, z);
418                 if (!vm->m_area.contains(i))
419                         continue;
420                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
421                         continue;
422
423                 if (biomemap && !biomes.empty()) {
424                         u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
425                         UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
426                         if (it == biomes.end())
427                                 continue;
428                 }
429
430                 // Same lazy generation optimization as in OreBlob
431                 if (!noise_generated) {
432                         noise_generated = true;
433                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
434                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
435                 }
436
437                 // randval ranges from -1..1
438                 float randval   = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
439                 float noiseval  = contour(noise->result[index]);
440                 float noiseval2 = contour(noise2->result[index]);
441                 if (noiseval * noiseval2 + randval * random_factor < nthresh)
442                         continue;
443
444                 vm->m_data[i] = n_ore;
445         }
446 }