]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mg_schematic.cpp
Add keybind to swap items between hands
[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 {
81         delete []schemdata;
82         delete []slice_probs;
83 }
84
85 ObjDef *Schematic::clone() const
86 {
87         auto def = new Schematic();
88         ObjDef::cloneTo(def);
89         NodeResolver::cloneTo(def);
90
91         def->c_nodes = c_nodes;
92         def->flags = flags;
93         def->size = size;
94         FATAL_ERROR_IF(!schemdata, "Schematic can only be cloned after loading");
95         u32 nodecount = size.X * size.Y * size.Z;
96         def->schemdata = new MapNode[nodecount];
97         memcpy(def->schemdata, schemdata, sizeof(MapNode) * nodecount);
98         def->slice_probs = new u8[size.Y];
99         memcpy(def->slice_probs, slice_probs, sizeof(u8) * size.Y);
100
101         return def;
102 }
103
104
105 void Schematic::resolveNodeNames()
106 {
107         c_nodes.clear();
108         getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
109
110         size_t bufsize = size.X * size.Y * size.Z;
111         for (size_t i = 0; i != bufsize; i++) {
112                 content_t c_original = schemdata[i].getContent();
113                 if (c_original >= c_nodes.size()) {
114                         errorstream << "Corrupt schematic. name=\"" << name
115                                 << "\" at index " << i << std::endl;
116                         c_original = 0;
117                 }
118                 // Unfold condensed ID layout to content_t
119                 schemdata[i].setContent(c_nodes[c_original]);
120         }
121 }
122
123
124 void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
125 {
126         assert(schemdata && slice_probs);
127         sanity_check(m_ndef != NULL);
128
129         int xstride = 1;
130         int ystride = size.X;
131         int zstride = size.X * size.Y;
132
133         s16 sx = size.X;
134         s16 sy = size.Y;
135         s16 sz = size.Z;
136
137         int i_start, i_step_x, i_step_z;
138         switch (rot) {
139                 case ROTATE_90:
140                         i_start  = sx - 1;
141                         i_step_x = zstride;
142                         i_step_z = -xstride;
143                         SWAP(s16, sx, sz);
144                         break;
145                 case ROTATE_180:
146                         i_start  = zstride * (sz - 1) + sx - 1;
147                         i_step_x = -xstride;
148                         i_step_z = -zstride;
149                         break;
150                 case ROTATE_270:
151                         i_start  = zstride * (sz - 1);
152                         i_step_x = -zstride;
153                         i_step_z = xstride;
154                         SWAP(s16, sx, sz);
155                         break;
156                 default:
157                         i_start  = 0;
158                         i_step_x = xstride;
159                         i_step_z = zstride;
160         }
161
162         s16 y_map = p.Y;
163         for (s16 y = 0; y != sy; y++) {
164                 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
165                         (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
166                         continue;
167
168                 for (s16 z = 0; z != sz; z++) {
169                         u32 i = z * i_step_z + y * ystride + i_start;
170                         for (s16 x = 0; x != sx; x++, i += i_step_x) {
171                                 v3s16 pos(p.X + x, y_map, p.Z + z);
172                                 if (!vm->m_area.contains(pos))
173                                         continue;
174
175                                 if (schemdata[i].getContent() == CONTENT_IGNORE)
176                                         continue;
177
178                                 u8 placement_prob     = schemdata[i].param1 & MTSCHEM_PROB_MASK;
179                                 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
180
181                                 if (placement_prob == MTSCHEM_PROB_NEVER)
182                                         continue;
183
184                                 u32 vi = vm->m_area.index(pos);
185                                 if (!force_place && !force_place_node) {
186                                         content_t c = vm->m_data[vi].getContent();
187                                         if (c != CONTENT_AIR && c != CONTENT_IGNORE)
188                                                 continue;
189                                 }
190
191                                 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
192                                         (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
193                                         continue;
194
195                                 vm->m_data[vi] = schemdata[i];
196                                 vm->m_data[vi].param1 = 0;
197
198                                 if (rot)
199                                         vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
200                         }
201                 }
202                 y_map++;
203         }
204 }
205
206
207 bool Schematic::placeOnVManip(MMVManip *vm, v3s16 p, u32 flags,
208         Rotation rot, bool force_place)
209 {
210         assert(vm != NULL);
211         assert(schemdata && slice_probs);
212         sanity_check(m_ndef != NULL);
213
214         //// Determine effective rotation and effective schematic dimensions
215         if (rot == ROTATE_RAND)
216                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
217
218         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
219                 v3s16(size.Z, size.Y, size.X) : size;
220
221         //// Adjust placement position if necessary
222         if (flags & DECO_PLACE_CENTER_X)
223                 p.X -= (s.X - 1) / 2;
224         if (flags & DECO_PLACE_CENTER_Y)
225                 p.Y -= (s.Y - 1) / 2;
226         if (flags & DECO_PLACE_CENTER_Z)
227                 p.Z -= (s.Z - 1) / 2;
228
229         blitToVManip(vm, p, rot, force_place);
230
231         return vm->m_area.contains(VoxelArea(p, p + s - v3s16(1, 1, 1)));
232 }
233
234 void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
235         Rotation rot, bool force_place)
236 {
237         std::map<v3s16, MapBlock *> modified_blocks;
238         std::map<v3s16, MapBlock *>::iterator it;
239
240         assert(map != NULL);
241         assert(schemdata != NULL);
242         sanity_check(m_ndef != NULL);
243
244         //// Determine effective rotation and effective schematic dimensions
245         if (rot == ROTATE_RAND)
246                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
247
248         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
249                         v3s16(size.Z, size.Y, size.X) : size;
250
251         //// Adjust placement position if necessary
252         if (flags & DECO_PLACE_CENTER_X)
253                 p.X -= (s.X - 1) / 2;
254         if (flags & DECO_PLACE_CENTER_Y)
255                 p.Y -= (s.Y - 1) / 2;
256         if (flags & DECO_PLACE_CENTER_Z)
257                 p.Z -= (s.Z - 1) / 2;
258
259         //// Create VManip for affected area, emerge our area, modify area
260         //// inside VManip, then blit back.
261         v3s16 bp1 = getNodeBlockPos(p);
262         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1, 1, 1));
263
264         MMVManip vm(map);
265         vm.initialEmerge(bp1, bp2);
266
267         blitToVManip(&vm, p, rot, force_place);
268
269         voxalgo::blit_back_with_light(map, &vm, &modified_blocks);
270
271         //// Carry out post-map-modification actions
272
273         //// Create & dispatch map modification events to observers
274         MapEditEvent event;
275         event.type = MEET_OTHER;
276         event.setModifiedBlocks(modified_blocks);
277
278         map->dispatchEvent(event);
279 }
280
281
282 bool Schematic::deserializeFromMts(std::istream *is)
283 {
284         std::istream &ss = *is;
285         content_t cignore = CONTENT_IGNORE;
286         bool have_cignore = false;
287
288         //// Read signature
289         u32 signature = readU32(ss);
290         if (signature != MTSCHEM_FILE_SIGNATURE) {
291                 errorstream << __FUNCTION__ << ": invalid schematic "
292                         "file" << std::endl;
293                 return false;
294         }
295
296         //// Read version
297         u16 version = readU16(ss);
298         if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
299                 errorstream << __FUNCTION__ << ": unsupported schematic "
300                         "file version" << std::endl;
301                 return false;
302         }
303
304         //// Read size
305         size = readV3S16(ss);
306
307         //// Read Y-slice probability values
308         delete []slice_probs;
309         slice_probs = new u8[size.Y];
310         for (int y = 0; y != size.Y; y++)
311                 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
312
313         //// Read node names
314         NodeResolver::reset();
315
316         u16 nidmapcount = readU16(ss);
317         for (int i = 0; i != nidmapcount; i++) {
318                 std::string name = deSerializeString16(ss);
319
320                 // Instances of "ignore" from v1 are converted to air (and instances
321                 // are fixed to have MTSCHEM_PROB_NEVER later on).
322                 if (name == "ignore") {
323                         name = "air";
324                         cignore = i;
325                         have_cignore = true;
326                 }
327
328                 m_nodenames.push_back(name);
329         }
330
331         // Prepare for node resolver
332         m_nnlistsizes.push_back(m_nodenames.size());
333
334         //// Read node data
335         size_t nodecount = size.X * size.Y * size.Z;
336
337         delete []schemdata;
338         schemdata = new MapNode[nodecount];
339
340         std::stringstream d_ss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
341         decompress(ss, d_ss, MTSCHEM_MAPNODE_SER_FMT_VER);
342         MapNode::deSerializeBulk(d_ss, MTSCHEM_MAPNODE_SER_FMT_VER, schemdata,
343                 nodecount, 2, 2);
344
345         // Fix probability values for nodes that were ignore; removed in v2
346         if (version < 2) {
347                 for (size_t i = 0; i != nodecount; i++) {
348                         if (schemdata[i].param1 == 0)
349                                 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
350                         if (have_cignore && schemdata[i].getContent() == cignore)
351                                 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
352                 }
353         }
354
355         // Fix probability values for probability range truncation introduced in v4
356         if (version < 4) {
357                 for (s16 y = 0; y != size.Y; y++)
358                         slice_probs[y] >>= 1;
359                 for (size_t i = 0; i != nodecount; i++)
360                         schemdata[i].param1 >>= 1;
361         }
362
363         return true;
364 }
365
366
367 bool Schematic::serializeToMts(std::ostream *os) const
368 {
369         // Nodes must not be resolved (-> condensed)
370         // checking here is not possible because "schemdata" might be temporary.
371
372         std::ostream &ss = *os;
373
374         writeU32(ss, MTSCHEM_FILE_SIGNATURE);         // signature
375         writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
376         writeV3S16(ss, size);                         // schematic size
377
378         for (int y = 0; y != size.Y; y++)             // Y slice probabilities
379                 writeU8(ss, slice_probs[y]);
380
381         writeU16(ss, m_nodenames.size()); // name count
382         for (size_t i = 0; i != m_nodenames.size(); i++) {
383                 ss << serializeString16(m_nodenames[i]); // node names
384         }
385
386         // compressed bulk node data
387         SharedBuffer<u8> buf = MapNode::serializeBulk(MTSCHEM_MAPNODE_SER_FMT_VER,
388                 schemdata, size.X * size.Y * size.Z, 2, 2);
389         compress(buf, ss, MTSCHEM_MAPNODE_SER_FMT_VER);
390
391         return true;
392 }
393
394
395 bool Schematic::serializeToLua(std::ostream *os, bool use_comments,
396         u32 indent_spaces) const
397 {
398         std::ostream &ss = *os;
399
400         std::string indent("\t");
401         if (indent_spaces > 0)
402                 indent.assign(indent_spaces, ' ');
403
404         bool resolve_done = isResolveDone();
405         FATAL_ERROR_IF(resolve_done && !m_ndef, "serializeToLua: NodeDefManager is required");
406
407         //// Write header
408         {
409                 ss << "schematic = {" << std::endl;
410                 ss << indent << "size = "
411                         << "{x=" << size.X
412                         << ", y=" << size.Y
413                         << ", z=" << size.Z
414                         << "}," << std::endl;
415         }
416
417         //// Write y-slice probabilities
418         {
419                 ss << indent << "yslice_prob = {" << std::endl;
420
421                 for (u16 y = 0; y != size.Y; y++) {
422                         u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
423
424                         ss << indent << indent << "{"
425                                 << "ypos=" << y
426                                 << ", prob=" << (u16)probability * 2
427                                 << "}," << std::endl;
428                 }
429
430                 ss << indent << "}," << std::endl;
431         }
432
433         //// Write node data
434         {
435                 ss << indent << "data = {" << std::endl;
436
437                 u32 i = 0;
438                 for (u16 z = 0; z != size.Z; z++)
439                 for (u16 y = 0; y != size.Y; y++) {
440                         if (use_comments) {
441                                 ss << std::endl
442                                         << indent << indent
443                                         << "-- z=" << z
444                                         << ", y=" << y << std::endl;
445                         }
446
447                         for (u16 x = 0; x != size.X; x++, i++) {
448                                 u8 probability   = schemdata[i].param1 & MTSCHEM_PROB_MASK;
449                                 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
450
451                                 // After node resolving: real content_t, lookup using NodeDefManager
452                                 // Prior node resolving: condensed ID, lookup using m_nodenames
453                                 content_t c = schemdata[i].getContent();
454
455                                 ss << indent << indent << "{" << "name=\"";
456
457                                 if (!resolve_done) {
458                                         // Prior node resolving (eg. direct schematic load)
459                                         FATAL_ERROR_IF(c >= m_nodenames.size(), "Invalid node list");
460                                         ss << m_nodenames[c];
461                                 } else  {
462                                         // After node resolving (eg. biome decoration)
463                                         ss << m_ndef->get(c).name;
464                                 }
465
466                                 ss << "\", prob=" << (u16)probability * 2
467                                         << ", param2=" << (u16)schemdata[i].param2;
468
469                                 if (force_place)
470                                         ss << ", force_place=true";
471
472                                 ss << "}," << std::endl;
473                         }
474                 }
475
476                 ss << indent << "}," << std::endl;
477         }
478
479         ss << "}" << std::endl;
480
481         return true;
482 }
483
484
485 bool Schematic::loadSchematicFromFile(const std::string &filename,
486         const NodeDefManager *ndef, StringMap *replace_names)
487 {
488         std::ifstream is(filename.c_str(), std::ios_base::binary);
489         if (!is.good()) {
490                 errorstream << __FUNCTION__ << ": unable to open file '"
491                         << filename << "'" << std::endl;
492                 return false;
493         }
494
495         if (!m_ndef)
496                 m_ndef = ndef;
497
498         if (!deserializeFromMts(&is))
499                 return false;
500
501         name = filename;
502
503         if (replace_names) {
504                 for (std::string &node_name : m_nodenames) {
505                         StringMap::iterator it = replace_names->find(node_name);
506                         if (it != replace_names->end())
507                                 node_name = it->second;
508                 }
509         }
510
511         if (m_ndef)
512                 m_ndef->pendNodeResolve(this);
513
514         return true;
515 }
516
517
518 bool Schematic::saveSchematicToFile(const std::string &filename,
519         const NodeDefManager *ndef)
520 {
521         Schematic *schem = this;
522
523         bool needs_condense = isResolveDone();
524
525         if (!m_ndef)
526                 m_ndef = ndef;
527
528         if (needs_condense) {
529                 if (!m_ndef)
530                         return false;
531
532                 schem = (Schematic *)this->clone();
533                 schem->condenseContentIds();
534         }
535
536         std::ostringstream os(std::ios_base::binary);
537         bool status = schem->serializeToMts(&os);
538
539         if (needs_condense)
540                 delete schem;
541
542         if (!status)
543                 return false;
544
545         return fs::safeWriteToFile(filename, os.str());
546 }
547
548
549 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
550 {
551         MMVManip *vm = new MMVManip(map);
552
553         v3s16 bp1 = getNodeBlockPos(p1);
554         v3s16 bp2 = getNodeBlockPos(p2);
555         vm->initialEmerge(bp1, bp2);
556
557         size = p2 - p1 + 1;
558
559         slice_probs = new u8[size.Y];
560         for (s16 y = 0; y != size.Y; y++)
561                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
562
563         schemdata = new MapNode[size.X * size.Y * size.Z];
564
565         u32 i = 0;
566         for (s16 z = p1.Z; z <= p2.Z; z++)
567         for (s16 y = p1.Y; y <= p2.Y; y++) {
568                 u32 vi = vm->m_area.index(p1.X, y, z);
569                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
570                         schemdata[i] = vm->m_data[vi];
571                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
572                 }
573         }
574
575         delete vm;
576
577         // Reset and mark as complete
578         NodeResolver::reset(true);
579
580         return true;
581 }
582
583
584 void Schematic::applyProbabilities(v3s16 p0,
585         std::vector<std::pair<v3s16, u8> > *plist,
586         std::vector<std::pair<s16, u8> > *splist)
587 {
588         for (size_t i = 0; i != plist->size(); i++) {
589                 v3s16 p = (*plist)[i].first - p0;
590                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
591                 if (index < size.Z * size.Y * size.X) {
592                         u8 prob = (*plist)[i].second;
593                         schemdata[index].param1 = prob;
594
595                         // trim unnecessary node names from schematic
596                         if (prob == MTSCHEM_PROB_NEVER)
597                                 schemdata[index].setContent(CONTENT_AIR);
598                 }
599         }
600
601         for (size_t i = 0; i != splist->size(); i++) {
602                 s16 slice = (*splist)[i].first;
603                 if (slice < size.Y)
604                         slice_probs[slice] = (*splist)[i].second;
605         }
606 }
607
608
609 void Schematic::condenseContentIds()
610 {
611         std::unordered_map<content_t, content_t> nodeidmap;
612         content_t numids = 0;
613
614         // Reset node resolve fields
615         NodeResolver::reset();
616
617         size_t nodecount = size.X * size.Y * size.Z;
618         for (size_t i = 0; i != nodecount; i++) {
619                 content_t id;
620                 content_t c = schemdata[i].getContent();
621
622                 auto it = nodeidmap.find(c);
623                 if (it == nodeidmap.end()) {
624                         id = numids;
625                         numids++;
626
627                         m_nodenames.push_back(m_ndef->get(c).name);
628                         nodeidmap.emplace(std::make_pair(c, id));
629                 } else {
630                         id = it->second;
631                 }
632                 schemdata[i].setContent(id);
633         }
634 }