+ // Try to find an existing mapping
+ std::map<content_t, content_t>::iterator j = mapping.find(global_id);
+ if(j != mapping.end())
+ {
+ id = j->second;
+ }
+ else
+ {
+ // We have to assign a new mapping
+ id = id_counter++;
+ mapping.insert(std::make_pair(global_id, id));
+
+ const ContentFeatures &f = nodedef->get(global_id);
+ const std::string &name = f.name;
+ if(name == "")
+ unknown_contents.insert(global_id);
+ else
+ nimap->set(id, name);
+ }
+
+ // Update the MapNode
+ nodes[i].setContent(id);
+ }
+ for(std::set<content_t>::const_iterator
+ i = unknown_contents.begin();
+ i != unknown_contents.end(); i++){
+ errorstream<<"getBlockNodeIdMapping(): IGNORING ERROR: "
+ <<"Name for node id "<<(*i)<<" not known"<<std::endl;
+ }
+}
+// Correct ids in the block to match nodedef based on names.
+// Unknown ones are added to nodedef.
+// Will not update itself to match id-name pairs in nodedef.
+static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
+ IGameDef *gamedef)
+{
+ INodeDefManager *nodedef = gamedef->ndef();
+ // This means the block contains incorrect ids, and we contain
+ // the information to convert those to names.
+ // nodedef contains information to convert our names to globally
+ // correct ids.
+ std::set<content_t> unnamed_contents;
+ std::set<std::string> unallocatable_contents;
+ for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++)
+ {
+ content_t local_id = nodes[i].getContent();
+ std::string name;
+ bool found = nimap->getName(local_id, name);
+ if(!found){
+ unnamed_contents.insert(local_id);
+ continue;
+ }
+ content_t global_id;
+ found = nodedef->getId(name, global_id);
+ if(!found){
+ global_id = gamedef->allocateUnknownNodeId(name);
+ if(global_id == CONTENT_IGNORE){
+ unallocatable_contents.insert(name);
+ continue;
+ }
+ }
+ nodes[i].setContent(global_id);
+ }
+ for(std::set<content_t>::const_iterator
+ i = unnamed_contents.begin();
+ i != unnamed_contents.end(); i++){
+ errorstream<<"correctBlockNodeIds(): IGNORING ERROR: "
+ <<"Block contains id "<<(*i)
+ <<" with no name mapping"<<std::endl;
+ }
+ for(std::set<std::string>::const_iterator
+ i = unallocatable_contents.begin();
+ i != unallocatable_contents.end(); i++){
+ errorstream<<"correctBlockNodeIds(): IGNORING ERROR: "
+ <<"Could not allocate global id for node name \""
+ <<(*i)<<"\""<<std::endl;
+ }
+}
+
+void MapBlock::serialize(std::ostream &os, u8 version, bool disk)