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