]> git.lizzy.rs Git - minetest.git/blob - src/mg_schematic.cpp
Fix client "double saving" simple singleplayer local maps
[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 const char *SchematicManager::ELEMENT_TITLE = "schematic";
32
33
34 ///////////////////////////////////////////////////////////////////////////////
35
36
37 Schematic::Schematic()
38 {
39         schemdata   = NULL;
40         slice_probs = NULL;
41         flags       = 0;
42         size        = v3s16(0, 0, 0);
43 }
44
45
46 Schematic::~Schematic()
47 {
48         delete []schemdata;
49         delete []slice_probs;
50 }
51
52
53 void Schematic::updateContentIds()
54 {
55         if (flags & SCHEM_CIDS_UPDATED)
56                 return;
57
58         flags |= SCHEM_CIDS_UPDATED;
59
60         for (int i = 0; i != size.X * size.Y * size.Z; i++)
61                 schemdata[i].setContent(c_nodes[schemdata[i].getContent()]);
62 }
63
64
65 void Schematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
66         Rotation rot, bool force_placement, INodeDefManager *ndef)
67 {
68         int xstride = 1;
69         int ystride = size.X;
70         int zstride = size.X * size.Y;
71
72         updateContentIds();
73
74         s16 sx = size.X;
75         s16 sy = size.Y;
76         s16 sz = size.Z;
77
78         int i_start, i_step_x, i_step_z;
79         switch (rot) {
80                 case ROTATE_90:
81                         i_start  = sx - 1;
82                         i_step_x = zstride;
83                         i_step_z = -xstride;
84                         SWAP(s16, sx, sz);
85                         break;
86                 case ROTATE_180:
87                         i_start  = zstride * (sz - 1) + sx - 1;
88                         i_step_x = -xstride;
89                         i_step_z = -zstride;
90                         break;
91                 case ROTATE_270:
92                         i_start  = zstride * (sz - 1);
93                         i_step_x = -zstride;
94                         i_step_z = xstride;
95                         SWAP(s16, sx, sz);
96                         break;
97                 default:
98                         i_start  = 0;
99                         i_step_x = xstride;
100                         i_step_z = zstride;
101         }
102
103         s16 y_map = p.Y;
104         for (s16 y = 0; y != sy; y++) {
105                 if (slice_probs[y] != MTSCHEM_PROB_ALWAYS &&
106                         myrand_range(1, 255) > slice_probs[y])
107                         continue;
108
109                 for (s16 z = 0; z != sz; z++) {
110                         u32 i = z * i_step_z + y * ystride + i_start;
111                         for (s16 x = 0; x != sx; x++, i += i_step_x) {
112                                 u32 vi = vm->m_area.index(p.X + x, y_map, p.Z + z);
113                                 if (!vm->m_area.contains(vi))
114                                         continue;
115
116                                 if (schemdata[i].getContent() == CONTENT_IGNORE)
117                                         continue;
118
119                                 if (schemdata[i].param1 == MTSCHEM_PROB_NEVER)
120                                         continue;
121
122                                 if (!force_placement) {
123                                         content_t c = vm->m_data[vi].getContent();
124                                         if (c != CONTENT_AIR && c != CONTENT_IGNORE)
125                                                 continue;
126                                 }
127
128                                 if (schemdata[i].param1 != MTSCHEM_PROB_ALWAYS &&
129                                         myrand_range(1, 255) > schemdata[i].param1)
130                                         continue;
131
132                                 vm->m_data[vi] = schemdata[i];
133                                 vm->m_data[vi].param1 = 0;
134
135                                 if (rot)
136                                         vm->m_data[vi].rotateAlongYAxis(ndef, rot);
137                         }
138                 }
139                 y_map++;
140         }
141 }
142
143
144 void Schematic::placeStructure(Map *map, v3s16 p, u32 flags,
145         Rotation rot, bool force_placement, INodeDefManager *ndef)
146 {
147         assert(schemdata != NULL);
148         ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
149
150         if (rot == ROTATE_RAND)
151                 rot = (Rotation)myrand_range(ROTATE_0, ROTATE_270);
152
153         v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
154                                 v3s16(size.Z, size.Y, size.X) : size;
155
156         if (flags & DECO_PLACE_CENTER_X)
157                 p.X -= (s.X + 1) / 2;
158         if (flags & DECO_PLACE_CENTER_Y)
159                 p.Y -= (s.Y + 1) / 2;
160         if (flags & DECO_PLACE_CENTER_Z)
161                 p.Z -= (s.Z + 1) / 2;
162
163         v3s16 bp1 = getNodeBlockPos(p);
164         v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
165         vm->initialEmerge(bp1, bp2);
166
167         blitToVManip(p, vm, rot, force_placement, ndef);
168
169         std::map<v3s16, MapBlock *> lighting_modified_blocks;
170         std::map<v3s16, MapBlock *> modified_blocks;
171         vm->blitBackAll(&modified_blocks);
172
173         // TODO: Optimize this by using Mapgen::calcLighting() instead
174         lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
175         map->updateLighting(lighting_modified_blocks, modified_blocks);
176
177         MapEditEvent event;
178         event.type = MEET_OTHER;
179         for (std::map<v3s16, MapBlock *>::iterator
180                 it = modified_blocks.begin();
181                 it != modified_blocks.end(); ++it)
182                 event.modified_blocks.insert(it->first);
183
184         map->dispatchEvent(&event);
185 }
186
187
188 bool Schematic::loadSchematicFromFile(const char *filename,
189         NodeResolver *resolver,
190         std::map<std::string, std::string> &replace_names)
191 {
192         content_t cignore = CONTENT_IGNORE;
193         bool have_cignore = false;
194
195         std::ifstream is(filename, std::ios_base::binary);
196
197         u32 signature = readU32(is);
198         if (signature != MTSCHEM_FILE_SIGNATURE) {
199                 errorstream << "loadSchematicFile: invalid schematic "
200                         "file" << std::endl;
201                 return false;
202         }
203
204         u16 version = readU16(is);
205         if (version > MTSCHEM_FILE_VER_HIGHEST_READ) {
206                 errorstream << "loadSchematicFile: unsupported schematic "
207                         "file version" << std::endl;
208                 return false;
209         }
210
211         size = readV3S16(is);
212
213         delete []slice_probs;
214         slice_probs = new u8[size.Y];
215         for (int y = 0; y != size.Y; y++)
216                 slice_probs[y] = (version >= 3) ? readU8(is) : MTSCHEM_PROB_ALWAYS;
217
218         int nodecount = size.X * size.Y * size.Z;
219
220         u16 nidmapcount = readU16(is);
221
222         for (int i = 0; i != nidmapcount; i++) {
223                 std::string name = deSerializeString(is);
224                 if (name == "ignore") {
225                         name = "air";
226                         cignore = i;
227                         have_cignore = true;
228                 }
229
230                 std::map<std::string, std::string>::iterator it;
231                 it = replace_names.find(name);
232                 if (it != replace_names.end())
233                         name = it->second;
234
235                 resolver->addNodeList(name.c_str(), &c_nodes);
236         }
237
238         delete []schemdata;
239         schemdata = new MapNode[nodecount];
240         MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schemdata,
241                                 nodecount, 2, 2, true);
242
243         if (version == 1) { // fix up the probability values
244                 for (int i = 0; i != nodecount; i++) {
245                         if (schemdata[i].param1 == 0)
246                                 schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
247                         if (have_cignore && schemdata[i].getContent() == cignore)
248                                 schemdata[i].param1 = MTSCHEM_PROB_NEVER;
249                 }
250         }
251
252         return true;
253 }
254
255
256 /*
257         Minetest Schematic File Format
258
259         All values are stored in big-endian byte order.
260         [u32] signature: 'MTSM'
261         [u16] version: 3
262         [u16] size X
263         [u16] size Y
264         [u16] size Z
265         For each Y:
266                 [u8] slice probability value
267         [Name-ID table] Name ID Mapping Table
268                 [u16] name-id count
269                 For each name-id mapping:
270                         [u16] name length
271                         [u8[]] name
272         ZLib deflated {
273         For each node in schematic:  (for z, y, x)
274                 [u16] content
275         For each node in schematic:
276                 [u8] probability of occurance (param1)
277         For each node in schematic:
278                 [u8] param2
279         }
280
281         Version changes:
282         1 - Initial version
283         2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
284         3 - Added y-slice probabilities; this allows for variable height structures
285 */
286 void Schematic::saveSchematicToFile(const char *filename, INodeDefManager *ndef)
287 {
288         std::ostringstream ss(std::ios_base::binary);
289
290         writeU32(ss, MTSCHEM_FILE_SIGNATURE);         // signature
291         writeU16(ss, MTSCHEM_FILE_VER_HIGHEST_WRITE); // version
292         writeV3S16(ss, size);                         // schematic size
293
294         for (int y = 0; y != size.Y; y++)             // Y slice probabilities
295                 writeU8(ss, slice_probs[y]);
296
297         std::vector<content_t> usednodes;
298         int nodecount = size.X * size.Y * size.Z;
299         build_nnlist_and_update_ids(schemdata, nodecount, &usednodes);
300
301         u16 numids = usednodes.size();
302         writeU16(ss, numids); // name count
303         for (int i = 0; i != numids; i++)
304                 ss << serializeString(ndef->get(usednodes[i]).name); // node names
305
306         // compressed bulk node data
307         MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE, schemdata,
308                                 nodecount, 2, 2, true);
309
310         fs::safeWriteToFile(filename, ss.str());
311 }
312
313
314 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
315         std::vector<content_t> *usednodes)
316 {
317         std::map<content_t, content_t> nodeidmap;
318         content_t numids = 0;
319
320         for (u32 i = 0; i != nodecount; i++) {
321                 content_t id;
322                 content_t c = nodes[i].getContent();
323
324                 std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
325                 if (it == nodeidmap.end()) {
326                         id = numids;
327                         numids++;
328
329                         usednodes->push_back(c);
330                         nodeidmap.insert(std::make_pair(c, id));
331                 } else {
332                         id = it->second;
333                 }
334                 nodes[i].setContent(id);
335         }
336 }
337
338
339 bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
340 {
341         ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
342
343         v3s16 bp1 = getNodeBlockPos(p1);
344         v3s16 bp2 = getNodeBlockPos(p2);
345         vm->initialEmerge(bp1, bp2);
346
347         size = p2 - p1 + 1;
348
349         slice_probs = new u8[size.Y];
350         for (s16 y = 0; y != size.Y; y++)
351                 slice_probs[y] = MTSCHEM_PROB_ALWAYS;
352
353         schemdata = new MapNode[size.X * size.Y * size.Z];
354
355         u32 i = 0;
356         for (s16 z = p1.Z; z <= p2.Z; z++)
357         for (s16 y = p1.Y; y <= p2.Y; y++) {
358                 u32 vi = vm->m_area.index(p1.X, y, z);
359                 for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
360                         schemdata[i] = vm->m_data[vi];
361                         schemdata[i].param1 = MTSCHEM_PROB_ALWAYS;
362                 }
363         }
364
365         delete vm;
366         return true;
367 }
368
369
370 void Schematic::applyProbabilities(v3s16 p0,
371         std::vector<std::pair<v3s16, u8> > *plist,
372         std::vector<std::pair<s16, u8> > *splist)
373 {
374         for (size_t i = 0; i != plist->size(); i++) {
375                 v3s16 p = (*plist)[i].first - p0;
376                 int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
377                 if (index < size.Z * size.Y * size.X) {
378                         u8 prob = (*plist)[i].second;
379                         schemdata[index].param1 = prob;
380
381                         // trim unnecessary node names from schematic
382                         if (prob == MTSCHEM_PROB_NEVER)
383                                 schemdata[index].setContent(CONTENT_AIR);
384                 }
385         }
386
387         for (size_t i = 0; i != splist->size(); i++) {
388                 s16 y = (*splist)[i].first - p0.Y;
389                 slice_probs[y] = (*splist)[i].second;
390         }
391 }