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"
37 #include "mapgen_v7.h"
39 FlagDesc flagdesc_mapgen[] = {
42 {"dungeons", MG_DUNGEONS},
43 {"v6_jungles", MGV6_JUNGLES},
44 {"v6_biome_blend", MGV6_BIOME_BLEND},
49 FlagDesc flagdesc_ore[] = {
50 {"absheight", OREFLAG_ABSHEIGHT},
51 {"scatter_noisedensity", OREFLAG_DENSITY},
52 {"claylike_nodeisnt", OREFLAG_NODEISNT},
57 ///////////////////////////////////////////////////////////////////////////////
60 Ore *createOre(OreType type) {
63 return new OreScatter;
66 //case ORE_CLAYLIKE: //TODO: implement this!
67 // return new OreClaylike;
80 void Ore::resolveNodeNames(INodeDefManager *ndef) {
81 if (ore == CONTENT_IGNORE) {
82 ore = ndef->getId(ore_name);
83 if (ore == CONTENT_IGNORE) {
84 errorstream << "Ore::resolveNodeNames: ore node '"
85 << ore_name << "' not defined";
87 wherein = CONTENT_AIR;
91 if (wherein == CONTENT_IGNORE) {
92 wherein = ndef->getId(wherein_name);
93 if (wherein == CONTENT_IGNORE) {
94 errorstream << "Ore::resolveNodeNames: wherein node '"
95 << wherein_name << "' not defined";
97 wherein = CONTENT_AIR;
103 void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
106 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
107 if (flags & OREFLAG_ABSHEIGHT)
108 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
113 if (in_range & ORE_RANGE_MIRROR) {
114 ymin = MYMAX(nmin.Y, -height_max);
115 ymax = MYMIN(nmax.Y, -height_min);
117 ymin = MYMAX(nmin.Y, height_min);
118 ymax = MYMIN(nmax.Y, height_max);
120 if (clust_size >= ymax - ymin + 1)
125 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
129 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
130 u32 blockseed, v3s16 nmin, v3s16 nmax) {
131 PseudoRandom pr(blockseed);
132 MapNode n_ore(ore, 0, ore_param2);
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;
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);
146 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
149 for (int z1 = 0; z1 != csize; z1++)
150 for (int y1 = 0; y1 != csize; y1++)
151 for (int x1 = 0; x1 != csize; x1++) {
152 if (pr.range(1, orechance) != 1)
155 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
156 if (vm->m_data[i].getContent() == wherein)
157 vm->m_data[i] = n_ore;
163 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
164 u32 blockseed, v3s16 nmin, v3s16 nmax) {
165 PseudoRandom pr(blockseed + 4234);
166 MapNode n_ore(ore, 0, ore_param2);
168 int max_height = clust_size;
169 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
172 int sx = nmax.X - nmin.X + 1;
173 int sz = nmax.Z - nmin.Z + 1;
174 noise = new Noise(np, 0, sx, sz);
176 noise->seed = seed + y_start;
177 noise->perlinMap2D(nmin.X, nmin.Z);
180 for (int z = nmin.Z; z <= nmax.Z; z++)
181 for (int x = nmin.X; x <= nmax.X; x++) {
182 float noiseval = noise->result[index++];
183 if (noiseval < nthresh)
186 int height = max_height * (1. / pr.range(1, 3));
187 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
188 int y1 = y0 + height;
189 for (int y = y0; y != y1; y++) {
190 u32 i = vm->m_area.index(x, y, z);
191 if (!vm->m_area.contains(i))
194 if (vm->m_data[i].getContent() == wherein)
195 vm->m_data[i] = n_ore;
201 Decoration *createDecoration(DecorationType type) {
204 return new DecoSimple;
205 //case DECO_SCHEMATIC:
206 // return new DecoSchematic;
208 // return new DecoLSystem;
215 Decoration::~Decoration() {
220 void Decoration::resolveNodeNames(INodeDefManager *ndef) {
221 if (c_place_on == CONTENT_IGNORE)
222 c_place_on = ndef->getId(place_on_name);
226 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
227 PseudoRandom ps(blockseed + 53);
228 int carea_size = nmax.X - nmin.X + 1;
230 // Divide area into parts
231 if (carea_size % sidelen) {
232 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
233 "sidelen; setting sidelen to " << carea_size << std::endl;
234 sidelen = carea_size;
237 s16 divlen = carea_size / sidelen;
238 int area = sidelen * sidelen;
240 for (s16 z0 = 0; z0 < divlen; z0++)
241 for (s16 x0 = 0; x0 < divlen; x0++) {
242 v2s16 p2d_center( // Center position of part of division
243 nmin.X + sidelen / 2 + sidelen * x0,
244 nmin.Z + sidelen / 2 + sidelen * z0
246 v2s16 p2d_min( // Minimum edge of part of division
247 nmin.X + sidelen * x0,
248 nmin.Z + sidelen * z0
250 v2s16 p2d_max( // Maximum edge of part of division
251 nmin.X + sidelen + sidelen * x0 - 1,
252 nmin.Z + sidelen + sidelen * z0 - 1
255 // Amount of decorations
257 NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
259 u32 deco_count = area * MYMAX(nval, 0.f);
261 for (u32 i = 0; i < deco_count; i++) {
262 s16 x = ps.range(p2d_min.X, p2d_max.X);
263 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
265 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
267 s16 y = mg->heightmap ?
268 mg->heightmap[mapindex] :
269 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
271 if (y < nmin.Y || y > nmax.Y)
274 int height = getHeight();
275 int max_y = nmax.Y + MAP_BLOCKSIZE;
276 if (y + 1 + height > max_y) {
279 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
281 JMutexAutoLock cutofflock(cutoff_mutex);
282 cutoffs.push_back(CutoffData(x, y, z, height));
287 std::set<u8>::iterator iter;
290 iter = biomes.find(mg->biomemap[mapindex]);
291 if (iter == biomes.end())
296 generate(mg, &ps, max_y, 0, v3s16(x, y, z));
303 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
304 PseudoRandom pr(blockseed + 53);
305 std::vector<CutoffData> handled_cutoffs;
307 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
309 JMutexAutoLock cutofflock(cutoff_mutex);
310 for (std::list<CutoffData>::iterator i = cutoffs.begin();
311 i != cutoffs.end(); ++i) {
312 CutoffData cutoff = *i;
314 s16 height = cutoff.height;
315 if (p.X < nmin.X || p.X > nmax.X ||
316 p.Z < nmin.Z || p.Z > nmax.Z)
318 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
321 handled_cutoffs.push_back(cutoff);
325 // Generate the cutoffs
326 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
327 v3s16 p = handled_cutoffs[i].p;
328 s16 height = handled_cutoffs[i].height;
330 if (p.Y + height > nmax.Y) {
331 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
332 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
335 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
338 // Remove cutoffs that were handled from the cutoff list
340 JMutexAutoLock cutofflock(cutoff_mutex);
341 for (std::list<CutoffData>::iterator i = cutoffs.begin();
342 i != cutoffs.end(); ++i) {
344 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
345 CutoffData coff = *i;
346 if (coff.p == handled_cutoffs[j].p)
347 i = cutoffs.erase(i);
355 void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
356 Decoration::resolveNodeNames(ndef);
358 if (c_deco == CONTENT_IGNORE) {
359 c_deco = ndef->getId(deco_name);
360 if (c_deco == CONTENT_IGNORE) {
361 errorstream << "DecoSimple::resolveNodeNames: decoration node '"
362 << deco_name << "' not defined";
363 c_deco = CONTENT_AIR;
366 if (c_spawnby == CONTENT_IGNORE) {
367 c_spawnby = ndef->getId(spawnby_name);
368 if (c_spawnby == CONTENT_IGNORE) {
369 errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
370 << deco_name << "' not defined";
372 c_spawnby = CONTENT_AIR;
376 if (c_decolist.size())
379 for (size_t i = 0; i != decolist_names.size(); i++) {
380 content_t c = ndef->getId(decolist_names[i]);
381 if (c == CONTENT_IGNORE) {
382 errorstream << "DecoSimple::resolveNodeNames: decolist node '"
383 << decolist_names[i] << "' not defined";
386 c_decolist.push_back(c);
391 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, s16 start_y, v3s16 p) {
392 ManualMapVoxelManipulator *vm = mg->vm;
394 u32 vi = vm->m_area.index(p);
395 if (vm->m_data[vi].getContent() != c_place_on &&
396 c_place_on != CONTENT_IGNORE)
399 if (nspawnby != -1) {
401 v3s16 dirs[8] = { // a Moore neighborhood
412 for (int i = 0; i != 8; i++) {
413 u32 index = vm->m_area.index(p + dirs[i]);
414 if (vm->m_area.contains(index) &&
415 vm->m_data[index].getContent() == c_spawnby)
419 if (nneighs < nspawnby)
423 size_t ndecos = c_decolist.size();
424 content_t c = ndecos ? c_decolist[pr->range(0, ndecos - 1)] : c_deco;
426 s16 height = (deco_height_max > 0) ?
427 pr->range(deco_height, deco_height_max) : deco_height;
429 height = MYMIN(height, max_y - p.Y);
431 v3s16 em = vm->m_area.getExtent();
432 for (int i = start_y; i < height; i++) {
433 vm->m_area.add_y(em, vi, 1);
434 vm->m_data[vi] = MapNode(c);
439 int DecoSimple::getHeight() {
440 return (deco_height_max > 0) ? deco_height_max : deco_height;
444 std::string DecoSimple::getName() {
449 ///////////////////////////////////////////////////////////////////////////////
464 // Returns Y one under area minimum if not found
465 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
466 v3s16 em = vm->m_area.getExtent();
467 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
468 s16 y_nodes_min = vm->m_area.MinEdge.Y;
469 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
472 for (y = y_nodes_max; y >= y_nodes_min; y--) {
473 MapNode &n = vm->m_data[i];
474 if (ndef->get(n).walkable)
477 vm->m_area.add_y(em, i, -1);
479 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
483 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
484 v3s16 em = vm->m_area.getExtent();
485 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
488 for (y = ymax; y >= ymin; y--) {
489 MapNode &n = vm->m_data[i];
490 if (ndef->get(n).walkable)
493 vm->m_area.add_y(em, i, -1);
499 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
503 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
505 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
506 for (s16 x = nmin.X; x <= nmax.X; x++) {
507 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
508 heightmap[index++] = y;
511 //printf("updateHeightmap: %dus\n", t.stop());
515 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
516 bool isliquid, wasliquid;
517 v3s16 em = vm->m_area.getExtent();
519 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
520 for (s16 x = nmin.X; x <= nmax.X; x++) {
523 u32 i = vm->m_area.index(x, nmax.Y, z);
524 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
525 isliquid = ndef->get(vm->m_data[i]).isLiquid();
527 // there was a change between liquid and nonliquid, add to queue
528 if (isliquid != wasliquid)
529 trans_liquid->push_back(v3s16(x, y, z));
531 wasliquid = isliquid;
532 vm->m_area.add_y(em, i, -1);
539 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
540 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
541 VoxelArea a(nmin, nmax);
543 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
544 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
545 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
546 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
547 vm->m_data[i].param1 = light;
553 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
554 if (light <= 1 || !a.contains(p))
557 u32 vi = vm->m_area.index(p);
558 MapNode &nn = vm->m_data[vi];
561 // should probably compare masked, but doesn't seem to make a difference
562 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
567 lightSpread(a, p + v3s16(0, 0, 1), light);
568 lightSpread(a, p + v3s16(0, 1, 0), light);
569 lightSpread(a, p + v3s16(1, 0, 0), light);
570 lightSpread(a, p - v3s16(0, 0, 1), light);
571 lightSpread(a, p - v3s16(0, 1, 0), light);
572 lightSpread(a, p - v3s16(1, 0, 0), light);
576 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
577 VoxelArea a(nmin, nmax);
578 bool block_is_underground = (water_level >= nmax.Y);
580 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
581 //TimeTaker t("updateLighting");
583 // first, send vertical rays of sunshine downward
584 v3s16 em = vm->m_area.getExtent();
585 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
586 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
587 // see if we can get a light value from the overtop
588 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
589 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
590 if (block_is_underground)
592 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
595 vm->m_area.add_y(em, i, -1);
597 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
598 MapNode &n = vm->m_data[i];
599 if (!ndef->get(n).sunlight_propagates)
601 n.param1 = LIGHT_SUN;
602 vm->m_area.add_y(em, i, -1);
607 // now spread the sunlight and light up any sources
608 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
609 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
610 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
611 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
612 MapNode &n = vm->m_data[i];
613 if (n.getContent() == CONTENT_IGNORE ||
614 !ndef->get(n).light_propagates)
617 u8 light_produced = ndef->get(n).light_source & 0x0F;
619 n.param1 = light_produced;
621 u8 light = n.param1 & 0x0F;
623 lightSpread(a, v3s16(x, y, z + 1), light);
624 lightSpread(a, v3s16(x, y + 1, z ), light);
625 lightSpread(a, v3s16(x + 1, y, z ), light);
626 lightSpread(a, v3s16(x, y, z - 1), light);
627 lightSpread(a, v3s16(x, y - 1, z ), light);
628 lightSpread(a, v3s16(x - 1, y, z ), light);
634 //printf("updateLighting: %dms\n", t.stop());
638 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
639 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
640 VoxelArea a(nmin, nmax);
641 bool block_is_underground = (water_level > nmax.Y);
642 bool sunlight = !block_is_underground;
644 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
646 for (int i = 0; i < 2; i++) {
647 enum LightBank bank = banks[i];
648 std::set<v3s16> light_sources;
649 std::map<v3s16, u8> unlight_from;
651 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
652 light_sources, unlight_from);
653 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
655 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
656 vm->spreadLight(bank, light_sources, ndef);
661 //////////////////////// Mapgen V6 parameter read/write
663 bool MapgenV6Params::readParams(Settings *settings) {
664 freq_desert = settings->getFloat("mgv6_freq_desert");
665 freq_beach = settings->getFloat("mgv6_freq_beach");
668 settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base) &&
669 settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher) &&
670 settings->getNoiseParams("mgv6_np_steepness", np_steepness) &&
671 settings->getNoiseParams("mgv6_np_height_select", np_height_select) &&
672 settings->getNoiseParams("mgv6_np_mud", np_mud) &&
673 settings->getNoiseParams("mgv6_np_beach", np_beach) &&
674 settings->getNoiseParams("mgv6_np_biome", np_biome) &&
675 settings->getNoiseParams("mgv6_np_cave", np_cave) &&
676 settings->getNoiseParams("mgv6_np_humidity", np_humidity) &&
677 settings->getNoiseParams("mgv6_np_trees", np_trees) &&
678 settings->getNoiseParams("mgv6_np_apple_trees", np_apple_trees);
683 void MapgenV6Params::writeParams(Settings *settings) {
684 settings->setFloat("mgv6_freq_desert", freq_desert);
685 settings->setFloat("mgv6_freq_beach", freq_beach);
687 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
688 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
689 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
690 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
691 settings->setNoiseParams("mgv6_np_mud", np_mud);
692 settings->setNoiseParams("mgv6_np_beach", np_beach);
693 settings->setNoiseParams("mgv6_np_biome", np_biome);
694 settings->setNoiseParams("mgv6_np_cave", np_cave);
695 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
696 settings->setNoiseParams("mgv6_np_trees", np_trees);
697 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
701 bool MapgenV7Params::readParams(Settings *settings) {
703 settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base) &&
704 settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt) &&
705 settings->getNoiseParams("mgv7_np_terrain_mod", np_terrain_mod) &&
706 settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist) &&
707 settings->getNoiseParams("mgv7_np_height_select", np_height_select) &&
708 settings->getNoiseParams("mgv7_np_ridge", np_ridge);
713 void MapgenV7Params::writeParams(Settings *settings) {
714 settings->setNoiseParams("mgv7_np_terrain_base", np_terrain_base);
715 settings->setNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
716 settings->setNoiseParams("mgv7_np_terrain_mod", np_terrain_mod);
717 settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
718 settings->setNoiseParams("mgv7_np_height_select", np_height_select);
719 settings->setNoiseParams("mgv7_np_ridge", np_ridge);
723 /////////////////////////////////// legacy static functions for farmesh
725 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
726 //just need to return something
732 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
733 double sandnoise = noise2d_perlin(
734 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
735 seed+59420, 3, 0.50);
737 return (sandnoise > 0.15);
741 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
742 double noise = noise2d_perlin(
743 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
745 double zeroval = -0.39;
749 return 0.04 * (noise-zeroval) / (1.0-zeroval);