]> git.lizzy.rs Git - minetest.git/blob - src/mg_schematic.cpp
Disable mesh cache by default
[minetest.git] / src / mg_schematic.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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 <fstream>
21 #include <typeinfo>
22 #include "mg_schematic.h"
23 #include "gamedef.h"
24 #include "mapgen.h"
25 #include "emerge.h"
26 #include "map.h"
27 #include "mapblock.h"
28 #include "log.h"
29 #include "util/numeric.h"
30 #include "util/serialize.h"
31 #include "serialization.h"
32 #include "filesys.h"
33
34 ///////////////////////////////////////////////////////////////////////////////
35
36
37 SchematicManager::SchematicManager(IGameDef *gamedef) :
38         ObjDefManager(gamedef, OBJDEF_SCHEMATIC)
39 {
40         m_gamedef = gamedef;
41 }
42
43
44 void SchematicManager::clear()
45 {
46         EmergeManager *emerge = m_gamedef->getEmergeManager();
47
48         // Remove all dangling references in Decorations
49         DecorationManager *decomgr = emerge->decomgr;
50         for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
51                 Decoration *deco = (Decoration *)decomgr->getRaw(i);
52
53                 try {
54                         DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
55                         if (dschem)
56                                 dschem->schematic = NULL;
57                 } catch (std::bad_cast) {
58                 }
59         }
60
61         ObjDefManager::clear();
62 }
63
64
65 ///////////////////////////////////////////////////////////////////////////////
66
67
68 Schematic::Schematic()
69 {
70         schemdata   = NULL;
71         slice_probs = NULL;
72         flags       = 0;
73         size        = v3s16(0, 0, 0);
74 }
75
76
77 Schematic::~Schematic()
78 {
79         delete []schemdata;
80         delete []slice_probs;
81 }
82
83
84 void Schematic::resolveNodeNames()
85 {
86         getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
87
88         size_t bufsize = size.X * size.Y * size.Z;
89         for (size_t i = 0; i != bufsize; i++) {
90                 content_t c_original = schemdata[i].getContent();
91                 content_t c_new = c_nodes[c_original];
92                 schemdata[i].setContent(c_new);
93         }
94 }
95
96
97 void Schematic::blitToVManip(v3s16 p, MMVManip *vm, Rotation rot, bool force_place)
98 {
99         sanity_check(m_ndef != NULL);
100
101         int xstride = 1;
102         int ystride = size.X;
103         int zstride = size.X * size.Y;
104
105         s16 sx = size.X;
106         s16 sy = size.Y;
107         s16 sz = size.Z;
108
109         int i_start, i_step_x, i_step_z;
110         switch (rot) {
111                 case ROTATE_90:
112                         i_start  = sx - 1;
113                         i_step_x = zstride;
114                         i_step_z = -xstride;
115                         SWAP(s16, sx, sz);
116                         break;
117                 case ROTATE_180:
118                         i_start  = zstride * (sz - 1) + sx - 1;
119                         i_step_x = -xstride;
120                         i_step_z = -zstride;
121                         break;
122                 case ROTATE_270:
123                         i_start  = zstride * (sz - 1);
124                         i_step_x = -zstride;
125                         i_step_z = xstride;
126                         SWAP(s16, sx, sz);
127                         break;
128                 default:
129                         i_start  = 0;
130                         i_step_x = xstride;
131                         i_step_z = zstride;
132         }
133
134         s16 y_map = p.Y;
135         for (s16 y = 0; y != sy; y++) {
136                 if ((slice_probs[y] != MTSCHEM_PROB_ALWAYS) &&
137                         (slice_probs[y] <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
138                         continue;
139
140                 for (s16 z = 0; z != sz; z++) {
141                         u32 i = z * i_step_z + y * ystride + i_start;
142                         for (s16 x = 0; x != sx; x++, i += i_step_x) {
143                                 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
144                                 if (!vm->m_area.contains(vi))
145                                         continue;
146
147                                 if (schemdata[i].getContent() == CONTENT_IGNORE)
148                                         continue;
149
150                                 u8 placement_prob     = schemdata[i].param1 & MTSCHEM_PROB_MASK;
151                                 bool force_place_node = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
152
153                                 if (placement_prob == MTSCHEM_PROB_NEVER)
154                                         continue;
155
156                                 if (!force_place && !force_place_node) {
157                                         content_t c = vm->m_data[vi].getContent();
158                                         if (c != CONTENT_AIR && c != CONTENT_IGNORE)
159                                                 continue;
160                                 }
161
162                                 if ((placement_prob != MTSCHEM_PROB_ALWAYS) &&
163                                         (placement_prob <= myrand_range(1, MTSCHEM_PROB_ALWAYS)))
164                                         continue;
165
166                                 vm->m_data[vi] = schemdata[i];
167                                 vm->m_data[vi].param1 = 0;
168
169                                 if (rot)
170                                         vm->m_data[vi].rotateAlongYAxis(m_ndef, rot);
171                         }
172                 }
173                 y_map++;
174         }
175 }
176
177
178 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
179         Rotation rot, bool force_place)
180 {
181         assert(schemdata != NULL); // Pre-condition
182         sanity_check(m_ndef != NULL);
183
184         MMVManip *vm = new MMVManip(map);
185
186         if (rot == ROTATE_RAND)
187                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
188
189         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
190                                 v3s16(size.Z, size.Y, size.X) : size;
191
192         if (flags & DECO_PLACE_CENTER_X)
193                 p.X -= (s.X + 1) / 2;
194         if (flags & DECO_PLACE_CENTER_Y)
195                 p.Y -= (s.Y + 1) / 2;
196         if (flags & DECO_PLACE_CENTER_Z)
197                 p.Z -= (s.Z + 1) / 2;
198
199         v3s16 bp1 = getNodeBlockPos(p);
200         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
201         vm->initialEmerge(bp1, bp2);
202
203         blitToVManip(p, vm, rot, force_place);
204
205         std::map<v3s16, MapBlock *> lighting_modified_blocks;
206         std::map<v3s16, MapBlock *> modified_blocks;
207         vm->blitBackAll(&modified_blocks);
208
209         // TODO: Optimize this by using Mapgen::calcLighting() instead
210         lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
211         map->updateLighting(lighting_modified_blocks, modified_blocks);
212
213         MapEditEvent event;
214         event.type = MEET_OTHER;
215         for (std::map<v3s16, MapBlock *>::iterator
216                 it = modified_blocks.begin();
217                 it != modified_blocks.end(); ++it)
218                 event.modified_blocks.insert(it->first);
219
220         map->dispatchEvent(&event);
221 }
222
223
224 bool Schematic::deserializeFromMts(std::istream *is,
225         std::vector<std::string> *names)
226 {
227         std::istream &ss = *is;
228         content_t cignore = CONTENT_IGNORE;
229         bool have_cignore = false;
230
231         //// Read signature
232         u32 signature = readU32(ss);
233         if (signature != MTSCHEM_FILE_SIGNATURE) {
234                 errorstream << "Schematic::deserializeFromMts: invalid schematic "
235                         "file" << std::endl;
236                 return false;
237         }
238
239         //// Read version
240         u16 version = readU16(ss);
241         if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
242                 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
243                         "file version" << std::endl;
244                 return false;
245         }
246
247         //// Read size
248         size = readV3S16(ss);
249
250         //// Read Y-slice probability values
251         delete []slice_probs;
252         slice_probs = new u8[size.Y];
253         for (int y = 0; y != size.Y; y++)
254                 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
255
256         //// Read node names
257         u16 nidmapcount = readU16(ss);
258         for (int i = 0; i != nidmapcount; i++) {
259                 std::string name = deSerializeString(ss);
260
261                 // Instances of "ignore" from v1 are converted to air (and instances
262                 // are fixed to have MTSCHEM_PROB_NEVER later on).
263                 if (name == "ignore") {
264                         name = "air";
265                         cignore = i;
266                         have_cignore = true;
267                 }
268
269                 names->push_back(name);
270         }
271
272         //// Read node data
273         size_t nodecount = size.X * size.Y * size.Z;
274
275         delete []schemdata;
276         schemdata = new MapNode[nodecount];
277
278         MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
279                 nodecount, 2, 2, true);
280
281         // Fix probability values for nodes that were ignore; removed in v2
282         if (version < 2) {
283                 for (size_t i = 0; i != nodecount; i++) {
284                         if (schemdata[i].param1 == 0)
285                                 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS_OLD;
286                         if (have_cignore && schemdata[i].getContent() == cignore)
287                                 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
288                 }
289         }
290
291         // Fix probability values for probability range truncation introduced in v4
292         if (version < 4) {
293                 for (s16 y = 0; y != size.Y; y++)
294                         slice_probs[y] >>= 1;
295                 for (size_t i = 0; i != nodecount; i++)
296                         schemdata[i].param1 >>= 1;
297         }
298
299         return true;
300 }
301
302
303 bool Schematic::serializeToMts(std::ostream *os,
304         const std::vector<std::string> &names)
305 {
306         std::ostream &ss = *os;
307
308         writeU32(ss, MTSCHEM_FILE_SIGNATURE);         // signature
309         writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
310         writeV3S16(ss, size);                         // schematic size
311
312         for (int y = 0; y != size.Y; y++)             // Y slice probabilities
313                 writeU8(ss, slice_probs[y]);
314
315         writeU16(ss, names.size()); // name count
316         for (size_t i = 0; i != names.size(); i++)
317                 ss << serializeString(names[i]); // node names
318
319         // compressed bulk node data
320         MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
321                 schemdata, size.X * size.Y * size.Z, 2, 2, true);
322
323         return true;
324 }
325
326
327 bool Schematic::serializeToLua(std::ostream *os,
328         const std::vector<std::string> &names, bool use_comments, u32 indent_spaces)
329 {
330         std::ostream &ss = *os;
331
332         std::string indent("\t");
333         if (indent_spaces > 0)
334                 indent.assign(indent_spaces, ' ');
335
336         //// Write header
337         {
338                 ss << "schematic = {" << std::endl;
339                 ss << indent << "size = "
340                         << "{x=" << size.X
341                         << ", y=" << size.Y
342                         << ", z=" << size.Z
343                         << "}," << std::endl;
344         }
345
346         //// Write y-slice probabilities
347         {
348                 ss << indent << "yslice_prob = {" << std::endl;
349
350                 for (u16 y = 0; y != size.Y; y++) {
351                         u8 probability = slice_probs[y] & MTSCHEM_PROB_MASK;
352
353                         ss << indent << indent << "{"
354                                 << "ypos=" << y
355                                 << ", prob=" << (u16)probability * 2
356                                 << "}," << std::endl;
357                 }
358
359                 ss << indent << "}," << std::endl;
360         }
361
362         //// Write node data
363         {
364                 ss << indent << "data = {" << std::endl;
365
366                 u32 i = 0;
367                 for (u16 z = 0; z != size.Z; z++)
368                 for (u16 y = 0; y != size.Y; y++) {
369                         if (use_comments) {
370                                 ss << std::endl
371                                         << indent << indent
372                                         << "-- z=" << z
373                                         << ", y=" << y << std::endl;
374                         }
375
376                         for (u16 x = 0; x != size.X; x++, i++) {
377                                 u8 probability   = schemdata[i].param1 & MTSCHEM_PROB_MASK;
378                                 bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
379
380                                 ss << indent << indent << "{"
381                                         << "name=\"" << names[schemdata[i].getContent()]
382                                         << "\", prob=" << (u16)probability * 2
383                                         << ", param2=" << (u16)schemdata[i].param2;
384
385                                 if (force_place)
386                                         ss << ", force_place=true";
387
388                                 ss << "}," << std::endl;
389                         }
390                 }
391
392                 ss << indent << "}," << std::endl;
393         }
394
395         ss << "}" << std::endl;
396
397         return true;
398 }
399
400
401 bool Schematic::loadSchematicFromFile(const std::string &filename,
402         INodeDefManager *ndef, StringMap *replace_names)
403 {
404         std::ifstream is(filename.c_str(), std::ios_base::binary);
405         if (!is.good()) {
406                 errorstream << "Schematic::loadSchematicFile: unable to open file '"
407                         << filename << "'" << std::endl;
408                 return false;
409         }
410
411         size_t origsize = m_nodenames.size();
412         if (!deserializeFromMts(&is, &m_nodenames))
413                 return false;
414
415         if (replace_names) {
416                 for (size_t i = origsize; i != m_nodenames.size(); i++) {
417                         std::string &name = m_nodenames[i];
418                         StringMap::iterator it = replace_names->find(name);
419                         if (it != replace_names->end())
420                                 name = it->second;
421                 }
422         }
423
424         m_nnlistsizes.push_back(m_nodenames.size() - origsize);
425
426         if (ndef)
427                 ndef->pendNodeResolve(this);
428
429         return true;
430 }
431
432
433 bool Schematic::saveSchematicToFile(const std::string &filename,
434         INodeDefManager *ndef)
435 {
436         MapNode *orig_schemdata = schemdata;
437         std::vector<std::string> ndef_nodenames;
438         std::vector<std::string> *names;
439
440         if (m_resolve_done && ndef == NULL)
441                 ndef = m_ndef;
442
443         if (ndef) {
444                 names = &ndef_nodenames;
445
446                 u32 volume = size.X * size.Y * size.Z;
447                 schemdata = new MapNode[volume];
448                 for (u32 i = 0; i != volume; i++)
449                         schemdata[i] = orig_schemdata[i];
450
451                 generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
452         } else { // otherwise, use the names we have on hand in the list
453                 names = &m_nodenames;
454         }
455
456         std::ostringstream os(std::ios_base::binary);
457         bool status = serializeToMts(&os, *names);
458
459         if (ndef) {
460                 delete []schemdata;
461                 schemdata = orig_schemdata;
462         }
463
464         if (!status)
465                 return false;
466
467         return fs::safeWriteToFile(filename, os.str());
468 }
469
470
471 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
472 {
473         MMVManip *vm = new MMVManip(map);
474
475         v3s16 bp1 = getNodeBlockPos(p1);
476         v3s16 bp2 = getNodeBlockPos(p2);
477         vm->initialEmerge(bp1, bp2);
478
479         size = p2 - p1 + 1;
480
481         slice_probs = new u8[size.Y];
482         for (s16 y = 0; y != size.Y; y++)
483                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
484
485         schemdata = new MapNode[size.X * size.Y * size.Z];
486
487         u32 i = 0;
488         for (s16 z = p1.Z; z <= p2.Z; z++)
489         for (s16 y = p1.Y; y <= p2.Y; y++) {
490                 u32 vi = vm->m_area.index(p1.X, y, z);
491                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
492                         schemdata[i] = vm->m_data[vi];
493                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
494                 }
495         }
496
497         delete vm;
498         return true;
499 }
500
501
502 void Schematic::applyProbabilities(v3s16 p0,
503         std::vector<std::pair<v3s16, u8> > *plist,
504         std::vector<std::pair<s16, u8> > *splist)
505 {
506         for (size_t i = 0; i != plist->size(); i++) {
507                 v3s16 p = (*plist)[i].first - p0;
508                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
509                 if (index < size.Z * size.Y * size.X) {
510                         u8 prob = (*plist)[i].second;
511                         schemdata[index].param1 = prob;
512
513                         // trim unnecessary node names from schematic
514                         if (prob == MTSCHEM_PROB_NEVER)
515                                 schemdata[index].setContent(CONTENT_AIR);
516                 }
517         }
518
519         for (size_t i = 0; i != splist->size(); i++) {
520                 s16 y = (*splist)[i].first - p0.Y;
521                 slice_probs[y] = (*splist)[i].second;
522         }
523 }
524
525
526 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
527         std::vector<std::string> *usednodes, INodeDefManager *ndef)
528 {
529         std::map<content_t, content_t> nodeidmap;
530         content_t numids = 0;
531
532         for (size_t i = 0; i != nodecount; i++) {
533                 content_t id;
534                 content_t c = nodes[i].getContent();
535
536                 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
537                 if (it == nodeidmap.end()) {
538                         id = numids;
539                         numids++;
540
541                         usednodes->push_back(ndef->get(c).name);
542                         nodeidmap.insert(std::make_pair(c, id));
543                 } else {
544                         id = it->second;
545                 }
546                 nodes[i].setContent(id);
547         }
548 }