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 infostream << "NodeDefManager: WARNING: 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 infostream << "NodeDefManager: WARNING: 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(
1016 tile->normal_texture ? true : false,
1017 tiledef->tileable_vertical, tiledef->tileable_horizontal);
1020 tile->material_flags = 0;
1021 if (backface_culling)
1022 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
1023 if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
1024 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1025 if (tiledef->tileable_horizontal)
1026 tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
1027 if (tiledef->tileable_vertical)
1028 tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
1030 // Animation parameters
1031 int frame_count = 1;
1032 if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
1033 // Get texture size to determine frame count by aspect ratio
1034 v2u32 size = tile->texture->getOriginalSize();
1035 int frame_height = (float)size.X /
1036 (float)tiledef->animation.aspect_w *
1037 (float)tiledef->animation.aspect_h;
1038 frame_count = size.Y / frame_height;
1039 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
1040 tile->animation_frame_count = frame_count;
1041 tile->animation_frame_length_ms = frame_length_ms;
1044 if (frame_count == 1) {
1045 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1047 std::ostringstream os(std::ios::binary);
1048 tile->frames.resize(frame_count);
1050 for (int i = 0; i < frame_count; i++) {
1055 os << tiledef->name << "^[verticalframe:"
1056 << frame_count << ":" << i;
1058 frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
1059 if (tile->normal_texture)
1060 frame.normal_texture = tsrc->getNormalTexture(os.str());
1061 frame.flags_texture = tile->flags_texture;
1062 tile->frames[i] = frame;
1069 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
1071 writeU8(os, 1); // version
1073 std::ostringstream os2(std::ios::binary);
1074 for (u32 i = 0; i < m_content_features.size(); i++) {
1075 if (i == CONTENT_IGNORE || i == CONTENT_AIR
1076 || i == CONTENT_UNKNOWN)
1078 const ContentFeatures *f = &m_content_features[i];
1082 // Wrap it in a string to allow different lengths without
1083 // strict version incompatibilities
1084 std::ostringstream wrapper_os(std::ios::binary);
1085 f->serialize(wrapper_os, protocol_version);
1086 os2<<serializeString(wrapper_os.str());
1088 // must not overflow
1089 u16 next = count + 1;
1090 FATAL_ERROR_IF(next < count, "Overflow");
1093 writeU16(os, count);
1094 os << serializeLongString(os2.str());
1098 void CNodeDefManager::deSerialize(std::istream &is)
1101 int version = readU8(is);
1103 throw SerializationError("unsupported NodeDefinitionManager version");
1104 u16 count = readU16(is);
1105 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1107 for (u16 n = 0; n < count; n++) {
1108 u16 i = readU16(is2);
1110 // Read it from the string wrapper
1111 std::string wrapper = deSerializeString(is2);
1112 std::istringstream wrapper_is(wrapper, std::ios::binary);
1113 f.deSerialize(wrapper_is);
1115 // Check error conditions
1116 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1117 infostream << "NodeDefManager::deSerialize(): WARNING: "
1118 "not changing builtin node " << i << std::endl;
1122 infostream << "NodeDefManager::deSerialize(): WARNING: "
1123 "received empty name" << std::endl;
1129 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1130 infostream << "NodeDefManager::deSerialize(): WARNING: "
1131 "already defined with different ID: " << f.name << std::endl;
1135 // All is ok, add node definition with the requested ID
1136 if (i >= m_content_features.size())
1137 m_content_features.resize((u32)(i) + 1);
1138 m_content_features[i] = f;
1139 addNameIdMapping(i, f.name);
1140 verbosestream << "deserialized " << f.name << std::endl;
1145 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1147 m_name_id_mapping.set(i, name);
1148 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1152 IWritableNodeDefManager *createNodeDefManager()
1154 return new CNodeDefManager();
1158 //// Serialization of old ContentFeatures formats
1159 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
1161 if (protocol_version == 13)
1163 writeU8(os, 5); // version
1164 os<<serializeString(name);
1165 writeU16(os, groups.size());
1166 for (ItemGroupList::const_iterator
1167 i = groups.begin(); i != groups.end(); i++) {
1168 os<<serializeString(i->first);
1169 writeS16(os, i->second);
1171 writeU8(os, drawtype);
1172 writeF1000(os, visual_scale);
1174 for (u32 i = 0; i < 6; i++)
1175 tiledef[i].serialize(os, protocol_version);
1176 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1178 for (u32 i = 0; i < 2; i++)
1179 tiledef_special[i].serialize(os, protocol_version);
1181 writeU8(os, post_effect_color.getAlpha());
1182 writeU8(os, post_effect_color.getRed());
1183 writeU8(os, post_effect_color.getGreen());
1184 writeU8(os, post_effect_color.getBlue());
1185 writeU8(os, param_type);
1186 writeU8(os, param_type_2);
1187 writeU8(os, is_ground_content);
1188 writeU8(os, light_propagates);
1189 writeU8(os, sunlight_propagates);
1190 writeU8(os, walkable);
1191 writeU8(os, pointable);
1192 writeU8(os, diggable);
1193 writeU8(os, climbable);
1194 writeU8(os, buildable_to);
1195 os<<serializeString(""); // legacy: used to be metadata_name
1196 writeU8(os, liquid_type);
1197 os<<serializeString(liquid_alternative_flowing);
1198 os<<serializeString(liquid_alternative_source);
1199 writeU8(os, liquid_viscosity);
1200 writeU8(os, light_source);
1201 writeU32(os, damage_per_second);
1202 node_box.serialize(os, protocol_version);
1203 selection_box.serialize(os, protocol_version);
1204 writeU8(os, legacy_facedir_simple);
1205 writeU8(os, legacy_wallmounted);
1206 serializeSimpleSoundSpec(sound_footstep, os);
1207 serializeSimpleSoundSpec(sound_dig, os);
1208 serializeSimpleSoundSpec(sound_dug, os);
1210 else if (protocol_version > 13 && protocol_version < 24) {
1211 writeU8(os, 6); // version
1212 os<<serializeString(name);
1213 writeU16(os, groups.size());
1214 for (ItemGroupList::const_iterator
1215 i = groups.begin(); i != groups.end(); i++) {
1216 os<<serializeString(i->first);
1217 writeS16(os, i->second);
1219 writeU8(os, drawtype);
1220 writeF1000(os, visual_scale);
1222 for (u32 i = 0; i < 6; i++)
1223 tiledef[i].serialize(os, protocol_version);
1224 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1226 for (u32 i = 0; i < 2; i++)
1227 tiledef_special[i].serialize(os, protocol_version);
1229 writeU8(os, post_effect_color.getAlpha());
1230 writeU8(os, post_effect_color.getRed());
1231 writeU8(os, post_effect_color.getGreen());
1232 writeU8(os, post_effect_color.getBlue());
1233 writeU8(os, param_type);
1234 writeU8(os, param_type_2);
1235 writeU8(os, is_ground_content);
1236 writeU8(os, light_propagates);
1237 writeU8(os, sunlight_propagates);
1238 writeU8(os, walkable);
1239 writeU8(os, pointable);
1240 writeU8(os, diggable);
1241 writeU8(os, climbable);
1242 writeU8(os, buildable_to);
1243 os<<serializeString(""); // legacy: used to be metadata_name
1244 writeU8(os, liquid_type);
1245 os<<serializeString(liquid_alternative_flowing);
1246 os<<serializeString(liquid_alternative_source);
1247 writeU8(os, liquid_viscosity);
1248 writeU8(os, liquid_renewable);
1249 writeU8(os, light_source);
1250 writeU32(os, damage_per_second);
1251 node_box.serialize(os, protocol_version);
1252 selection_box.serialize(os, protocol_version);
1253 writeU8(os, legacy_facedir_simple);
1254 writeU8(os, legacy_wallmounted);
1255 serializeSimpleSoundSpec(sound_footstep, os);
1256 serializeSimpleSoundSpec(sound_dig, os);
1257 serializeSimpleSoundSpec(sound_dug, os);
1258 writeU8(os, rightclickable);
1259 writeU8(os, drowning);
1260 writeU8(os, leveled);
1261 writeU8(os, liquid_range);
1263 throw SerializationError("ContentFeatures::serialize(): "
1264 "Unsupported version requested");
1268 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1270 if (version == 5) // In PROTOCOL_VERSION 13
1272 name = deSerializeString(is);
1274 u32 groups_size = readU16(is);
1275 for(u32 i=0; i<groups_size; i++){
1276 std::string name = deSerializeString(is);
1277 int value = readS16(is);
1278 groups[name] = value;
1280 drawtype = (enum NodeDrawType)readU8(is);
1281 visual_scale = readF1000(is);
1282 if (readU8(is) != 6)
1283 throw SerializationError("unsupported tile count");
1284 for (u32 i = 0; i < 6; i++)
1285 tiledef[i].deSerialize(is);
1286 if (readU8(is) != CF_SPECIAL_COUNT)
1287 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1288 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1289 tiledef_special[i].deSerialize(is);
1291 post_effect_color.setAlpha(readU8(is));
1292 post_effect_color.setRed(readU8(is));
1293 post_effect_color.setGreen(readU8(is));
1294 post_effect_color.setBlue(readU8(is));
1295 param_type = (enum ContentParamType)readU8(is);
1296 param_type_2 = (enum ContentParamType2)readU8(is);
1297 is_ground_content = readU8(is);
1298 light_propagates = readU8(is);
1299 sunlight_propagates = readU8(is);
1300 walkable = readU8(is);
1301 pointable = readU8(is);
1302 diggable = readU8(is);
1303 climbable = readU8(is);
1304 buildable_to = readU8(is);
1305 deSerializeString(is); // legacy: used to be metadata_name
1306 liquid_type = (enum LiquidType)readU8(is);
1307 liquid_alternative_flowing = deSerializeString(is);
1308 liquid_alternative_source = deSerializeString(is);
1309 liquid_viscosity = readU8(is);
1310 light_source = readU8(is);
1311 damage_per_second = readU32(is);
1312 node_box.deSerialize(is);
1313 selection_box.deSerialize(is);
1314 legacy_facedir_simple = readU8(is);
1315 legacy_wallmounted = readU8(is);
1316 deSerializeSimpleSoundSpec(sound_footstep, is);
1317 deSerializeSimpleSoundSpec(sound_dig, is);
1318 deSerializeSimpleSoundSpec(sound_dug, is);
1319 } else if (version == 6) {
1320 name = deSerializeString(is);
1322 u32 groups_size = readU16(is);
1323 for (u32 i = 0; i < groups_size; i++) {
1324 std::string name = deSerializeString(is);
1325 int value = readS16(is);
1326 groups[name] = value;
1328 drawtype = (enum NodeDrawType)readU8(is);
1329 visual_scale = readF1000(is);
1330 if (readU8(is) != 6)
1331 throw SerializationError("unsupported tile count");
1332 for (u32 i = 0; i < 6; i++)
1333 tiledef[i].deSerialize(is);
1334 // CF_SPECIAL_COUNT in version 6 = 2
1335 if (readU8(is) != 2)
1336 throw SerializationError("unsupported CF_SPECIAL_COUNT");
1337 for (u32 i = 0; i < 2; i++)
1338 tiledef_special[i].deSerialize(is);
1340 post_effect_color.setAlpha(readU8(is));
1341 post_effect_color.setRed(readU8(is));
1342 post_effect_color.setGreen(readU8(is));
1343 post_effect_color.setBlue(readU8(is));
1344 param_type = (enum ContentParamType)readU8(is);
1345 param_type_2 = (enum ContentParamType2)readU8(is);
1346 is_ground_content = readU8(is);
1347 light_propagates = readU8(is);
1348 sunlight_propagates = readU8(is);
1349 walkable = readU8(is);
1350 pointable = readU8(is);
1351 diggable = readU8(is);
1352 climbable = readU8(is);
1353 buildable_to = readU8(is);
1354 deSerializeString(is); // legacy: used to be metadata_name
1355 liquid_type = (enum LiquidType)readU8(is);
1356 liquid_alternative_flowing = deSerializeString(is);
1357 liquid_alternative_source = deSerializeString(is);
1358 liquid_viscosity = readU8(is);
1359 liquid_renewable = readU8(is);
1360 light_source = readU8(is);
1361 damage_per_second = readU32(is);
1362 node_box.deSerialize(is);
1363 selection_box.deSerialize(is);
1364 legacy_facedir_simple = readU8(is);
1365 legacy_wallmounted = readU8(is);
1366 deSerializeSimpleSoundSpec(sound_footstep, is);
1367 deSerializeSimpleSoundSpec(sound_dig, is);
1368 deSerializeSimpleSoundSpec(sound_dug, is);
1369 rightclickable = readU8(is);
1370 drowning = readU8(is);
1371 leveled = readU8(is);
1372 liquid_range = readU8(is);
1374 throw SerializationError("unsupported ContentFeatures version");
1379 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1381 return m_node_registration_complete;
1385 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1387 m_node_registration_complete = completed;
1391 void CNodeDefManager::pendNodeResolve(NodeResolver *nr)
1394 if (m_node_registration_complete)
1395 nr->nodeResolveInternal();
1397 m_pending_resolve_callbacks.push_back(nr);
1401 bool CNodeDefManager::cancelNodeResolveCallback(NodeResolver *nr)
1403 size_t len = m_pending_resolve_callbacks.size();
1404 for (size_t i = 0; i != len; i++) {
1405 if (nr != m_pending_resolve_callbacks[i])
1409 m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
1410 m_pending_resolve_callbacks.resize(len);
1418 void CNodeDefManager::runNodeResolveCallbacks()
1420 for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
1421 NodeResolver *nr = m_pending_resolve_callbacks[i];
1422 nr->nodeResolveInternal();
1425 m_pending_resolve_callbacks.clear();
1429 void CNodeDefManager::resetNodeResolveState()
1431 m_node_registration_complete = false;
1432 m_pending_resolve_callbacks.clear();
1440 NodeResolver::NodeResolver()
1443 m_nodenames_idx = 0;
1444 m_nnlistsizes_idx = 0;
1445 m_resolve_done = false;
1447 m_nodenames.reserve(16);
1448 m_nnlistsizes.reserve(4);
1452 NodeResolver::~NodeResolver()
1454 if (!m_resolve_done && m_ndef)
1455 m_ndef->cancelNodeResolveCallback(this);
1459 void NodeResolver::nodeResolveInternal()
1461 m_nodenames_idx = 0;
1462 m_nnlistsizes_idx = 0;
1465 m_resolve_done = true;
1467 m_nodenames.clear();
1468 m_nnlistsizes.clear();
1472 bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
1473 const std::string &node_alt, content_t c_fallback)
1475 if (m_nodenames_idx == m_nodenames.size()) {
1476 *result_out = c_fallback;
1477 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1482 std::string name = m_nodenames[m_nodenames_idx++];
1484 bool success = m_ndef->getId(name, c);
1485 if (!success && node_alt != "") {
1487 success = m_ndef->getId(name, c);
1491 errorstream << "NodeResolver: failed to resolve node name '" << name
1492 << "'." << std::endl;
1501 bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
1502 bool all_required, content_t c_fallback)
1504 bool success = true;
1506 if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
1507 errorstream << "NodeResolver: no more node lists" << std::endl;
1511 size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
1514 if (m_nodenames_idx == m_nodenames.size()) {
1515 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1520 std::string &name = m_nodenames[m_nodenames_idx++];
1522 if (name.substr(0,6) != "group:") {
1523 if (m_ndef->getId(name, c)) {
1524 result_out->push_back(c);
1525 } else if (all_required) {
1526 errorstream << "NodeResolver: failed to resolve node name '"
1527 << name << "'." << std::endl;
1528 result_out->push_back(c_fallback);
1532 std::set<content_t> cids;
1533 std::set<content_t>::iterator it;
1534 m_ndef->getIds(name, cids);
1535 for (it = cids.begin(); it != cids.end(); ++it)
1536 result_out->push_back(*it);