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);
152 int max_height = clust_size;
154 int y_start = pr.range(ymin, ymax - max_height);
157 int sx = nmax.X - nmin.X + 1;
158 int sz = nmax.Z - nmin.Z + 1;
159 noise = new Noise(np, 0, sx, sz);
161 noise->seed = mg->seed + y_start;
162 noise->perlinMap2D(x0, z0);
165 for (int z = z0; z != z1; z++)
166 for (int x = x0; x != x1; x++) {
168 if (noise->result[index++] < nthresh)
171 int height = max_height * (1. / pr.range(1, 3));
172 int y0 = y_start + pr.range(1, 3) - 1;
173 int y1 = y0 + height;
174 for (int y = y0; y != y1; y++) {
175 u32 i = vm->m_area.index(x, y, z);
176 if (!vm->m_area.contains(i))
179 if (vm->m_data[i].getContent() == wherein)
180 vm->m_data[i] = n_ore;
186 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
187 bool isliquid, wasliquid;
188 v3s16 em = vm->m_area.getExtent();
190 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
191 for (s16 x = nmin.X; x <= nmax.X; x++) {
194 u32 i = vm->m_area.index(x, nmax.Y, z);
195 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
196 isliquid = ndef->get(vm->m_data[i]).isLiquid();
198 // there was a change between liquid and nonliquid, add to queue
199 if (isliquid != wasliquid)
200 trans_liquid->push_back(v3s16(x, y, z));
202 wasliquid = isliquid;
203 vm->m_area.add_y(em, i, -1);
210 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
211 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
212 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
213 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
215 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
216 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
217 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
218 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
219 vm->m_data[i].param1 = light;
225 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
226 if (light <= 1 || !a.contains(p))
229 u32 vi = vm->m_area.index(p);
230 MapNode &nn = vm->m_data[vi];
233 // should probably compare masked, but doesn't seem to make a difference
234 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
239 lightSpread(a, p + v3s16(0, 0, 1), light);
240 lightSpread(a, p + v3s16(0, 1, 0), light);
241 lightSpread(a, p + v3s16(1, 0, 0), light);
242 lightSpread(a, p - v3s16(0, 0, 1), light);
243 lightSpread(a, p - v3s16(0, 1, 0), light);
244 lightSpread(a, p - v3s16(1, 0, 0), light);
248 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
249 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
250 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
251 bool block_is_underground = (water_level >= nmax.Y);
253 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
254 //TimeTaker t("updateLighting");
256 // first, send vertical rays of sunshine downward
257 v3s16 em = vm->m_area.getExtent();
258 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
259 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
260 // see if we can get a light value from the overtop
261 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
262 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
263 if (block_is_underground)
265 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
268 vm->m_area.add_y(em, i, -1);
270 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
271 MapNode &n = vm->m_data[i];
272 if (!ndef->get(n).sunlight_propagates)
274 n.param1 = LIGHT_SUN;
275 vm->m_area.add_y(em, i, -1);
280 // now spread the sunlight and light up any sources
281 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
282 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
283 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
284 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
285 MapNode &n = vm->m_data[i];
286 if (n.getContent() == CONTENT_IGNORE ||
287 !ndef->get(n).light_propagates)
290 u8 light_produced = ndef->get(n).light_source & 0x0F;
292 n.param1 = light_produced;
294 u8 light = n.param1 & 0x0F;
296 lightSpread(a, v3s16(x, y, z + 1), light);
297 lightSpread(a, v3s16(x, y + 1, z ), light);
298 lightSpread(a, v3s16(x + 1, y, z ), light);
299 lightSpread(a, v3s16(x, y, z - 1), light);
300 lightSpread(a, v3s16(x, y - 1, z ), light);
301 lightSpread(a, v3s16(x - 1, y, z ), light);
307 //printf("updateLighting: %dms\n", t.stop());
311 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
312 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
314 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
315 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
316 bool block_is_underground = (water_level > nmax.Y);
317 bool sunlight = !block_is_underground;
319 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
321 for (int i = 0; i < 2; i++) {
322 enum LightBank bank = banks[i];
323 std::set<v3s16> light_sources;
324 std::map<v3s16, u8> unlight_from;
326 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
327 light_sources, unlight_from);
328 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
330 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
331 vm->spreadLight(bank, light_sources, ndef);
336 //////////////////////// Mapgen V6 parameter read/write
338 bool MapgenV6Params::readParams(Settings *settings) {
339 freq_desert = settings->getFloat("mgv6_freq_desert");
340 freq_beach = settings->getFloat("mgv6_freq_beach");
342 np_terrain_base = settings->getNoiseParams("mgv6_np_terrain_base");
343 np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
344 np_steepness = settings->getNoiseParams("mgv6_np_steepness");
345 np_height_select = settings->getNoiseParams("mgv6_np_height_select");
346 np_mud = settings->getNoiseParams("mgv6_np_mud");
347 np_beach = settings->getNoiseParams("mgv6_np_beach");
348 np_biome = settings->getNoiseParams("mgv6_np_biome");
349 np_cave = settings->getNoiseParams("mgv6_np_cave");
350 np_humidity = settings->getNoiseParams("mgv6_np_humidity");
351 np_trees = settings->getNoiseParams("mgv6_np_trees");
352 np_apple_trees = settings->getNoiseParams("mgv6_np_apple_trees");
355 np_terrain_base && np_terrain_higher && np_steepness &&
356 np_height_select && np_trees && np_mud &&
357 np_beach && np_biome && np_cave &&
358 np_humidity && np_apple_trees;
363 void MapgenV6Params::writeParams(Settings *settings) {
364 settings->setFloat("mgv6_freq_desert", freq_desert);
365 settings->setFloat("mgv6_freq_beach", freq_beach);
367 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
368 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
369 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
370 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
371 settings->setNoiseParams("mgv6_np_mud", np_mud);
372 settings->setNoiseParams("mgv6_np_beach", np_beach);
373 settings->setNoiseParams("mgv6_np_biome", np_biome);
374 settings->setNoiseParams("mgv6_np_cave", np_cave);
375 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
376 settings->setNoiseParams("mgv6_np_trees", np_trees);
377 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
381 /////////////////////////////////// legacy static functions for farmesh
384 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
385 //just need to return something
391 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
392 double sandnoise = noise2d_perlin(
393 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
394 seed+59420, 3, 0.50);
396 return (sandnoise > 0.15);
400 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
401 double noise = noise2d_perlin(
402 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
404 double zeroval = -0.39;
408 return 0.04 * (noise-zeroval) / (1.0-zeroval);