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;
112 resolveNodeNames(mg->ndef);
115 if (in_range & ORE_RANGE_MIRROR) {
116 ymin = MYMAX(nmin.Y, -height_max);
117 ymax = MYMIN(nmax.Y, -height_min);
119 ymin = MYMAX(nmin.Y, height_min);
120 ymax = MYMIN(nmax.Y, height_max);
122 if (clust_size >= ymax - ymin + 1)
127 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
131 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
132 u32 blockseed, v3s16 nmin, v3s16 nmax) {
133 PseudoRandom pr(blockseed);
134 MapNode n_ore(ore, 0, ore_param2);
136 int volume = (nmax.X - nmin.X + 1) *
137 (nmax.Y - nmin.Y + 1) *
138 (nmax.Z - nmin.Z + 1);
139 int csize = clust_size;
140 int orechance = (csize * csize * csize) / clust_num_ores;
141 int nclusters = volume / clust_scarcity;
143 for (int i = 0; i != nclusters; i++) {
144 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
145 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
146 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
148 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
151 for (int z1 = 0; z1 != csize; z1++)
152 for (int y1 = 0; y1 != csize; y1++)
153 for (int x1 = 0; x1 != csize; x1++) {
154 if (pr.range(1, orechance) != 1)
157 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
158 if (vm->m_data[i].getContent() == wherein)
159 vm->m_data[i] = n_ore;
165 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
166 u32 blockseed, v3s16 nmin, v3s16 nmax) {
167 PseudoRandom pr(blockseed + 4234);
168 MapNode n_ore(ore, 0, ore_param2);
170 int max_height = clust_size;
171 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
174 int sx = nmax.X - nmin.X + 1;
175 int sz = nmax.Z - nmin.Z + 1;
176 noise = new Noise(np, 0, sx, sz);
178 noise->seed = seed + y_start;
179 noise->perlinMap2D(nmin.X, nmin.Z);
182 for (int z = nmin.Z; z <= nmax.Z; z++)
183 for (int x = nmin.X; x <= nmax.X; x++) {
184 float noiseval = noise->result[index++];
185 if (noiseval < nthresh)
188 int height = max_height * (1. / pr.range(1, 3));
189 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
190 int y1 = y0 + height;
191 for (int y = y0; y != y1; y++) {
192 u32 i = vm->m_area.index(x, y, z);
193 if (!vm->m_area.contains(i))
196 if (vm->m_data[i].getContent() == wherein)
197 vm->m_data[i] = n_ore;
203 Decoration *createDecoration(DecorationType type) {
206 return new DecoSimple;
207 //case DECO_SCHEMATIC:
208 // return new DecoSchematic;
210 // return new DecoLSystem;
217 Decoration::~Decoration() {
222 void Decoration::resolveNodeNames(INodeDefManager *ndef) {
223 if (c_place_on == CONTENT_IGNORE)
224 c_place_on = ndef->getId(place_on_name);
228 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
229 resolveNodeNames(mg->ndef);
231 PseudoRandom ps(blockseed + 53);
232 int carea_size = nmax.X - nmin.X + 1;
234 // Divide area into parts
235 if (carea_size % sidelen) {
236 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
237 "sidelen; setting sidelen to " << carea_size << std::endl;
238 sidelen = carea_size;
241 s16 divlen = carea_size / sidelen;
242 int area = sidelen * sidelen;
244 for (s16 z0 = 0; z0 < divlen; z0++)
245 for (s16 x0 = 0; x0 < divlen; x0++) {
246 v2s16 p2d_center( // Center position of part of division
247 nmin.X + sidelen / 2 + sidelen * x0,
248 nmin.Z + sidelen / 2 + sidelen * z0
250 v2s16 p2d_min( // Minimum edge of part of division
251 nmin.X + sidelen * x0,
252 nmin.Z + sidelen * z0
254 v2s16 p2d_max( // Maximum edge of part of division
255 nmin.X + sidelen + sidelen * x0 - 1,
256 nmin.Z + sidelen + sidelen * z0 - 1
259 // Amount of decorations
261 NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
263 u32 deco_count = area * MYMAX(nval, 0.f);
265 for (u32 i = 0; i < deco_count; i++) {
266 s16 x = ps.range(p2d_min.X, p2d_max.X);
267 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
269 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
271 s16 y = mg->heightmap ?
272 mg->heightmap[mapindex] :
273 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
275 if (y < nmin.Y || y > nmax.Y)
278 int height = getHeight();
279 int max_y = nmax.Y + MAP_BLOCKSIZE;
280 if (y + 1 + height > max_y) {
283 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
285 JMutexAutoLock cutofflock(cutoff_mutex);
286 cutoffs.push_back(CutoffData(x, y, z, height));
291 std::set<u8>::iterator iter;
294 iter = biomes.find(mg->biomemap[mapindex]);
295 if (iter == biomes.end())
300 generate(mg, &ps, max_y, 0, v3s16(x, y, z));
307 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
308 PseudoRandom pr(blockseed + 53);
309 std::vector<CutoffData> handled_cutoffs;
311 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
313 JMutexAutoLock cutofflock(cutoff_mutex);
314 for (std::list<CutoffData>::iterator i = cutoffs.begin();
315 i != cutoffs.end(); ++i) {
316 CutoffData cutoff = *i;
318 s16 height = cutoff.height;
319 if (p.X < nmin.X || p.X > nmax.X ||
320 p.Z < nmin.Z || p.Z > nmax.Z)
322 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
325 handled_cutoffs.push_back(cutoff);
329 // Generate the cutoffs
330 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
331 v3s16 p = handled_cutoffs[i].p;
332 s16 height = handled_cutoffs[i].height;
334 if (p.Y + height > nmax.Y) {
335 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
336 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
339 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
342 // Remove cutoffs that were handled from the cutoff list
344 JMutexAutoLock cutofflock(cutoff_mutex);
345 for (std::list<CutoffData>::iterator i = cutoffs.begin();
346 i != cutoffs.end(); ++i) {
348 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
349 CutoffData coff = *i;
350 if (coff.p == handled_cutoffs[j].p)
351 i = cutoffs.erase(i);
359 void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
360 Decoration::resolveNodeNames(ndef);
362 if (c_deco == CONTENT_IGNORE) {
363 c_deco = ndef->getId(deco_name);
364 if (c_deco == CONTENT_IGNORE) {
365 errorstream << "DecoSimple::resolveNodeNames: decoration node '"
366 << deco_name << "' not defined";
367 c_deco = CONTENT_AIR;
370 if (c_spawnby == CONTENT_IGNORE) {
371 c_spawnby = ndef->getId(spawnby_name);
372 if (c_spawnby == CONTENT_IGNORE) {
373 errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
374 << deco_name << "' not defined";
376 c_spawnby = CONTENT_AIR;
380 if (c_decolist.size())
383 for (size_t i = 0; i != decolist_names.size(); i++) {
384 content_t c = ndef->getId(decolist_names[i]);
385 if (c == CONTENT_IGNORE) {
386 errorstream << "DecoSimple::resolveNodeNames: decolist node '"
387 << decolist_names[i] << "' not defined";
390 c_decolist.push_back(c);
395 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, s16 start_y, v3s16 p) {
396 ManualMapVoxelManipulator *vm = mg->vm;
398 u32 vi = vm->m_area.index(p);
399 if (vm->m_data[vi].getContent() != c_place_on &&
400 c_place_on != CONTENT_IGNORE)
403 if (nspawnby != -1) {
405 v3s16 dirs[8] = { // a Moore neighborhood
416 for (int i = 0; i != 8; i++) {
417 u32 index = vm->m_area.index(p + dirs[i]);
418 if (vm->m_area.contains(index) &&
419 vm->m_data[index].getContent() == c_spawnby)
423 if (nneighs < nspawnby)
427 size_t ndecos = c_decolist.size();
428 content_t c = ndecos ? c_decolist[pr->range(0, ndecos - 1)] : c_deco;
430 s16 height = (deco_height_max > 0) ?
431 pr->range(deco_height, deco_height_max) : deco_height;
433 height = MYMIN(height, max_y - p.Y);
435 v3s16 em = vm->m_area.getExtent();
436 for (int i = start_y; i < height; i++) {
437 vm->m_area.add_y(em, vi, 1);
438 vm->m_data[vi] = MapNode(c);
443 int DecoSimple::getHeight() {
444 return (deco_height_max > 0) ? deco_height_max : deco_height;
448 std::string DecoSimple::getName() {
453 ///////////////////////////////////////////////////////////////////////////////
468 // Returns Y one under area minimum if not found
469 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
470 v3s16 em = vm->m_area.getExtent();
471 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
472 s16 y_nodes_min = vm->m_area.MinEdge.Y;
473 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
476 for (y = y_nodes_max; y >= y_nodes_min; y--) {
477 MapNode &n = vm->m_data[i];
478 if (ndef->get(n).walkable)
481 vm->m_area.add_y(em, i, -1);
483 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
487 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
488 v3s16 em = vm->m_area.getExtent();
489 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
492 for (y = ymax; y >= ymin; y--) {
493 MapNode &n = vm->m_data[i];
494 if (ndef->get(n).walkable)
497 vm->m_area.add_y(em, i, -1);
503 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
507 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
509 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
510 for (s16 x = nmin.X; x <= nmax.X; x++) {
511 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
512 heightmap[index++] = y;
515 //printf("updateHeightmap: %dus\n", t.stop());
519 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
520 bool isliquid, wasliquid;
521 v3s16 em = vm->m_area.getExtent();
523 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
524 for (s16 x = nmin.X; x <= nmax.X; x++) {
527 u32 i = vm->m_area.index(x, nmax.Y, z);
528 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
529 isliquid = ndef->get(vm->m_data[i]).isLiquid();
531 // there was a change between liquid and nonliquid, add to queue
532 if (isliquid != wasliquid)
533 trans_liquid->push_back(v3s16(x, y, z));
535 wasliquid = isliquid;
536 vm->m_area.add_y(em, i, -1);
543 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
544 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
545 VoxelArea a(nmin, nmax);
547 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
548 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
549 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
550 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
551 vm->m_data[i].param1 = light;
557 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
558 if (light <= 1 || !a.contains(p))
561 u32 vi = vm->m_area.index(p);
562 MapNode &nn = vm->m_data[vi];
565 // should probably compare masked, but doesn't seem to make a difference
566 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
571 lightSpread(a, p + v3s16(0, 0, 1), light);
572 lightSpread(a, p + v3s16(0, 1, 0), light);
573 lightSpread(a, p + v3s16(1, 0, 0), light);
574 lightSpread(a, p - v3s16(0, 0, 1), light);
575 lightSpread(a, p - v3s16(0, 1, 0), light);
576 lightSpread(a, p - v3s16(1, 0, 0), light);
580 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
581 VoxelArea a(nmin, nmax);
582 bool block_is_underground = (water_level >= nmax.Y);
584 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
585 //TimeTaker t("updateLighting");
587 // first, send vertical rays of sunshine downward
588 v3s16 em = vm->m_area.getExtent();
589 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
590 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
591 // see if we can get a light value from the overtop
592 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
593 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
594 if (block_is_underground)
596 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
599 vm->m_area.add_y(em, i, -1);
601 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
602 MapNode &n = vm->m_data[i];
603 if (!ndef->get(n).sunlight_propagates)
605 n.param1 = LIGHT_SUN;
606 vm->m_area.add_y(em, i, -1);
611 // now spread the sunlight and light up any sources
612 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
613 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
614 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
615 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
616 MapNode &n = vm->m_data[i];
617 if (n.getContent() == CONTENT_IGNORE ||
618 !ndef->get(n).light_propagates)
621 u8 light_produced = ndef->get(n).light_source & 0x0F;
623 n.param1 = light_produced;
625 u8 light = n.param1 & 0x0F;
627 lightSpread(a, v3s16(x, y, z + 1), light);
628 lightSpread(a, v3s16(x, y + 1, z ), light);
629 lightSpread(a, v3s16(x + 1, y, z ), light);
630 lightSpread(a, v3s16(x, y, z - 1), light);
631 lightSpread(a, v3s16(x, y - 1, z ), light);
632 lightSpread(a, v3s16(x - 1, y, z ), light);
638 //printf("updateLighting: %dms\n", t.stop());
642 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
643 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
644 VoxelArea a(nmin, nmax);
645 bool block_is_underground = (water_level > nmax.Y);
646 bool sunlight = !block_is_underground;
648 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
650 for (int i = 0; i < 2; i++) {
651 enum LightBank bank = banks[i];
652 std::set<v3s16> light_sources;
653 std::map<v3s16, u8> unlight_from;
655 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
656 light_sources, unlight_from);
657 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
659 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
660 vm->spreadLight(bank, light_sources, ndef);
665 //////////////////////// Mapgen V6 parameter read/write
667 bool MapgenV6Params::readParams(Settings *settings) {
668 freq_desert = settings->getFloat("mgv6_freq_desert");
669 freq_beach = settings->getFloat("mgv6_freq_beach");
672 settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base) &&
673 settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher) &&
674 settings->getNoiseParams("mgv6_np_steepness", np_steepness) &&
675 settings->getNoiseParams("mgv6_np_height_select", np_height_select) &&
676 settings->getNoiseParams("mgv6_np_mud", np_mud) &&
677 settings->getNoiseParams("mgv6_np_beach", np_beach) &&
678 settings->getNoiseParams("mgv6_np_biome", np_biome) &&
679 settings->getNoiseParams("mgv6_np_cave", np_cave) &&
680 settings->getNoiseParams("mgv6_np_humidity", np_humidity) &&
681 settings->getNoiseParams("mgv6_np_trees", np_trees) &&
682 settings->getNoiseParams("mgv6_np_apple_trees", np_apple_trees);
687 void MapgenV6Params::writeParams(Settings *settings) {
688 settings->setFloat("mgv6_freq_desert", freq_desert);
689 settings->setFloat("mgv6_freq_beach", freq_beach);
691 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
692 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
693 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
694 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
695 settings->setNoiseParams("mgv6_np_mud", np_mud);
696 settings->setNoiseParams("mgv6_np_beach", np_beach);
697 settings->setNoiseParams("mgv6_np_biome", np_biome);
698 settings->setNoiseParams("mgv6_np_cave", np_cave);
699 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
700 settings->setNoiseParams("mgv6_np_trees", np_trees);
701 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
705 bool MapgenV7Params::readParams(Settings *settings) {
707 settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base) &&
708 settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt) &&
709 settings->getNoiseParams("mgv7_np_terrain_mod", np_terrain_mod) &&
710 settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist) &&
711 settings->getNoiseParams("mgv7_np_height_select", np_height_select) &&
712 settings->getNoiseParams("mgv7_np_ridge", np_ridge);
717 void MapgenV7Params::writeParams(Settings *settings) {
718 settings->setNoiseParams("mgv7_np_terrain_base", np_terrain_base);
719 settings->setNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
720 settings->setNoiseParams("mgv7_np_terrain_mod", np_terrain_mod);
721 settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
722 settings->setNoiseParams("mgv7_np_height_select", np_height_select);
723 settings->setNoiseParams("mgv7_np_ridge", np_ridge);
727 /////////////////////////////////// legacy static functions for farmesh
729 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
730 //just need to return something
736 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
737 double sandnoise = noise2d_perlin(
738 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
739 seed+59420, 3, 0.50);
741 return (sandnoise > 0.15);
745 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
746 double noise = noise2d_perlin(
747 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
749 double zeroval = -0.39;
753 return 0.04 * (noise-zeroval) / (1.0-zeroval);