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
27 #include <IMeshManipulator.h>
31 #include "nameidmapping.h"
32 #include "util/numeric.h"
33 #include "util/serialize.h"
34 #include "exceptions.h"
44 type = NODEBOX_REGULAR;
47 // default is sign/ladder-like
48 wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
49 wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
50 wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
53 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
55 int version = protocol_version >= 21 ? 2 : 1;
58 if (version == 1 && type == NODEBOX_LEVELED)
59 writeU8(os, NODEBOX_FIXED);
63 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
65 writeU16(os, fixed.size());
66 for(std::vector<aabb3f>::const_iterator
68 i != fixed.end(); i++)
70 writeV3F1000(os, i->MinEdge);
71 writeV3F1000(os, i->MaxEdge);
74 else if(type == NODEBOX_WALLMOUNTED)
76 writeV3F1000(os, wall_top.MinEdge);
77 writeV3F1000(os, wall_top.MaxEdge);
78 writeV3F1000(os, wall_bottom.MinEdge);
79 writeV3F1000(os, wall_bottom.MaxEdge);
80 writeV3F1000(os, wall_side.MinEdge);
81 writeV3F1000(os, wall_side.MaxEdge);
85 void NodeBox::deSerialize(std::istream &is)
87 int version = readU8(is);
88 if(version < 1 || version > 2)
89 throw SerializationError("unsupported NodeBox version");
93 type = (enum NodeBoxType)readU8(is);
95 if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
97 u16 fixed_count = readU16(is);
101 box.MinEdge = readV3F1000(is);
102 box.MaxEdge = readV3F1000(is);
103 fixed.push_back(box);
106 else if(type == NODEBOX_WALLMOUNTED)
108 wall_top.MinEdge = readV3F1000(is);
109 wall_top.MaxEdge = readV3F1000(is);
110 wall_bottom.MinEdge = readV3F1000(is);
111 wall_bottom.MaxEdge = readV3F1000(is);
112 wall_side.MinEdge = readV3F1000(is);
113 wall_side.MaxEdge = readV3F1000(is);
121 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
123 if(protocol_version >= 17)
127 os<<serializeString(name);
128 writeU8(os, animation.type);
129 writeU16(os, animation.aspect_w);
130 writeU16(os, animation.aspect_h);
131 writeF1000(os, animation.length);
132 if(protocol_version >= 17)
133 writeU8(os, backface_culling);
136 void TileDef::deSerialize(std::istream &is)
138 int version = readU8(is);
139 name = deSerializeString(is);
140 animation.type = (TileAnimationType)readU8(is);
141 animation.aspect_w = readU16(is);
142 animation.aspect_h = readU16(is);
143 animation.length = readF1000(is);
145 backface_culling = readU8(is);
149 SimpleSoundSpec serialization
152 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
155 os<<serializeString(ss.name);
156 writeF1000(os, ss.gain);
158 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
160 ss.name = deSerializeString(is);
161 ss.gain = readF1000(is);
168 ContentFeatures::ContentFeatures()
173 ContentFeatures::~ContentFeatures()
177 void ContentFeatures::reset()
184 visual_solidness = 0;
185 backface_culling = true;
187 has_on_construct = false;
188 has_on_destruct = false;
189 has_after_destruct = false;
193 NOTE: Most of this is always overridden by the default values given
198 // Unknown nodes can be dug
199 groups["dig_immediate"] = 2;
200 drawtype = NDT_NORMAL;
203 for(u32 i = 0; i < 24; i++)
207 for(u32 i = 0; i < 6; i++)
208 tiledef[i] = TileDef();
209 for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
210 tiledef_special[j] = TileDef();
212 post_effect_color = video::SColor(0, 0, 0, 0);
213 param_type = CPT_NONE;
214 param_type_2 = CPT2_NONE;
215 is_ground_content = false;
216 light_propagates = false;
217 sunlight_propagates = false;
222 buildable_to = false;
223 rightclickable = true;
225 liquid_type = LIQUID_NONE;
226 liquid_alternative_flowing = "";
227 liquid_alternative_source = "";
228 liquid_viscosity = 0;
229 liquid_renewable = true;
231 liquid_range = LIQUID_LEVEL_MAX+1;
234 damage_per_second = 0;
235 node_box = NodeBox();
236 selection_box = NodeBox();
237 collision_box = NodeBox();
239 legacy_facedir_simple = false;
240 legacy_wallmounted = false;
241 sound_footstep = SimpleSoundSpec();
242 sound_dig = SimpleSoundSpec("__group");
243 sound_dug = SimpleSoundSpec();
246 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
248 if(protocol_version < 24){
249 serializeOld(os, protocol_version);
253 writeU8(os, 7); // version
254 os<<serializeString(name);
255 writeU16(os, groups.size());
256 for(ItemGroupList::const_iterator
257 i = groups.begin(); i != groups.end(); i++){
258 os<<serializeString(i->first);
259 writeS16(os, i->second);
261 writeU8(os, drawtype);
262 writeF1000(os, visual_scale);
264 for(u32 i = 0; i < 6; i++)
265 tiledef[i].serialize(os, protocol_version);
266 writeU8(os, CF_SPECIAL_COUNT);
267 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++){
268 tiledef_special[i].serialize(os, protocol_version);
271 writeU8(os, post_effect_color.getAlpha());
272 writeU8(os, post_effect_color.getRed());
273 writeU8(os, post_effect_color.getGreen());
274 writeU8(os, post_effect_color.getBlue());
275 writeU8(os, param_type);
276 writeU8(os, param_type_2);
277 writeU8(os, is_ground_content);
278 writeU8(os, light_propagates);
279 writeU8(os, sunlight_propagates);
280 writeU8(os, walkable);
281 writeU8(os, pointable);
282 writeU8(os, diggable);
283 writeU8(os, climbable);
284 writeU8(os, buildable_to);
285 os<<serializeString(""); // legacy: used to be metadata_name
286 writeU8(os, liquid_type);
287 os<<serializeString(liquid_alternative_flowing);
288 os<<serializeString(liquid_alternative_source);
289 writeU8(os, liquid_viscosity);
290 writeU8(os, liquid_renewable);
291 writeU8(os, light_source);
292 writeU32(os, damage_per_second);
293 node_box.serialize(os, protocol_version);
294 selection_box.serialize(os, protocol_version);
295 writeU8(os, legacy_facedir_simple);
296 writeU8(os, legacy_wallmounted);
297 serializeSimpleSoundSpec(sound_footstep, os);
298 serializeSimpleSoundSpec(sound_dig, os);
299 serializeSimpleSoundSpec(sound_dug, os);
300 writeU8(os, rightclickable);
301 writeU8(os, drowning);
302 writeU8(os, leveled);
303 writeU8(os, liquid_range);
305 // Stuff below should be moved to correct place in a version that otherwise changes
306 // the protocol version
307 os<<serializeString(mesh);
308 collision_box.serialize(os, protocol_version);
311 void ContentFeatures::deSerialize(std::istream &is)
313 int version = readU8(is);
315 deSerializeOld(is, version);
319 name = deSerializeString(is);
321 u32 groups_size = readU16(is);
322 for(u32 i = 0; i < groups_size; i++){
323 std::string name = deSerializeString(is);
324 int value = readS16(is);
325 groups[name] = value;
327 drawtype = (enum NodeDrawType)readU8(is);
328 visual_scale = readF1000(is);
330 throw SerializationError("unsupported tile count");
331 for(u32 i = 0; i < 6; i++)
332 tiledef[i].deSerialize(is);
333 if(readU8(is) != CF_SPECIAL_COUNT)
334 throw SerializationError("unsupported CF_SPECIAL_COUNT");
335 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
336 tiledef_special[i].deSerialize(is);
338 post_effect_color.setAlpha(readU8(is));
339 post_effect_color.setRed(readU8(is));
340 post_effect_color.setGreen(readU8(is));
341 post_effect_color.setBlue(readU8(is));
342 param_type = (enum ContentParamType)readU8(is);
343 param_type_2 = (enum ContentParamType2)readU8(is);
344 is_ground_content = readU8(is);
345 light_propagates = readU8(is);
346 sunlight_propagates = readU8(is);
347 walkable = readU8(is);
348 pointable = readU8(is);
349 diggable = readU8(is);
350 climbable = readU8(is);
351 buildable_to = readU8(is);
352 deSerializeString(is); // legacy: used to be metadata_name
353 liquid_type = (enum LiquidType)readU8(is);
354 liquid_alternative_flowing = deSerializeString(is);
355 liquid_alternative_source = deSerializeString(is);
356 liquid_viscosity = readU8(is);
357 liquid_renewable = readU8(is);
358 light_source = readU8(is);
359 damage_per_second = readU32(is);
360 node_box.deSerialize(is);
361 selection_box.deSerialize(is);
362 legacy_facedir_simple = readU8(is);
363 legacy_wallmounted = readU8(is);
364 deSerializeSimpleSoundSpec(sound_footstep, is);
365 deSerializeSimpleSoundSpec(sound_dig, is);
366 deSerializeSimpleSoundSpec(sound_dug, is);
367 rightclickable = readU8(is);
368 drowning = readU8(is);
369 leveled = readU8(is);
370 liquid_range = readU8(is);
372 // If you add anything here, insert it primarily inside the try-catch
373 // block to not need to increase the version.
375 // Stuff below should be moved to correct place in a version that
376 // otherwise changes the protocol version
377 mesh = deSerializeString(is);
378 collision_box.deSerialize(is);
379 }catch(SerializationError &e) {};
386 class CNodeDefManager: public IWritableNodeDefManager {
389 virtual ~CNodeDefManager();
391 virtual IWritableNodeDefManager *clone();
392 inline virtual const ContentFeatures& get(content_t c) const;
393 inline virtual const ContentFeatures& get(const MapNode &n) const;
394 virtual bool getId(const std::string &name, content_t &result) const;
395 virtual content_t getId(const std::string &name) const;
396 virtual void getIds(const std::string &name, std::set<content_t> &result) const;
397 virtual const ContentFeatures& get(const std::string &name) const;
398 content_t allocateId();
399 virtual content_t set(const std::string &name, const ContentFeatures &def);
400 virtual content_t allocateDummy(const std::string &name);
401 virtual void updateAliases(IItemDefManager *idef);
402 virtual void updateTextures(IGameDef *gamedef);
403 void serialize(std::ostream &os, u16 protocol_version);
404 void deSerialize(std::istream &is);
405 virtual NodeResolver *getResolver();
408 void addNameIdMapping(content_t i, std::string name);
410 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
411 u32 shader_id, bool use_normal_texture, bool backface_culling,
412 u8 alpha, u8 material_type);
415 // Features indexed by id
416 std::vector<ContentFeatures> m_content_features;
418 // A mapping for fast converting back and forth between names and ids
419 NameIdMapping m_name_id_mapping;
421 // Like m_name_id_mapping, but only from names to ids, and includes
422 // item aliases too. Updated by updateAliases()
423 // Note: Not serialized.
425 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
427 // A mapping from groups to a list of content_ts (and their levels)
428 // that belong to it. Necessary for a direct lookup in getIds().
429 // Note: Not serialized.
430 std::map<std::string, GroupItems> m_group_to_items;
432 // Next possibly free id
435 // NodeResolver to queue pending node resolutions
436 NodeResolver m_resolver;
440 CNodeDefManager::CNodeDefManager() :
447 CNodeDefManager::~CNodeDefManager()
450 for (u32 i = 0; i < m_content_features.size(); i++) {
451 ContentFeatures *f = &m_content_features[i];
452 for (u32 j = 0; j < 24; j++) {
454 f->mesh_ptr[j]->drop();
461 void CNodeDefManager::clear()
463 m_content_features.clear();
464 m_name_id_mapping.clear();
465 m_name_id_mapping_with_aliases.clear();
466 m_group_to_items.clear();
469 u32 initial_length = 0;
470 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
471 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
472 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
473 m_content_features.resize(initial_length);
475 // Set CONTENT_UNKNOWN
479 // Insert directly into containers
480 content_t c = CONTENT_UNKNOWN;
481 m_content_features[c] = f;
482 addNameIdMapping(c, f.name);
489 f.drawtype = NDT_AIRLIKE;
490 f.param_type = CPT_LIGHT;
491 f.light_propagates = true;
492 f.sunlight_propagates = true;
496 f.buildable_to = true;
497 f.is_ground_content = true;
498 // Insert directly into containers
499 content_t c = CONTENT_AIR;
500 m_content_features[c] = f;
501 addNameIdMapping(c, f.name);
504 // Set CONTENT_IGNORE
508 f.drawtype = NDT_AIRLIKE;
509 f.param_type = CPT_NONE;
510 f.light_propagates = false;
511 f.sunlight_propagates = false;
515 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
516 f.is_ground_content = true;
517 // Insert directly into containers
518 content_t c = CONTENT_IGNORE;
519 m_content_features[c] = f;
520 addNameIdMapping(c, f.name);
525 IWritableNodeDefManager *CNodeDefManager::clone()
527 CNodeDefManager *mgr = new CNodeDefManager();
533 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
535 return c < m_content_features.size()
536 ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
540 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
542 return get(n.getContent());
546 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
548 std::map<std::string, content_t>::const_iterator
549 i = m_name_id_mapping_with_aliases.find(name);
550 if(i == m_name_id_mapping_with_aliases.end())
557 content_t CNodeDefManager::getId(const std::string &name) const
559 content_t id = CONTENT_IGNORE;
565 void CNodeDefManager::getIds(const std::string &name,
566 std::set<content_t> &result) const
568 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
569 if (name.substr(0,6) != "group:") {
570 content_t id = CONTENT_IGNORE;
575 std::string group = name.substr(6);
577 std::map<std::string, GroupItems>::const_iterator
578 i = m_group_to_items.find(group);
579 if (i == m_group_to_items.end())
582 const GroupItems &items = i->second;
583 for (GroupItems::const_iterator j = items.begin();
584 j != items.end(); ++j) {
585 if ((*j).second != 0)
586 result.insert((*j).first);
588 //printf("getIds: %dus\n", t.stop());
592 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
594 content_t id = CONTENT_UNKNOWN;
600 // returns CONTENT_IGNORE if no free ID found
601 content_t CNodeDefManager::allocateId()
603 for (content_t id = m_next_id;
604 id >= m_next_id; // overflow?
606 while (id >= m_content_features.size()) {
607 m_content_features.push_back(ContentFeatures());
609 const ContentFeatures &f = m_content_features[id];
615 // If we arrive here, an overflow occurred in id.
616 // That means no ID was found
617 return CONTENT_IGNORE;
621 // IWritableNodeDefManager
622 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
625 assert(name == def.name);
627 // Don't allow redefining ignore (but allow air and unknown)
628 if (name == "ignore") {
629 infostream << "NodeDefManager: WARNING: Ignoring "
630 "CONTENT_IGNORE redefinition"<<std::endl;
631 return CONTENT_IGNORE;
634 content_t id = CONTENT_IGNORE;
635 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
638 if (id == CONTENT_IGNORE) {
639 infostream << "NodeDefManager: WARNING: Absolute "
640 "limit reached" << std::endl;
641 return CONTENT_IGNORE;
643 assert(id != CONTENT_IGNORE);
644 addNameIdMapping(id, name);
646 m_content_features[id] = def;
647 verbosestream << "NodeDefManager: registering content id \"" << id
648 << "\": name=\"" << def.name << "\""<<std::endl;
650 // Add this content to the list of all groups it belongs to
651 // FIXME: This should remove a node from groups it no longer
652 // belongs to when a node is re-registered
653 for (ItemGroupList::const_iterator i = def.groups.begin();
654 i != def.groups.end(); ++i) {
655 std::string group_name = i->first;
657 std::map<std::string, GroupItems>::iterator
658 j = m_group_to_items.find(group_name);
659 if (j == m_group_to_items.end()) {
660 m_group_to_items[group_name].push_back(
661 std::make_pair(id, i->second));
663 GroupItems &items = j->second;
664 items.push_back(std::make_pair(id, i->second));
671 content_t CNodeDefManager::allocateDummy(const std::string &name)
680 void CNodeDefManager::updateAliases(IItemDefManager *idef)
682 std::set<std::string> all = idef->getAll();
683 m_name_id_mapping_with_aliases.clear();
684 for (std::set<std::string>::iterator
685 i = all.begin(); i != all.end(); i++) {
686 std::string name = *i;
687 std::string convert_to = idef->getAlias(name);
689 if (m_name_id_mapping.getId(convert_to, id)) {
690 m_name_id_mapping_with_aliases.insert(
691 std::make_pair(name, id));
697 void CNodeDefManager::updateTextures(IGameDef *gamedef)
700 infostream << "CNodeDefManager::updateTextures(): Updating "
701 "textures in node definitions" << std::endl;
703 ITextureSource *tsrc = gamedef->tsrc();
704 IShaderSource *shdsrc = gamedef->getShaderSource();
705 scene::ISceneManager* smgr = gamedef->getSceneManager();
706 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
708 bool new_style_water = g_settings->getBool("new_style_water");
709 bool new_style_leaves = g_settings->getBool("new_style_leaves");
710 bool connected_glass = g_settings->getBool("connected_glass");
711 bool opaque_water = g_settings->getBool("opaque_water");
712 bool enable_shaders = g_settings->getBool("enable_shaders");
713 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
714 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
715 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
717 bool use_normal_texture = enable_shaders &&
718 (enable_bumpmapping || enable_parallax_occlusion);
720 for (u32 i = 0; i < m_content_features.size(); i++) {
721 ContentFeatures *f = &m_content_features[i];
723 // Figure out the actual tiles to use
725 for (u32 j = 0; j < 6; j++) {
726 tiledef[j] = f->tiledef[j];
727 if (tiledef[j].name == "")
728 tiledef[j].name = "unknown_node.png";
731 bool is_liquid = false;
732 bool is_water_surface = false;
734 u8 material_type = (f->alpha == 255) ?
735 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
737 switch (f->drawtype) {
746 assert(f->liquid_type == LIQUID_SOURCE);
749 if (new_style_water){
753 f->backface_culling = false;
757 case NDT_FLOWINGLIQUID:
758 assert(f->liquid_type == LIQUID_FLOWING);
766 f->visual_solidness = 1;
768 case NDT_GLASSLIKE_FRAMED:
770 f->visual_solidness = 1;
772 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
774 f->visual_solidness = 1;
775 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
779 f->visual_solidness = 1;
781 case NDT_ALLFACES_OPTIONAL:
782 if (new_style_leaves) {
783 f->drawtype = NDT_ALLFACES;
785 f->visual_solidness = 1;
787 f->drawtype = NDT_NORMAL;
789 for (u32 i = 0; i < 6; i++)
790 tiledef[i].name += std::string("^[noalpha");
793 material_type = TILE_MATERIAL_WAVING_LEAVES;
797 f->backface_culling = false;
799 material_type = TILE_MATERIAL_WAVING_PLANTS;
802 f->backface_culling = false;
807 f->backface_culling = false;
819 material_type = (f->alpha == 255) ?
820 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
821 if (f->name == "default:water_source")
822 is_water_surface = true;
826 for (u16 j = 0; j < 6; j++) {
827 tile_shader[j] = shdsrc->getShader("nodes_shader",
828 material_type, f->drawtype);
831 if (is_water_surface) {
832 tile_shader[0] = shdsrc->getShader("water_surface_shader",
833 material_type, f->drawtype);
836 // Tiles (fill in f->tiles[])
837 for (u16 j = 0; j < 6; j++) {
838 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
839 use_normal_texture, f->backface_culling, f->alpha, material_type);
842 // Special tiles (fill in f->special_tiles[])
843 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
844 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
845 tile_shader[j], use_normal_texture,
846 f->tiledef_special[j].backface_culling, f->alpha, material_type);
849 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
851 // Read the mesh and apply scale
852 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
854 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
855 scaleMesh(f->mesh_ptr[0], scale);
856 recalculateBoundingBox(f->mesh_ptr[0]);
857 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
859 } else if ((f->drawtype == NDT_NODEBOX) &&
860 ((f->node_box.type == NODEBOX_REGULAR) ||
861 (f->node_box.type == NODEBOX_FIXED)) &&
862 (!f->node_box.fixed.empty())) {
863 //Convert regular nodebox nodes to meshnodes
864 //Change the drawtype and apply scale
865 f->drawtype = NDT_MESH;
866 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
867 v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
868 scaleMesh(f->mesh_ptr[0], scale);
869 recalculateBoundingBox(f->mesh_ptr[0]);
870 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
873 //Cache 6dfacedir and wallmounted rotated clones of meshes
874 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
875 for (u16 j = 1; j < 24; j++) {
876 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
877 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
878 recalculateBoundingBox(f->mesh_ptr[j]);
879 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
881 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
882 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
883 for (u16 j = 1; j < 6; j++) {
884 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
885 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
886 recalculateBoundingBox(f->mesh_ptr[j]);
887 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
889 rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
890 recalculateBoundingBox(f->mesh_ptr[0]);
891 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
899 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
900 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
901 bool backface_culling, u8 alpha, u8 material_type)
903 tile->shader_id = shader_id;
904 tile->texture = tsrc->getTexture(tiledef->name, &tile->texture_id);
906 tile->material_type = material_type;
909 if (use_normal_texture)
910 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
913 tile->material_flags = 0;
914 if (backface_culling)
915 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
916 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
917 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
919 // Animation parameters
921 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
922 // Get texture size to determine frame count by aspect ratio
923 v2u32 size = tile->texture->getOriginalSize();
924 int frame_height = (float)size.X /
925 (float)tiledef->animation.aspect_w *
926 (float)tiledef->animation.aspect_h;
927 frame_count = size.Y / frame_height;
928 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
929 tile->animation_frame_count = frame_count;
930 tile->animation_frame_length_ms = frame_length_ms;
933 if (frame_count == 1) {
934 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
936 std::ostringstream os(std::ios::binary);
937 for (int i = 0; i < frame_count; i++) {
941 os << tiledef->name << "^[verticalframe:"
942 << frame_count << ":" << i;
944 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
945 if (tile->normal_texture)
946 frame.normal_texture = tsrc->getNormalTexture(os.str());
947 tile->frames[i] = frame;
954 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
956 writeU8(os, 1); // version
958 std::ostringstream os2(std::ios::binary);
959 for (u32 i = 0; i < m_content_features.size(); i++) {
960 if (i == CONTENT_IGNORE || i == CONTENT_AIR
961 || i == CONTENT_UNKNOWN)
963 ContentFeatures *f = &m_content_features[i];
967 // Wrap it in a string to allow different lengths without
968 // strict version incompatibilities
969 std::ostringstream wrapper_os(std::ios::binary);
970 f->serialize(wrapper_os, protocol_version);
971 os2<<serializeString(wrapper_os.str());
973 assert(count + 1 > count); // must not overflow
977 os << serializeLongString(os2.str());
981 void CNodeDefManager::deSerialize(std::istream &is)
984 int version = readU8(is);
986 throw SerializationError("unsupported NodeDefinitionManager version");
987 u16 count = readU16(is);
988 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
990 for (u16 n = 0; n < count; n++) {
991 u16 i = readU16(is2);
993 // Read it from the string wrapper
994 std::string wrapper = deSerializeString(is2);
995 std::istringstream wrapper_is(wrapper, std::ios::binary);
996 f.deSerialize(wrapper_is);
998 // Check error conditions
999 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1000 infostream << "NodeDefManager::deSerialize(): WARNING: "
1001 "not changing builtin node " << i << std::endl;
1005 infostream << "NodeDefManager::deSerialize(): WARNING: "
1006 "received empty name" << std::endl;
1012 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1013 infostream << "NodeDefManager::deSerialize(): WARNING: "
1014 "already defined with different ID: " << f.name << std::endl;
1018 // All is ok, add node definition with the requested ID
1019 if (i >= m_content_features.size())
1020 m_content_features.resize((u32)(i) + 1);
1021 m_content_features[i] = f;
1022 addNameIdMapping(i, f.name);
1023 verbosestream << "deserialized " << f.name << std::endl;
1028 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1030 m_name_id_mapping.set(i, name);
1031 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1035 NodeResolver *CNodeDefManager::getResolver()
1041 IWritableNodeDefManager *createNodeDefManager()
1043 return new CNodeDefManager();
1047 //// Serialization of old ContentFeatures formats
1048 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1050 if (protocol_version == 13)
1052 writeU8(os, 5); // version
1053 os<<serializeString(name);
1054 writeU16(os, groups.size());
1055 for (ItemGroupList::const_iterator
1056 i = groups.begin(); i != groups.end(); i++) {
1057 os<<serializeString(i->first);
1058 writeS16(os, i->second);
1060 writeU8(os, drawtype);
1061 writeF1000(os, visual_scale);
1063 for (u32 i = 0; i < 6; i++)
1064 tiledef[i].serialize(os, protocol_version);
1065 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1067 for (u32 i = 0; i < 2; i++)
1068 tiledef_special[i].serialize(os, protocol_version);
1070 writeU8(os, post_effect_color.getAlpha());
1071 writeU8(os, post_effect_color.getRed());
1072 writeU8(os, post_effect_color.getGreen());
1073 writeU8(os, post_effect_color.getBlue());
1074 writeU8(os, param_type);
1075 writeU8(os, param_type_2);
1076 writeU8(os, is_ground_content);
1077 writeU8(os, light_propagates);
1078 writeU8(os, sunlight_propagates);
1079 writeU8(os, walkable);
1080 writeU8(os, pointable);
1081 writeU8(os, diggable);
1082 writeU8(os, climbable);
1083 writeU8(os, buildable_to);
1084 os<<serializeString(""); // legacy: used to be metadata_name
1085 writeU8(os, liquid_type);
1086 os<<serializeString(liquid_alternative_flowing);
1087 os<<serializeString(liquid_alternative_source);
1088 writeU8(os, liquid_viscosity);
1089 writeU8(os, light_source);
1090 writeU32(os, damage_per_second);
1091 node_box.serialize(os, protocol_version);
1092 selection_box.serialize(os, protocol_version);
1093 writeU8(os, legacy_facedir_simple);
1094 writeU8(os, legacy_wallmounted);
1095 serializeSimpleSoundSpec(sound_footstep, os);
1096 serializeSimpleSoundSpec(sound_dig, os);
1097 serializeSimpleSoundSpec(sound_dug, os);
1099 else if (protocol_version > 13 && protocol_version < 24) {
1100 writeU8(os, 6); // version
1101 os<<serializeString(name);
1102 writeU16(os, groups.size());
1103 for (ItemGroupList::const_iterator
1104 i = groups.begin(); i != groups.end(); i++) {
1105 os<<serializeString(i->first);
1106 writeS16(os, i->second);
1108 writeU8(os, drawtype);
1109 writeF1000(os, visual_scale);
1111 for (u32 i = 0; i < 6; i++)
1112 tiledef[i].serialize(os, protocol_version);
1113 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1115 for (u32 i = 0; i < 2; i++)
1116 tiledef_special[i].serialize(os, protocol_version);
1118 writeU8(os, post_effect_color.getAlpha());
1119 writeU8(os, post_effect_color.getRed());
1120 writeU8(os, post_effect_color.getGreen());
1121 writeU8(os, post_effect_color.getBlue());
1122 writeU8(os, param_type);
1123 writeU8(os, param_type_2);
1124 writeU8(os, is_ground_content);
1125 writeU8(os, light_propagates);
1126 writeU8(os, sunlight_propagates);
1127 writeU8(os, walkable);
1128 writeU8(os, pointable);
1129 writeU8(os, diggable);
1130 writeU8(os, climbable);
1131 writeU8(os, buildable_to);
1132 os<<serializeString(""); // legacy: used to be metadata_name
1133 writeU8(os, liquid_type);
1134 os<<serializeString(liquid_alternative_flowing);
1135 os<<serializeString(liquid_alternative_source);
1136 writeU8(os, liquid_viscosity);
1137 writeU8(os, liquid_renewable);
1138 writeU8(os, light_source);
1139 writeU32(os, damage_per_second);
1140 node_box.serialize(os, protocol_version);
1141 selection_box.serialize(os, protocol_version);
1142 writeU8(os, legacy_facedir_simple);
1143 writeU8(os, legacy_wallmounted);
1144 serializeSimpleSoundSpec(sound_footstep, os);
1145 serializeSimpleSoundSpec(sound_dig, os);
1146 serializeSimpleSoundSpec(sound_dug, os);
1147 writeU8(os, rightclickable);
1148 writeU8(os, drowning);
1149 writeU8(os, leveled);
1150 writeU8(os, liquid_range);
1152 throw SerializationError("ContentFeatures::serialize(): "
1153 "Unsupported version requested");
1157 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1159 if (version == 5) // In PROTOCOL_VERSION 13
1161 name = deSerializeString(is);
1163 u32 groups_size = readU16(is);
1164 for(u32 i=0; i<groups_size; i++){
1165 std::string name = deSerializeString(is);
1166 int value = readS16(is);
1167 groups[name] = value;
1169 drawtype = (enum NodeDrawType)readU8(is);
1170 visual_scale = readF1000(is);
1171 if (readU8(is) != 6)
1172 throw SerializationError("unsupported tile count");
1173 for (u32 i = 0; i < 6; i++)
1174 tiledef[i].deSerialize(is);
1175 if (readU8(is) != CF_SPECIAL_COUNT)
1176 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1177 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1178 tiledef_special[i].deSerialize(is);
1180 post_effect_color.setAlpha(readU8(is));
1181 post_effect_color.setRed(readU8(is));
1182 post_effect_color.setGreen(readU8(is));
1183 post_effect_color.setBlue(readU8(is));
1184 param_type = (enum ContentParamType)readU8(is);
1185 param_type_2 = (enum ContentParamType2)readU8(is);
1186 is_ground_content = readU8(is);
1187 light_propagates = readU8(is);
1188 sunlight_propagates = readU8(is);
1189 walkable = readU8(is);
1190 pointable = readU8(is);
1191 diggable = readU8(is);
1192 climbable = readU8(is);
1193 buildable_to = readU8(is);
1194 deSerializeString(is); // legacy: used to be metadata_name
1195 liquid_type = (enum LiquidType)readU8(is);
1196 liquid_alternative_flowing = deSerializeString(is);
1197 liquid_alternative_source = deSerializeString(is);
1198 liquid_viscosity = readU8(is);
1199 light_source = readU8(is);
1200 damage_per_second = readU32(is);
1201 node_box.deSerialize(is);
1202 selection_box.deSerialize(is);
1203 legacy_facedir_simple = readU8(is);
1204 legacy_wallmounted = readU8(is);
1205 deSerializeSimpleSoundSpec(sound_footstep, is);
1206 deSerializeSimpleSoundSpec(sound_dig, is);
1207 deSerializeSimpleSoundSpec(sound_dug, is);
1208 } else if (version == 6) {
1209 name = deSerializeString(is);
1211 u32 groups_size = readU16(is);
1212 for (u32 i = 0; i < groups_size; i++) {
1213 std::string name = deSerializeString(is);
1214 int value = readS16(is);
1215 groups[name] = value;
1217 drawtype = (enum NodeDrawType)readU8(is);
1218 visual_scale = readF1000(is);
1219 if (readU8(is) != 6)
1220 throw SerializationError("unsupported tile count");
1221 for (u32 i = 0; i < 6; i++)
1222 tiledef[i].deSerialize(is);
1223 // CF_SPECIAL_COUNT in version 6 = 2
1224 if (readU8(is) != 2)
1225 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1226 for (u32 i = 0; i < 2; i++)
1227 tiledef_special[i].deSerialize(is);
1229 post_effect_color.setAlpha(readU8(is));
1230 post_effect_color.setRed(readU8(is));
1231 post_effect_color.setGreen(readU8(is));
1232 post_effect_color.setBlue(readU8(is));
1233 param_type = (enum ContentParamType)readU8(is);
1234 param_type_2 = (enum ContentParamType2)readU8(is);
1235 is_ground_content = readU8(is);
1236 light_propagates = readU8(is);
1237 sunlight_propagates = readU8(is);
1238 walkable = readU8(is);
1239 pointable = readU8(is);
1240 diggable = readU8(is);
1241 climbable = readU8(is);
1242 buildable_to = readU8(is);
1243 deSerializeString(is); // legacy: used to be metadata_name
1244 liquid_type = (enum LiquidType)readU8(is);
1245 liquid_alternative_flowing = deSerializeString(is);
1246 liquid_alternative_source = deSerializeString(is);
1247 liquid_viscosity = readU8(is);
1248 liquid_renewable = readU8(is);
1249 light_source = readU8(is);
1250 damage_per_second = readU32(is);
1251 node_box.deSerialize(is);
1252 selection_box.deSerialize(is);
1253 legacy_facedir_simple = readU8(is);
1254 legacy_wallmounted = readU8(is);
1255 deSerializeSimpleSoundSpec(sound_footstep, is);
1256 deSerializeSimpleSoundSpec(sound_dig, is);
1257 deSerializeSimpleSoundSpec(sound_dug, is);
1258 rightclickable = readU8(is);
1259 drowning = readU8(is);
1260 leveled = readU8(is);
1261 liquid_range = readU8(is);
1263 throw SerializationError("unsupported ContentFeatures version");
1271 NodeResolver::NodeResolver(INodeDefManager *ndef)
1274 m_is_node_registration_complete = false;
1278 NodeResolver::~NodeResolver()
1280 while (!m_pending_contents.empty()) {
1281 NodeResolveInfo *nri = m_pending_contents.front();
1282 m_pending_contents.pop_front();
1288 int NodeResolver::addNode(const std::string &n_wanted, const std::string &n_alt,
1289 content_t c_fallback, content_t *content)
1291 if (m_is_node_registration_complete) {
1292 if (m_ndef->getId(n_wanted, *content))
1293 return NR_STATUS_SUCCESS;
1295 if (n_alt == "" || !m_ndef->getId(n_alt, *content)) {
1296 *content = c_fallback;
1297 return NR_STATUS_FAILURE;
1300 return NR_STATUS_SUCCESS;
1302 NodeResolveInfo *nfi = new NodeResolveInfo;
1303 nfi->n_wanted = n_wanted;
1305 nfi->c_fallback = c_fallback;
1306 nfi->output = content;
1308 m_pending_contents.push_back(nfi);
1310 return NR_STATUS_PENDING;
1315 int NodeResolver::addNodeList(const std::string &nodename,
1316 std::vector<content_t> *content_vec)
1318 if (m_is_node_registration_complete) {
1319 std::set<content_t> idset;
1320 std::set<content_t>::iterator it;
1322 m_ndef->getIds(nodename, idset);
1323 for (it = idset.begin(); it != idset.end(); ++it)
1324 content_vec->push_back(*it);
1326 return idset.size() ? NR_STATUS_SUCCESS : NR_STATUS_FAILURE;
1328 m_pending_content_vecs.push_back(
1329 std::make_pair(nodename, content_vec));
1330 return NR_STATUS_PENDING;
1335 bool NodeResolver::cancelNode(content_t *content)
1339 for (std::list<NodeResolveInfo *>::iterator
1340 it = m_pending_contents.begin();
1341 it != m_pending_contents.end();
1343 NodeResolveInfo *nfi = *it;
1344 if (nfi->output == content) {
1345 it = m_pending_contents.erase(it);
1355 int NodeResolver::cancelNodeList(std::vector<content_t> *content_vec)
1357 int num_canceled = 0;
1359 for (ContentVectorResolveList::iterator
1360 it = m_pending_content_vecs.begin();
1361 it != m_pending_content_vecs.end();
1363 if (it->second == content_vec) {
1364 it = m_pending_content_vecs.erase(it);
1369 return num_canceled;
1373 int NodeResolver::resolveNodes()
1377 //// Resolve pending single node name -> content ID mappings
1378 while (!m_pending_contents.empty()) {
1379 NodeResolveInfo *nri = m_pending_contents.front();
1380 m_pending_contents.pop_front();
1382 bool success = true;
1383 if (!m_ndef->getId(nri->n_wanted, *nri->output)) {
1384 success = (nri->n_alt != "") ?
1385 m_ndef->getId(nri->n_alt, *nri->output) : false;
1389 *nri->output = nri->c_fallback;
1391 errorstream << "NodeResolver::resolveNodes(): Failed to "
1392 "resolve '" << nri->n_wanted;
1393 if (nri->n_alt != "")
1394 errorstream << "' and '" << nri->n_alt;
1395 errorstream << "'" << std::endl;
1401 //// Resolve pending node names and add to content_t vector
1402 while (!m_pending_content_vecs.empty()) {
1403 std::pair<std::string, std::vector<content_t> *> item =
1404 m_pending_content_vecs.front();
1405 m_pending_content_vecs.pop_front();
1407 std::string &name = item.first;
1408 std::vector<content_t> *output = item.second;
1410 std::set<content_t> idset;
1411 std::set<content_t>::iterator it;
1413 m_ndef->getIds(name, idset);
1414 for (it = idset.begin(); it != idset.end(); ++it)
1415 output->push_back(*it);
1417 if (idset.empty()) {
1419 errorstream << "NodeResolver::resolveNodes(): Failed to "
1420 "resolve '" << name << "'" << std::endl;
1424 //// Mark node registration as complete so future resolve
1425 //// requests are satisfied immediately
1426 m_is_node_registration_complete = true;