3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
27 //#include "serverobject.h"
28 #include "content_sao.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
36 #include "mapgen_v6.h"
38 FlagDesc flagdesc_mapgen[] = {
41 {"dungeons", MG_DUNGEONS},
42 {"v6_jungles", MGV6_JUNGLES},
43 {"v6_biome_blend", MGV6_BIOME_BLEND},
49 ///////////////////////////////////////////////////////////////////////////////
52 Ore *createOre(OreType type) {
55 return new OreScatter;
58 //case ORE_CLAYLIKE: //TODO: implement this!
59 // return new OreClaylike;
66 void Ore::resolveNodeNames(INodeDefManager *ndef) {
67 if (ore == CONTENT_IGNORE) {
68 ore = ndef->getId(ore_name);
69 if (ore == CONTENT_IGNORE) {
70 errorstream << "Ore::resolveNodeNames: ore node '"
71 << ore_name << "' not defined";
73 wherein = CONTENT_AIR;
77 if (wherein == CONTENT_IGNORE) {
78 wherein = ndef->getId(wherein_name);
79 if (wherein == CONTENT_IGNORE) {
80 errorstream << "Ore::resolveNodeNames: wherein node '"
81 << wherein_name << "' not defined";
83 wherein = CONTENT_AIR;
89 void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
90 if (nmin.Y > height_max || nmax.Y < height_min)
93 resolveNodeNames(mg->ndef);
96 ManualMapVoxelManipulator *vm = mg->vm;
97 PseudoRandom pr(blockseed);
99 int ymin = MYMAX(nmin.Y, height_min);
100 int ymax = MYMIN(nmax.Y, height_max);
101 if (clust_size >= ymax - ymin + 1)
104 int volume = (nmax.X - nmin.X + 1) *
105 (nmax.Y - nmin.Y + 1) *
106 (nmax.Z - nmin.Z + 1);
107 int csize = clust_size;
108 int orechance = (csize * csize * csize) / clust_num_ores;
109 int nclusters = volume / clust_scarcity;
111 for (int i = 0; i != nclusters; i++) {
112 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
113 int y0 = pr.range(ymin, ymax - csize + 1);
114 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
116 if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
119 for (int z1 = 0; z1 != csize; z1++)
120 for (int y1 = 0; y1 != csize; y1++)
121 for (int x1 = 0; x1 != csize; x1++) {
122 if (pr.range(1, orechance) != 1)
125 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
126 if (vm->m_data[i].getContent() == wherein)
127 vm->m_data[i] = n_ore;
133 void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
134 if (nmin.Y > height_max || nmax.Y < height_min)
137 resolveNodeNames(mg->ndef);
140 ManualMapVoxelManipulator *vm = mg->vm;
141 PseudoRandom pr(blockseed + 4234);
143 int ymin = MYMAX(nmin.Y, height_min);
144 int ymax = MYMIN(nmax.Y, height_max);
145 if (clust_size >= ymax - ymin + 1)
154 int max_height = clust_size;
155 int y_start = pr.range(ymin, ymax - max_height);
158 int sx = nmax.X - nmin.X + 1;
159 int sz = nmax.Z - nmin.Z + 1;
160 noise = new Noise(np, 0, sx, sz);
162 noise->seed = mg->seed + y_start;
163 noise->perlinMap2D(x0, z0);
166 for (int z = z0; z != z1; z++)
167 for (int x = x0; x != x1; x++) {
168 float noiseval = noise->result[index++];
169 if (noiseval < nthresh)
172 int height = max_height * (1. / pr.range(1, 3));
173 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
174 int y1 = y0 + height;
175 for (int y = y0; y != y1; y++) {
176 u32 i = vm->m_area.index(x, y, z);
177 if (!vm->m_area.contains(i))
180 if (vm->m_data[i].getContent() == wherein)
181 vm->m_data[i] = n_ore;
187 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
188 bool isliquid, wasliquid;
189 v3s16 em = vm->m_area.getExtent();
191 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
192 for (s16 x = nmin.X; x <= nmax.X; x++) {
195 u32 i = vm->m_area.index(x, nmax.Y, z);
196 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
197 isliquid = ndef->get(vm->m_data[i]).isLiquid();
199 // there was a change between liquid and nonliquid, add to queue
200 if (isliquid != wasliquid)
201 trans_liquid->push_back(v3s16(x, y, z));
203 wasliquid = isliquid;
204 vm->m_area.add_y(em, i, -1);
211 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
212 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
213 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
214 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
216 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
217 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
218 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
219 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
220 vm->m_data[i].param1 = light;
226 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
227 if (light <= 1 || !a.contains(p))
230 u32 vi = vm->m_area.index(p);
231 MapNode &nn = vm->m_data[vi];
234 // should probably compare masked, but doesn't seem to make a difference
235 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
240 lightSpread(a, p + v3s16(0, 0, 1), light);
241 lightSpread(a, p + v3s16(0, 1, 0), light);
242 lightSpread(a, p + v3s16(1, 0, 0), light);
243 lightSpread(a, p - v3s16(0, 0, 1), light);
244 lightSpread(a, p - v3s16(0, 1, 0), light);
245 lightSpread(a, p - v3s16(1, 0, 0), light);
249 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
250 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
251 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
252 bool block_is_underground = (water_level >= nmax.Y);
254 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
255 //TimeTaker t("updateLighting");
257 // first, send vertical rays of sunshine downward
258 v3s16 em = vm->m_area.getExtent();
259 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
260 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
261 // see if we can get a light value from the overtop
262 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
263 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
264 if (block_is_underground)
266 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
269 vm->m_area.add_y(em, i, -1);
271 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
272 MapNode &n = vm->m_data[i];
273 if (!ndef->get(n).sunlight_propagates)
275 n.param1 = LIGHT_SUN;
276 vm->m_area.add_y(em, i, -1);
281 // now spread the sunlight and light up any sources
282 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
283 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
284 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
285 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
286 MapNode &n = vm->m_data[i];
287 if (n.getContent() == CONTENT_IGNORE ||
288 !ndef->get(n).light_propagates)
291 u8 light_produced = ndef->get(n).light_source & 0x0F;
293 n.param1 = light_produced;
295 u8 light = n.param1 & 0x0F;
297 lightSpread(a, v3s16(x, y, z + 1), light);
298 lightSpread(a, v3s16(x, y + 1, z ), light);
299 lightSpread(a, v3s16(x + 1, y, z ), light);
300 lightSpread(a, v3s16(x, y, z - 1), light);
301 lightSpread(a, v3s16(x, y - 1, z ), light);
302 lightSpread(a, v3s16(x - 1, y, z ), light);
308 //printf("updateLighting: %dms\n", t.stop());
312 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
313 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
315 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
316 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
317 bool block_is_underground = (water_level > nmax.Y);
318 bool sunlight = !block_is_underground;
320 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
322 for (int i = 0; i < 2; i++) {
323 enum LightBank bank = banks[i];
324 std::set<v3s16> light_sources;
325 std::map<v3s16, u8> unlight_from;
327 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
328 light_sources, unlight_from);
329 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
331 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
332 vm->spreadLight(bank, light_sources, ndef);
337 //////////////////////// Mapgen V6 parameter read/write
339 bool MapgenV6Params::readParams(Settings *settings) {
340 freq_desert = settings->getFloat("mgv6_freq_desert");
341 freq_beach = settings->getFloat("mgv6_freq_beach");
343 np_terrain_base = settings->getNoiseParams("mgv6_np_terrain_base");
344 np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
345 np_steepness = settings->getNoiseParams("mgv6_np_steepness");
346 np_height_select = settings->getNoiseParams("mgv6_np_height_select");
347 np_mud = settings->getNoiseParams("mgv6_np_mud");
348 np_beach = settings->getNoiseParams("mgv6_np_beach");
349 np_biome = settings->getNoiseParams("mgv6_np_biome");
350 np_cave = settings->getNoiseParams("mgv6_np_cave");
351 np_humidity = settings->getNoiseParams("mgv6_np_humidity");
352 np_trees = settings->getNoiseParams("mgv6_np_trees");
353 np_apple_trees = settings->getNoiseParams("mgv6_np_apple_trees");
356 np_terrain_base && np_terrain_higher && np_steepness &&
357 np_height_select && np_trees && np_mud &&
358 np_beach && np_biome && np_cave &&
359 np_humidity && np_apple_trees;
364 void MapgenV6Params::writeParams(Settings *settings) {
365 settings->setFloat("mgv6_freq_desert", freq_desert);
366 settings->setFloat("mgv6_freq_beach", freq_beach);
368 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
369 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
370 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
371 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
372 settings->setNoiseParams("mgv6_np_mud", np_mud);
373 settings->setNoiseParams("mgv6_np_beach", np_beach);
374 settings->setNoiseParams("mgv6_np_biome", np_biome);
375 settings->setNoiseParams("mgv6_np_cave", np_cave);
376 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
377 settings->setNoiseParams("mgv6_np_trees", np_trees);
378 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
382 /////////////////////////////////// legacy static functions for farmesh
385 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
386 //just need to return something
392 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
393 double sandnoise = noise2d_perlin(
394 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
395 seed+59420, 3, 0.50);
397 return (sandnoise > 0.15);
401 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
402 double noise = noise2d_perlin(
403 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
405 double zeroval = -0.39;
409 return 0.04 * (noise-zeroval) / (1.0-zeroval);