]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodedef.cpp
Move globals from main.cpp to more sane locations
[dragonfireclient.git] / src / nodedef.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "nodedef.h"
21
22 #include "itemdef.h"
23 #ifndef SERVER
24 #include "client/tile.h"
25 #include "mesh.h"
26 #include <IMeshManipulator.h>
27 #endif
28 #include "log.h"
29 #include "settings.h"
30 #include "nameidmapping.h"
31 #include "util/numeric.h"
32 #include "util/serialize.h"
33 #include "exceptions.h"
34 #include "debug.h"
35 #include "gamedef.h"
36
37 /*
38         NodeBox
39 */
40
41 void NodeBox::reset()
42 {
43         type = NODEBOX_REGULAR;
44         // default is empty
45         fixed.clear();
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);
50 }
51
52 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
53 {
54         int version = protocol_version >= 21 ? 2 : 1;
55         writeU8(os, version);
56
57         if (version == 1 && type == NODEBOX_LEVELED)
58                 writeU8(os, NODEBOX_FIXED);
59         else
60                 writeU8(os, type);
61
62         if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
63         {
64                 writeU16(os, fixed.size());
65                 for(std::vector<aabb3f>::const_iterator
66                                 i = fixed.begin();
67                                 i != fixed.end(); i++)
68                 {
69                         writeV3F1000(os, i->MinEdge);
70                         writeV3F1000(os, i->MaxEdge);
71                 }
72         }
73         else if(type == NODEBOX_WALLMOUNTED)
74         {
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);
81         }
82 }
83
84 void NodeBox::deSerialize(std::istream &is)
85 {
86         int version = readU8(is);
87         if(version < 1 || version > 2)
88                 throw SerializationError("unsupported NodeBox version");
89
90         reset();
91
92         type = (enum NodeBoxType)readU8(is);
93
94         if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
95         {
96                 u16 fixed_count = readU16(is);
97                 while(fixed_count--)
98                 {
99                         aabb3f box;
100                         box.MinEdge = readV3F1000(is);
101                         box.MaxEdge = readV3F1000(is);
102                         fixed.push_back(box);
103                 }
104         }
105         else if(type == NODEBOX_WALLMOUNTED)
106         {
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);
113         }
114 }
115
116 /*
117         TileDef
118 */
119
120 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
121 {
122         if(protocol_version >= 17)
123                 writeU8(os, 1);
124         else
125                 writeU8(os, 0);
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);
133 }
134
135 void TileDef::deSerialize(std::istream &is)
136 {
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);
143         if(version >= 1)
144                 backface_culling = readU8(is);
145 }
146
147 /*
148         SimpleSoundSpec serialization
149 */
150
151 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
152                 std::ostream &os)
153 {
154         os<<serializeString(ss.name);
155         writeF1000(os, ss.gain);
156 }
157 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
158 {
159         ss.name = deSerializeString(is);
160         ss.gain = readF1000(is);
161 }
162
163 /*
164         ContentFeatures
165 */
166
167 ContentFeatures::ContentFeatures()
168 {
169         reset();
170 }
171
172 ContentFeatures::~ContentFeatures()
173 {
174 }
175
176 void ContentFeatures::reset()
177 {
178         /*
179                 Cached stuff
180         */
181 #ifndef SERVER
182         solidness = 2;
183         visual_solidness = 0;
184         backface_culling = true;
185 #endif
186         has_on_construct = false;
187         has_on_destruct = false;
188         has_after_destruct = false;
189         /*
190                 Actual data
191
192                 NOTE: Most of this is always overridden by the default values given
193                       in builtin.lua
194         */
195         name = "";
196         groups.clear();
197         // Unknown nodes can be dug
198         groups["dig_immediate"] = 2;
199         drawtype = NDT_NORMAL;
200         mesh = "";
201 #ifndef SERVER
202         for(u32 i = 0; i < 24; i++)
203                 mesh_ptr[i] = NULL;
204 #endif
205         visual_scale = 1.0;
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();
210         alpha = 255;
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;
217         walkable = true;
218         pointable = true;
219         diggable = true;
220         climbable = false;
221         buildable_to = false;
222         rightclickable = true;
223         leveled = 0;
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;
230         drowning = 0;
231         light_source = 0;
232         damage_per_second = 0;
233         node_box = NodeBox();
234         selection_box = NodeBox();
235         collision_box = NodeBox();
236         waving = 0;
237         legacy_facedir_simple = false;
238         legacy_wallmounted = false;
239         sound_footstep = SimpleSoundSpec();
240         sound_dig = SimpleSoundSpec("__group");
241         sound_dug = SimpleSoundSpec();
242 }
243
244 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
245 {
246         if(protocol_version < 24){
247                 serializeOld(os, protocol_version);
248                 return;
249         }
250
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);
258         }
259         writeU8(os, drawtype);
260         writeF1000(os, visual_scale);
261         writeU8(os, 6);
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);
267         }
268         writeU8(os, alpha);
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);
302         writeU8(os, waving);
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);
307 }
308
309 void ContentFeatures::deSerialize(std::istream &is)
310 {
311         int version = readU8(is);
312         if(version != 7){
313                 deSerializeOld(is, version);
314                 return;
315         }
316
317         name = deSerializeString(is);
318         groups.clear();
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;
324         }
325         drawtype = (enum NodeDrawType)readU8(is);
326         visual_scale = readF1000(is);
327         if(readU8(is) != 6)
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);
335         alpha = readU8(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);
369         waving = readU8(is);
370         // If you add anything here, insert it primarily inside the try-catch
371         // block to not need to increase the version.
372         try{
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) {};
378 }
379
380 /*
381         CNodeDefManager
382 */
383
384 class CNodeDefManager: public IWritableNodeDefManager {
385 public:
386         CNodeDefManager();
387         virtual ~CNodeDefManager();
388         void clear();
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);
405
406         inline virtual bool getNodeRegistrationStatus() const;
407         inline virtual void setNodeRegistrationStatus(bool completed);
408
409         virtual void pendNodeResolve(NodeResolveInfo *nri);
410         virtual void cancelNodeResolve(NodeResolver *resolver);
411         virtual void runNodeResolverCallbacks();
412
413         virtual bool getIdFromResolveInfo(NodeResolveInfo *nri,
414                 const std::string &node_alt, content_t c_fallback, content_t &result);
415         virtual bool getIdsFromResolveInfo(NodeResolveInfo *nri,
416                 std::vector<content_t> &result);
417
418 private:
419         void addNameIdMapping(content_t i, std::string name);
420 #ifndef SERVER
421         void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
422                 u32 shader_id, bool use_normal_texture, bool backface_culling,
423                 u8 alpha, u8 material_type);
424 #endif
425
426         // Features indexed by id
427         std::vector<ContentFeatures> m_content_features;
428
429         // A mapping for fast converting back and forth between names and ids
430         NameIdMapping m_name_id_mapping;
431
432         // Like m_name_id_mapping, but only from names to ids, and includes
433         // item aliases too. Updated by updateAliases()
434         // Note: Not serialized.
435
436         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
437
438         // A mapping from groups to a list of content_ts (and their levels)
439         // that belong to it.  Necessary for a direct lookup in getIds().
440         // Note: Not serialized.
441         std::map<std::string, GroupItems> m_group_to_items;
442
443         // Next possibly free id
444         content_t m_next_id;
445
446         // List of node strings and node resolver callbacks to perform
447         std::list<NodeResolveInfo *> m_pending_node_lookups;
448
449         // True when all nodes have been registered
450         bool m_node_registration_complete;
451 };
452
453
454 CNodeDefManager::CNodeDefManager()
455 {
456         clear();
457 }
458
459
460 CNodeDefManager::~CNodeDefManager()
461 {
462 #ifndef SERVER
463         for (u32 i = 0; i < m_content_features.size(); i++) {
464                 ContentFeatures *f = &m_content_features[i];
465                 for (u32 j = 0; j < 24; j++) {
466                         if (f->mesh_ptr[j])
467                                 f->mesh_ptr[j]->drop();
468                 }
469         }
470 #endif
471 }
472
473
474 void CNodeDefManager::clear()
475 {
476         m_content_features.clear();
477         m_name_id_mapping.clear();
478         m_name_id_mapping_with_aliases.clear();
479         m_group_to_items.clear();
480         m_next_id = 0;
481
482         m_node_registration_complete = false;
483         for (std::list<NodeResolveInfo *>::iterator
484                         it = m_pending_node_lookups.begin();
485                         it != m_pending_node_lookups.end();
486                         ++it)
487                 delete *it;
488         m_pending_node_lookups.clear();
489
490         u32 initial_length = 0;
491         initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
492         initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
493         initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
494         m_content_features.resize(initial_length);
495
496         // Set CONTENT_UNKNOWN
497         {
498                 ContentFeatures f;
499                 f.name = "unknown";
500                 // Insert directly into containers
501                 content_t c = CONTENT_UNKNOWN;
502                 m_content_features[c] = f;
503                 addNameIdMapping(c, f.name);
504         }
505
506         // Set CONTENT_AIR
507         {
508                 ContentFeatures f;
509                 f.name                = "air";
510                 f.drawtype            = NDT_AIRLIKE;
511                 f.param_type          = CPT_LIGHT;
512                 f.light_propagates    = true;
513                 f.sunlight_propagates = true;
514                 f.walkable            = false;
515                 f.pointable           = false;
516                 f.diggable            = false;
517                 f.buildable_to        = true;
518                 f.is_ground_content   = true;
519                 // Insert directly into containers
520                 content_t c = CONTENT_AIR;
521                 m_content_features[c] = f;
522                 addNameIdMapping(c, f.name);
523         }
524
525         // Set CONTENT_IGNORE
526         {
527                 ContentFeatures f;
528                 f.name                = "ignore";
529                 f.drawtype            = NDT_AIRLIKE;
530                 f.param_type          = CPT_NONE;
531                 f.light_propagates    = false;
532                 f.sunlight_propagates = false;
533                 f.walkable            = false;
534                 f.pointable           = false;
535                 f.diggable            = false;
536                 f.buildable_to        = true; // A way to remove accidental CONTENT_IGNOREs
537                 f.is_ground_content   = true;
538                 // Insert directly into containers
539                 content_t c = CONTENT_IGNORE;
540                 m_content_features[c] = f;
541                 addNameIdMapping(c, f.name);
542         }
543 }
544
545
546 IWritableNodeDefManager *CNodeDefManager::clone()
547 {
548         CNodeDefManager *mgr = new CNodeDefManager();
549         *mgr = *this;
550         return mgr;
551 }
552
553
554 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
555 {
556         return c < m_content_features.size()
557                         ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
558 }
559
560
561 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
562 {
563         return get(n.getContent());
564 }
565
566
567 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
568 {
569         std::map<std::string, content_t>::const_iterator
570                 i = m_name_id_mapping_with_aliases.find(name);
571         if(i == m_name_id_mapping_with_aliases.end())
572                 return false;
573         result = i->second;
574         return true;
575 }
576
577
578 content_t CNodeDefManager::getId(const std::string &name) const
579 {
580         content_t id = CONTENT_IGNORE;
581         getId(name, id);
582         return id;
583 }
584
585
586 void CNodeDefManager::getIds(const std::string &name,
587                 std::set<content_t> &result) const
588 {
589         //TimeTaker t("getIds", NULL, PRECISION_MICRO);
590         if (name.substr(0,6) != "group:") {
591                 content_t id = CONTENT_IGNORE;
592                 if(getId(name, id))
593                         result.insert(id);
594                 return;
595         }
596         std::string group = name.substr(6);
597
598         std::map<std::string, GroupItems>::const_iterator
599                 i = m_group_to_items.find(group);
600         if (i == m_group_to_items.end())
601                 return;
602
603         const GroupItems &items = i->second;
604         for (GroupItems::const_iterator j = items.begin();
605                 j != items.end(); ++j) {
606                 if ((*j).second != 0)
607                         result.insert((*j).first);
608         }
609         //printf("getIds: %dus\n", t.stop());
610 }
611
612
613 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
614 {
615         content_t id = CONTENT_UNKNOWN;
616         getId(name, id);
617         return get(id);
618 }
619
620
621 // returns CONTENT_IGNORE if no free ID found
622 content_t CNodeDefManager::allocateId()
623 {
624         for (content_t id = m_next_id;
625                         id >= m_next_id; // overflow?
626                         ++id) {
627                 while (id >= m_content_features.size()) {
628                         m_content_features.push_back(ContentFeatures());
629                 }
630                 const ContentFeatures &f = m_content_features[id];
631                 if (f.name == "") {
632                         m_next_id = id + 1;
633                         return id;
634                 }
635         }
636         // If we arrive here, an overflow occurred in id.
637         // That means no ID was found
638         return CONTENT_IGNORE;
639 }
640
641
642 // IWritableNodeDefManager
643 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
644 {
645         // Pre-conditions
646         assert(name != "");
647         assert(name == def.name);
648
649         // Don't allow redefining ignore (but allow air and unknown)
650         if (name == "ignore") {
651                 infostream << "NodeDefManager: WARNING: Ignoring "
652                         "CONTENT_IGNORE redefinition"<<std::endl;
653                 return CONTENT_IGNORE;
654         }
655
656         content_t id = CONTENT_IGNORE;
657         if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
658                 // Get new id
659                 id = allocateId();
660                 if (id == CONTENT_IGNORE) {
661                         infostream << "NodeDefManager: WARNING: Absolute "
662                                 "limit reached" << std::endl;
663                         return CONTENT_IGNORE;
664                 }
665                 assert(id != CONTENT_IGNORE);
666                 addNameIdMapping(id, name);
667         }
668         m_content_features[id] = def;
669         verbosestream << "NodeDefManager: registering content id \"" << id
670                 << "\": name=\"" << def.name << "\""<<std::endl;
671
672         // Add this content to the list of all groups it belongs to
673         // FIXME: This should remove a node from groups it no longer
674         // belongs to when a node is re-registered
675         for (ItemGroupList::const_iterator i = def.groups.begin();
676                 i != def.groups.end(); ++i) {
677                 std::string group_name = i->first;
678
679                 std::map<std::string, GroupItems>::iterator
680                         j = m_group_to_items.find(group_name);
681                 if (j == m_group_to_items.end()) {
682                         m_group_to_items[group_name].push_back(
683                                         std::make_pair(id, i->second));
684                 } else {
685                         GroupItems &items = j->second;
686                         items.push_back(std::make_pair(id, i->second));
687                 }
688         }
689         return id;
690 }
691
692
693 content_t CNodeDefManager::allocateDummy(const std::string &name)
694 {
695         assert(name != "");     // Pre-condition
696         ContentFeatures f;
697         f.name = name;
698         return set(name, f);
699 }
700
701
702 void CNodeDefManager::updateAliases(IItemDefManager *idef)
703 {
704         std::set<std::string> all = idef->getAll();
705         m_name_id_mapping_with_aliases.clear();
706         for (std::set<std::string>::iterator
707                         i = all.begin(); i != all.end(); i++) {
708                 std::string name = *i;
709                 std::string convert_to = idef->getAlias(name);
710                 content_t id;
711                 if (m_name_id_mapping.getId(convert_to, id)) {
712                         m_name_id_mapping_with_aliases.insert(
713                                         std::make_pair(name, id));
714                 }
715         }
716 }
717
718
719 void CNodeDefManager::updateTextures(IGameDef *gamedef,
720         void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
721         void *progress_callback_args)
722 {
723 #ifndef SERVER
724         infostream << "CNodeDefManager::updateTextures(): Updating "
725                 "textures in node definitions" << std::endl;
726
727         ITextureSource *tsrc = gamedef->tsrc();
728         IShaderSource *shdsrc = gamedef->getShaderSource();
729         scene::ISceneManager* smgr = gamedef->getSceneManager();
730         scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
731
732         bool new_style_water           = g_settings->getBool("new_style_water");
733         bool new_style_leaves          = g_settings->getBool("new_style_leaves");
734         bool connected_glass           = g_settings->getBool("connected_glass");
735         bool opaque_water              = g_settings->getBool("opaque_water");
736         bool enable_shaders            = g_settings->getBool("enable_shaders");
737         bool enable_bumpmapping        = g_settings->getBool("enable_bumpmapping");
738         bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
739         bool enable_mesh_cache         = g_settings->getBool("enable_mesh_cache");
740
741         bool use_normal_texture = enable_shaders &&
742                 (enable_bumpmapping || enable_parallax_occlusion);
743
744         u32 size = m_content_features.size();
745
746         for (u32 i = 0; i < size; i++) {
747                 ContentFeatures *f = &m_content_features[i];
748
749                 // Figure out the actual tiles to use
750                 TileDef tiledef[6];
751                 for (u32 j = 0; j < 6; j++) {
752                         tiledef[j] = f->tiledef[j];
753                         if (tiledef[j].name == "")
754                                 tiledef[j].name = "unknown_node.png";
755                 }
756
757                 bool is_liquid = false;
758                 bool is_water_surface = false;
759
760                 u8 material_type = (f->alpha == 255) ?
761                         TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
762
763                 switch (f->drawtype) {
764                 default:
765                 case NDT_NORMAL:
766                         f->solidness = 2;
767                         break;
768                 case NDT_AIRLIKE:
769                         f->solidness = 0;
770                         break;
771                 case NDT_LIQUID:
772                         assert(f->liquid_type == LIQUID_SOURCE);
773                         if (opaque_water)
774                                 f->alpha = 255;
775                         if (new_style_water){
776                                 f->solidness = 0;
777                         } else {
778                                 f->solidness = 1;
779                                 f->backface_culling = false;
780                         }
781                         is_liquid = true;
782                         break;
783                 case NDT_FLOWINGLIQUID:
784                         assert(f->liquid_type == LIQUID_FLOWING);
785                         f->solidness = 0;
786                         if (opaque_water)
787                                 f->alpha = 255;
788                         is_liquid = true;
789                         break;
790                 case NDT_GLASSLIKE:
791                         f->solidness = 0;
792                         f->visual_solidness = 1;
793                         break;
794                 case NDT_GLASSLIKE_FRAMED:
795                         f->solidness = 0;
796                         f->visual_solidness = 1;
797                         break;
798                 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
799                         f->solidness = 0;
800                         f->visual_solidness = 1;
801                         f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
802                         break;
803                 case NDT_ALLFACES:
804                         f->solidness = 0;
805                         f->visual_solidness = 1;
806                         break;
807                 case NDT_ALLFACES_OPTIONAL:
808                         if (new_style_leaves) {
809                                 f->drawtype = NDT_ALLFACES;
810                                 f->solidness = 0;
811                                 f->visual_solidness = 1;
812                         } else {
813                                 f->drawtype = NDT_NORMAL;
814                                 f->solidness = 2;
815                                 for (u32 i = 0; i < 6; i++)
816                                         tiledef[i].name += std::string("^[noalpha");
817                         }
818                         if (f->waving == 1)
819                                 material_type = TILE_MATERIAL_WAVING_LEAVES;
820                         break;
821                 case NDT_PLANTLIKE:
822                         f->solidness = 0;
823                         f->backface_culling = false;
824                         if (f->waving == 1)
825                                 material_type = TILE_MATERIAL_WAVING_PLANTS;
826                         break;
827                 case NDT_FIRELIKE:
828                         f->backface_culling = false;
829                         f->solidness = 0;
830                         break;
831                 case NDT_MESH:
832                         f->solidness = 0;
833                         f->backface_culling = false;
834                         break;
835                 case NDT_TORCHLIKE:
836                 case NDT_SIGNLIKE:
837                 case NDT_FENCELIKE:
838                 case NDT_RAILLIKE:
839                 case NDT_NODEBOX:
840                         f->solidness = 0;
841                         break;
842                 }
843
844                 if (is_liquid) {
845                         material_type = (f->alpha == 255) ?
846                                 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
847                         if (f->name == "default:water_source")
848                                 is_water_surface = true;
849                 }
850
851                 u32 tile_shader[6];
852                 for (u16 j = 0; j < 6; j++) {
853                         tile_shader[j] = shdsrc->getShader("nodes_shader",
854                                 material_type, f->drawtype);
855                 }
856
857                 if (is_water_surface) {
858                         tile_shader[0] = shdsrc->getShader("water_surface_shader",
859                                 material_type, f->drawtype);
860                 }
861
862                 // Tiles (fill in f->tiles[])
863                 for (u16 j = 0; j < 6; j++) {
864                         fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
865                                 use_normal_texture, f->backface_culling, f->alpha, material_type);
866                 }
867
868                 // Special tiles (fill in f->special_tiles[])
869                 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
870                         fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
871                                 tile_shader[j], use_normal_texture,
872                                 f->tiledef_special[j].backface_culling, f->alpha, material_type);
873                 }
874
875                 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
876                         // Meshnode drawtype
877                         // Read the mesh and apply scale
878                         f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
879                         if (f->mesh_ptr[0]){
880                                 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
881                                 scaleMesh(f->mesh_ptr[0], scale);
882                                 recalculateBoundingBox(f->mesh_ptr[0]);
883                                 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
884                         }
885                 } else if ((f->drawtype == NDT_NODEBOX) &&
886                                 ((f->node_box.type == NODEBOX_REGULAR) ||
887                                 (f->node_box.type == NODEBOX_FIXED)) &&
888                                 (!f->node_box.fixed.empty())) {
889                         //Convert regular nodebox nodes to meshnodes
890                         //Change the drawtype and apply scale
891                         f->drawtype = NDT_MESH;
892                         f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
893                         v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
894                         scaleMesh(f->mesh_ptr[0], scale);
895                         recalculateBoundingBox(f->mesh_ptr[0]);
896                         meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
897                 }
898
899                 //Cache 6dfacedir and wallmounted rotated clones of meshes
900                 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
901                         for (u16 j = 1; j < 24; j++) {
902                                 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
903                                 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
904                                 recalculateBoundingBox(f->mesh_ptr[j]);
905                                 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
906                         }
907                 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
908                         static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
909                         for (u16 j = 1; j < 6; j++) {
910                                 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
911                                 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
912                                 recalculateBoundingBox(f->mesh_ptr[j]);
913                                 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
914                         }
915                         rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
916                         recalculateBoundingBox(f->mesh_ptr[0]);
917                         meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
918                 }
919
920                 progress_callback(progress_callback_args, i, size);
921         }
922 #endif
923 }
924
925
926 #ifndef SERVER
927 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
928                 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
929                 bool backface_culling, u8 alpha, u8 material_type)
930 {
931         tile->shader_id     = shader_id;
932         tile->texture       = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
933         tile->alpha         = alpha;
934         tile->material_type = material_type;
935
936         // Normal texture
937         if (use_normal_texture)
938                 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
939
940         // Material flags
941         tile->material_flags = 0;
942         if (backface_culling)
943                 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
944         if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
945                 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
946
947         // Animation parameters
948         int frame_count = 1;
949         if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
950                 // Get texture size to determine frame count by aspect ratio
951                 v2u32 size = tile->texture->getOriginalSize();
952                 int frame_height = (float)size.X /
953                                 (float)tiledef->animation.aspect_w *
954                                 (float)tiledef->animation.aspect_h;
955                 frame_count = size.Y / frame_height;
956                 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
957                 tile->animation_frame_count = frame_count;
958                 tile->animation_frame_length_ms = frame_length_ms;
959         }
960
961         if (frame_count == 1) {
962                 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
963         } else {
964                 std::ostringstream os(std::ios::binary);
965                 tile->frames.resize(frame_count);
966
967                 for (int i = 0; i < frame_count; i++) {
968
969                         FrameSpec frame;
970
971                         os.str("");
972                         os << tiledef->name << "^[verticalframe:"
973                                 << frame_count << ":" << i;
974
975                         frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
976                         if (tile->normal_texture)
977                                 frame.normal_texture = tsrc->getNormalTexture(os.str());
978                         tile->frames[i] = frame;
979                 }
980         }
981 }
982 #endif
983
984
985 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version)
986 {
987         writeU8(os, 1); // version
988         u16 count = 0;
989         std::ostringstream os2(std::ios::binary);
990         for (u32 i = 0; i < m_content_features.size(); i++) {
991                 if (i == CONTENT_IGNORE || i == CONTENT_AIR
992                                 || i == CONTENT_UNKNOWN)
993                         continue;
994                 ContentFeatures *f = &m_content_features[i];
995                 if (f->name == "")
996                         continue;
997                 writeU16(os2, i);
998                 // Wrap it in a string to allow different lengths without
999                 // strict version incompatibilities
1000                 std::ostringstream wrapper_os(std::ios::binary);
1001                 f->serialize(wrapper_os, protocol_version);
1002                 os2<<serializeString(wrapper_os.str());
1003
1004                 // must not overflow
1005                 u16 next = count + 1;
1006                 FATAL_ERROR_IF(next < count, "Overflow");
1007                 count++;
1008         }
1009         writeU16(os, count);
1010         os << serializeLongString(os2.str());
1011 }
1012
1013
1014 void CNodeDefManager::deSerialize(std::istream &is)
1015 {
1016         clear();
1017         int version = readU8(is);
1018         if (version != 1)
1019                 throw SerializationError("unsupported NodeDefinitionManager version");
1020         u16 count = readU16(is);
1021         std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1022         ContentFeatures f;
1023         for (u16 n = 0; n < count; n++) {
1024                 u16 i = readU16(is2);
1025
1026                 // Read it from the string wrapper
1027                 std::string wrapper = deSerializeString(is2);
1028                 std::istringstream wrapper_is(wrapper, std::ios::binary);
1029                 f.deSerialize(wrapper_is);
1030
1031                 // Check error conditions
1032                 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1033                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1034                                 "not changing builtin node " << i << std::endl;
1035                         continue;
1036                 }
1037                 if (f.name == "") {
1038                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1039                                 "received empty name" << std::endl;
1040                         continue;
1041                 }
1042
1043                 // Ignore aliases
1044                 u16 existing_id;
1045                 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1046                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1047                                 "already defined with different ID: " << f.name << std::endl;
1048                         continue;
1049                 }
1050
1051                 // All is ok, add node definition with the requested ID
1052                 if (i >= m_content_features.size())
1053                         m_content_features.resize((u32)(i) + 1);
1054                 m_content_features[i] = f;
1055                 addNameIdMapping(i, f.name);
1056                 verbosestream << "deserialized " << f.name << std::endl;
1057         }
1058 }
1059
1060
1061 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1062 {
1063         m_name_id_mapping.set(i, name);
1064         m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1065 }
1066
1067
1068 IWritableNodeDefManager *createNodeDefManager()
1069 {
1070         return new CNodeDefManager();
1071 }
1072
1073
1074 //// Serialization of old ContentFeatures formats
1075 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
1076 {
1077         if (protocol_version == 13)
1078         {
1079                 writeU8(os, 5); // version
1080                 os<<serializeString(name);
1081                 writeU16(os, groups.size());
1082                 for (ItemGroupList::const_iterator
1083                                 i = groups.begin(); i != groups.end(); i++) {
1084                         os<<serializeString(i->first);
1085                         writeS16(os, i->second);
1086                 }
1087                 writeU8(os, drawtype);
1088                 writeF1000(os, visual_scale);
1089                 writeU8(os, 6);
1090                 for (u32 i = 0; i < 6; i++)
1091                         tiledef[i].serialize(os, protocol_version);
1092                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1093                 writeU8(os, 2);
1094                 for (u32 i = 0; i < 2; i++)
1095                         tiledef_special[i].serialize(os, protocol_version);
1096                 writeU8(os, alpha);
1097                 writeU8(os, post_effect_color.getAlpha());
1098                 writeU8(os, post_effect_color.getRed());
1099                 writeU8(os, post_effect_color.getGreen());
1100                 writeU8(os, post_effect_color.getBlue());
1101                 writeU8(os, param_type);
1102                 writeU8(os, param_type_2);
1103                 writeU8(os, is_ground_content);
1104                 writeU8(os, light_propagates);
1105                 writeU8(os, sunlight_propagates);
1106                 writeU8(os, walkable);
1107                 writeU8(os, pointable);
1108                 writeU8(os, diggable);
1109                 writeU8(os, climbable);
1110                 writeU8(os, buildable_to);
1111                 os<<serializeString(""); // legacy: used to be metadata_name
1112                 writeU8(os, liquid_type);
1113                 os<<serializeString(liquid_alternative_flowing);
1114                 os<<serializeString(liquid_alternative_source);
1115                 writeU8(os, liquid_viscosity);
1116                 writeU8(os, light_source);
1117                 writeU32(os, damage_per_second);
1118                 node_box.serialize(os, protocol_version);
1119                 selection_box.serialize(os, protocol_version);
1120                 writeU8(os, legacy_facedir_simple);
1121                 writeU8(os, legacy_wallmounted);
1122                 serializeSimpleSoundSpec(sound_footstep, os);
1123                 serializeSimpleSoundSpec(sound_dig, os);
1124                 serializeSimpleSoundSpec(sound_dug, os);
1125         }
1126         else if (protocol_version > 13 && protocol_version < 24) {
1127                 writeU8(os, 6); // version
1128                 os<<serializeString(name);
1129                 writeU16(os, groups.size());
1130                 for (ItemGroupList::const_iterator
1131                         i = groups.begin(); i != groups.end(); i++) {
1132                                 os<<serializeString(i->first);
1133                                 writeS16(os, i->second);
1134                 }
1135                 writeU8(os, drawtype);
1136                 writeF1000(os, visual_scale);
1137                 writeU8(os, 6);
1138                 for (u32 i = 0; i < 6; i++)
1139                         tiledef[i].serialize(os, protocol_version);
1140                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1141                 writeU8(os, 2);
1142                 for (u32 i = 0; i < 2; i++)
1143                         tiledef_special[i].serialize(os, protocol_version);
1144                 writeU8(os, alpha);
1145                 writeU8(os, post_effect_color.getAlpha());
1146                 writeU8(os, post_effect_color.getRed());
1147                 writeU8(os, post_effect_color.getGreen());
1148                 writeU8(os, post_effect_color.getBlue());
1149                 writeU8(os, param_type);
1150                 writeU8(os, param_type_2);
1151                 writeU8(os, is_ground_content);
1152                 writeU8(os, light_propagates);
1153                 writeU8(os, sunlight_propagates);
1154                 writeU8(os, walkable);
1155                 writeU8(os, pointable);
1156                 writeU8(os, diggable);
1157                 writeU8(os, climbable);
1158                 writeU8(os, buildable_to);
1159                 os<<serializeString(""); // legacy: used to be metadata_name
1160                 writeU8(os, liquid_type);
1161                 os<<serializeString(liquid_alternative_flowing);
1162                 os<<serializeString(liquid_alternative_source);
1163                 writeU8(os, liquid_viscosity);
1164                 writeU8(os, liquid_renewable);
1165                 writeU8(os, light_source);
1166                 writeU32(os, damage_per_second);
1167                 node_box.serialize(os, protocol_version);
1168                 selection_box.serialize(os, protocol_version);
1169                 writeU8(os, legacy_facedir_simple);
1170                 writeU8(os, legacy_wallmounted);
1171                 serializeSimpleSoundSpec(sound_footstep, os);
1172                 serializeSimpleSoundSpec(sound_dig, os);
1173                 serializeSimpleSoundSpec(sound_dug, os);
1174                 writeU8(os, rightclickable);
1175                 writeU8(os, drowning);
1176                 writeU8(os, leveled);
1177                 writeU8(os, liquid_range);
1178         } else
1179                 throw SerializationError("ContentFeatures::serialize(): "
1180                         "Unsupported version requested");
1181 }
1182
1183
1184 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1185 {
1186         if (version == 5) // In PROTOCOL_VERSION 13
1187         {
1188                 name = deSerializeString(is);
1189                 groups.clear();
1190                 u32 groups_size = readU16(is);
1191                 for(u32 i=0; i<groups_size; i++){
1192                         std::string name = deSerializeString(is);
1193                         int value = readS16(is);
1194                         groups[name] = value;
1195                 }
1196                 drawtype = (enum NodeDrawType)readU8(is);
1197                 visual_scale = readF1000(is);
1198                 if (readU8(is) != 6)
1199                         throw SerializationError("unsupported tile count");
1200                 for (u32 i = 0; i < 6; i++)
1201                         tiledef[i].deSerialize(is);
1202                 if (readU8(is) != CF_SPECIAL_COUNT)
1203                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1204                 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1205                         tiledef_special[i].deSerialize(is);
1206                 alpha = readU8(is);
1207                 post_effect_color.setAlpha(readU8(is));
1208                 post_effect_color.setRed(readU8(is));
1209                 post_effect_color.setGreen(readU8(is));
1210                 post_effect_color.setBlue(readU8(is));
1211                 param_type = (enum ContentParamType)readU8(is);
1212                 param_type_2 = (enum ContentParamType2)readU8(is);
1213                 is_ground_content = readU8(is);
1214                 light_propagates = readU8(is);
1215                 sunlight_propagates = readU8(is);
1216                 walkable = readU8(is);
1217                 pointable = readU8(is);
1218                 diggable = readU8(is);
1219                 climbable = readU8(is);
1220                 buildable_to = readU8(is);
1221                 deSerializeString(is); // legacy: used to be metadata_name
1222                 liquid_type = (enum LiquidType)readU8(is);
1223                 liquid_alternative_flowing = deSerializeString(is);
1224                 liquid_alternative_source = deSerializeString(is);
1225                 liquid_viscosity = readU8(is);
1226                 light_source = readU8(is);
1227                 damage_per_second = readU32(is);
1228                 node_box.deSerialize(is);
1229                 selection_box.deSerialize(is);
1230                 legacy_facedir_simple = readU8(is);
1231                 legacy_wallmounted = readU8(is);
1232                 deSerializeSimpleSoundSpec(sound_footstep, is);
1233                 deSerializeSimpleSoundSpec(sound_dig, is);
1234                 deSerializeSimpleSoundSpec(sound_dug, is);
1235         } else if (version == 6) {
1236                 name = deSerializeString(is);
1237                 groups.clear();
1238                 u32 groups_size = readU16(is);
1239                 for (u32 i = 0; i < groups_size; i++) {
1240                         std::string name = deSerializeString(is);
1241                         int     value = readS16(is);
1242                         groups[name] = value;
1243                 }
1244                 drawtype = (enum NodeDrawType)readU8(is);
1245                 visual_scale = readF1000(is);
1246                 if (readU8(is) != 6)
1247                         throw SerializationError("unsupported tile count");
1248                 for (u32 i = 0; i < 6; i++)
1249                         tiledef[i].deSerialize(is);
1250                 // CF_SPECIAL_COUNT in version 6 = 2
1251                 if (readU8(is) != 2)
1252                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1253                 for (u32 i = 0; i < 2; i++)
1254                         tiledef_special[i].deSerialize(is);
1255                 alpha = readU8(is);
1256                 post_effect_color.setAlpha(readU8(is));
1257                 post_effect_color.setRed(readU8(is));
1258                 post_effect_color.setGreen(readU8(is));
1259                 post_effect_color.setBlue(readU8(is));
1260                 param_type = (enum ContentParamType)readU8(is);
1261                 param_type_2 = (enum ContentParamType2)readU8(is);
1262                 is_ground_content = readU8(is);
1263                 light_propagates = readU8(is);
1264                 sunlight_propagates = readU8(is);
1265                 walkable = readU8(is);
1266                 pointable = readU8(is);
1267                 diggable = readU8(is);
1268                 climbable = readU8(is);
1269                 buildable_to = readU8(is);
1270                 deSerializeString(is); // legacy: used to be metadata_name
1271                 liquid_type = (enum LiquidType)readU8(is);
1272                 liquid_alternative_flowing = deSerializeString(is);
1273                 liquid_alternative_source = deSerializeString(is);
1274                 liquid_viscosity = readU8(is);
1275                 liquid_renewable = readU8(is);
1276                 light_source = readU8(is);
1277                 damage_per_second = readU32(is);
1278                 node_box.deSerialize(is);
1279                 selection_box.deSerialize(is);
1280                 legacy_facedir_simple = readU8(is);
1281                 legacy_wallmounted = readU8(is);
1282                 deSerializeSimpleSoundSpec(sound_footstep, is);
1283                 deSerializeSimpleSoundSpec(sound_dig, is);
1284                 deSerializeSimpleSoundSpec(sound_dug, is);
1285                 rightclickable = readU8(is);
1286                 drowning = readU8(is);
1287                 leveled = readU8(is);
1288                 liquid_range = readU8(is);
1289         } else {
1290                 throw SerializationError("unsupported ContentFeatures version");
1291         }
1292 }
1293
1294
1295 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1296 {
1297         return m_node_registration_complete;
1298 }
1299
1300
1301 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1302 {
1303         m_node_registration_complete = completed;
1304 }
1305
1306
1307 void CNodeDefManager::pendNodeResolve(NodeResolveInfo *nri)
1308 {
1309         nri->resolver->m_ndef = this;
1310         if (m_node_registration_complete) {
1311                 nri->resolver->resolveNodeNames(nri);
1312                 nri->resolver->m_lookup_done = true;
1313                 delete nri;
1314         } else {
1315                 m_pending_node_lookups.push_back(nri);
1316         }
1317 }
1318
1319
1320 void CNodeDefManager::cancelNodeResolve(NodeResolver *resolver)
1321 {
1322         for (std::list<NodeResolveInfo *>::iterator
1323                         it = m_pending_node_lookups.begin();
1324                         it != m_pending_node_lookups.end();
1325                         ++it) {
1326                 NodeResolveInfo *nri = *it;
1327                 if (resolver == nri->resolver) {
1328                         it = m_pending_node_lookups.erase(it);
1329                         delete nri;
1330                 }
1331         }
1332 }
1333
1334
1335 void CNodeDefManager::runNodeResolverCallbacks()
1336 {
1337         while (!m_pending_node_lookups.empty()) {
1338                 NodeResolveInfo *nri = m_pending_node_lookups.front();
1339                 m_pending_node_lookups.pop_front();
1340                 nri->resolver->resolveNodeNames(nri);
1341                 nri->resolver->m_lookup_done = true;
1342                 delete nri;
1343         }
1344 }
1345
1346
1347 bool CNodeDefManager::getIdFromResolveInfo(NodeResolveInfo *nri,
1348         const std::string &node_alt, content_t c_fallback, content_t &result)
1349 {
1350         if (nri->nodenames.empty()) {
1351                 result = c_fallback;
1352                 errorstream << "Resolver empty nodename list" << std::endl;
1353                 return false;
1354         }
1355
1356         content_t c;
1357         std::string name = nri->nodenames.front();
1358         nri->nodenames.pop_front();
1359
1360         bool success = getId(name, c);
1361         if (!success && node_alt != "") {
1362                 name = node_alt;
1363                 success = getId(name, c);
1364         }
1365
1366         if (!success) {
1367                 errorstream << "Resolver: Failed to resolve node name '" << name
1368                         << "'." << std::endl;
1369                 c = c_fallback;
1370         }
1371
1372         result = c;
1373         return success;
1374 }
1375
1376
1377 bool CNodeDefManager::getIdsFromResolveInfo(NodeResolveInfo *nri,
1378         std::vector<content_t> &result)
1379 {
1380         bool success = true;
1381
1382         if (nri->nodelistinfo.empty()) {
1383                 errorstream << "Resolver: Empty nodelistinfo list" << std::endl;
1384                 return false;
1385         }
1386
1387         NodeListInfo listinfo = nri->nodelistinfo.front();
1388         nri->nodelistinfo.pop_front();
1389
1390         while (listinfo.length--) {
1391                 if (nri->nodenames.empty()) {
1392                         errorstream << "Resolver: Empty nodename list" << std::endl;
1393                         return false;
1394                 }
1395
1396                 content_t c;
1397                 std::string name = nri->nodenames.front();
1398                 nri->nodenames.pop_front();
1399
1400                 if (name.substr(0,6) != "group:") {
1401                         if (getId(name, c)) {
1402                                 result.push_back(c);
1403                         } else if (listinfo.all_required) {
1404                                 errorstream << "Resolver: Failed to resolve node name '" << name
1405                                         << "'." << std::endl;
1406                                 result.push_back(listinfo.c_fallback);
1407                                 success = false;
1408                         }
1409                 } else {
1410                         std::set<content_t> cids;
1411                         std::set<content_t>::iterator it;
1412                         getIds(name, cids);
1413                         for (it = cids.begin(); it != cids.end(); ++it)
1414                                 result.push_back(*it);
1415                 }
1416         }
1417
1418         return success;
1419 }