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.
29 FlagDesc flagdesc_ore[] = {
30 {"absheight", OREFLAG_ABSHEIGHT}, // Non-functional
31 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
32 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
37 ///////////////////////////////////////////////////////////////////////////////
40 OreManager::OreManager(IGameDef *gamedef) :
41 ObjDefManager(gamedef, OBJDEF_ORE)
46 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed,
47 v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
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, ore_zero_level);
64 void OreManager::clear()
66 for (size_t i = 0; i < m_objects.size(); i++) {
67 Ore *ore = (Ore *)m_objects[i];
74 ///////////////////////////////////////////////////////////////////////////////
82 void Ore::resolveNodeNames()
84 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
85 getIdsFromNrBacklog(&c_wherein);
89 size_t Ore::placeOre(Mapgen *mg, u32 blockseed,
90 v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
92 // Ore y_min / y_max is displaced by ore_zero_level or remains unchanged.
93 // Any ore with a limit at +-MAX_MAP_GENERATION_LIMIT is considered to have
94 // that limit at +-infinity, so we do not alter that limit.
95 s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ?
96 -MAX_MAP_GENERATION_LIMIT : y_min + ore_zero_level;
98 s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ?
99 MAX_MAP_GENERATION_LIMIT : y_max + ore_zero_level;
101 if (nmin.Y > y_max_disp || nmax.Y < y_min_disp)
104 int actual_ymin = MYMAX(nmin.Y, y_min_disp);
105 int actual_ymax = MYMIN(nmax.Y, y_max_disp);
106 if (clust_size >= actual_ymax - actual_ymin + 1)
109 nmin.Y = actual_ymin;
110 nmax.Y = actual_ymax;
111 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
117 ///////////////////////////////////////////////////////////////////////////////
120 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
121 v3s16 nmin, v3s16 nmax, u8 *biomemap)
123 PcgRandom pr(blockseed);
124 MapNode n_ore(c_ore, 0, ore_param2);
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;
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);
139 if ((flags & OREFLAG_USE_NOISE) &&
140 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
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())
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)
156 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
157 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
160 vm->m_data[i] = n_ore;
166 ///////////////////////////////////////////////////////////////////////////////
169 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
170 v3s16 nmin, v3s16 nmax, u8 *biomemap)
172 PcgRandom pr(blockseed + 4234);
173 MapNode n_ore(c_ore, 0, ore_param2);
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;
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;
184 int sx = nmax.X - nmin.X + 1;
185 int sz = nmax.Z - nmin.Z + 1;
186 noise = new Noise(&np, 0, sx, sz);
188 noise->seed = mapseed + y_start;
189 noise->perlinMap2D(nmin.X, nmin.Z);
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)
198 if (biomemap && !biomes.empty()) {
199 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
200 if (it == biomes.end())
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);
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))
213 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
216 vm->m_data[i] = n_ore;
222 ///////////////////////////////////////////////////////////////////////////////
232 delete noise_puff_top;
233 delete noise_puff_bottom;
237 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
238 v3s16 nmin, v3s16 nmax, u8 *biomemap)
240 PcgRandom pr(blockseed + 4234);
241 MapNode n_ore(c_ore, 0, ore_param2);
243 int y_start = pr.range(nmin.Y, nmax.Y);
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);
253 noise->seed = mapseed + y_start;
254 noise->perlinMap2D(nmin.X, nmin.Z);
255 bool noise_generated = false;
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)
264 if (biomemap && !biomes.empty()) {
265 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
266 if (it == biomes.end())
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);
276 float ntop = noise_puff_top->result[index];
277 float nbottom = noise_puff_bottom->result[index];
279 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
280 float ndiff = noiseval - nthresh;
288 int y0 = ymid - nbottom;
289 int y1 = ymid + ntop;
291 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
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))
298 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
301 vm->m_data[i] = n_ore;
307 ///////////////////////////////////////////////////////////////////////////////
310 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
311 v3s16 nmin, v3s16 nmax, u8 *biomemap)
313 PcgRandom pr(blockseed + 2404);
314 MapNode n_ore(c_ore, 0, ore_param2);
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;
324 noise = new Noise(&np, mapseed, csize, csize, csize);
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);
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())
338 bool noise_generated = false;
339 noise->seed = blockseed + i;
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()))
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);
356 float noiseval = noise->result[index];
358 float xdist = (s32)x1 - (s32)csize / 2;
359 float ydist = (s32)y1 - (s32)csize / 2;
360 float zdist = (s32)z1 - (s32)csize / 2;
362 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
364 if (noiseval < nthresh)
367 vm->m_data[i] = n_ore;
373 ///////////////////////////////////////////////////////////////////////////////
387 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
388 v3s16 nmin, v3s16 nmax, u8 *biomemap)
390 PcgRandom pr(blockseed + 520);
391 MapNode n_ore(c_ore, 0, ore_param2);
393 u32 sizex = (nmax.X - nmin.X + 1);
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);
402 bool noise_generated = false;
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))
411 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
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())
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);
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)
435 vm->m_data[i] = n_ore;