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"
36 #include <fstream> // Used in applyTextureOverrides()
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 >= 26)
125 else if (protocol_version >= 17)
129 os<<serializeString(name);
130 writeU8(os, animation.type);
131 writeU16(os, animation.aspect_w);
132 writeU16(os, animation.aspect_h);
133 writeF1000(os, animation.length);
134 if (protocol_version >= 17)
135 writeU8(os, backface_culling);
136 if (protocol_version >= 26) {
137 writeU8(os, tileable_horizontal);
138 writeU8(os, tileable_vertical);
142 void TileDef::deSerialize(std::istream &is)
144 int version = readU8(is);
145 name = deSerializeString(is);
146 animation.type = (TileAnimationType)readU8(is);
147 animation.aspect_w = readU16(is);
148 animation.aspect_h = readU16(is);
149 animation.length = readF1000(is);
151 backface_culling = readU8(is);
153 tileable_horizontal = readU8(is);
154 tileable_vertical = readU8(is);
160 SimpleSoundSpec serialization
163 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
166 os<<serializeString(ss.name);
167 writeF1000(os, ss.gain);
169 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
171 ss.name = deSerializeString(is);
172 ss.gain = readF1000(is);
179 ContentFeatures::ContentFeatures()
184 ContentFeatures::~ContentFeatures()
188 void ContentFeatures::reset()
195 visual_solidness = 0;
196 backface_culling = true;
199 has_on_construct = false;
200 has_on_destruct = false;
201 has_after_destruct = false;
205 NOTE: Most of this is always overridden by the default values given
210 // Unknown nodes can be dug
211 groups["dig_immediate"] = 2;
212 drawtype = NDT_NORMAL;
215 for(u32 i = 0; i < 24; i++)
217 minimap_color = video::SColor(0, 0, 0, 0);
220 for(u32 i = 0; i < 6; i++)
221 tiledef[i] = TileDef();
222 for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
223 tiledef_special[j] = TileDef();
225 post_effect_color = video::SColor(0, 0, 0, 0);
226 param_type = CPT_NONE;
227 param_type_2 = CPT2_NONE;
228 is_ground_content = false;
229 light_propagates = false;
230 sunlight_propagates = false;
235 buildable_to = false;
236 rightclickable = true;
238 liquid_type = LIQUID_NONE;
239 liquid_alternative_flowing = "";
240 liquid_alternative_source = "";
241 liquid_viscosity = 0;
242 liquid_renewable = true;
243 liquid_range = LIQUID_LEVEL_MAX+1;
246 damage_per_second = 0;
247 node_box = NodeBox();
248 selection_box = NodeBox();
249 collision_box = NodeBox();
251 legacy_facedir_simple = false;
252 legacy_wallmounted = false;
253 sound_footstep = SimpleSoundSpec();
254 sound_dig = SimpleSoundSpec("__group");
255 sound_dug = SimpleSoundSpec();
258 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
260 if(protocol_version < 24){
261 serializeOld(os, protocol_version);
265 writeU8(os, 7); // version
266 os<<serializeString(name);
267 writeU16(os, groups.size());
268 for(ItemGroupList::const_iterator
269 i = groups.begin(); i != groups.end(); ++i){
270 os<<serializeString(i->first);
271 writeS16(os, i->second);
273 writeU8(os, drawtype);
274 writeF1000(os, visual_scale);
276 for(u32 i = 0; i < 6; i++)
277 tiledef[i].serialize(os, protocol_version);
278 writeU8(os, CF_SPECIAL_COUNT);
279 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++){
280 tiledef_special[i].serialize(os, protocol_version);
283 writeU8(os, post_effect_color.getAlpha());
284 writeU8(os, post_effect_color.getRed());
285 writeU8(os, post_effect_color.getGreen());
286 writeU8(os, post_effect_color.getBlue());
287 writeU8(os, param_type);
288 writeU8(os, param_type_2);
289 writeU8(os, is_ground_content);
290 writeU8(os, light_propagates);
291 writeU8(os, sunlight_propagates);
292 writeU8(os, walkable);
293 writeU8(os, pointable);
294 writeU8(os, diggable);
295 writeU8(os, climbable);
296 writeU8(os, buildable_to);
297 os<<serializeString(""); // legacy: used to be metadata_name
298 writeU8(os, liquid_type);
299 os<<serializeString(liquid_alternative_flowing);
300 os<<serializeString(liquid_alternative_source);
301 writeU8(os, liquid_viscosity);
302 writeU8(os, liquid_renewable);
303 writeU8(os, light_source);
304 writeU32(os, damage_per_second);
305 node_box.serialize(os, protocol_version);
306 selection_box.serialize(os, protocol_version);
307 writeU8(os, legacy_facedir_simple);
308 writeU8(os, legacy_wallmounted);
309 serializeSimpleSoundSpec(sound_footstep, os);
310 serializeSimpleSoundSpec(sound_dig, os);
311 serializeSimpleSoundSpec(sound_dug, os);
312 writeU8(os, rightclickable);
313 writeU8(os, drowning);
314 writeU8(os, leveled);
315 writeU8(os, liquid_range);
317 // Stuff below should be moved to correct place in a version that otherwise changes
318 // the protocol version
319 os<<serializeString(mesh);
320 collision_box.serialize(os, protocol_version);
323 void ContentFeatures::deSerialize(std::istream &is)
325 int version = readU8(is);
327 deSerializeOld(is, version);
331 name = deSerializeString(is);
333 u32 groups_size = readU16(is);
334 for(u32 i = 0; i < groups_size; i++){
335 std::string name = deSerializeString(is);
336 int value = readS16(is);
337 groups[name] = value;
339 drawtype = (enum NodeDrawType)readU8(is);
340 visual_scale = readF1000(is);
342 throw SerializationError("unsupported tile count");
343 for(u32 i = 0; i < 6; i++)
344 tiledef[i].deSerialize(is);
345 if(readU8(is) != CF_SPECIAL_COUNT)
346 throw SerializationError("unsupported CF_SPECIAL_COUNT");
347 for(u32 i = 0; i < CF_SPECIAL_COUNT; i++)
348 tiledef_special[i].deSerialize(is);
350 post_effect_color.setAlpha(readU8(is));
351 post_effect_color.setRed(readU8(is));
352 post_effect_color.setGreen(readU8(is));
353 post_effect_color.setBlue(readU8(is));
354 param_type = (enum ContentParamType)readU8(is);
355 param_type_2 = (enum ContentParamType2)readU8(is);
356 is_ground_content = readU8(is);
357 light_propagates = readU8(is);
358 sunlight_propagates = readU8(is);
359 walkable = readU8(is);
360 pointable = readU8(is);
361 diggable = readU8(is);
362 climbable = readU8(is);
363 buildable_to = readU8(is);
364 deSerializeString(is); // legacy: used to be metadata_name
365 liquid_type = (enum LiquidType)readU8(is);
366 liquid_alternative_flowing = deSerializeString(is);
367 liquid_alternative_source = deSerializeString(is);
368 liquid_viscosity = readU8(is);
369 liquid_renewable = readU8(is);
370 light_source = readU8(is);
371 damage_per_second = readU32(is);
372 node_box.deSerialize(is);
373 selection_box.deSerialize(is);
374 legacy_facedir_simple = readU8(is);
375 legacy_wallmounted = readU8(is);
376 deSerializeSimpleSoundSpec(sound_footstep, is);
377 deSerializeSimpleSoundSpec(sound_dig, is);
378 deSerializeSimpleSoundSpec(sound_dug, is);
379 rightclickable = readU8(is);
380 drowning = readU8(is);
381 leveled = readU8(is);
382 liquid_range = readU8(is);
384 // If you add anything here, insert it primarily inside the try-catch
385 // block to not need to increase the version.
387 // Stuff below should be moved to correct place in a version that
388 // otherwise changes the protocol version
389 mesh = deSerializeString(is);
390 collision_box.deSerialize(is);
391 }catch(SerializationError &e) {};
398 class CNodeDefManager: public IWritableNodeDefManager {
401 virtual ~CNodeDefManager();
403 virtual IWritableNodeDefManager *clone();
404 inline virtual const ContentFeatures& get(content_t c) const;
405 inline virtual const ContentFeatures& get(const MapNode &n) const;
406 virtual bool getId(const std::string &name, content_t &result) const;
407 virtual content_t getId(const std::string &name) const;
408 virtual void getIds(const std::string &name, std::set<content_t> &result) const;
409 virtual const ContentFeatures& get(const std::string &name) const;
410 content_t allocateId();
411 virtual content_t set(const std::string &name, const ContentFeatures &def);
412 virtual content_t allocateDummy(const std::string &name);
413 virtual void updateAliases(IItemDefManager *idef);
414 virtual void applyTextureOverrides(const std::string &override_filepath);
415 virtual void updateTextures(IGameDef *gamedef,
416 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
417 void *progress_cbk_args);
418 void serialize(std::ostream &os, u16 protocol_version) const;
419 void deSerialize(std::istream &is);
421 inline virtual bool getNodeRegistrationStatus() const;
422 inline virtual void setNodeRegistrationStatus(bool completed);
424 virtual void pendNodeResolve(NodeResolver *nr);
425 virtual bool cancelNodeResolveCallback(NodeResolver *nr);
426 virtual void runNodeResolveCallbacks();
427 virtual void resetNodeResolveState();
430 void addNameIdMapping(content_t i, std::string name);
432 void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
433 u32 shader_id, bool use_normal_texture, bool backface_culling,
434 u8 alpha, u8 material_type);
437 // Features indexed by id
438 std::vector<ContentFeatures> m_content_features;
440 // A mapping for fast converting back and forth between names and ids
441 NameIdMapping m_name_id_mapping;
443 // Like m_name_id_mapping, but only from names to ids, and includes
444 // item aliases too. Updated by updateAliases()
445 // Note: Not serialized.
447 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
449 // A mapping from groups to a list of content_ts (and their levels)
450 // that belong to it. Necessary for a direct lookup in getIds().
451 // Note: Not serialized.
452 std::map<std::string, GroupItems> m_group_to_items;
454 // Next possibly free id
457 // NodeResolvers to callback once node registration has ended
458 std::vector<NodeResolver *> m_pending_resolve_callbacks;
460 // True when all nodes have been registered
461 bool m_node_registration_complete;
465 CNodeDefManager::CNodeDefManager()
471 CNodeDefManager::~CNodeDefManager()
474 for (u32 i = 0; i < m_content_features.size(); i++) {
475 ContentFeatures *f = &m_content_features[i];
476 for (u32 j = 0; j < 24; j++) {
478 f->mesh_ptr[j]->drop();
485 void CNodeDefManager::clear()
487 m_content_features.clear();
488 m_name_id_mapping.clear();
489 m_name_id_mapping_with_aliases.clear();
490 m_group_to_items.clear();
493 resetNodeResolveState();
495 u32 initial_length = 0;
496 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
497 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
498 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
499 m_content_features.resize(initial_length);
501 // Set CONTENT_UNKNOWN
505 // Insert directly into containers
506 content_t c = CONTENT_UNKNOWN;
507 m_content_features[c] = f;
508 addNameIdMapping(c, f.name);
515 f.drawtype = NDT_AIRLIKE;
516 f.param_type = CPT_LIGHT;
517 f.light_propagates = true;
518 f.sunlight_propagates = true;
522 f.buildable_to = true;
523 f.is_ground_content = true;
524 // Insert directly into containers
525 content_t c = CONTENT_AIR;
526 m_content_features[c] = f;
527 addNameIdMapping(c, f.name);
530 // Set CONTENT_IGNORE
534 f.drawtype = NDT_AIRLIKE;
535 f.param_type = CPT_NONE;
536 f.light_propagates = false;
537 f.sunlight_propagates = false;
541 f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
542 f.is_ground_content = true;
543 // Insert directly into containers
544 content_t c = CONTENT_IGNORE;
545 m_content_features[c] = f;
546 addNameIdMapping(c, f.name);
551 IWritableNodeDefManager *CNodeDefManager::clone()
553 CNodeDefManager *mgr = new CNodeDefManager();
559 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
561 return c < m_content_features.size()
562 ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
566 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
568 return get(n.getContent());
572 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
574 std::map<std::string, content_t>::const_iterator
575 i = m_name_id_mapping_with_aliases.find(name);
576 if(i == m_name_id_mapping_with_aliases.end())
583 content_t CNodeDefManager::getId(const std::string &name) const
585 content_t id = CONTENT_IGNORE;
591 void CNodeDefManager::getIds(const std::string &name,
592 std::set<content_t> &result) const
594 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
595 if (name.substr(0,6) != "group:") {
596 content_t id = CONTENT_IGNORE;
601 std::string group = name.substr(6);
603 std::map<std::string, GroupItems>::const_iterator
604 i = m_group_to_items.find(group);
605 if (i == m_group_to_items.end())
608 const GroupItems &items = i->second;
609 for (GroupItems::const_iterator j = items.begin();
610 j != items.end(); ++j) {
611 if ((*j).second != 0)
612 result.insert((*j).first);
614 //printf("getIds: %dus\n", t.stop());
618 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
620 content_t id = CONTENT_UNKNOWN;
626 // returns CONTENT_IGNORE if no free ID found
627 content_t CNodeDefManager::allocateId()
629 for (content_t id = m_next_id;
630 id >= m_next_id; // overflow?
632 while (id >= m_content_features.size()) {
633 m_content_features.push_back(ContentFeatures());
635 const ContentFeatures &f = m_content_features[id];
641 // If we arrive here, an overflow occurred in id.
642 // That means no ID was found
643 return CONTENT_IGNORE;
647 // IWritableNodeDefManager
648 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
652 assert(name == def.name);
654 // Don't allow redefining ignore (but allow air and unknown)
655 if (name == "ignore") {
656 warningstream << "NodeDefManager: Ignoring "
657 "CONTENT_IGNORE redefinition"<<std::endl;
658 return CONTENT_IGNORE;
661 content_t id = CONTENT_IGNORE;
662 if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
665 if (id == CONTENT_IGNORE) {
666 warningstream << "NodeDefManager: Absolute "
667 "limit reached" << std::endl;
668 return CONTENT_IGNORE;
670 assert(id != CONTENT_IGNORE);
671 addNameIdMapping(id, name);
673 m_content_features[id] = def;
674 verbosestream << "NodeDefManager: registering content id \"" << id
675 << "\": name=\"" << def.name << "\""<<std::endl;
677 // Add this content to the list of all groups it belongs to
678 // FIXME: This should remove a node from groups it no longer
679 // belongs to when a node is re-registered
680 for (ItemGroupList::const_iterator i = def.groups.begin();
681 i != def.groups.end(); ++i) {
682 std::string group_name = i->first;
684 std::map<std::string, GroupItems>::iterator
685 j = m_group_to_items.find(group_name);
686 if (j == m_group_to_items.end()) {
687 m_group_to_items[group_name].push_back(
688 std::make_pair(id, i->second));
690 GroupItems &items = j->second;
691 items.push_back(std::make_pair(id, i->second));
698 content_t CNodeDefManager::allocateDummy(const std::string &name)
700 assert(name != ""); // Pre-condition
707 void CNodeDefManager::updateAliases(IItemDefManager *idef)
709 std::set<std::string> all = idef->getAll();
710 m_name_id_mapping_with_aliases.clear();
711 for (std::set<std::string>::iterator
712 i = all.begin(); i != all.end(); ++i) {
713 std::string name = *i;
714 std::string convert_to = idef->getAlias(name);
716 if (m_name_id_mapping.getId(convert_to, id)) {
717 m_name_id_mapping_with_aliases.insert(
718 std::make_pair(name, id));
723 void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath)
725 infostream << "CNodeDefManager::applyTextureOverrides(): Applying "
726 "overrides to textures from " << override_filepath << std::endl;
728 std::ifstream infile(override_filepath.c_str());
731 while (std::getline(infile, line)) {
733 if (trim(line) == "")
735 std::vector<std::string> splitted = str_split(line, ' ');
736 if (splitted.size() != 3) {
737 errorstream << override_filepath
738 << ":" << line_c << " Could not apply texture override \""
739 << line << "\": Syntax error" << std::endl;
744 if (!getId(splitted[0], id)) {
745 errorstream << override_filepath
746 << ":" << line_c << " Could not apply texture override \""
747 << line << "\": Unknown node \""
748 << splitted[0] << "\"" << std::endl;
752 ContentFeatures &nodedef = m_content_features[id];
754 if (splitted[1] == "top")
755 nodedef.tiledef[0].name = splitted[2];
756 else if (splitted[1] == "bottom")
757 nodedef.tiledef[1].name = splitted[2];
758 else if (splitted[1] == "right")
759 nodedef.tiledef[2].name = splitted[2];
760 else if (splitted[1] == "left")
761 nodedef.tiledef[3].name = splitted[2];
762 else if (splitted[1] == "back")
763 nodedef.tiledef[4].name = splitted[2];
764 else if (splitted[1] == "front")
765 nodedef.tiledef[5].name = splitted[2];
766 else if (splitted[1] == "all" || splitted[1] == "*")
767 for (int i = 0; i < 6; i++)
768 nodedef.tiledef[i].name = splitted[2];
769 else if (splitted[1] == "sides")
770 for (int i = 2; i < 6; i++)
771 nodedef.tiledef[i].name = splitted[2];
773 errorstream << override_filepath
774 << ":" << line_c << " Could not apply texture override \""
775 << line << "\": Unknown node side \""
776 << splitted[1] << "\"" << std::endl;
782 void CNodeDefManager::updateTextures(IGameDef *gamedef,
783 void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
784 void *progress_callback_args)
787 infostream << "CNodeDefManager::updateTextures(): Updating "
788 "textures in node definitions" << std::endl;
789 ITextureSource *tsrc = gamedef->tsrc();
790 IShaderSource *shdsrc = gamedef->getShaderSource();
791 scene::ISceneManager* smgr = gamedef->getSceneManager();
792 scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
794 bool new_style_water = g_settings->getBool("new_style_water");
795 bool connected_glass = g_settings->getBool("connected_glass");
796 bool opaque_water = g_settings->getBool("opaque_water");
797 bool enable_shaders = g_settings->getBool("enable_shaders");
798 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
799 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
800 bool enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
801 bool enable_minimap = g_settings->getBool("enable_minimap");
802 std::string leaves_style = g_settings->get("leaves_style");
804 bool use_normal_texture = enable_shaders &&
805 (enable_bumpmapping || enable_parallax_occlusion);
807 u32 size = m_content_features.size();
809 for (u32 i = 0; i < size; i++) {
810 ContentFeatures *f = &m_content_features[i];
812 // minimap pixel color - the average color of a texture
813 if (enable_minimap && f->tiledef[0].name != "")
814 f->minimap_color = tsrc->getTextureAverageColor(f->tiledef[0].name);
816 // Figure out the actual tiles to use
818 for (u32 j = 0; j < 6; j++) {
819 tiledef[j] = f->tiledef[j];
820 if (tiledef[j].name == "")
821 tiledef[j].name = "unknown_node.png";
824 bool is_liquid = false;
825 bool is_water_surface = false;
827 u8 material_type = (f->alpha == 255) ?
828 TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
830 switch (f->drawtype) {
839 assert(f->liquid_type == LIQUID_SOURCE);
842 if (new_style_water){
846 f->backface_culling = false;
850 case NDT_FLOWINGLIQUID:
851 assert(f->liquid_type == LIQUID_FLOWING);
859 f->visual_solidness = 1;
861 case NDT_GLASSLIKE_FRAMED:
863 f->visual_solidness = 1;
865 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
867 f->visual_solidness = 1;
868 f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
872 f->visual_solidness = 1;
874 case NDT_ALLFACES_OPTIONAL:
875 if (leaves_style == "fancy") {
876 f->drawtype = NDT_ALLFACES;
878 f->visual_solidness = 1;
879 } else if (leaves_style == "simple") {
880 for (u32 j = 0; j < 6; j++) {
881 if (f->tiledef_special[j].name != "")
882 tiledef[j].name = f->tiledef_special[j].name;
884 f->drawtype = NDT_GLASSLIKE;
886 f->visual_solidness = 1;
888 f->drawtype = NDT_NORMAL;
890 for (u32 i = 0; i < 6; i++)
891 tiledef[i].name += std::string("^[noalpha");
894 material_type = TILE_MATERIAL_WAVING_LEAVES;
898 f->backface_culling = false;
900 material_type = TILE_MATERIAL_WAVING_PLANTS;
903 f->backface_culling = false;
908 f->backface_culling = false;
920 material_type = (f->alpha == 255) ?
921 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
922 if (f->name == "default:water_source")
923 is_water_surface = true;
927 for (u16 j = 0; j < 6; j++) {
928 tile_shader[j] = shdsrc->getShader("nodes_shader",
929 material_type, f->drawtype);
932 if (is_water_surface) {
933 tile_shader[0] = shdsrc->getShader("water_surface_shader",
934 material_type, f->drawtype);
937 // Tiles (fill in f->tiles[])
938 for (u16 j = 0; j < 6; j++) {
939 fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
940 use_normal_texture, f->backface_culling, f->alpha, material_type);
943 // Special tiles (fill in f->special_tiles[])
944 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
945 fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
946 tile_shader[j], use_normal_texture,
947 f->tiledef_special[j].backface_culling, f->alpha, material_type);
950 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
952 // Read the mesh and apply scale
953 f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
955 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
956 scaleMesh(f->mesh_ptr[0], scale);
957 recalculateBoundingBox(f->mesh_ptr[0]);
958 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
960 } else if ((f->drawtype == NDT_NODEBOX) &&
961 ((f->node_box.type == NODEBOX_REGULAR) ||
962 (f->node_box.type == NODEBOX_FIXED)) &&
963 (!f->node_box.fixed.empty())) {
964 //Convert regular nodebox nodes to meshnodes
965 //Change the drawtype and apply scale
966 f->drawtype = NDT_MESH;
967 f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
968 v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
969 scaleMesh(f->mesh_ptr[0], scale);
970 recalculateBoundingBox(f->mesh_ptr[0]);
971 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
974 //Cache 6dfacedir and wallmounted rotated clones of meshes
975 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
976 for (u16 j = 1; j < 24; j++) {
977 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
978 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
979 recalculateBoundingBox(f->mesh_ptr[j]);
980 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
982 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
983 static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
984 for (u16 j = 1; j < 6; j++) {
985 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
986 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
987 recalculateBoundingBox(f->mesh_ptr[j]);
988 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
990 rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
991 recalculateBoundingBox(f->mesh_ptr[0]);
992 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
995 progress_callback(progress_callback_args, i, size);
1002 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
1003 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
1004 bool backface_culling, u8 alpha, u8 material_type)
1006 tile->shader_id = shader_id;
1007 tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
1008 tile->alpha = alpha;
1009 tile->material_type = material_type;
1011 // Normal texture and shader flags texture
1012 if (use_normal_texture) {
1013 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
1015 tile->flags_texture = tsrc->getShaderFlagsTexture(tile->normal_texture ? true : false);
1018 tile->material_flags = 0;
1019 if (backface_culling)
1020 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
1021 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
1022 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1023 if (tiledef->tileable_horizontal)
1024 tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
1025 if (tiledef->tileable_vertical)
1026 tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
1028 // Animation parameters
1029 int frame_count = 1;
1030 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
1031 // Get texture size to determine frame count by aspect ratio
1032 v2u32 size = tile->texture->getOriginalSize();
1033 int frame_height = (float)size.X /
1034 (float)tiledef->animation.aspect_w *
1035 (float)tiledef->animation.aspect_h;
1036 frame_count = size.Y / frame_height;
1037 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
1038 tile->animation_frame_count = frame_count;
1039 tile->animation_frame_length_ms = frame_length_ms;
1042 if (frame_count == 1) {
1043 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1045 std::ostringstream os(std::ios::binary);
1046 tile->frames.resize(frame_count);
1048 for (int i = 0; i < frame_count; i++) {
1053 os << tiledef->name << "^[verticalframe:"
1054 << frame_count << ":" << i;
1056 frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
1057 if (tile->normal_texture)
1058 frame.normal_texture = tsrc->getNormalTexture(os.str());
1059 frame.flags_texture = tile->flags_texture;
1060 tile->frames[i] = frame;
1067 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
1069 writeU8(os, 1); // version
1071 std::ostringstream os2(std::ios::binary);
1072 for (u32 i = 0; i < m_content_features.size(); i++) {
1073 if (i == CONTENT_IGNORE || i == CONTENT_AIR
1074 || i == CONTENT_UNKNOWN)
1076 const ContentFeatures *f = &m_content_features[i];
1080 // Wrap it in a string to allow different lengths without
1081 // strict version incompatibilities
1082 std::ostringstream wrapper_os(std::ios::binary);
1083 f->serialize(wrapper_os, protocol_version);
1084 os2<<serializeString(wrapper_os.str());
1086 // must not overflow
1087 u16 next = count + 1;
1088 FATAL_ERROR_IF(next < count, "Overflow");
1091 writeU16(os, count);
1092 os << serializeLongString(os2.str());
1096 void CNodeDefManager::deSerialize(std::istream &is)
1099 int version = readU8(is);
1101 throw SerializationError("unsupported NodeDefinitionManager version");
1102 u16 count = readU16(is);
1103 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1105 for (u16 n = 0; n < count; n++) {
1106 u16 i = readU16(is2);
1108 // Read it from the string wrapper
1109 std::string wrapper = deSerializeString(is2);
1110 std::istringstream wrapper_is(wrapper, std::ios::binary);
1111 f.deSerialize(wrapper_is);
1113 // Check error conditions
1114 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1115 warningstream << "NodeDefManager::deSerialize(): "
1116 "not changing builtin node " << i << std::endl;
1120 warningstream << "NodeDefManager::deSerialize(): "
1121 "received empty name" << std::endl;
1127 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1128 warningstream << "NodeDefManager::deSerialize(): "
1129 "already defined with different ID: " << f.name << std::endl;
1133 // All is ok, add node definition with the requested ID
1134 if (i >= m_content_features.size())
1135 m_content_features.resize((u32)(i) + 1);
1136 m_content_features[i] = f;
1137 addNameIdMapping(i, f.name);
1138 verbosestream << "deserialized " << f.name << std::endl;
1143 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1145 m_name_id_mapping.set(i, name);
1146 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1150 IWritableNodeDefManager *createNodeDefManager()
1152 return new CNodeDefManager();
1156 //// Serialization of old ContentFeatures formats
1157 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
1159 if (protocol_version == 13)
1161 writeU8(os, 5); // version
1162 os<<serializeString(name);
1163 writeU16(os, groups.size());
1164 for (ItemGroupList::const_iterator
1165 i = groups.begin(); i != groups.end(); ++i) {
1166 os<<serializeString(i->first);
1167 writeS16(os, i->second);
1169 writeU8(os, drawtype);
1170 writeF1000(os, visual_scale);
1172 for (u32 i = 0; i < 6; i++)
1173 tiledef[i].serialize(os, protocol_version);
1174 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1176 for (u32 i = 0; i < 2; i++)
1177 tiledef_special[i].serialize(os, protocol_version);
1179 writeU8(os, post_effect_color.getAlpha());
1180 writeU8(os, post_effect_color.getRed());
1181 writeU8(os, post_effect_color.getGreen());
1182 writeU8(os, post_effect_color.getBlue());
1183 writeU8(os, param_type);
1184 writeU8(os, param_type_2);
1185 writeU8(os, is_ground_content);
1186 writeU8(os, light_propagates);
1187 writeU8(os, sunlight_propagates);
1188 writeU8(os, walkable);
1189 writeU8(os, pointable);
1190 writeU8(os, diggable);
1191 writeU8(os, climbable);
1192 writeU8(os, buildable_to);
1193 os<<serializeString(""); // legacy: used to be metadata_name
1194 writeU8(os, liquid_type);
1195 os<<serializeString(liquid_alternative_flowing);
1196 os<<serializeString(liquid_alternative_source);
1197 writeU8(os, liquid_viscosity);
1198 writeU8(os, light_source);
1199 writeU32(os, damage_per_second);
1200 node_box.serialize(os, protocol_version);
1201 selection_box.serialize(os, protocol_version);
1202 writeU8(os, legacy_facedir_simple);
1203 writeU8(os, legacy_wallmounted);
1204 serializeSimpleSoundSpec(sound_footstep, os);
1205 serializeSimpleSoundSpec(sound_dig, os);
1206 serializeSimpleSoundSpec(sound_dug, os);
1208 else if (protocol_version > 13 && protocol_version < 24) {
1209 writeU8(os, 6); // version
1210 os<<serializeString(name);
1211 writeU16(os, groups.size());
1212 for (ItemGroupList::const_iterator
1213 i = groups.begin(); i != groups.end(); ++i) {
1214 os<<serializeString(i->first);
1215 writeS16(os, i->second);
1217 writeU8(os, drawtype);
1218 writeF1000(os, visual_scale);
1220 for (u32 i = 0; i < 6; i++)
1221 tiledef[i].serialize(os, protocol_version);
1222 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1224 for (u32 i = 0; i < 2; i++)
1225 tiledef_special[i].serialize(os, protocol_version);
1227 writeU8(os, post_effect_color.getAlpha());
1228 writeU8(os, post_effect_color.getRed());
1229 writeU8(os, post_effect_color.getGreen());
1230 writeU8(os, post_effect_color.getBlue());
1231 writeU8(os, param_type);
1232 writeU8(os, param_type_2);
1233 writeU8(os, is_ground_content);
1234 writeU8(os, light_propagates);
1235 writeU8(os, sunlight_propagates);
1236 writeU8(os, walkable);
1237 writeU8(os, pointable);
1238 writeU8(os, diggable);
1239 writeU8(os, climbable);
1240 writeU8(os, buildable_to);
1241 os<<serializeString(""); // legacy: used to be metadata_name
1242 writeU8(os, liquid_type);
1243 os<<serializeString(liquid_alternative_flowing);
1244 os<<serializeString(liquid_alternative_source);
1245 writeU8(os, liquid_viscosity);
1246 writeU8(os, liquid_renewable);
1247 writeU8(os, light_source);
1248 writeU32(os, damage_per_second);
1249 node_box.serialize(os, protocol_version);
1250 selection_box.serialize(os, protocol_version);
1251 writeU8(os, legacy_facedir_simple);
1252 writeU8(os, legacy_wallmounted);
1253 serializeSimpleSoundSpec(sound_footstep, os);
1254 serializeSimpleSoundSpec(sound_dig, os);
1255 serializeSimpleSoundSpec(sound_dug, os);
1256 writeU8(os, rightclickable);
1257 writeU8(os, drowning);
1258 writeU8(os, leveled);
1259 writeU8(os, liquid_range);
1261 throw SerializationError("ContentFeatures::serialize(): "
1262 "Unsupported version requested");
1266 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1268 if (version == 5) // In PROTOCOL_VERSION 13
1270 name = deSerializeString(is);
1272 u32 groups_size = readU16(is);
1273 for(u32 i=0; i<groups_size; i++){
1274 std::string name = deSerializeString(is);
1275 int value = readS16(is);
1276 groups[name] = value;
1278 drawtype = (enum NodeDrawType)readU8(is);
1279 visual_scale = readF1000(is);
1280 if (readU8(is) != 6)
1281 throw SerializationError("unsupported tile count");
1282 for (u32 i = 0; i < 6; i++)
1283 tiledef[i].deSerialize(is);
1284 if (readU8(is) != CF_SPECIAL_COUNT)
1285 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1286 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1287 tiledef_special[i].deSerialize(is);
1289 post_effect_color.setAlpha(readU8(is));
1290 post_effect_color.setRed(readU8(is));
1291 post_effect_color.setGreen(readU8(is));
1292 post_effect_color.setBlue(readU8(is));
1293 param_type = (enum ContentParamType)readU8(is);
1294 param_type_2 = (enum ContentParamType2)readU8(is);
1295 is_ground_content = readU8(is);
1296 light_propagates = readU8(is);
1297 sunlight_propagates = readU8(is);
1298 walkable = readU8(is);
1299 pointable = readU8(is);
1300 diggable = readU8(is);
1301 climbable = readU8(is);
1302 buildable_to = readU8(is);
1303 deSerializeString(is); // legacy: used to be metadata_name
1304 liquid_type = (enum LiquidType)readU8(is);
1305 liquid_alternative_flowing = deSerializeString(is);
1306 liquid_alternative_source = deSerializeString(is);
1307 liquid_viscosity = readU8(is);
1308 light_source = readU8(is);
1309 damage_per_second = readU32(is);
1310 node_box.deSerialize(is);
1311 selection_box.deSerialize(is);
1312 legacy_facedir_simple = readU8(is);
1313 legacy_wallmounted = readU8(is);
1314 deSerializeSimpleSoundSpec(sound_footstep, is);
1315 deSerializeSimpleSoundSpec(sound_dig, is);
1316 deSerializeSimpleSoundSpec(sound_dug, is);
1317 } else if (version == 6) {
1318 name = deSerializeString(is);
1320 u32 groups_size = readU16(is);
1321 for (u32 i = 0; i < groups_size; i++) {
1322 std::string name = deSerializeString(is);
1323 int value = readS16(is);
1324 groups[name] = value;
1326 drawtype = (enum NodeDrawType)readU8(is);
1327 visual_scale = readF1000(is);
1328 if (readU8(is) != 6)
1329 throw SerializationError("unsupported tile count");
1330 for (u32 i = 0; i < 6; i++)
1331 tiledef[i].deSerialize(is);
1332 // CF_SPECIAL_COUNT in version 6 = 2
1333 if (readU8(is) != 2)
1334 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1335 for (u32 i = 0; i < 2; i++)
1336 tiledef_special[i].deSerialize(is);
1338 post_effect_color.setAlpha(readU8(is));
1339 post_effect_color.setRed(readU8(is));
1340 post_effect_color.setGreen(readU8(is));
1341 post_effect_color.setBlue(readU8(is));
1342 param_type = (enum ContentParamType)readU8(is);
1343 param_type_2 = (enum ContentParamType2)readU8(is);
1344 is_ground_content = readU8(is);
1345 light_propagates = readU8(is);
1346 sunlight_propagates = readU8(is);
1347 walkable = readU8(is);
1348 pointable = readU8(is);
1349 diggable = readU8(is);
1350 climbable = readU8(is);
1351 buildable_to = readU8(is);
1352 deSerializeString(is); // legacy: used to be metadata_name
1353 liquid_type = (enum LiquidType)readU8(is);
1354 liquid_alternative_flowing = deSerializeString(is);
1355 liquid_alternative_source = deSerializeString(is);
1356 liquid_viscosity = readU8(is);
1357 liquid_renewable = readU8(is);
1358 light_source = readU8(is);
1359 damage_per_second = readU32(is);
1360 node_box.deSerialize(is);
1361 selection_box.deSerialize(is);
1362 legacy_facedir_simple = readU8(is);
1363 legacy_wallmounted = readU8(is);
1364 deSerializeSimpleSoundSpec(sound_footstep, is);
1365 deSerializeSimpleSoundSpec(sound_dig, is);
1366 deSerializeSimpleSoundSpec(sound_dug, is);
1367 rightclickable = readU8(is);
1368 drowning = readU8(is);
1369 leveled = readU8(is);
1370 liquid_range = readU8(is);
1372 throw SerializationError("unsupported ContentFeatures version");
1377 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1379 return m_node_registration_complete;
1383 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1385 m_node_registration_complete = completed;
1389 void CNodeDefManager::pendNodeResolve(NodeResolver *nr)
1392 if (m_node_registration_complete)
1393 nr->nodeResolveInternal();
1395 m_pending_resolve_callbacks.push_back(nr);
1399 bool CNodeDefManager::cancelNodeResolveCallback(NodeResolver *nr)
1401 size_t len = m_pending_resolve_callbacks.size();
1402 for (size_t i = 0; i != len; i++) {
1403 if (nr != m_pending_resolve_callbacks[i])
1407 m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
1408 m_pending_resolve_callbacks.resize(len);
1416 void CNodeDefManager::runNodeResolveCallbacks()
1418 for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
1419 NodeResolver *nr = m_pending_resolve_callbacks[i];
1420 nr->nodeResolveInternal();
1423 m_pending_resolve_callbacks.clear();
1427 void CNodeDefManager::resetNodeResolveState()
1429 m_node_registration_complete = false;
1430 m_pending_resolve_callbacks.clear();
1438 NodeResolver::NodeResolver()
1441 m_nodenames_idx = 0;
1442 m_nnlistsizes_idx = 0;
1443 m_resolve_done = false;
1445 m_nodenames.reserve(16);
1446 m_nnlistsizes.reserve(4);
1450 NodeResolver::~NodeResolver()
1452 if (!m_resolve_done && m_ndef)
1453 m_ndef->cancelNodeResolveCallback(this);
1457 void NodeResolver::nodeResolveInternal()
1459 m_nodenames_idx = 0;
1460 m_nnlistsizes_idx = 0;
1463 m_resolve_done = true;
1465 m_nodenames.clear();
1466 m_nnlistsizes.clear();
1470 bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
1471 const std::string &node_alt, content_t c_fallback)
1473 if (m_nodenames_idx == m_nodenames.size()) {
1474 *result_out = c_fallback;
1475 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1480 std::string name = m_nodenames[m_nodenames_idx++];
1482 bool success = m_ndef->getId(name, c);
1483 if (!success && node_alt != "") {
1485 success = m_ndef->getId(name, c);
1489 errorstream << "NodeResolver: failed to resolve node name '" << name
1490 << "'." << std::endl;
1499 bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
1500 bool all_required, content_t c_fallback)
1502 bool success = true;
1504 if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
1505 errorstream << "NodeResolver: no more node lists" << std::endl;
1509 size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
1512 if (m_nodenames_idx == m_nodenames.size()) {
1513 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1518 std::string &name = m_nodenames[m_nodenames_idx++];
1520 if (name.substr(0,6) != "group:") {
1521 if (m_ndef->getId(name, c)) {
1522 result_out->push_back(c);
1523 } else if (all_required) {
1524 errorstream << "NodeResolver: failed to resolve node name '"
1525 << name << "'." << std::endl;
1526 result_out->push_back(c_fallback);
1530 std::set<content_t> cids;
1531 std::set<content_t>::iterator it;
1532 m_ndef->getIds(name, cids);
1533 for (it = cids.begin(); it != cids.end(); ++it)
1534 result_out->push_back(*it);