]> git.lizzy.rs Git - minetest.git/blob - src/mg_schematic.cpp
Remove unused variable Client::m_active_blocks
[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,
98         bool force_placement, INodeDefManager *ndef)
99 {
100         int xstride = 1;
101         int ystride = size.X;
102         int zstride = size.X * size.Y;
103
104         s16 sx = size.X;
105         s16 sy = size.Y;
106         s16 sz = size.Z;
107
108         int i_start, i_step_x, i_step_z;
109         switch (rot) {
110                 case ROTATE_90:
111                         i_start  = sx - 1;
112                         i_step_x = zstride;
113                         i_step_z = -xstride;
114                         SWAP(s16, sx, sz);
115                         break;
116                 case ROTATE_180:
117                         i_start  = zstride * (sz - 1) + sx - 1;
118                         i_step_x = -xstride;
119                         i_step_z = -zstride;
120                         break;
121                 case ROTATE_270:
122                         i_start  = zstride * (sz - 1);
123                         i_step_x = -zstride;
124                         i_step_z = xstride;
125                         SWAP(s16, sx, sz);
126                         break;
127                 default:
128                         i_start  = 0;
129                         i_step_x = xstride;
130                         i_step_z = zstride;
131         }
132
133         s16 y_map = p.Y;
134         for (s16 y = 0; y != sy; y++) {
135                 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
136                         myrand_range(1, 255) > slice_probs[y])
137                         continue;
138
139                 for (s16 z = 0; z != sz; z++) {
140                         u32 i = z * i_step_z + y * ystride + i_start;
141                         for (s16 x = 0; x != sx; x++, i += i_step_x) {
142                                 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
143                                 if (!vm->m_area.contains(vi))
144                                         continue;
145
146                                 if (schemdata[i].getContent() == CONTENT_IGNORE)
147                                         continue;
148
149                                 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
150                                         continue;
151
152                                 if (!force_placement) {
153                                         content_t c = vm->m_data[vi].getContent();
154                                         if (c != CONTENT_AIR && c != CONTENT_IGNORE)
155                                                 continue;
156                                 }
157
158                                 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
159                                         myrand_range(1, 255) > schemdata[i].param1)
160                                         continue;
161
162                                 vm->m_data[vi] = schemdata[i];
163                                 vm->m_data[vi].param1 = 0;
164
165                                 if (rot)
166                                         vm->m_data[vi].rotateAlongYAxis(ndef, rot);
167                         }
168                 }
169                 y_map++;
170         }
171 }
172
173
174 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot,
175         bool force_placement, INodeDefManager *ndef)
176 {
177         assert(schemdata != NULL); // Pre-condition
178         MMVManip *vm = new MMVManip(map);
179
180         if (rot == ROTATE_RAND)
181                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
182
183         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
184                                 v3s16(size.Z, size.Y, size.X) : size;
185
186         if (flags & DECO_PLACE_CENTER_X)
187                 p.X -= (s.X + 1) / 2;
188         if (flags & DECO_PLACE_CENTER_Y)
189                 p.Y -= (s.Y + 1) / 2;
190         if (flags & DECO_PLACE_CENTER_Z)
191                 p.Z -= (s.Z + 1) / 2;
192
193         v3s16 bp1 = getNodeBlockPos(p);
194         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
195         vm->initialEmerge(bp1, bp2);
196
197         blitToVManip(p, vm, rot, force_placement, ndef);
198
199         std::map<v3s16, MapBlock *> lighting_modified_blocks;
200         std::map<v3s16, MapBlock *> modified_blocks;
201         vm->blitBackAll(&modified_blocks);
202
203         // TODO: Optimize this by using Mapgen::calcLighting() instead
204         lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
205         map->updateLighting(lighting_modified_blocks, modified_blocks);
206
207         MapEditEvent event;
208         event.type = MEET_OTHER;
209         for (std::map<v3s16, MapBlock *>::iterator
210                 it = modified_blocks.begin();
211                 it != modified_blocks.end(); ++it)
212                 event.modified_blocks.insert(it->first);
213
214         map->dispatchEvent(&event);
215 }
216
217
218 bool Schematic::deserializeFromMts(std::istream *is, std::vector<std::string> *names)
219 {
220         std::istream &ss = *is;
221         content_t cignore = CONTENT_IGNORE;
222         bool have_cignore = false;
223
224         u32 signature = readU32(ss);
225         if (signature != MTSCHEM_FILE_SIGNATURE) {
226                 errorstream << "Schematic::deserializeFromMts: invalid schematic "
227                         "file" << std::endl;
228                 return false;
229         }
230
231         u16 version = readU16(ss);
232         if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
233                 errorstream << "Schematic::deserializeFromMts: unsupported schematic "
234                         "file version" << std::endl;
235                 return false;
236         }
237
238         size = readV3S16(ss);
239
240         delete []slice_probs;
241         slice_probs = new u8[size.Y];
242         for (int y = 0; y != size.Y; y++)
243                 slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS;
244
245         u16 nidmapcount = readU16(ss);
246         for (int i = 0; i != nidmapcount; i++) {
247                 std::string name = deSerializeString(ss);
248
249                 // Instances of "ignore" from ver 1 are converted to air (and instances
250                 // are fixed to have MTSCHEM_PROB_NEVER later on).
251                 if (name == "ignore") {
252                         name = "air";
253                         cignore = i;
254                         have_cignore = true;
255                 }
256
257                 names->push_back(name);
258         }
259
260         size_t nodecount = size.X * size.Y * size.Z;
261
262         delete []schemdata;
263         schemdata = new MapNode[nodecount];
264
265         MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
266                 nodecount, 2, 2, true);
267
268         // fix any probability values for nodes that were ignore
269         if (version == 1) {
270                 for (size_t i = 0; i != nodecount; i++) {
271                         if (schemdata[i].param1 == 0)
272                                 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
273                         if (have_cignore && schemdata[i].getContent() == cignore)
274                                 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
275                 }
276         }
277
278         return true;
279 }
280
281
282 bool Schematic::serializeToMts(std::ostream *os)
283 {
284         std::ostream &ss = *os;
285
286         writeU32(ss, MTSCHEM_FILE_SIGNATURE);         // signature
287         writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
288         writeV3S16(ss, size);                         // schematic size
289
290         for (int y = 0; y != size.Y; y++)             // Y slice probabilities
291                 writeU8(ss, slice_probs[y]);
292
293         std::vector<content_t> usednodes;
294         int nodecount = size.X * size.Y * size.Z;
295         build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
296
297         u16 numids = usednodes.size();
298         writeU16(ss, numids); // name count
299         for (int i = 0; i != numids; i++)
300                 ss << serializeString(getNodeName(usednodes[i])); // node names
301
302         // compressed bulk node data
303         MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
304                 schemdata, nodecount, 2, 2, true);
305
306         return true;
307 }
308
309
310 bool Schematic::serializeToLua(std::ostream *os, bool use_comments)
311 {
312         std::ostream &ss = *os;
313
314         //// Write header
315         {
316                 ss << "schematic = {" << std::endl;
317                 ss << "\tsize = "
318                         << "{x=" << size.X
319                         << ", y=" << size.Y
320                         << ", z=" << size.Z
321                         << "}," << std::endl;
322         }
323
324         //// Write y-slice probabilities
325         {
326                 ss << "\tyslice_prob = {" << std::endl;
327
328                 for (u16 y = 0; y != size.Y; y++) {
329                         ss << "\t\t{"
330                                 << "ypos=" << y
331                                 << ", prob=" << (u16)slice_probs[y]
332                                 << "}," << std::endl;
333                 }
334
335                 ss << "\t}," << std::endl;
336         }
337
338         //// Write node data
339         {
340                 ss << "\tdata = {" << std::endl;
341
342                 u32 i = 0;
343                 for (u16 z = 0; z != size.Z; z++)
344                 for (u16 y = 0; y != size.Y; y++) {
345                         if (use_comments) {
346                                 ss << std::endl
347                                         << "\t\t-- z=" << z
348                                         << ", y=" << y << std::endl;
349                         }
350
351                         for (u16 x = 0; x != size.X; x++, i++) {
352                                 ss << "\t\t{"
353                                         << "name=\"" << getNodeName(schemdata[i].getContent())
354                                         << "\", param1=" << (u16)schemdata[i].param1
355                                         << ", param2=" << (u16)schemdata[i].param2
356                                         << "}," << std::endl;
357                         }
358                 }
359
360                 ss << "\t}," << std::endl;
361         }
362
363         ss << "}" << std::endl;
364
365         return true;
366 }
367
368
369 bool Schematic::loadSchematicFromFile(const std::string &filename,
370         INodeDefManager *ndef, StringMap *replace_names,
371         NodeResolveMethod resolve_method)
372 {
373         std::ifstream is(filename.c_str(), std::ios_base::binary);
374         if (!is.good()) {
375                 errorstream << "Schematic::loadSchematicFile: unable to open file '"
376                         << filename << "'" << std::endl;
377                 return false;
378         }
379
380         size_t origsize = m_nodenames.size();
381         if (!deserializeFromMts(&is, &m_nodenames))
382                 return false;
383
384         if (replace_names) {
385                 for (size_t i = origsize; i != m_nodenames.size(); i++) {
386                         std::string &name = m_nodenames[i];
387                         StringMap::iterator it = replace_names->find(name);
388                         if (it != replace_names->end())
389                                 name = it->second;
390                 }
391         }
392
393         m_nnlistsizes.push_back(m_nodenames.size() - origsize);
394
395         ndef->pendNodeResolve(this, resolve_method);
396
397         return true;
398 }
399
400
401 bool Schematic::saveSchematicToFile(const std::string &filename)
402 {
403         std::ostringstream os(std::ios_base::binary);
404         serializeToMts(&os);
405         return fs::safeWriteToFile(filename, os.str());
406 }
407
408
409 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
410 {
411         MMVManip *vm = new MMVManip(map);
412
413         v3s16 bp1 = getNodeBlockPos(p1);
414         v3s16 bp2 = getNodeBlockPos(p2);
415         vm->initialEmerge(bp1, bp2);
416
417         size = p2 - p1 + 1;
418
419         slice_probs = new u8[size.Y];
420         for (s16 y = 0; y != size.Y; y++)
421                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
422
423         schemdata = new MapNode[size.X * size.Y * size.Z];
424
425         u32 i = 0;
426         for (s16 z = p1.Z; z <= p2.Z; z++)
427         for (s16 y = p1.Y; y <= p2.Y; y++) {
428                 u32 vi = vm->m_area.index(p1.X, y, z);
429                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
430                         schemdata[i] = vm->m_data[vi];
431                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
432                 }
433         }
434
435         delete vm;
436         return true;
437 }
438
439
440 void Schematic::applyProbabilities(v3s16 p0,
441         std::vector<std::pair<v3s16, u8> > *plist,
442         std::vector<std::pair<s16, u8> > *splist)
443 {
444         for (size_t i = 0; i != plist->size(); i++) {
445                 v3s16 p = (*plist)[i].first - p0;
446                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
447                 if (index < size.Z * size.Y * size.X) {
448                         u8 prob = (*plist)[i].second;
449                         schemdata[index].param1 = prob;
450
451                         // trim unnecessary node names from schematic
452                         if (prob == MTSCHEM_PROB_NEVER)
453                                 schemdata[index].setContent(CONTENT_AIR);
454                 }
455         }
456
457         for (size_t i = 0; i != splist->size(); i++) {
458                 s16 y = (*splist)[i].first - p0.Y;
459                 slice_probs[y] = (*splist)[i].second;
460         }
461 }
462
463
464 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
465         std::vector<content_t> *usednodes)
466 {
467         std::map<content_t, content_t> nodeidmap;
468         content_t numids = 0;
469
470         for (u32 i = 0; i != nodecount; i++) {
471                 content_t id;
472                 content_t c = nodes[i].getContent();
473
474                 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
475                 if (it == nodeidmap.end()) {
476                         id = numids;
477                         numids++;
478
479                         usednodes->push_back(c);
480                         nodeidmap.insert(std::make_pair(c, id));
481                 } else {
482                         id = it->second;
483                 }
484                 nodes[i].setContent(id);
485         }
486 }