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,
48 v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
52 for (size_t i = 0; i != m_objects.size(); i++) {
53 Ore *ore = (Ore *)m_objects[i];
57 nplaced += ore->placeOre(mg, blockseed, nmin, nmax, ore_zero_level);
65 void OreManager::clear()
67 for (ObjDef *object : m_objects) {
68 Ore *ore = (Ore *) object;
75 ///////////////////////////////////////////////////////////////////////////////
83 void Ore::resolveNodeNames()
85 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
86 getIdsFromNrBacklog(&c_wherein);
90 size_t Ore::placeOre(Mapgen *mg, u32 blockseed,
91 v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
93 // Ore y_min / y_max is displaced by ore_zero_level or remains unchanged.
94 // Any ore with a limit at +-MAX_MAP_GENERATION_LIMIT is considered to have
95 // that limit at +-infinity, so we do not alter that limit.
96 s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ?
97 -MAX_MAP_GENERATION_LIMIT : y_min + ore_zero_level;
99 s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ?
100 MAX_MAP_GENERATION_LIMIT : y_max + ore_zero_level;
102 if (nmin.Y > y_max_disp || nmax.Y < y_min_disp)
105 int actual_ymin = MYMAX(nmin.Y, y_min_disp);
106 int actual_ymax = MYMIN(nmax.Y, y_max_disp);
107 if (clust_size >= actual_ymax - actual_ymin + 1)
110 nmin.Y = actual_ymin;
111 nmax.Y = actual_ymax;
112 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
118 ///////////////////////////////////////////////////////////////////////////////
121 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
122 v3s16 nmin, v3s16 nmax, u8 *biomemap)
124 PcgRandom pr(blockseed);
125 MapNode n_ore(c_ore, 0, ore_param2);
127 u32 sizex = (nmax.X - nmin.X + 1);
128 u32 volume = (nmax.X - nmin.X + 1) *
129 (nmax.Y - nmin.Y + 1) *
130 (nmax.Z - nmin.Z + 1);
131 u32 csize = clust_size;
132 u32 cvolume = csize * csize * csize;
133 u32 nclusters = volume / clust_scarcity;
135 for (u32 i = 0; i != nclusters; i++) {
136 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
137 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
138 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
140 if ((flags & OREFLAG_USE_NOISE) &&
141 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
144 if (biomemap && !biomes.empty()) {
145 u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
146 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
147 if (it == biomes.end())
151 for (u32 z1 = 0; z1 != csize; z1++)
152 for (u32 y1 = 0; y1 != csize; y1++)
153 for (u32 x1 = 0; x1 != csize; x1++) {
154 if (pr.range(1, cvolume) > clust_num_ores)
157 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
158 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
161 vm->m_data[i] = n_ore;
167 ///////////////////////////////////////////////////////////////////////////////
170 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
171 v3s16 nmin, v3s16 nmax, u8 *biomemap)
173 PcgRandom pr(blockseed + 4234);
174 MapNode n_ore(c_ore, 0, ore_param2);
176 u16 max_height = column_height_max;
177 int y_start_min = nmin.Y + max_height;
178 int y_start_max = nmax.Y - max_height;
180 int y_start = y_start_min < y_start_max ?
181 pr.range(y_start_min, y_start_max) :
182 (y_start_min + y_start_max) / 2;
185 int sx = nmax.X - nmin.X + 1;
186 int sz = nmax.Z - nmin.Z + 1;
187 noise = new Noise(&np, 0, sx, sz);
189 noise->seed = mapseed + y_start;
190 noise->perlinMap2D(nmin.X, nmin.Z);
193 for (int z = nmin.Z; z <= nmax.Z; z++)
194 for (int x = nmin.X; x <= nmax.X; x++, index++) {
195 float noiseval = noise->result[index];
196 if (noiseval < nthresh)
199 if (biomemap && !biomes.empty()) {
200 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
201 if (it == biomes.end())
205 u16 height = pr.range(column_height_min, column_height_max);
206 int ymidpoint = y_start + noiseval;
207 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
208 int y1 = MYMIN(nmax.Y, y0 + height - 1);
210 for (int y = y0; y <= y1; y++) {
211 u32 i = vm->m_area.index(x, y, z);
212 if (!vm->m_area.contains(i))
214 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
217 vm->m_data[i] = n_ore;
223 ///////////////////////////////////////////////////////////////////////////////
226 delete noise_puff_top;
227 delete noise_puff_bottom;
231 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
232 v3s16 nmin, v3s16 nmax, u8 *biomemap)
234 PcgRandom pr(blockseed + 4234);
235 MapNode n_ore(c_ore, 0, ore_param2);
237 int y_start = pr.range(nmin.Y, nmax.Y);
240 int sx = nmax.X - nmin.X + 1;
241 int sz = nmax.Z - nmin.Z + 1;
242 noise = new Noise(&np, 0, sx, sz);
243 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
244 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
247 noise->seed = mapseed + y_start;
248 noise->perlinMap2D(nmin.X, nmin.Z);
249 bool noise_generated = false;
252 for (int z = nmin.Z; z <= nmax.Z; z++)
253 for (int x = nmin.X; x <= nmax.X; x++, index++) {
254 float noiseval = noise->result[index];
255 if (noiseval < nthresh)
258 if (biomemap && !biomes.empty()) {
259 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
260 if (it == biomes.end())
264 if (!noise_generated) {
265 noise_generated = true;
266 noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
267 noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
270 float ntop = noise_puff_top->result[index];
271 float nbottom = noise_puff_bottom->result[index];
273 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
274 float ndiff = noiseval - nthresh;
282 int y0 = ymid - nbottom;
283 int y1 = ymid + ntop;
285 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
288 for (int y = y0; y <= y1; y++) {
289 u32 i = vm->m_area.index(x, y, z);
290 if (!vm->m_area.contains(i))
292 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
295 vm->m_data[i] = n_ore;
301 ///////////////////////////////////////////////////////////////////////////////
304 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
305 v3s16 nmin, v3s16 nmax, u8 *biomemap)
307 PcgRandom pr(blockseed + 2404);
308 MapNode n_ore(c_ore, 0, ore_param2);
310 u32 sizex = (nmax.X - nmin.X + 1);
311 u32 volume = (nmax.X - nmin.X + 1) *
312 (nmax.Y - nmin.Y + 1) *
313 (nmax.Z - nmin.Z + 1);
314 u32 csize = clust_size;
315 u32 nblobs = volume / clust_scarcity;
318 noise = new Noise(&np, mapseed, csize, csize, csize);
320 for (u32 i = 0; i != nblobs; i++) {
321 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
322 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
323 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
325 if (biomemap && !biomes.empty()) {
326 u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
327 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
328 if (it == biomes.end())
332 bool noise_generated = false;
333 noise->seed = blockseed + i;
336 for (u32 z1 = 0; z1 != csize; z1++)
337 for (u32 y1 = 0; y1 != csize; y1++)
338 for (u32 x1 = 0; x1 != csize; x1++, index++) {
339 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
340 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
343 // Lazily generate noise only if there's a chance of ore being placed
344 // This simple optimization makes calls 6x faster on average
345 if (!noise_generated) {
346 noise_generated = true;
347 noise->perlinMap3D(x0, y0, z0);
350 float noiseval = noise->result[index];
352 float xdist = (s32)x1 - (s32)csize / 2;
353 float ydist = (s32)y1 - (s32)csize / 2;
354 float zdist = (s32)z1 - (s32)csize / 2;
356 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
358 if (noiseval < nthresh)
361 vm->m_data[i] = n_ore;
367 ///////////////////////////////////////////////////////////////////////////////
374 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
375 v3s16 nmin, v3s16 nmax, u8 *biomemap)
377 PcgRandom pr(blockseed + 520);
378 MapNode n_ore(c_ore, 0, ore_param2);
380 u32 sizex = (nmax.X - nmin.X + 1);
383 int sx = nmax.X - nmin.X + 1;
384 int sy = nmax.Y - nmin.Y + 1;
385 int sz = nmax.Z - nmin.Z + 1;
386 noise = new Noise(&np, mapseed, sx, sy, sz);
387 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
389 bool noise_generated = false;
392 for (int z = nmin.Z; z <= nmax.Z; z++)
393 for (int y = nmin.Y; y <= nmax.Y; y++)
394 for (int x = nmin.X; x <= nmax.X; x++, index++) {
395 u32 i = vm->m_area.index(x, y, z);
396 if (!vm->m_area.contains(i))
398 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
401 if (biomemap && !biomes.empty()) {
402 u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
403 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
404 if (it == biomes.end())
408 // Same lazy generation optimization as in OreBlob
409 if (!noise_generated) {
410 noise_generated = true;
411 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
412 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
415 // randval ranges from -1..1
416 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
417 float noiseval = contour(noise->result[index]);
418 float noiseval2 = contour(noise2->result[index]);
419 if (noiseval * noiseval2 + randval * random_factor < nthresh)
422 vm->m_data[i] = n_ore;