3 Copyright (C) 2015-2020 paramat
4 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
26 #include "util/numeric.h"
30 FlagDesc flagdesc_ore[] = {{"absheight", OREFLAG_ABSHEIGHT}, // Non-functional
31 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
32 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE}, {NULL, 0}};
34 ///////////////////////////////////////////////////////////////////////////////
36 OreManager::OreManager(IGameDef *gamedef) : ObjDefManager(gamedef, OBJDEF_ORE)
40 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
44 for (size_t i = 0; i != m_objects.size(); i++) {
45 Ore *ore = (Ore *)m_objects[i];
49 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
56 void OreManager::clear()
58 for (ObjDef *object : m_objects) {
59 Ore *ore = (Ore *)object;
65 OreManager *OreManager::clone() const
67 auto mgr = new OreManager();
68 ObjDefManager::cloneTo(mgr);
72 ///////////////////////////////////////////////////////////////////////////////
79 void Ore::resolveNodeNames()
81 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
82 getIdsFromNrBacklog(&c_wherein);
85 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
87 if (nmin.Y > y_max || nmax.Y < y_min)
90 int actual_ymin = MYMAX(nmin.Y, y_min);
91 int actual_ymax = MYMIN(nmax.Y, y_max);
92 if (clust_size >= actual_ymax - actual_ymin + 1)
97 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
102 void Ore::cloneTo(Ore *def) const
104 ObjDef::cloneTo(def);
105 NodeResolver::cloneTo(def);
107 def->c_wherein = c_wherein;
108 def->clust_scarcity = clust_scarcity;
109 def->clust_num_ores = clust_num_ores;
110 def->clust_size = clust_size;
113 def->ore_param2 = ore_param2;
115 def->nthresh = nthresh;
117 def->noise = nullptr; // cannot be shared! so created on demand
118 def->biomes = biomes;
121 ///////////////////////////////////////////////////////////////////////////////
123 ObjDef *OreScatter::clone() const
125 auto def = new OreScatter();
130 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin,
131 v3s16 nmax, biome_t *biomemap)
133 PcgRandom pr(blockseed);
134 MapNode n_ore(c_ore, 0, ore_param2);
136 u32 sizex = (nmax.X - nmin.X + 1);
137 u32 volume = (nmax.X - nmin.X + 1) * (nmax.Y - nmin.Y + 1) *
138 (nmax.Z - nmin.Z + 1);
139 u32 csize = clust_size;
140 u32 cvolume = csize * csize * csize;
141 u32 nclusters = volume / clust_scarcity;
143 for (u32 i = 0; i != nclusters; i++) {
144 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
145 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
146 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
148 if ((flags & OREFLAG_USE_NOISE) &&
149 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
152 if (biomemap && !biomes.empty()) {
153 u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
154 auto it = biomes.find(biomemap[index]);
155 if (it == biomes.end())
159 for (u32 z1 = 0; z1 != csize; z1++)
160 for (u32 y1 = 0; y1 != csize; y1++)
161 for (u32 x1 = 0; x1 != csize; x1++) {
162 if (pr.range(1, cvolume) > clust_num_ores)
165 u32 i = vm->m_area.index(
166 x0 + x1, y0 + y1, z0 + z1);
167 if (!CONTAINS(c_wherein,
168 vm->m_data[i].getContent()))
171 vm->m_data[i] = n_ore;
176 ///////////////////////////////////////////////////////////////////////////////
178 ObjDef *OreSheet::clone() const
180 auto def = new OreSheet();
183 def->column_height_max = column_height_max;
184 def->column_height_min = column_height_min;
185 def->column_midpoint_factor = column_midpoint_factor;
190 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
193 PcgRandom pr(blockseed + 4234);
194 MapNode n_ore(c_ore, 0, ore_param2);
196 u16 max_height = column_height_max;
197 int y_start_min = nmin.Y + max_height;
198 int y_start_max = nmax.Y - max_height;
200 int y_start = y_start_min < y_start_max ? pr.range(y_start_min, y_start_max)
201 : (y_start_min + y_start_max) / 2;
204 int sx = nmax.X - nmin.X + 1;
205 int sz = nmax.Z - nmin.Z + 1;
206 noise = new Noise(&np, 0, sx, sz);
208 noise->seed = mapseed + y_start;
209 noise->perlinMap2D(nmin.X, nmin.Z);
212 for (int z = nmin.Z; z <= nmax.Z; z++)
213 for (int x = nmin.X; x <= nmax.X; x++, index++) {
214 float noiseval = noise->result[index];
215 if (noiseval < nthresh)
218 if (biomemap && !biomes.empty()) {
219 auto it = biomes.find(biomemap[index]);
220 if (it == biomes.end())
224 u16 height = pr.range(column_height_min, column_height_max);
225 int ymidpoint = y_start + noiseval;
226 int y0 = MYMAX(nmin.Y,
227 ymidpoint - height * (1 - column_midpoint_factor));
228 int y1 = MYMIN(nmax.Y, y0 + height - 1);
230 for (int y = y0; y <= y1; y++) {
231 u32 i = vm->m_area.index(x, y, z);
232 if (!vm->m_area.contains(i))
234 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
237 vm->m_data[i] = n_ore;
242 ///////////////////////////////////////////////////////////////////////////////
246 delete noise_puff_top;
247 delete noise_puff_bottom;
250 ObjDef *OrePuff::clone() const
252 auto def = new OrePuff();
255 def->np_puff_top = np_puff_top;
256 def->np_puff_bottom = np_puff_bottom;
257 def->noise_puff_top = nullptr; // cannot be shared, on-demand
258 def->noise_puff_bottom = nullptr;
263 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
266 PcgRandom pr(blockseed + 4234);
267 MapNode n_ore(c_ore, 0, ore_param2);
269 int y_start = pr.range(nmin.Y, nmax.Y);
272 int sx = nmax.X - nmin.X + 1;
273 int sz = nmax.Z - nmin.Z + 1;
274 noise = new Noise(&np, 0, sx, sz);
275 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
276 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
279 noise->seed = mapseed + y_start;
280 noise->perlinMap2D(nmin.X, nmin.Z);
281 bool noise_generated = false;
284 for (int z = nmin.Z; z <= nmax.Z; z++)
285 for (int x = nmin.X; x <= nmax.X; x++, index++) {
286 float noiseval = noise->result[index];
287 if (noiseval < nthresh)
290 if (biomemap && !biomes.empty()) {
291 auto it = biomes.find(biomemap[index]);
292 if (it == biomes.end())
296 if (!noise_generated) {
297 noise_generated = true;
298 noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
299 noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
302 float ntop = noise_puff_top->result[index];
303 float nbottom = noise_puff_bottom->result[index];
305 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
306 float ndiff = noiseval - nthresh;
314 int y0 = ymid - nbottom;
315 int y1 = ymid + ntop;
317 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
320 for (int y = y0; y <= y1; y++) {
321 u32 i = vm->m_area.index(x, y, z);
322 if (!vm->m_area.contains(i))
324 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
327 vm->m_data[i] = n_ore;
332 ///////////////////////////////////////////////////////////////////////////////
334 ObjDef *OreBlob::clone() const
336 auto def = new OreBlob();
341 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
344 PcgRandom pr(blockseed + 2404);
345 MapNode n_ore(c_ore, 0, ore_param2);
347 u32 sizex = (nmax.X - nmin.X + 1);
348 u32 volume = (nmax.X - nmin.X + 1) * (nmax.Y - nmin.Y + 1) *
349 (nmax.Z - nmin.Z + 1);
350 u32 csize = clust_size;
351 u32 nblobs = volume / clust_scarcity;
354 noise = new Noise(&np, mapseed, csize, csize, csize);
356 for (u32 i = 0; i != nblobs; i++) {
357 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
358 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
359 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
361 if (biomemap && !biomes.empty()) {
362 u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
363 auto it = biomes.find(biomemap[bmapidx]);
364 if (it == biomes.end())
368 bool noise_generated = false;
369 noise->seed = blockseed + i;
372 for (u32 z1 = 0; z1 != csize; z1++)
373 for (u32 y1 = 0; y1 != csize; y1++)
374 for (u32 x1 = 0; x1 != csize; x1++, index++) {
375 u32 i = vm->m_area.index(
376 x0 + x1, y0 + y1, z0 + z1);
377 if (!CONTAINS(c_wherein,
378 vm->m_data[i].getContent()))
381 // Lazily generate noise only if there's a chance
382 // of ore being placed This simple optimization
383 // makes calls 6x faster on average
384 if (!noise_generated) {
385 noise_generated = true;
386 noise->perlinMap3D(x0, y0, z0);
389 float noiseval = noise->result[index];
391 float xdist = (s32)x1 - (s32)csize / 2;
392 float ydist = (s32)y1 - (s32)csize / 2;
393 float zdist = (s32)z1 - (s32)csize / 2;
395 noiseval -= std::sqrt(xdist * xdist +
400 if (noiseval < nthresh)
403 vm->m_data[i] = n_ore;
408 ///////////////////////////////////////////////////////////////////////////////
415 ObjDef *OreVein::clone() const
417 auto def = new OreVein();
420 def->random_factor = random_factor;
421 def->noise2 = nullptr; // cannot be shared, on-demand
422 def->sizey_prev = sizey_prev;
427 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin, v3s16 nmax,
430 PcgRandom pr(blockseed + 520);
431 MapNode n_ore(c_ore, 0, ore_param2);
433 int sizex = nmax.X - nmin.X + 1;
434 int sizey = nmax.Y - nmin.Y + 1;
435 // Because this ore uses 3D noise the perlinmap Y size can be different in
436 // different mapchunks due to ore Y limits. So recreate the noise objects
437 // if Y size has changed.
438 // Because these noise objects are created multiple times for this ore type
439 // it is necessary to 'delete' them here.
440 if (!noise || sizey != sizey_prev) {
443 int sizez = nmax.Z - nmin.Z + 1;
444 noise = new Noise(&np, mapseed, sizex, sizey, sizez);
445 noise2 = new Noise(&np, mapseed + 436, sizex, sizey, sizez);
449 bool noise_generated = false;
451 for (int z = nmin.Z; z <= nmax.Z; z++)
452 for (int y = nmin.Y; y <= nmax.Y; y++)
453 for (int x = nmin.X; x <= nmax.X; x++, index++) {
454 u32 i = vm->m_area.index(x, y, z);
455 if (!vm->m_area.contains(i))
457 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
460 if (biomemap && !biomes.empty()) {
461 u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
462 auto it = biomes.find(biomemap[bmapidx]);
463 if (it == biomes.end())
467 // Same lazy generation optimization as in OreBlob
468 if (!noise_generated) {
469 noise_generated = true;
470 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
471 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
474 // randval ranges from -1..1
475 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) -
477 float noiseval = contour(noise->result[index]);
478 float noiseval2 = contour(noise2->result[index]);
479 if (noiseval * noiseval2 + randval * random_factor <
483 vm->m_data[i] = n_ore;
487 ///////////////////////////////////////////////////////////////////////////////
489 OreStratum::~OreStratum()
491 delete noise_stratum_thickness;
494 ObjDef *OreStratum::clone() const
496 auto def = new OreStratum();
499 def->np_stratum_thickness = np_stratum_thickness;
500 def->noise_stratum_thickness = nullptr; // cannot be shared, on-demand
501 def->stratum_thickness = stratum_thickness;
506 void OreStratum::generate(MMVManip *vm, int mapseed, u32 blockseed, v3s16 nmin,
507 v3s16 nmax, biome_t *biomemap)
509 PcgRandom pr(blockseed + 4234);
510 MapNode n_ore(c_ore, 0, ore_param2);
512 if (flags & OREFLAG_USE_NOISE) {
514 int sx = nmax.X - nmin.X + 1;
515 int sz = nmax.Z - nmin.Z + 1;
516 noise = new Noise(&np, 0, sx, sz);
518 noise->perlinMap2D(nmin.X, nmin.Z);
521 if (flags & OREFLAG_USE_NOISE2) {
522 if (!noise_stratum_thickness) {
523 int sx = nmax.X - nmin.X + 1;
524 int sz = nmax.Z - nmin.Z + 1;
525 noise_stratum_thickness =
526 new Noise(&np_stratum_thickness, 0, sx, sz);
528 noise_stratum_thickness->perlinMap2D(nmin.X, nmin.Z);
533 for (int z = nmin.Z; z <= nmax.Z; z++)
534 for (int x = nmin.X; x <= nmax.X; x++, index++) {
535 if (biomemap && !biomes.empty()) {
536 auto it = biomes.find(biomemap[index]);
537 if (it == biomes.end())
544 if (flags & OREFLAG_USE_NOISE) {
546 ((flags & OREFLAG_USE_NOISE2) ? noise_stratum_thickness
548 : (float)stratum_thickness) /
550 float nmid = noise->result[index];
551 y0 = MYMAX(nmin.Y, std::ceil(nmid - nhalfthick));
552 y1 = MYMIN(nmax.Y, nmid + nhalfthick);
553 } else { // Simple horizontal stratum
558 for (int y = y0; y <= y1; y++) {
559 if (pr.range(1, clust_scarcity) != 1)
562 u32 i = vm->m_area.index(x, y, z);
563 if (!vm->m_area.contains(i))
565 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
568 vm->m_data[i] = n_ore;