Decoration: Fix schematic probability mess with new MTS file version
[oweals/minetest.git] / src / mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "mapgen.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
32 #include "profiler.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
35 #include "treegen.h"
36 #include "mapgen_v6.h"
37 #include "mapgen_v7.h"
38 #include "util/serialize.h"
39
40 FlagDesc flagdesc_mapgen[] = {
41         {"trees",          MG_TREES},
42         {"caves",          MG_CAVES},
43         {"dungeons",       MG_DUNGEONS},
44         {"v6_jungles",     MGV6_JUNGLES},
45         {"v6_biome_blend", MGV6_BIOME_BLEND},
46         {"flat",           MG_FLAT},
47         {"nolight",        MG_NOLIGHT},
48         {NULL,             0}
49 };
50
51 FlagDesc flagdesc_ore[] = {
52         {"absheight",            OREFLAG_ABSHEIGHT},
53         {"scatter_noisedensity", OREFLAG_DENSITY},
54         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
55         {NULL,                   0}
56 };
57
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},
62         {NULL,             0}
63 };
64
65 ///////////////////////////////////////////////////////////////////////////////
66
67
68 Ore *createOre(OreType type) {
69         switch (type) {
70                 case ORE_SCATTER:
71                         return new OreScatter;
72                 case ORE_SHEET:
73                         return new OreSheet;
74                 //case ORE_CLAYLIKE: //TODO: implement this!
75                 //      return new OreClaylike;
76                 default:
77                         return NULL;
78         }
79 }
80
81
82 Ore::~Ore() {
83         delete np;
84         delete noise;
85 }
86
87
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";
94                         ore = CONTENT_AIR;
95                         wherein.push_back(CONTENT_AIR);
96                         return;
97                 }
98         }
99
100         for (size_t i=0; i != wherein_names.size(); i++) {
101                 std::string name = wherein_names[i];
102                 content_t c = ndef->getId(name);
103                 if (c != CONTENT_IGNORE) {
104                         wherein.push_back(c);
105                 }
106         }
107 }
108
109
110 void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
111         int in_range = 0;
112
113         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
114         if (flags & OREFLAG_ABSHEIGHT)
115                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
116         if (!in_range)
117                 return;
118
119         int ymin, ymax;
120         if (in_range & ORE_RANGE_MIRROR) {
121                 ymin = MYMAX(nmin.Y, -height_max);
122                 ymax = MYMIN(nmax.Y, -height_min);
123         } else {
124                 ymin = MYMAX(nmin.Y, height_min);
125                 ymax = MYMIN(nmax.Y, height_max);
126         }
127         if (clust_size >= ymax - ymin + 1)
128                 return;
129
130         nmin.Y = ymin;
131         nmax.Y = ymax;
132         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
133 }
134
135
136 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
137                                                   u32 blockseed, v3s16 nmin, v3s16 nmax) {
138         PseudoRandom pr(blockseed);
139         MapNode n_ore(ore, 0, ore_param2);
140
141         int volume = (nmax.X - nmin.X + 1) *
142                                  (nmax.Y - nmin.Y + 1) *
143                                  (nmax.Z - nmin.Z + 1);
144         int csize     = clust_size;
145         int orechance = (csize * csize * csize) / clust_num_ores;
146         int nclusters = volume / clust_scarcity;
147
148         for (int i = 0; i != nclusters; i++) {
149                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
150                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
151                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
152  
153                 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
154                         continue;
155
156                 for (int z1 = 0; z1 != csize; z1++)
157                 for (int y1 = 0; y1 != csize; y1++)
158                 for (int x1 = 0; x1 != csize; x1++) {
159                         if (pr.range(1, orechance) != 1)
160                                 continue;
161
162                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
163                         for (size_t ii = 0; ii < wherein.size(); ii++)
164                                 if (vm->m_data[i].getContent() == wherein[ii])
165                                         vm->m_data[i] = n_ore;
166                 }
167         }
168 }
169
170
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);
175
176         int max_height = clust_size;
177         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
178
179         if (!noise) {
180                 int sx = nmax.X - nmin.X + 1;
181                 int sz = nmax.Z - nmin.Z + 1;
182                 noise = new Noise(np, 0, sx, sz);
183         }
184         noise->seed = seed + y_start;
185         noise->perlinMap2D(nmin.X, nmin.Z);
186
187         int index = 0;
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)
192                         continue;
193
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))
200                                 continue;
201
202                         for (size_t ii = 0; ii < wherein.size(); ii++)
203                                 if (vm->m_data[i].getContent() == wherein[ii])
204                                         vm->m_data[i] = n_ore;
205                 }
206         }
207 }
208
209
210 ///////////////////////////////////////////////////////////////////////////////
211
212
213 Decoration *createDecoration(DecorationType type) {
214         switch (type) {
215                 case DECO_SIMPLE:
216                         return new DecoSimple;
217                 case DECO_SCHEMATIC:
218                         return new DecoSchematic;
219                 //case DECO_LSYSTEM:
220                 //      return new DecoLSystem;
221                 default:
222                         return NULL;
223         }
224 }
225
226
227 Decoration::Decoration() {
228         mapseed    = 0;
229         np         = NULL;
230         fill_ratio = 0;
231         sidelen    = 1;
232 }
233
234
235 Decoration::~Decoration() {
236         delete np;
237 }
238
239
240 void Decoration::resolveNodeNames(INodeDefManager *ndef) {
241         this->ndef = ndef;
242         
243         if (c_place_on == CONTENT_IGNORE)
244                 c_place_on = ndef->getId(place_on_name);
245 }
246
247
248 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
249         PseudoRandom ps(blockseed + 53);
250         int carea_size = nmax.X - nmin.X + 1;
251
252         // Divide area into parts
253         if (carea_size % sidelen) {
254                 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
255                         "sidelen; setting sidelen to " << carea_size << std::endl;
256                 sidelen = carea_size;
257         }
258         
259         s16 divlen = carea_size / sidelen;
260         int area = sidelen * sidelen;
261
262         for (s16 z0 = 0; z0 < divlen; z0++)
263         for (s16 x0 = 0; x0 < divlen; x0++) {
264                 v2s16 p2d_center( // Center position of part of division
265                         nmin.X + sidelen / 2 + sidelen * x0,
266                         nmin.Z + sidelen / 2 + sidelen * z0
267                 );
268                 v2s16 p2d_min( // Minimum edge of part of division
269                         nmin.X + sidelen * x0,
270                         nmin.Z + sidelen * z0
271                 );
272                 v2s16 p2d_max( // Maximum edge of part of division
273                         nmin.X + sidelen + sidelen * x0 - 1,
274                         nmin.Z + sidelen + sidelen * z0 - 1
275                 );
276
277                 // Amount of decorations
278                 float nval = np ?
279                         NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
280                         fill_ratio;
281                 u32 deco_count = area * MYMAX(nval, 0.f);
282
283                 for (u32 i = 0; i < deco_count; i++) {
284                         s16 x = ps.range(p2d_min.X, p2d_max.X);
285                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
286
287                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
288                         
289                         s16 y = mg->heightmap ? 
290                                         mg->heightmap[mapindex] :
291                                         mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
292                                         
293                         if (y < nmin.Y || y > nmax.Y)
294                                 continue;
295
296                         int height = getHeight();
297                         int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
298                         if (y + 1 + height > max_y) {
299                                 continue;
300 #if 0
301                                 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
302                                 //add to queue
303                                 JMutexAutoLock cutofflock(cutoff_mutex);
304                                 cutoffs.push_back(CutoffData(x, y, z, height));
305 #endif
306                         }
307
308                         if (mg->biomemap) {
309                                 std::set<u8>::iterator iter;
310                                 
311                                 if (biomes.size()) {
312                                         iter = biomes.find(mg->biomemap[mapindex]);
313                                         if (iter == biomes.end())
314                                                 continue;
315                                 }
316                         }
317
318                         generate(mg, &ps, max_y, v3s16(x, y, z));
319                 }
320         }
321 }
322
323
324 #if 0
325 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
326         PseudoRandom pr(blockseed + 53);
327         std::vector<CutoffData> handled_cutoffs;
328         
329         // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
330         {
331                 JMutexAutoLock cutofflock(cutoff_mutex);
332                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
333                         i != cutoffs.end(); ++i) {
334                         CutoffData cutoff = *i;
335                         v3s16 p    = cutoff.p;
336                         s16 height = cutoff.height;
337                         if (p.X < nmin.X || p.X > nmax.X ||
338                                 p.Z < nmin.Z || p.Z > nmax.Z)
339                                 continue;
340                         if (p.Y + height < nmin.Y || p.Y > nmax.Y)
341                                 continue;
342                         
343                         handled_cutoffs.push_back(cutoff);
344                 }
345         }
346         
347         // Generate the cutoffs
348         for (size_t i = 0; i != handled_cutoffs.size(); i++) {
349                 v3s16 p    = handled_cutoffs[i].p;
350                 s16 height = handled_cutoffs[i].height;
351                 
352                 if (p.Y + height > nmax.Y) {
353                         //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
354                         cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
355                 }
356                 
357                 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
358         }
359         
360         // Remove cutoffs that were handled from the cutoff list
361         {
362                 JMutexAutoLock cutofflock(cutoff_mutex);
363                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
364                         i != cutoffs.end(); ++i) {
365                         
366                         for (size_t j = 0; j != handled_cutoffs.size(); j++) {
367                                 CutoffData coff = *i;
368                                 if (coff.p == handled_cutoffs[j].p)
369                                         i = cutoffs.erase(i);
370                         }
371                 }
372         }       
373 }
374 #endif
375
376
377 ///////////////////////////////////////////////////////////////////////////////
378
379
380 void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
381         Decoration::resolveNodeNames(ndef);
382         
383         if (c_deco == CONTENT_IGNORE) {
384                 c_deco = ndef->getId(deco_name);
385                 if (c_deco == CONTENT_IGNORE) {
386                         errorstream << "DecoSimple::resolveNodeNames: decoration node '"
387                                 << deco_name << "' not defined" << std::endl;
388                         c_deco = CONTENT_AIR;
389                 }
390         }
391         if (c_spawnby == CONTENT_IGNORE) {
392                 c_spawnby = ndef->getId(spawnby_name);
393                 if (c_spawnby == CONTENT_IGNORE) {
394                         errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
395                                 << deco_name << "' not defined" << std::endl;
396                         nspawnby = -1;
397                         c_spawnby = CONTENT_AIR;
398                 }
399         }
400         
401         if (c_decolist.size())
402                 return;
403         
404         for (size_t i = 0; i != decolist_names.size(); i++) {           
405                 content_t c = ndef->getId(decolist_names[i]);
406                 if (c == CONTENT_IGNORE) {
407                         errorstream << "DecoSimple::resolveNodeNames: decolist node '"
408                                 << decolist_names[i] << "' not defined" << std::endl;
409                         c = CONTENT_AIR;
410                 }
411                 c_decolist.push_back(c);
412         }
413 }
414
415
416 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
417         ManualMapVoxelManipulator *vm = mg->vm;
418
419         u32 vi = vm->m_area.index(p);
420         if (vm->m_data[vi].getContent() != c_place_on &&
421                 c_place_on != CONTENT_IGNORE)
422                 return;
423                 
424         if (nspawnby != -1) {
425                 int nneighs = 0;
426                 v3s16 dirs[8] = { // a Moore neighborhood
427                         v3s16( 0, 0,  1),
428                         v3s16( 0, 0, -1),
429                         v3s16( 1, 0,  0),
430                         v3s16(-1, 0,  0),
431                         v3s16( 1, 0,  1),
432                         v3s16(-1, 0,  1),
433                         v3s16(-1, 0, -1),
434                         v3s16( 1, 0, -1)
435                 };
436                 
437                 for (int i = 0; i != 8; i++) {
438                         u32 index = vm->m_area.index(p + dirs[i]);
439                         if (vm->m_area.contains(index) &&
440                                 vm->m_data[index].getContent() == c_spawnby)
441                                 nneighs++;
442                 }
443                 
444                 if (nneighs < nspawnby)
445                         return;
446         }
447         
448         size_t ndecos = c_decolist.size();
449         content_t c_place = ndecos ? c_decolist[pr->range(0, ndecos - 1)] : c_deco;
450
451         s16 height = (deco_height_max > 0) ?
452                 pr->range(deco_height, deco_height_max) : deco_height;
453
454         height = MYMIN(height, max_y - p.Y);
455         
456         v3s16 em = vm->m_area.getExtent();
457         for (int i = 0; i < height; i++) {
458                 vm->m_area.add_y(em, vi, 1);
459                 
460                 content_t c = vm->m_data[vi].getContent();
461                 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
462                         break;
463                 
464                 vm->m_data[vi] = MapNode(c_place);
465         }
466 }
467
468
469 int DecoSimple::getHeight() {
470         return (deco_height_max > 0) ? deco_height_max : deco_height;
471 }
472
473
474 std::string DecoSimple::getName() {
475         return deco_name;
476 }
477
478
479 ///////////////////////////////////////////////////////////////////////////////
480
481
482 DecoSchematic::DecoSchematic() {
483         node_names = NULL;
484         schematic  = NULL;
485         flags      = 0;
486         size       = v3s16(0, 0, 0);
487 }
488
489
490 DecoSchematic::~DecoSchematic() {
491         delete node_names;
492         delete []schematic;
493 }
494
495
496 void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
497         Decoration::resolveNodeNames(ndef);
498         
499         if (filename.empty())
500                 return;
501         
502         if (!node_names) {
503                 errorstream << "DecoSchematic::resolveNodeNames: node name list was "
504                         "not created" << std::endl;
505                 return;
506         }
507         
508         for (size_t i = 0; i != node_names->size(); i++) {
509                 std::string name = node_names->at(i);
510                 
511                 std::map<std::string, std::string>::iterator it;
512                 it = replacements.find(name);
513                 if (it != replacements.end())
514                         name = it->second;
515                 
516                 content_t c = ndef->getId(name);
517                 if (c == CONTENT_IGNORE) {
518                         errorstream << "DecoSchematic::resolveNodeNames: node '"
519                                 << name << "' not defined" << std::endl;
520                         c = CONTENT_AIR;
521                 }
522                 
523                 c_nodes.push_back(c);
524         }
525                 
526         for (int i = 0; i != size.X * size.Y * size.Z; i++)
527                 schematic[i].setContent(c_nodes[schematic[i].getContent()]);
528         
529         delete node_names;
530         node_names = NULL;
531 }
532
533
534 void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
535         ManualMapVoxelManipulator *vm = mg->vm;
536
537         if (flags & DECO_PLACE_CENTER_X)
538                 p.X -= (size.X + 1) / 2;
539         if (flags & DECO_PLACE_CENTER_Y)
540                 p.Y -= (size.Y + 1) / 2;
541         if (flags & DECO_PLACE_CENTER_Z)
542                 p.Z -= (size.Z + 1) / 2;
543                 
544         u32 vi = vm->m_area.index(p);
545         if (vm->m_data[vi].getContent() != c_place_on &&
546                 c_place_on != CONTENT_IGNORE)
547                 return;
548         
549         Rotation rot = (rotation == ROTATE_RAND) ?
550                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
551         
552         blitToVManip(p, vm, rot, false);
553 }
554
555
556 int DecoSchematic::getHeight() {
557         return size.Y;
558 }
559
560
561 std::string DecoSchematic::getName() {
562         return filename;
563 }
564
565
566 void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
567                                                                 Rotation rot, bool force_placement) {
568         int xstride = 1;
569         int ystride = size.X;
570         int zstride = size.X * size.Y;
571
572         s16 sx = size.X;
573         s16 sy = size.Y;
574         s16 sz = size.Z;
575
576         int i_start, i_step_x, i_step_z;
577         switch (rot) {
578                 case ROTATE_90:
579                         i_start  = sx - 1;
580                         i_step_x = zstride;
581                         i_step_z = -xstride;
582                         SWAP(s16, sx, sz);
583                         break;
584                 case ROTATE_180:
585                         i_start  = zstride * (sz - 1) + sx - 1;
586                         i_step_x = -xstride;
587                         i_step_z = -zstride;
588                         break;
589                 case ROTATE_270:
590                         i_start  = zstride * (sz - 1);
591                         i_step_x = -zstride;
592                         i_step_z = xstride;
593                         SWAP(s16, sx, sz);
594                         break;
595                 default:
596                         i_start  = 0;
597                         i_step_x = xstride;
598                         i_step_z = zstride;
599         }
600         
601         for (s16 z = 0; z != sz; z++)
602         for (s16 y = 0; y != sy; y++) {
603                 u32 i = z * i_step_z + y * ystride + i_start;
604                 for (s16 x = 0; x != sx; x++, i += i_step_x) {
605                         u32 vi = vm->m_area.index(p.X + x, p.Y + y, p.Z + z);
606                         if (!vm->m_area.contains(vi))
607                                 continue;
608                         
609                         if (schematic[i].getContent() == CONTENT_IGNORE)
610                                 continue;
611                                 
612                         if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
613                                 continue;
614
615                         if (!force_placement) {
616                                 content_t c = vm->m_data[vi].getContent();
617                                 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
618                                         continue;
619                         }
620                         
621                         if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
622                                 myrand_range(1, 255) > schematic[i].param1)
623                                 continue;
624                         
625                         vm->m_data[vi] = schematic[i];
626                         vm->m_data[vi].param1 = 0;
627                         
628                         if (rot)
629                                 vm->m_data[vi].rotateAlongYAxis(ndef, rot);
630                 }
631         }
632 }
633
634
635 void DecoSchematic::placeStructure(Map *map, v3s16 p) {
636         assert(schematic != NULL);
637         ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
638
639         Rotation rot = (rotation == ROTATE_RAND) ?
640                 (Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
641         
642         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
643                                 v3s16(size.Z, size.Y, size.X) : size;
644
645         if (flags & DECO_PLACE_CENTER_X)
646                 p.X -= (s.X + 1) / 2;
647         if (flags & DECO_PLACE_CENTER_Y)
648                 p.Y -= (s.Y + 1) / 2;
649         if (flags & DECO_PLACE_CENTER_Z)
650                 p.Z -= (s.Z + 1) / 2;
651                 
652         v3s16 bp1 = getNodeBlockPos(p);
653         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
654         vm->initialEmerge(bp1, bp2);
655         
656         blitToVManip(p, vm, rot, true);
657         
658         std::map<v3s16, MapBlock *> lighting_modified_blocks;
659         std::map<v3s16, MapBlock *> modified_blocks;
660         vm->blitBackAll(&modified_blocks);
661         
662         // TODO: Optimize this by using Mapgen::calcLighting() instead
663         lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
664         map->updateLighting(lighting_modified_blocks, modified_blocks);
665
666         MapEditEvent event;
667         event.type = MEET_OTHER;
668         for (std::map<v3s16, MapBlock *>::iterator
669                 it = modified_blocks.begin();
670                 it != modified_blocks.end(); ++it)
671                 event.modified_blocks.insert(it->first);
672                 
673         map->dispatchEvent(&event);
674 }
675
676
677 bool DecoSchematic::loadSchematicFile() {
678         content_t cignore = CONTENT_IGNORE;
679         bool have_cignore = false;
680         
681         std::ifstream is(filename.c_str(), std::ios_base::binary);
682
683         u32 signature = readU32(is);
684         if (signature != MTSCHEM_FILE_SIGNATURE) {
685                 errorstream << "loadSchematicFile: invalid schematic "
686                         "file" << std::endl;
687                 return false;
688         }
689         
690         u16 version = readU16(is);
691         if (version > 2) {
692                 errorstream << "loadSchematicFile: unsupported schematic "
693                         "file version" << std::endl;
694                 return false;
695         }
696
697         size = readV3S16(is);
698         int nodecount = size.X * size.Y * size.Z;
699         
700         u16 nidmapcount = readU16(is);
701         
702         node_names = new std::vector<std::string>;
703         for (int i = 0; i != nidmapcount; i++) {
704                 std::string name = deSerializeString(is);
705                 if (name == "ignore") {
706                         name = "air";
707                         cignore = i;
708                         have_cignore = true;
709                 }
710                 node_names->push_back(name);
711         }
712
713         delete schematic;
714         schematic = new MapNode[nodecount];
715         MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
716                                 nodecount, 2, 2, true);
717         
718         if (version == 1) { // fix up the probability values
719                 for (int i = 0; i != nodecount; i++) {
720                         if (schematic[i].param1 == 0)
721                                 schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
722                         if (have_cignore && schematic[i].getContent() == cignore)
723                                 schematic[i].param1 = MTSCHEM_PROB_NEVER;
724                 }
725         }
726         
727         return true;
728 }
729
730
731 /*
732         Minetest Schematic File Format
733
734         All values are stored in big-endian byte order.
735         [u32] signature: 'MTSM'
736         [u16] version: 2
737         [u16] size X
738         [u16] size Y
739         [u16] size Z
740         [Name-ID table] Name ID Mapping Table
741                 [u16] name-id count
742                 For each name-id mapping:
743                         [u16] name length
744                         [u8[]] name
745         ZLib deflated {
746         For each node in schematic:  (for z, y, x)
747                 [u16] content
748         For each node in schematic:
749                 [u8] probability of occurance (param1)
750         For each node in schematic:
751                 [u8] param2
752         }
753         
754         Version changes:
755         1 - Initial version
756         2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
757 */
758 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
759         std::ofstream os(filename.c_str(), std::ios_base::binary);
760
761         writeU32(os, MTSCHEM_FILE_SIGNATURE); // signature
762         writeU16(os, 2);      // version
763         writeV3S16(os, size); // schematic size
764         
765         std::vector<content_t> usednodes;
766         int nodecount = size.X * size.Y * size.Z;
767         build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
768         
769         u16 numids = usednodes.size();
770         writeU16(os, numids); // name count
771         for (int i = 0; i != numids; i++)
772                 os << serializeString(ndef->get(usednodes[i]).name); // node names
773                 
774         // compressed bulk node data
775         MapNode::serializeBulk(os, SER_FMT_VER_HIGHEST_WRITE, schematic,
776                                 nodecount, 2, 2, true);
777 }
778
779
780 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
781                                                 std::vector<content_t> *usednodes) {
782         std::map<content_t, content_t> nodeidmap;
783         content_t numids = 0;
784         
785         for (u32 i = 0; i != nodecount; i++) {
786                 content_t id;
787                 content_t c = nodes[i].getContent();
788
789                 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
790                 if (it == nodeidmap.end()) {
791                         id = numids;
792                         numids++;
793
794                         usednodes->push_back(c);
795                         nodeidmap.insert(std::make_pair(c, id));
796                 } else {
797                         id = it->second;
798                 }
799                 nodes[i].setContent(id);
800         }
801 }
802
803
804 bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
805         ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
806
807         v3s16 bp1 = getNodeBlockPos(p1);
808         v3s16 bp2 = getNodeBlockPos(p2);
809         vm->initialEmerge(bp1, bp2);
810         
811         size = p2 - p1 + 1;
812         schematic = new MapNode[size.X * size.Y * size.Z];
813         
814         u32 i = 0;
815         for (s16 z = p1.Z; z <= p2.Z; z++)
816         for (s16 y = p1.Y; y <= p2.Y; y++) {
817                 u32 vi = vm->m_area.index(p1.X, y, z);
818                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
819                         schematic[i] = vm->m_data[vi];
820                         schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
821                 }
822         }
823
824         delete vm;
825         return true;
826 }
827
828
829 void DecoSchematic::applyProbabilities(std::vector<std::pair<v3s16, u8> > *plist,
830                                                                         v3s16 p0) {
831         for (size_t i = 0; i != plist->size(); i++) {
832                 v3s16 p = (*plist)[i].first - p0;
833                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
834                 if (index < size.Z * size.Y * size.X) {
835                         u8 prob = (*plist)[i].second;
836                         schematic[index].param1 = prob;
837                         
838                         // trim unnecessary node names from schematic
839                         if (prob == MTSCHEM_PROB_NEVER)
840                                 schematic[index].setContent(CONTENT_AIR);
841                 }
842         }
843 }
844
845
846 ///////////////////////////////////////////////////////////////////////////////
847
848
849 Mapgen::Mapgen() {
850         seed        = 0;
851         water_level = 0;
852         generating  = false;
853         id          = -1;
854         vm          = NULL;
855         ndef        = NULL;
856         heightmap   = NULL;
857         biomemap    = NULL;
858 }
859
860
861 // Returns Y one under area minimum if not found
862 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
863         v3s16 em = vm->m_area.getExtent();
864         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
865         s16 y_nodes_min = vm->m_area.MinEdge.Y;
866         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
867         s16 y;
868         
869         for (y = y_nodes_max; y >= y_nodes_min; y--) {
870                 MapNode &n = vm->m_data[i];
871                 if (ndef->get(n).walkable)
872                         break;
873
874                 vm->m_area.add_y(em, i, -1);
875         }
876         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
877 }
878
879
880 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
881         v3s16 em = vm->m_area.getExtent();
882         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
883         s16 y;
884         
885         for (y = ymax; y >= ymin; y--) {
886                 MapNode &n = vm->m_data[i];
887                 if (ndef->get(n).walkable)
888                         break;
889
890                 vm->m_area.add_y(em, i, -1);
891         }
892         return y;
893 }
894
895
896 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
897         if (!heightmap)
898                 return;
899         
900         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
901         int index = 0;
902         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
903                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
904                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
905
906                         // if the values found are out of range, trust the old heightmap
907                         if (y == nmax.Y && heightmap[index] > nmax.Y)
908                                 continue;
909                         if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
910                                 continue;
911                                 
912                         heightmap[index] = y;
913                 }
914         }
915         //printf("updateHeightmap: %dus\n", t.stop());
916 }
917
918
919 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
920         bool isliquid, wasliquid, rare;
921         v3s16 em  = vm->m_area.getExtent();
922         rare = g_settings->getBool("liquid_finite");
923         int rarecnt = 0;
924
925         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
926                 for (s16 x = nmin.X; x <= nmax.X; x++) {
927                         wasliquid = true;
928
929                         u32 i = vm->m_area.index(x, nmax.Y, z);
930                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
931                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
932
933                                 // there was a change between liquid and nonliquid, add to queue. no need to add every with liquid_finite
934                                 if (isliquid != wasliquid && (!rare || !(rarecnt++ % 36)))
935                                         trans_liquid->push_back(v3s16(x, y, z));
936
937                                 wasliquid = isliquid;
938                                 vm->m_area.add_y(em, i, -1);
939                         }
940                 }
941         }
942 }
943
944
945 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
946         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
947         VoxelArea a(nmin, nmax);
948
949         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
950                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
951                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
952                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
953                                 vm->m_data[i].param1 = light;
954                 }
955         }
956 }
957
958
959 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
960         if (light <= 1 || !a.contains(p))
961                 return;
962
963         u32 vi = vm->m_area.index(p);
964         MapNode &nn = vm->m_data[vi];
965
966         light--;
967         // should probably compare masked, but doesn't seem to make a difference
968         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
969                 return;
970
971         nn.param1 = light;
972
973         lightSpread(a, p + v3s16(0, 0, 1), light);
974         lightSpread(a, p + v3s16(0, 1, 0), light);
975         lightSpread(a, p + v3s16(1, 0, 0), light);
976         lightSpread(a, p - v3s16(0, 0, 1), light);
977         lightSpread(a, p - v3s16(0, 1, 0), light);
978         lightSpread(a, p - v3s16(1, 0, 0), light);
979 }
980
981
982 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
983         VoxelArea a(nmin, nmax);
984         bool block_is_underground = (water_level >= nmax.Y);
985
986         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
987         //TimeTaker t("updateLighting");
988
989         // first, send vertical rays of sunshine downward
990         v3s16 em = vm->m_area.getExtent();
991         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
992                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
993                         // see if we can get a light value from the overtop
994                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
995                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
996                                 if (block_is_underground)
997                                         continue;
998                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
999                                 continue;
1000                         }
1001                         vm->m_area.add_y(em, i, -1);
1002  
1003                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
1004                                 MapNode &n = vm->m_data[i];
1005                                 if (!ndef->get(n).sunlight_propagates)
1006                                         break;
1007                                 n.param1 = LIGHT_SUN;
1008                                 vm->m_area.add_y(em, i, -1);
1009                         }
1010                 }
1011         }
1012
1013         // now spread the sunlight and light up any sources
1014         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
1015                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
1016                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
1017                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
1018                                 MapNode &n = vm->m_data[i];
1019                                 if (n.getContent() == CONTENT_IGNORE ||
1020                                         !ndef->get(n).light_propagates)
1021                                         continue;
1022
1023                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
1024                                 if (light_produced)
1025                                         n.param1 = light_produced;
1026
1027                                 u8 light = n.param1 & 0x0F;
1028                                 if (light) {
1029                                         lightSpread(a, v3s16(x,     y,     z + 1), light - 1);
1030                                         lightSpread(a, v3s16(x,     y + 1, z    ), light - 1);
1031                                         lightSpread(a, v3s16(x + 1, y,     z    ), light - 1);
1032                                         lightSpread(a, v3s16(x,     y,     z - 1), light - 1);
1033                                         lightSpread(a, v3s16(x,     y - 1, z    ), light - 1);
1034                                         lightSpread(a, v3s16(x - 1, y,     z    ), light - 1);
1035                                 }
1036                         }
1037                 }
1038         }
1039
1040         //printf("updateLighting: %dms\n", t.stop());
1041 }
1042
1043
1044 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
1045         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1046         VoxelArea a(nmin, nmax);
1047         bool block_is_underground = (water_level > nmax.Y);
1048         bool sunlight = !block_is_underground;
1049
1050         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
1051
1052         for (int i = 0; i < 2; i++) {
1053                 enum LightBank bank = banks[i];
1054                 std::set<v3s16> light_sources;
1055                 std::map<v3s16, u8> unlight_from;
1056
1057                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
1058                                                                                          light_sources, unlight_from);
1059                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
1060
1061                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
1062                 vm->spreadLight(bank, light_sources, ndef);
1063         }
1064 }
1065  
1066  
1067 //////////////////////// Mapgen V6 parameter read/write
1068  
1069 bool MapgenV6Params::readParams(Settings *settings) {
1070         freq_desert = settings->getFloat("mgv6_freq_desert");
1071         freq_beach  = settings->getFloat("mgv6_freq_beach");
1072
1073         bool success = 
1074                 settings->getNoiseParams("mgv6_np_terrain_base",   np_terrain_base)   &&
1075                 settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher) &&
1076                 settings->getNoiseParams("mgv6_np_steepness",      np_steepness)      &&
1077                 settings->getNoiseParams("mgv6_np_height_select",  np_height_select)  &&
1078                 settings->getNoiseParams("mgv6_np_mud",            np_mud)            &&
1079                 settings->getNoiseParams("mgv6_np_beach",          np_beach)          &&
1080                 settings->getNoiseParams("mgv6_np_biome",          np_biome)          &&
1081                 settings->getNoiseParams("mgv6_np_cave",           np_cave)           &&
1082                 settings->getNoiseParams("mgv6_np_humidity",       np_humidity)       &&
1083                 settings->getNoiseParams("mgv6_np_trees",          np_trees)          &&
1084                 settings->getNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
1085         return success;
1086 }
1087  
1088  
1089 void MapgenV6Params::writeParams(Settings *settings) {
1090         settings->setFloat("mgv6_freq_desert", freq_desert);
1091         settings->setFloat("mgv6_freq_beach",  freq_beach);
1092  
1093         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
1094         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
1095         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
1096         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
1097         settings->setNoiseParams("mgv6_np_mud",            np_mud);
1098         settings->setNoiseParams("mgv6_np_beach",          np_beach);
1099         settings->setNoiseParams("mgv6_np_biome",          np_biome);
1100         settings->setNoiseParams("mgv6_np_cave",           np_cave);
1101         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
1102         settings->setNoiseParams("mgv6_np_trees",          np_trees);
1103         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
1104 }
1105
1106
1107 bool MapgenV7Params::readParams(Settings *settings) {
1108         bool success = 
1109                 settings->getNoiseParams("mgv7_np_terrain_base",    np_terrain_base)    &&
1110                 settings->getNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt)     &&
1111                 settings->getNoiseParams("mgv7_np_terrain_persist", np_terrain_persist) &&
1112                 settings->getNoiseParams("mgv7_np_height_select",   np_height_select)   &&
1113                 settings->getNoiseParams("mgv7_np_filler_depth",    np_filler_depth)    &&
1114                 settings->getNoiseParams("mgv7_np_mount_height",    np_mount_height)    &&
1115                 settings->getNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater)    &&
1116                 settings->getNoiseParams("mgv7_np_mountain",        np_mountain)        &&
1117                 settings->getNoiseParams("mgv7_np_ridge",           np_ridge);
1118         return success;
1119 }
1120
1121
1122 void MapgenV7Params::writeParams(Settings *settings) {
1123         settings->setNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
1124         settings->setNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
1125         settings->setNoiseParams("mgv7_np_terrain_persist", np_terrain_persist);
1126         settings->setNoiseParams("mgv7_np_height_select",   np_height_select);
1127         settings->setNoiseParams("mgv7_np_filler_depth",    np_filler_depth);
1128         settings->setNoiseParams("mgv7_np_mount_height",    np_mount_height);
1129         settings->setNoiseParams("mgv7_np_ridge_uwater",    np_ridge_uwater);
1130         settings->setNoiseParams("mgv7_np_mountain",        np_mountain);
1131         settings->setNoiseParams("mgv7_np_ridge",           np_ridge);
1132 }
1133
1134
1135 /////////////////////////////////// legacy static functions for farmesh
1136
1137 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
1138         //just need to return something
1139         s16 level = 5;
1140         return level;
1141 }
1142
1143
1144 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
1145         double sandnoise = noise2d_perlin(
1146                                 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
1147                                 seed+59420, 3, 0.50);
1148  
1149         return (sandnoise > 0.15);
1150 }
1151
1152
1153 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
1154         double noise = noise2d_perlin(
1155                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
1156                         seed+2, 4, 0.66);
1157         double zeroval = -0.39;
1158         if(noise < zeroval)
1159                 return 0;
1160         else
1161                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
1162 }