/* Minetest-c55 Copyright (C) 2010 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "nodedef.h" #include "main.h" // For g_settings #include "itemdef.h" #ifndef SERVER #include "tile.h" #endif #include "log.h" #include "settings.h" #include "nameidmapping.h" /* NodeBox */ void NodeBox::serialize(std::ostream &os) const { writeU8(os, 0); // version writeU8(os, type); writeV3F1000(os, fixed.MinEdge); writeV3F1000(os, fixed.MaxEdge); writeV3F1000(os, wall_top.MinEdge); writeV3F1000(os, wall_top.MaxEdge); writeV3F1000(os, wall_bottom.MinEdge); writeV3F1000(os, wall_bottom.MaxEdge); writeV3F1000(os, wall_side.MinEdge); writeV3F1000(os, wall_side.MaxEdge); } void NodeBox::deSerialize(std::istream &is) { int version = readU8(is); if(version != 0) throw SerializationError("unsupported NodeBox version"); type = (enum NodeBoxType)readU8(is); fixed.MinEdge = readV3F1000(is); fixed.MaxEdge = readV3F1000(is); wall_top.MinEdge = readV3F1000(is); wall_top.MaxEdge = readV3F1000(is); wall_bottom.MinEdge = readV3F1000(is); wall_bottom.MaxEdge = readV3F1000(is); wall_side.MinEdge = readV3F1000(is); wall_side.MaxEdge = readV3F1000(is); } /* MaterialSpec */ void MaterialSpec::serialize(std::ostream &os) const { os<first); writeS16(os, i->second); } writeU8(os, drawtype); writeF1000(os, visual_scale); writeU8(os, 6); for(u32 i=0; i<6; i++) os<set(i, get(i)); } return mgr; } virtual const ContentFeatures& get(content_t c) const { assert(c <= MAX_CONTENT); return m_content_features[c]; } virtual const ContentFeatures& get(const MapNode &n) const { return get(n.getContent()); } virtual bool getId(const std::string &name, content_t &result) const { std::map::const_iterator i = m_name_id_mapping_with_aliases.find(name); if(i == m_name_id_mapping_with_aliases.end()) return false; result = i->second; return true; } virtual content_t getId(const std::string &name) const { content_t id = CONTENT_IGNORE; getId(name, id); return id; } virtual void getIds(const std::string &name, std::set &result) const { if(name.substr(0,6) != "group:"){ content_t id = CONTENT_IGNORE; if(getId(name, id)) result.insert(id); return; } std::string group = name.substr(6); for(u16 id=0; id<=MAX_CONTENT; id++) { const ContentFeatures &f = m_content_features[id]; if(f.name == "") // Quickly discard undefined nodes continue; if(itemgroup_get(f.groups, group) != 0) result.insert(id); } } virtual const ContentFeatures& get(const std::string &name) const { content_t id = CONTENT_IGNORE; getId(name, id); return get(id); } // IWritableNodeDefManager virtual void set(content_t c, const ContentFeatures &def) { verbosestream<<"registerNode: registering content id \""< all = idef->getAll(); m_name_id_mapping_with_aliases.clear(); for(std::set::iterator i = all.begin(); i != all.end(); i++) { std::string name = *i; std::string convert_to = idef->getAlias(name); content_t id; if(m_name_id_mapping.getId(convert_to, id)) { m_name_id_mapping_with_aliases.insert( std::make_pair(name, id)); } } } virtual void updateTextures(ITextureSource *tsrc) { #ifndef SERVER infostream<<"CNodeDefManager::updateTextures(): Updating " <<"textures in node definitions"<getBool("new_style_water"); bool new_style_leaves = g_settings->getBool("new_style_leaves"); bool opaque_water = g_settings->getBool("opaque_water"); for(u16 i=0; i<=MAX_CONTENT; i++) { ContentFeatures *f = &m_content_features[i]; std::string tname_tiles[6]; for(u32 j=0; j<6; j++) { tname_tiles[j] = f->tname_tiles[j]; if(tname_tiles[j] == "") tname_tiles[j] = "unknown_block.png"; } switch(f->drawtype){ default: case NDT_NORMAL: f->solidness = 2; break; case NDT_AIRLIKE: f->solidness = 0; break; case NDT_LIQUID: assert(f->liquid_type == LIQUID_SOURCE); if(opaque_water) f->alpha = 255; if(new_style_water){ f->solidness = 0; } else { f->solidness = 1; if(f->alpha == 255) f->solidness = 2; f->backface_culling = false; } break; case NDT_FLOWINGLIQUID: assert(f->liquid_type == LIQUID_FLOWING); f->solidness = 0; if(opaque_water) f->alpha = 255; break; case NDT_GLASSLIKE: f->solidness = 0; f->visual_solidness = 1; break; case NDT_ALLFACES: f->solidness = 0; f->visual_solidness = 1; break; case NDT_ALLFACES_OPTIONAL: if(new_style_leaves){ f->drawtype = NDT_ALLFACES; f->solidness = 0; f->visual_solidness = 1; } else { f->drawtype = NDT_NORMAL; f->solidness = 2; for(u32 i=0; i<6; i++){ tname_tiles[i] += std::string("^[noalpha"); } } break; case NDT_TORCHLIKE: case NDT_SIGNLIKE: case NDT_PLANTLIKE: case NDT_FENCELIKE: case NDT_RAILLIKE: f->solidness = 0; break; } // Tile textures for(u16 j=0; j<6; j++){ f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]); f->tiles[j].alpha = f->alpha; if(f->alpha == 255) f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE; else f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX; f->tiles[j].material_flags = 0; if(f->backface_culling) f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; } // Special tiles for(u16 j=0; jspecial_tiles[j].texture = tsrc->getTexture(f->mspec_special[j].tname); f->special_tiles[j].alpha = f->alpha; if(f->alpha == 255) f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE; else f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX; f->special_tiles[j].material_flags = 0; if(f->mspec_special[j].backface_culling) f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; } } #endif } void serialize(std::ostream &os) { writeU8(os, 1); // version u16 count = 0; std::ostringstream os2(std::ios::binary); for(u16 i=0; i<=MAX_CONTENT; i++) { if(i == CONTENT_IGNORE || i == CONTENT_AIR) continue; ContentFeatures *f = &m_content_features[i]; if(f->name == "") continue; writeU16(os2, i); // Wrap it in a string to allow different lengths without // strict version incompatibilities std::ostringstream wrapper_os(std::ios::binary); f->serialize(wrapper_os); os2< MAX_CONTENT){ errorstream<<"ContentFeatures::deSerialize(): " <<"Too large content id: "<deSerialize(wrapper_is); if(f->name != "") addNameIdMapping(i, f->name); } } private: void addNameIdMapping(content_t i, std::string name) { m_name_id_mapping.set(i, name); m_name_id_mapping_with_aliases.insert(std::make_pair(name, i)); } private: // Features indexed by id ContentFeatures m_content_features[MAX_CONTENT+1]; // A mapping for fast converting back and forth between names and ids NameIdMapping m_name_id_mapping; // Like m_name_id_mapping, but only from names to ids, and includes // item aliases too. Updated by updateAliases() // Note: Not serialized. std::map m_name_id_mapping_with_aliases; }; IWritableNodeDefManager* createNodeDefManager() { return new CNodeDefManager(); }