3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
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[] = {
31 {"absheight", OREFLAG_ABSHEIGHT}, // Non-functional
32 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
33 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
38 ///////////////////////////////////////////////////////////////////////////////
41 OreManager::OreManager(IGameDef *gamedef) :
42 ObjDefManager(gamedef, OBJDEF_ORE)
47 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
51 for (size_t i = 0; i != m_objects.size(); i++) {
52 Ore *ore = (Ore *)m_objects[i];
56 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
64 void OreManager::clear()
66 for (ObjDef *object : m_objects) {
67 Ore *ore = (Ore *) object;
74 ///////////////////////////////////////////////////////////////////////////////
83 void Ore::resolveNodeNames()
85 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
86 getIdsFromNrBacklog(&c_wherein);
90 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
92 if (nmin.Y > y_max || nmax.Y < y_min)
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)
100 nmin.Y = actual_ymin;
101 nmax.Y = actual_ymax;
102 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
108 ///////////////////////////////////////////////////////////////////////////////
111 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
112 v3s16 nmin, v3s16 nmax, u8 *biomemap)
114 PcgRandom pr(blockseed);
115 MapNode n_ore(c_ore, 0, ore_param2);
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;
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);
130 if ((flags & OREFLAG_USE_NOISE) &&
131 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
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())
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)
147 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
148 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
151 vm->m_data[i] = n_ore;
157 ///////////////////////////////////////////////////////////////////////////////
160 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
161 v3s16 nmin, v3s16 nmax, u8 *biomemap)
163 PcgRandom pr(blockseed + 4234);
164 MapNode n_ore(c_ore, 0, ore_param2);
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;
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;
175 int sx = nmax.X - nmin.X + 1;
176 int sz = nmax.Z - nmin.Z + 1;
177 noise = new Noise(&np, 0, sx, sz);
179 noise->seed = mapseed + y_start;
180 noise->perlinMap2D(nmin.X, nmin.Z);
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)
189 if (biomemap && !biomes.empty()) {
190 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
191 if (it == biomes.end())
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);
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))
204 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
207 vm->m_data[i] = n_ore;
213 ///////////////////////////////////////////////////////////////////////////////
218 delete noise_puff_top;
219 delete noise_puff_bottom;
223 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
224 v3s16 nmin, v3s16 nmax, u8 *biomemap)
226 PcgRandom pr(blockseed + 4234);
227 MapNode n_ore(c_ore, 0, ore_param2);
229 int y_start = pr.range(nmin.Y, nmax.Y);
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);
239 noise->seed = mapseed + y_start;
240 noise->perlinMap2D(nmin.X, nmin.Z);
241 bool noise_generated = false;
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)
250 if (biomemap && !biomes.empty()) {
251 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
252 if (it == biomes.end())
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);
262 float ntop = noise_puff_top->result[index];
263 float nbottom = noise_puff_bottom->result[index];
265 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
266 float ndiff = noiseval - nthresh;
274 int y0 = ymid - nbottom;
275 int y1 = ymid + ntop;
277 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
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))
284 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
287 vm->m_data[i] = n_ore;
293 ///////////////////////////////////////////////////////////////////////////////
296 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
297 v3s16 nmin, v3s16 nmax, u8 *biomemap)
299 PcgRandom pr(blockseed + 2404);
300 MapNode n_ore(c_ore, 0, ore_param2);
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;
310 noise = new Noise(&np, mapseed, csize, csize, csize);
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);
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())
324 bool noise_generated = false;
325 noise->seed = blockseed + i;
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()))
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);
342 float noiseval = noise->result[index];
344 float xdist = (s32)x1 - (s32)csize / 2;
345 float ydist = (s32)y1 - (s32)csize / 2;
346 float zdist = (s32)z1 - (s32)csize / 2;
348 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
350 if (noiseval < nthresh)
353 vm->m_data[i] = n_ore;
359 ///////////////////////////////////////////////////////////////////////////////
368 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
369 v3s16 nmin, v3s16 nmax, u8 *biomemap)
371 PcgRandom pr(blockseed + 520);
372 MapNode n_ore(c_ore, 0, ore_param2);
374 u32 sizex = (nmax.X - nmin.X + 1);
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);
383 bool noise_generated = false;
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))
392 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
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())
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);
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)
416 vm->m_data[i] = n_ore;
421 ///////////////////////////////////////////////////////////////////////////////
424 OreStratum::~OreStratum()
426 delete noise_stratum_thickness;
430 void OreStratum::generate(MMVManip *vm, int mapseed, u32 blockseed,
431 v3s16 nmin, v3s16 nmax, u8 *biomemap)
433 PcgRandom pr(blockseed + 4234);
434 MapNode n_ore(c_ore, 0, ore_param2);
437 int sx = nmax.X - nmin.X + 1;
438 int sz = nmax.Z - nmin.Z + 1;
439 noise = new Noise(&np, 0, sx, sz);
440 noise_stratum_thickness = new Noise(&np_stratum_thickness, 0, sx, sz);
442 noise->perlinMap2D(nmin.X, nmin.Z);
443 noise_stratum_thickness->perlinMap2D(nmin.X, nmin.Z);
447 for (int z = nmin.Z; z <= nmax.Z; z++)
448 for (int x = nmin.X; x <= nmax.X; x++, index++) {
449 if (biomemap && !biomes.empty()) {
450 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
451 if (it == biomes.end())
455 float nmid = noise->result[index];
456 float nhalfthick = noise_stratum_thickness->result[index] / 2.0f;
457 int y0 = MYMAX(nmin.Y, nmid - nhalfthick);
458 int y1 = MYMIN(nmax.Y, nmid + nhalfthick);
460 for (int y = y0; y <= y1; y++) {
461 if (pr.range(1, clust_scarcity) != 1)
464 u32 i = vm->m_area.index(x, y, z);
465 if (!vm->m_area.contains(i))
467 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
470 vm->m_data[i] = n_ore;