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"
38 #include "util/serialize.h"
40 FlagDesc flagdesc_mapgen[] = {
43 {"dungeons", MG_DUNGEONS},
44 {"v6_jungles", MGV6_JUNGLES},
45 {"v6_biome_blend", MGV6_BIOME_BLEND},
47 {"nolight", MG_NOLIGHT},
51 FlagDesc flagdesc_ore[] = {
52 {"absheight", OREFLAG_ABSHEIGHT},
53 {"scatter_noisedensity", OREFLAG_DENSITY},
54 {"claylike_nodeisnt", OREFLAG_NODEISNT},
58 FlagDesc flagdesc_deco_schematic[] = {
59 {"place_center_x", DECO_PLACE_CENTER_X},
60 {"place_center_y", DECO_PLACE_CENTER_Y},
61 {"place_center_z", DECO_PLACE_CENTER_Z},
65 ///////////////////////////////////////////////////////////////////////////////
68 Ore *createOre(OreType type) {
71 return new OreScatter;
74 //case ORE_CLAYLIKE: //TODO: implement this!
75 // return new OreClaylike;
88 void Ore::resolveNodeNames(INodeDefManager *ndef) {
89 if (ore == CONTENT_IGNORE) {
90 ore = ndef->getId(ore_name);
91 if (ore == CONTENT_IGNORE) {
92 errorstream << "Ore::resolveNodeNames: ore node '"
93 << ore_name << "' not defined";
95 wherein = CONTENT_AIR;
99 if (wherein == CONTENT_IGNORE) {
100 wherein = ndef->getId(wherein_name);
101 if (wherein == CONTENT_IGNORE) {
102 errorstream << "Ore::resolveNodeNames: wherein node '"
103 << wherein_name << "' not defined";
105 wherein = CONTENT_AIR;
111 void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
114 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
115 if (flags & OREFLAG_ABSHEIGHT)
116 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
121 if (in_range & ORE_RANGE_MIRROR) {
122 ymin = MYMAX(nmin.Y, -height_max);
123 ymax = MYMIN(nmax.Y, -height_min);
125 ymin = MYMAX(nmin.Y, height_min);
126 ymax = MYMIN(nmax.Y, height_max);
128 if (clust_size >= ymax - ymin + 1)
133 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
137 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
138 u32 blockseed, v3s16 nmin, v3s16 nmax) {
139 PseudoRandom pr(blockseed);
140 MapNode n_ore(ore, 0, ore_param2);
142 int volume = (nmax.X - nmin.X + 1) *
143 (nmax.Y - nmin.Y + 1) *
144 (nmax.Z - nmin.Z + 1);
145 int csize = clust_size;
146 int orechance = (csize * csize * csize) / clust_num_ores;
147 int nclusters = volume / clust_scarcity;
149 for (int i = 0; i != nclusters; i++) {
150 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
151 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
152 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
154 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
157 for (int z1 = 0; z1 != csize; z1++)
158 for (int y1 = 0; y1 != csize; y1++)
159 for (int x1 = 0; x1 != csize; x1++) {
160 if (pr.range(1, orechance) != 1)
163 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
164 if (vm->m_data[i].getContent() == wherein)
165 vm->m_data[i] = n_ore;
171 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
172 u32 blockseed, v3s16 nmin, v3s16 nmax) {
173 PseudoRandom pr(blockseed + 4234);
174 MapNode n_ore(ore, 0, ore_param2);
176 int max_height = clust_size;
177 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
180 int sx = nmax.X - nmin.X + 1;
181 int sz = nmax.Z - nmin.Z + 1;
182 noise = new Noise(np, 0, sx, sz);
184 noise->seed = seed + y_start;
185 noise->perlinMap2D(nmin.X, nmin.Z);
188 for (int z = nmin.Z; z <= nmax.Z; z++)
189 for (int x = nmin.X; x <= nmax.X; x++) {
190 float noiseval = noise->result[index++];
191 if (noiseval < nthresh)
194 int height = max_height * (1. / pr.range(1, 3));
195 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
196 int y1 = y0 + height;
197 for (int y = y0; y != y1; y++) {
198 u32 i = vm->m_area.index(x, y, z);
199 if (!vm->m_area.contains(i))
202 if (vm->m_data[i].getContent() == wherein)
203 vm->m_data[i] = n_ore;
209 ///////////////////////////////////////////////////////////////////////////////
212 Decoration *createDecoration(DecorationType type) {
215 return new DecoSimple;
217 return new DecoSchematic;
219 // return new DecoLSystem;
226 Decoration::Decoration() {
234 Decoration::~Decoration() {
239 void Decoration::resolveNodeNames(INodeDefManager *ndef) {
240 if (c_place_on == CONTENT_IGNORE)
241 c_place_on = ndef->getId(place_on_name);
245 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
246 PseudoRandom ps(blockseed + 53);
247 int carea_size = nmax.X - nmin.X + 1;
249 // Divide area into parts
250 if (carea_size % sidelen) {
251 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
252 "sidelen; setting sidelen to " << carea_size << std::endl;
253 sidelen = carea_size;
256 s16 divlen = carea_size / sidelen;
257 int area = sidelen * sidelen;
259 for (s16 z0 = 0; z0 < divlen; z0++)
260 for (s16 x0 = 0; x0 < divlen; x0++) {
261 v2s16 p2d_center( // Center position of part of division
262 nmin.X + sidelen / 2 + sidelen * x0,
263 nmin.Z + sidelen / 2 + sidelen * z0
265 v2s16 p2d_min( // Minimum edge of part of division
266 nmin.X + sidelen * x0,
267 nmin.Z + sidelen * z0
269 v2s16 p2d_max( // Maximum edge of part of division
270 nmin.X + sidelen + sidelen * x0 - 1,
271 nmin.Z + sidelen + sidelen * z0 - 1
274 // Amount of decorations
276 NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
278 u32 deco_count = area * MYMAX(nval, 0.f);
280 for (u32 i = 0; i < deco_count; i++) {
281 s16 x = ps.range(p2d_min.X, p2d_max.X);
282 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
284 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
286 s16 y = mg->heightmap ?
287 mg->heightmap[mapindex] :
288 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
290 if (y < nmin.Y || y > nmax.Y)
293 int height = getHeight();
294 int max_y = nmax.Y + MAP_BLOCKSIZE;
295 if (y + 1 + height > max_y) {
298 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
300 JMutexAutoLock cutofflock(cutoff_mutex);
301 cutoffs.push_back(CutoffData(x, y, z, height));
306 std::set<u8>::iterator iter;
309 iter = biomes.find(mg->biomemap[mapindex]);
310 if (iter == biomes.end())
315 generate(mg, &ps, max_y, v3s16(x, y, z));
322 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
323 PseudoRandom pr(blockseed + 53);
324 std::vector<CutoffData> handled_cutoffs;
326 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
328 JMutexAutoLock cutofflock(cutoff_mutex);
329 for (std::list<CutoffData>::iterator i = cutoffs.begin();
330 i != cutoffs.end(); ++i) {
331 CutoffData cutoff = *i;
333 s16 height = cutoff.height;
334 if (p.X < nmin.X || p.X > nmax.X ||
335 p.Z < nmin.Z || p.Z > nmax.Z)
337 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
340 handled_cutoffs.push_back(cutoff);
344 // Generate the cutoffs
345 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
346 v3s16 p = handled_cutoffs[i].p;
347 s16 height = handled_cutoffs[i].height;
349 if (p.Y + height > nmax.Y) {
350 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
351 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
354 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
357 // Remove cutoffs that were handled from the cutoff list
359 JMutexAutoLock cutofflock(cutoff_mutex);
360 for (std::list<CutoffData>::iterator i = cutoffs.begin();
361 i != cutoffs.end(); ++i) {
363 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
364 CutoffData coff = *i;
365 if (coff.p == handled_cutoffs[j].p)
366 i = cutoffs.erase(i);
374 ///////////////////////////////////////////////////////////////////////////////
377 void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
378 Decoration::resolveNodeNames(ndef);
380 if (c_deco == CONTENT_IGNORE) {
381 c_deco = ndef->getId(deco_name);
382 if (c_deco == CONTENT_IGNORE) {
383 errorstream << "DecoSimple::resolveNodeNames: decoration node '"
384 << deco_name << "' not defined" << std::endl;
385 c_deco = CONTENT_AIR;
388 if (c_spawnby == CONTENT_IGNORE) {
389 c_spawnby = ndef->getId(spawnby_name);
390 if (c_spawnby == CONTENT_IGNORE) {
391 errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
392 << deco_name << "' not defined" << std::endl;
394 c_spawnby = CONTENT_AIR;
398 if (c_decolist.size())
401 for (size_t i = 0; i != decolist_names.size(); i++) {
402 content_t c = ndef->getId(decolist_names[i]);
403 if (c == CONTENT_IGNORE) {
404 errorstream << "DecoSimple::resolveNodeNames: decolist node '"
405 << decolist_names[i] << "' not defined" << std::endl;
408 c_decolist.push_back(c);
413 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
414 ManualMapVoxelManipulator *vm = mg->vm;
416 u32 vi = vm->m_area.index(p);
417 if (vm->m_data[vi].getContent() != c_place_on &&
418 c_place_on != CONTENT_IGNORE)
421 if (nspawnby != -1) {
423 v3s16 dirs[8] = { // a Moore neighborhood
434 for (int i = 0; i != 8; i++) {
435 u32 index = vm->m_area.index(p + dirs[i]);
436 if (vm->m_area.contains(index) &&
437 vm->m_data[index].getContent() == c_spawnby)
441 if (nneighs < nspawnby)
445 size_t ndecos = c_decolist.size();
446 content_t c_place = ndecos ? c_decolist[pr->range(0, ndecos - 1)] : c_deco;
448 s16 height = (deco_height_max > 0) ?
449 pr->range(deco_height, deco_height_max) : deco_height;
451 height = MYMIN(height, max_y - p.Y);
453 v3s16 em = vm->m_area.getExtent();
454 for (int i = 0; i < height; i++) {
455 vm->m_area.add_y(em, vi, 1);
457 content_t c = vm->m_data[vi].getContent();
458 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
461 vm->m_data[vi] = MapNode(c_place);
466 int DecoSimple::getHeight() {
467 return (deco_height_max > 0) ? deco_height_max : deco_height;
471 std::string DecoSimple::getName() {
476 ///////////////////////////////////////////////////////////////////////////////
479 DecoSchematic::DecoSchematic() {
483 size = v3s16(0, 0, 0);
487 DecoSchematic::~DecoSchematic() {
493 void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
494 Decoration::resolveNodeNames(ndef);
496 if (filename.empty())
500 errorstream << "DecoSchematic::resolveNodeNames: node name list was "
501 "not created" << std::endl;
505 for (size_t i = 0; i != node_names->size(); i++) {
506 content_t c = ndef->getId(node_names->at(i));
507 if (c == CONTENT_IGNORE) {
508 errorstream << "DecoSchematic::resolveNodeNames: node '"
509 << node_names->at(i) << "' not defined" << std::endl;
512 c_nodes.push_back(c);
515 for (int i = 0; i != size.X * size.Y * size.Z; i++)
516 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
523 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
524 ManualMapVoxelManipulator *vm = mg->vm;
526 if (flags & DECO_PLACE_CENTER_X)
527 p.X -= (size.X + 1) / 2;
528 if (flags & DECO_PLACE_CENTER_Y)
529 p.Y -= (size.Y + 1) / 2;
530 if (flags & DECO_PLACE_CENTER_Z)
531 p.Z -= (size.Z + 1) / 2;
533 u32 vi = vm->m_area.index(p);
534 if (vm->m_data[vi].getContent() != c_place_on &&
535 c_place_on != CONTENT_IGNORE)
539 for (s16 z = 0; z != size.Z; z++)
540 for (s16 y = 0; y != size.Y; y++) {
541 vi = vm->m_area.index(p.X, p.Y + y, p.Z + z);
542 for (s16 x = 0; x != size.X; x++, i++, vi++) {
543 if (!vm->m_area.contains(vi))
546 if (schematic[i].getContent() == CONTENT_IGNORE)
549 content_t c = vm->m_data[vi].getContent();
550 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
553 if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
556 vm->m_data[vi] = schematic[i];
557 vm->m_data[vi].param1 = 0;
563 int DecoSchematic::getHeight() {
568 std::string DecoSchematic::getName() {
573 void DecoSchematic::placeStructure(Map *map, v3s16 p) {
574 assert(schematic != NULL);
575 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
577 if (flags & DECO_PLACE_CENTER_X)
578 p.X -= (size.X + 1) / 2;
579 if (flags & DECO_PLACE_CENTER_Y)
580 p.Y -= (size.Y + 1) / 2;
581 if (flags & DECO_PLACE_CENTER_Z)
582 p.Z -= (size.Z + 1) / 2;
584 v3s16 bp1 = getNodeBlockPos(p);
585 v3s16 bp2 = getNodeBlockPos(p + size - v3s16(1,1,1));
586 vm->initialEmerge(bp1, bp2);
589 for (s16 z = 0; z != size.Z; z++)
590 for (s16 y = 0; y != size.Y; y++) {
591 u32 vi = vm->m_area.index(p.X, p.Y + y, p.Z + z);
592 for (s16 x = 0; x != size.X; x++, i++, vi++) {
593 if (!vm->m_area.contains(vi))
596 if (schematic[i].getContent() == CONTENT_IGNORE)
599 if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
602 vm->m_data[vi] = schematic[i];
603 vm->m_data[vi].param1 = 0;
607 std::map<v3s16, MapBlock *> lighting_modified_blocks;
608 std::map<v3s16, MapBlock *> modified_blocks;
609 vm->blitBackAll(&modified_blocks);
611 // TODO: Optimize this by using Mapgen::calcLighting() instead
612 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
613 map->updateLighting(lighting_modified_blocks, modified_blocks);
616 event.type = MEET_OTHER;
617 for (std::map<v3s16, MapBlock *>::iterator
618 it = modified_blocks.begin();
619 it != modified_blocks.end(); ++it)
620 event.modified_blocks.insert(it->first);
622 map->dispatchEvent(&event);
626 bool DecoSchematic::loadSchematicFile() {
627 std::ifstream is(filename.c_str(), std::ios_base::binary);
629 u32 signature = readU32(is);
630 if (signature != 'MTSM') {
631 errorstream << "loadSchematicFile: invalid schematic "
636 u16 version = readU16(is);
638 errorstream << "loadSchematicFile: unsupported schematic "
639 "file version" << std::endl;
643 size = readV3S16(is);
644 int nodecount = size.X * size.Y * size.Z;
646 u16 nidmapcount = readU16(is);
648 node_names = new std::vector<std::string>;
649 for (int i = 0; i != nidmapcount; i++) {
650 std::string name = deSerializeString(is);
651 node_names->push_back(name);
655 schematic = new MapNode[nodecount];
656 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST, schematic,
657 nodecount, 2, 2, true);
664 Minetest Schematic File Format
666 All values are stored in big-endian byte order.
667 [u32] signature: 'MTSM'
672 [Name-ID table] Name ID Mapping Table
674 For each name-id mapping:
678 For each node in schematic: (for z, y, x)
680 For each node in schematic:
681 [u8] probability of occurance (param1)
682 For each node in schematic:
686 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
687 std::ofstream os(filename.c_str(), std::ios_base::binary);
689 writeU32(os, 'MTSM'); // signature
690 writeU16(os, 1); // version
691 writeV3S16(os, size); // schematic size
693 std::vector<content_t> usednodes;
694 int nodecount = size.X * size.Y * size.Z;
695 build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
697 u16 numids = usednodes.size();
698 writeU16(os, numids); // name count
699 for (int i = 0; i != numids; i++)
700 os << serializeString(ndef->get(usednodes[i]).name); // node names
702 // compressed bulk node data
703 MapNode::serializeBulk(os, SER_FMT_VER_HIGHEST, schematic,
704 nodecount, 2, 2, true);
708 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
709 std::vector<content_t> *usednodes) {
710 std::map<content_t, content_t> nodeidmap;
711 content_t numids = 0;
713 for (u32 i = 0; i != nodecount; i++) {
715 content_t c = nodes[i].getContent();
717 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
718 if (it == nodeidmap.end()) {
722 usednodes->push_back(c);
723 nodeidmap.insert(std::make_pair(c, id));
727 nodes[i].setContent(id);
732 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
733 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
735 v3s16 bp1 = getNodeBlockPos(p1);
736 v3s16 bp2 = getNodeBlockPos(p2);
737 vm->initialEmerge(bp1, bp2);
740 schematic = new MapNode[size.X * size.Y * size.Z];
743 for (s16 z = p1.Z; z <= p2.Z; z++)
744 for (s16 y = p1.Y; y <= p2.Y; y++) {
745 u32 vi = vm->m_area.index(p1.X, y, z);
746 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
747 schematic[i] = vm->m_data[vi];
748 schematic[i].param1 = 0;
757 void DecoSchematic::applyProbabilities(std::vector<std::pair<v3s16, s16> > *plist, v3s16 p0) {
758 for (size_t i = 0; i != plist->size(); i++) {
759 v3s16 p = (*plist)[i].first - p0;
760 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
761 if (index < size.Z * size.Y * size.X) {
762 s16 prob = (*plist)[i].second;
764 schematic[index].param1 = prob;
766 schematic[index].setContent(CONTENT_IGNORE);
772 ///////////////////////////////////////////////////////////////////////////////
787 // Returns Y one under area minimum if not found
788 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
789 v3s16 em = vm->m_area.getExtent();
790 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
791 s16 y_nodes_min = vm->m_area.MinEdge.Y;
792 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
795 for (y = y_nodes_max; y >= y_nodes_min; y--) {
796 MapNode &n = vm->m_data[i];
797 if (ndef->get(n).walkable)
800 vm->m_area.add_y(em, i, -1);
802 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
806 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
807 v3s16 em = vm->m_area.getExtent();
808 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
811 for (y = ymax; y >= ymin; y--) {
812 MapNode &n = vm->m_data[i];
813 if (ndef->get(n).walkable)
816 vm->m_area.add_y(em, i, -1);
822 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
826 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
828 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
829 for (s16 x = nmin.X; x <= nmax.X; x++) {
830 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
831 heightmap[index++] = y;
834 //printf("updateHeightmap: %dus\n", t.stop());
838 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
839 bool isliquid, wasliquid;
840 v3s16 em = vm->m_area.getExtent();
842 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
843 for (s16 x = nmin.X; x <= nmax.X; x++) {
846 u32 i = vm->m_area.index(x, nmax.Y, z);
847 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
848 isliquid = ndef->get(vm->m_data[i]).isLiquid();
850 // there was a change between liquid and nonliquid, add to queue
851 if (isliquid != wasliquid)
852 trans_liquid->push_back(v3s16(x, y, z));
854 wasliquid = isliquid;
855 vm->m_area.add_y(em, i, -1);
862 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
863 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
864 VoxelArea a(nmin, nmax);
866 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
867 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
868 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
869 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
870 vm->m_data[i].param1 = light;
876 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
877 if (light <= 1 || !a.contains(p))
880 u32 vi = vm->m_area.index(p);
881 MapNode &nn = vm->m_data[vi];
884 // should probably compare masked, but doesn't seem to make a difference
885 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
890 lightSpread(a, p + v3s16(0, 0, 1), light);
891 lightSpread(a, p + v3s16(0, 1, 0), light);
892 lightSpread(a, p + v3s16(1, 0, 0), light);
893 lightSpread(a, p - v3s16(0, 0, 1), light);
894 lightSpread(a, p - v3s16(0, 1, 0), light);
895 lightSpread(a, p - v3s16(1, 0, 0), light);
899 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
900 VoxelArea a(nmin, nmax);
901 bool block_is_underground = (water_level >= nmax.Y);
903 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
904 //TimeTaker t("updateLighting");
906 // first, send vertical rays of sunshine downward
907 v3s16 em = vm->m_area.getExtent();
908 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
909 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
910 // see if we can get a light value from the overtop
911 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
912 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
913 if (block_is_underground)
915 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
918 vm->m_area.add_y(em, i, -1);
920 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
921 MapNode &n = vm->m_data[i];
922 if (!ndef->get(n).sunlight_propagates)
924 n.param1 = LIGHT_SUN;
925 vm->m_area.add_y(em, i, -1);
930 // now spread the sunlight and light up any sources
931 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
932 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
933 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
934 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
935 MapNode &n = vm->m_data[i];
936 if (n.getContent() == CONTENT_IGNORE ||
937 !ndef->get(n).light_propagates)
940 u8 light_produced = ndef->get(n).light_source & 0x0F;
942 n.param1 = light_produced;
944 u8 light = n.param1 & 0x0F;
946 lightSpread(a, v3s16(x, y, z + 1), light - 1);
947 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
948 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
949 lightSpread(a, v3s16(x, y, z - 1), light - 1);
950 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
951 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
957 //printf("updateLighting: %dms\n", t.stop());
961 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
962 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
963 VoxelArea a(nmin, nmax);
964 bool block_is_underground = (water_level > nmax.Y);
965 bool sunlight = !block_is_underground;
967 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
969 for (int i = 0; i < 2; i++) {
970 enum LightBank bank = banks[i];
971 std::set<v3s16> light_sources;
972 std::map<v3s16, u8> unlight_from;
974 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
975 light_sources, unlight_from);
976 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
978 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
979 vm->spreadLight(bank, light_sources, ndef);
984 //////////////////////// Mapgen V6 parameter read/write
986 bool MapgenV6Params::readParams(Settings *settings) {
987 freq_desert = settings->getFloat("mgv6_freq_desert");
988 freq_beach = settings->getFloat("mgv6_freq_beach");
991 settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base) &&
992 settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher) &&
993 settings->getNoiseParams("mgv6_np_steepness", np_steepness) &&
994 settings->getNoiseParams("mgv6_np_height_select", np_height_select) &&
995 settings->getNoiseParams("mgv6_np_mud", np_mud) &&
996 settings->getNoiseParams("mgv6_np_beach", np_beach) &&
997 settings->getNoiseParams("mgv6_np_biome", np_biome) &&
998 settings->getNoiseParams("mgv6_np_cave", np_cave) &&
999 settings->getNoiseParams("mgv6_np_humidity", np_humidity) &&
1000 settings->getNoiseParams("mgv6_np_trees", np_trees) &&
1001 settings->getNoiseParams("mgv6_np_apple_trees", np_apple_trees);
1006 void MapgenV6Params::writeParams(Settings *settings) {
1007 settings->setFloat("mgv6_freq_desert", freq_desert);
1008 settings->setFloat("mgv6_freq_beach", freq_beach);
1010 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
1011 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
1012 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
1013 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
1014 settings->setNoiseParams("mgv6_np_mud", np_mud);
1015 settings->setNoiseParams("mgv6_np_beach", np_beach);
1016 settings->setNoiseParams("mgv6_np_biome", np_biome);
1017 settings->setNoiseParams("mgv6_np_cave", np_cave);
1018 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
1019 settings->setNoiseParams("mgv6_np_trees", np_trees);
1020 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
1024 bool MapgenV7Params::readParams(Settings *settings) {
1026 settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base) &&
1027 settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt) &&
1028 settings->getNoiseParams("mgv7_np_terrain_mod", np_terrain_mod) &&
1029 settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist) &&
1030 settings->getNoiseParams("mgv7_np_height_select", np_height_select) &&
1031 settings->getNoiseParams("mgv7_np_ridge", np_ridge);
1036 void MapgenV7Params::writeParams(Settings *settings) {
1037 settings->setNoiseParams("mgv7_np_terrain_base", np_terrain_base);
1038 settings->setNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);
1039 settings->setNoiseParams("mgv7_np_terrain_mod", np_terrain_mod);
1040 settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
1041 settings->setNoiseParams("mgv7_np_height_select", np_height_select);
1042 settings->setNoiseParams("mgv7_np_ridge", np_ridge);
1046 /////////////////////////////////// legacy static functions for farmesh
1048 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
1049 //just need to return something
1055 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
1056 double sandnoise = noise2d_perlin(
1057 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
1058 seed+59420, 3, 0.50);
1060 return (sandnoise > 0.15);
1064 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
1065 double noise = noise2d_perlin(
1066 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
1068 double zeroval = -0.39;
1072 return 0.04 * (noise-zeroval) / (1.0-zeroval);