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