3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
23 #include "util/numeric.h"
27 const char *OreManager::ELEMENT_TITLE = "ore";
29 FlagDesc flagdesc_ore[] = {
30 {"absheight", OREFLAG_ABSHEIGHT},
35 ///////////////////////////////////////////////////////////////////////////////
38 OreManager::OreManager(IGameDef *gamedef) :
39 GenElementManager(gamedef)
44 size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
48 for (size_t i = 0; i != m_elements.size(); i++) {
49 Ore *ore = (Ore *)m_elements[i];
53 nplaced += ore->placeOre(mg, seed, nmin, nmax);
61 void OreManager::clear()
63 for (size_t i = 0; i < m_elements.size(); i++) {
64 Ore *ore = (Ore *)m_elements[i];
71 ///////////////////////////////////////////////////////////////////////////////
87 void Ore::resolveNodeNames(NodeResolveInfo *nri)
89 m_ndef->getIdFromResolveInfo(nri, "", CONTENT_AIR, c_ore);
90 m_ndef->getIdsFromResolveInfo(nri, c_wherein);
94 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
98 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
99 if (flags & OREFLAG_ABSHEIGHT)
100 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
105 if (in_range & ORE_RANGE_MIRROR) {
106 ymin = MYMAX(nmin.Y, -height_max);
107 ymax = MYMIN(nmax.Y, -height_min);
109 ymin = MYMAX(nmin.Y, height_min);
110 ymax = MYMIN(nmax.Y, height_max);
112 if (clust_size >= ymax - ymin + 1)
117 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
123 ///////////////////////////////////////////////////////////////////////////////
126 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
127 u32 blockseed, v3s16 nmin, v3s16 nmax)
129 PseudoRandom pr(blockseed);
130 MapNode n_ore(c_ore, 0, ore_param2);
132 int volume = (nmax.X - nmin.X + 1) *
133 (nmax.Y - nmin.Y + 1) *
134 (nmax.Z - nmin.Z + 1);
135 int csize = clust_size;
136 int orechance = (csize * csize * csize) / clust_num_ores;
137 int nclusters = volume / clust_scarcity;
139 for (int i = 0; i != nclusters; i++) {
140 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
141 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
142 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
144 if ((flags & OREFLAG_USE_NOISE) &&
145 (NoisePerlin3D(&np, x0, y0, z0, seed) < nthresh))
148 for (int z1 = 0; z1 != csize; z1++)
149 for (int y1 = 0; y1 != csize; y1++)
150 for (int x1 = 0; x1 != csize; x1++) {
151 if (pr.range(1, orechance) != 1)
154 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
155 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
158 vm->m_data[i] = n_ore;
164 ///////////////////////////////////////////////////////////////////////////////
167 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
168 u32 blockseed, v3s16 nmin, v3s16 nmax)
170 PseudoRandom pr(blockseed + 4234);
171 MapNode n_ore(c_ore, 0, ore_param2);
173 int max_height = clust_size;
174 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
177 int sx = nmax.X - nmin.X + 1;
178 int sz = nmax.Z - nmin.Z + 1;
179 noise = new Noise(&np, 0, sx, sz);
181 noise->seed = seed + y_start;
182 noise->perlinMap2D(nmin.X, nmin.Z);
185 for (int z = nmin.Z; z <= nmax.Z; z++)
186 for (int x = nmin.X; x <= nmax.X; x++) {
187 float noiseval = noise->result[index++];
188 if (noiseval < nthresh)
191 int height = max_height * (1. / pr.range(1, 3));
192 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
193 int y1 = y0 + height;
194 for (int y = y0; y != y1; y++) {
195 u32 i = vm->m_area.index(x, y, z);
196 if (!vm->m_area.contains(i))
198 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
201 vm->m_data[i] = n_ore;
207 ///////////////////////////////////////////////////////////////////////////////
209 void OreBlob::generate(ManualMapVoxelManipulator *vm, int seed, u32 blockseed,
210 v3s16 nmin, v3s16 nmax)
212 PseudoRandom pr(blockseed + 2404);
213 MapNode n_ore(c_ore, 0, ore_param2);
215 int volume = (nmax.X - nmin.X + 1) *
216 (nmax.Y - nmin.Y + 1) *
217 (nmax.Z - nmin.Z + 1);
218 int csize = clust_size;
219 int nblobs = volume / clust_scarcity;
222 noise = new Noise(&np, seed, csize, csize, csize);
224 for (int i = 0; i != nblobs; i++) {
225 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
226 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
227 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
229 bool noise_generated = false;
230 noise->seed = blockseed + i;
233 for (int z1 = 0; z1 != csize; z1++)
234 for (int y1 = 0; y1 != csize; y1++)
235 for (int x1 = 0; x1 != csize; x1++, index++) {
236 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
237 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
240 // Lazily generate noise only if there's a chance of ore being placed
241 // This simple optimization makes calls 6x faster on average
242 if (!noise_generated) {
243 noise_generated = true;
244 noise->perlinMap3D(x0, y0, z0);
247 float noiseval = noise->result[index];
249 float xdist = x1 - csize / 2;
250 float ydist = y1 - csize / 2;
251 float zdist = z1 - csize / 2;
253 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
255 if (noiseval < nthresh)
258 vm->m_data[i] = n_ore;
264 ///////////////////////////////////////////////////////////////////////////////
272 void OreVein::generate(ManualMapVoxelManipulator *vm, int seed, u32 blockseed,
273 v3s16 nmin, v3s16 nmax)
275 PseudoRandom pr(blockseed + 520);
276 MapNode n_ore(c_ore, 0, ore_param2);
279 int sx = nmax.X - nmin.X + 1;
280 int sy = nmax.Y - nmin.Y + 1;
281 int sz = nmax.Z - nmin.Z + 1;
282 noise = new Noise(&np, seed, sx, sy, sz);
283 noise2 = new Noise(&np, seed + 436, sx, sy, sz);
285 bool noise_generated = false;
288 for (int z = nmin.Z; z <= nmax.Z; z++)
289 for (int y = nmin.Y; y <= nmax.Y; y++)
290 for (int x = nmin.X; x <= nmax.X; x++, index++) {
291 u32 i = vm->m_area.index(x, y, z);
292 if (!vm->m_area.contains(i))
294 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
297 // Same lazy generation optimization as in OreBlob
298 if (!noise_generated) {
299 noise_generated = true;
300 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
301 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
304 // randval ranges from -1..1
305 float randval = (float)pr.next() / (PSEUDORANDOM_MAX / 2) - 1.f;
306 float noiseval = contour(noise->result[index]);
307 float noiseval2 = contour(noise2->result[index]);
308 if (noiseval * noiseval2 + randval * random_factor < nthresh)
311 vm->m_data[i] = n_ore;