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 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
155 vm->m_data[i] = n_ore;
161 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
162 u32 blockseed, v3s16 nmin, v3s16 nmax) {
163 PseudoRandom pr(blockseed + 4234);
164 MapNode n_ore(c_ore, 0, ore_param2);
166 int max_height = clust_size;
167 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
170 int sx = nmax.X - nmin.X + 1;
171 int sz = nmax.Z - nmin.Z + 1;
172 noise = new Noise(np, 0, sx, sz);
174 noise->seed = seed + y_start;
175 noise->perlinMap2D(nmin.X, nmin.Z);
178 for (int z = nmin.Z; z <= nmax.Z; z++)
179 for (int x = nmin.X; x <= nmax.X; x++) {
180 float noiseval = noise->result[index++];
181 if (noiseval < nthresh)
184 int height = max_height * (1. / pr.range(1, 3));
185 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
186 int y1 = y0 + height;
187 for (int y = y0; y != y1; y++) {
188 u32 i = vm->m_area.index(x, y, z);
189 if (!vm->m_area.contains(i))
191 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
194 vm->m_data[i] = n_ore;
200 ///////////////////////////////////////////////////////////////////////////////
203 Decoration *createDecoration(DecorationType type) {
206 return new DecoSimple;
208 return new DecoSchematic;
210 // return new DecoLSystem;
217 Decoration::Decoration() {
225 Decoration::~Decoration() {
230 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
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 - 1;
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, 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 ///////////////////////////////////////////////////////////////////////////////
361 bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p) {
362 // Don't bother if there aren't any decorations to place
363 if (c_decos.size() == 0)
366 u32 vi = vm->m_area.index(p);
368 // Check if the decoration can be placed on this node
369 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
372 // Don't continue if there are no spawnby constraints
388 // Check a Moore neighborhood if there are enough spawnby nodes
389 for (size_t i = 0; i != ARRLEN(dirs); i++) {
390 u32 index = vm->m_area.index(p + dirs[i]);
391 if (!vm->m_area.contains(index))
394 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
398 if (nneighs < nspawnby)
405 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
406 ManualMapVoxelManipulator *vm = mg->vm;
408 if (!canPlaceDecoration(vm, p))
411 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
413 s16 height = (deco_height_max > 0) ?
414 pr->range(deco_height, deco_height_max) : deco_height;
416 height = MYMIN(height, max_y - p.Y);
418 v3s16 em = vm->m_area.getExtent();
419 u32 vi = vm->m_area.index(p);
420 for (int i = 0; i < height; i++) {
421 vm->m_area.add_y(em, vi, 1);
423 content_t c = vm->m_data[vi].getContent();
424 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
427 vm->m_data[vi] = MapNode(c_place);
432 int DecoSimple::getHeight() {
433 return (deco_height_max > 0) ? deco_height_max : deco_height;
437 std::string DecoSimple::getName() {
442 ///////////////////////////////////////////////////////////////////////////////
445 DecoSchematic::DecoSchematic() {
449 size = v3s16(0, 0, 0);
453 DecoSchematic::~DecoSchematic() {
455 delete []slice_probs;
459 void DecoSchematic::updateContentIds() {
460 if (flags & DECO_SCHEM_CIDS_UPDATED)
463 flags |= DECO_SCHEM_CIDS_UPDATED;
465 for (int i = 0; i != size.X * size.Y * size.Z; i++)
466 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
470 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
471 ManualMapVoxelManipulator *vm = mg->vm;
473 if (flags & DECO_PLACE_CENTER_X)
474 p.X -= (size.X + 1) / 2;
475 if (flags & DECO_PLACE_CENTER_Y)
476 p.Y -= (size.Y + 1) / 2;
477 if (flags & DECO_PLACE_CENTER_Z)
478 p.Z -= (size.Z + 1) / 2;
480 u32 vi = vm->m_area.index(p);
481 content_t c = vm->m_data[vi].getContent();
482 if (!CONTAINS(c_place_on, c))
485 Rotation rot = (rotation == ROTATE_RAND) ?
486 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
488 blitToVManip(p, vm, rot, false);
492 int DecoSchematic::getHeight() {
497 std::string DecoSchematic::getName() {
502 void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
503 Rotation rot, bool force_placement) {
505 int ystride = size.X;
506 int zstride = size.X * size.Y;
514 int i_start, i_step_x, i_step_z;
523 i_start = zstride * (sz - 1) + sx - 1;
528 i_start = zstride * (sz - 1);
540 for (s16 y = 0; y != sy; y++) {
541 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
542 myrand_range(1, 255) > slice_probs[y])
545 for (s16 z = 0; z != sz; z++) {
546 u32 i = z * i_step_z + y * ystride + i_start;
547 for (s16 x = 0; x != sx; x++, i += i_step_x) {
548 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
549 if (!vm->m_area.contains(vi))
552 if (schematic[i].getContent() == CONTENT_IGNORE)
555 if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
558 if (!force_placement) {
559 content_t c = vm->m_data[vi].getContent();
560 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
564 if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
565 myrand_range(1, 255) > schematic[i].param1)
568 vm->m_data[vi] = schematic[i];
569 vm->m_data[vi].param1 = 0;
572 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
580 void DecoSchematic::placeStructure(Map *map, v3s16 p, bool force_placement) {
581 assert(schematic != NULL);
582 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
584 Rotation rot = (rotation == ROTATE_RAND) ?
585 (Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
587 v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
588 v3s16(size.Z, size.Y, size.X) : size;
590 if (flags & DECO_PLACE_CENTER_X)
591 p.X -= (s.X + 1) / 2;
592 if (flags & DECO_PLACE_CENTER_Y)
593 p.Y -= (s.Y + 1) / 2;
594 if (flags & DECO_PLACE_CENTER_Z)
595 p.Z -= (s.Z + 1) / 2;
597 v3s16 bp1 = getNodeBlockPos(p);
598 v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
599 vm->initialEmerge(bp1, bp2);
601 blitToVManip(p, vm, rot, force_placement);
603 std::map<v3s16, MapBlock *> lighting_modified_blocks;
604 std::map<v3s16, MapBlock *> modified_blocks;
605 vm->blitBackAll(&modified_blocks);
607 // TODO: Optimize this by using Mapgen::calcLighting() instead
608 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
609 map->updateLighting(lighting_modified_blocks, modified_blocks);
612 event.type = MEET_OTHER;
613 for (std::map<v3s16, MapBlock *>::iterator
614 it = modified_blocks.begin();
615 it != modified_blocks.end(); ++it)
616 event.modified_blocks.insert(it->first);
618 map->dispatchEvent(&event);
622 bool DecoSchematic::loadSchematicFile(NodeResolver *resolver,
623 std::map<std::string, std::string> &replace_names)
625 content_t cignore = CONTENT_IGNORE;
626 bool have_cignore = false;
628 std::ifstream is(filename.c_str(), std::ios_base::binary);
630 u32 signature = readU32(is);
631 if (signature != MTSCHEM_FILE_SIGNATURE) {
632 errorstream << "loadSchematicFile: invalid schematic "
637 u16 version = readU16(is);
638 if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
639 errorstream << "loadSchematicFile: unsupported schematic "
640 "file version" << std::endl;
644 size = readV3S16(is);
646 delete []slice_probs;
647 slice_probs = new u8[size.Y];
649 for (int y = 0; y != size.Y; y++)
650 slice_probs[y] = readU8(is);
652 for (int y = 0; y != size.Y; y++)
653 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
656 int nodecount = size.X * size.Y * size.Z;
658 u16 nidmapcount = readU16(is);
660 for (int i = 0; i != nidmapcount; i++) {
661 std::string name = deSerializeString(is);
662 if (name == "ignore") {
668 std::map<std::string, std::string>::iterator it;
670 it = replace_names.find(name);
671 if (it != replace_names.end())
674 resolver->addNodeList(name.c_str(), &c_nodes);
678 schematic = new MapNode[nodecount];
679 MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
680 nodecount, 2, 2, true);
682 if (version == 1) { // fix up the probability values
683 for (int i = 0; i != nodecount; i++) {
684 if (schematic[i].param1 == 0)
685 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
686 if (have_cignore && schematic[i].getContent() == cignore)
687 schematic[i].param1 = MTSCHEM_PROB_NEVER;
696 Minetest Schematic File Format
698 All values are stored in big-endian byte order.
699 [u32] signature: 'MTSM'
705 [u8] slice probability value
706 [Name-ID table] Name ID Mapping Table
708 For each name-id mapping:
712 For each node in schematic: (for z, y, x)
714 For each node in schematic:
715 [u8] probability of occurance (param1)
716 For each node in schematic:
722 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
723 3 - Added y-slice probabilities; this allows for variable height structures
725 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
726 std::ostringstream ss(std::ios_base::binary);
728 writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
729 writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
730 writeV3S16(ss, size); // schematic size
732 for (int y = 0; y != size.Y; y++) // Y slice probabilities
733 writeU8(ss, slice_probs[y]);
735 std::vector<content_t> usednodes;
736 int nodecount = size.X * size.Y * size.Z;
737 build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
739 u16 numids = usednodes.size();
740 writeU16(ss, numids); // name count
741 for (int i = 0; i != numids; i++)
742 ss << serializeString(ndef->get(usednodes[i]).name); // node names
744 // compressed bulk node data
745 MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schematic,
746 nodecount, 2, 2, true);
748 fs::safeWriteToFile(filename, ss.str());
752 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
753 std::vector<content_t> *usednodes) {
754 std::map<content_t, content_t> nodeidmap;
755 content_t numids = 0;
757 for (u32 i = 0; i != nodecount; i++) {
759 content_t c = nodes[i].getContent();
761 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
762 if (it == nodeidmap.end()) {
766 usednodes->push_back(c);
767 nodeidmap.insert(std::make_pair(c, id));
771 nodes[i].setContent(id);
776 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
777 ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
779 v3s16 bp1 = getNodeBlockPos(p1);
780 v3s16 bp2 = getNodeBlockPos(p2);
781 vm->initialEmerge(bp1, bp2);
785 slice_probs = new u8[size.Y];
786 for (s16 y = 0; y != size.Y; y++)
787 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
789 schematic = new MapNode[size.X * size.Y * size.Z];
792 for (s16 z = p1.Z; z <= p2.Z; z++)
793 for (s16 y = p1.Y; y <= p2.Y; y++) {
794 u32 vi = vm->m_area.index(p1.X, y, z);
795 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
796 schematic[i] = vm->m_data[vi];
797 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
806 void DecoSchematic::applyProbabilities(v3s16 p0,
807 std::vector<std::pair<v3s16, u8> > *plist,
808 std::vector<std::pair<s16, u8> > *splist) {
810 for (size_t i = 0; i != plist->size(); i++) {
811 v3s16 p = (*plist)[i].first - p0;
812 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
813 if (index < size.Z * size.Y * size.X) {
814 u8 prob = (*plist)[i].second;
815 schematic[index].param1 = prob;
817 // trim unnecessary node names from schematic
818 if (prob == MTSCHEM_PROB_NEVER)
819 schematic[index].setContent(CONTENT_AIR);
823 for (size_t i = 0; i != splist->size(); i++) {
824 s16 y = (*splist)[i].first - p0.Y;
825 slice_probs[y] = (*splist)[i].second;
830 ///////////////////////////////////////////////////////////////////////////////
843 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
844 gen_notifications[i] = new std::vector<v3s16>;
849 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
850 delete gen_notifications[i];
854 // Returns Y one under area minimum if not found
855 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
856 v3s16 em = vm->m_area.getExtent();
857 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
858 s16 y_nodes_min = vm->m_area.MinEdge.Y;
859 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
862 for (y = y_nodes_max; y >= y_nodes_min; y--) {
863 MapNode &n = vm->m_data[i];
864 if (ndef->get(n).walkable)
867 vm->m_area.add_y(em, i, -1);
869 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
873 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
874 v3s16 em = vm->m_area.getExtent();
875 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
878 for (y = ymax; y >= ymin; y--) {
879 MapNode &n = vm->m_data[i];
880 if (ndef->get(n).walkable)
883 vm->m_area.add_y(em, i, -1);
889 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
893 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
895 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
896 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
897 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
899 // if the values found are out of range, trust the old heightmap
900 if (y == nmax.Y && heightmap[index] > nmax.Y)
902 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
905 heightmap[index] = y;
908 //printf("updateHeightmap: %dus\n", t.stop());
912 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
913 bool isliquid, wasliquid;
914 v3s16 em = vm->m_area.getExtent();
916 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
917 for (s16 x = nmin.X; x <= nmax.X; x++) {
920 u32 i = vm->m_area.index(x, nmax.Y, z);
921 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
922 isliquid = ndef->get(vm->m_data[i]).isLiquid();
924 // there was a change between liquid and nonliquid, add to queue.
925 if (isliquid != wasliquid)
926 trans_liquid->push_back(v3s16(x, y, z));
928 wasliquid = isliquid;
929 vm->m_area.add_y(em, i, -1);
936 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
937 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
938 VoxelArea a(nmin, nmax);
940 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
941 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
942 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
943 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
944 vm->m_data[i].param1 = light;
950 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
951 if (light <= 1 || !a.contains(p))
954 u32 vi = vm->m_area.index(p);
955 MapNode &nn = vm->m_data[vi];
958 // should probably compare masked, but doesn't seem to make a difference
959 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
964 lightSpread(a, p + v3s16(0, 0, 1), light);
965 lightSpread(a, p + v3s16(0, 1, 0), light);
966 lightSpread(a, p + v3s16(1, 0, 0), light);
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);
973 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
974 VoxelArea a(nmin, nmax);
975 bool block_is_underground = (water_level >= nmax.Y);
977 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
978 //TimeTaker t("updateLighting");
980 // first, send vertical rays of sunshine downward
981 v3s16 em = vm->m_area.getExtent();
982 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
983 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
984 // see if we can get a light value from the overtop
985 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
986 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
987 if (block_is_underground)
989 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
992 vm->m_area.add_y(em, i, -1);
994 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
995 MapNode &n = vm->m_data[i];
996 if (!ndef->get(n).sunlight_propagates)
998 n.param1 = LIGHT_SUN;
999 vm->m_area.add_y(em, i, -1);
1004 // now spread the sunlight and light up any sources
1005 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1006 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
1007 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
1008 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
1009 MapNode &n = vm->m_data[i];
1010 if (n.getContent() == CONTENT_IGNORE ||
1011 !ndef->get(n).light_propagates)
1014 u8 light_produced = ndef->get(n).light_source & 0x0F;
1016 n.param1 = light_produced;
1018 u8 light = n.param1 & 0x0F;
1020 lightSpread(a, v3s16(x, y, z + 1), light - 1);
1021 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
1022 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
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);
1031 //printf("updateLighting: %dms\n", t.stop());
1035 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
1036 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1037 VoxelArea a(nmin, nmax);
1038 bool block_is_underground = (water_level > nmax.Y);
1039 bool sunlight = !block_is_underground;
1041 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1043 for (int i = 0; i < 2; i++) {
1044 enum LightBank bank = banks[i];
1045 std::set<v3s16> light_sources;
1046 std::map<v3s16, u8> unlight_from;
1048 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
1049 light_sources, unlight_from);
1050 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
1052 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
1053 vm->spreadLight(bank, light_sources, ndef);