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.
24 #include "client/tile.h"
26 #include <IMeshManipulator.h>
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;
229 liquid_range = LIQUID_LEVEL_MAX+1;
232 damage_per_second = 0;
233 node_box = NodeBox();
234 selection_box = NodeBox();
235 collision_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);
306 collision_box.serialize(os, protocol_version);
309 void ContentFeatures::deSerialize(std::istream &is)
311 int version = readU8(is);
313 deSerializeOld(is, version);
317 name = deSerializeString(is);
319 u32 groups_size = readU16(is);
320 for(u32 i = 0; i < groups_size; i++){
321 std::string name = deSerializeString(is);
322 int value = readS16(is);
323 groups[name] = value;
325 drawtype = (enum NodeDrawType)readU8(is);
326 visual_scale = readF1000(is);
328 throw SerializationError("unsupported tile count");
329 for(u32 i = 0; i < 6; i++)
330 tiledef[i].deSerialize(is);
331 if(readU8(is) != CF_SPECIAL_COUNT)
332 throw SerializationError("unsupported CF_SPECIAL_COUNT");
333 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
334 tiledef_special[i].deSerialize(is);
336 post_effect_color.setAlpha(readU8(is));
337 post_effect_color.setRed(readU8(is));
338 post_effect_color.setGreen(readU8(is));
339 post_effect_color.setBlue(readU8(is));
340 param_type = (enum ContentParamType)readU8(is);
341 param_type_2 = (enum ContentParamType2)readU8(is);
342 is_ground_content = readU8(is);
343 light_propagates = readU8(is);
344 sunlight_propagates = readU8(is);
345 walkable = readU8(is);
346 pointable = readU8(is);
347 diggable = readU8(is);
348 climbable = readU8(is);
349 buildable_to = readU8(is);
350 deSerializeString(is); // legacy: used to be metadata_name
351 liquid_type = (enum LiquidType)readU8(is);
352 liquid_alternative_flowing = deSerializeString(is);
353 liquid_alternative_source = deSerializeString(is);
354 liquid_viscosity = readU8(is);
355 liquid_renewable = readU8(is);
356 light_source = readU8(is);
357 damage_per_second = readU32(is);
358 node_box.deSerialize(is);
359 selection_box.deSerialize(is);
360 legacy_facedir_simple = readU8(is);
361 legacy_wallmounted = readU8(is);
362 deSerializeSimpleSoundSpec(sound_footstep, is);
363 deSerializeSimpleSoundSpec(sound_dig, is);
364 deSerializeSimpleSoundSpec(sound_dug, is);
365 rightclickable = readU8(is);
366 drowning = readU8(is);
367 leveled = readU8(is);
368 liquid_range = readU8(is);
370 // If you add anything here, insert it primarily inside the try-catch
371 // block to not need to increase the version.
373 // Stuff below should be moved to correct place in a version that
374 // otherwise changes the protocol version
375 mesh = deSerializeString(is);
376 collision_box.deSerialize(is);
377 }catch(SerializationError &e) {};
384 class CNodeDefManager: public IWritableNodeDefManager {
387 virtual ~CNodeDefManager();
389 virtual IWritableNodeDefManager *clone();
390 inline virtual const ContentFeatures& get(content_t c) const;
391 inline virtual const ContentFeatures& get(const MapNode &n) const;
392 virtual bool getId(const std::string &name, content_t &result) const;
393 virtual content_t getId(const std::string &name) const;
394 virtual void getIds(const std::string &name, std::set<content_t> &result) const;
395 virtual const ContentFeatures& get(const std::string &name) const;
396 content_t allocateId();
397 virtual content_t set(const std::string &name, const ContentFeatures &def);
398 virtual content_t allocateDummy(const std::string &name);
399 virtual void updateAliases(IItemDefManager *idef);
400 virtual void updateTextures(IGameDef *gamedef,
401 /*argument: */void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
402 /*argument: */void *progress_callback_args);
403 void serialize(std::ostream &os, u16 protocol_version);
404 void deSerialize(std::istream &is);
406 inline virtual bool getNodeRegistrationStatus() const;
407 inline virtual void setNodeRegistrationStatus(bool completed);
409 virtual void pendNodeResolve(NodeResolveInfo *nri);
410 virtual void cancelNodeResolve(NodeResolver *resolver);
411 virtual void runNodeResolverCallbacks();
413 virtual bool getIdFromResolveInfo(NodeResolveInfo *nri,
414 const std::string &node_alt, content_t c_fallback, content_t &result);
415 virtual bool getIdsFromResolveInfo(NodeResolveInfo *nri,
416 std::vector<content_t> &result);
419 void addNameIdMapping(content_t i, std::string name);
421 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
422 u32 shader_id, bool use_normal_texture, bool backface_culling,
423 u8 alpha, u8 material_type);
426 // Features indexed by id
427 std::vector<ContentFeatures> m_content_features;
429 // A mapping for fast converting back and forth between names and ids
430 NameIdMapping m_name_id_mapping;
432 // Like m_name_id_mapping, but only from names to ids, and includes
433 // item aliases too. Updated by updateAliases()
434 // Note: Not serialized.
436 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
438 // A mapping from groups to a list of content_ts (and their levels)
439 // that belong to it. Necessary for a direct lookup in getIds().
440 // Note: Not serialized.
441 std::map<std::string, GroupItems> m_group_to_items;
443 // Next possibly free id
446 // List of node strings and node resolver callbacks to perform
447 std::list<NodeResolveInfo *> m_pending_node_lookups;
449 // True when all nodes have been registered
450 bool m_node_registration_complete;
454 CNodeDefManager::CNodeDefManager()
460 CNodeDefManager::~CNodeDefManager()
463 for (u32 i = 0; i < m_content_features.size(); i++) {
464 ContentFeatures *f = &m_content_features[i];
465 for (u32 j = 0; j < 24; j++) {
467 f->mesh_ptr[j]->drop();
474 void CNodeDefManager::clear()
476 m_content_features.clear();
477 m_name_id_mapping.clear();
478 m_name_id_mapping_with_aliases.clear();
479 m_group_to_items.clear();
482 m_node_registration_complete = false;
483 for (std::list<NodeResolveInfo *>::iterator
484 it = m_pending_node_lookups.begin();
485 it != m_pending_node_lookups.end();
488 m_pending_node_lookups.clear();
490 u32 initial_length = 0;
491 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
492 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
493 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
494 m_content_features.resize(initial_length);
496 // Set CONTENT_UNKNOWN
500 // Insert directly into containers
501 content_t c = CONTENT_UNKNOWN;
502 m_content_features[c] = f;
503 addNameIdMapping(c, f.name);
510 f.drawtype = NDT_AIRLIKE;
511 f.param_type = CPT_LIGHT;
512 f.light_propagates = true;
513 f.sunlight_propagates = true;
517 f.buildable_to = true;
518 f.is_ground_content = true;
519 // Insert directly into containers
520 content_t c = CONTENT_AIR;
521 m_content_features[c] = f;
522 addNameIdMapping(c, f.name);
525 // Set CONTENT_IGNORE
529 f.drawtype = NDT_AIRLIKE;
530 f.param_type = CPT_NONE;
531 f.light_propagates = false;
532 f.sunlight_propagates = false;
536 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
537 f.is_ground_content = true;
538 // Insert directly into containers
539 content_t c = CONTENT_IGNORE;
540 m_content_features[c] = f;
541 addNameIdMapping(c, f.name);
546 IWritableNodeDefManager *CNodeDefManager::clone()
548 CNodeDefManager *mgr = new CNodeDefManager();
554 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
556 return c < m_content_features.size()
557 ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
561 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
563 return get(n.getContent());
567 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
569 std::map<std::string, content_t>::const_iterator
570 i = m_name_id_mapping_with_aliases.find(name);
571 if(i == m_name_id_mapping_with_aliases.end())
578 content_t CNodeDefManager::getId(const std::string &name) const
580 content_t id = CONTENT_IGNORE;
586 void CNodeDefManager::getIds(const std::string &name,
587 std::set<content_t> &result) const
589 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
590 if (name.substr(0,6) != "group:") {
591 content_t id = CONTENT_IGNORE;
596 std::string group = name.substr(6);
598 std::map<std::string, GroupItems>::const_iterator
599 i = m_group_to_items.find(group);
600 if (i == m_group_to_items.end())
603 const GroupItems &items = i->second;
604 for (GroupItems::const_iterator j = items.begin();
605 j != items.end(); ++j) {
606 if ((*j).second != 0)
607 result.insert((*j).first);
609 //printf("getIds: %dus\n", t.stop());
613 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
615 content_t id = CONTENT_UNKNOWN;
621 // returns CONTENT_IGNORE if no free ID found
622 content_t CNodeDefManager::allocateId()
624 for (content_t id = m_next_id;
625 id >= m_next_id; // overflow?
627 while (id >= m_content_features.size()) {
628 m_content_features.push_back(ContentFeatures());
630 const ContentFeatures &f = m_content_features[id];
636 // If we arrive here, an overflow occurred in id.
637 // That means no ID was found
638 return CONTENT_IGNORE;
642 // IWritableNodeDefManager
643 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
647 assert(name == def.name);
649 // Don't allow redefining ignore (but allow air and unknown)
650 if (name == "ignore") {
651 infostream << "NodeDefManager: WARNING: Ignoring "
652 "CONTENT_IGNORE redefinition"<<std::endl;
653 return CONTENT_IGNORE;
656 content_t id = CONTENT_IGNORE;
657 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
660 if (id == CONTENT_IGNORE) {
661 infostream << "NodeDefManager: WARNING: Absolute "
662 "limit reached" << std::endl;
663 return CONTENT_IGNORE;
665 assert(id != CONTENT_IGNORE);
666 addNameIdMapping(id, name);
668 m_content_features[id] = def;
669 verbosestream << "NodeDefManager: registering content id \"" << id
670 << "\": name=\"" << def.name << "\""<<std::endl;
672 // Add this content to the list of all groups it belongs to
673 // FIXME: This should remove a node from groups it no longer
674 // belongs to when a node is re-registered
675 for (ItemGroupList::const_iterator i = def.groups.begin();
676 i != def.groups.end(); ++i) {
677 std::string group_name = i->first;
679 std::map<std::string, GroupItems>::iterator
680 j = m_group_to_items.find(group_name);
681 if (j == m_group_to_items.end()) {
682 m_group_to_items[group_name].push_back(
683 std::make_pair(id, i->second));
685 GroupItems &items = j->second;
686 items.push_back(std::make_pair(id, i->second));
693 content_t CNodeDefManager::allocateDummy(const std::string &name)
695 assert(name != ""); // Pre-condition
702 void CNodeDefManager::updateAliases(IItemDefManager *idef)
704 std::set<std::string> all = idef->getAll();
705 m_name_id_mapping_with_aliases.clear();
706 for (std::set<std::string>::iterator
707 i = all.begin(); i != all.end(); i++) {
708 std::string name = *i;
709 std::string convert_to = idef->getAlias(name);
711 if (m_name_id_mapping.getId(convert_to, id)) {
712 m_name_id_mapping_with_aliases.insert(
713 std::make_pair(name, id));
719 void CNodeDefManager::updateTextures(IGameDef *gamedef,
720 void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
721 void *progress_callback_args)
724 infostream << "CNodeDefManager::updateTextures(): Updating "
725 "textures in node definitions" << std::endl;
727 ITextureSource *tsrc = gamedef->tsrc();
728 IShaderSource *shdsrc = gamedef->getShaderSource();
729 scene::ISceneManager* smgr = gamedef->getSceneManager();
730 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
732 bool new_style_water = g_settings->getBool("new_style_water");
733 bool new_style_leaves = g_settings->getBool("new_style_leaves");
734 bool connected_glass = g_settings->getBool("connected_glass");
735 bool opaque_water = g_settings->getBool("opaque_water");
736 bool enable_shaders = g_settings->getBool("enable_shaders");
737 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
738 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
739 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
741 bool use_normal_texture = enable_shaders &&
742 (enable_bumpmapping || enable_parallax_occlusion);
744 u32 size = m_content_features.size();
746 for (u32 i = 0; i < size; i++) {
747 ContentFeatures *f = &m_content_features[i];
749 // Figure out the actual tiles to use
751 for (u32 j = 0; j < 6; j++) {
752 tiledef[j] = f->tiledef[j];
753 if (tiledef[j].name == "")
754 tiledef[j].name = "unknown_node.png";
757 bool is_liquid = false;
758 bool is_water_surface = false;
760 u8 material_type = (f->alpha == 255) ?
761 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
763 switch (f->drawtype) {
772 assert(f->liquid_type == LIQUID_SOURCE);
775 if (new_style_water){
779 f->backface_culling = false;
783 case NDT_FLOWINGLIQUID:
784 assert(f->liquid_type == LIQUID_FLOWING);
792 f->visual_solidness = 1;
794 case NDT_GLASSLIKE_FRAMED:
796 f->visual_solidness = 1;
798 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
800 f->visual_solidness = 1;
801 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
805 f->visual_solidness = 1;
807 case NDT_ALLFACES_OPTIONAL:
808 if (new_style_leaves) {
809 f->drawtype = NDT_ALLFACES;
811 f->visual_solidness = 1;
813 f->drawtype = NDT_NORMAL;
815 for (u32 i = 0; i < 6; i++)
816 tiledef[i].name += std::string("^[noalpha");
819 material_type = TILE_MATERIAL_WAVING_LEAVES;
823 f->backface_culling = false;
825 material_type = TILE_MATERIAL_WAVING_PLANTS;
828 f->backface_culling = false;
833 f->backface_culling = false;
845 material_type = (f->alpha == 255) ?
846 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
847 if (f->name == "default:water_source")
848 is_water_surface = true;
852 for (u16 j = 0; j < 6; j++) {
853 tile_shader[j] = shdsrc->getShader("nodes_shader",
854 material_type, f->drawtype);
857 if (is_water_surface) {
858 tile_shader[0] = shdsrc->getShader("water_surface_shader",
859 material_type, f->drawtype);
862 // Tiles (fill in f->tiles[])
863 for (u16 j = 0; j < 6; j++) {
864 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
865 use_normal_texture, f->backface_culling, f->alpha, material_type);
868 // Special tiles (fill in f->special_tiles[])
869 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
870 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
871 tile_shader[j], use_normal_texture,
872 f->tiledef_special[j].backface_culling, f->alpha, material_type);
875 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
877 // Read the mesh and apply scale
878 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
880 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
881 scaleMesh(f->mesh_ptr[0], scale);
882 recalculateBoundingBox(f->mesh_ptr[0]);
883 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
885 } else if ((f->drawtype == NDT_NODEBOX) &&
886 ((f->node_box.type == NODEBOX_REGULAR) ||
887 (f->node_box.type == NODEBOX_FIXED)) &&
888 (!f->node_box.fixed.empty())) {
889 //Convert regular nodebox nodes to meshnodes
890 //Change the drawtype and apply scale
891 f->drawtype = NDT_MESH;
892 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
893 v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
894 scaleMesh(f->mesh_ptr[0], scale);
895 recalculateBoundingBox(f->mesh_ptr[0]);
896 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
899 //Cache 6dfacedir and wallmounted rotated clones of meshes
900 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
901 for (u16 j = 1; j < 24; j++) {
902 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
903 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
904 recalculateBoundingBox(f->mesh_ptr[j]);
905 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
907 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
908 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
909 for (u16 j = 1; j < 6; j++) {
910 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
911 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
912 recalculateBoundingBox(f->mesh_ptr[j]);
913 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
915 rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
916 recalculateBoundingBox(f->mesh_ptr[0]);
917 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
920 progress_callback(progress_callback_args, i, size);
927 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
928 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
929 bool backface_culling, u8 alpha, u8 material_type)
931 tile->shader_id = shader_id;
932 tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
934 tile->material_type = material_type;
937 if (use_normal_texture)
938 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
941 tile->material_flags = 0;
942 if (backface_culling)
943 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
944 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
945 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
947 // Animation parameters
949 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
950 // Get texture size to determine frame count by aspect ratio
951 v2u32 size = tile->texture->getOriginalSize();
952 int frame_height = (float)size.X /
953 (float)tiledef->animation.aspect_w *
954 (float)tiledef->animation.aspect_h;
955 frame_count = size.Y / frame_height;
956 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
957 tile->animation_frame_count = frame_count;
958 tile->animation_frame_length_ms = frame_length_ms;
961 if (frame_count == 1) {
962 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
964 std::ostringstream os(std::ios::binary);
965 tile->frames.resize(frame_count);
967 for (int i = 0; i < frame_count; i++) {
972 os << tiledef->name << "^[verticalframe:"
973 << frame_count << ":" << i;
975 frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
976 if (tile->normal_texture)
977 frame.normal_texture = tsrc->getNormalTexture(os.str());
978 tile->frames[i] = frame;
985 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
987 writeU8(os, 1); // version
989 std::ostringstream os2(std::ios::binary);
990 for (u32 i = 0; i < m_content_features.size(); i++) {
991 if (i == CONTENT_IGNORE || i == CONTENT_AIR
992 || i == CONTENT_UNKNOWN)
994 ContentFeatures *f = &m_content_features[i];
998 // Wrap it in a string to allow different lengths without
999 // strict version incompatibilities
1000 std::ostringstream wrapper_os(std::ios::binary);
1001 f->serialize(wrapper_os, protocol_version);
1002 os2<<serializeString(wrapper_os.str());
1004 // must not overflow
1005 u16 next = count + 1;
1006 FATAL_ERROR_IF(next < count, "Overflow");
1009 writeU16(os, count);
1010 os << serializeLongString(os2.str());
1014 void CNodeDefManager::deSerialize(std::istream &is)
1017 int version = readU8(is);
1019 throw SerializationError("unsupported NodeDefinitionManager version");
1020 u16 count = readU16(is);
1021 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1023 for (u16 n = 0; n < count; n++) {
1024 u16 i = readU16(is2);
1026 // Read it from the string wrapper
1027 std::string wrapper = deSerializeString(is2);
1028 std::istringstream wrapper_is(wrapper, std::ios::binary);
1029 f.deSerialize(wrapper_is);
1031 // Check error conditions
1032 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1033 infostream << "NodeDefManager::deSerialize(): WARNING: "
1034 "not changing builtin node " << i << std::endl;
1038 infostream << "NodeDefManager::deSerialize(): WARNING: "
1039 "received empty name" << std::endl;
1045 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1046 infostream << "NodeDefManager::deSerialize(): WARNING: "
1047 "already defined with different ID: " << f.name << std::endl;
1051 // All is ok, add node definition with the requested ID
1052 if (i >= m_content_features.size())
1053 m_content_features.resize((u32)(i) + 1);
1054 m_content_features[i] = f;
1055 addNameIdMapping(i, f.name);
1056 verbosestream << "deserialized " << f.name << std::endl;
1061 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1063 m_name_id_mapping.set(i, name);
1064 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1068 IWritableNodeDefManager *createNodeDefManager()
1070 return new CNodeDefManager();
1074 //// Serialization of old ContentFeatures formats
1075 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1077 if (protocol_version == 13)
1079 writeU8(os, 5); // version
1080 os<<serializeString(name);
1081 writeU16(os, groups.size());
1082 for (ItemGroupList::const_iterator
1083 i = groups.begin(); i != groups.end(); i++) {
1084 os<<serializeString(i->first);
1085 writeS16(os, i->second);
1087 writeU8(os, drawtype);
1088 writeF1000(os, visual_scale);
1090 for (u32 i = 0; i < 6; i++)
1091 tiledef[i].serialize(os, protocol_version);
1092 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1094 for (u32 i = 0; i < 2; i++)
1095 tiledef_special[i].serialize(os, protocol_version);
1097 writeU8(os, post_effect_color.getAlpha());
1098 writeU8(os, post_effect_color.getRed());
1099 writeU8(os, post_effect_color.getGreen());
1100 writeU8(os, post_effect_color.getBlue());
1101 writeU8(os, param_type);
1102 writeU8(os, param_type_2);
1103 writeU8(os, is_ground_content);
1104 writeU8(os, light_propagates);
1105 writeU8(os, sunlight_propagates);
1106 writeU8(os, walkable);
1107 writeU8(os, pointable);
1108 writeU8(os, diggable);
1109 writeU8(os, climbable);
1110 writeU8(os, buildable_to);
1111 os<<serializeString(""); // legacy: used to be metadata_name
1112 writeU8(os, liquid_type);
1113 os<<serializeString(liquid_alternative_flowing);
1114 os<<serializeString(liquid_alternative_source);
1115 writeU8(os, liquid_viscosity);
1116 writeU8(os, light_source);
1117 writeU32(os, damage_per_second);
1118 node_box.serialize(os, protocol_version);
1119 selection_box.serialize(os, protocol_version);
1120 writeU8(os, legacy_facedir_simple);
1121 writeU8(os, legacy_wallmounted);
1122 serializeSimpleSoundSpec(sound_footstep, os);
1123 serializeSimpleSoundSpec(sound_dig, os);
1124 serializeSimpleSoundSpec(sound_dug, os);
1126 else if (protocol_version > 13 && protocol_version < 24) {
1127 writeU8(os, 6); // version
1128 os<<serializeString(name);
1129 writeU16(os, groups.size());
1130 for (ItemGroupList::const_iterator
1131 i = groups.begin(); i != groups.end(); i++) {
1132 os<<serializeString(i->first);
1133 writeS16(os, i->second);
1135 writeU8(os, drawtype);
1136 writeF1000(os, visual_scale);
1138 for (u32 i = 0; i < 6; i++)
1139 tiledef[i].serialize(os, protocol_version);
1140 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1142 for (u32 i = 0; i < 2; i++)
1143 tiledef_special[i].serialize(os, protocol_version);
1145 writeU8(os, post_effect_color.getAlpha());
1146 writeU8(os, post_effect_color.getRed());
1147 writeU8(os, post_effect_color.getGreen());
1148 writeU8(os, post_effect_color.getBlue());
1149 writeU8(os, param_type);
1150 writeU8(os, param_type_2);
1151 writeU8(os, is_ground_content);
1152 writeU8(os, light_propagates);
1153 writeU8(os, sunlight_propagates);
1154 writeU8(os, walkable);
1155 writeU8(os, pointable);
1156 writeU8(os, diggable);
1157 writeU8(os, climbable);
1158 writeU8(os, buildable_to);
1159 os<<serializeString(""); // legacy: used to be metadata_name
1160 writeU8(os, liquid_type);
1161 os<<serializeString(liquid_alternative_flowing);
1162 os<<serializeString(liquid_alternative_source);
1163 writeU8(os, liquid_viscosity);
1164 writeU8(os, liquid_renewable);
1165 writeU8(os, light_source);
1166 writeU32(os, damage_per_second);
1167 node_box.serialize(os, protocol_version);
1168 selection_box.serialize(os, protocol_version);
1169 writeU8(os, legacy_facedir_simple);
1170 writeU8(os, legacy_wallmounted);
1171 serializeSimpleSoundSpec(sound_footstep, os);
1172 serializeSimpleSoundSpec(sound_dig, os);
1173 serializeSimpleSoundSpec(sound_dug, os);
1174 writeU8(os, rightclickable);
1175 writeU8(os, drowning);
1176 writeU8(os, leveled);
1177 writeU8(os, liquid_range);
1179 throw SerializationError("ContentFeatures::serialize(): "
1180 "Unsupported version requested");
1184 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1186 if (version == 5) // In PROTOCOL_VERSION 13
1188 name = deSerializeString(is);
1190 u32 groups_size = readU16(is);
1191 for(u32 i=0; i<groups_size; i++){
1192 std::string name = deSerializeString(is);
1193 int value = readS16(is);
1194 groups[name] = value;
1196 drawtype = (enum NodeDrawType)readU8(is);
1197 visual_scale = readF1000(is);
1198 if (readU8(is) != 6)
1199 throw SerializationError("unsupported tile count");
1200 for (u32 i = 0; i < 6; i++)
1201 tiledef[i].deSerialize(is);
1202 if (readU8(is) != CF_SPECIAL_COUNT)
1203 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1204 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1205 tiledef_special[i].deSerialize(is);
1207 post_effect_color.setAlpha(readU8(is));
1208 post_effect_color.setRed(readU8(is));
1209 post_effect_color.setGreen(readU8(is));
1210 post_effect_color.setBlue(readU8(is));
1211 param_type = (enum ContentParamType)readU8(is);
1212 param_type_2 = (enum ContentParamType2)readU8(is);
1213 is_ground_content = readU8(is);
1214 light_propagates = readU8(is);
1215 sunlight_propagates = readU8(is);
1216 walkable = readU8(is);
1217 pointable = readU8(is);
1218 diggable = readU8(is);
1219 climbable = readU8(is);
1220 buildable_to = readU8(is);
1221 deSerializeString(is); // legacy: used to be metadata_name
1222 liquid_type = (enum LiquidType)readU8(is);
1223 liquid_alternative_flowing = deSerializeString(is);
1224 liquid_alternative_source = deSerializeString(is);
1225 liquid_viscosity = readU8(is);
1226 light_source = readU8(is);
1227 damage_per_second = readU32(is);
1228 node_box.deSerialize(is);
1229 selection_box.deSerialize(is);
1230 legacy_facedir_simple = readU8(is);
1231 legacy_wallmounted = readU8(is);
1232 deSerializeSimpleSoundSpec(sound_footstep, is);
1233 deSerializeSimpleSoundSpec(sound_dig, is);
1234 deSerializeSimpleSoundSpec(sound_dug, is);
1235 } else if (version == 6) {
1236 name = deSerializeString(is);
1238 u32 groups_size = readU16(is);
1239 for (u32 i = 0; i < groups_size; i++) {
1240 std::string name = deSerializeString(is);
1241 int value = readS16(is);
1242 groups[name] = value;
1244 drawtype = (enum NodeDrawType)readU8(is);
1245 visual_scale = readF1000(is);
1246 if (readU8(is) != 6)
1247 throw SerializationError("unsupported tile count");
1248 for (u32 i = 0; i < 6; i++)
1249 tiledef[i].deSerialize(is);
1250 // CF_SPECIAL_COUNT in version 6 = 2
1251 if (readU8(is) != 2)
1252 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1253 for (u32 i = 0; i < 2; i++)
1254 tiledef_special[i].deSerialize(is);
1256 post_effect_color.setAlpha(readU8(is));
1257 post_effect_color.setRed(readU8(is));
1258 post_effect_color.setGreen(readU8(is));
1259 post_effect_color.setBlue(readU8(is));
1260 param_type = (enum ContentParamType)readU8(is);
1261 param_type_2 = (enum ContentParamType2)readU8(is);
1262 is_ground_content = readU8(is);
1263 light_propagates = readU8(is);
1264 sunlight_propagates = readU8(is);
1265 walkable = readU8(is);
1266 pointable = readU8(is);
1267 diggable = readU8(is);
1268 climbable = readU8(is);
1269 buildable_to = readU8(is);
1270 deSerializeString(is); // legacy: used to be metadata_name
1271 liquid_type = (enum LiquidType)readU8(is);
1272 liquid_alternative_flowing = deSerializeString(is);
1273 liquid_alternative_source = deSerializeString(is);
1274 liquid_viscosity = readU8(is);
1275 liquid_renewable = readU8(is);
1276 light_source = readU8(is);
1277 damage_per_second = readU32(is);
1278 node_box.deSerialize(is);
1279 selection_box.deSerialize(is);
1280 legacy_facedir_simple = readU8(is);
1281 legacy_wallmounted = readU8(is);
1282 deSerializeSimpleSoundSpec(sound_footstep, is);
1283 deSerializeSimpleSoundSpec(sound_dig, is);
1284 deSerializeSimpleSoundSpec(sound_dug, is);
1285 rightclickable = readU8(is);
1286 drowning = readU8(is);
1287 leveled = readU8(is);
1288 liquid_range = readU8(is);
1290 throw SerializationError("unsupported ContentFeatures version");
1295 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1297 return m_node_registration_complete;
1301 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1303 m_node_registration_complete = completed;
1307 void CNodeDefManager::pendNodeResolve(NodeResolveInfo *nri)
1309 nri->resolver->m_ndef = this;
1310 if (m_node_registration_complete) {
1311 nri->resolver->resolveNodeNames(nri);
1312 nri->resolver->m_lookup_done = true;
1315 m_pending_node_lookups.push_back(nri);
1320 void CNodeDefManager::cancelNodeResolve(NodeResolver *resolver)
1322 for (std::list<NodeResolveInfo *>::iterator
1323 it = m_pending_node_lookups.begin();
1324 it != m_pending_node_lookups.end();
1326 NodeResolveInfo *nri = *it;
1327 if (resolver == nri->resolver) {
1328 it = m_pending_node_lookups.erase(it);
1335 void CNodeDefManager::runNodeResolverCallbacks()
1337 while (!m_pending_node_lookups.empty()) {
1338 NodeResolveInfo *nri = m_pending_node_lookups.front();
1339 m_pending_node_lookups.pop_front();
1340 nri->resolver->resolveNodeNames(nri);
1341 nri->resolver->m_lookup_done = true;
1347 bool CNodeDefManager::getIdFromResolveInfo(NodeResolveInfo *nri,
1348 const std::string &node_alt, content_t c_fallback, content_t &result)
1350 if (nri->nodenames.empty()) {
1351 result = c_fallback;
1352 errorstream << "Resolver empty nodename list" << std::endl;
1357 std::string name = nri->nodenames.front();
1358 nri->nodenames.pop_front();
1360 bool success = getId(name, c);
1361 if (!success && node_alt != "") {
1363 success = getId(name, c);
1367 errorstream << "Resolver: Failed to resolve node name '" << name
1368 << "'." << std::endl;
1377 bool CNodeDefManager::getIdsFromResolveInfo(NodeResolveInfo *nri,
1378 std::vector<content_t> &result)
1380 bool success = true;
1382 if (nri->nodelistinfo.empty()) {
1383 errorstream << "Resolver: Empty nodelistinfo list" << std::endl;
1387 NodeListInfo listinfo = nri->nodelistinfo.front();
1388 nri->nodelistinfo.pop_front();
1390 while (listinfo.length--) {
1391 if (nri->nodenames.empty()) {
1392 errorstream << "Resolver: Empty nodename list" << std::endl;
1397 std::string name = nri->nodenames.front();
1398 nri->nodenames.pop_front();
1400 if (name.substr(0,6) != "group:") {
1401 if (getId(name, c)) {
1402 result.push_back(c);
1403 } else if (listinfo.all_required) {
1404 errorstream << "Resolver: Failed to resolve node name '" << name
1405 << "'." << std::endl;
1406 result.push_back(listinfo.c_fallback);
1410 std::set<content_t> cids;
1411 std::set<content_t>::iterator it;
1413 for (it = cids.begin(); it != cids.end(); ++it)
1414 result.push_back(*it);