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.
22 #include "main.h" // For g_settings
29 #include "nameidmapping.h"
30 #include "util/numeric.h"
31 #include "util/serialize.h"
32 #include "exceptions.h"
41 type = NODEBOX_REGULAR;
44 // default is sign/ladder-like
45 wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
46 wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
47 wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
50 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
52 int version = protocol_version >= 21 ? 2 : 1;
55 if (version == 1 && type == NODEBOX_LEVELED)
56 writeU8(os, NODEBOX_FIXED);
60 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
62 writeU16(os, fixed.size());
63 for(std::vector<aabb3f>::const_iterator
65 i != fixed.end(); i++)
67 writeV3F1000(os, i->MinEdge);
68 writeV3F1000(os, i->MaxEdge);
71 else if(type == NODEBOX_WALLMOUNTED)
73 writeV3F1000(os, wall_top.MinEdge);
74 writeV3F1000(os, wall_top.MaxEdge);
75 writeV3F1000(os, wall_bottom.MinEdge);
76 writeV3F1000(os, wall_bottom.MaxEdge);
77 writeV3F1000(os, wall_side.MinEdge);
78 writeV3F1000(os, wall_side.MaxEdge);
82 void NodeBox::deSerialize(std::istream &is)
84 int version = readU8(is);
85 if(version < 1 || version > 2)
86 throw SerializationError("unsupported NodeBox version");
90 type = (enum NodeBoxType)readU8(is);
92 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
94 u16 fixed_count = readU16(is);
98 box.MinEdge = readV3F1000(is);
99 box.MaxEdge = readV3F1000(is);
100 fixed.push_back(box);
103 else if(type == NODEBOX_WALLMOUNTED)
105 wall_top.MinEdge = readV3F1000(is);
106 wall_top.MaxEdge = readV3F1000(is);
107 wall_bottom.MinEdge = readV3F1000(is);
108 wall_bottom.MaxEdge = readV3F1000(is);
109 wall_side.MinEdge = readV3F1000(is);
110 wall_side.MaxEdge = readV3F1000(is);
118 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
120 if(protocol_version >= 17)
124 os<<serializeString(name);
125 writeU8(os, animation.type);
126 writeU16(os, animation.aspect_w);
127 writeU16(os, animation.aspect_h);
128 writeF1000(os, animation.length);
129 if(protocol_version >= 17)
130 writeU8(os, backface_culling);
133 void TileDef::deSerialize(std::istream &is)
135 int version = readU8(is);
136 name = deSerializeString(is);
137 animation.type = (TileAnimationType)readU8(is);
138 animation.aspect_w = readU16(is);
139 animation.aspect_h = readU16(is);
140 animation.length = readF1000(is);
142 backface_culling = readU8(is);
146 SimpleSoundSpec serialization
149 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
152 os<<serializeString(ss.name);
153 writeF1000(os, ss.gain);
155 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
157 ss.name = deSerializeString(is);
158 ss.gain = readF1000(is);
165 ContentFeatures::ContentFeatures()
170 ContentFeatures::~ContentFeatures()
174 void ContentFeatures::reset()
181 visual_solidness = 0;
182 backface_culling = true;
184 has_on_construct = false;
185 has_on_destruct = false;
186 has_after_destruct = false;
190 NOTE: Most of this is always overridden by the default values given
195 // Unknown nodes can be dug
196 groups["dig_immediate"] = 2;
197 drawtype = NDT_NORMAL;
199 for(u32 i = 0; i < 6; i++)
200 tiledef[i] = TileDef();
201 for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
202 tiledef_special[j] = TileDef();
204 post_effect_color = video::SColor(0, 0, 0, 0);
205 param_type = CPT_NONE;
206 param_type_2 = CPT2_NONE;
207 is_ground_content = false;
208 light_propagates = false;
209 sunlight_propagates = false;
214 buildable_to = false;
215 rightclickable = true;
217 liquid_type = LIQUID_NONE;
218 liquid_alternative_flowing = "";
219 liquid_alternative_source = "";
220 liquid_viscosity = 0;
221 liquid_renewable = true;
223 liquid_range = LIQUID_LEVEL_MAX+1;
226 damage_per_second = 0;
227 node_box = NodeBox();
228 selection_box = NodeBox();
230 legacy_facedir_simple = false;
231 legacy_wallmounted = false;
232 sound_footstep = SimpleSoundSpec();
233 sound_dig = SimpleSoundSpec("__group");
234 sound_dug = SimpleSoundSpec();
237 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
239 if(protocol_version < 24){
240 serializeOld(os, protocol_version);
244 writeU8(os, 7); // version
245 os<<serializeString(name);
246 writeU16(os, groups.size());
247 for(ItemGroupList::const_iterator
248 i = groups.begin(); i != groups.end(); i++){
249 os<<serializeString(i->first);
250 writeS16(os, i->second);
252 writeU8(os, drawtype);
253 writeF1000(os, visual_scale);
255 for(u32 i = 0; i < 6; i++)
256 tiledef[i].serialize(os, protocol_version);
257 writeU8(os, CF_SPECIAL_COUNT);
258 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++){
259 tiledef_special[i].serialize(os, protocol_version);
262 writeU8(os, post_effect_color.getAlpha());
263 writeU8(os, post_effect_color.getRed());
264 writeU8(os, post_effect_color.getGreen());
265 writeU8(os, post_effect_color.getBlue());
266 writeU8(os, param_type);
267 writeU8(os, param_type_2);
268 writeU8(os, is_ground_content);
269 writeU8(os, light_propagates);
270 writeU8(os, sunlight_propagates);
271 writeU8(os, walkable);
272 writeU8(os, pointable);
273 writeU8(os, diggable);
274 writeU8(os, climbable);
275 writeU8(os, buildable_to);
276 os<<serializeString(""); // legacy: used to be metadata_name
277 writeU8(os, liquid_type);
278 os<<serializeString(liquid_alternative_flowing);
279 os<<serializeString(liquid_alternative_source);
280 writeU8(os, liquid_viscosity);
281 writeU8(os, liquid_renewable);
282 writeU8(os, light_source);
283 writeU32(os, damage_per_second);
284 node_box.serialize(os, protocol_version);
285 selection_box.serialize(os, protocol_version);
286 writeU8(os, legacy_facedir_simple);
287 writeU8(os, legacy_wallmounted);
288 serializeSimpleSoundSpec(sound_footstep, os);
289 serializeSimpleSoundSpec(sound_dig, os);
290 serializeSimpleSoundSpec(sound_dug, os);
291 writeU8(os, rightclickable);
292 writeU8(os, drowning);
293 writeU8(os, leveled);
294 writeU8(os, liquid_range);
296 // Stuff below should be moved to correct place in a version that otherwise changes
297 // the protocol version
300 void ContentFeatures::deSerialize(std::istream &is)
302 int version = readU8(is);
304 deSerializeOld(is, version);
308 name = deSerializeString(is);
310 u32 groups_size = readU16(is);
311 for(u32 i = 0; i < groups_size; i++){
312 std::string name = deSerializeString(is);
313 int value = readS16(is);
314 groups[name] = value;
316 drawtype = (enum NodeDrawType)readU8(is);
317 visual_scale = readF1000(is);
319 throw SerializationError("unsupported tile count");
320 for(u32 i = 0; i < 6; i++)
321 tiledef[i].deSerialize(is);
322 if(readU8(is) != CF_SPECIAL_COUNT)
323 throw SerializationError("unsupported CF_SPECIAL_COUNT");
324 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
325 tiledef_special[i].deSerialize(is);
327 post_effect_color.setAlpha(readU8(is));
328 post_effect_color.setRed(readU8(is));
329 post_effect_color.setGreen(readU8(is));
330 post_effect_color.setBlue(readU8(is));
331 param_type = (enum ContentParamType)readU8(is);
332 param_type_2 = (enum ContentParamType2)readU8(is);
333 is_ground_content = readU8(is);
334 light_propagates = readU8(is);
335 sunlight_propagates = readU8(is);
336 walkable = readU8(is);
337 pointable = readU8(is);
338 diggable = readU8(is);
339 climbable = readU8(is);
340 buildable_to = readU8(is);
341 deSerializeString(is); // legacy: used to be metadata_name
342 liquid_type = (enum LiquidType)readU8(is);
343 liquid_alternative_flowing = deSerializeString(is);
344 liquid_alternative_source = deSerializeString(is);
345 liquid_viscosity = readU8(is);
346 liquid_renewable = readU8(is);
347 light_source = readU8(is);
348 damage_per_second = readU32(is);
349 node_box.deSerialize(is);
350 selection_box.deSerialize(is);
351 legacy_facedir_simple = readU8(is);
352 legacy_wallmounted = readU8(is);
353 deSerializeSimpleSoundSpec(sound_footstep, is);
354 deSerializeSimpleSoundSpec(sound_dig, is);
355 deSerializeSimpleSoundSpec(sound_dug, is);
356 rightclickable = readU8(is);
357 drowning = readU8(is);
358 leveled = readU8(is);
359 liquid_range = readU8(is);
361 // If you add anything here, insert it primarily inside the try-catch
362 // block to not need to increase the version.
364 // Stuff below should be moved to correct place in a version that
365 // otherwise changes the protocol version
366 }catch(SerializationError &e) {};
373 class CNodeDefManager: public IWritableNodeDefManager
378 m_content_features.clear();
379 m_name_id_mapping.clear();
380 m_name_id_mapping_with_aliases.clear();
381 m_group_to_items.clear();
384 u32 initial_length = 0;
385 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
386 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
387 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
388 m_content_features.resize(initial_length);
390 // Set CONTENT_UNKNOWN
394 // Insert directly into containers
395 content_t c = CONTENT_UNKNOWN;
396 m_content_features[c] = f;
397 addNameIdMapping(c, f.name);
404 f.drawtype = NDT_AIRLIKE;
405 f.param_type = CPT_LIGHT;
406 f.light_propagates = true;
407 f.sunlight_propagates = true;
411 f.buildable_to = true;
412 f.is_ground_content = true;
413 // Insert directly into containers
414 content_t c = CONTENT_AIR;
415 m_content_features[c] = f;
416 addNameIdMapping(c, f.name);
419 // Set CONTENT_IGNORE
423 f.drawtype = NDT_AIRLIKE;
424 f.param_type = CPT_NONE;
425 f.light_propagates = false;
426 f.sunlight_propagates = false;
430 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
431 f.is_ground_content = true;
432 // Insert directly into containers
433 content_t c = CONTENT_IGNORE;
434 m_content_features[c] = f;
435 addNameIdMapping(c, f.name);
442 virtual ~CNodeDefManager()
445 virtual IWritableNodeDefManager* clone()
447 CNodeDefManager *mgr = new CNodeDefManager();
451 virtual const ContentFeatures& get(content_t c) const
453 if(c < m_content_features.size())
454 return m_content_features[c];
456 return m_content_features[CONTENT_UNKNOWN];
458 virtual const ContentFeatures& get(const MapNode &n) const
460 return get(n.getContent());
462 virtual bool getId(const std::string &name, content_t &result) const
464 std::map<std::string, content_t>::const_iterator
465 i = m_name_id_mapping_with_aliases.find(name);
466 if(i == m_name_id_mapping_with_aliases.end())
471 virtual content_t getId(const std::string &name) const
473 content_t id = CONTENT_IGNORE;
477 virtual void getIds(const std::string &name, std::set<content_t> &result)
480 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
481 if(name.substr(0,6) != "group:"){
482 content_t id = CONTENT_IGNORE;
487 std::string group = name.substr(6);
489 std::map<std::string, GroupItems>::const_iterator
490 i = m_group_to_items.find(group);
491 if (i == m_group_to_items.end())
494 const GroupItems &items = i->second;
495 for (GroupItems::const_iterator j = items.begin();
496 j != items.end(); ++j) {
497 if ((*j).second != 0)
498 result.insert((*j).first);
500 //printf("getIds: %dus\n", t.stop());
502 virtual const ContentFeatures& get(const std::string &name) const
504 content_t id = CONTENT_UNKNOWN;
508 // returns CONTENT_IGNORE if no free ID found
509 content_t allocateId()
511 for(content_t id = m_next_id;
512 id >= m_next_id; // overflow?
514 while(id >= m_content_features.size()){
515 m_content_features.push_back(ContentFeatures());
517 const ContentFeatures &f = m_content_features[id];
523 // If we arrive here, an overflow occurred in id.
524 // That means no ID was found
525 return CONTENT_IGNORE;
527 // IWritableNodeDefManager
528 virtual content_t set(const std::string &name,
529 const ContentFeatures &def)
532 assert(name == def.name);
534 // Don't allow redefining ignore (but allow air and unknown)
535 if(name == "ignore"){
536 infostream<<"NodeDefManager: WARNING: Ignoring "
537 <<"CONTENT_IGNORE redefinition"<<std::endl;
538 return CONTENT_IGNORE;
541 content_t id = CONTENT_IGNORE;
542 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
546 if(id == CONTENT_IGNORE){
547 infostream<<"NodeDefManager: WARNING: Absolute "
548 <<"limit reached"<<std::endl;
549 return CONTENT_IGNORE;
551 assert(id != CONTENT_IGNORE);
552 addNameIdMapping(id, name);
554 m_content_features[id] = def;
555 verbosestream<<"NodeDefManager: registering content id \""<<id
556 <<"\": name=\""<<def.name<<"\""<<std::endl;
558 // Add this content to the list of all groups it belongs to
559 // FIXME: This should remove a node from groups it no longer
560 // belongs to when a node is re-registered
561 for (ItemGroupList::const_iterator i = def.groups.begin();
562 i != def.groups.end(); ++i) {
563 std::string group_name = i->first;
565 std::map<std::string, GroupItems>::iterator
566 j = m_group_to_items.find(group_name);
567 if (j == m_group_to_items.end()) {
568 m_group_to_items[group_name].push_back(
569 std::make_pair(id, i->second));
571 GroupItems &items = j->second;
572 items.push_back(std::make_pair(id, i->second));
577 virtual content_t allocateDummy(const std::string &name)
584 virtual void updateAliases(IItemDefManager *idef)
586 std::set<std::string> all = idef->getAll();
587 m_name_id_mapping_with_aliases.clear();
588 for(std::set<std::string>::iterator
589 i = all.begin(); i != all.end(); i++)
591 std::string name = *i;
592 std::string convert_to = idef->getAlias(name);
594 if(m_name_id_mapping.getId(convert_to, id))
596 m_name_id_mapping_with_aliases.insert(
597 std::make_pair(name, id));
601 virtual void updateTextures(ITextureSource *tsrc,
602 IShaderSource *shdsrc)
605 infostream<<"CNodeDefManager::updateTextures(): Updating "
606 <<"textures in node definitions"<<std::endl;
608 bool new_style_water = g_settings->getBool("new_style_water");
609 bool new_style_leaves = g_settings->getBool("new_style_leaves");
610 bool connected_glass = g_settings->getBool("connected_glass");
611 bool opaque_water = g_settings->getBool("opaque_water");
612 bool enable_shaders = g_settings->getBool("enable_shaders");
613 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
614 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
616 for(u32 i=0; i<m_content_features.size(); i++)
618 ContentFeatures *f = &m_content_features[i];
620 // Figure out the actual tiles to use
622 for(u32 j = 0; j < 6; j++)
624 tiledef[j] = f->tiledef[j];
625 if(tiledef[j].name == "")
626 tiledef[j].name = "unknown_node.png";
629 bool is_liquid = false;
630 bool is_water_surface = false;
633 material_type = (f->alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
644 assert(f->liquid_type == LIQUID_SOURCE);
651 f->backface_culling = false;
655 case NDT_FLOWINGLIQUID:
656 assert(f->liquid_type == LIQUID_FLOWING);
664 f->visual_solidness = 1;
666 case NDT_GLASSLIKE_FRAMED:
668 f->visual_solidness = 1;
670 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
672 f->visual_solidness = 1;
673 if (connected_glass) {
674 f->drawtype = NDT_GLASSLIKE_FRAMED;
676 f->drawtype = NDT_GLASSLIKE;
681 f->visual_solidness = 1;
683 case NDT_ALLFACES_OPTIONAL:
684 if(new_style_leaves){
685 f->drawtype = NDT_ALLFACES;
687 f->visual_solidness = 1;
689 f->drawtype = NDT_NORMAL;
691 for(u32 i=0; i<6; i++){
692 tiledef[i].name += std::string("^[noalpha");
696 material_type = TILE_MATERIAL_WAVING_LEAVES;
700 f->backface_culling = false;
702 material_type = TILE_MATERIAL_WAVING_PLANTS;
705 f->backface_culling = false;
716 material_type = (f->alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
717 if (f->name == "default:water_source")
718 is_water_surface = true;
721 for(u16 j=0; j<6; j++)
722 tile_shader[j] = shdsrc->getShader("nodes_shader",material_type, f->drawtype);
724 if (is_water_surface)
725 tile_shader[0] = shdsrc->getShader("water_surface_shader",material_type, f->drawtype);
727 // Tiles (fill in f->tiles[])
728 for(u16 j = 0; j < 6; j++){
730 f->tiles[j].shader_id = tile_shader[j];
732 f->tiles[j].texture = tsrc->getTexture(
734 &f->tiles[j].texture_id);
736 if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion))
737 f->tiles[j].normal_texture = tsrc->getNormalTexture(tiledef[j].name);
739 f->tiles[j].alpha = f->alpha;
741 f->tiles[j].material_type = material_type;
743 f->tiles[j].material_flags = 0;
744 if(f->backface_culling)
745 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
746 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
747 f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
748 // Animation parameters
750 if(f->tiles[j].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
751 // Get texture size to determine frame count by
753 v2u32 size = f->tiles[j].texture->getOriginalSize();
754 int frame_height = (float)size.X /
755 (float)tiledef[j].animation.aspect_w *
756 (float)tiledef[j].animation.aspect_h;
757 frame_count = size.Y / frame_height;
758 int frame_length_ms = 1000.0 *
759 tiledef[j].animation.length / frame_count;
760 f->tiles[j].animation_frame_count = frame_count;
761 f->tiles[j].animation_frame_length_ms = frame_length_ms;
763 if(frame_count == 1) {
764 f->tiles[j].material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
766 std::ostringstream os(std::ios::binary);
767 for (int i = 0; i < frame_count; i++) {
770 os<<tiledef[j].name<<"^[verticalframe:"<<frame_count<<":"<<i;
771 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
772 if (f->tiles[j].normal_texture)
773 frame.normal_texture = tsrc->getNormalTexture(os.str());
774 f->tiles[j].frames[i]=frame;
778 // Special tiles (fill in f->special_tiles[])
779 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
781 f->special_tiles[j].shader_id = tile_shader[j];
783 f->special_tiles[j].texture = tsrc->getTexture(
784 f->tiledef_special[j].name,
785 &f->special_tiles[j].texture_id);
787 if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion))
788 f->special_tiles[j].normal_texture = tsrc->getNormalTexture(f->tiledef_special[j].name);
790 f->special_tiles[j].alpha = f->alpha;
792 f->special_tiles[j].material_type = material_type;
794 f->special_tiles[j].material_flags = 0;
795 if(f->tiledef_special[j].backface_culling)
796 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
797 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
798 f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
799 // Animation parameters
801 if(f->special_tiles[j].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
802 // Get texture size to determine frame count by
804 v2u32 size = f->special_tiles[j].texture->getOriginalSize();
805 int frame_height = (float)size.X /
806 (float)f->tiledef_special[j].animation.aspect_w *
807 (float)f->tiledef_special[j].animation.aspect_h;
808 frame_count = size.Y / frame_height;
809 int frame_length_ms = 1000.0 *
810 f->tiledef_special[j].animation.length / frame_count;
811 f->special_tiles[j].animation_frame_count = frame_count;
812 f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
814 if(frame_count == 1) {
815 f->special_tiles[j].material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
817 std::ostringstream os(std::ios::binary);
818 for (int i = 0; i < frame_count; i++) {
821 os<<f->tiledef_special[j].name<<"^[verticalframe:"<<frame_count<<":"<<i;
822 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
823 if (f->special_tiles[j].normal_texture)
824 frame.normal_texture = tsrc->getNormalTexture(os.str());
825 f->special_tiles[j].frames[i]=frame;
832 void serialize(std::ostream &os, u16 protocol_version)
834 writeU8(os, 1); // version
836 std::ostringstream os2(std::ios::binary);
837 for(u32 i = 0; i < m_content_features.size(); i++)
839 if(i == CONTENT_IGNORE || i == CONTENT_AIR
840 || i == CONTENT_UNKNOWN)
842 ContentFeatures *f = &m_content_features[i];
846 // Wrap it in a string to allow different lengths without
847 // strict version incompatibilities
848 std::ostringstream wrapper_os(std::ios::binary);
849 f->serialize(wrapper_os, protocol_version);
850 os2<<serializeString(wrapper_os.str());
852 assert(count + 1 > count); // must not overflow
856 os<<serializeLongString(os2.str());
858 void deSerialize(std::istream &is)
861 int version = readU8(is);
863 throw SerializationError("unsupported NodeDefinitionManager version");
864 u16 count = readU16(is);
865 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
867 for(u16 n = 0; n < count; n++){
868 u16 i = readU16(is2);
870 // Read it from the string wrapper
871 std::string wrapper = deSerializeString(is2);
872 std::istringstream wrapper_is(wrapper, std::ios::binary);
873 f.deSerialize(wrapper_is);
875 // Check error conditions
876 if(i == CONTENT_IGNORE || i == CONTENT_AIR
877 || i == CONTENT_UNKNOWN){
878 infostream<<"NodeDefManager::deSerialize(): WARNING: "
879 <<"not changing builtin node "<<i
884 infostream<<"NodeDefManager::deSerialize(): WARNING: "
885 <<"received empty name"<<std::endl;
889 bool found = m_name_id_mapping.getId(f.name, existing_id); // ignore aliases
890 if(found && i != existing_id){
891 infostream<<"NodeDefManager::deSerialize(): WARNING: "
892 <<"already defined with different ID: "
897 // All is ok, add node definition with the requested ID
898 if(i >= m_content_features.size())
899 m_content_features.resize((u32)(i) + 1);
900 m_content_features[i] = f;
901 addNameIdMapping(i, f.name);
902 verbosestream<<"deserialized "<<f.name<<std::endl;
906 void addNameIdMapping(content_t i, std::string name)
908 m_name_id_mapping.set(i, name);
909 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
912 // Features indexed by id
913 std::vector<ContentFeatures> m_content_features;
914 // A mapping for fast converting back and forth between names and ids
915 NameIdMapping m_name_id_mapping;
916 // Like m_name_id_mapping, but only from names to ids, and includes
917 // item aliases too. Updated by updateAliases()
918 // Note: Not serialized.
919 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
920 // A mapping from groups to a list of content_ts (and their levels)
921 // that belong to it. Necessary for a direct lookup in getIds().
922 // Note: Not serialized.
923 std::map<std::string, GroupItems> m_group_to_items;
924 // Next possibly free id
928 IWritableNodeDefManager* createNodeDefManager()
930 return new CNodeDefManager();
934 Serialization of old ContentFeatures formats
937 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
939 if(protocol_version == 13)
941 writeU8(os, 5); // version
942 os<<serializeString(name);
943 writeU16(os, groups.size());
944 for(ItemGroupList::const_iterator
945 i = groups.begin(); i != groups.end(); i++){
946 os<<serializeString(i->first);
947 writeS16(os, i->second);
949 writeU8(os, drawtype);
950 writeF1000(os, visual_scale);
952 for(u32 i = 0; i < 6; i++)
953 tiledef[i].serialize(os, protocol_version);
954 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
956 for(u32 i = 0; i < 2; i++){
957 tiledef_special[i].serialize(os, protocol_version);
960 writeU8(os, post_effect_color.getAlpha());
961 writeU8(os, post_effect_color.getRed());
962 writeU8(os, post_effect_color.getGreen());
963 writeU8(os, post_effect_color.getBlue());
964 writeU8(os, param_type);
965 writeU8(os, param_type_2);
966 writeU8(os, is_ground_content);
967 writeU8(os, light_propagates);
968 writeU8(os, sunlight_propagates);
969 writeU8(os, walkable);
970 writeU8(os, pointable);
971 writeU8(os, diggable);
972 writeU8(os, climbable);
973 writeU8(os, buildable_to);
974 os<<serializeString(""); // legacy: used to be metadata_name
975 writeU8(os, liquid_type);
976 os<<serializeString(liquid_alternative_flowing);
977 os<<serializeString(liquid_alternative_source);
978 writeU8(os, liquid_viscosity);
979 writeU8(os, light_source);
980 writeU32(os, damage_per_second);
981 node_box.serialize(os, protocol_version);
982 selection_box.serialize(os, protocol_version);
983 writeU8(os, legacy_facedir_simple);
984 writeU8(os, legacy_wallmounted);
985 serializeSimpleSoundSpec(sound_footstep, os);
986 serializeSimpleSoundSpec(sound_dig, os);
987 serializeSimpleSoundSpec(sound_dug, os);
989 else if (protocol_version > 13 && protocol_version < 24) {
990 writeU8(os, 6); // version
991 os<<serializeString(name);
992 writeU16(os, groups.size());
993 for(ItemGroupList::const_iterator
994 i = groups.begin(); i != groups.end(); i++){
995 os<<serializeString(i->first);
996 writeS16(os, i->second);
998 writeU8(os, drawtype);
999 writeF1000(os, visual_scale);
1001 for(u32 i = 0; i < 6; i++)
1002 tiledef[i].serialize(os, protocol_version);
1003 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1005 for(u32 i = 0; i < 2; i++){
1006 tiledef_special[i].serialize(os, protocol_version);
1009 writeU8(os, post_effect_color.getAlpha());
1010 writeU8(os, post_effect_color.getRed());
1011 writeU8(os, post_effect_color.getGreen());
1012 writeU8(os, post_effect_color.getBlue());
1013 writeU8(os, param_type);
1014 writeU8(os, param_type_2);
1015 writeU8(os, is_ground_content);
1016 writeU8(os, light_propagates);
1017 writeU8(os, sunlight_propagates);
1018 writeU8(os, walkable);
1019 writeU8(os, pointable);
1020 writeU8(os, diggable);
1021 writeU8(os, climbable);
1022 writeU8(os, buildable_to);
1023 os<<serializeString(""); // legacy: used to be metadata_name
1024 writeU8(os, liquid_type);
1025 os<<serializeString(liquid_alternative_flowing);
1026 os<<serializeString(liquid_alternative_source);
1027 writeU8(os, liquid_viscosity);
1028 writeU8(os, liquid_renewable);
1029 writeU8(os, light_source);
1030 writeU32(os, damage_per_second);
1031 node_box.serialize(os, protocol_version);
1032 selection_box.serialize(os, protocol_version);
1033 writeU8(os, legacy_facedir_simple);
1034 writeU8(os, legacy_wallmounted);
1035 serializeSimpleSoundSpec(sound_footstep, os);
1036 serializeSimpleSoundSpec(sound_dig, os);
1037 serializeSimpleSoundSpec(sound_dug, os);
1038 writeU8(os, rightclickable);
1039 writeU8(os, drowning);
1040 writeU8(os, leveled);
1041 writeU8(os, liquid_range);
1043 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
1046 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1048 if(version == 5) // In PROTOCOL_VERSION 13
1050 name = deSerializeString(is);
1052 u32 groups_size = readU16(is);
1053 for(u32 i=0; i<groups_size; i++){
1054 std::string name = deSerializeString(is);
1055 int value = readS16(is);
1056 groups[name] = value;
1058 drawtype = (enum NodeDrawType)readU8(is);
1059 visual_scale = readF1000(is);
1061 throw SerializationError("unsupported tile count");
1062 for(u32 i=0; i<6; i++)
1063 tiledef[i].deSerialize(is);
1064 if(readU8(is) != CF_SPECIAL_COUNT)
1065 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1066 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
1067 tiledef_special[i].deSerialize(is);
1069 post_effect_color.setAlpha(readU8(is));
1070 post_effect_color.setRed(readU8(is));
1071 post_effect_color.setGreen(readU8(is));
1072 post_effect_color.setBlue(readU8(is));
1073 param_type = (enum ContentParamType)readU8(is);
1074 param_type_2 = (enum ContentParamType2)readU8(is);
1075 is_ground_content = readU8(is);
1076 light_propagates = readU8(is);
1077 sunlight_propagates = readU8(is);
1078 walkable = readU8(is);
1079 pointable = readU8(is);
1080 diggable = readU8(is);
1081 climbable = readU8(is);
1082 buildable_to = readU8(is);
1083 deSerializeString(is); // legacy: used to be metadata_name
1084 liquid_type = (enum LiquidType)readU8(is);
1085 liquid_alternative_flowing = deSerializeString(is);
1086 liquid_alternative_source = deSerializeString(is);
1087 liquid_viscosity = readU8(is);
1088 light_source = readU8(is);
1089 damage_per_second = readU32(is);
1090 node_box.deSerialize(is);
1091 selection_box.deSerialize(is);
1092 legacy_facedir_simple = readU8(is);
1093 legacy_wallmounted = readU8(is);
1094 deSerializeSimpleSoundSpec(sound_footstep, is);
1095 deSerializeSimpleSoundSpec(sound_dig, is);
1096 deSerializeSimpleSoundSpec(sound_dug, is);
1097 } else if (version == 6) {
1098 name = deSerializeString(is);
1100 u32 groups_size = readU16(is);
1101 for(u32 i=0; i<groups_size; i++){
1102 std::string name = deSerializeString(is);
1103 int value = readS16(is);
1104 groups[name] = value;
1106 drawtype = (enum NodeDrawType)readU8(is);
1107 visual_scale = readF1000(is);
1109 throw SerializationError("unsupported tile count");
1110 for(u32 i=0; i<6; i++)
1111 tiledef[i].deSerialize(is);
1112 // CF_SPECIAL_COUNT in version 6 = 2
1114 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1115 for(u32 i=0; i<2; i++)
1116 tiledef_special[i].deSerialize(is);
1118 post_effect_color.setAlpha(readU8(is));
1119 post_effect_color.setRed(readU8(is));
1120 post_effect_color.setGreen(readU8(is));
1121 post_effect_color.setBlue(readU8(is));
1122 param_type = (enum ContentParamType)readU8(is);
1123 param_type_2 = (enum ContentParamType2)readU8(is);
1124 is_ground_content = readU8(is);
1125 light_propagates = readU8(is);
1126 sunlight_propagates = readU8(is);
1127 walkable = readU8(is);
1128 pointable = readU8(is);
1129 diggable = readU8(is);
1130 climbable = readU8(is);
1131 buildable_to = readU8(is);
1132 deSerializeString(is); // legacy: used to be metadata_name
1133 liquid_type = (enum LiquidType)readU8(is);
1134 liquid_alternative_flowing = deSerializeString(is);
1135 liquid_alternative_source = deSerializeString(is);
1136 liquid_viscosity = readU8(is);
1137 liquid_renewable = readU8(is);
1138 light_source = readU8(is);
1139 damage_per_second = readU32(is);
1140 node_box.deSerialize(is);
1141 selection_box.deSerialize(is);
1142 legacy_facedir_simple = readU8(is);
1143 legacy_wallmounted = readU8(is);
1144 deSerializeSimpleSoundSpec(sound_footstep, is);
1145 deSerializeSimpleSoundSpec(sound_dig, is);
1146 deSerializeSimpleSoundSpec(sound_dug, is);
1147 rightclickable = readU8(is);
1148 drowning = readU8(is);
1149 leveled = readU8(is);
1150 liquid_range = readU8(is);
1152 throw SerializationError("unsupported ContentFeatures version");