]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mg_ore.cpp
Ore: Add Blob ore type
[dragonfireclient.git] / src / mg_ore.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20 #include "mg_ore.h"
21 #include "mapgen.h"
22 #include "noise.h"
23 #include "util/numeric.h"
24 #include "map.h"
25 #include "log.h"
26
27 const char *OreManager::ELEMENT_TITLE = "ore";
28
29 FlagDesc flagdesc_ore[] = {
30         {"absheight",            OREFLAG_ABSHEIGHT},
31         {"scatter_noisedensity", OREFLAG_DENSITY},
32         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
33         {NULL,                   0}
34 };
35
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39
40 OreManager::OreManager(IGameDef *gamedef) :
41         GenElementManager(gamedef)
42 {
43 }
44
45
46 size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
47 {
48         size_t nplaced = 0;
49
50         for (size_t i = 0; i != m_elements.size(); i++) {
51                 Ore *ore = (Ore *)m_elements[i];
52                 if (!ore)
53                         continue;
54
55                 nplaced += ore->placeOre(mg, seed, nmin, nmax);
56                 seed++;
57         }
58
59         return nplaced;
60 }
61
62
63 void OreManager::clear()
64 {
65         for (size_t i = 0; i < m_elements.size(); i++) {
66                 Ore *ore = (Ore *)m_elements[i];
67                 delete ore;
68         }
69         m_elements.clear();
70 }
71
72
73 ///////////////////////////////////////////////////////////////////////////////
74
75
76 Ore::Ore()
77 {
78         flags = 0;
79         noise = NULL;
80 }
81
82
83 Ore::~Ore()
84 {
85         delete noise;
86 }
87
88
89 void Ore::resolveNodeNames(NodeResolveInfo *nri)
90 {
91         m_ndef->getIdFromResolveInfo(nri, "", CONTENT_AIR, c_ore);
92         m_ndef->getIdsFromResolveInfo(nri, c_wherein);
93 }
94
95
96 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
97 {
98         int in_range = 0;
99
100         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
101         if (flags & OREFLAG_ABSHEIGHT)
102                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
103         if (!in_range)
104                 return 0;
105
106         int ymin, ymax;
107         if (in_range & ORE_RANGE_MIRROR) {
108                 ymin = MYMAX(nmin.Y, -height_max);
109                 ymax = MYMIN(nmax.Y, -height_min);
110         } else {
111                 ymin = MYMAX(nmin.Y, height_min);
112                 ymax = MYMIN(nmax.Y, height_max);
113         }
114         if (clust_size >= ymax - ymin + 1)
115                 return 0;
116
117         nmin.Y = ymin;
118         nmax.Y = ymax;
119         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
120
121         return 1;
122 }
123
124
125 ///////////////////////////////////////////////////////////////////////////////
126
127
128 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
129         u32 blockseed, v3s16 nmin, v3s16 nmax)
130 {
131         PseudoRandom pr(blockseed);
132         MapNode n_ore(c_ore, 0, ore_param2);
133
134         int volume = (nmax.X - nmin.X + 1) *
135                                  (nmax.Y - nmin.Y + 1) *
136                                  (nmax.Z - nmin.Z + 1);
137         int csize     = clust_size;
138         int orechance = (csize * csize * csize) / clust_num_ores;
139         int nclusters = volume / clust_scarcity;
140
141         for (int i = 0; i != nclusters; i++) {
142                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
143                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
144                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
145
146                 if ((flags & OREFLAG_USE_NOISE) &&
147                         (NoisePerlin3D(&np, x0, y0, z0, seed) < nthresh))
148                         continue;
149
150                 for (int z1 = 0; z1 != csize; z1++)
151                 for (int y1 = 0; y1 != csize; y1++)
152                 for (int x1 = 0; x1 != csize; x1++) {
153                         if (pr.range(1, orechance) != 1)
154                                 continue;
155
156                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
157                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
158                                 continue;
159
160                         vm->m_data[i] = n_ore;
161                 }
162         }
163 }
164
165
166 ///////////////////////////////////////////////////////////////////////////////
167
168
169 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
170         u32 blockseed, v3s16 nmin, v3s16 nmax)
171 {
172         PseudoRandom pr(blockseed + 4234);
173         MapNode n_ore(c_ore, 0, ore_param2);
174
175         int max_height = clust_size;
176         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
177
178         if (!noise) {
179                 int sx = nmax.X - nmin.X + 1;
180                 int sz = nmax.Z - nmin.Z + 1;
181                 noise = new Noise(&np, 0, sx, sz);
182         }
183         noise->seed = seed + y_start;
184         noise->perlinMap2D(nmin.X, nmin.Z);
185
186         size_t index = 0;
187         for (int z = nmin.Z; z <= nmax.Z; z++)
188         for (int x = nmin.X; x <= nmax.X; x++) {
189                 float noiseval = noise->result[index++];
190                 if (noiseval < nthresh)
191                         continue;
192
193                 int height = max_height * (1. / pr.range(1, 3));
194                 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
195                 int y1 = y0 + height;
196                 for (int y = y0; y != y1; y++) {
197                         u32 i = vm->m_area.index(x, y, z);
198                         if (!vm->m_area.contains(i))
199                                 continue;
200                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
201                                 continue;
202
203                         vm->m_data[i] = n_ore;
204                 }
205         }
206 }
207
208
209 ///////////////////////////////////////////////////////////////////////////////
210
211 void OreBlob::generate(ManualMapVoxelManipulator *vm, int seed, u32 blockseed,
212         v3s16 nmin, v3s16 nmax)
213 {
214         PseudoRandom pr(blockseed + 2404);
215         MapNode n_ore(c_ore, 0, ore_param2);
216
217         int volume = (nmax.X - nmin.X + 1) *
218                                  (nmax.Y - nmin.Y + 1) *
219                                  (nmax.Z - nmin.Z + 1);
220         int csize     = clust_size;
221         int nblobs = volume / clust_scarcity;
222
223         if (!noise)
224                 noise = new Noise(&np, seed, csize, csize, csize);
225
226         for (int i = 0; i != nblobs; i++) {
227                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
228                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
229                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
230
231                 bool noise_generated = false;
232                 noise->seed = blockseed + i;
233
234                 size_t index = 0;
235                 for (int z1 = 0; z1 != csize; z1++)
236                 for (int y1 = 0; y1 != csize; y1++)
237                 for (int x1 = 0; x1 != csize; x1++, index++) {
238                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
239                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
240                                 continue;
241
242                         // Lazily generate noise only if there's a chance of ore being placed
243                         // This simple optimization makes calls 6x faster on average
244                         if (!noise_generated) {
245                                 noise_generated = true;
246                                 noise->perlinMap3D(x0, y0, z0);
247                         }
248
249                         float noiseval = noise->result[index];
250
251                         float xdist = x1 - csize / 2;
252                         float ydist = y1 - csize / 2;
253                         float zdist = z1 - csize / 2;
254
255                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
256
257                         if (noiseval < nthresh)
258                                 continue;
259
260                         vm->m_data[i] = n_ore;
261                 }
262         }
263 }