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
25 #include "client/tile.h"
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;
230 liquid_range = LIQUID_LEVEL_MAX+1;
233 damage_per_second = 0;
234 node_box = NodeBox();
235 selection_box = NodeBox();
236 collision_box = NodeBox();
238 legacy_facedir_simple = false;
239 legacy_wallmounted = false;
240 sound_footstep = SimpleSoundSpec();
241 sound_dig = SimpleSoundSpec("__group");
242 sound_dug = SimpleSoundSpec();
245 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
247 if(protocol_version < 24){
248 serializeOld(os, protocol_version);
252 writeU8(os, 7); // version
253 os<<serializeString(name);
254 writeU16(os, groups.size());
255 for(ItemGroupList::const_iterator
256 i = groups.begin(); i != groups.end(); i++){
257 os<<serializeString(i->first);
258 writeS16(os, i->second);
260 writeU8(os, drawtype);
261 writeF1000(os, visual_scale);
263 for(u32 i = 0; i < 6; i++)
264 tiledef[i].serialize(os, protocol_version);
265 writeU8(os, CF_SPECIAL_COUNT);
266 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++){
267 tiledef_special[i].serialize(os, protocol_version);
270 writeU8(os, post_effect_color.getAlpha());
271 writeU8(os, post_effect_color.getRed());
272 writeU8(os, post_effect_color.getGreen());
273 writeU8(os, post_effect_color.getBlue());
274 writeU8(os, param_type);
275 writeU8(os, param_type_2);
276 writeU8(os, is_ground_content);
277 writeU8(os, light_propagates);
278 writeU8(os, sunlight_propagates);
279 writeU8(os, walkable);
280 writeU8(os, pointable);
281 writeU8(os, diggable);
282 writeU8(os, climbable);
283 writeU8(os, buildable_to);
284 os<<serializeString(""); // legacy: used to be metadata_name
285 writeU8(os, liquid_type);
286 os<<serializeString(liquid_alternative_flowing);
287 os<<serializeString(liquid_alternative_source);
288 writeU8(os, liquid_viscosity);
289 writeU8(os, liquid_renewable);
290 writeU8(os, light_source);
291 writeU32(os, damage_per_second);
292 node_box.serialize(os, protocol_version);
293 selection_box.serialize(os, protocol_version);
294 writeU8(os, legacy_facedir_simple);
295 writeU8(os, legacy_wallmounted);
296 serializeSimpleSoundSpec(sound_footstep, os);
297 serializeSimpleSoundSpec(sound_dig, os);
298 serializeSimpleSoundSpec(sound_dug, os);
299 writeU8(os, rightclickable);
300 writeU8(os, drowning);
301 writeU8(os, leveled);
302 writeU8(os, liquid_range);
304 // Stuff below should be moved to correct place in a version that otherwise changes
305 // the protocol version
306 os<<serializeString(mesh);
307 collision_box.serialize(os, protocol_version);
310 void ContentFeatures::deSerialize(std::istream &is)
312 int version = readU8(is);
314 deSerializeOld(is, version);
318 name = deSerializeString(is);
320 u32 groups_size = readU16(is);
321 for(u32 i = 0; i < groups_size; i++){
322 std::string name = deSerializeString(is);
323 int value = readS16(is);
324 groups[name] = value;
326 drawtype = (enum NodeDrawType)readU8(is);
327 visual_scale = readF1000(is);
329 throw SerializationError("unsupported tile count");
330 for(u32 i = 0; i < 6; i++)
331 tiledef[i].deSerialize(is);
332 if(readU8(is) != CF_SPECIAL_COUNT)
333 throw SerializationError("unsupported CF_SPECIAL_COUNT");
334 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
335 tiledef_special[i].deSerialize(is);
337 post_effect_color.setAlpha(readU8(is));
338 post_effect_color.setRed(readU8(is));
339 post_effect_color.setGreen(readU8(is));
340 post_effect_color.setBlue(readU8(is));
341 param_type = (enum ContentParamType)readU8(is);
342 param_type_2 = (enum ContentParamType2)readU8(is);
343 is_ground_content = readU8(is);
344 light_propagates = readU8(is);
345 sunlight_propagates = readU8(is);
346 walkable = readU8(is);
347 pointable = readU8(is);
348 diggable = readU8(is);
349 climbable = readU8(is);
350 buildable_to = readU8(is);
351 deSerializeString(is); // legacy: used to be metadata_name
352 liquid_type = (enum LiquidType)readU8(is);
353 liquid_alternative_flowing = deSerializeString(is);
354 liquid_alternative_source = deSerializeString(is);
355 liquid_viscosity = readU8(is);
356 liquid_renewable = readU8(is);
357 light_source = readU8(is);
358 damage_per_second = readU32(is);
359 node_box.deSerialize(is);
360 selection_box.deSerialize(is);
361 legacy_facedir_simple = readU8(is);
362 legacy_wallmounted = readU8(is);
363 deSerializeSimpleSoundSpec(sound_footstep, is);
364 deSerializeSimpleSoundSpec(sound_dig, is);
365 deSerializeSimpleSoundSpec(sound_dug, is);
366 rightclickable = readU8(is);
367 drowning = readU8(is);
368 leveled = readU8(is);
369 liquid_range = readU8(is);
371 // If you add anything here, insert it primarily inside the try-catch
372 // block to not need to increase the version.
374 // Stuff below should be moved to correct place in a version that
375 // otherwise changes the protocol version
376 mesh = deSerializeString(is);
377 collision_box.deSerialize(is);
378 }catch(SerializationError &e) {};
385 class CNodeDefManager: public IWritableNodeDefManager {
388 virtual ~CNodeDefManager();
390 virtual IWritableNodeDefManager *clone();
391 inline virtual const ContentFeatures& get(content_t c) const;
392 inline virtual const ContentFeatures& get(const MapNode &n) const;
393 virtual bool getId(const std::string &name, content_t &result) const;
394 virtual content_t getId(const std::string &name) const;
395 virtual void getIds(const std::string &name, std::set<content_t> &result) const;
396 virtual const ContentFeatures& get(const std::string &name) const;
397 content_t allocateId();
398 virtual content_t set(const std::string &name, const ContentFeatures &def);
399 virtual content_t allocateDummy(const std::string &name);
400 virtual void updateAliases(IItemDefManager *idef);
401 virtual void updateTextures(IGameDef *gamedef);
402 void serialize(std::ostream &os, u16 protocol_version);
403 void deSerialize(std::istream &is);
405 inline virtual bool getNodeRegistrationStatus() const;
406 inline virtual void setNodeRegistrationStatus(bool completed);
408 virtual void pendNodeResolve(NodeResolveInfo *nri);
409 virtual void cancelNodeResolve(NodeResolver *resolver);
410 virtual void runNodeResolverCallbacks();
412 virtual bool getIdFromResolveInfo(NodeResolveInfo *nri,
413 const std::string &node_alt, content_t c_fallback, content_t &result);
414 virtual bool getIdsFromResolveInfo(NodeResolveInfo *nri,
415 std::vector<content_t> &result);
418 void addNameIdMapping(content_t i, std::string name);
420 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
421 u32 shader_id, bool use_normal_texture, bool backface_culling,
422 u8 alpha, u8 material_type);
425 // Features indexed by id
426 std::vector<ContentFeatures> m_content_features;
428 // A mapping for fast converting back and forth between names and ids
429 NameIdMapping m_name_id_mapping;
431 // Like m_name_id_mapping, but only from names to ids, and includes
432 // item aliases too. Updated by updateAliases()
433 // Note: Not serialized.
435 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
437 // A mapping from groups to a list of content_ts (and their levels)
438 // that belong to it. Necessary for a direct lookup in getIds().
439 // Note: Not serialized.
440 std::map<std::string, GroupItems> m_group_to_items;
442 // Next possibly free id
445 // List of node strings and node resolver callbacks to perform
446 std::vector<NodeResolveInfo *> m_pending_node_lookups;
448 // True when all nodes have been registered
449 bool m_node_registration_complete;
453 CNodeDefManager::CNodeDefManager()
459 CNodeDefManager::~CNodeDefManager()
462 for (u32 i = 0; i < m_content_features.size(); i++) {
463 ContentFeatures *f = &m_content_features[i];
464 for (u32 j = 0; j < 24; j++) {
466 f->mesh_ptr[j]->drop();
473 void CNodeDefManager::clear()
475 m_content_features.clear();
476 m_name_id_mapping.clear();
477 m_name_id_mapping_with_aliases.clear();
478 m_group_to_items.clear();
481 m_node_registration_complete = false;
482 for (std::vector<NodeResolveInfo *>::iterator
483 it = m_pending_node_lookups.begin();
484 it != m_pending_node_lookups.end();
487 m_pending_node_lookups.clear();
489 u32 initial_length = 0;
490 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
491 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
492 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
493 m_content_features.resize(initial_length);
495 // Set CONTENT_UNKNOWN
499 // Insert directly into containers
500 content_t c = CONTENT_UNKNOWN;
501 m_content_features[c] = f;
502 addNameIdMapping(c, f.name);
509 f.drawtype = NDT_AIRLIKE;
510 f.param_type = CPT_LIGHT;
511 f.light_propagates = true;
512 f.sunlight_propagates = true;
516 f.buildable_to = true;
517 f.is_ground_content = true;
518 // Insert directly into containers
519 content_t c = CONTENT_AIR;
520 m_content_features[c] = f;
521 addNameIdMapping(c, f.name);
524 // Set CONTENT_IGNORE
528 f.drawtype = NDT_AIRLIKE;
529 f.param_type = CPT_NONE;
530 f.light_propagates = false;
531 f.sunlight_propagates = false;
535 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
536 f.is_ground_content = true;
537 // Insert directly into containers
538 content_t c = CONTENT_IGNORE;
539 m_content_features[c] = f;
540 addNameIdMapping(c, f.name);
545 IWritableNodeDefManager *CNodeDefManager::clone()
547 CNodeDefManager *mgr = new CNodeDefManager();
553 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
555 return c < m_content_features.size()
556 ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
560 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
562 return get(n.getContent());
566 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
568 std::map<std::string, content_t>::const_iterator
569 i = m_name_id_mapping_with_aliases.find(name);
570 if(i == m_name_id_mapping_with_aliases.end())
577 content_t CNodeDefManager::getId(const std::string &name) const
579 content_t id = CONTENT_IGNORE;
585 void CNodeDefManager::getIds(const std::string &name,
586 std::set<content_t> &result) const
588 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
589 if (name.substr(0,6) != "group:") {
590 content_t id = CONTENT_IGNORE;
595 std::string group = name.substr(6);
597 std::map<std::string, GroupItems>::const_iterator
598 i = m_group_to_items.find(group);
599 if (i == m_group_to_items.end())
602 const GroupItems &items = i->second;
603 for (GroupItems::const_iterator j = items.begin();
604 j != items.end(); ++j) {
605 if ((*j).second != 0)
606 result.insert((*j).first);
608 //printf("getIds: %dus\n", t.stop());
612 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
614 content_t id = CONTENT_UNKNOWN;
620 // returns CONTENT_IGNORE if no free ID found
621 content_t CNodeDefManager::allocateId()
623 for (content_t id = m_next_id;
624 id >= m_next_id; // overflow?
626 while (id >= m_content_features.size()) {
627 m_content_features.push_back(ContentFeatures());
629 const ContentFeatures &f = m_content_features[id];
635 // If we arrive here, an overflow occurred in id.
636 // That means no ID was found
637 return CONTENT_IGNORE;
641 // IWritableNodeDefManager
642 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
645 assert(name == def.name);
647 // Don't allow redefining ignore (but allow air and unknown)
648 if (name == "ignore") {
649 infostream << "NodeDefManager: WARNING: Ignoring "
650 "CONTENT_IGNORE redefinition"<<std::endl;
651 return CONTENT_IGNORE;
654 content_t id = CONTENT_IGNORE;
655 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
658 if (id == CONTENT_IGNORE) {
659 infostream << "NodeDefManager: WARNING: Absolute "
660 "limit reached" << std::endl;
661 return CONTENT_IGNORE;
663 assert(id != CONTENT_IGNORE);
664 addNameIdMapping(id, name);
666 m_content_features[id] = def;
667 verbosestream << "NodeDefManager: registering content id \"" << id
668 << "\": name=\"" << def.name << "\""<<std::endl;
670 // Add this content to the list of all groups it belongs to
671 // FIXME: This should remove a node from groups it no longer
672 // belongs to when a node is re-registered
673 for (ItemGroupList::const_iterator i = def.groups.begin();
674 i != def.groups.end(); ++i) {
675 std::string group_name = i->first;
677 std::map<std::string, GroupItems>::iterator
678 j = m_group_to_items.find(group_name);
679 if (j == m_group_to_items.end()) {
680 m_group_to_items[group_name].push_back(
681 std::make_pair(id, i->second));
683 GroupItems &items = j->second;
684 items.push_back(std::make_pair(id, i->second));
691 content_t CNodeDefManager::allocateDummy(const std::string &name)
700 void CNodeDefManager::updateAliases(IItemDefManager *idef)
702 std::set<std::string> all = idef->getAll();
703 m_name_id_mapping_with_aliases.clear();
704 for (std::set<std::string>::iterator
705 i = all.begin(); i != all.end(); i++) {
706 std::string name = *i;
707 std::string convert_to = idef->getAlias(name);
709 if (m_name_id_mapping.getId(convert_to, id)) {
710 m_name_id_mapping_with_aliases.insert(
711 std::make_pair(name, id));
717 void CNodeDefManager::updateTextures(IGameDef *gamedef)
720 infostream << "CNodeDefManager::updateTextures(): Updating "
721 "textures in node definitions" << std::endl;
723 ITextureSource *tsrc = gamedef->tsrc();
724 IShaderSource *shdsrc = gamedef->getShaderSource();
725 scene::ISceneManager* smgr = gamedef->getSceneManager();
726 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
728 bool new_style_water = g_settings->getBool("new_style_water");
729 bool new_style_leaves = g_settings->getBool("new_style_leaves");
730 bool connected_glass = g_settings->getBool("connected_glass");
731 bool opaque_water = g_settings->getBool("opaque_water");
732 bool enable_shaders = g_settings->getBool("enable_shaders");
733 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
734 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
735 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
737 bool use_normal_texture = enable_shaders &&
738 (enable_bumpmapping || enable_parallax_occlusion);
740 for (u32 i = 0; i < m_content_features.size(); i++) {
741 ContentFeatures *f = &m_content_features[i];
743 // Figure out the actual tiles to use
745 for (u32 j = 0; j < 6; j++) {
746 tiledef[j] = f->tiledef[j];
747 if (tiledef[j].name == "")
748 tiledef[j].name = "unknown_node.png";
751 bool is_liquid = false;
752 bool is_water_surface = false;
754 u8 material_type = (f->alpha == 255) ?
755 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
757 switch (f->drawtype) {
766 assert(f->liquid_type == LIQUID_SOURCE);
769 if (new_style_water){
773 f->backface_culling = false;
777 case NDT_FLOWINGLIQUID:
778 assert(f->liquid_type == LIQUID_FLOWING);
786 f->visual_solidness = 1;
788 case NDT_GLASSLIKE_FRAMED:
790 f->visual_solidness = 1;
792 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
794 f->visual_solidness = 1;
795 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
799 f->visual_solidness = 1;
801 case NDT_ALLFACES_OPTIONAL:
802 if (new_style_leaves) {
803 f->drawtype = NDT_ALLFACES;
805 f->visual_solidness = 1;
807 f->drawtype = NDT_NORMAL;
809 for (u32 i = 0; i < 6; i++)
810 tiledef[i].name += std::string("^[noalpha");
813 material_type = TILE_MATERIAL_WAVING_LEAVES;
817 f->backface_culling = false;
819 material_type = TILE_MATERIAL_WAVING_PLANTS;
822 f->backface_culling = false;
827 f->backface_culling = false;
839 material_type = (f->alpha == 255) ?
840 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
841 if (f->name == "default:water_source")
842 is_water_surface = true;
846 for (u16 j = 0; j < 6; j++) {
847 tile_shader[j] = shdsrc->getShader("nodes_shader",
848 material_type, f->drawtype);
851 if (is_water_surface) {
852 tile_shader[0] = shdsrc->getShader("water_surface_shader",
853 material_type, f->drawtype);
856 // Tiles (fill in f->tiles[])
857 for (u16 j = 0; j < 6; j++) {
858 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
859 use_normal_texture, f->backface_culling, f->alpha, material_type);
862 // Special tiles (fill in f->special_tiles[])
863 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
864 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
865 tile_shader[j], use_normal_texture,
866 f->tiledef_special[j].backface_culling, f->alpha, material_type);
869 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
871 // Read the mesh and apply scale
872 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
874 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
875 scaleMesh(f->mesh_ptr[0], scale);
876 recalculateBoundingBox(f->mesh_ptr[0]);
877 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
879 } else if ((f->drawtype == NDT_NODEBOX) &&
880 ((f->node_box.type == NODEBOX_REGULAR) ||
881 (f->node_box.type == NODEBOX_FIXED)) &&
882 (!f->node_box.fixed.empty())) {
883 //Convert regular nodebox nodes to meshnodes
884 //Change the drawtype and apply scale
885 f->drawtype = NDT_MESH;
886 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
887 v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
888 scaleMesh(f->mesh_ptr[0], scale);
889 recalculateBoundingBox(f->mesh_ptr[0]);
890 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
893 //Cache 6dfacedir and wallmounted rotated clones of meshes
894 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
895 for (u16 j = 1; j < 24; j++) {
896 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
897 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
898 recalculateBoundingBox(f->mesh_ptr[j]);
899 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
901 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
902 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
903 for (u16 j = 1; j < 6; j++) {
904 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
905 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
906 recalculateBoundingBox(f->mesh_ptr[j]);
907 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
909 rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
910 recalculateBoundingBox(f->mesh_ptr[0]);
911 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
919 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
920 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
921 bool backface_culling, u8 alpha, u8 material_type)
923 tile->shader_id = shader_id;
924 tile->texture = tsrc->getTexture(tiledef->name, &tile->texture_id);
926 tile->material_type = material_type;
929 if (use_normal_texture)
930 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
933 tile->material_flags = 0;
934 if (backface_culling)
935 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
936 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
937 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
939 // Animation parameters
941 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
942 // Get texture size to determine frame count by aspect ratio
943 v2u32 size = tile->texture->getOriginalSize();
944 int frame_height = (float)size.X /
945 (float)tiledef->animation.aspect_w *
946 (float)tiledef->animation.aspect_h;
947 frame_count = size.Y / frame_height;
948 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
949 tile->animation_frame_count = frame_count;
950 tile->animation_frame_length_ms = frame_length_ms;
953 if (frame_count == 1) {
954 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
956 std::ostringstream os(std::ios::binary);
957 tile->frames.resize(frame_count);
959 for (int i = 0; i < frame_count; i++) {
964 os << tiledef->name << "^[verticalframe:"
965 << frame_count << ":" << i;
967 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
968 if (tile->normal_texture)
969 frame.normal_texture = tsrc->getNormalTexture(os.str());
970 tile->frames[i] = frame;
977 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
979 writeU8(os, 1); // version
981 std::ostringstream os2(std::ios::binary);
982 for (u32 i = 0; i < m_content_features.size(); i++) {
983 if (i == CONTENT_IGNORE || i == CONTENT_AIR
984 || i == CONTENT_UNKNOWN)
986 ContentFeatures *f = &m_content_features[i];
990 // Wrap it in a string to allow different lengths without
991 // strict version incompatibilities
992 std::ostringstream wrapper_os(std::ios::binary);
993 f->serialize(wrapper_os, protocol_version);
994 os2<<serializeString(wrapper_os.str());
996 assert(count + 1 > count); // must not overflow
1000 os << serializeLongString(os2.str());
1004 void CNodeDefManager::deSerialize(std::istream &is)
1007 int version = readU8(is);
1009 throw SerializationError("unsupported NodeDefinitionManager version");
1010 u16 count = readU16(is);
1011 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1013 for (u16 n = 0; n < count; n++) {
1014 u16 i = readU16(is2);
1016 // Read it from the string wrapper
1017 std::string wrapper = deSerializeString(is2);
1018 std::istringstream wrapper_is(wrapper, std::ios::binary);
1019 f.deSerialize(wrapper_is);
1021 // Check error conditions
1022 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1023 infostream << "NodeDefManager::deSerialize(): WARNING: "
1024 "not changing builtin node " << i << std::endl;
1028 infostream << "NodeDefManager::deSerialize(): WARNING: "
1029 "received empty name" << std::endl;
1035 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1036 infostream << "NodeDefManager::deSerialize(): WARNING: "
1037 "already defined with different ID: " << f.name << std::endl;
1041 // All is ok, add node definition with the requested ID
1042 if (i >= m_content_features.size())
1043 m_content_features.resize((u32)(i) + 1);
1044 m_content_features[i] = f;
1045 addNameIdMapping(i, f.name);
1046 verbosestream << "deserialized " << f.name << std::endl;
1051 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1053 m_name_id_mapping.set(i, name);
1054 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1058 IWritableNodeDefManager *createNodeDefManager()
1060 return new CNodeDefManager();
1064 //// Serialization of old ContentFeatures formats
1065 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1067 if (protocol_version == 13)
1069 writeU8(os, 5); // version
1070 os<<serializeString(name);
1071 writeU16(os, groups.size());
1072 for (ItemGroupList::const_iterator
1073 i = groups.begin(); i != groups.end(); i++) {
1074 os<<serializeString(i->first);
1075 writeS16(os, i->second);
1077 writeU8(os, drawtype);
1078 writeF1000(os, visual_scale);
1080 for (u32 i = 0; i < 6; i++)
1081 tiledef[i].serialize(os, protocol_version);
1082 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1084 for (u32 i = 0; i < 2; i++)
1085 tiledef_special[i].serialize(os, protocol_version);
1087 writeU8(os, post_effect_color.getAlpha());
1088 writeU8(os, post_effect_color.getRed());
1089 writeU8(os, post_effect_color.getGreen());
1090 writeU8(os, post_effect_color.getBlue());
1091 writeU8(os, param_type);
1092 writeU8(os, param_type_2);
1093 writeU8(os, is_ground_content);
1094 writeU8(os, light_propagates);
1095 writeU8(os, sunlight_propagates);
1096 writeU8(os, walkable);
1097 writeU8(os, pointable);
1098 writeU8(os, diggable);
1099 writeU8(os, climbable);
1100 writeU8(os, buildable_to);
1101 os<<serializeString(""); // legacy: used to be metadata_name
1102 writeU8(os, liquid_type);
1103 os<<serializeString(liquid_alternative_flowing);
1104 os<<serializeString(liquid_alternative_source);
1105 writeU8(os, liquid_viscosity);
1106 writeU8(os, light_source);
1107 writeU32(os, damage_per_second);
1108 node_box.serialize(os, protocol_version);
1109 selection_box.serialize(os, protocol_version);
1110 writeU8(os, legacy_facedir_simple);
1111 writeU8(os, legacy_wallmounted);
1112 serializeSimpleSoundSpec(sound_footstep, os);
1113 serializeSimpleSoundSpec(sound_dig, os);
1114 serializeSimpleSoundSpec(sound_dug, os);
1116 else if (protocol_version > 13 && protocol_version < 24) {
1117 writeU8(os, 6); // version
1118 os<<serializeString(name);
1119 writeU16(os, groups.size());
1120 for (ItemGroupList::const_iterator
1121 i = groups.begin(); i != groups.end(); i++) {
1122 os<<serializeString(i->first);
1123 writeS16(os, i->second);
1125 writeU8(os, drawtype);
1126 writeF1000(os, visual_scale);
1128 for (u32 i = 0; i < 6; i++)
1129 tiledef[i].serialize(os, protocol_version);
1130 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1132 for (u32 i = 0; i < 2; i++)
1133 tiledef_special[i].serialize(os, protocol_version);
1135 writeU8(os, post_effect_color.getAlpha());
1136 writeU8(os, post_effect_color.getRed());
1137 writeU8(os, post_effect_color.getGreen());
1138 writeU8(os, post_effect_color.getBlue());
1139 writeU8(os, param_type);
1140 writeU8(os, param_type_2);
1141 writeU8(os, is_ground_content);
1142 writeU8(os, light_propagates);
1143 writeU8(os, sunlight_propagates);
1144 writeU8(os, walkable);
1145 writeU8(os, pointable);
1146 writeU8(os, diggable);
1147 writeU8(os, climbable);
1148 writeU8(os, buildable_to);
1149 os<<serializeString(""); // legacy: used to be metadata_name
1150 writeU8(os, liquid_type);
1151 os<<serializeString(liquid_alternative_flowing);
1152 os<<serializeString(liquid_alternative_source);
1153 writeU8(os, liquid_viscosity);
1154 writeU8(os, liquid_renewable);
1155 writeU8(os, light_source);
1156 writeU32(os, damage_per_second);
1157 node_box.serialize(os, protocol_version);
1158 selection_box.serialize(os, protocol_version);
1159 writeU8(os, legacy_facedir_simple);
1160 writeU8(os, legacy_wallmounted);
1161 serializeSimpleSoundSpec(sound_footstep, os);
1162 serializeSimpleSoundSpec(sound_dig, os);
1163 serializeSimpleSoundSpec(sound_dug, os);
1164 writeU8(os, rightclickable);
1165 writeU8(os, drowning);
1166 writeU8(os, leveled);
1167 writeU8(os, liquid_range);
1169 throw SerializationError("ContentFeatures::serialize(): "
1170 "Unsupported version requested");
1174 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1176 if (version == 5) // In PROTOCOL_VERSION 13
1178 name = deSerializeString(is);
1180 u32 groups_size = readU16(is);
1181 for(u32 i=0; i<groups_size; i++){
1182 std::string name = deSerializeString(is);
1183 int value = readS16(is);
1184 groups[name] = value;
1186 drawtype = (enum NodeDrawType)readU8(is);
1187 visual_scale = readF1000(is);
1188 if (readU8(is) != 6)
1189 throw SerializationError("unsupported tile count");
1190 for (u32 i = 0; i < 6; i++)
1191 tiledef[i].deSerialize(is);
1192 if (readU8(is) != CF_SPECIAL_COUNT)
1193 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1194 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1195 tiledef_special[i].deSerialize(is);
1197 post_effect_color.setAlpha(readU8(is));
1198 post_effect_color.setRed(readU8(is));
1199 post_effect_color.setGreen(readU8(is));
1200 post_effect_color.setBlue(readU8(is));
1201 param_type = (enum ContentParamType)readU8(is);
1202 param_type_2 = (enum ContentParamType2)readU8(is);
1203 is_ground_content = readU8(is);
1204 light_propagates = readU8(is);
1205 sunlight_propagates = readU8(is);
1206 walkable = readU8(is);
1207 pointable = readU8(is);
1208 diggable = readU8(is);
1209 climbable = readU8(is);
1210 buildable_to = readU8(is);
1211 deSerializeString(is); // legacy: used to be metadata_name
1212 liquid_type = (enum LiquidType)readU8(is);
1213 liquid_alternative_flowing = deSerializeString(is);
1214 liquid_alternative_source = deSerializeString(is);
1215 liquid_viscosity = readU8(is);
1216 light_source = readU8(is);
1217 damage_per_second = readU32(is);
1218 node_box.deSerialize(is);
1219 selection_box.deSerialize(is);
1220 legacy_facedir_simple = readU8(is);
1221 legacy_wallmounted = readU8(is);
1222 deSerializeSimpleSoundSpec(sound_footstep, is);
1223 deSerializeSimpleSoundSpec(sound_dig, is);
1224 deSerializeSimpleSoundSpec(sound_dug, is);
1225 } else if (version == 6) {
1226 name = deSerializeString(is);
1228 u32 groups_size = readU16(is);
1229 for (u32 i = 0; i < groups_size; i++) {
1230 std::string name = deSerializeString(is);
1231 int value = readS16(is);
1232 groups[name] = value;
1234 drawtype = (enum NodeDrawType)readU8(is);
1235 visual_scale = readF1000(is);
1236 if (readU8(is) != 6)
1237 throw SerializationError("unsupported tile count");
1238 for (u32 i = 0; i < 6; i++)
1239 tiledef[i].deSerialize(is);
1240 // CF_SPECIAL_COUNT in version 6 = 2
1241 if (readU8(is) != 2)
1242 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1243 for (u32 i = 0; i < 2; i++)
1244 tiledef_special[i].deSerialize(is);
1246 post_effect_color.setAlpha(readU8(is));
1247 post_effect_color.setRed(readU8(is));
1248 post_effect_color.setGreen(readU8(is));
1249 post_effect_color.setBlue(readU8(is));
1250 param_type = (enum ContentParamType)readU8(is);
1251 param_type_2 = (enum ContentParamType2)readU8(is);
1252 is_ground_content = readU8(is);
1253 light_propagates = readU8(is);
1254 sunlight_propagates = readU8(is);
1255 walkable = readU8(is);
1256 pointable = readU8(is);
1257 diggable = readU8(is);
1258 climbable = readU8(is);
1259 buildable_to = readU8(is);
1260 deSerializeString(is); // legacy: used to be metadata_name
1261 liquid_type = (enum LiquidType)readU8(is);
1262 liquid_alternative_flowing = deSerializeString(is);
1263 liquid_alternative_source = deSerializeString(is);
1264 liquid_viscosity = readU8(is);
1265 liquid_renewable = readU8(is);
1266 light_source = readU8(is);
1267 damage_per_second = readU32(is);
1268 node_box.deSerialize(is);
1269 selection_box.deSerialize(is);
1270 legacy_facedir_simple = readU8(is);
1271 legacy_wallmounted = readU8(is);
1272 deSerializeSimpleSoundSpec(sound_footstep, is);
1273 deSerializeSimpleSoundSpec(sound_dig, is);
1274 deSerializeSimpleSoundSpec(sound_dug, is);
1275 rightclickable = readU8(is);
1276 drowning = readU8(is);
1277 leveled = readU8(is);
1278 liquid_range = readU8(is);
1280 throw SerializationError("unsupported ContentFeatures version");
1285 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1287 return m_node_registration_complete;
1291 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1293 m_node_registration_complete = completed;
1297 void CNodeDefManager::pendNodeResolve(NodeResolveInfo *nri)
1299 nri->resolver->m_ndef = this;
1300 if (m_node_registration_complete) {
1301 nri->resolver->resolveNodeNames(nri);
1302 nri->resolver->m_lookup_done = true;
1305 m_pending_node_lookups.push_back(nri);
1310 void CNodeDefManager::cancelNodeResolve(NodeResolver *resolver)
1312 for (std::vector<NodeResolveInfo *>::iterator
1313 it = m_pending_node_lookups.begin();
1314 it != m_pending_node_lookups.end();
1316 NodeResolveInfo *nri = *it;
1317 if (resolver == nri->resolver) {
1318 it = m_pending_node_lookups.erase(it);
1325 void CNodeDefManager::runNodeResolverCallbacks()
1327 while (!m_pending_node_lookups.empty()) {
1328 NodeResolveInfo *nri = m_pending_node_lookups.front();
1329 m_pending_node_lookups.erase(m_pending_node_lookups.begin());
1330 nri->resolver->resolveNodeNames(nri);
1331 nri->resolver->m_lookup_done = true;
1337 bool CNodeDefManager::getIdFromResolveInfo(NodeResolveInfo *nri,
1338 const std::string &node_alt, content_t c_fallback, content_t &result)
1340 if (nri->nodenames.empty()) {
1341 result = c_fallback;
1342 errorstream << "Resolver empty nodename list" << std::endl;
1347 std::string name = nri->nodenames.front();
1348 nri->nodenames.erase(nri->nodenames.begin());
1350 bool success = getId(name, c);
1351 if (!success && node_alt != "") {
1353 success = getId(name, c);
1357 errorstream << "Resolver: Failed to resolve node name '" << name
1358 << "'." << std::endl;
1367 bool CNodeDefManager::getIdsFromResolveInfo(NodeResolveInfo *nri,
1368 std::vector<content_t> &result)
1370 bool success = true;
1372 if (nri->nodelistinfo.empty()) {
1373 errorstream << "Resolver: Empty nodelistinfo list" << std::endl;
1377 NodeListInfo listinfo = nri->nodelistinfo.front();
1378 nri->nodelistinfo.pop_front();
1380 while (listinfo.length--) {
1381 if (nri->nodenames.empty()) {
1382 errorstream << "Resolver: Empty nodename list" << std::endl;
1387 std::string name = nri->nodenames.front();
1388 nri->nodenames.erase(nri->nodenames.begin());
1390 if (name.substr(0,6) != "group:") {
1391 if (getId(name, c)) {
1392 result.push_back(c);
1393 } else if (listinfo.all_required) {
1394 errorstream << "Resolver: Failed to resolve node name '" << name
1395 << "'." << std::endl;
1396 result.push_back(listinfo.c_fallback);
1400 std::set<content_t> cids;
1401 std::set<content_t>::iterator it;
1403 for (it = cids.begin(); it != cids.end(); ++it)
1404 result.push_back(*it);