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