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
30 #include "nameidmapping.h"
31 #include "util/numeric.h"
32 #include "util/serialize.h"
33 #include "exceptions.h"
43 type = NODEBOX_REGULAR;
46 // default is sign/ladder-like
47 wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
48 wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
49 wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
52 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
54 int version = protocol_version >= 21 ? 2 : 1;
57 if (version == 1 && type == NODEBOX_LEVELED)
58 writeU8(os, NODEBOX_FIXED);
62 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
64 writeU16(os, fixed.size());
65 for(std::vector<aabb3f>::const_iterator
67 i != fixed.end(); i++)
69 writeV3F1000(os, i->MinEdge);
70 writeV3F1000(os, i->MaxEdge);
73 else if(type == NODEBOX_WALLMOUNTED)
75 writeV3F1000(os, wall_top.MinEdge);
76 writeV3F1000(os, wall_top.MaxEdge);
77 writeV3F1000(os, wall_bottom.MinEdge);
78 writeV3F1000(os, wall_bottom.MaxEdge);
79 writeV3F1000(os, wall_side.MinEdge);
80 writeV3F1000(os, wall_side.MaxEdge);
84 void NodeBox::deSerialize(std::istream &is)
86 int version = readU8(is);
87 if(version < 1 || version > 2)
88 throw SerializationError("unsupported NodeBox version");
92 type = (enum NodeBoxType)readU8(is);
94 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
96 u16 fixed_count = readU16(is);
100 box.MinEdge = readV3F1000(is);
101 box.MaxEdge = readV3F1000(is);
102 fixed.push_back(box);
105 else if(type == NODEBOX_WALLMOUNTED)
107 wall_top.MinEdge = readV3F1000(is);
108 wall_top.MaxEdge = readV3F1000(is);
109 wall_bottom.MinEdge = readV3F1000(is);
110 wall_bottom.MaxEdge = readV3F1000(is);
111 wall_side.MinEdge = readV3F1000(is);
112 wall_side.MaxEdge = readV3F1000(is);
120 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
122 if(protocol_version >= 17)
126 os<<serializeString(name);
127 writeU8(os, animation.type);
128 writeU16(os, animation.aspect_w);
129 writeU16(os, animation.aspect_h);
130 writeF1000(os, animation.length);
131 if(protocol_version >= 17)
132 writeU8(os, backface_culling);
135 void TileDef::deSerialize(std::istream &is)
137 int version = readU8(is);
138 name = deSerializeString(is);
139 animation.type = (TileAnimationType)readU8(is);
140 animation.aspect_w = readU16(is);
141 animation.aspect_h = readU16(is);
142 animation.length = readF1000(is);
144 backface_culling = readU8(is);
148 SimpleSoundSpec serialization
151 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
154 os<<serializeString(ss.name);
155 writeF1000(os, ss.gain);
157 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
159 ss.name = deSerializeString(is);
160 ss.gain = readF1000(is);
167 ContentFeatures::ContentFeatures()
172 ContentFeatures::~ContentFeatures()
176 void ContentFeatures::reset()
183 visual_solidness = 0;
184 backface_culling = true;
186 has_on_construct = false;
187 has_on_destruct = false;
188 has_after_destruct = false;
192 NOTE: Most of this is always overridden by the default values given
197 // Unknown nodes can be dug
198 groups["dig_immediate"] = 2;
199 drawtype = NDT_NORMAL;
202 for(u32 i = 0; i < 24; i++)
206 for(u32 i = 0; i < 6; i++)
207 tiledef[i] = TileDef();
208 for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
209 tiledef_special[j] = TileDef();
211 post_effect_color = video::SColor(0, 0, 0, 0);
212 param_type = CPT_NONE;
213 param_type_2 = CPT2_NONE;
214 is_ground_content = false;
215 light_propagates = false;
216 sunlight_propagates = false;
221 buildable_to = false;
222 rightclickable = true;
224 liquid_type = LIQUID_NONE;
225 liquid_alternative_flowing = "";
226 liquid_alternative_source = "";
227 liquid_viscosity = 0;
228 liquid_renewable = true;
230 liquid_range = LIQUID_LEVEL_MAX+1;
233 damage_per_second = 0;
234 node_box = NodeBox();
235 selection_box = NodeBox();
237 legacy_facedir_simple = false;
238 legacy_wallmounted = false;
239 sound_footstep = SimpleSoundSpec();
240 sound_dig = SimpleSoundSpec("__group");
241 sound_dug = SimpleSoundSpec();
244 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
246 if(protocol_version < 24){
247 serializeOld(os, protocol_version);
251 writeU8(os, 7); // version
252 os<<serializeString(name);
253 writeU16(os, groups.size());
254 for(ItemGroupList::const_iterator
255 i = groups.begin(); i != groups.end(); i++){
256 os<<serializeString(i->first);
257 writeS16(os, i->second);
259 writeU8(os, drawtype);
260 writeF1000(os, visual_scale);
262 for(u32 i = 0; i < 6; i++)
263 tiledef[i].serialize(os, protocol_version);
264 writeU8(os, CF_SPECIAL_COUNT);
265 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++){
266 tiledef_special[i].serialize(os, protocol_version);
269 writeU8(os, post_effect_color.getAlpha());
270 writeU8(os, post_effect_color.getRed());
271 writeU8(os, post_effect_color.getGreen());
272 writeU8(os, post_effect_color.getBlue());
273 writeU8(os, param_type);
274 writeU8(os, param_type_2);
275 writeU8(os, is_ground_content);
276 writeU8(os, light_propagates);
277 writeU8(os, sunlight_propagates);
278 writeU8(os, walkable);
279 writeU8(os, pointable);
280 writeU8(os, diggable);
281 writeU8(os, climbable);
282 writeU8(os, buildable_to);
283 os<<serializeString(""); // legacy: used to be metadata_name
284 writeU8(os, liquid_type);
285 os<<serializeString(liquid_alternative_flowing);
286 os<<serializeString(liquid_alternative_source);
287 writeU8(os, liquid_viscosity);
288 writeU8(os, liquid_renewable);
289 writeU8(os, light_source);
290 writeU32(os, damage_per_second);
291 node_box.serialize(os, protocol_version);
292 selection_box.serialize(os, protocol_version);
293 writeU8(os, legacy_facedir_simple);
294 writeU8(os, legacy_wallmounted);
295 serializeSimpleSoundSpec(sound_footstep, os);
296 serializeSimpleSoundSpec(sound_dig, os);
297 serializeSimpleSoundSpec(sound_dug, os);
298 writeU8(os, rightclickable);
299 writeU8(os, drowning);
300 writeU8(os, leveled);
301 writeU8(os, liquid_range);
303 // Stuff below should be moved to correct place in a version that otherwise changes
304 // the protocol version
305 os<<serializeString(mesh);
308 void ContentFeatures::deSerialize(std::istream &is)
310 int version = readU8(is);
312 deSerializeOld(is, version);
316 name = deSerializeString(is);
318 u32 groups_size = readU16(is);
319 for(u32 i = 0; i < groups_size; i++){
320 std::string name = deSerializeString(is);
321 int value = readS16(is);
322 groups[name] = value;
324 drawtype = (enum NodeDrawType)readU8(is);
325 visual_scale = readF1000(is);
327 throw SerializationError("unsupported tile count");
328 for(u32 i = 0; i < 6; i++)
329 tiledef[i].deSerialize(is);
330 if(readU8(is) != CF_SPECIAL_COUNT)
331 throw SerializationError("unsupported CF_SPECIAL_COUNT");
332 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
333 tiledef_special[i].deSerialize(is);
335 post_effect_color.setAlpha(readU8(is));
336 post_effect_color.setRed(readU8(is));
337 post_effect_color.setGreen(readU8(is));
338 post_effect_color.setBlue(readU8(is));
339 param_type = (enum ContentParamType)readU8(is);
340 param_type_2 = (enum ContentParamType2)readU8(is);
341 is_ground_content = readU8(is);
342 light_propagates = readU8(is);
343 sunlight_propagates = readU8(is);
344 walkable = readU8(is);
345 pointable = readU8(is);
346 diggable = readU8(is);
347 climbable = readU8(is);
348 buildable_to = readU8(is);
349 deSerializeString(is); // legacy: used to be metadata_name
350 liquid_type = (enum LiquidType)readU8(is);
351 liquid_alternative_flowing = deSerializeString(is);
352 liquid_alternative_source = deSerializeString(is);
353 liquid_viscosity = readU8(is);
354 liquid_renewable = readU8(is);
355 light_source = readU8(is);
356 damage_per_second = readU32(is);
357 node_box.deSerialize(is);
358 selection_box.deSerialize(is);
359 legacy_facedir_simple = readU8(is);
360 legacy_wallmounted = readU8(is);
361 deSerializeSimpleSoundSpec(sound_footstep, is);
362 deSerializeSimpleSoundSpec(sound_dig, is);
363 deSerializeSimpleSoundSpec(sound_dug, is);
364 rightclickable = readU8(is);
365 drowning = readU8(is);
366 leveled = readU8(is);
367 liquid_range = readU8(is);
369 // If you add anything here, insert it primarily inside the try-catch
370 // block to not need to increase the version.
372 // Stuff below should be moved to correct place in a version that
373 // otherwise changes the protocol version
374 mesh = deSerializeString(is);
375 }catch(SerializationError &e) {};
382 class CNodeDefManager: public IWritableNodeDefManager {
385 virtual ~CNodeDefManager();
387 virtual IWritableNodeDefManager *clone();
388 virtual const ContentFeatures& get(content_t c) const;
389 virtual const ContentFeatures& get(const MapNode &n) const;
390 virtual bool getId(const std::string &name, content_t &result) const;
391 virtual content_t getId(const std::string &name) const;
392 virtual void getIds(const std::string &name, std::set<content_t> &result) const;
393 virtual const ContentFeatures& get(const std::string &name) const;
394 content_t allocateId();
395 virtual content_t set(const std::string &name, const ContentFeatures &def);
396 virtual content_t allocateDummy(const std::string &name);
397 virtual void updateAliases(IItemDefManager *idef);
398 virtual void updateTextures(IGameDef *gamedef);
399 void serialize(std::ostream &os, u16 protocol_version);
400 void deSerialize(std::istream &is);
403 void addNameIdMapping(content_t i, std::string name);
405 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
406 u32 shader_id, bool use_normal_texture, bool backface_culling,
407 u8 alpha, u8 material_type);
410 // Features indexed by id
411 std::vector<ContentFeatures> m_content_features;
413 // A mapping for fast converting back and forth between names and ids
414 NameIdMapping m_name_id_mapping;
416 // Like m_name_id_mapping, but only from names to ids, and includes
417 // item aliases too. Updated by updateAliases()
418 // Note: Not serialized.
420 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
422 // A mapping from groups to a list of content_ts (and their levels)
423 // that belong to it. Necessary for a direct lookup in getIds().
424 // Note: Not serialized.
425 std::map<std::string, GroupItems> m_group_to_items;
427 // Next possibly free id
432 CNodeDefManager::CNodeDefManager()
438 CNodeDefManager::~CNodeDefManager()
443 void CNodeDefManager::clear()
445 m_content_features.clear();
446 m_name_id_mapping.clear();
447 m_name_id_mapping_with_aliases.clear();
448 m_group_to_items.clear();
451 u32 initial_length = 0;
452 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
453 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
454 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
455 m_content_features.resize(initial_length);
457 // Set CONTENT_UNKNOWN
461 // Insert directly into containers
462 content_t c = CONTENT_UNKNOWN;
463 m_content_features[c] = f;
464 addNameIdMapping(c, f.name);
471 f.drawtype = NDT_AIRLIKE;
472 f.param_type = CPT_LIGHT;
473 f.light_propagates = true;
474 f.sunlight_propagates = true;
478 f.buildable_to = true;
479 f.is_ground_content = true;
480 // Insert directly into containers
481 content_t c = CONTENT_AIR;
482 m_content_features[c] = f;
483 addNameIdMapping(c, f.name);
486 // Set CONTENT_IGNORE
490 f.drawtype = NDT_AIRLIKE;
491 f.param_type = CPT_NONE;
492 f.light_propagates = false;
493 f.sunlight_propagates = false;
497 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
498 f.is_ground_content = true;
499 // Insert directly into containers
500 content_t c = CONTENT_IGNORE;
501 m_content_features[c] = f;
502 addNameIdMapping(c, f.name);
507 IWritableNodeDefManager *CNodeDefManager::clone()
509 CNodeDefManager *mgr = new CNodeDefManager();
515 const ContentFeatures& CNodeDefManager::get(content_t c) const
517 if (c < m_content_features.size())
518 return m_content_features[c];
520 return m_content_features[CONTENT_UNKNOWN];
524 const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
526 return get(n.getContent());
530 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
532 std::map<std::string, content_t>::const_iterator
533 i = m_name_id_mapping_with_aliases.find(name);
534 if(i == m_name_id_mapping_with_aliases.end())
541 content_t CNodeDefManager::getId(const std::string &name) const
543 content_t id = CONTENT_IGNORE;
549 void CNodeDefManager::getIds(const std::string &name,
550 std::set<content_t> &result) const
552 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
553 if (name.substr(0,6) != "group:") {
554 content_t id = CONTENT_IGNORE;
559 std::string group = name.substr(6);
561 std::map<std::string, GroupItems>::const_iterator
562 i = m_group_to_items.find(group);
563 if (i == m_group_to_items.end())
566 const GroupItems &items = i->second;
567 for (GroupItems::const_iterator j = items.begin();
568 j != items.end(); ++j) {
569 if ((*j).second != 0)
570 result.insert((*j).first);
572 //printf("getIds: %dus\n", t.stop());
576 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
578 content_t id = CONTENT_UNKNOWN;
584 // returns CONTENT_IGNORE if no free ID found
585 content_t CNodeDefManager::allocateId()
587 for (content_t id = m_next_id;
588 id >= m_next_id; // overflow?
590 while (id >= m_content_features.size()) {
591 m_content_features.push_back(ContentFeatures());
593 const ContentFeatures &f = m_content_features[id];
599 // If we arrive here, an overflow occurred in id.
600 // That means no ID was found
601 return CONTENT_IGNORE;
605 // IWritableNodeDefManager
606 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
609 assert(name == def.name);
611 // Don't allow redefining ignore (but allow air and unknown)
612 if (name == "ignore") {
613 infostream << "NodeDefManager: WARNING: Ignoring "
614 "CONTENT_IGNORE redefinition"<<std::endl;
615 return CONTENT_IGNORE;
618 content_t id = CONTENT_IGNORE;
619 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
622 if (id == CONTENT_IGNORE) {
623 infostream << "NodeDefManager: WARNING: Absolute "
624 "limit reached" << std::endl;
625 return CONTENT_IGNORE;
627 assert(id != CONTENT_IGNORE);
628 addNameIdMapping(id, name);
630 m_content_features[id] = def;
631 verbosestream << "NodeDefManager: registering content id \"" << id
632 << "\": name=\"" << def.name << "\""<<std::endl;
634 // Add this content to the list of all groups it belongs to
635 // FIXME: This should remove a node from groups it no longer
636 // belongs to when a node is re-registered
637 for (ItemGroupList::const_iterator i = def.groups.begin();
638 i != def.groups.end(); ++i) {
639 std::string group_name = i->first;
641 std::map<std::string, GroupItems>::iterator
642 j = m_group_to_items.find(group_name);
643 if (j == m_group_to_items.end()) {
644 m_group_to_items[group_name].push_back(
645 std::make_pair(id, i->second));
647 GroupItems &items = j->second;
648 items.push_back(std::make_pair(id, i->second));
655 content_t CNodeDefManager::allocateDummy(const std::string &name)
664 void CNodeDefManager::updateAliases(IItemDefManager *idef)
666 std::set<std::string> all = idef->getAll();
667 m_name_id_mapping_with_aliases.clear();
668 for (std::set<std::string>::iterator
669 i = all.begin(); i != all.end(); i++) {
670 std::string name = *i;
671 std::string convert_to = idef->getAlias(name);
673 if (m_name_id_mapping.getId(convert_to, id)) {
674 m_name_id_mapping_with_aliases.insert(
675 std::make_pair(name, id));
681 void CNodeDefManager::updateTextures(IGameDef *gamedef)
684 infostream << "CNodeDefManager::updateTextures(): Updating "
685 "textures in node definitions" << std::endl;
687 ITextureSource *tsrc = gamedef->tsrc();
688 IShaderSource *shdsrc = gamedef->getShaderSource();
690 bool new_style_water = g_settings->getBool("new_style_water");
691 bool new_style_leaves = g_settings->getBool("new_style_leaves");
692 bool connected_glass = g_settings->getBool("connected_glass");
693 bool opaque_water = g_settings->getBool("opaque_water");
694 bool enable_shaders = g_settings->getBool("enable_shaders");
695 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
696 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
698 bool use_normal_texture = enable_shaders &&
699 (enable_bumpmapping || enable_parallax_occlusion);
701 for (u32 i = 0; i < m_content_features.size(); i++) {
702 ContentFeatures *f = &m_content_features[i];
704 // Figure out the actual tiles to use
706 for (u32 j = 0; j < 6; j++) {
707 tiledef[j] = f->tiledef[j];
708 if (tiledef[j].name == "")
709 tiledef[j].name = "unknown_node.png";
712 bool is_liquid = false;
713 bool is_water_surface = false;
715 u8 material_type = (f->alpha == 255) ?
716 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
718 switch (f->drawtype) {
727 assert(f->liquid_type == LIQUID_SOURCE);
730 if (new_style_water){
734 f->backface_culling = false;
738 case NDT_FLOWINGLIQUID:
739 assert(f->liquid_type == LIQUID_FLOWING);
747 f->visual_solidness = 1;
749 case NDT_GLASSLIKE_FRAMED:
751 f->visual_solidness = 1;
753 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
755 f->visual_solidness = 1;
756 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
760 f->visual_solidness = 1;
762 case NDT_ALLFACES_OPTIONAL:
763 if (new_style_leaves) {
764 f->drawtype = NDT_ALLFACES;
766 f->visual_solidness = 1;
768 f->drawtype = NDT_NORMAL;
770 for (u32 i = 0; i < 6; i++)
771 tiledef[i].name += std::string("^[noalpha");
774 material_type = TILE_MATERIAL_WAVING_LEAVES;
778 f->backface_culling = false;
780 material_type = TILE_MATERIAL_WAVING_PLANTS;
783 f->backface_culling = false;
788 f->backface_culling = false;
800 material_type = (f->alpha == 255) ?
801 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
802 if (f->name == "default:water_source")
803 is_water_surface = true;
807 for (u16 j = 0; j < 6; j++) {
808 tile_shader[j] = shdsrc->getShader("nodes_shader",
809 material_type, f->drawtype);
812 if (is_water_surface) {
813 tile_shader[0] = shdsrc->getShader("water_surface_shader",
814 material_type, f->drawtype);
817 // Tiles (fill in f->tiles[])
818 for (u16 j = 0; j < 6; j++) {
819 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
820 use_normal_texture, f->backface_culling, f->alpha, material_type);
823 // Special tiles (fill in f->special_tiles[])
824 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
825 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
826 tile_shader[j], use_normal_texture,
827 f->tiledef_special[j].backface_culling, f->alpha, material_type);
831 // Read the mesh and apply scale
832 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
833 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
834 scaleMesh(f->mesh_ptr[0], v3f(f->visual_scale,f->visual_scale,f->visual_scale));
835 recalculateBoundingBox(f->mesh_ptr[0]);
838 //Convert regular nodebox nodes to meshnodes
839 //Change the drawtype and apply scale
840 if ((f->drawtype == NDT_NODEBOX) &&
841 ((f->node_box.type == NODEBOX_REGULAR) || (f->node_box.type == NODEBOX_FIXED)) &&
842 (!f->node_box.fixed.empty())) {
843 f->drawtype = NDT_MESH;
844 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
845 scaleMesh(f->mesh_ptr[0], v3f(f->visual_scale,f->visual_scale,f->visual_scale));
846 recalculateBoundingBox(f->mesh_ptr[0]);
849 //Cache 6dfacedir rotated clones of meshes
850 if (f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
851 for (u16 j = 1; j < 24; j++) {
852 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
853 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
854 recalculateBoundingBox(f->mesh_ptr[j]);
863 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
864 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
865 bool backface_culling, u8 alpha, u8 material_type)
867 tile->shader_id = shader_id;
868 tile->texture = tsrc->getTexture(tiledef->name, &tile->texture_id);
870 tile->material_type = material_type;
873 if (use_normal_texture)
874 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
877 tile->material_flags = 0;
878 if (backface_culling)
879 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
880 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
881 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
883 // Animation parameters
885 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
886 // Get texture size to determine frame count by aspect ratio
887 v2u32 size = tile->texture->getOriginalSize();
888 int frame_height = (float)size.X /
889 (float)tiledef->animation.aspect_w *
890 (float)tiledef->animation.aspect_h;
891 frame_count = size.Y / frame_height;
892 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
893 tile->animation_frame_count = frame_count;
894 tile->animation_frame_length_ms = frame_length_ms;
897 if (frame_count == 1) {
898 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
900 std::ostringstream os(std::ios::binary);
901 for (int i = 0; i < frame_count; i++) {
905 os << tiledef->name << "^[verticalframe:"
906 << frame_count << ":" << i;
908 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
909 if (tile->normal_texture)
910 frame.normal_texture = tsrc->getNormalTexture(os.str());
911 tile->frames[i] = frame;
918 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
920 writeU8(os, 1); // version
922 std::ostringstream os2(std::ios::binary);
923 for (u32 i = 0; i < m_content_features.size(); i++) {
924 if (i == CONTENT_IGNORE || i == CONTENT_AIR
925 || i == CONTENT_UNKNOWN)
927 ContentFeatures *f = &m_content_features[i];
931 // Wrap it in a string to allow different lengths without
932 // strict version incompatibilities
933 std::ostringstream wrapper_os(std::ios::binary);
934 f->serialize(wrapper_os, protocol_version);
935 os2<<serializeString(wrapper_os.str());
937 assert(count + 1 > count); // must not overflow
941 os << serializeLongString(os2.str());
945 void CNodeDefManager::deSerialize(std::istream &is)
948 int version = readU8(is);
950 throw SerializationError("unsupported NodeDefinitionManager version");
951 u16 count = readU16(is);
952 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
954 for (u16 n = 0; n < count; n++) {
955 u16 i = readU16(is2);
957 // Read it from the string wrapper
958 std::string wrapper = deSerializeString(is2);
959 std::istringstream wrapper_is(wrapper, std::ios::binary);
960 f.deSerialize(wrapper_is);
962 // Check error conditions
963 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
964 infostream << "NodeDefManager::deSerialize(): WARNING: "
965 "not changing builtin node " << i << std::endl;
969 infostream << "NodeDefManager::deSerialize(): WARNING: "
970 "received empty name" << std::endl;
976 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
977 infostream << "NodeDefManager::deSerialize(): WARNING: "
978 "already defined with different ID: " << f.name << std::endl;
982 // All is ok, add node definition with the requested ID
983 if (i >= m_content_features.size())
984 m_content_features.resize((u32)(i) + 1);
985 m_content_features[i] = f;
986 addNameIdMapping(i, f.name);
987 verbosestream << "deserialized " << f.name << std::endl;
992 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
994 m_name_id_mapping.set(i, name);
995 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
999 IWritableNodeDefManager *createNodeDefManager()
1001 return new CNodeDefManager();
1005 //// Serialization of old ContentFeatures formats
1006 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1008 if (protocol_version == 13)
1010 writeU8(os, 5); // version
1011 os<<serializeString(name);
1012 writeU16(os, groups.size());
1013 for (ItemGroupList::const_iterator
1014 i = groups.begin(); i != groups.end(); i++) {
1015 os<<serializeString(i->first);
1016 writeS16(os, i->second);
1018 writeU8(os, drawtype);
1019 writeF1000(os, visual_scale);
1021 for (u32 i = 0; i < 6; i++)
1022 tiledef[i].serialize(os, protocol_version);
1023 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1025 for (u32 i = 0; i < 2; i++)
1026 tiledef_special[i].serialize(os, protocol_version);
1028 writeU8(os, post_effect_color.getAlpha());
1029 writeU8(os, post_effect_color.getRed());
1030 writeU8(os, post_effect_color.getGreen());
1031 writeU8(os, post_effect_color.getBlue());
1032 writeU8(os, param_type);
1033 writeU8(os, param_type_2);
1034 writeU8(os, is_ground_content);
1035 writeU8(os, light_propagates);
1036 writeU8(os, sunlight_propagates);
1037 writeU8(os, walkable);
1038 writeU8(os, pointable);
1039 writeU8(os, diggable);
1040 writeU8(os, climbable);
1041 writeU8(os, buildable_to);
1042 os<<serializeString(""); // legacy: used to be metadata_name
1043 writeU8(os, liquid_type);
1044 os<<serializeString(liquid_alternative_flowing);
1045 os<<serializeString(liquid_alternative_source);
1046 writeU8(os, liquid_viscosity);
1047 writeU8(os, light_source);
1048 writeU32(os, damage_per_second);
1049 node_box.serialize(os, protocol_version);
1050 selection_box.serialize(os, protocol_version);
1051 writeU8(os, legacy_facedir_simple);
1052 writeU8(os, legacy_wallmounted);
1053 serializeSimpleSoundSpec(sound_footstep, os);
1054 serializeSimpleSoundSpec(sound_dig, os);
1055 serializeSimpleSoundSpec(sound_dug, os);
1057 else if (protocol_version > 13 && protocol_version < 24) {
1058 writeU8(os, 6); // version
1059 os<<serializeString(name);
1060 writeU16(os, groups.size());
1061 for (ItemGroupList::const_iterator
1062 i = groups.begin(); i != groups.end(); i++) {
1063 os<<serializeString(i->first);
1064 writeS16(os, i->second);
1066 writeU8(os, drawtype);
1067 writeF1000(os, visual_scale);
1069 for (u32 i = 0; i < 6; i++)
1070 tiledef[i].serialize(os, protocol_version);
1071 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1073 for (u32 i = 0; i < 2; i++)
1074 tiledef_special[i].serialize(os, protocol_version);
1076 writeU8(os, post_effect_color.getAlpha());
1077 writeU8(os, post_effect_color.getRed());
1078 writeU8(os, post_effect_color.getGreen());
1079 writeU8(os, post_effect_color.getBlue());
1080 writeU8(os, param_type);
1081 writeU8(os, param_type_2);
1082 writeU8(os, is_ground_content);
1083 writeU8(os, light_propagates);
1084 writeU8(os, sunlight_propagates);
1085 writeU8(os, walkable);
1086 writeU8(os, pointable);
1087 writeU8(os, diggable);
1088 writeU8(os, climbable);
1089 writeU8(os, buildable_to);
1090 os<<serializeString(""); // legacy: used to be metadata_name
1091 writeU8(os, liquid_type);
1092 os<<serializeString(liquid_alternative_flowing);
1093 os<<serializeString(liquid_alternative_source);
1094 writeU8(os, liquid_viscosity);
1095 writeU8(os, liquid_renewable);
1096 writeU8(os, light_source);
1097 writeU32(os, damage_per_second);
1098 node_box.serialize(os, protocol_version);
1099 selection_box.serialize(os, protocol_version);
1100 writeU8(os, legacy_facedir_simple);
1101 writeU8(os, legacy_wallmounted);
1102 serializeSimpleSoundSpec(sound_footstep, os);
1103 serializeSimpleSoundSpec(sound_dig, os);
1104 serializeSimpleSoundSpec(sound_dug, os);
1105 writeU8(os, rightclickable);
1106 writeU8(os, drowning);
1107 writeU8(os, leveled);
1108 writeU8(os, liquid_range);
1110 throw SerializationError("ContentFeatures::serialize(): "
1111 "Unsupported version requested");
1115 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1117 if (version == 5) // In PROTOCOL_VERSION 13
1119 name = deSerializeString(is);
1121 u32 groups_size = readU16(is);
1122 for(u32 i=0; i<groups_size; i++){
1123 std::string name = deSerializeString(is);
1124 int value = readS16(is);
1125 groups[name] = value;
1127 drawtype = (enum NodeDrawType)readU8(is);
1128 visual_scale = readF1000(is);
1129 if (readU8(is) != 6)
1130 throw SerializationError("unsupported tile count");
1131 for (u32 i = 0; i < 6; i++)
1132 tiledef[i].deSerialize(is);
1133 if (readU8(is) != CF_SPECIAL_COUNT)
1134 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1135 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1136 tiledef_special[i].deSerialize(is);
1138 post_effect_color.setAlpha(readU8(is));
1139 post_effect_color.setRed(readU8(is));
1140 post_effect_color.setGreen(readU8(is));
1141 post_effect_color.setBlue(readU8(is));
1142 param_type = (enum ContentParamType)readU8(is);
1143 param_type_2 = (enum ContentParamType2)readU8(is);
1144 is_ground_content = readU8(is);
1145 light_propagates = readU8(is);
1146 sunlight_propagates = readU8(is);
1147 walkable = readU8(is);
1148 pointable = readU8(is);
1149 diggable = readU8(is);
1150 climbable = readU8(is);
1151 buildable_to = readU8(is);
1152 deSerializeString(is); // legacy: used to be metadata_name
1153 liquid_type = (enum LiquidType)readU8(is);
1154 liquid_alternative_flowing = deSerializeString(is);
1155 liquid_alternative_source = deSerializeString(is);
1156 liquid_viscosity = readU8(is);
1157 light_source = readU8(is);
1158 damage_per_second = readU32(is);
1159 node_box.deSerialize(is);
1160 selection_box.deSerialize(is);
1161 legacy_facedir_simple = readU8(is);
1162 legacy_wallmounted = readU8(is);
1163 deSerializeSimpleSoundSpec(sound_footstep, is);
1164 deSerializeSimpleSoundSpec(sound_dig, is);
1165 deSerializeSimpleSoundSpec(sound_dug, is);
1166 } else if (version == 6) {
1167 name = deSerializeString(is);
1169 u32 groups_size = readU16(is);
1170 for (u32 i = 0; i < groups_size; i++) {
1171 std::string name = deSerializeString(is);
1172 int value = readS16(is);
1173 groups[name] = value;
1175 drawtype = (enum NodeDrawType)readU8(is);
1176 visual_scale = readF1000(is);
1177 if (readU8(is) != 6)
1178 throw SerializationError("unsupported tile count");
1179 for (u32 i = 0; i < 6; i++)
1180 tiledef[i].deSerialize(is);
1181 // CF_SPECIAL_COUNT in version 6 = 2
1182 if (readU8(is) != 2)
1183 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1184 for (u32 i = 0; i < 2; i++)
1185 tiledef_special[i].deSerialize(is);
1187 post_effect_color.setAlpha(readU8(is));
1188 post_effect_color.setRed(readU8(is));
1189 post_effect_color.setGreen(readU8(is));
1190 post_effect_color.setBlue(readU8(is));
1191 param_type = (enum ContentParamType)readU8(is);
1192 param_type_2 = (enum ContentParamType2)readU8(is);
1193 is_ground_content = readU8(is);
1194 light_propagates = readU8(is);
1195 sunlight_propagates = readU8(is);
1196 walkable = readU8(is);
1197 pointable = readU8(is);
1198 diggable = readU8(is);
1199 climbable = readU8(is);
1200 buildable_to = readU8(is);
1201 deSerializeString(is); // legacy: used to be metadata_name
1202 liquid_type = (enum LiquidType)readU8(is);
1203 liquid_alternative_flowing = deSerializeString(is);
1204 liquid_alternative_source = deSerializeString(is);
1205 liquid_viscosity = readU8(is);
1206 liquid_renewable = readU8(is);
1207 light_source = readU8(is);
1208 damage_per_second = readU32(is);
1209 node_box.deSerialize(is);
1210 selection_box.deSerialize(is);
1211 legacy_facedir_simple = readU8(is);
1212 legacy_wallmounted = readU8(is);
1213 deSerializeSimpleSoundSpec(sound_footstep, is);
1214 deSerializeSimpleSoundSpec(sound_dig, is);
1215 deSerializeSimpleSoundSpec(sound_dug, is);
1216 rightclickable = readU8(is);
1217 drowning = readU8(is);
1218 leveled = readU8(is);
1219 liquid_range = readU8(is);
1221 throw SerializationError("unsupported ContentFeatures version");