]> git.lizzy.rs Git - minetest.git/blob - src/mapblock.cpp
Move client-specific files to 'src/client' (#7902)
[minetest.git] / src / mapblock.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "mapblock.h"
21
22 #include <sstream>
23 #include "map.h"
24 #include "light.h"
25 #include "nodedef.h"
26 #include "nodemetadata.h"
27 #include "gamedef.h"
28 #include "log.h"
29 #include "nameidmapping.h"
30 #include "content_mapnode.h" // For legacy name-id mapping
31 #include "content_nodemeta.h" // For legacy deserialization
32 #include "serialization.h"
33 #ifndef SERVER
34 #include "client/mapblock_mesh.h"
35 #endif
36 #include "porting.h"
37 #include "util/string.h"
38 #include "util/serialize.h"
39 #include "util/basic_macros.h"
40
41 static const char *modified_reason_strings[] = {
42         "initial",
43         "reallocate",
44         "setIsUnderground",
45         "setLightingExpired",
46         "setGenerated",
47         "setNode",
48         "setNodeNoCheck",
49         "setTimestamp",
50         "NodeMetaRef::reportMetadataChange",
51         "clearAllObjects",
52         "Timestamp expired (step)",
53         "addActiveObjectRaw",
54         "removeRemovedObjects/remove",
55         "removeRemovedObjects/deactivate",
56         "Stored list cleared in activateObjects due to overflow",
57         "deactivateFarObjects: Static data moved in",
58         "deactivateFarObjects: Static data moved out",
59         "deactivateFarObjects: Static data changed considerably",
60         "finishBlockMake: expireDayNightDiff",
61         "unknown",
62 };
63
64
65 /*
66         MapBlock
67 */
68
69 MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
70                 m_parent(parent),
71                 m_pos(pos),
72                 m_pos_relative(pos * MAP_BLOCKSIZE),
73                 m_gamedef(gamedef)
74 {
75         if (!dummy)
76                 reallocate();
77 }
78
79 MapBlock::~MapBlock()
80 {
81 #ifndef SERVER
82         {
83                 delete mesh;
84                 mesh = nullptr;
85         }
86 #endif
87
88         delete[] data;
89 }
90
91 bool MapBlock::isValidPositionParent(v3s16 p)
92 {
93         if (isValidPosition(p)) {
94                 return true;
95         }
96
97         return m_parent->isValidPosition(getPosRelative() + p);
98 }
99
100 MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
101 {
102         if (!isValidPosition(p))
103                 return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
104
105         if (!data) {
106                 if (is_valid_position)
107                         *is_valid_position = false;
108                 return {CONTENT_IGNORE};
109         }
110         if (is_valid_position)
111                 *is_valid_position = true;
112         return data[p.Z * zstride + p.Y * ystride + p.X];
113 }
114
115 std::string MapBlock::getModifiedReasonString()
116 {
117         std::string reason;
118
119         const u32 ubound = MYMIN(sizeof(m_modified_reason) * CHAR_BIT,
120                 ARRLEN(modified_reason_strings));
121
122         for (u32 i = 0; i != ubound; i++) {
123                 if ((m_modified_reason & (1 << i)) == 0)
124                         continue;
125
126                 reason += modified_reason_strings[i];
127                 reason += ", ";
128         }
129
130         if (reason.length() > 2)
131                 reason.resize(reason.length() - 2);
132
133         return reason;
134 }
135
136
137 void MapBlock::copyTo(VoxelManipulator &dst)
138 {
139         v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
140         VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
141
142         // Copy from data to VoxelManipulator
143         dst.copyFrom(data, data_area, v3s16(0,0,0),
144                         getPosRelative(), data_size);
145 }
146
147 void MapBlock::copyFrom(VoxelManipulator &dst)
148 {
149         v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
150         VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
151
152         // Copy from VoxelManipulator to data
153         dst.copyTo(data, data_area, v3s16(0,0,0),
154                         getPosRelative(), data_size);
155 }
156
157 void MapBlock::actuallyUpdateDayNightDiff()
158 {
159         const NodeDefManager *nodemgr = m_gamedef->ndef();
160
161         // Running this function un-expires m_day_night_differs
162         m_day_night_differs_expired = false;
163
164         if (!data) {
165                 m_day_night_differs = false;
166                 return;
167         }
168
169         bool differs = false;
170
171         /*
172                 Check if any lighting value differs
173         */
174
175         MapNode previous_n(CONTENT_IGNORE);
176         for (u32 i = 0; i < nodecount; i++) {
177                 MapNode n = data[i];
178
179                 // If node is identical to previous node, don't verify if it differs
180                 if (n == previous_n)
181                         continue;
182
183                 differs = !n.isLightDayNightEq(nodemgr);
184                 if (differs)
185                         break;
186                 previous_n = n;
187         }
188
189         /*
190                 If some lighting values differ, check if the whole thing is
191                 just air. If it is just air, differs = false
192         */
193         if (differs) {
194                 bool only_air = true;
195                 for (u32 i = 0; i < nodecount; i++) {
196                         MapNode &n = data[i];
197                         if (n.getContent() != CONTENT_AIR) {
198                                 only_air = false;
199                                 break;
200                         }
201                 }
202                 if (only_air)
203                         differs = false;
204         }
205
206         // Set member variable
207         m_day_night_differs = differs;
208 }
209
210 void MapBlock::expireDayNightDiff()
211 {
212         if (!data) {
213                 m_day_night_differs = false;
214                 m_day_night_differs_expired = false;
215                 return;
216         }
217
218         m_day_night_differs_expired = true;
219 }
220
221 s16 MapBlock::getGroundLevel(v2s16 p2d)
222 {
223         if(isDummy())
224                 return -3;
225         try
226         {
227                 s16 y = MAP_BLOCKSIZE-1;
228                 for(; y>=0; y--)
229                 {
230                         MapNode n = getNodeRef(p2d.X, y, p2d.Y);
231                         if (m_gamedef->ndef()->get(n).walkable) {
232                                 if(y == MAP_BLOCKSIZE-1)
233                                         return -2;
234
235                                 return y;
236                         }
237                 }
238                 return -1;
239         }
240         catch(InvalidPositionException &e)
241         {
242                 return -3;
243         }
244 }
245
246 /*
247         Serialization
248 */
249 // List relevant id-name pairs for ids in the block using nodedef
250 // Renumbers the content IDs (starting at 0 and incrementing
251 // use static memory requires about 65535 * sizeof(int) ram in order to be
252 // sure we can handle all content ids. But it's absolutely worth it as it's
253 // a speedup of 4 for one of the major time consuming functions on storing
254 // mapblocks.
255 static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
256 static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
257         const NodeDefManager *nodedef)
258 {
259         memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
260
261         std::set<content_t> unknown_contents;
262         content_t id_counter = 0;
263         for (u32 i = 0; i < MapBlock::nodecount; i++) {
264                 content_t global_id = nodes[i].getContent();
265                 content_t id = CONTENT_IGNORE;
266
267                 // Try to find an existing mapping
268                 if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
269                         id = getBlockNodeIdMapping_mapping[global_id];
270                 }
271                 else
272                 {
273                         // We have to assign a new mapping
274                         id = id_counter++;
275                         getBlockNodeIdMapping_mapping[global_id] = id;
276
277                         const ContentFeatures &f = nodedef->get(global_id);
278                         const std::string &name = f.name;
279                         if (name.empty())
280                                 unknown_contents.insert(global_id);
281                         else
282                                 nimap->set(id, name);
283                 }
284
285                 // Update the MapNode
286                 nodes[i].setContent(id);
287         }
288         for (u16 unknown_content : unknown_contents) {
289                 errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
290                                 << "Name for node id " << unknown_content << " not known" << std::endl;
291         }
292 }
293 // Correct ids in the block to match nodedef based on names.
294 // Unknown ones are added to nodedef.
295 // Will not update itself to match id-name pairs in nodedef.
296 static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
297                 IGameDef *gamedef)
298 {
299         const NodeDefManager *nodedef = gamedef->ndef();
300         // This means the block contains incorrect ids, and we contain
301         // the information to convert those to names.
302         // nodedef contains information to convert our names to globally
303         // correct ids.
304         std::unordered_set<content_t> unnamed_contents;
305         std::unordered_set<std::string> unallocatable_contents;
306
307         bool previous_exists = false;
308         content_t previous_local_id = CONTENT_IGNORE;
309         content_t previous_global_id = CONTENT_IGNORE;
310
311         for (u32 i = 0; i < MapBlock::nodecount; i++) {
312                 content_t local_id = nodes[i].getContent();
313                 // If previous node local_id was found and same than before, don't lookup maps
314                 // apply directly previous resolved id
315                 // This permits to massively improve loading performance when nodes are similar
316                 // example: default:air, default:stone are massively present
317                 if (previous_exists && local_id == previous_local_id) {
318                         nodes[i].setContent(previous_global_id);
319                         continue;
320                 }
321
322                 std::string name;
323                 if (!nimap->getName(local_id, name)) {
324                         unnamed_contents.insert(local_id);
325                         previous_exists = false;
326                         continue;
327                 }
328
329                 content_t global_id;
330                 if (!nodedef->getId(name, global_id)) {
331                         global_id = gamedef->allocateUnknownNodeId(name);
332                         if (global_id == CONTENT_IGNORE) {
333                                 unallocatable_contents.insert(name);
334                                 previous_exists = false;
335                                 continue;
336                         }
337                 }
338                 nodes[i].setContent(global_id);
339
340                 // Save previous node local_id & global_id result
341                 previous_local_id = local_id;
342                 previous_global_id = global_id;
343                 previous_exists = true;
344         }
345
346         for (const content_t c: unnamed_contents) {
347                 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
348                                 << "Block contains id " << c
349                                 << " with no name mapping" << std::endl;
350         }
351         for (const std::string &node_name: unallocatable_contents) {
352                 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
353                                 << "Could not allocate global id for node name \""
354                                 << node_name << "\"" << std::endl;
355         }
356 }
357
358 void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
359 {
360         if(!ser_ver_supported(version))
361                 throw VersionMismatchException("ERROR: MapBlock format not supported");
362
363         if (!data)
364                 throw SerializationError("ERROR: Not writing dummy block.");
365
366         FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
367
368         // First byte
369         u8 flags = 0;
370         if(is_underground)
371                 flags |= 0x01;
372         if(getDayNightDiff())
373                 flags |= 0x02;
374         if (!m_generated)
375                 flags |= 0x08;
376         writeU8(os, flags);
377         if (version >= 27) {
378                 writeU16(os, m_lighting_complete);
379         }
380
381         /*
382                 Bulk node data
383         */
384         NameIdMapping nimap;
385         if(disk)
386         {
387                 MapNode *tmp_nodes = new MapNode[nodecount];
388                 for(u32 i=0; i<nodecount; i++)
389                         tmp_nodes[i] = data[i];
390                 getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
391
392                 u8 content_width = 2;
393                 u8 params_width = 2;
394                 writeU8(os, content_width);
395                 writeU8(os, params_width);
396                 MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
397                                 content_width, params_width, true);
398                 delete[] tmp_nodes;
399         }
400         else
401         {
402                 u8 content_width = 2;
403                 u8 params_width = 2;
404                 writeU8(os, content_width);
405                 writeU8(os, params_width);
406                 MapNode::serializeBulk(os, version, data, nodecount,
407                                 content_width, params_width, true);
408         }
409
410         /*
411                 Node metadata
412         */
413         std::ostringstream oss(std::ios_base::binary);
414         m_node_metadata.serialize(oss, version, disk);
415         compressZlib(oss.str(), os);
416
417         /*
418                 Data that goes to disk, but not the network
419         */
420         if(disk)
421         {
422                 if(version <= 24){
423                         // Node timers
424                         m_node_timers.serialize(os, version);
425                 }
426
427                 // Static objects
428                 m_static_objects.serialize(os);
429
430                 // Timestamp
431                 writeU32(os, getTimestamp());
432
433                 // Write block-specific node definition id mapping
434                 nimap.serialize(os);
435
436                 if(version >= 25){
437                         // Node timers
438                         m_node_timers.serialize(os, version);
439                 }
440         }
441 }
442
443 void MapBlock::serializeNetworkSpecific(std::ostream &os)
444 {
445         if (!data) {
446                 throw SerializationError("ERROR: Not writing dummy block.");
447         }
448
449         writeU8(os, 1); // version
450         writeF1000(os, 0); // deprecated heat
451         writeF1000(os, 0); // deprecated humidity
452 }
453
454 void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
455 {
456         if(!ser_ver_supported(version))
457                 throw VersionMismatchException("ERROR: MapBlock format not supported");
458
459         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
460
461         m_day_night_differs_expired = false;
462
463         if(version <= 21)
464         {
465                 deSerialize_pre22(is, version, disk);
466                 return;
467         }
468
469         u8 flags = readU8(is);
470         is_underground = (flags & 0x01) != 0;
471         m_day_night_differs = (flags & 0x02) != 0;
472         if (version < 27)
473                 m_lighting_complete = 0xFFFF;
474         else
475                 m_lighting_complete = readU16(is);
476         m_generated = (flags & 0x08) == 0;
477
478         /*
479                 Bulk node data
480         */
481         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
482                         <<": Bulk node data"<<std::endl);
483         u8 content_width = readU8(is);
484         u8 params_width = readU8(is);
485         if(content_width != 1 && content_width != 2)
486                 throw SerializationError("MapBlock::deSerialize(): invalid content_width");
487         if(params_width != 2)
488                 throw SerializationError("MapBlock::deSerialize(): invalid params_width");
489         MapNode::deSerializeBulk(is, version, data, nodecount,
490                         content_width, params_width, true);
491
492         /*
493                 NodeMetadata
494         */
495         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
496                         <<": Node metadata"<<std::endl);
497         // Ignore errors
498         try {
499                 std::ostringstream oss(std::ios_base::binary);
500                 decompressZlib(is, oss);
501                 std::istringstream iss(oss.str(), std::ios_base::binary);
502                 if (version >= 23)
503                         m_node_metadata.deSerialize(iss, m_gamedef->idef());
504                 else
505                         content_nodemeta_deserialize_legacy(iss,
506                                 &m_node_metadata, &m_node_timers,
507                                 m_gamedef->idef());
508         } catch(SerializationError &e) {
509                 warningstream<<"MapBlock::deSerialize(): Ignoring an error"
510                                 <<" while deserializing node metadata at ("
511                                 <<PP(getPos())<<": "<<e.what()<<std::endl;
512         }
513
514         /*
515                 Data that is only on disk
516         */
517         if(disk)
518         {
519                 // Node timers
520                 if(version == 23){
521                         // Read unused zero
522                         readU8(is);
523                 }
524                 if(version == 24){
525                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
526                                         <<": Node timers (ver==24)"<<std::endl);
527                         m_node_timers.deSerialize(is, version);
528                 }
529
530                 // Static objects
531                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
532                                 <<": Static objects"<<std::endl);
533                 m_static_objects.deSerialize(is);
534
535                 // Timestamp
536                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
537                                 <<": Timestamp"<<std::endl);
538                 setTimestamp(readU32(is));
539                 m_disk_timestamp = m_timestamp;
540
541                 // Dynamically re-set ids based on node names
542                 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
543                                 <<": NameIdMapping"<<std::endl);
544                 NameIdMapping nimap;
545                 nimap.deSerialize(is);
546                 correctBlockNodeIds(&nimap, data, m_gamedef);
547
548                 if(version >= 25){
549                         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
550                                         <<": Node timers (ver>=25)"<<std::endl);
551                         m_node_timers.deSerialize(is, version);
552                 }
553         }
554
555         TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
556                         <<": Done."<<std::endl);
557 }
558
559 void MapBlock::deSerializeNetworkSpecific(std::istream &is)
560 {
561         try {
562                 int version = readU8(is);
563                 //if(version != 1)
564                 //      throw SerializationError("unsupported MapBlock version");
565                 if(version >= 1) {
566                         readF1000(is); // deprecated heat
567                         readF1000(is); // deprecated humidity
568                 }
569         }
570         catch(SerializationError &e)
571         {
572                 warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
573                                 <<": "<<e.what()<<std::endl;
574         }
575 }
576
577 /*
578         Legacy serialization
579 */
580
581 void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
582 {
583         // Initialize default flags
584         is_underground = false;
585         m_day_night_differs = false;
586         m_lighting_complete = 0xFFFF;
587         m_generated = true;
588
589         // Make a temporary buffer
590         u32 ser_length = MapNode::serializedLength(version);
591         SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
592
593         // These have no compression
594         if (version <= 3 || version == 5 || version == 6) {
595                 char tmp;
596                 is.read(&tmp, 1);
597                 if (is.gcount() != 1)
598                         throw SerializationError(std::string(FUNCTION_NAME)
599                                 + ": not enough input data");
600                 is_underground = tmp;
601                 is.read((char *)*databuf_nodelist, nodecount * ser_length);
602                 if ((u32)is.gcount() != nodecount * ser_length)
603                         throw SerializationError(std::string(FUNCTION_NAME)
604                                 + ": not enough input data");
605         } else if (version <= 10) {
606                 u8 t8;
607                 is.read((char *)&t8, 1);
608                 is_underground = t8;
609
610                 {
611                         // Uncompress and set material data
612                         std::ostringstream os(std::ios_base::binary);
613                         decompress(is, os, version);
614                         std::string s = os.str();
615                         if (s.size() != nodecount)
616                                 throw SerializationError(std::string(FUNCTION_NAME)
617                                         + ": not enough input data");
618                         for (u32 i = 0; i < s.size(); i++) {
619                                 databuf_nodelist[i*ser_length] = s[i];
620                         }
621                 }
622                 {
623                         // Uncompress and set param data
624                         std::ostringstream os(std::ios_base::binary);
625                         decompress(is, os, version);
626                         std::string s = os.str();
627                         if (s.size() != nodecount)
628                                 throw SerializationError(std::string(FUNCTION_NAME)
629                                         + ": not enough input data");
630                         for (u32 i = 0; i < s.size(); i++) {
631                                 databuf_nodelist[i*ser_length + 1] = s[i];
632                         }
633                 }
634
635                 if (version >= 10) {
636                         // Uncompress and set param2 data
637                         std::ostringstream os(std::ios_base::binary);
638                         decompress(is, os, version);
639                         std::string s = os.str();
640                         if (s.size() != nodecount)
641                                 throw SerializationError(std::string(FUNCTION_NAME)
642                                         + ": not enough input data");
643                         for (u32 i = 0; i < s.size(); i++) {
644                                 databuf_nodelist[i*ser_length + 2] = s[i];
645                         }
646                 }
647         } else { // All other versions (10 to 21)
648                 u8 flags;
649                 is.read((char*)&flags, 1);
650                 is_underground = (flags & 0x01) != 0;
651                 m_day_night_differs = (flags & 0x02) != 0;
652                 if(version >= 18)
653                         m_generated = (flags & 0x08) == 0;
654
655                 // Uncompress data
656                 std::ostringstream os(std::ios_base::binary);
657                 decompress(is, os, version);
658                 std::string s = os.str();
659                 if (s.size() != nodecount * 3)
660                         throw SerializationError(std::string(FUNCTION_NAME)
661                                 + ": decompress resulted in size other than nodecount*3");
662
663                 // deserialize nodes from buffer
664                 for (u32 i = 0; i < nodecount; i++) {
665                         databuf_nodelist[i*ser_length] = s[i];
666                         databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
667                         databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
668                 }
669
670                 /*
671                         NodeMetadata
672                 */
673                 if (version >= 14) {
674                         // Ignore errors
675                         try {
676                                 if (version <= 15) {
677                                         std::string data = deSerializeString(is);
678                                         std::istringstream iss(data, std::ios_base::binary);
679                                         content_nodemeta_deserialize_legacy(iss,
680                                                 &m_node_metadata, &m_node_timers,
681                                                 m_gamedef->idef());
682                                 } else {
683                                         //std::string data = deSerializeLongString(is);
684                                         std::ostringstream oss(std::ios_base::binary);
685                                         decompressZlib(is, oss);
686                                         std::istringstream iss(oss.str(), std::ios_base::binary);
687                                         content_nodemeta_deserialize_legacy(iss,
688                                                 &m_node_metadata, &m_node_timers,
689                                                 m_gamedef->idef());
690                                 }
691                         } catch(SerializationError &e) {
692                                 warningstream<<"MapBlock::deSerialize(): Ignoring an error"
693                                                 <<" while deserializing node metadata"<<std::endl;
694                         }
695                 }
696         }
697
698         // Deserialize node data
699         for (u32 i = 0; i < nodecount; i++) {
700                 data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
701         }
702
703         if (disk) {
704                 /*
705                         Versions up from 9 have block objects. (DEPRECATED)
706                 */
707                 if (version >= 9) {
708                         u16 count = readU16(is);
709                         // Not supported and length not known if count is not 0
710                         if(count != 0){
711                                 warningstream<<"MapBlock::deSerialize_pre22(): "
712                                                 <<"Ignoring stuff coming at and after MBOs"<<std::endl;
713                                 return;
714                         }
715                 }
716
717                 /*
718                         Versions up from 15 have static objects.
719                 */
720                 if (version >= 15)
721                         m_static_objects.deSerialize(is);
722
723                 // Timestamp
724                 if (version >= 17) {
725                         setTimestamp(readU32(is));
726                         m_disk_timestamp = m_timestamp;
727                 } else {
728                         setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
729                 }
730
731                 // Dynamically re-set ids based on node names
732                 NameIdMapping nimap;
733                 // If supported, read node definition id mapping
734                 if (version >= 21) {
735                         nimap.deSerialize(is);
736                 // Else set the legacy mapping
737                 } else {
738                         content_mapnode_get_name_id_mapping(&nimap);
739                 }
740                 correctBlockNodeIds(&nimap, data, m_gamedef);
741         }
742
743
744         // Legacy data changes
745         // This code has to convert from pre-22 to post-22 format.
746         const NodeDefManager *nodedef = m_gamedef->ndef();
747         for(u32 i=0; i<nodecount; i++)
748         {
749                 const ContentFeatures &f = nodedef->get(data[i].getContent());
750                 // Mineral
751                 if(nodedef->getId("default:stone") == data[i].getContent()
752                                 && data[i].getParam1() == 1)
753                 {
754                         data[i].setContent(nodedef->getId("default:stone_with_coal"));
755                         data[i].setParam1(0);
756                 }
757                 else if(nodedef->getId("default:stone") == data[i].getContent()
758                                 && data[i].getParam1() == 2)
759                 {
760                         data[i].setContent(nodedef->getId("default:stone_with_iron"));
761                         data[i].setParam1(0);
762                 }
763                 // facedir_simple
764                 if(f.legacy_facedir_simple)
765                 {
766                         data[i].setParam2(data[i].getParam1());
767                         data[i].setParam1(0);
768                 }
769                 // wall_mounted
770                 if(f.legacy_wallmounted)
771                 {
772                         u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
773                         u8 dir_old_format = data[i].getParam2();
774                         u8 dir_new_format = 0;
775                         for(u8 j=0; j<8; j++)
776                         {
777                                 if((dir_old_format & wallmounted_new_to_old[j]) != 0)
778                                 {
779                                         dir_new_format = j;
780                                         break;
781                                 }
782                         }
783                         data[i].setParam2(dir_new_format);
784                 }
785         }
786
787 }
788
789 /*
790         Get a quick string to describe what a block actually contains
791 */
792 std::string analyze_block(MapBlock *block)
793 {
794         if(block == NULL)
795                 return "NULL";
796
797         std::ostringstream desc;
798
799         v3s16 p = block->getPos();
800         char spos[25];
801         porting::mt_snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
802         desc<<spos;
803
804         switch(block->getModified())
805         {
806         case MOD_STATE_CLEAN:
807                 desc<<"CLEAN,           ";
808                 break;
809         case MOD_STATE_WRITE_AT_UNLOAD:
810                 desc<<"WRITE_AT_UNLOAD, ";
811                 break;
812         case MOD_STATE_WRITE_NEEDED:
813                 desc<<"WRITE_NEEDED,    ";
814                 break;
815         default:
816                 desc<<"unknown getModified()="+itos(block->getModified())+", ";
817         }
818
819         if(block->isGenerated())
820                 desc<<"is_gen [X], ";
821         else
822                 desc<<"is_gen [ ], ";
823
824         if(block->getIsUnderground())
825                 desc<<"is_ug [X], ";
826         else
827                 desc<<"is_ug [ ], ";
828
829         desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
830
831         if(block->isDummy())
832         {
833                 desc<<"Dummy, ";
834         }
835         else
836         {
837                 bool full_ignore = true;
838                 bool some_ignore = false;
839                 bool full_air = true;
840                 bool some_air = false;
841                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
842                 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
843                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
844                 {
845                         v3s16 p(x0,y0,z0);
846                         MapNode n = block->getNodeNoEx(p);
847                         content_t c = n.getContent();
848                         if(c == CONTENT_IGNORE)
849                                 some_ignore = true;
850                         else
851                                 full_ignore = false;
852                         if(c == CONTENT_AIR)
853                                 some_air = true;
854                         else
855                                 full_air = false;
856                 }
857
858                 desc<<"content {";
859
860                 std::ostringstream ss;
861
862                 if(full_ignore)
863                         ss<<"IGNORE (full), ";
864                 else if(some_ignore)
865                         ss<<"IGNORE, ";
866
867                 if(full_air)
868                         ss<<"AIR (full), ";
869                 else if(some_air)
870                         ss<<"AIR, ";
871
872                 if(ss.str().size()>=2)
873                         desc<<ss.str().substr(0, ss.str().size()-2);
874
875                 desc<<"}, ";
876         }
877
878         return desc.str().substr(0, desc.str().size()-2);
879 }
880
881
882 //END