]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mg_schematic.cpp
(se)SerializeString: Include max length in the name
[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 = deSerializeString16(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) const
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 << serializeString16(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,
387         u32 indent_spaces) const
388 {
389         std::ostream &ss = *os;
390
391         std::string indent("\t");
392         if (indent_spaces > 0)
393                 indent.assign(indent_spaces, ' ');
394
395         //// Write header
396         {
397                 ss << "schematic = {" << std::endl;
398                 ss << indent << "size = "
399                         << "{x=" << size.X
400                         << ", y=" << size.Y
401                         << ", z=" << size.Z
402                         << "}," << std::endl;
403         }
404
405         //// Write y-slice probabilities
406         {
407                 ss << indent << "yslice_prob = {" << std::endl;
408
409                 for (u16 y = 0; y != size.Y; y++) {
410                         u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
411
412                         ss << indent << indent << "{"
413                                 << "ypos=" << y
414                                 << ", prob=" << (u16)probability * 2
415                                 << "}," << std::endl;
416                 }
417
418                 ss << indent << "}," << std::endl;
419         }
420
421         //// Write node data
422         {
423                 ss << indent << "data = {" << std::endl;
424
425                 u32 i = 0;
426                 for (u16 z = 0; z != size.Z; z++)
427                 for (u16 y = 0; y != size.Y; y++) {
428                         if (use_comments) {
429                                 ss << std::endl
430                                         << indent << indent
431                                         << "-- z=" << z
432                                         << ", y=" << y << std::endl;
433                         }
434
435                         for (u16 x = 0; x != size.X; x++, i++) {
436                                 u8 probability   = schemdata[i].param1 & MTSCHEM_PROB_MASK;
437                                 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
438
439                                 ss << indent << indent << "{"
440                                         << "name=\"" << names[schemdata[i].getContent()]
441                                         << "\", prob=" << (u16)probability * 2
442                                         << ", param2=" << (u16)schemdata[i].param2;
443
444                                 if (force_place)
445                                         ss << ", force_place=true";
446
447                                 ss << "}," << std::endl;
448                         }
449                 }
450
451                 ss << indent << "}," << std::endl;
452         }
453
454         ss << "}" << std::endl;
455
456         return true;
457 }
458
459
460 bool Schematic::loadSchematicFromFile(const std::string &filename,
461         const NodeDefManager *ndef, StringMap *replace_names)
462 {
463         std::ifstream is(filename.c_str(), std::ios_base::binary);
464         if (!is.good()) {
465                 errorstream << __FUNCTION__ << ": unable to open file '"
466                         << filename << "'" << std::endl;
467                 return false;
468         }
469
470         size_t origsize = m_nodenames.size();
471         if (!deserializeFromMts(&is, &m_nodenames))
472                 return false;
473
474         m_nnlistsizes.push_back(m_nodenames.size() - origsize);
475
476         name = filename;
477
478         if (replace_names) {
479                 for (size_t i = origsize; i < m_nodenames.size(); i++) {
480                         std::string &node_name = m_nodenames[i];
481                         StringMap::iterator it = replace_names->find(node_name);
482                         if (it != replace_names->end())
483                                 node_name = it->second;
484                 }
485         }
486
487         if (ndef)
488                 ndef->pendNodeResolve(this);
489
490         return true;
491 }
492
493
494 bool Schematic::saveSchematicToFile(const std::string &filename,
495         const NodeDefManager *ndef)
496 {
497         MapNode *orig_schemdata = schemdata;
498         std::vector<std::string> ndef_nodenames;
499         std::vector<std::string> *names;
500
501         if (m_resolve_done && ndef == NULL)
502                 ndef = m_ndef;
503
504         if (ndef) {
505                 names = &ndef_nodenames;
506
507                 u32 volume = size.X * size.Y * size.Z;
508                 schemdata = new MapNode[volume];
509                 for (u32 i = 0; i != volume; i++)
510                         schemdata[i] = orig_schemdata[i];
511
512                 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
513         } else { // otherwise, use the names we have on hand in the list
514                 names = &m_nodenames;
515         }
516
517         std::ostringstream os(std::ios_base::binary);
518         bool status = serializeToMts(&os, *names);
519
520         if (ndef) {
521                 delete []schemdata;
522                 schemdata = orig_schemdata;
523         }
524
525         if (!status)
526                 return false;
527
528         return fs::safeWriteToFile(filename, os.str());
529 }
530
531
532 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
533 {
534         MMVManip *vm = new MMVManip(map);
535
536         v3s16 bp1 = getNodeBlockPos(p1);
537         v3s16 bp2 = getNodeBlockPos(p2);
538         vm->initialEmerge(bp1, bp2);
539
540         size = p2 - p1 + 1;
541
542         slice_probs = new u8[size.Y];
543         for (s16 y = 0; y != size.Y; y++)
544                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
545
546         schemdata = new MapNode[size.X * size.Y * size.Z];
547
548         u32 i = 0;
549         for (s16 z = p1.Z; z <= p2.Z; z++)
550         for (s16 y = p1.Y; y <= p2.Y; y++) {
551                 u32 vi = vm->m_area.index(p1.X, y, z);
552                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
553                         schemdata[i] = vm->m_data[vi];
554                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
555                 }
556         }
557
558         delete vm;
559         return true;
560 }
561
562
563 void Schematic::applyProbabilities(v3s16 p0,
564         std::vector<std::pair<v3s16, u8> > *plist,
565         std::vector<std::pair<s16, u8> > *splist)
566 {
567         for (size_t i = 0; i != plist->size(); i++) {
568                 v3s16 p = (*plist)[i].first - p0;
569                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
570                 if (index < size.Z * size.Y * size.X) {
571                         u8 prob = (*plist)[i].second;
572                         schemdata[index].param1 = prob;
573
574                         // trim unnecessary node names from schematic
575                         if (prob == MTSCHEM_PROB_NEVER)
576                                 schemdata[index].setContent(CONTENT_AIR);
577                 }
578         }
579
580         for (size_t i = 0; i != splist->size(); i++) {
581                 s16 y = (*splist)[i].first - p0.Y;
582                 slice_probs[y] = (*splist)[i].second;
583         }
584 }
585
586
587 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
588         std::vector<std::string> *usednodes, const NodeDefManager *ndef)
589 {
590         std::unordered_map<content_t, content_t> nodeidmap;
591         content_t numids = 0;
592
593         for (size_t i = 0; i != nodecount; i++) {
594                 content_t id;
595                 content_t c = nodes[i].getContent();
596
597                 std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
598                 if (it == nodeidmap.end()) {
599                         id = numids;
600                         numids++;
601
602                         usednodes->push_back(ndef->get(c).name);
603                         nodeidmap.insert(std::make_pair(c, id));
604                 } else {
605                         id = it->second;
606                 }
607                 nodes[i].setContent(id);
608         }
609 }