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 "profiler.h" // For TimeTaker
40 type = NODEBOX_REGULAR;
43 // default is sign/ladder-like
44 wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
45 wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
46 wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
49 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
51 int version = protocol_version >= 21 ? 2 : 1;
54 if (version == 1 && type == NODEBOX_LEVELED)
55 writeU8(os, NODEBOX_FIXED);
59 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
61 writeU16(os, fixed.size());
62 for(std::vector<aabb3f>::const_iterator
64 i != fixed.end(); i++)
66 writeV3F1000(os, i->MinEdge);
67 writeV3F1000(os, i->MaxEdge);
70 else if(type == NODEBOX_WALLMOUNTED)
72 writeV3F1000(os, wall_top.MinEdge);
73 writeV3F1000(os, wall_top.MaxEdge);
74 writeV3F1000(os, wall_bottom.MinEdge);
75 writeV3F1000(os, wall_bottom.MaxEdge);
76 writeV3F1000(os, wall_side.MinEdge);
77 writeV3F1000(os, wall_side.MaxEdge);
81 void NodeBox::deSerialize(std::istream &is)
83 int version = readU8(is);
84 if(version < 1 || version > 2)
85 throw SerializationError("unsupported NodeBox version");
89 type = (enum NodeBoxType)readU8(is);
91 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
93 u16 fixed_count = readU16(is);
97 box.MinEdge = readV3F1000(is);
98 box.MaxEdge = readV3F1000(is);
102 else if(type == NODEBOX_WALLMOUNTED)
104 wall_top.MinEdge = readV3F1000(is);
105 wall_top.MaxEdge = readV3F1000(is);
106 wall_bottom.MinEdge = readV3F1000(is);
107 wall_bottom.MaxEdge = readV3F1000(is);
108 wall_side.MinEdge = readV3F1000(is);
109 wall_side.MaxEdge = readV3F1000(is);
117 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
119 if(protocol_version >= 17)
123 os<<serializeString(name);
124 writeU8(os, animation.type);
125 writeU16(os, animation.aspect_w);
126 writeU16(os, animation.aspect_h);
127 writeF1000(os, animation.length);
128 if(protocol_version >= 17)
129 writeU8(os, backface_culling);
132 void TileDef::deSerialize(std::istream &is)
134 int version = readU8(is);
135 name = deSerializeString(is);
136 animation.type = (TileAnimationType)readU8(is);
137 animation.aspect_w = readU16(is);
138 animation.aspect_h = readU16(is);
139 animation.length = readF1000(is);
141 backface_culling = readU8(is);
145 SimpleSoundSpec serialization
148 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
151 os<<serializeString(ss.name);
152 writeF1000(os, ss.gain);
154 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
156 ss.name = deSerializeString(is);
157 ss.gain = readF1000(is);
164 ContentFeatures::ContentFeatures()
169 ContentFeatures::~ContentFeatures()
173 void ContentFeatures::reset()
180 visual_solidness = 0;
181 backface_culling = true;
183 has_on_construct = false;
184 has_on_destruct = false;
185 has_after_destruct = false;
189 NOTE: Most of this is always overridden by the default values given
194 // Unknown nodes can be dug
195 groups["dig_immediate"] = 2;
196 drawtype = NDT_NORMAL;
198 for(u32 i=0; i<6; i++)
199 tiledef[i] = TileDef();
200 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
201 tiledef_special[j] = TileDef();
203 post_effect_color = video::SColor(0, 0, 0, 0);
204 param_type = CPT_NONE;
205 param_type_2 = CPT2_NONE;
206 is_ground_content = false;
207 light_propagates = false;
208 sunlight_propagates = false;
213 buildable_to = false;
214 rightclickable = true;
216 liquid_type = LIQUID_NONE;
217 liquid_alternative_flowing = "";
218 liquid_alternative_source = "";
219 liquid_viscosity = 0;
220 liquid_renewable = true;
222 liquid_range = LIQUID_LEVEL_MAX+1;
225 damage_per_second = 0;
226 node_box = NodeBox();
227 selection_box = NodeBox();
229 legacy_facedir_simple = false;
230 legacy_wallmounted = false;
231 sound_footstep = SimpleSoundSpec();
232 sound_dig = SimpleSoundSpec("__group");
233 sound_dug = SimpleSoundSpec();
236 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
238 if(protocol_version < 14){
239 serializeOld(os, protocol_version);
243 writeU8(os, 6); // version
244 os<<serializeString(name);
245 writeU16(os, groups.size());
246 for(ItemGroupList::const_iterator
247 i = groups.begin(); i != groups.end(); i++){
248 os<<serializeString(i->first);
249 writeS16(os, i->second);
251 writeU8(os, drawtype);
252 writeF1000(os, visual_scale);
254 for(u32 i=0; i<6; i++)
255 tiledef[i].serialize(os, protocol_version);
256 writeU8(os, CF_SPECIAL_COUNT);
257 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
258 tiledef_special[i].serialize(os, protocol_version);
261 writeU8(os, post_effect_color.getAlpha());
262 writeU8(os, post_effect_color.getRed());
263 writeU8(os, post_effect_color.getGreen());
264 writeU8(os, post_effect_color.getBlue());
265 writeU8(os, param_type);
266 writeU8(os, param_type_2);
267 writeU8(os, is_ground_content);
268 writeU8(os, light_propagates);
269 writeU8(os, sunlight_propagates);
270 writeU8(os, walkable);
271 writeU8(os, pointable);
272 writeU8(os, diggable);
273 writeU8(os, climbable);
274 writeU8(os, buildable_to);
275 os<<serializeString(""); // legacy: used to be metadata_name
276 writeU8(os, liquid_type);
277 os<<serializeString(liquid_alternative_flowing);
278 os<<serializeString(liquid_alternative_source);
279 writeU8(os, liquid_viscosity);
280 writeU8(os, liquid_renewable);
281 writeU8(os, light_source);
282 writeU32(os, damage_per_second);
283 node_box.serialize(os, protocol_version);
284 selection_box.serialize(os, protocol_version);
285 writeU8(os, legacy_facedir_simple);
286 writeU8(os, legacy_wallmounted);
287 serializeSimpleSoundSpec(sound_footstep, os);
288 serializeSimpleSoundSpec(sound_dig, os);
289 serializeSimpleSoundSpec(sound_dug, os);
290 writeU8(os, rightclickable);
291 writeU8(os, drowning);
292 writeU8(os, leveled);
293 writeU8(os, liquid_range);
294 // Stuff below should be moved to correct place in a version that otherwise changes
295 // the protocol version
299 void ContentFeatures::deSerialize(std::istream &is)
301 int version = readU8(is);
303 deSerializeOld(is, version);
307 name = deSerializeString(is);
309 u32 groups_size = readU16(is);
310 for(u32 i=0; i<groups_size; i++){
311 std::string name = deSerializeString(is);
312 int value = readS16(is);
313 groups[name] = value;
315 drawtype = (enum NodeDrawType)readU8(is);
316 visual_scale = readF1000(is);
318 throw SerializationError("unsupported tile count");
319 for(u32 i=0; i<6; i++)
320 tiledef[i].deSerialize(is);
321 if(readU8(is) != CF_SPECIAL_COUNT)
322 throw SerializationError("unsupported CF_SPECIAL_COUNT");
323 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
324 tiledef_special[i].deSerialize(is);
326 post_effect_color.setAlpha(readU8(is));
327 post_effect_color.setRed(readU8(is));
328 post_effect_color.setGreen(readU8(is));
329 post_effect_color.setBlue(readU8(is));
330 param_type = (enum ContentParamType)readU8(is);
331 param_type_2 = (enum ContentParamType2)readU8(is);
332 is_ground_content = readU8(is);
333 light_propagates = readU8(is);
334 sunlight_propagates = readU8(is);
335 walkable = readU8(is);
336 pointable = readU8(is);
337 diggable = readU8(is);
338 climbable = readU8(is);
339 buildable_to = readU8(is);
340 deSerializeString(is); // legacy: used to be metadata_name
341 liquid_type = (enum LiquidType)readU8(is);
342 liquid_alternative_flowing = deSerializeString(is);
343 liquid_alternative_source = deSerializeString(is);
344 liquid_viscosity = readU8(is);
345 liquid_renewable = readU8(is);
346 light_source = readU8(is);
347 damage_per_second = readU32(is);
348 node_box.deSerialize(is);
349 selection_box.deSerialize(is);
350 legacy_facedir_simple = readU8(is);
351 legacy_wallmounted = readU8(is);
352 deSerializeSimpleSoundSpec(sound_footstep, is);
353 deSerializeSimpleSoundSpec(sound_dig, is);
354 deSerializeSimpleSoundSpec(sound_dug, is);
355 rightclickable = readU8(is);
356 drowning = readU8(is);
357 leveled = readU8(is);
358 liquid_range = readU8(is);
359 // If you add anything here, insert it primarily inside the try-catch
360 // block to not need to increase the version.
362 // Stuff below should be moved to correct place in a version that
363 // otherwise changes the protocol version
365 }catch(SerializationError &e) {};
372 class CNodeDefManager: public IWritableNodeDefManager
377 m_content_features.clear();
378 m_name_id_mapping.clear();
379 m_name_id_mapping_with_aliases.clear();
380 m_group_to_items.clear();
383 u32 initial_length = 0;
384 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
385 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
386 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
387 m_content_features.resize(initial_length);
389 // Set CONTENT_UNKNOWN
393 // Insert directly into containers
394 content_t c = CONTENT_UNKNOWN;
395 m_content_features[c] = f;
396 addNameIdMapping(c, f.name);
403 f.drawtype = NDT_AIRLIKE;
404 f.param_type = CPT_LIGHT;
405 f.light_propagates = true;
406 f.sunlight_propagates = true;
410 f.buildable_to = true;
411 f.is_ground_content = true;
412 // Insert directly into containers
413 content_t c = CONTENT_AIR;
414 m_content_features[c] = f;
415 addNameIdMapping(c, f.name);
418 // Set CONTENT_IGNORE
422 f.drawtype = NDT_AIRLIKE;
423 f.param_type = CPT_NONE;
424 f.light_propagates = false;
425 f.sunlight_propagates = false;
429 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
430 f.is_ground_content = true;
431 // Insert directly into containers
432 content_t c = CONTENT_IGNORE;
433 m_content_features[c] = f;
434 addNameIdMapping(c, f.name);
441 virtual ~CNodeDefManager()
444 virtual IWritableNodeDefManager* clone()
446 CNodeDefManager *mgr = new CNodeDefManager();
450 virtual const ContentFeatures& get(content_t c) const
452 if(c < m_content_features.size())
453 return m_content_features[c];
455 return m_content_features[CONTENT_UNKNOWN];
457 virtual const ContentFeatures& get(const MapNode &n) const
459 return get(n.getContent());
461 virtual bool getId(const std::string &name, content_t &result) const
463 std::map<std::string, content_t>::const_iterator
464 i = m_name_id_mapping_with_aliases.find(name);
465 if(i == m_name_id_mapping_with_aliases.end())
470 virtual content_t getId(const std::string &name) const
472 content_t id = CONTENT_IGNORE;
476 virtual void getIds(const std::string &name, std::set<content_t> &result)
479 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
480 if(name.substr(0,6) != "group:"){
481 content_t id = CONTENT_IGNORE;
486 std::string group = name.substr(6);
488 std::map<std::string, GroupItems>::const_iterator
489 i = m_group_to_items.find(group);
490 if (i == m_group_to_items.end())
493 const GroupItems &items = i->second;
494 for (GroupItems::const_iterator j = items.begin();
495 j != items.end(); ++j) {
496 if ((*j).second != 0)
497 result.insert((*j).first);
499 //printf("getIds: %dus\n", t.stop());
501 virtual const ContentFeatures& get(const std::string &name) const
503 content_t id = CONTENT_UNKNOWN;
507 // returns CONTENT_IGNORE if no free ID found
508 content_t allocateId()
510 for(content_t id = m_next_id;
511 id >= m_next_id; // overflow?
513 while(id >= m_content_features.size()){
514 m_content_features.push_back(ContentFeatures());
516 const ContentFeatures &f = m_content_features[id];
522 // If we arrive here, an overflow occurred in id.
523 // That means no ID was found
524 return CONTENT_IGNORE;
526 // IWritableNodeDefManager
527 virtual content_t set(const std::string &name,
528 const ContentFeatures &def)
531 assert(name == def.name);
533 // Don't allow redefining ignore (but allow air and unknown)
534 if(name == "ignore"){
535 infostream<<"NodeDefManager: WARNING: Ignoring "
536 <<"CONTENT_IGNORE redefinition"<<std::endl;
537 return CONTENT_IGNORE;
540 content_t id = CONTENT_IGNORE;
541 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
545 if(id == CONTENT_IGNORE){
546 infostream<<"NodeDefManager: WARNING: Absolute "
547 <<"limit reached"<<std::endl;
548 return CONTENT_IGNORE;
550 assert(id != CONTENT_IGNORE);
551 addNameIdMapping(id, name);
553 m_content_features[id] = def;
554 verbosestream<<"NodeDefManager: registering content id \""<<id
555 <<"\": name=\""<<def.name<<"\""<<std::endl;
557 // Add this content to the list of all groups it belongs to
558 // FIXME: This should remove a node from groups it no longer
559 // belongs to when a node is re-registered
560 for (ItemGroupList::const_iterator i = def.groups.begin();
561 i != def.groups.end(); ++i) {
562 std::string group_name = i->first;
564 std::map<std::string, GroupItems>::iterator
565 j = m_group_to_items.find(group_name);
566 if (j == m_group_to_items.end()) {
567 m_group_to_items[group_name].push_back(
568 std::make_pair(id, i->second));
570 GroupItems &items = j->second;
571 items.push_back(std::make_pair(id, i->second));
576 virtual content_t allocateDummy(const std::string &name)
583 virtual void updateAliases(IItemDefManager *idef)
585 std::set<std::string> all = idef->getAll();
586 m_name_id_mapping_with_aliases.clear();
587 for(std::set<std::string>::iterator
588 i = all.begin(); i != all.end(); i++)
590 std::string name = *i;
591 std::string convert_to = idef->getAlias(name);
593 if(m_name_id_mapping.getId(convert_to, id))
595 m_name_id_mapping_with_aliases.insert(
596 std::make_pair(name, id));
600 virtual void updateTextures(ITextureSource *tsrc,
601 IShaderSource *shdsrc)
604 infostream<<"CNodeDefManager::updateTextures(): Updating "
605 <<"textures in node definitions"<<std::endl;
607 bool new_style_water = g_settings->getBool("new_style_water");
608 bool new_style_leaves = g_settings->getBool("new_style_leaves");
609 bool opaque_water = g_settings->getBool("opaque_water");
611 for(u32 i=0; i<m_content_features.size(); i++)
613 ContentFeatures *f = &m_content_features[i];
615 // Figure out the actual tiles to use
617 for(u32 j=0; j<6; j++)
619 tiledef[j] = f->tiledef[j];
620 if(tiledef[j].name == "")
621 tiledef[j].name = "unknown_node.png";
624 bool is_liquid = false;
625 bool is_water_surface = false;
628 material_type = (f->alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
639 assert(f->liquid_type == LIQUID_SOURCE);
646 f->backface_culling = false;
650 case NDT_FLOWINGLIQUID:
651 assert(f->liquid_type == LIQUID_FLOWING);
659 f->visual_solidness = 1;
661 case NDT_GLASSLIKE_FRAMED:
663 f->visual_solidness = 1;
667 f->visual_solidness = 1;
669 case NDT_ALLFACES_OPTIONAL:
670 if(new_style_leaves){
671 f->drawtype = NDT_ALLFACES;
673 f->visual_solidness = 1;
675 f->drawtype = NDT_NORMAL;
677 for(u32 i=0; i<6; i++){
678 tiledef[i].name += std::string("^[noalpha");
682 material_type = TILE_MATERIAL_WAVING_LEAVES;
686 f->backface_culling = false;
688 material_type = TILE_MATERIAL_WAVING_PLANTS;
700 material_type = (f->alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
701 if (f->name == "default:water_source")
702 is_water_surface = true;
705 for(u16 j=0; j<6; j++)
706 tile_shader[j] = shdsrc->getShader("nodes_shader",material_type, f->drawtype);
708 if (is_water_surface)
709 tile_shader[0] = shdsrc->getShader("water_surface_shader",material_type, f->drawtype);
711 // Tiles (fill in f->tiles[])
712 for(u16 j=0; j<6; j++){
714 f->tiles[j].shader_id = tile_shader[j];
716 f->tiles[j].texture = tsrc->getTexture(
718 &f->tiles[j].texture_id);
720 f->tiles[j].alpha = f->alpha;
722 f->tiles[j].material_type = material_type;
724 f->tiles[j].material_flags = 0;
725 if(f->backface_culling)
726 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
727 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
728 f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
729 // Animation parameters
730 if(f->tiles[j].material_flags &
731 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
733 // Get texture size to determine frame count by
735 v2u32 size = f->tiles[j].texture->getOriginalSize();
736 int frame_height = (float)size.X /
737 (float)tiledef[j].animation.aspect_w *
738 (float)tiledef[j].animation.aspect_h;
739 int frame_count = size.Y / frame_height;
740 int frame_length_ms = 1000.0 *
741 tiledef[j].animation.length / frame_count;
742 f->tiles[j].animation_frame_count = frame_count;
743 f->tiles[j].animation_frame_length_ms = frame_length_ms;
745 // If there are no frames for an animation, switch
746 // animation off (so that having specified an animation
747 // for something but not using it in the texture pack
748 // gives no overhead)
749 if(frame_count == 1){
750 f->tiles[j].material_flags &=
751 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
755 // Special tiles (fill in f->special_tiles[])
756 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
758 f->special_tiles[j].shader_id = tile_shader[j];
760 f->special_tiles[j].texture = tsrc->getTexture(
761 f->tiledef_special[j].name,
762 &f->special_tiles[j].texture_id);
764 f->special_tiles[j].alpha = f->alpha;
766 f->special_tiles[j].material_type = material_type;
768 f->special_tiles[j].material_flags = 0;
769 if(f->tiledef_special[j].backface_culling)
770 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
771 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
772 f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
773 // Animation parameters
774 if(f->special_tiles[j].material_flags &
775 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
777 // Get texture size to determine frame count by
779 v2u32 size = f->special_tiles[j].texture->getOriginalSize();
780 int frame_height = (float)size.X /
781 (float)f->tiledef_special[j].animation.aspect_w *
782 (float)f->tiledef_special[j].animation.aspect_h;
783 int frame_count = size.Y / frame_height;
784 int frame_length_ms = 1000.0 *
785 f->tiledef_special[j].animation.length / frame_count;
786 f->special_tiles[j].animation_frame_count = frame_count;
787 f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
789 // If there are no frames for an animation, switch
790 // animation off (so that having specified an animation
791 // for something but not using it in the texture pack
792 // gives no overhead)
793 if(frame_count == 1){
794 f->special_tiles[j].material_flags &=
795 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
802 void serialize(std::ostream &os, u16 protocol_version)
804 writeU8(os, 1); // version
806 std::ostringstream os2(std::ios::binary);
807 for(u32 i=0; i<m_content_features.size(); i++)
809 if(i == CONTENT_IGNORE || i == CONTENT_AIR
810 || i == CONTENT_UNKNOWN)
812 ContentFeatures *f = &m_content_features[i];
816 // Wrap it in a string to allow different lengths without
817 // strict version incompatibilities
818 std::ostringstream wrapper_os(std::ios::binary);
819 f->serialize(wrapper_os, protocol_version);
820 os2<<serializeString(wrapper_os.str());
822 assert(count + 1 > count); // must not overflow
826 os<<serializeLongString(os2.str());
828 void deSerialize(std::istream &is)
831 int version = readU8(is);
833 throw SerializationError("unsupported NodeDefinitionManager version");
834 u16 count = readU16(is);
835 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
837 for(u16 n=0; n<count; n++){
838 u16 i = readU16(is2);
840 // Read it from the string wrapper
841 std::string wrapper = deSerializeString(is2);
842 std::istringstream wrapper_is(wrapper, std::ios::binary);
843 f.deSerialize(wrapper_is);
845 // Check error conditions
846 if(i == CONTENT_IGNORE || i == CONTENT_AIR
847 || i == CONTENT_UNKNOWN){
848 infostream<<"NodeDefManager::deSerialize(): WARNING: "
849 <<"not changing builtin node "<<i
854 infostream<<"NodeDefManager::deSerialize(): WARNING: "
855 <<"received empty name"<<std::endl;
859 bool found = m_name_id_mapping.getId(f.name, existing_id); // ignore aliases
860 if(found && i != existing_id){
861 infostream<<"NodeDefManager::deSerialize(): WARNING: "
862 <<"already defined with different ID: "
867 // All is ok, add node definition with the requested ID
868 if(i >= m_content_features.size())
869 m_content_features.resize((u32)(i) + 1);
870 m_content_features[i] = f;
871 addNameIdMapping(i, f.name);
872 verbosestream<<"deserialized "<<f.name<<std::endl;
876 void addNameIdMapping(content_t i, std::string name)
878 m_name_id_mapping.set(i, name);
879 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
882 // Features indexed by id
883 std::vector<ContentFeatures> m_content_features;
884 // A mapping for fast converting back and forth between names and ids
885 NameIdMapping m_name_id_mapping;
886 // Like m_name_id_mapping, but only from names to ids, and includes
887 // item aliases too. Updated by updateAliases()
888 // Note: Not serialized.
889 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
890 // A mapping from groups to a list of content_ts (and their levels)
891 // that belong to it. Necessary for a direct lookup in getIds().
892 // Note: Not serialized.
893 std::map<std::string, GroupItems> m_group_to_items;
894 // Next possibly free id
898 IWritableNodeDefManager* createNodeDefManager()
900 return new CNodeDefManager();
904 Serialization of old ContentFeatures formats
907 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
909 if(protocol_version == 13)
911 writeU8(os, 5); // version
912 os<<serializeString(name);
913 writeU16(os, groups.size());
914 for(ItemGroupList::const_iterator
915 i = groups.begin(); i != groups.end(); i++){
916 os<<serializeString(i->first);
917 writeS16(os, i->second);
919 writeU8(os, drawtype);
920 writeF1000(os, visual_scale);
922 for(u32 i=0; i<6; i++)
923 tiledef[i].serialize(os, protocol_version);
924 writeU8(os, CF_SPECIAL_COUNT);
925 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
926 tiledef_special[i].serialize(os, protocol_version);
929 writeU8(os, post_effect_color.getAlpha());
930 writeU8(os, post_effect_color.getRed());
931 writeU8(os, post_effect_color.getGreen());
932 writeU8(os, post_effect_color.getBlue());
933 writeU8(os, param_type);
934 writeU8(os, param_type_2);
935 writeU8(os, is_ground_content);
936 writeU8(os, light_propagates);
937 writeU8(os, sunlight_propagates);
938 writeU8(os, walkable);
939 writeU8(os, pointable);
940 writeU8(os, diggable);
941 writeU8(os, climbable);
942 writeU8(os, buildable_to);
943 os<<serializeString(""); // legacy: used to be metadata_name
944 writeU8(os, liquid_type);
945 os<<serializeString(liquid_alternative_flowing);
946 os<<serializeString(liquid_alternative_source);
947 writeU8(os, liquid_viscosity);
948 writeU8(os, light_source);
949 writeU32(os, damage_per_second);
950 node_box.serialize(os, protocol_version);
951 selection_box.serialize(os, protocol_version);
952 writeU8(os, legacy_facedir_simple);
953 writeU8(os, legacy_wallmounted);
954 serializeSimpleSoundSpec(sound_footstep, os);
955 serializeSimpleSoundSpec(sound_dig, os);
956 serializeSimpleSoundSpec(sound_dug, os);
960 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
964 void ContentFeatures::deSerializeOld(std::istream &is, int version)
966 if(version == 5) // In PROTOCOL_VERSION 13
968 name = deSerializeString(is);
970 u32 groups_size = readU16(is);
971 for(u32 i=0; i<groups_size; i++){
972 std::string name = deSerializeString(is);
973 int value = readS16(is);
974 groups[name] = value;
976 drawtype = (enum NodeDrawType)readU8(is);
977 visual_scale = readF1000(is);
979 throw SerializationError("unsupported tile count");
980 for(u32 i=0; i<6; i++)
981 tiledef[i].deSerialize(is);
982 if(readU8(is) != CF_SPECIAL_COUNT)
983 throw SerializationError("unsupported CF_SPECIAL_COUNT");
984 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
985 tiledef_special[i].deSerialize(is);
987 post_effect_color.setAlpha(readU8(is));
988 post_effect_color.setRed(readU8(is));
989 post_effect_color.setGreen(readU8(is));
990 post_effect_color.setBlue(readU8(is));
991 param_type = (enum ContentParamType)readU8(is);
992 param_type_2 = (enum ContentParamType2)readU8(is);
993 is_ground_content = readU8(is);
994 light_propagates = readU8(is);
995 sunlight_propagates = readU8(is);
996 walkable = readU8(is);
997 pointable = readU8(is);
998 diggable = readU8(is);
999 climbable = readU8(is);
1000 buildable_to = readU8(is);
1001 deSerializeString(is); // legacy: used to be metadata_name
1002 liquid_type = (enum LiquidType)readU8(is);
1003 liquid_alternative_flowing = deSerializeString(is);
1004 liquid_alternative_source = deSerializeString(is);
1005 liquid_viscosity = readU8(is);
1006 light_source = readU8(is);
1007 damage_per_second = readU32(is);
1008 node_box.deSerialize(is);
1009 selection_box.deSerialize(is);
1010 legacy_facedir_simple = readU8(is);
1011 legacy_wallmounted = readU8(is);
1012 deSerializeSimpleSoundSpec(sound_footstep, is);
1013 deSerializeSimpleSoundSpec(sound_dig, is);
1014 deSerializeSimpleSoundSpec(sound_dug, is);
1018 throw SerializationError("unsupported ContentFeatures version");