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(NodeResolver *nr, NodeResolveMethod how);
410 virtual bool cancelNodeResolveCallback(NodeResolver *nr);
411 virtual void runNodeResolveCallbacks();
414 void addNameIdMapping(content_t i, std::string name);
416 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
417 u32 shader_id, bool use_normal_texture, bool backface_culling,
418 u8 alpha, u8 material_type);
421 // Features indexed by id
422 std::vector<ContentFeatures> m_content_features;
424 // A mapping for fast converting back and forth between names and ids
425 NameIdMapping m_name_id_mapping;
427 // Like m_name_id_mapping, but only from names to ids, and includes
428 // item aliases too. Updated by updateAliases()
429 // Note: Not serialized.
431 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
433 // A mapping from groups to a list of content_ts (and their levels)
434 // that belong to it. Necessary for a direct lookup in getIds().
435 // Note: Not serialized.
436 std::map<std::string, GroupItems> m_group_to_items;
438 // Next possibly free id
441 // NodeResolvers to callback once node registration has ended
442 std::vector<NodeResolver *> m_pending_resolve_callbacks;
444 // True when all nodes have been registered
445 bool m_node_registration_complete;
449 CNodeDefManager::CNodeDefManager()
455 CNodeDefManager::~CNodeDefManager()
458 for (u32 i = 0; i < m_content_features.size(); i++) {
459 ContentFeatures *f = &m_content_features[i];
460 for (u32 j = 0; j < 24; j++) {
462 f->mesh_ptr[j]->drop();
469 void CNodeDefManager::clear()
471 m_content_features.clear();
472 m_name_id_mapping.clear();
473 m_name_id_mapping_with_aliases.clear();
474 m_group_to_items.clear();
477 m_node_registration_complete = false;
478 m_pending_resolve_callbacks.clear();
480 u32 initial_length = 0;
481 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
482 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
483 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
484 m_content_features.resize(initial_length);
486 // Set CONTENT_UNKNOWN
490 // Insert directly into containers
491 content_t c = CONTENT_UNKNOWN;
492 m_content_features[c] = f;
493 addNameIdMapping(c, f.name);
500 f.drawtype = NDT_AIRLIKE;
501 f.param_type = CPT_LIGHT;
502 f.light_propagates = true;
503 f.sunlight_propagates = true;
507 f.buildable_to = true;
508 f.is_ground_content = true;
509 // Insert directly into containers
510 content_t c = CONTENT_AIR;
511 m_content_features[c] = f;
512 addNameIdMapping(c, f.name);
515 // Set CONTENT_IGNORE
519 f.drawtype = NDT_AIRLIKE;
520 f.param_type = CPT_NONE;
521 f.light_propagates = false;
522 f.sunlight_propagates = false;
526 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
527 f.is_ground_content = true;
528 // Insert directly into containers
529 content_t c = CONTENT_IGNORE;
530 m_content_features[c] = f;
531 addNameIdMapping(c, f.name);
536 IWritableNodeDefManager *CNodeDefManager::clone()
538 CNodeDefManager *mgr = new CNodeDefManager();
544 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
546 return c < m_content_features.size()
547 ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
551 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
553 return get(n.getContent());
557 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
559 std::map<std::string, content_t>::const_iterator
560 i = m_name_id_mapping_with_aliases.find(name);
561 if(i == m_name_id_mapping_with_aliases.end())
568 content_t CNodeDefManager::getId(const std::string &name) const
570 content_t id = CONTENT_IGNORE;
576 void CNodeDefManager::getIds(const std::string &name,
577 std::set<content_t> &result) const
579 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
580 if (name.substr(0,6) != "group:") {
581 content_t id = CONTENT_IGNORE;
586 std::string group = name.substr(6);
588 std::map<std::string, GroupItems>::const_iterator
589 i = m_group_to_items.find(group);
590 if (i == m_group_to_items.end())
593 const GroupItems &items = i->second;
594 for (GroupItems::const_iterator j = items.begin();
595 j != items.end(); ++j) {
596 if ((*j).second != 0)
597 result.insert((*j).first);
599 //printf("getIds: %dus\n", t.stop());
603 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
605 content_t id = CONTENT_UNKNOWN;
611 // returns CONTENT_IGNORE if no free ID found
612 content_t CNodeDefManager::allocateId()
614 for (content_t id = m_next_id;
615 id >= m_next_id; // overflow?
617 while (id >= m_content_features.size()) {
618 m_content_features.push_back(ContentFeatures());
620 const ContentFeatures &f = m_content_features[id];
626 // If we arrive here, an overflow occurred in id.
627 // That means no ID was found
628 return CONTENT_IGNORE;
632 // IWritableNodeDefManager
633 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
637 assert(name == def.name);
639 // Don't allow redefining ignore (but allow air and unknown)
640 if (name == "ignore") {
641 infostream << "NodeDefManager: WARNING: Ignoring "
642 "CONTENT_IGNORE redefinition"<<std::endl;
643 return CONTENT_IGNORE;
646 content_t id = CONTENT_IGNORE;
647 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
650 if (id == CONTENT_IGNORE) {
651 infostream << "NodeDefManager: WARNING: Absolute "
652 "limit reached" << std::endl;
653 return CONTENT_IGNORE;
655 assert(id != CONTENT_IGNORE);
656 addNameIdMapping(id, name);
658 m_content_features[id] = def;
659 verbosestream << "NodeDefManager: registering content id \"" << id
660 << "\": name=\"" << def.name << "\""<<std::endl;
662 // Add this content to the list of all groups it belongs to
663 // FIXME: This should remove a node from groups it no longer
664 // belongs to when a node is re-registered
665 for (ItemGroupList::const_iterator i = def.groups.begin();
666 i != def.groups.end(); ++i) {
667 std::string group_name = i->first;
669 std::map<std::string, GroupItems>::iterator
670 j = m_group_to_items.find(group_name);
671 if (j == m_group_to_items.end()) {
672 m_group_to_items[group_name].push_back(
673 std::make_pair(id, i->second));
675 GroupItems &items = j->second;
676 items.push_back(std::make_pair(id, i->second));
683 content_t CNodeDefManager::allocateDummy(const std::string &name)
685 assert(name != ""); // Pre-condition
692 void CNodeDefManager::updateAliases(IItemDefManager *idef)
694 std::set<std::string> all = idef->getAll();
695 m_name_id_mapping_with_aliases.clear();
696 for (std::set<std::string>::iterator
697 i = all.begin(); i != all.end(); i++) {
698 std::string name = *i;
699 std::string convert_to = idef->getAlias(name);
701 if (m_name_id_mapping.getId(convert_to, id)) {
702 m_name_id_mapping_with_aliases.insert(
703 std::make_pair(name, id));
709 void CNodeDefManager::updateTextures(IGameDef *gamedef,
710 void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
711 void *progress_callback_args)
714 infostream << "CNodeDefManager::updateTextures(): Updating "
715 "textures in node definitions" << std::endl;
717 ITextureSource *tsrc = gamedef->tsrc();
718 IShaderSource *shdsrc = gamedef->getShaderSource();
719 scene::ISceneManager* smgr = gamedef->getSceneManager();
720 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
722 bool new_style_water = g_settings->getBool("new_style_water");
723 bool new_style_leaves = g_settings->getBool("new_style_leaves");
724 bool connected_glass = g_settings->getBool("connected_glass");
725 bool opaque_water = g_settings->getBool("opaque_water");
726 bool enable_shaders = g_settings->getBool("enable_shaders");
727 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
728 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
729 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
731 bool use_normal_texture = enable_shaders &&
732 (enable_bumpmapping || enable_parallax_occlusion);
734 u32 size = m_content_features.size();
736 for (u32 i = 0; i < size; i++) {
737 ContentFeatures *f = &m_content_features[i];
739 // Figure out the actual tiles to use
741 for (u32 j = 0; j < 6; j++) {
742 tiledef[j] = f->tiledef[j];
743 if (tiledef[j].name == "")
744 tiledef[j].name = "unknown_node.png";
747 bool is_liquid = false;
748 bool is_water_surface = false;
750 u8 material_type = (f->alpha == 255) ?
751 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
753 switch (f->drawtype) {
762 assert(f->liquid_type == LIQUID_SOURCE);
765 if (new_style_water){
769 f->backface_culling = false;
773 case NDT_FLOWINGLIQUID:
774 assert(f->liquid_type == LIQUID_FLOWING);
782 f->visual_solidness = 1;
784 case NDT_GLASSLIKE_FRAMED:
786 f->visual_solidness = 1;
788 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
790 f->visual_solidness = 1;
791 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
795 f->visual_solidness = 1;
797 case NDT_ALLFACES_OPTIONAL:
798 if (new_style_leaves) {
799 f->drawtype = NDT_ALLFACES;
801 f->visual_solidness = 1;
803 f->drawtype = NDT_NORMAL;
805 for (u32 i = 0; i < 6; i++)
806 tiledef[i].name += std::string("^[noalpha");
809 material_type = TILE_MATERIAL_WAVING_LEAVES;
813 f->backface_culling = false;
815 material_type = TILE_MATERIAL_WAVING_PLANTS;
818 f->backface_culling = false;
823 f->backface_culling = false;
835 material_type = (f->alpha == 255) ?
836 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
837 if (f->name == "default:water_source")
838 is_water_surface = true;
842 for (u16 j = 0; j < 6; j++) {
843 tile_shader[j] = shdsrc->getShader("nodes_shader",
844 material_type, f->drawtype);
847 if (is_water_surface) {
848 tile_shader[0] = shdsrc->getShader("water_surface_shader",
849 material_type, f->drawtype);
852 // Tiles (fill in f->tiles[])
853 for (u16 j = 0; j < 6; j++) {
854 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
855 use_normal_texture, f->backface_culling, f->alpha, material_type);
858 // Special tiles (fill in f->special_tiles[])
859 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
860 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
861 tile_shader[j], use_normal_texture,
862 f->tiledef_special[j].backface_culling, f->alpha, material_type);
865 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
867 // Read the mesh and apply scale
868 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
870 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
871 scaleMesh(f->mesh_ptr[0], scale);
872 recalculateBoundingBox(f->mesh_ptr[0]);
873 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
875 } else if ((f->drawtype == NDT_NODEBOX) &&
876 ((f->node_box.type == NODEBOX_REGULAR) ||
877 (f->node_box.type == NODEBOX_FIXED)) &&
878 (!f->node_box.fixed.empty())) {
879 //Convert regular nodebox nodes to meshnodes
880 //Change the drawtype and apply scale
881 f->drawtype = NDT_MESH;
882 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
883 v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
884 scaleMesh(f->mesh_ptr[0], scale);
885 recalculateBoundingBox(f->mesh_ptr[0]);
886 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
889 //Cache 6dfacedir and wallmounted rotated clones of meshes
890 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
891 for (u16 j = 1; j < 24; j++) {
892 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
893 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
894 recalculateBoundingBox(f->mesh_ptr[j]);
895 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
897 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
898 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
899 for (u16 j = 1; j < 6; j++) {
900 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
901 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
902 recalculateBoundingBox(f->mesh_ptr[j]);
903 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
905 rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
906 recalculateBoundingBox(f->mesh_ptr[0]);
907 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
910 progress_callback(progress_callback_args, i, size);
917 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
918 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
919 bool backface_culling, u8 alpha, u8 material_type)
921 tile->shader_id = shader_id;
922 tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
924 tile->material_type = material_type;
927 if (use_normal_texture)
928 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
931 tile->material_flags = 0;
932 if (backface_culling)
933 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
934 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
935 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
937 // Animation parameters
939 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
940 // Get texture size to determine frame count by aspect ratio
941 v2u32 size = tile->texture->getOriginalSize();
942 int frame_height = (float)size.X /
943 (float)tiledef->animation.aspect_w *
944 (float)tiledef->animation.aspect_h;
945 frame_count = size.Y / frame_height;
946 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
947 tile->animation_frame_count = frame_count;
948 tile->animation_frame_length_ms = frame_length_ms;
951 if (frame_count == 1) {
952 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
954 std::ostringstream os(std::ios::binary);
955 tile->frames.resize(frame_count);
957 for (int i = 0; i < frame_count; i++) {
962 os << tiledef->name << "^[verticalframe:"
963 << frame_count << ":" << i;
965 frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
966 if (tile->normal_texture)
967 frame.normal_texture = tsrc->getNormalTexture(os.str());
968 tile->frames[i] = frame;
975 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
977 writeU8(os, 1); // version
979 std::ostringstream os2(std::ios::binary);
980 for (u32 i = 0; i < m_content_features.size(); i++) {
981 if (i == CONTENT_IGNORE || i == CONTENT_AIR
982 || i == CONTENT_UNKNOWN)
984 ContentFeatures *f = &m_content_features[i];
988 // Wrap it in a string to allow different lengths without
989 // strict version incompatibilities
990 std::ostringstream wrapper_os(std::ios::binary);
991 f->serialize(wrapper_os, protocol_version);
992 os2<<serializeString(wrapper_os.str());
995 u16 next = count + 1;
996 FATAL_ERROR_IF(next < count, "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(NodeResolver *nr, NodeResolveMethod how)
1302 case NODE_RESOLVE_NONE:
1304 case NODE_RESOLVE_DIRECT:
1305 nr->nodeResolveInternal();
1307 case NODE_RESOLVE_DEFERRED:
1308 if (m_node_registration_complete)
1309 nr->nodeResolveInternal();
1311 m_pending_resolve_callbacks.push_back(nr);
1317 bool CNodeDefManager::cancelNodeResolveCallback(NodeResolver *nr)
1319 size_t len = m_pending_resolve_callbacks.size();
1320 for (size_t i = 0; i != len; i++) {
1321 if (nr != m_pending_resolve_callbacks[i])
1325 m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
1326 m_pending_resolve_callbacks.resize(len);
1334 void CNodeDefManager::runNodeResolveCallbacks()
1336 for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
1337 NodeResolver *nr = m_pending_resolve_callbacks[i];
1338 nr->nodeResolveInternal();
1341 m_pending_resolve_callbacks.clear();
1349 NodeResolver::NodeResolver()
1352 m_nodenames_idx = 0;
1353 m_nnlistsizes_idx = 0;
1354 m_resolve_done = false;
1356 m_nodenames.reserve(16);
1357 m_nnlistsizes.reserve(4);
1361 NodeResolver::~NodeResolver()
1363 if (!m_resolve_done && m_ndef)
1364 m_ndef->cancelNodeResolveCallback(this);
1368 void NodeResolver::nodeResolveInternal()
1370 m_nodenames_idx = 0;
1371 m_nnlistsizes_idx = 0;
1374 m_resolve_done = true;
1376 m_nodenames.clear();
1377 m_nnlistsizes.clear();
1381 const std::string &NodeResolver::getNodeName(content_t c) const
1383 if (c < m_nodenames.size())
1384 return m_nodenames[c];
1387 return m_ndef->get(c).name;
1389 static const std::string unknown_str("unknown");
1394 bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
1395 const std::string &node_alt, content_t c_fallback)
1397 if (m_nodenames_idx == m_nodenames.size()) {
1398 *result_out = c_fallback;
1399 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1404 std::string name = m_nodenames[m_nodenames_idx++];
1406 bool success = m_ndef->getId(name, c);
1407 if (!success && node_alt != "") {
1409 success = m_ndef->getId(name, c);
1413 errorstream << "NodeResolver: failed to resolve node name '" << name
1414 << "'." << std::endl;
1423 bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
1424 bool all_required, content_t c_fallback)
1426 bool success = true;
1428 if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
1429 errorstream << "NodeResolver: no more node lists" << std::endl;
1433 size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
1436 if (m_nodenames_idx == m_nodenames.size()) {
1437 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1442 std::string &name = m_nodenames[m_nodenames_idx++];
1444 if (name.substr(0,6) != "group:") {
1445 if (m_ndef->getId(name, c)) {
1446 result_out->push_back(c);
1447 } else if (all_required) {
1448 errorstream << "NodeResolver: failed to resolve node name '"
1449 << name << "'." << std::endl;
1450 result_out->push_back(c_fallback);
1454 std::set<content_t> cids;
1455 std::set<content_t>::iterator it;
1456 m_ndef->getIds(name, cids);
1457 for (it = cids.begin(); it != cids.end(); ++it)
1458 result_out->push_back(*it);