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