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 "content_sao.h"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
35 #include "mapgen_v6.h"
36 #include "mapgen_v7.h"
37 #include "serialization.h"
38 #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::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
102 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
103 if (flags & OREFLAG_ABSHEIGHT)
104 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
109 if (in_range & ORE_RANGE_MIRROR) {
110 ymin = MYMAX(nmin.Y, -height_max);
111 ymax = MYMIN(nmax.Y, -height_min);
113 ymin = MYMAX(nmin.Y, height_min);
114 ymax = MYMIN(nmax.Y, height_max);
116 if (clust_size >= ymax - ymin + 1)
121 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
125 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
126 u32 blockseed, v3s16 nmin, v3s16 nmax) {
127 PseudoRandom pr(blockseed);
128 MapNode n_ore(c_ore, 0, ore_param2);
130 int volume = (nmax.X - nmin.X + 1) *
131 (nmax.Y - nmin.Y + 1) *
132 (nmax.Z - nmin.Z + 1);
133 int csize = clust_size;
134 int orechance = (csize * csize * csize) / clust_num_ores;
135 int nclusters = volume / clust_scarcity;
137 for (int i = 0; i != nclusters; i++) {
138 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
139 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
140 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
142 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
145 for (int z1 = 0; z1 != csize; z1++)
146 for (int y1 = 0; y1 != csize; y1++)
147 for (int x1 = 0; x1 != csize; x1++) {
148 if (pr.range(1, orechance) != 1)
151 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
152 for (size_t ii = 0; ii < c_wherein.size(); ii++)
153 if (vm->m_data[i].getContent() == c_wherein[ii])
154 vm->m_data[i] = n_ore;
160 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
161 u32 blockseed, v3s16 nmin, v3s16 nmax) {
162 PseudoRandom pr(blockseed + 4234);
163 MapNode n_ore(c_ore, 0, ore_param2);
165 int max_height = clust_size;
166 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
169 int sx = nmax.X - nmin.X + 1;
170 int sz = nmax.Z - nmin.Z + 1;
171 noise = new Noise(np, 0, sx, sz);
173 noise->seed = seed + y_start;
174 noise->perlinMap2D(nmin.X, nmin.Z);
177 for (int z = nmin.Z; z <= nmax.Z; z++)
178 for (int x = nmin.X; x <= nmax.X; x++) {
179 float noiseval = noise->result[index++];
180 if (noiseval < nthresh)
183 int height = max_height * (1. / pr.range(1, 3));
184 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
185 int y1 = y0 + height;
186 for (int y = y0; y != y1; y++) {
187 u32 i = vm->m_area.index(x, y, z);
188 if (!vm->m_area.contains(i))
191 for (size_t ii = 0; ii < c_wherein.size(); ii++) {
192 if (vm->m_data[i].getContent() == c_wherein[ii]) {
193 vm->m_data[i] = n_ore;
202 ///////////////////////////////////////////////////////////////////////////////
205 Decoration *createDecoration(DecorationType type) {
208 return new DecoSimple;
210 return new DecoSchematic;
212 // return new DecoLSystem;
219 Decoration::Decoration() {
227 Decoration::~Decoration() {
232 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
233 PseudoRandom ps(blockseed + 53);
234 int carea_size = nmax.X - nmin.X + 1;
236 // Divide area into parts
237 if (carea_size % sidelen) {
238 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
239 "sidelen; setting sidelen to " << carea_size << std::endl;
240 sidelen = carea_size;
243 s16 divlen = carea_size / sidelen;
244 int area = sidelen * sidelen;
246 for (s16 z0 = 0; z0 < divlen; z0++)
247 for (s16 x0 = 0; x0 < divlen; x0++) {
248 v2s16 p2d_center( // Center position of part of division
249 nmin.X + sidelen / 2 + sidelen * x0,
250 nmin.Z + sidelen / 2 + sidelen * z0
252 v2s16 p2d_min( // Minimum edge of part of division
253 nmin.X + sidelen * x0,
254 nmin.Z + sidelen * z0
256 v2s16 p2d_max( // Maximum edge of part of division
257 nmin.X + sidelen + sidelen * x0 - 1,
258 nmin.Z + sidelen + sidelen * z0 - 1
261 // Amount of decorations
263 NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
265 u32 deco_count = area * MYMAX(nval, 0.f);
267 for (u32 i = 0; i < deco_count; i++) {
268 s16 x = ps.range(p2d_min.X, p2d_max.X);
269 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
271 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
273 s16 y = mg->heightmap ?
274 mg->heightmap[mapindex] :
275 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
277 if (y < nmin.Y || y > nmax.Y)
280 int height = getHeight();
281 int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
282 if (y + 1 + height > max_y) {
285 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
287 JMutexAutoLock cutofflock(cutoff_mutex);
288 cutoffs.push_back(CutoffData(x, y, z, height));
293 std::set<u8>::iterator iter;
296 iter = biomes.find(mg->biomemap[mapindex]);
297 if (iter == biomes.end())
302 generate(mg, &ps, max_y, v3s16(x, y, z));
309 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
310 PseudoRandom pr(blockseed + 53);
311 std::vector<CutoffData> handled_cutoffs;
313 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
315 JMutexAutoLock cutofflock(cutoff_mutex);
316 for (std::list<CutoffData>::iterator i = cutoffs.begin();
317 i != cutoffs.end(); ++i) {
318 CutoffData cutoff = *i;
320 s16 height = cutoff.height;
321 if (p.X < nmin.X || p.X > nmax.X ||
322 p.Z < nmin.Z || p.Z > nmax.Z)
324 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
327 handled_cutoffs.push_back(cutoff);
331 // Generate the cutoffs
332 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
333 v3s16 p = handled_cutoffs[i].p;
334 s16 height = handled_cutoffs[i].height;
336 if (p.Y + height > nmax.Y) {
337 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
338 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
341 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
344 // Remove cutoffs that were handled from the cutoff list
346 JMutexAutoLock cutofflock(cutoff_mutex);
347 for (std::list<CutoffData>::iterator i = cutoffs.begin();
348 i != cutoffs.end(); ++i) {
350 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
351 CutoffData coff = *i;
352 if (coff.p == handled_cutoffs[j].p)
353 i = cutoffs.erase(i);
361 ///////////////////////////////////////////////////////////////////////////////
364 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
365 ManualMapVoxelManipulator *vm = mg->vm;
367 u32 vi = vm->m_area.index(p);
368 content_t c = vm->m_data[vi].getContent();
370 for (idx = 0; idx != c_place_on.size(); idx++) {
371 if (c == c_place_on[idx])
374 if ((idx != 0) && (idx == c_place_on.size()))
377 if (nspawnby != -1) {
379 v3s16 dirs[8] = { // a Moore neighborhood
390 for (int i = 0; i != 8; i++) {
391 u32 index = vm->m_area.index(p + dirs[i]);
392 if (!vm->m_area.contains(index))
395 content_t c = vm->m_data[index].getContent();
396 for (size_t j = 0; j != c_spawnby.size(); j++) {
397 if (c == c_spawnby[j]) {
404 if (nneighs < nspawnby)
408 if (c_decos.size() == 0)
410 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
412 s16 height = (deco_height_max > 0) ?
413 pr->range(deco_height, deco_height_max) : deco_height;
415 height = MYMIN(height, max_y - p.Y);
417 v3s16 em = vm->m_area.getExtent();
418 for (int i = 0; i < height; i++) {
419 vm->m_area.add_y(em, vi, 1);
421 content_t c = vm->m_data[vi].getContent();
422 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
425 vm->m_data[vi] = MapNode(c_place);
430 int DecoSimple::getHeight() {
431 return (deco_height_max > 0) ? deco_height_max : deco_height;
435 std::string DecoSimple::getName() {
440 ///////////////////////////////////////////////////////////////////////////////
443 DecoSchematic::DecoSchematic() {
447 size = v3s16(0, 0, 0);
451 DecoSchematic::~DecoSchematic() {
453 delete []slice_probs;
457 void DecoSchematic::updateContentIds() {
458 if (flags & DECO_SCHEM_CIDS_UPDATED)
461 flags |= DECO_SCHEM_CIDS_UPDATED;
463 for (int i = 0; i != size.X * size.Y * size.Z; i++)
464 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
468 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
469 ManualMapVoxelManipulator *vm = mg->vm;
471 if (flags & DECO_PLACE_CENTER_X)
472 p.X -= (size.X + 1) / 2;
473 if (flags & DECO_PLACE_CENTER_Y)
474 p.Y -= (size.Y + 1) / 2;
475 if (flags & DECO_PLACE_CENTER_Z)
476 p.Z -= (size.Z + 1) / 2;
478 u32 vi = vm->m_area.index(p);
479 content_t c = vm->m_data[vi].getContent();
481 for (idx = 0; idx != c_place_on.size(); idx++) {
482 if (c == c_place_on[idx])
485 if ((idx != 0) && (idx == c_place_on.size()))
488 Rotation rot = (rotation == ROTATE_RAND) ?
489 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
491 blitToVManip(p, vm, rot, false);
495 int DecoSchematic::getHeight() {
500 std::string DecoSchematic::getName() {
505 void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
506 Rotation rot, bool force_placement) {
508 int ystride = size.X;
509 int zstride = size.X * size.Y;
517 int i_start, i_step_x, i_step_z;
526 i_start = zstride * (sz - 1) + sx - 1;
531 i_start = zstride * (sz - 1);
543 for (s16 y = 0; y != sy; y++) {
544 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
545 myrand_range(1, 255) > slice_probs[y])
548 for (s16 z = 0; z != sz; z++) {
549 u32 i = z * i_step_z + y * ystride + i_start;
550 for (s16 x = 0; x != sx; x++, i += i_step_x) {
551 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
552 if (!vm->m_area.contains(vi))
555 if (schematic[i].getContent() == CONTENT_IGNORE)
558 if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
561 if (!force_placement) {
562 content_t c = vm->m_data[vi].getContent();
563 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
567 if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
568 myrand_range(1, 255) > schematic[i].param1)
571 vm->m_data[vi] = schematic[i];
572 vm->m_data[vi].param1 = 0;
575 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
583 void DecoSchematic::placeStructure(Map *map, v3s16 p, bool force_placement) {
584 assert(schematic != NULL);
585 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
587 Rotation rot = (rotation == ROTATE_RAND) ?
588 (Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
590 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
591 v3s16(size.Z, size.Y, size.X) : size;
593 if (flags & DECO_PLACE_CENTER_X)
594 p.X -= (s.X + 1) / 2;
595 if (flags & DECO_PLACE_CENTER_Y)
596 p.Y -= (s.Y + 1) / 2;
597 if (flags & DECO_PLACE_CENTER_Z)
598 p.Z -= (s.Z + 1) / 2;
600 v3s16 bp1 = getNodeBlockPos(p);
601 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
602 vm->initialEmerge(bp1, bp2);
604 blitToVManip(p, vm, rot, force_placement);
606 std::map<v3s16, MapBlock *> lighting_modified_blocks;
607 std::map<v3s16, MapBlock *> modified_blocks;
608 vm->blitBackAll(&modified_blocks);
610 // TODO: Optimize this by using Mapgen::calcLighting() instead
611 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
612 map->updateLighting(lighting_modified_blocks, modified_blocks);
615 event.type = MEET_OTHER;
616 for (std::map<v3s16, MapBlock *>::iterator
617 it = modified_blocks.begin();
618 it != modified_blocks.end(); ++it)
619 event.modified_blocks.insert(it->first);
621 map->dispatchEvent(&event);
625 bool DecoSchematic::loadSchematicFile(NodeResolver *resolver,
626 std::map<std::string, std::string> &replace_names)
628 content_t cignore = CONTENT_IGNORE;
629 bool have_cignore = false;
631 std::ifstream is(filename.c_str(), std::ios_base::binary);
633 u32 signature = readU32(is);
634 if (signature != MTSCHEM_FILE_SIGNATURE) {
635 errorstream << "loadSchematicFile: invalid schematic "
640 u16 version = readU16(is);
641 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
642 errorstream << "loadSchematicFile: unsupported schematic "
643 "file version" << std::endl;
647 size = readV3S16(is);
649 delete []slice_probs;
650 slice_probs = new u8[size.Y];
652 for (int y = 0; y != size.Y; y++)
653 slice_probs[y] = readU8(is);
655 for (int y = 0; y != size.Y; y++)
656 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
659 int nodecount = size.X * size.Y * size.Z;
661 u16 nidmapcount = readU16(is);
663 for (int i = 0; i != nidmapcount; i++) {
664 std::string name = deSerializeString(is);
665 if (name == "ignore") {
671 std::map<std::string, std::string>::iterator it;
673 it = replace_names.find(name);
674 if (it != replace_names.end())
677 resolver->addNodeList(name.c_str(), &c_nodes);
681 schematic = new MapNode[nodecount];
682 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
683 nodecount, 2, 2, true);
685 if (version == 1) { // fix up the probability values
686 for (int i = 0; i != nodecount; i++) {
687 if (schematic[i].param1 == 0)
688 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
689 if (have_cignore && schematic[i].getContent() == cignore)
690 schematic[i].param1 = MTSCHEM_PROB_NEVER;
699 Minetest Schematic File Format
701 All values are stored in big-endian byte order.
702 [u32] signature: 'MTSM'
708 [u8] slice probability value
709 [Name-ID table] Name ID Mapping Table
711 For each name-id mapping:
715 For each node in schematic: (for z, y, x)
717 For each node in schematic:
718 [u8] probability of occurance (param1)
719 For each node in schematic:
725 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
726 3 - Added y-slice probabilities; this allows for variable height structures
728 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
729 std::ostringstream ss(std::ios_base::binary);
731 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
732 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
733 writeV3S16(ss, size); // schematic size
735 for (int y = 0; y != size.Y; y++) // Y slice probabilities
736 writeU8(ss, slice_probs[y]);
738 std::vector<content_t> usednodes;
739 int nodecount = size.X * size.Y * size.Z;
740 build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
742 u16 numids = usednodes.size();
743 writeU16(ss, numids); // name count
744 for (int i = 0; i != numids; i++)
745 ss << serializeString(ndef->get(usednodes[i]).name); // node names
747 // compressed bulk node data
748 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schematic,
749 nodecount, 2, 2, true);
751 fs::safeWriteToFile(filename, ss.str());
755 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
756 std::vector<content_t> *usednodes) {
757 std::map<content_t, content_t> nodeidmap;
758 content_t numids = 0;
760 for (u32 i = 0; i != nodecount; i++) {
762 content_t c = nodes[i].getContent();
764 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
765 if (it == nodeidmap.end()) {
769 usednodes->push_back(c);
770 nodeidmap.insert(std::make_pair(c, id));
774 nodes[i].setContent(id);
779 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
780 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
782 v3s16 bp1 = getNodeBlockPos(p1);
783 v3s16 bp2 = getNodeBlockPos(p2);
784 vm->initialEmerge(bp1, bp2);
788 slice_probs = new u8[size.Y];
789 for (s16 y = 0; y != size.Y; y++)
790 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
792 schematic = new MapNode[size.X * size.Y * size.Z];
795 for (s16 z = p1.Z; z <= p2.Z; z++)
796 for (s16 y = p1.Y; y <= p2.Y; y++) {
797 u32 vi = vm->m_area.index(p1.X, y, z);
798 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
799 schematic[i] = vm->m_data[vi];
800 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
809 void DecoSchematic::applyProbabilities(v3s16 p0,
810 std::vector<std::pair<v3s16, u8> > *plist,
811 std::vector<std::pair<s16, u8> > *splist) {
813 for (size_t i = 0; i != plist->size(); i++) {
814 v3s16 p = (*plist)[i].first - p0;
815 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
816 if (index < size.Z * size.Y * size.X) {
817 u8 prob = (*plist)[i].second;
818 schematic[index].param1 = prob;
820 // trim unnecessary node names from schematic
821 if (prob == MTSCHEM_PROB_NEVER)
822 schematic[index].setContent(CONTENT_AIR);
826 for (size_t i = 0; i != splist->size(); i++) {
827 s16 y = (*splist)[i].first - p0.Y;
828 slice_probs[y] = (*splist)[i].second;
833 ///////////////////////////////////////////////////////////////////////////////
846 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
847 gen_notifications[i] = new std::vector<v3s16>;
852 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
853 delete gen_notifications[i];
857 // Returns Y one under area minimum if not found
858 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
859 v3s16 em = vm->m_area.getExtent();
860 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
861 s16 y_nodes_min = vm->m_area.MinEdge.Y;
862 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
865 for (y = y_nodes_max; y >= y_nodes_min; y--) {
866 MapNode &n = vm->m_data[i];
867 if (ndef->get(n).walkable)
870 vm->m_area.add_y(em, i, -1);
872 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
876 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
877 v3s16 em = vm->m_area.getExtent();
878 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
881 for (y = ymax; y >= ymin; y--) {
882 MapNode &n = vm->m_data[i];
883 if (ndef->get(n).walkable)
886 vm->m_area.add_y(em, i, -1);
892 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
896 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
898 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
899 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
900 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
902 // if the values found are out of range, trust the old heightmap
903 if (y == nmax.Y && heightmap[index] > nmax.Y)
905 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
908 heightmap[index] = y;
911 //printf("updateHeightmap: %dus\n", t.stop());
915 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
916 bool isliquid, wasliquid;
917 v3s16 em = vm->m_area.getExtent();
919 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
920 for (s16 x = nmin.X; x <= nmax.X; x++) {
923 u32 i = vm->m_area.index(x, nmax.Y, z);
924 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
925 isliquid = ndef->get(vm->m_data[i]).isLiquid();
927 // there was a change between liquid and nonliquid, add to queue.
928 if (isliquid != wasliquid)
929 trans_liquid->push_back(v3s16(x, y, z));
931 wasliquid = isliquid;
932 vm->m_area.add_y(em, i, -1);
939 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
940 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
941 VoxelArea a(nmin, nmax);
943 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
944 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
945 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
946 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
947 vm->m_data[i].param1 = light;
953 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
954 if (light <= 1 || !a.contains(p))
957 u32 vi = vm->m_area.index(p);
958 MapNode &nn = vm->m_data[vi];
961 // should probably compare masked, but doesn't seem to make a difference
962 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
967 lightSpread(a, p + v3s16(0, 0, 1), light);
968 lightSpread(a, p + v3s16(0, 1, 0), light);
969 lightSpread(a, p + v3s16(1, 0, 0), light);
970 lightSpread(a, p - v3s16(0, 0, 1), light);
971 lightSpread(a, p - v3s16(0, 1, 0), light);
972 lightSpread(a, p - v3s16(1, 0, 0), light);
976 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
977 VoxelArea a(nmin, nmax);
978 bool block_is_underground = (water_level >= nmax.Y);
980 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
981 //TimeTaker t("updateLighting");
983 // first, send vertical rays of sunshine downward
984 v3s16 em = vm->m_area.getExtent();
985 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
986 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
987 // see if we can get a light value from the overtop
988 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
989 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
990 if (block_is_underground)
992 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
995 vm->m_area.add_y(em, i, -1);
997 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
998 MapNode &n = vm->m_data[i];
999 if (!ndef->get(n).sunlight_propagates)
1001 n.param1 = LIGHT_SUN;
1002 vm->m_area.add_y(em, i, -1);
1007 // now spread the sunlight and light up any sources
1008 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1009 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
1010 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
1011 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
1012 MapNode &n = vm->m_data[i];
1013 if (n.getContent() == CONTENT_IGNORE ||
1014 !ndef->get(n).light_propagates)
1017 u8 light_produced = ndef->get(n).light_source & 0x0F;
1019 n.param1 = light_produced;
1021 u8 light = n.param1 & 0x0F;
1023 lightSpread(a, v3s16(x, y, z + 1), light - 1);
1024 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
1025 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
1026 lightSpread(a, v3s16(x, y, z - 1), light - 1);
1027 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
1028 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
1034 //printf("updateLighting: %dms\n", t.stop());
1038 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
1039 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1040 VoxelArea a(nmin, nmax);
1041 bool block_is_underground = (water_level > nmax.Y);
1042 bool sunlight = !block_is_underground;
1044 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1046 for (int i = 0; i < 2; i++) {
1047 enum LightBank bank = banks[i];
1048 std::set<v3s16> light_sources;
1049 std::map<v3s16, u8> unlight_from;
1051 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
1052 light_sources, unlight_from);
1053 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
1055 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
1056 vm->spreadLight(bank, light_sources, ndef);