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