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