3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 #include "nodemetadata.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"
34 #include "mapblock_mesh.h"
36 #include "util/string.h"
37 #include "util/serialize.h"
38 #include "util/basic_macros.h"
40 static const char *modified_reason_strings[] = {
49 "NodeMetaRef::reportMetadataChange",
51 "Timestamp expired (step)",
53 "removeRemovedObjects/remove",
54 "removeRemovedObjects/deactivate",
55 "Stored list cleared in activateObjects due to overflow",
56 "deactivateFarObjects: Static data moved in",
57 "deactivateFarObjects: Static data moved out",
58 "deactivateFarObjects: Static data changed considerably",
59 "finishBlockMake: expireDayNightDiff",
68 MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
71 m_pos_relative(pos * MAP_BLOCKSIZE),
90 bool MapBlock::isValidPositionParent(v3s16 p)
92 if (isValidPosition(p)) {
96 return m_parent->isValidPosition(getPosRelative() + p);
99 MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
101 if (!isValidPosition(p))
102 return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
105 if (is_valid_position)
106 *is_valid_position = false;
107 return {CONTENT_IGNORE};
109 if (is_valid_position)
110 *is_valid_position = true;
111 return data[p.Z * zstride + p.Y * ystride + p.X];
114 std::string MapBlock::getModifiedReasonString()
118 const u32 ubound = MYMIN(sizeof(m_modified_reason) * CHAR_BIT,
119 ARRLEN(modified_reason_strings));
121 for (u32 i = 0; i != ubound; i++) {
122 if ((m_modified_reason & (1 << i)) == 0)
125 reason += modified_reason_strings[i];
129 if (reason.length() > 2)
130 reason.resize(reason.length() - 2);
136 void MapBlock::copyTo(VoxelManipulator &dst)
138 v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
139 VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
141 // Copy from data to VoxelManipulator
142 dst.copyFrom(data, data_area, v3s16(0,0,0),
143 getPosRelative(), data_size);
146 void MapBlock::copyFrom(VoxelManipulator &dst)
148 v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
149 VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
151 // Copy from VoxelManipulator to data
152 dst.copyTo(data, data_area, v3s16(0,0,0),
153 getPosRelative(), data_size);
156 void MapBlock::actuallyUpdateDayNightDiff()
158 const NodeDefManager *nodemgr = m_gamedef->ndef();
160 // Running this function un-expires m_day_night_differs
161 m_day_night_differs_expired = false;
164 m_day_night_differs = false;
168 bool differs = false;
171 Check if any lighting value differs
174 MapNode previous_n(CONTENT_IGNORE);
175 for (u32 i = 0; i < nodecount; i++) {
178 // If node is identical to previous node, don't verify if it differs
182 differs = !n.isLightDayNightEq(nodemgr);
189 If some lighting values differ, check if the whole thing is
190 just air. If it is just air, differs = false
193 bool only_air = true;
194 for (u32 i = 0; i < nodecount; i++) {
195 MapNode &n = data[i];
196 if (n.getContent() != CONTENT_AIR) {
205 // Set member variable
206 m_day_night_differs = differs;
209 void MapBlock::expireDayNightDiff()
212 m_day_night_differs = false;
213 m_day_night_differs_expired = false;
217 m_day_night_differs_expired = true;
220 s16 MapBlock::getGroundLevel(v2s16 p2d)
226 s16 y = MAP_BLOCKSIZE-1;
229 MapNode n = getNodeRef(p2d.X, y, p2d.Y);
230 if (m_gamedef->ndef()->get(n).walkable) {
231 if(y == MAP_BLOCKSIZE-1)
239 catch(InvalidPositionException &e)
248 // List relevant id-name pairs for ids in the block using nodedef
249 // Renumbers the content IDs (starting at 0 and incrementing
250 // use static memory requires about 65535 * sizeof(int) ram in order to be
251 // sure we can handle all content ids. But it's absolutely worth it as it's
252 // a speedup of 4 for one of the major time consuming functions on storing
254 static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
255 static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
256 const NodeDefManager *nodedef)
258 memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
260 std::set<content_t> unknown_contents;
261 content_t id_counter = 0;
262 for (u32 i = 0; i < MapBlock::nodecount; i++) {
263 content_t global_id = nodes[i].getContent();
264 content_t id = CONTENT_IGNORE;
266 // Try to find an existing mapping
267 if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
268 id = getBlockNodeIdMapping_mapping[global_id];
272 // We have to assign a new mapping
274 getBlockNodeIdMapping_mapping[global_id] = id;
276 const ContentFeatures &f = nodedef->get(global_id);
277 const std::string &name = f.name;
279 unknown_contents.insert(global_id);
281 nimap->set(id, name);
284 // Update the MapNode
285 nodes[i].setContent(id);
287 for (u16 unknown_content : unknown_contents) {
288 errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
289 << "Name for node id " << unknown_content << " not known" << std::endl;
292 // Correct ids in the block to match nodedef based on names.
293 // Unknown ones are added to nodedef.
294 // Will not update itself to match id-name pairs in nodedef.
295 static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
298 const NodeDefManager *nodedef = gamedef->ndef();
299 // This means the block contains incorrect ids, and we contain
300 // the information to convert those to names.
301 // nodedef contains information to convert our names to globally
303 std::unordered_set<content_t> unnamed_contents;
304 std::unordered_set<std::string> unallocatable_contents;
306 bool previous_exists = false;
307 content_t previous_local_id = CONTENT_IGNORE;
308 content_t previous_global_id = CONTENT_IGNORE;
310 for (u32 i = 0; i < MapBlock::nodecount; i++) {
311 content_t local_id = nodes[i].getContent();
312 // If previous node local_id was found and same than before, don't lookup maps
313 // apply directly previous resolved id
314 // This permits to massively improve loading performance when nodes are similar
315 // example: default:air, default:stone are massively present
316 if (previous_exists && local_id == previous_local_id) {
317 nodes[i].setContent(previous_global_id);
322 if (!nimap->getName(local_id, name)) {
323 unnamed_contents.insert(local_id);
324 previous_exists = false;
329 if (!nodedef->getId(name, global_id)) {
330 global_id = gamedef->allocateUnknownNodeId(name);
331 if (global_id == CONTENT_IGNORE) {
332 unallocatable_contents.insert(name);
333 previous_exists = false;
337 nodes[i].setContent(global_id);
339 // Save previous node local_id & global_id result
340 previous_local_id = local_id;
341 previous_global_id = global_id;
342 previous_exists = true;
345 for (const content_t c: unnamed_contents) {
346 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
347 << "Block contains id " << c
348 << " with no name mapping" << std::endl;
350 for (const std::string &node_name: unallocatable_contents) {
351 errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
352 << "Could not allocate global id for node name \""
353 << node_name << "\"" << std::endl;
357 void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
359 if(!ser_ver_supported(version))
360 throw VersionMismatchException("ERROR: MapBlock format not supported");
363 throw SerializationError("ERROR: Not writing dummy block.");
365 FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
371 if(getDayNightDiff())
377 writeU16(os, m_lighting_complete);
386 MapNode *tmp_nodes = new MapNode[nodecount];
387 for(u32 i=0; i<nodecount; i++)
388 tmp_nodes[i] = data[i];
389 getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
391 u8 content_width = 2;
393 writeU8(os, content_width);
394 writeU8(os, params_width);
395 MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
396 content_width, params_width, true);
401 u8 content_width = 2;
403 writeU8(os, content_width);
404 writeU8(os, params_width);
405 MapNode::serializeBulk(os, version, data, nodecount,
406 content_width, params_width, true);
412 std::ostringstream oss(std::ios_base::binary);
413 m_node_metadata.serialize(oss, version, disk);
414 compressZlib(oss.str(), os);
417 Data that goes to disk, but not the network
423 m_node_timers.serialize(os, version);
427 m_static_objects.serialize(os);
430 writeU32(os, getTimestamp());
432 // Write block-specific node definition id mapping
437 m_node_timers.serialize(os, version);
442 void MapBlock::serializeNetworkSpecific(std::ostream &os)
445 throw SerializationError("ERROR: Not writing dummy block.");
448 writeU8(os, 1); // version
449 writeF1000(os, 0); // deprecated heat
450 writeF1000(os, 0); // deprecated humidity
453 void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
455 if(!ser_ver_supported(version))
456 throw VersionMismatchException("ERROR: MapBlock format not supported");
458 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
460 m_day_night_differs_expired = false;
464 deSerialize_pre22(is, version, disk);
468 u8 flags = readU8(is);
469 is_underground = (flags & 0x01) != 0;
470 m_day_night_differs = (flags & 0x02) != 0;
472 m_lighting_complete = 0xFFFF;
474 m_lighting_complete = readU16(is);
475 m_generated = (flags & 0x08) == 0;
480 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
481 <<": Bulk node data"<<std::endl);
482 u8 content_width = readU8(is);
483 u8 params_width = readU8(is);
484 if(content_width != 1 && content_width != 2)
485 throw SerializationError("MapBlock::deSerialize(): invalid content_width");
486 if(params_width != 2)
487 throw SerializationError("MapBlock::deSerialize(): invalid params_width");
488 MapNode::deSerializeBulk(is, version, data, nodecount,
489 content_width, params_width, true);
494 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
495 <<": Node metadata"<<std::endl);
498 std::ostringstream oss(std::ios_base::binary);
499 decompressZlib(is, oss);
500 std::istringstream iss(oss.str(), std::ios_base::binary);
502 m_node_metadata.deSerialize(iss, m_gamedef->idef());
504 content_nodemeta_deserialize_legacy(iss,
505 &m_node_metadata, &m_node_timers,
507 } catch(SerializationError &e) {
508 warningstream<<"MapBlock::deSerialize(): Ignoring an error"
509 <<" while deserializing node metadata at ("
510 <<PP(getPos())<<": "<<e.what()<<std::endl;
514 Data that is only on disk
524 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
525 <<": Node timers (ver==24)"<<std::endl);
526 m_node_timers.deSerialize(is, version);
530 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
531 <<": Static objects"<<std::endl);
532 m_static_objects.deSerialize(is);
535 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
536 <<": Timestamp"<<std::endl);
537 setTimestamp(readU32(is));
538 m_disk_timestamp = m_timestamp;
540 // Dynamically re-set ids based on node names
541 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
542 <<": NameIdMapping"<<std::endl);
544 nimap.deSerialize(is);
545 correctBlockNodeIds(&nimap, data, m_gamedef);
548 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
549 <<": Node timers (ver>=25)"<<std::endl);
550 m_node_timers.deSerialize(is, version);
554 TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
555 <<": Done."<<std::endl);
558 void MapBlock::deSerializeNetworkSpecific(std::istream &is)
561 int version = readU8(is);
563 // throw SerializationError("unsupported MapBlock version");
565 readF1000(is); // deprecated heat
566 readF1000(is); // deprecated humidity
569 catch(SerializationError &e)
571 warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
572 <<": "<<e.what()<<std::endl;
580 void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
582 // Initialize default flags
583 is_underground = false;
584 m_day_night_differs = false;
585 m_lighting_complete = 0xFFFF;
588 // Make a temporary buffer
589 u32 ser_length = MapNode::serializedLength(version);
590 SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
592 // These have no compression
593 if (version <= 3 || version == 5 || version == 6) {
596 if (is.gcount() != 1)
597 throw SerializationError(std::string(FUNCTION_NAME)
598 + ": not enough input data");
599 is_underground = tmp;
600 is.read((char *)*databuf_nodelist, nodecount * ser_length);
601 if ((u32)is.gcount() != nodecount * ser_length)
602 throw SerializationError(std::string(FUNCTION_NAME)
603 + ": not enough input data");
604 } else if (version <= 10) {
606 is.read((char *)&t8, 1);
610 // Uncompress and set material data
611 std::ostringstream os(std::ios_base::binary);
612 decompress(is, os, version);
613 std::string s = os.str();
614 if (s.size() != nodecount)
615 throw SerializationError(std::string(FUNCTION_NAME)
616 + ": not enough input data");
617 for (u32 i = 0; i < s.size(); i++) {
618 databuf_nodelist[i*ser_length] = s[i];
622 // Uncompress and set param data
623 std::ostringstream os(std::ios_base::binary);
624 decompress(is, os, version);
625 std::string s = os.str();
626 if (s.size() != nodecount)
627 throw SerializationError(std::string(FUNCTION_NAME)
628 + ": not enough input data");
629 for (u32 i = 0; i < s.size(); i++) {
630 databuf_nodelist[i*ser_length + 1] = s[i];
635 // Uncompress and set param2 data
636 std::ostringstream os(std::ios_base::binary);
637 decompress(is, os, version);
638 std::string s = os.str();
639 if (s.size() != nodecount)
640 throw SerializationError(std::string(FUNCTION_NAME)
641 + ": not enough input data");
642 for (u32 i = 0; i < s.size(); i++) {
643 databuf_nodelist[i*ser_length + 2] = s[i];
646 } else { // All other versions (10 to 21)
648 is.read((char*)&flags, 1);
649 is_underground = (flags & 0x01) != 0;
650 m_day_night_differs = (flags & 0x02) != 0;
652 m_generated = (flags & 0x08) == 0;
655 std::ostringstream os(std::ios_base::binary);
656 decompress(is, os, version);
657 std::string s = os.str();
658 if (s.size() != nodecount * 3)
659 throw SerializationError(std::string(FUNCTION_NAME)
660 + ": decompress resulted in size other than nodecount*3");
662 // deserialize nodes from buffer
663 for (u32 i = 0; i < nodecount; i++) {
664 databuf_nodelist[i*ser_length] = s[i];
665 databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
666 databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
676 std::string data = deSerializeString(is);
677 std::istringstream iss(data, std::ios_base::binary);
678 content_nodemeta_deserialize_legacy(iss,
679 &m_node_metadata, &m_node_timers,
682 //std::string data = deSerializeLongString(is);
683 std::ostringstream oss(std::ios_base::binary);
684 decompressZlib(is, oss);
685 std::istringstream iss(oss.str(), std::ios_base::binary);
686 content_nodemeta_deserialize_legacy(iss,
687 &m_node_metadata, &m_node_timers,
690 } catch(SerializationError &e) {
691 warningstream<<"MapBlock::deSerialize(): Ignoring an error"
692 <<" while deserializing node metadata"<<std::endl;
697 // Deserialize node data
698 for (u32 i = 0; i < nodecount; i++) {
699 data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
704 Versions up from 9 have block objects. (DEPRECATED)
707 u16 count = readU16(is);
708 // Not supported and length not known if count is not 0
710 warningstream<<"MapBlock::deSerialize_pre22(): "
711 <<"Ignoring stuff coming at and after MBOs"<<std::endl;
717 Versions up from 15 have static objects.
720 m_static_objects.deSerialize(is);
724 setTimestamp(readU32(is));
725 m_disk_timestamp = m_timestamp;
727 setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
730 // Dynamically re-set ids based on node names
732 // If supported, read node definition id mapping
734 nimap.deSerialize(is);
735 // Else set the legacy mapping
737 content_mapnode_get_name_id_mapping(&nimap);
739 correctBlockNodeIds(&nimap, data, m_gamedef);
743 // Legacy data changes
744 // This code has to convert from pre-22 to post-22 format.
745 const NodeDefManager *nodedef = m_gamedef->ndef();
746 for(u32 i=0; i<nodecount; i++)
748 const ContentFeatures &f = nodedef->get(data[i].getContent());
750 if(nodedef->getId("default:stone") == data[i].getContent()
751 && data[i].getParam1() == 1)
753 data[i].setContent(nodedef->getId("default:stone_with_coal"));
754 data[i].setParam1(0);
756 else if(nodedef->getId("default:stone") == data[i].getContent()
757 && data[i].getParam1() == 2)
759 data[i].setContent(nodedef->getId("default:stone_with_iron"));
760 data[i].setParam1(0);
763 if(f.legacy_facedir_simple)
765 data[i].setParam2(data[i].getParam1());
766 data[i].setParam1(0);
769 if(f.legacy_wallmounted)
771 u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
772 u8 dir_old_format = data[i].getParam2();
773 u8 dir_new_format = 0;
774 for(u8 j=0; j<8; j++)
776 if((dir_old_format & wallmounted_new_to_old[j]) != 0)
782 data[i].setParam2(dir_new_format);
789 Get a quick string to describe what a block actually contains
791 std::string analyze_block(MapBlock *block)
796 std::ostringstream desc;
798 v3s16 p = block->getPos();
800 snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
803 switch(block->getModified())
805 case MOD_STATE_CLEAN:
808 case MOD_STATE_WRITE_AT_UNLOAD:
809 desc<<"WRITE_AT_UNLOAD, ";
811 case MOD_STATE_WRITE_NEEDED:
812 desc<<"WRITE_NEEDED, ";
815 desc<<"unknown getModified()="+itos(block->getModified())+", ";
818 if(block->isGenerated())
819 desc<<"is_gen [X], ";
821 desc<<"is_gen [ ], ";
823 if(block->getIsUnderground())
828 desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
836 bool full_ignore = true;
837 bool some_ignore = false;
838 bool full_air = true;
839 bool some_air = false;
840 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
841 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
842 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
845 MapNode n = block->getNodeNoEx(p);
846 content_t c = n.getContent();
847 if(c == CONTENT_IGNORE)
859 std::ostringstream ss;
862 ss<<"IGNORE (full), ";
871 if(ss.str().size()>=2)
872 desc<<ss.str().substr(0, ss.str().size()-2);
877 return desc.str().substr(0, desc.str().size()-2);