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