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 "serialization.h"
39 #include "util/serialize.h"
43 FlagDesc flagdesc_mapgen[] = {
46 {"dungeons", MG_DUNGEONS},
52 FlagDesc flagdesc_ore[] = {
53 {"absheight", OREFLAG_ABSHEIGHT},
54 {"scatter_noisedensity", OREFLAG_DENSITY},
55 {"claylike_nodeisnt", OREFLAG_NODEISNT},
59 FlagDesc flagdesc_deco_schematic[] = {
60 {"place_center_x", DECO_PLACE_CENTER_X},
61 {"place_center_y", DECO_PLACE_CENTER_Y},
62 {"place_center_z", DECO_PLACE_CENTER_Z},
66 FlagDesc flagdesc_gennotify[] = {
67 {"dungeon", 1 << GENNOTIFY_DUNGEON},
68 {"temple", 1 << GENNOTIFY_TEMPLE},
69 {"cave_begin", 1 << GENNOTIFY_CAVE_BEGIN},
70 {"cave_end", 1 << GENNOTIFY_CAVE_END},
71 {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
72 {"large_cave_end", 1 << GENNOTIFY_LARGECAVE_END},
76 ///////////////////////////////////////////////////////////////////////////////
79 Ore *createOre(OreType type) {
82 return new OreScatter;
85 //case ORE_CLAYLIKE: //TODO: implement this!
86 // return new OreClaylike;
99 void Ore::resolveNodeNames(INodeDefManager *ndef) {
100 if (ore == CONTENT_IGNORE) {
101 ore = ndef->getId(ore_name);
102 if (ore == CONTENT_IGNORE) {
103 errorstream << "Ore::resolveNodeNames: ore node '"
104 << ore_name << "' not defined";
106 wherein.push_back(CONTENT_AIR);
111 for (size_t i=0; i != wherein_names.size(); i++) {
112 std::string name = wherein_names[i];
113 content_t c = ndef->getId(name);
114 if (c != CONTENT_IGNORE) {
115 wherein.push_back(c);
121 void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
124 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
125 if (flags & OREFLAG_ABSHEIGHT)
126 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
131 if (in_range & ORE_RANGE_MIRROR) {
132 ymin = MYMAX(nmin.Y, -height_max);
133 ymax = MYMIN(nmax.Y, -height_min);
135 ymin = MYMAX(nmin.Y, height_min);
136 ymax = MYMIN(nmax.Y, height_max);
138 if (clust_size >= ymax - ymin + 1)
143 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
147 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
148 u32 blockseed, v3s16 nmin, v3s16 nmax) {
149 PseudoRandom pr(blockseed);
150 MapNode n_ore(ore, 0, ore_param2);
152 int volume = (nmax.X - nmin.X + 1) *
153 (nmax.Y - nmin.Y + 1) *
154 (nmax.Z - nmin.Z + 1);
155 int csize = clust_size;
156 int orechance = (csize * csize * csize) / clust_num_ores;
157 int nclusters = volume / clust_scarcity;
159 for (int i = 0; i != nclusters; i++) {
160 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
161 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
162 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
164 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
167 for (int z1 = 0; z1 != csize; z1++)
168 for (int y1 = 0; y1 != csize; y1++)
169 for (int x1 = 0; x1 != csize; x1++) {
170 if (pr.range(1, orechance) != 1)
173 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
174 for (size_t ii = 0; ii < wherein.size(); ii++)
175 if (vm->m_data[i].getContent() == wherein[ii])
176 vm->m_data[i] = n_ore;
182 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
183 u32 blockseed, v3s16 nmin, v3s16 nmax) {
184 PseudoRandom pr(blockseed + 4234);
185 MapNode n_ore(ore, 0, ore_param2);
187 int max_height = clust_size;
188 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
191 int sx = nmax.X - nmin.X + 1;
192 int sz = nmax.Z - nmin.Z + 1;
193 noise = new Noise(np, 0, sx, sz);
195 noise->seed = seed + y_start;
196 noise->perlinMap2D(nmin.X, nmin.Z);
199 for (int z = nmin.Z; z <= nmax.Z; z++)
200 for (int x = nmin.X; x <= nmax.X; x++) {
201 float noiseval = noise->result[index++];
202 if (noiseval < nthresh)
205 int height = max_height * (1. / pr.range(1, 3));
206 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
207 int y1 = y0 + height;
208 for (int y = y0; y != y1; y++) {
209 u32 i = vm->m_area.index(x, y, z);
210 if (!vm->m_area.contains(i))
213 for (size_t ii = 0; ii < wherein.size(); ii++)
214 if (vm->m_data[i].getContent() == wherein[ii])
215 vm->m_data[i] = n_ore;
221 ///////////////////////////////////////////////////////////////////////////////
224 Decoration *createDecoration(DecorationType type) {
227 return new DecoSimple;
229 return new DecoSchematic;
231 // return new DecoLSystem;
238 Decoration::Decoration() {
246 Decoration::~Decoration() {
251 void Decoration::resolveNodeNames(INodeDefManager *ndef) {
254 if (c_place_on == CONTENT_IGNORE)
255 c_place_on = ndef->getId(place_on_name);
259 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
260 PseudoRandom ps(blockseed + 53);
261 int carea_size = nmax.X - nmin.X + 1;
263 // Divide area into parts
264 if (carea_size % sidelen) {
265 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
266 "sidelen; setting sidelen to " << carea_size << std::endl;
267 sidelen = carea_size;
270 s16 divlen = carea_size / sidelen;
271 int area = sidelen * sidelen;
273 for (s16 z0 = 0; z0 < divlen; z0++)
274 for (s16 x0 = 0; x0 < divlen; x0++) {
275 v2s16 p2d_center( // Center position of part of division
276 nmin.X + sidelen / 2 + sidelen * x0,
277 nmin.Z + sidelen / 2 + sidelen * z0
279 v2s16 p2d_min( // Minimum edge of part of division
280 nmin.X + sidelen * x0,
281 nmin.Z + sidelen * z0
283 v2s16 p2d_max( // Maximum edge of part of division
284 nmin.X + sidelen + sidelen * x0 - 1,
285 nmin.Z + sidelen + sidelen * z0 - 1
288 // Amount of decorations
290 NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
292 u32 deco_count = area * MYMAX(nval, 0.f);
294 for (u32 i = 0; i < deco_count; i++) {
295 s16 x = ps.range(p2d_min.X, p2d_max.X);
296 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
298 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
300 s16 y = mg->heightmap ?
301 mg->heightmap[mapindex] :
302 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
304 if (y < nmin.Y || y > nmax.Y)
307 int height = getHeight();
308 int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
309 if (y + 1 + height > max_y) {
312 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
314 JMutexAutoLock cutofflock(cutoff_mutex);
315 cutoffs.push_back(CutoffData(x, y, z, height));
320 std::set<u8>::iterator iter;
323 iter = biomes.find(mg->biomemap[mapindex]);
324 if (iter == biomes.end())
329 generate(mg, &ps, max_y, v3s16(x, y, z));
336 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
337 PseudoRandom pr(blockseed + 53);
338 std::vector<CutoffData> handled_cutoffs;
340 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
342 JMutexAutoLock cutofflock(cutoff_mutex);
343 for (std::list<CutoffData>::iterator i = cutoffs.begin();
344 i != cutoffs.end(); ++i) {
345 CutoffData cutoff = *i;
347 s16 height = cutoff.height;
348 if (p.X < nmin.X || p.X > nmax.X ||
349 p.Z < nmin.Z || p.Z > nmax.Z)
351 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
354 handled_cutoffs.push_back(cutoff);
358 // Generate the cutoffs
359 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
360 v3s16 p = handled_cutoffs[i].p;
361 s16 height = handled_cutoffs[i].height;
363 if (p.Y + height > nmax.Y) {
364 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
365 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
368 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
371 // Remove cutoffs that were handled from the cutoff list
373 JMutexAutoLock cutofflock(cutoff_mutex);
374 for (std::list<CutoffData>::iterator i = cutoffs.begin();
375 i != cutoffs.end(); ++i) {
377 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
378 CutoffData coff = *i;
379 if (coff.p == handled_cutoffs[j].p)
380 i = cutoffs.erase(i);
388 ///////////////////////////////////////////////////////////////////////////////
391 void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
392 Decoration::resolveNodeNames(ndef);
394 if (c_deco == CONTENT_IGNORE && !decolist_names.size()) {
395 c_deco = ndef->getId(deco_name);
396 if (c_deco == CONTENT_IGNORE) {
397 errorstream << "DecoSimple::resolveNodeNames: decoration node '"
398 << deco_name << "' not defined" << std::endl;
399 c_deco = CONTENT_AIR;
402 if (c_spawnby == CONTENT_IGNORE) {
403 c_spawnby = ndef->getId(spawnby_name);
404 if (c_spawnby == CONTENT_IGNORE) {
405 errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
406 << spawnby_name << "' not defined" << std::endl;
408 c_spawnby = CONTENT_AIR;
412 if (c_decolist.size())
415 for (size_t i = 0; i != decolist_names.size(); i++) {
416 content_t c = ndef->getId(decolist_names[i]);
417 if (c == CONTENT_IGNORE) {
418 errorstream << "DecoSimple::resolveNodeNames: decolist node '"
419 << decolist_names[i] << "' not defined" << std::endl;
422 c_decolist.push_back(c);
427 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
428 ManualMapVoxelManipulator *vm = mg->vm;
430 u32 vi = vm->m_area.index(p);
431 if (vm->m_data[vi].getContent() != c_place_on &&
432 c_place_on != CONTENT_IGNORE)
435 if (nspawnby != -1) {
437 v3s16 dirs[8] = { // a Moore neighborhood
448 for (int i = 0; i != 8; i++) {
449 u32 index = vm->m_area.index(p + dirs[i]);
450 if (vm->m_area.contains(index) &&
451 vm->m_data[index].getContent() == c_spawnby)
455 if (nneighs < nspawnby)
459 size_t ndecos = c_decolist.size();
460 content_t c_place = ndecos ? c_decolist[pr->range(0, ndecos - 1)] : c_deco;
462 s16 height = (deco_height_max > 0) ?
463 pr->range(deco_height, deco_height_max) : deco_height;
465 height = MYMIN(height, max_y - p.Y);
467 v3s16 em = vm->m_area.getExtent();
468 for (int i = 0; i < height; i++) {
469 vm->m_area.add_y(em, vi, 1);
471 content_t c = vm->m_data[vi].getContent();
472 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
475 vm->m_data[vi] = MapNode(c_place);
480 int DecoSimple::getHeight() {
481 return (deco_height_max > 0) ? deco_height_max : deco_height;
485 std::string DecoSimple::getName() {
490 ///////////////////////////////////////////////////////////////////////////////
493 DecoSchematic::DecoSchematic() {
498 size = v3s16(0, 0, 0);
502 DecoSchematic::~DecoSchematic() {
505 delete []slice_probs;
509 void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
510 Decoration::resolveNodeNames(ndef);
512 if (filename.empty())
516 errorstream << "DecoSchematic::resolveNodeNames: node name list was "
517 "not created" << std::endl;
521 for (size_t i = 0; i != node_names->size(); i++) {
522 std::string name = node_names->at(i);
524 std::map<std::string, std::string>::iterator it;
525 it = replacements.find(name);
526 if (it != replacements.end())
529 content_t c = ndef->getId(name);
530 if (c == CONTENT_IGNORE) {
531 errorstream << "DecoSchematic::resolveNodeNames: node '"
532 << name << "' not defined" << std::endl;
536 c_nodes.push_back(c);
539 for (int i = 0; i != size.X * size.Y * size.Z; i++)
540 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
547 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
548 ManualMapVoxelManipulator *vm = mg->vm;
550 if (flags & DECO_PLACE_CENTER_X)
551 p.X -= (size.X + 1) / 2;
552 if (flags & DECO_PLACE_CENTER_Y)
553 p.Y -= (size.Y + 1) / 2;
554 if (flags & DECO_PLACE_CENTER_Z)
555 p.Z -= (size.Z + 1) / 2;
557 u32 vi = vm->m_area.index(p);
558 if (vm->m_data[vi].getContent() != c_place_on &&
559 c_place_on != CONTENT_IGNORE)
562 Rotation rot = (rotation == ROTATE_RAND) ?
563 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
565 blitToVManip(p, vm, rot, false);
569 int DecoSchematic::getHeight() {
574 std::string DecoSchematic::getName() {
579 void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
580 Rotation rot, bool force_placement) {
582 int ystride = size.X;
583 int zstride = size.X * size.Y;
589 int i_start, i_step_x, i_step_z;
598 i_start = zstride * (sz - 1) + sx - 1;
603 i_start = zstride * (sz - 1);
615 for (s16 y = 0; y != sy; y++) {
616 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
617 myrand_range(1, 255) > slice_probs[y])
620 for (s16 z = 0; z != sz; z++) {
621 u32 i = z * i_step_z + y * ystride + i_start;
622 for (s16 x = 0; x != sx; x++, i += i_step_x) {
623 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
624 if (!vm->m_area.contains(vi))
627 if (schematic[i].getContent() == CONTENT_IGNORE)
630 if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
633 if (!force_placement) {
634 content_t c = vm->m_data[vi].getContent();
635 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
639 if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
640 myrand_range(1, 255) > schematic[i].param1)
643 vm->m_data[vi] = schematic[i];
644 vm->m_data[vi].param1 = 0;
647 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
655 void DecoSchematic::placeStructure(Map *map, v3s16 p, bool force_placement) {
656 assert(schematic != NULL);
657 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
659 Rotation rot = (rotation == ROTATE_RAND) ?
660 (Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
662 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
663 v3s16(size.Z, size.Y, size.X) : size;
665 if (flags & DECO_PLACE_CENTER_X)
666 p.X -= (s.X + 1) / 2;
667 if (flags & DECO_PLACE_CENTER_Y)
668 p.Y -= (s.Y + 1) / 2;
669 if (flags & DECO_PLACE_CENTER_Z)
670 p.Z -= (s.Z + 1) / 2;
672 v3s16 bp1 = getNodeBlockPos(p);
673 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
674 vm->initialEmerge(bp1, bp2);
676 blitToVManip(p, vm, rot, force_placement);
678 std::map<v3s16, MapBlock *> lighting_modified_blocks;
679 std::map<v3s16, MapBlock *> modified_blocks;
680 vm->blitBackAll(&modified_blocks);
682 // TODO: Optimize this by using Mapgen::calcLighting() instead
683 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
684 map->updateLighting(lighting_modified_blocks, modified_blocks);
687 event.type = MEET_OTHER;
688 for (std::map<v3s16, MapBlock *>::iterator
689 it = modified_blocks.begin();
690 it != modified_blocks.end(); ++it)
691 event.modified_blocks.insert(it->first);
693 map->dispatchEvent(&event);
697 bool DecoSchematic::loadSchematicFile() {
698 content_t cignore = CONTENT_IGNORE;
699 bool have_cignore = false;
701 std::ifstream is(filename.c_str(), std::ios_base::binary);
703 u32 signature = readU32(is);
704 if (signature != MTSCHEM_FILE_SIGNATURE) {
705 errorstream << "loadSchematicFile: invalid schematic "
710 u16 version = readU16(is);
711 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
712 errorstream << "loadSchematicFile: unsupported schematic "
713 "file version" << std::endl;
717 size = readV3S16(is);
719 delete []slice_probs;
720 slice_probs = new u8[size.Y];
722 for (int y = 0; y != size.Y; y++)
723 slice_probs[y] = readU8(is);
725 for (int y = 0; y != size.Y; y++)
726 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
729 int nodecount = size.X * size.Y * size.Z;
731 u16 nidmapcount = readU16(is);
733 node_names = new std::vector<std::string>;
734 for (int i = 0; i != nidmapcount; i++) {
735 std::string name = deSerializeString(is);
736 if (name == "ignore") {
741 node_names->push_back(name);
745 schematic = new MapNode[nodecount];
746 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
747 nodecount, 2, 2, true);
749 if (version == 1) { // fix up the probability values
750 for (int i = 0; i != nodecount; i++) {
751 if (schematic[i].param1 == 0)
752 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
753 if (have_cignore && schematic[i].getContent() == cignore)
754 schematic[i].param1 = MTSCHEM_PROB_NEVER;
763 Minetest Schematic File Format
765 All values are stored in big-endian byte order.
766 [u32] signature: 'MTSM'
772 [u8] slice probability value
773 [Name-ID table] Name ID Mapping Table
775 For each name-id mapping:
779 For each node in schematic: (for z, y, x)
781 For each node in schematic:
782 [u8] probability of occurance (param1)
783 For each node in schematic:
789 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
790 3 - Added y-slice probabilities; this allows for variable height structures
792 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
793 std::ostringstream ss(std::ios_base::binary);
795 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
796 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
797 writeV3S16(ss, size); // schematic size
799 for (int y = 0; y != size.Y; y++) // Y slice probabilities
800 writeU8(ss, slice_probs[y]);
802 std::vector<content_t> usednodes;
803 int nodecount = size.X * size.Y * size.Z;
804 build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
806 u16 numids = usednodes.size();
807 writeU16(ss, numids); // name count
808 for (int i = 0; i != numids; i++)
809 ss << serializeString(ndef->get(usednodes[i]).name); // node names
811 // compressed bulk node data
812 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schematic,
813 nodecount, 2, 2, true);
815 fs::safeWriteToFile(filename, ss.str());
819 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
820 std::vector<content_t> *usednodes) {
821 std::map<content_t, content_t> nodeidmap;
822 content_t numids = 0;
824 for (u32 i = 0; i != nodecount; i++) {
826 content_t c = nodes[i].getContent();
828 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
829 if (it == nodeidmap.end()) {
833 usednodes->push_back(c);
834 nodeidmap.insert(std::make_pair(c, id));
838 nodes[i].setContent(id);
843 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
844 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
846 v3s16 bp1 = getNodeBlockPos(p1);
847 v3s16 bp2 = getNodeBlockPos(p2);
848 vm->initialEmerge(bp1, bp2);
852 slice_probs = new u8[size.Y];
853 for (s16 y = 0; y != size.Y; y++)
854 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
856 schematic = new MapNode[size.X * size.Y * size.Z];
859 for (s16 z = p1.Z; z <= p2.Z; z++)
860 for (s16 y = p1.Y; y <= p2.Y; y++) {
861 u32 vi = vm->m_area.index(p1.X, y, z);
862 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
863 schematic[i] = vm->m_data[vi];
864 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
873 void DecoSchematic::applyProbabilities(v3s16 p0,
874 std::vector<std::pair<v3s16, u8> > *plist,
875 std::vector<std::pair<s16, u8> > *splist) {
877 for (size_t i = 0; i != plist->size(); i++) {
878 v3s16 p = (*plist)[i].first - p0;
879 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
880 if (index < size.Z * size.Y * size.X) {
881 u8 prob = (*plist)[i].second;
882 schematic[index].param1 = prob;
884 // trim unnecessary node names from schematic
885 if (prob == MTSCHEM_PROB_NEVER)
886 schematic[index].setContent(CONTENT_AIR);
890 for (size_t i = 0; i != splist->size(); i++) {
891 s16 y = (*splist)[i].first - p0.Y;
892 slice_probs[y] = (*splist)[i].second;
897 ///////////////////////////////////////////////////////////////////////////////
910 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
911 gen_notifications[i] = new std::vector<v3s16>;
916 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
917 delete gen_notifications[i];
921 // Returns Y one under area minimum if not found
922 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
923 v3s16 em = vm->m_area.getExtent();
924 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
925 s16 y_nodes_min = vm->m_area.MinEdge.Y;
926 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
929 for (y = y_nodes_max; y >= y_nodes_min; y--) {
930 MapNode &n = vm->m_data[i];
931 if (ndef->get(n).walkable)
934 vm->m_area.add_y(em, i, -1);
936 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
940 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
941 v3s16 em = vm->m_area.getExtent();
942 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
945 for (y = ymax; y >= ymin; y--) {
946 MapNode &n = vm->m_data[i];
947 if (ndef->get(n).walkable)
950 vm->m_area.add_y(em, i, -1);
956 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
960 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
962 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
963 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
964 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
966 // if the values found are out of range, trust the old heightmap
967 if (y == nmax.Y && heightmap[index] > nmax.Y)
969 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
972 heightmap[index] = y;
975 //printf("updateHeightmap: %dus\n", t.stop());
979 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
980 bool isliquid, wasliquid;
981 v3s16 em = vm->m_area.getExtent();
983 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
984 for (s16 x = nmin.X; x <= nmax.X; x++) {
987 u32 i = vm->m_area.index(x, nmax.Y, z);
988 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
989 isliquid = ndef->get(vm->m_data[i]).isLiquid();
991 // there was a change between liquid and nonliquid, add to queue.
992 if (isliquid != wasliquid)
993 trans_liquid->push_back(v3s16(x, y, z));
995 wasliquid = isliquid;
996 vm->m_area.add_y(em, i, -1);
1003 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
1004 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1005 VoxelArea a(nmin, nmax);
1007 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1008 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
1009 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
1010 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
1011 vm->m_data[i].param1 = light;
1017 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
1018 if (light <= 1 || !a.contains(p))
1021 u32 vi = vm->m_area.index(p);
1022 MapNode &nn = vm->m_data[vi];
1025 // should probably compare masked, but doesn't seem to make a difference
1026 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
1031 lightSpread(a, p + v3s16(0, 0, 1), light);
1032 lightSpread(a, p + v3s16(0, 1, 0), light);
1033 lightSpread(a, p + v3s16(1, 0, 0), light);
1034 lightSpread(a, p - v3s16(0, 0, 1), light);
1035 lightSpread(a, p - v3s16(0, 1, 0), light);
1036 lightSpread(a, p - v3s16(1, 0, 0), light);
1040 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
1041 VoxelArea a(nmin, nmax);
1042 bool block_is_underground = (water_level >= nmax.Y);
1044 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1045 //TimeTaker t("updateLighting");
1047 // first, send vertical rays of sunshine downward
1048 v3s16 em = vm->m_area.getExtent();
1049 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1050 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
1051 // see if we can get a light value from the overtop
1052 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
1053 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
1054 if (block_is_underground)
1056 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
1059 vm->m_area.add_y(em, i, -1);
1061 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
1062 MapNode &n = vm->m_data[i];
1063 if (!ndef->get(n).sunlight_propagates)
1065 n.param1 = LIGHT_SUN;
1066 vm->m_area.add_y(em, i, -1);
1071 // now spread the sunlight and light up any sources
1072 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1073 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
1074 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
1075 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
1076 MapNode &n = vm->m_data[i];
1077 if (n.getContent() == CONTENT_IGNORE ||
1078 !ndef->get(n).light_propagates)
1081 u8 light_produced = ndef->get(n).light_source & 0x0F;
1083 n.param1 = light_produced;
1085 u8 light = n.param1 & 0x0F;
1087 lightSpread(a, v3s16(x, y, z + 1), light - 1);
1088 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
1089 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
1090 lightSpread(a, v3s16(x, y, z - 1), light - 1);
1091 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
1092 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
1098 //printf("updateLighting: %dms\n", t.stop());
1102 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
1103 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1104 VoxelArea a(nmin, nmax);
1105 bool block_is_underground = (water_level > nmax.Y);
1106 bool sunlight = !block_is_underground;
1108 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1110 for (int i = 0; i < 2; i++) {
1111 enum LightBank bank = banks[i];
1112 std::set<v3s16> light_sources;
1113 std::map<v3s16, u8> unlight_from;
1115 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
1116 light_sources, unlight_from);
1117 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
1119 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
1120 vm->spreadLight(bank, light_sources, ndef);