Give the Mapgen on each EmergeThread its own Biome/Ore/Deco/SchemManager copy
[oweals/minetest.git] / src / mapgen / mg_schematic.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 paramat
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <fstream>
22 #include <typeinfo>
23 #include "mg_schematic.h"
24 #include "server.h"
25 #include "mapgen.h"
26 #include "emerge.h"
27 #include "map.h"
28 #include "mapblock.h"
29 #include "log.h"
30 #include "util/numeric.h"
31 #include "util/serialize.h"
32 #include "serialization.h"
33 #include "filesys.h"
34 #include "voxelalgorithms.h"
35
36 ///////////////////////////////////////////////////////////////////////////////
37
38
39 SchematicManager::SchematicManager(Server *server) :
40         ObjDefManager(server, OBJDEF_SCHEMATIC),
41         m_server(server)
42 {
43 }
44
45
46 SchematicManager *SchematicManager::clone() const
47 {
48         auto mgr = new SchematicManager();
49         assert(mgr);
50         ObjDefManager::cloneTo(mgr);
51         return mgr;
52 }
53
54
55 void SchematicManager::clear()
56 {
57         EmergeManager *emerge = m_server->getEmergeManager();
58
59         // Remove all dangling references in Decorations
60         DecorationManager *decomgr = emerge->getWritableDecorationManager();
61         for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
62                 Decoration *deco = (Decoration *)decomgr->getRaw(i);
63
64                 try {
65                         DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
66                         if (dschem)
67                                 dschem->schematic = NULL;
68                 } catch (const std::bad_cast &) {
69                 }
70         }
71
72         ObjDefManager::clear();
73 }
74
75
76 ///////////////////////////////////////////////////////////////////////////////
77
78
79 Schematic::Schematic()
80 = default;
81
82
83 Schematic::~Schematic()
84 {
85         delete []schemdata;
86         delete []slice_probs;
87 }
88
89 ObjDef *Schematic::clone() const
90 {
91         auto def = new Schematic();
92         ObjDef::cloneTo(def);
93         NodeResolver::cloneTo(def);
94
95         def->c_nodes = c_nodes;
96         def->flags = flags;
97         def->size = size;
98         FATAL_ERROR_IF(!schemdata, "Schematic can only be cloned after loading");
99         u32 nodecount = size.X * size.Y * size.Z;
100         def->schemdata = new MapNode[nodecount];
101         memcpy(def->schemdata, schemdata, sizeof(MapNode) * nodecount);
102         def->slice_probs = new u8[size.Y];
103         memcpy(def->slice_probs, slice_probs, sizeof(u8) * size.Y);
104
105         return def;
106 }
107
108
109 void Schematic::resolveNodeNames()
110 {
111         getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
112
113         size_t bufsize = size.X * size.Y * size.Z;
114         for (size_t i = 0; i != bufsize; i++) {
115                 content_t c_original = schemdata[i].getContent();
116                 content_t c_new = c_nodes[c_original];
117                 schemdata[i].setContent(c_new);
118         }
119 }
120
121
122 void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
123 {
124         assert(schemdata && slice_probs);
125         sanity_check(m_ndef != NULL);
126
127         int xstride = 1;
128         int ystride = size.X;
129         int zstride = size.X * size.Y;
130
131         s16 sx = size.X;
132         s16 sy = size.Y;
133         s16 sz = size.Z;
134
135         int i_start, i_step_x, i_step_z;
136         switch (rot) {
137                 case ROTATE_90:
138                         i_start  = sx - 1;
139                         i_step_x = zstride;
140                         i_step_z = -xstride;
141                         SWAP(s16, sx, sz);
142                         break;
143                 case ROTATE_180:
144                         i_start  = zstride * (sz - 1) + sx - 1;
145                         i_step_x = -xstride;
146                         i_step_z = -zstride;
147                         break;
148                 case ROTATE_270:
149                         i_start  = zstride * (sz - 1);
150                         i_step_x = -zstride;
151                         i_step_z = xstride;
152                         SWAP(s16, sx, sz);
153                         break;
154                 default:
155                         i_start  = 0;
156                         i_step_x = xstride;
157                         i_step_z = zstride;
158         }
159
160         s16 y_map = p.Y;
161         for (s16 y = 0; y != sy; y++) {
162                 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
163                         (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
164                         continue;
165
166                 for (s16 z = 0; z != sz; z++) {
167                         u32 i = z * i_step_z + y * ystride + i_start;
168                         for (s16 x = 0; x != sx; x++, i += i_step_x) {
169                                 v3s16 pos(p.X + x, y_map, p.Z + z);
170                                 if (!vm->m_area.contains(pos))
171                                         continue;
172
173                                 if (schemdata[i].getContent() == CONTENT_IGNORE)
174                                         continue;
175
176                                 u8 placement_prob     = schemdata[i].param1 & MTSCHEM_PROB_MASK;
177                                 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
178
179                                 if (placement_prob == MTSCHEM_PROB_NEVER)
180                                         continue;
181
182                                 u32 vi = vm->m_area.index(pos);
183                                 if (!force_place && !force_place_node) {
184                                         content_t c = vm->m_data[vi].getContent();
185                                         if (c != CONTENT_AIR && c != CONTENT_IGNORE)
186                                                 continue;
187                                 }
188
189                                 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
190                                         (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
191                                         continue;
192
193                                 vm->m_data[vi] = schemdata[i];
194                                 vm->m_data[vi].param1 = 0;
195
196                                 if (rot)
197                                         vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
198                         }
199                 }
200                 y_map++;
201         }
202 }
203
204
205 bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags,
206         Rotation rot, bool force_place)
207 {
208         assert(vm != NULL);
209         assert(schemdata && slice_probs);
210         sanity_check(m_ndef != NULL);
211
212         //// Determine effective rotation and effective schematic dimensions
213         if (rot == ROTATE_RAND)
214                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
215
216         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
217                 v3s16(size.Z, size.Y, size.X) : size;
218
219         //// Adjust placement position if necessary
220         if (flags & DECO_PLACE_CENTER_X)
221                 p.X -= (s.X - 1) / 2;
222         if (flags & DECO_PLACE_CENTER_Y)
223                 p.Y -= (s.Y - 1) / 2;
224         if (flags & DECO_PLACE_CENTER_Z)
225                 p.Z -= (s.Z - 1) / 2;
226
227         blitToVManip(vm, p, rot, force_place);
228
229         return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1, 1, 1)));
230 }
231
232 void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
233         Rotation rot, bool force_place)
234 {
235         std::map<v3s16, MapBlock *> lighting_modified_blocks;
236         std::map<v3s16, MapBlock *> modified_blocks;
237         std::map<v3s16, MapBlock *>::iterator it;
238
239         assert(map != NULL);
240         assert(schemdata != NULL);
241         sanity_check(m_ndef != NULL);
242
243         //// Determine effective rotation and effective schematic dimensions
244         if (rot == ROTATE_RAND)
245                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
246
247         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
248                         v3s16(size.Z, size.Y, size.X) : size;
249
250         //// Adjust placement position if necessary
251         if (flags & DECO_PLACE_CENTER_X)
252                 p.X -= (s.X - 1) / 2;
253         if (flags & DECO_PLACE_CENTER_Y)
254                 p.Y -= (s.Y - 1) / 2;
255         if (flags & DECO_PLACE_CENTER_Z)
256                 p.Z -= (s.Z - 1) / 2;
257
258         //// Create VManip for effected area, emerge our area, modify area
259         //// inside VManip, then blit back.
260         v3s16 bp1 = getNodeBlockPos(p);
261         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1, 1, 1));
262
263         MMVManip vm(map);
264         vm.initialEmerge(bp1, bp2);
265
266         blitToVManip(&vm, p, rot, force_place);
267
268         voxalgo::blit_back_with_light(map, &vm, &modified_blocks);
269
270         //// Carry out post-map-modification actions
271
272         //// Create & dispatch map modification events to observers
273         MapEditEvent event;
274         event.type = MEET_OTHER;
275         for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it)
276                 event.modified_blocks.insert(it->first);
277
278         map->dispatchEvent(event);
279 }
280
281
282 bool Schematic::deserializeFromMts(std::istream *is,
283         std::vector<std::string> *names)
284 {
285         std::istream &ss = *is;
286         content_t cignore = CONTENT_IGNORE;
287         bool have_cignore = false;
288
289         //// Read signature
290         u32 signature = readU32(ss);
291         if (signature != MTSCHEM_FILE_SIGNATURE) {
292                 errorstream << __FUNCTION__ << ": invalid schematic "
293                         "file" << std::endl;
294                 return false;
295         }
296
297         //// Read version
298         u16 version = readU16(ss);
299         if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
300                 errorstream << __FUNCTION__ << ": unsupported schematic "
301                         "file version" << std::endl;
302                 return false;
303         }
304
305         //// Read size
306         size = readV3S16(ss);
307
308         //// Read Y-slice probability values
309         delete []slice_probs;
310         slice_probs = new u8[size.Y];
311         for (int y = 0; y != size.Y; y++)
312                 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
313
314         //// Read node names
315         u16 nidmapcount = readU16(ss);
316         for (int i = 0; i != nidmapcount; i++) {
317                 std::string name = deSerializeString(ss);
318
319                 // Instances of "ignore" from v1 are converted to air (and instances
320                 // are fixed to have MTSCHEM_PROB_NEVER later on).
321                 if (name == "ignore") {
322                         name = "air";
323                         cignore = i;
324                         have_cignore = true;
325                 }
326
327                 names->push_back(name);
328         }
329
330         //// Read node data
331         size_t nodecount = size.X * size.Y * size.Z;
332
333         delete []schemdata;
334         schemdata = new MapNode[nodecount];
335
336         MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
337                 nodecount, 2, 2, true);
338
339         // Fix probability values for nodes that were ignore; removed in v2
340         if (version < 2) {
341                 for (size_t i = 0; i != nodecount; i++) {
342                         if (schemdata[i].param1 == 0)
343                                 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
344                         if (have_cignore && schemdata[i].getContent() == cignore)
345                                 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
346                 }
347         }
348
349         // Fix probability values for probability range truncation introduced in v4
350         if (version < 4) {
351                 for (s16 y = 0; y != size.Y; y++)
352                         slice_probs[y] >>= 1;
353                 for (size_t i = 0; i != nodecount; i++)
354                         schemdata[i].param1 >>= 1;
355         }
356
357         return true;
358 }
359
360
361 bool Schematic::serializeToMts(std::ostream *os,
362         const std::vector<std::string> &names)
363 {
364         std::ostream &ss = *os;
365
366         writeU32(ss, MTSCHEM_FILE_SIGNATURE);         // signature
367         writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
368         writeV3S16(ss, size);                         // schematic size
369
370         for (int y = 0; y != size.Y; y++)             // Y slice probabilities
371                 writeU8(ss, slice_probs[y]);
372
373         writeU16(ss, names.size()); // name count
374         for (size_t i = 0; i != names.size(); i++)
375                 ss << serializeString(names[i]); // node names
376
377         // compressed bulk node data
378         MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
379                 schemdata, size.X * size.Y * size.Z, 2, 2, true);
380
381         return true;
382 }
383
384
385 bool Schematic::serializeToLua(std::ostream *os,
386         const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
387 {
388         std::ostream &ss = *os;
389
390         std::string indent("\t");
391         if (indent_spaces > 0)
392                 indent.assign(indent_spaces, ' ');
393
394         //// Write header
395         {
396                 ss << "schematic = {" << std::endl;
397                 ss << indent << "size = "
398                         << "{x=" << size.X
399                         << ", y=" << size.Y
400                         << ", z=" << size.Z
401                         << "}," << std::endl;
402         }
403
404         //// Write y-slice probabilities
405         {
406                 ss << indent << "yslice_prob = {" << std::endl;
407
408                 for (u16 y = 0; y != size.Y; y++) {
409                         u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
410
411                         ss << indent << indent << "{"
412                                 << "ypos=" << y
413                                 << ", prob=" << (u16)probability * 2
414                                 << "}," << std::endl;
415                 }
416
417                 ss << indent << "}," << std::endl;
418         }
419
420         //// Write node data
421         {
422                 ss << indent << "data = {" << std::endl;
423
424                 u32 i = 0;
425                 for (u16 z = 0; z != size.Z; z++)
426                 for (u16 y = 0; y != size.Y; y++) {
427                         if (use_comments) {
428                                 ss << std::endl
429                                         << indent << indent
430                                         << "-- z=" << z
431                                         << ", y=" << y << std::endl;
432                         }
433
434                         for (u16 x = 0; x != size.X; x++, i++) {
435                                 u8 probability   = schemdata[i].param1 & MTSCHEM_PROB_MASK;
436                                 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
437
438                                 ss << indent << indent << "{"
439                                         << "name=\"" << names[schemdata[i].getContent()]
440                                         << "\", prob=" << (u16)probability * 2
441                                         << ", param2=" << (u16)schemdata[i].param2;
442
443                                 if (force_place)
444                                         ss << ", force_place=true";
445
446                                 ss << "}," << std::endl;
447                         }
448                 }
449
450                 ss << indent << "}," << std::endl;
451         }
452
453         ss << "}" << std::endl;
454
455         return true;
456 }
457
458
459 bool Schematic::loadSchematicFromFile(const std::string &filename,
460         const NodeDefManager *ndef, StringMap *replace_names)
461 {
462         std::ifstream is(filename.c_str(), std::ios_base::binary);
463         if (!is.good()) {
464                 errorstream << __FUNCTION__ << ": unable to open file '"
465                         << filename << "'" << std::endl;
466                 return false;
467         }
468
469         size_t origsize = m_nodenames.size();
470         if (!deserializeFromMts(&is, &m_nodenames))
471                 return false;
472
473         m_nnlistsizes.push_back(m_nodenames.size() - origsize);
474
475         name = filename;
476
477         if (replace_names) {
478                 for (size_t i = origsize; i < m_nodenames.size(); i++) {
479                         std::string &node_name = m_nodenames[i];
480                         StringMap::iterator it = replace_names->find(node_name);
481                         if (it != replace_names->end())
482                                 node_name = it->second;
483                 }
484         }
485
486         if (ndef)
487                 ndef->pendNodeResolve(this);
488
489         return true;
490 }
491
492
493 bool Schematic::saveSchematicToFile(const std::string &filename,
494         const NodeDefManager *ndef)
495 {
496         MapNode *orig_schemdata = schemdata;
497         std::vector<std::string> ndef_nodenames;
498         std::vector<std::string> *names;
499
500         if (m_resolve_done && ndef == NULL)
501                 ndef = m_ndef;
502
503         if (ndef) {
504                 names = &ndef_nodenames;
505
506                 u32 volume = size.X * size.Y * size.Z;
507                 schemdata = new MapNode[volume];
508                 for (u32 i = 0; i != volume; i++)
509                         schemdata[i] = orig_schemdata[i];
510
511                 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
512         } else { // otherwise, use the names we have on hand in the list
513                 names = &m_nodenames;
514         }
515
516         std::ostringstream os(std::ios_base::binary);
517         bool status = serializeToMts(&os, *names);
518
519         if (ndef) {
520                 delete []schemdata;
521                 schemdata = orig_schemdata;
522         }
523
524         if (!status)
525                 return false;
526
527         return fs::safeWriteToFile(filename, os.str());
528 }
529
530
531 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
532 {
533         MMVManip *vm = new MMVManip(map);
534
535         v3s16 bp1 = getNodeBlockPos(p1);
536         v3s16 bp2 = getNodeBlockPos(p2);
537         vm->initialEmerge(bp1, bp2);
538
539         size = p2 - p1 + 1;
540
541         slice_probs = new u8[size.Y];
542         for (s16 y = 0; y != size.Y; y++)
543                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
544
545         schemdata = new MapNode[size.X * size.Y * size.Z];
546
547         u32 i = 0;
548         for (s16 z = p1.Z; z <= p2.Z; z++)
549         for (s16 y = p1.Y; y <= p2.Y; y++) {
550                 u32 vi = vm->m_area.index(p1.X, y, z);
551                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
552                         schemdata[i] = vm->m_data[vi];
553                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
554                 }
555         }
556
557         delete vm;
558         return true;
559 }
560
561
562 void Schematic::applyProbabilities(v3s16 p0,
563         std::vector<std::pair<v3s16, u8> > *plist,
564         std::vector<std::pair<s16, u8> > *splist)
565 {
566         for (size_t i = 0; i != plist->size(); i++) {
567                 v3s16 p = (*plist)[i].first - p0;
568                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
569                 if (index < size.Z * size.Y * size.X) {
570                         u8 prob = (*plist)[i].second;
571                         schemdata[index].param1 = prob;
572
573                         // trim unnecessary node names from schematic
574                         if (prob == MTSCHEM_PROB_NEVER)
575                                 schemdata[index].setContent(CONTENT_AIR);
576                 }
577         }
578
579         for (size_t i = 0; i != splist->size(); i++) {
580                 s16 y = (*splist)[i].first - p0.Y;
581                 slice_probs[y] = (*splist)[i].second;
582         }
583 }
584
585
586 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
587         std::vector<std::string> *usednodes, const NodeDefManager *ndef)
588 {
589         std::unordered_map<content_t, content_t> nodeidmap;
590         content_t numids = 0;
591
592         for (size_t i = 0; i != nodecount; i++) {
593                 content_t id;
594                 content_t c = nodes[i].getContent();
595
596                 std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
597                 if (it == nodeidmap.end()) {
598                         id = numids;
599                         numids++;
600
601                         usednodes->push_back(ndef->get(c).name);
602                         nodeidmap.insert(std::make_pair(c, id));
603                 } else {
604                         id = it->second;
605                 }
606                 nodes[i].setContent(id);
607         }
608 }