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