]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.cpp
Add minimap feature
[minetest.git] / src / nodedef.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "nodedef.h"
21
22 #include "itemdef.h"
23 #ifndef SERVER
24 #include "client/tile.h"
25 #include "mesh.h"
26 #include <IMeshManipulator.h>
27 #endif
28 #include "log.h"
29 #include "settings.h"
30 #include "nameidmapping.h"
31 #include "util/numeric.h"
32 #include "util/serialize.h"
33 #include "exceptions.h"
34 #include "debug.h"
35 #include "gamedef.h"
36 #include <fstream> // Used in applyTextureOverrides()
37
38 /*
39         NodeBox
40 */
41
42 void NodeBox::reset()
43 {
44         type = NODEBOX_REGULAR;
45         // default is empty
46         fixed.clear();
47         // default is sign/ladder-like
48         wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
49         wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
50         wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
51 }
52
53 void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
54 {
55         int version = protocol_version >= 21 ? 2 : 1;
56         writeU8(os, version);
57
58         if (version == 1 && type == NODEBOX_LEVELED)
59                 writeU8(os, NODEBOX_FIXED);
60         else
61                 writeU8(os, type);
62
63         if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
64         {
65                 writeU16(os, fixed.size());
66                 for(std::vector<aabb3f>::const_iterator
67                                 i = fixed.begin();
68                                 i != fixed.end(); i++)
69                 {
70                         writeV3F1000(os, i->MinEdge);
71                         writeV3F1000(os, i->MaxEdge);
72                 }
73         }
74         else if(type == NODEBOX_WALLMOUNTED)
75         {
76                 writeV3F1000(os, wall_top.MinEdge);
77                 writeV3F1000(os, wall_top.MaxEdge);
78                 writeV3F1000(os, wall_bottom.MinEdge);
79                 writeV3F1000(os, wall_bottom.MaxEdge);
80                 writeV3F1000(os, wall_side.MinEdge);
81                 writeV3F1000(os, wall_side.MaxEdge);
82         }
83 }
84
85 void NodeBox::deSerialize(std::istream &is)
86 {
87         int version = readU8(is);
88         if(version < 1 || version > 2)
89                 throw SerializationError("unsupported NodeBox version");
90
91         reset();
92
93         type = (enum NodeBoxType)readU8(is);
94
95         if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
96         {
97                 u16 fixed_count = readU16(is);
98                 while(fixed_count--)
99                 {
100                         aabb3f box;
101                         box.MinEdge = readV3F1000(is);
102                         box.MaxEdge = readV3F1000(is);
103                         fixed.push_back(box);
104                 }
105         }
106         else if(type == NODEBOX_WALLMOUNTED)
107         {
108                 wall_top.MinEdge = readV3F1000(is);
109                 wall_top.MaxEdge = readV3F1000(is);
110                 wall_bottom.MinEdge = readV3F1000(is);
111                 wall_bottom.MaxEdge = readV3F1000(is);
112                 wall_side.MinEdge = readV3F1000(is);
113                 wall_side.MaxEdge = readV3F1000(is);
114         }
115 }
116
117 /*
118         TileDef
119 */
120
121 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
122 {
123         if(protocol_version >= 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         minimap_color = video::SColor(0, 0, 0, 0);
206 #endif
207         visual_scale = 1.0;
208         for(u32 i = 0; i < 6; i++)
209                 tiledef[i] = TileDef();
210         for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
211                 tiledef_special[j] = TileDef();
212         alpha = 255;
213         post_effect_color = video::SColor(0, 0, 0, 0);
214         param_type = CPT_NONE;
215         param_type_2 = CPT2_NONE;
216         is_ground_content = false;
217         light_propagates = false;
218         sunlight_propagates = false;
219         walkable = true;
220         pointable = true;
221         diggable = true;
222         climbable = false;
223         buildable_to = false;
224         rightclickable = true;
225         leveled = 0;
226         liquid_type = LIQUID_NONE;
227         liquid_alternative_flowing = "";
228         liquid_alternative_source = "";
229         liquid_viscosity = 0;
230         liquid_renewable = true;
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) const
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         inline virtual const ContentFeatures& get(content_t c) const;
393         inline 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 applyTextureOverrides(const std::string &override_filepath);
403         virtual void updateTextures(IGameDef *gamedef,
404                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
405                 void *progress_cbk_args);
406         void serialize(std::ostream &os, u16 protocol_version) const;
407         void deSerialize(std::istream &is);
408
409         inline virtual bool getNodeRegistrationStatus() const;
410         inline virtual void setNodeRegistrationStatus(bool completed);
411
412         virtual void pendNodeResolve(NodeResolver *nr);
413         virtual bool cancelNodeResolveCallback(NodeResolver *nr);
414         virtual void runNodeResolveCallbacks();
415         virtual void resetNodeResolveState();
416
417 private:
418         void addNameIdMapping(content_t i, std::string name);
419 #ifndef SERVER
420         void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
421                 u32 shader_id, bool use_normal_texture, bool backface_culling,
422                 u8 alpha, u8 material_type);
423 #endif
424
425         // Features indexed by id
426         std::vector<ContentFeatures> m_content_features;
427
428         // A mapping for fast converting back and forth between names and ids
429         NameIdMapping m_name_id_mapping;
430
431         // Like m_name_id_mapping, but only from names to ids, and includes
432         // item aliases too. Updated by updateAliases()
433         // Note: Not serialized.
434
435         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
436
437         // A mapping from groups to a list of content_ts (and their levels)
438         // that belong to it.  Necessary for a direct lookup in getIds().
439         // Note: Not serialized.
440         std::map<std::string, GroupItems> m_group_to_items;
441
442         // Next possibly free id
443         content_t m_next_id;
444
445         // NodeResolvers to callback once node registration has ended
446         std::vector<NodeResolver *> m_pending_resolve_callbacks;
447
448         // True when all nodes have been registered
449         bool m_node_registration_complete;
450 };
451
452
453 CNodeDefManager::CNodeDefManager()
454 {
455         clear();
456 }
457
458
459 CNodeDefManager::~CNodeDefManager()
460 {
461 #ifndef SERVER
462         for (u32 i = 0; i < m_content_features.size(); i++) {
463                 ContentFeatures *f = &m_content_features[i];
464                 for (u32 j = 0; j < 24; j++) {
465                         if (f->mesh_ptr[j])
466                                 f->mesh_ptr[j]->drop();
467                 }
468         }
469 #endif
470 }
471
472
473 void CNodeDefManager::clear()
474 {
475         m_content_features.clear();
476         m_name_id_mapping.clear();
477         m_name_id_mapping_with_aliases.clear();
478         m_group_to_items.clear();
479         m_next_id = 0;
480
481         resetNodeResolveState();
482
483         u32 initial_length = 0;
484         initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
485         initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
486         initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
487         m_content_features.resize(initial_length);
488
489         // Set CONTENT_UNKNOWN
490         {
491                 ContentFeatures f;
492                 f.name = "unknown";
493                 // Insert directly into containers
494                 content_t c = CONTENT_UNKNOWN;
495                 m_content_features[c] = f;
496                 addNameIdMapping(c, f.name);
497         }
498
499         // Set CONTENT_AIR
500         {
501                 ContentFeatures f;
502                 f.name                = "air";
503                 f.drawtype            = NDT_AIRLIKE;
504                 f.param_type          = CPT_LIGHT;
505                 f.light_propagates    = true;
506                 f.sunlight_propagates = true;
507                 f.walkable            = false;
508                 f.pointable           = false;
509                 f.diggable            = false;
510                 f.buildable_to        = true;
511                 f.is_ground_content   = true;
512                 // Insert directly into containers
513                 content_t c = CONTENT_AIR;
514                 m_content_features[c] = f;
515                 addNameIdMapping(c, f.name);
516         }
517
518         // Set CONTENT_IGNORE
519         {
520                 ContentFeatures f;
521                 f.name                = "ignore";
522                 f.drawtype            = NDT_AIRLIKE;
523                 f.param_type          = CPT_NONE;
524                 f.light_propagates    = false;
525                 f.sunlight_propagates = false;
526                 f.walkable            = false;
527                 f.pointable           = false;
528                 f.diggable            = false;
529                 f.buildable_to        = true; // A way to remove accidental CONTENT_IGNOREs
530                 f.is_ground_content   = true;
531                 // Insert directly into containers
532                 content_t c = CONTENT_IGNORE;
533                 m_content_features[c] = f;
534                 addNameIdMapping(c, f.name);
535         }
536 }
537
538
539 IWritableNodeDefManager *CNodeDefManager::clone()
540 {
541         CNodeDefManager *mgr = new CNodeDefManager();
542         *mgr = *this;
543         return mgr;
544 }
545
546
547 inline const ContentFeatures& CNodeDefManager::get(content_t c) const
548 {
549         return c < m_content_features.size()
550                         ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
551 }
552
553
554 inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
555 {
556         return get(n.getContent());
557 }
558
559
560 bool CNodeDefManager::getId(const std::string &name, content_t &result) const
561 {
562         std::map<std::string, content_t>::const_iterator
563                 i = m_name_id_mapping_with_aliases.find(name);
564         if(i == m_name_id_mapping_with_aliases.end())
565                 return false;
566         result = i->second;
567         return true;
568 }
569
570
571 content_t CNodeDefManager::getId(const std::string &name) const
572 {
573         content_t id = CONTENT_IGNORE;
574         getId(name, id);
575         return id;
576 }
577
578
579 void CNodeDefManager::getIds(const std::string &name,
580                 std::set<content_t> &result) const
581 {
582         //TimeTaker t("getIds", NULL, PRECISION_MICRO);
583         if (name.substr(0,6) != "group:") {
584                 content_t id = CONTENT_IGNORE;
585                 if(getId(name, id))
586                         result.insert(id);
587                 return;
588         }
589         std::string group = name.substr(6);
590
591         std::map<std::string, GroupItems>::const_iterator
592                 i = m_group_to_items.find(group);
593         if (i == m_group_to_items.end())
594                 return;
595
596         const GroupItems &items = i->second;
597         for (GroupItems::const_iterator j = items.begin();
598                 j != items.end(); ++j) {
599                 if ((*j).second != 0)
600                         result.insert((*j).first);
601         }
602         //printf("getIds: %dus\n", t.stop());
603 }
604
605
606 const ContentFeatures& CNodeDefManager::get(const std::string &name) const
607 {
608         content_t id = CONTENT_UNKNOWN;
609         getId(name, id);
610         return get(id);
611 }
612
613
614 // returns CONTENT_IGNORE if no free ID found
615 content_t CNodeDefManager::allocateId()
616 {
617         for (content_t id = m_next_id;
618                         id >= m_next_id; // overflow?
619                         ++id) {
620                 while (id >= m_content_features.size()) {
621                         m_content_features.push_back(ContentFeatures());
622                 }
623                 const ContentFeatures &f = m_content_features[id];
624                 if (f.name == "") {
625                         m_next_id = id + 1;
626                         return id;
627                 }
628         }
629         // If we arrive here, an overflow occurred in id.
630         // That means no ID was found
631         return CONTENT_IGNORE;
632 }
633
634
635 // IWritableNodeDefManager
636 content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
637 {
638         // Pre-conditions
639         assert(name != "");
640         assert(name == def.name);
641
642         // Don't allow redefining ignore (but allow air and unknown)
643         if (name == "ignore") {
644                 infostream << "NodeDefManager: WARNING: Ignoring "
645                         "CONTENT_IGNORE redefinition"<<std::endl;
646                 return CONTENT_IGNORE;
647         }
648
649         content_t id = CONTENT_IGNORE;
650         if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
651                 // Get new id
652                 id = allocateId();
653                 if (id == CONTENT_IGNORE) {
654                         infostream << "NodeDefManager: WARNING: Absolute "
655                                 "limit reached" << std::endl;
656                         return CONTENT_IGNORE;
657                 }
658                 assert(id != CONTENT_IGNORE);
659                 addNameIdMapping(id, name);
660         }
661         m_content_features[id] = def;
662         verbosestream << "NodeDefManager: registering content id \"" << id
663                 << "\": name=\"" << def.name << "\""<<std::endl;
664
665         // Add this content to the list of all groups it belongs to
666         // FIXME: This should remove a node from groups it no longer
667         // belongs to when a node is re-registered
668         for (ItemGroupList::const_iterator i = def.groups.begin();
669                 i != def.groups.end(); ++i) {
670                 std::string group_name = i->first;
671
672                 std::map<std::string, GroupItems>::iterator
673                         j = m_group_to_items.find(group_name);
674                 if (j == m_group_to_items.end()) {
675                         m_group_to_items[group_name].push_back(
676                                 std::make_pair(id, i->second));
677                 } else {
678                         GroupItems &items = j->second;
679                         items.push_back(std::make_pair(id, i->second));
680                 }
681         }
682         return id;
683 }
684
685
686 content_t CNodeDefManager::allocateDummy(const std::string &name)
687 {
688         assert(name != "");     // Pre-condition
689         ContentFeatures f;
690         f.name = name;
691         return set(name, f);
692 }
693
694
695 void CNodeDefManager::updateAliases(IItemDefManager *idef)
696 {
697         std::set<std::string> all = idef->getAll();
698         m_name_id_mapping_with_aliases.clear();
699         for (std::set<std::string>::iterator
700                         i = all.begin(); i != all.end(); i++) {
701                 std::string name = *i;
702                 std::string convert_to = idef->getAlias(name);
703                 content_t id;
704                 if (m_name_id_mapping.getId(convert_to, id)) {
705                         m_name_id_mapping_with_aliases.insert(
706                                 std::make_pair(name, id));
707                 }
708         }
709 }
710
711 void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath)
712 {
713         infostream << "CNodeDefManager::applyTextureOverrides(): Applying "
714                 "overrides to textures from " << override_filepath << std::endl;
715
716         std::ifstream infile(override_filepath.c_str());
717         std::string line;
718         int line_c = 0;
719         while (std::getline(infile, line)) {
720                 line_c++;
721                 if (trim(line) == "")
722                         continue;
723                 std::vector<std::string> splitted = str_split(line, ' ');
724                 if (splitted.size() != 3) {
725                         errorstream << override_filepath
726                                 << ":" << line_c << " Could not apply texture override \""
727                                 << line << "\": Syntax error" << std::endl;
728                         continue;
729                 }
730
731                 content_t id;
732                 if (!getId(splitted[0], id)) {
733                         errorstream << override_filepath
734                                 << ":" << line_c << " Could not apply texture override \""
735                                 << line << "\": Unknown node \""
736                                 << splitted[0] << "\"" << std::endl;
737                         continue;
738                 }
739
740                 ContentFeatures &nodedef = m_content_features[id];
741
742                 if (splitted[1] == "top")
743                         nodedef.tiledef[0].name = splitted[2];
744                 else if (splitted[1] == "bottom")
745                         nodedef.tiledef[1].name = splitted[2];
746                 else if (splitted[1] == "right")
747                         nodedef.tiledef[2].name = splitted[2];
748                 else if (splitted[1] == "left")
749                         nodedef.tiledef[3].name = splitted[2];
750                 else if (splitted[1] == "back")
751                         nodedef.tiledef[4].name = splitted[2];
752                 else if (splitted[1] == "front")
753                         nodedef.tiledef[5].name = splitted[2];
754                 else if (splitted[1] == "all" || splitted[1] == "*")
755                         for (int i = 0; i < 6; i++)
756                                 nodedef.tiledef[i].name = splitted[2];
757                 else if (splitted[1] == "sides")
758                         for (int i = 2; i < 6; i++)
759                                 nodedef.tiledef[i].name = splitted[2];
760                 else {
761                         errorstream << override_filepath
762                                 << ":" << line_c << " Could not apply texture override \""
763                                 << line << "\": Unknown node side \""
764                                 << splitted[1] << "\"" << std::endl;
765                         continue;
766                 }
767         }
768 }
769
770 void CNodeDefManager::updateTextures(IGameDef *gamedef,
771         void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
772         void *progress_callback_args)
773 {
774 #ifndef SERVER
775         infostream << "CNodeDefManager::updateTextures(): Updating "
776                 "textures in node definitions" << std::endl;
777         ITextureSource *tsrc = gamedef->tsrc();
778         IShaderSource *shdsrc = gamedef->getShaderSource();
779         scene::ISceneManager* smgr = gamedef->getSceneManager();
780         scene::IMeshManipulator* meshmanip = smgr->getMeshManipulator();
781
782         bool new_style_water           = g_settings->getBool("new_style_water");
783         bool new_style_leaves          = g_settings->getBool("new_style_leaves");
784         bool connected_glass           = g_settings->getBool("connected_glass");
785         bool opaque_water              = g_settings->getBool("opaque_water");
786         bool enable_shaders            = g_settings->getBool("enable_shaders");
787         bool enable_bumpmapping        = g_settings->getBool("enable_bumpmapping");
788         bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
789         bool enable_mesh_cache         = g_settings->getBool("enable_mesh_cache");
790
791         bool use_normal_texture = enable_shaders &&
792                 (enable_bumpmapping || enable_parallax_occlusion);
793
794         u32 size = m_content_features.size();
795
796         for (u32 i = 0; i < size; i++) {
797                 ContentFeatures *f = &m_content_features[i];
798
799                 // minimap pixel color - the average color of a texture
800                 if (f->tiledef[0].name != "")
801                         f->minimap_color = tsrc->getTextureAverageColor(f->tiledef[0].name);
802
803                 // Figure out the actual tiles to use
804                 TileDef tiledef[6];
805                 for (u32 j = 0; j < 6; j++) {
806                         tiledef[j] = f->tiledef[j];
807                         if (tiledef[j].name == "")
808                                 tiledef[j].name = "unknown_node.png";
809                 }
810
811                 bool is_liquid = false;
812                 bool is_water_surface = false;
813
814                 u8 material_type = (f->alpha == 255) ?
815                         TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
816
817                 switch (f->drawtype) {
818                 default:
819                 case NDT_NORMAL:
820                         f->solidness = 2;
821                         break;
822                 case NDT_AIRLIKE:
823                         f->solidness = 0;
824                         break;
825                 case NDT_LIQUID:
826                         assert(f->liquid_type == LIQUID_SOURCE);
827                         if (opaque_water)
828                                 f->alpha = 255;
829                         if (new_style_water){
830                                 f->solidness = 0;
831                         } else {
832                                 f->solidness = 1;
833                                 f->backface_culling = false;
834                         }
835                         is_liquid = true;
836                         break;
837                 case NDT_FLOWINGLIQUID:
838                         assert(f->liquid_type == LIQUID_FLOWING);
839                         f->solidness = 0;
840                         if (opaque_water)
841                                 f->alpha = 255;
842                         is_liquid = true;
843                         break;
844                 case NDT_GLASSLIKE:
845                         f->solidness = 0;
846                         f->visual_solidness = 1;
847                         break;
848                 case NDT_GLASSLIKE_FRAMED:
849                         f->solidness = 0;
850                         f->visual_solidness = 1;
851                         break;
852                 case NDT_GLASSLIKE_FRAMED_OPTIONAL:
853                         f->solidness = 0;
854                         f->visual_solidness = 1;
855                         f->drawtype = connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
856                         break;
857                 case NDT_ALLFACES:
858                         f->solidness = 0;
859                         f->visual_solidness = 1;
860                         break;
861                 case NDT_ALLFACES_OPTIONAL:
862                         if (new_style_leaves) {
863                                 f->drawtype = NDT_ALLFACES;
864                                 f->solidness = 0;
865                                 f->visual_solidness = 1;
866                         } else {
867                                 f->drawtype = NDT_NORMAL;
868                                 f->solidness = 2;
869                                 for (u32 i = 0; i < 6; i++)
870                                         tiledef[i].name += std::string("^[noalpha");
871                         }
872                         if (f->waving == 1)
873                                 material_type = TILE_MATERIAL_WAVING_LEAVES;
874                         break;
875                 case NDT_PLANTLIKE:
876                         f->solidness = 0;
877                         f->backface_culling = false;
878                         if (f->waving == 1)
879                                 material_type = TILE_MATERIAL_WAVING_PLANTS;
880                         break;
881                 case NDT_FIRELIKE:
882                         f->backface_culling = false;
883                         f->solidness = 0;
884                         break;
885                 case NDT_MESH:
886                         f->solidness = 0;
887                         f->backface_culling = false;
888                         break;
889                 case NDT_TORCHLIKE:
890                 case NDT_SIGNLIKE:
891                 case NDT_FENCELIKE:
892                 case NDT_RAILLIKE:
893                 case NDT_NODEBOX:
894                         f->solidness = 0;
895                         break;
896                 }
897
898                 if (is_liquid) {
899                         material_type = (f->alpha == 255) ?
900                                 TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
901                         if (f->name == "default:water_source")
902                                 is_water_surface = true;
903                 }
904
905                 u32 tile_shader[6];
906                 for (u16 j = 0; j < 6; j++) {
907                         tile_shader[j] = shdsrc->getShader("nodes_shader",
908                                 material_type, f->drawtype);
909                 }
910
911                 if (is_water_surface) {
912                         tile_shader[0] = shdsrc->getShader("water_surface_shader",
913                                 material_type, f->drawtype);
914                 }
915
916                 // Tiles (fill in f->tiles[])
917                 for (u16 j = 0; j < 6; j++) {
918                         fillTileAttribs(tsrc, &f->tiles[j], &tiledef[j], tile_shader[j],
919                                 use_normal_texture, f->backface_culling, f->alpha, material_type);
920                 }
921
922                 // Special tiles (fill in f->special_tiles[])
923                 for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
924                         fillTileAttribs(tsrc, &f->special_tiles[j], &f->tiledef_special[j],
925                                 tile_shader[j], use_normal_texture,
926                                 f->tiledef_special[j].backface_culling, f->alpha, material_type);
927                 }
928
929                 if ((f->drawtype == NDT_MESH) && (f->mesh != "")) {
930                         // Meshnode drawtype
931                         // Read the mesh and apply scale
932                         f->mesh_ptr[0] = gamedef->getMesh(f->mesh);
933                         if (f->mesh_ptr[0]){
934                                 v3f scale = v3f(1.0, 1.0, 1.0) * BS * f->visual_scale;
935                                 scaleMesh(f->mesh_ptr[0], scale);
936                                 recalculateBoundingBox(f->mesh_ptr[0]);
937                                 meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
938                         }
939                 } else if ((f->drawtype == NDT_NODEBOX) &&
940                                 ((f->node_box.type == NODEBOX_REGULAR) ||
941                                 (f->node_box.type == NODEBOX_FIXED)) &&
942                                 (!f->node_box.fixed.empty())) {
943                         //Convert regular nodebox nodes to meshnodes
944                         //Change the drawtype and apply scale
945                         f->drawtype = NDT_MESH;
946                         f->mesh_ptr[0] = convertNodeboxNodeToMesh(f);
947                         v3f scale = v3f(1.0, 1.0, 1.0) * f->visual_scale;
948                         scaleMesh(f->mesh_ptr[0], scale);
949                         recalculateBoundingBox(f->mesh_ptr[0]);
950                         meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
951                 }
952
953                 //Cache 6dfacedir and wallmounted rotated clones of meshes
954                 if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_FACEDIR)) {
955                         for (u16 j = 1; j < 24; j++) {
956                                 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
957                                 rotateMeshBy6dFacedir(f->mesh_ptr[j], j);
958                                 recalculateBoundingBox(f->mesh_ptr[j]);
959                                 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
960                         }
961                 } else if (enable_mesh_cache && f->mesh_ptr[0] && (f->param_type_2 == CPT2_WALLMOUNTED)) {
962                         static const u8 wm_to_6d[6] = {20, 0, 16+1, 12+3, 8, 4+2};
963                         for (u16 j = 1; j < 6; j++) {
964                                 f->mesh_ptr[j] = cloneMesh(f->mesh_ptr[0]);
965                                 rotateMeshBy6dFacedir(f->mesh_ptr[j], wm_to_6d[j]);
966                                 recalculateBoundingBox(f->mesh_ptr[j]);
967                                 meshmanip->recalculateNormals(f->mesh_ptr[j], true, false);
968                         }
969                         rotateMeshBy6dFacedir(f->mesh_ptr[0], wm_to_6d[0]);
970                         recalculateBoundingBox(f->mesh_ptr[0]);
971                         meshmanip->recalculateNormals(f->mesh_ptr[0], true, false);
972                 }
973
974                 progress_callback(progress_callback_args, i, size);
975         }
976 #endif
977 }
978
979
980 #ifndef SERVER
981 void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
982                 TileDef *tiledef, u32 shader_id, bool use_normal_texture,
983                 bool backface_culling, u8 alpha, u8 material_type)
984 {
985         tile->shader_id     = shader_id;
986         tile->texture       = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
987         tile->alpha         = alpha;
988         tile->material_type = material_type;
989
990         // Normal texture
991         if (use_normal_texture)
992                 tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
993
994         // Material flags
995         tile->material_flags = 0;
996         if (backface_culling)
997                 tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
998         if (tiledef->animation.type == TAT_VERTICAL_FRAMES)
999                 tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1000
1001         // Animation parameters
1002         int frame_count = 1;
1003         if (tile->material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
1004                 // Get texture size to determine frame count by aspect ratio
1005                 v2u32 size = tile->texture->getOriginalSize();
1006                 int frame_height = (float)size.X /
1007                                 (float)tiledef->animation.aspect_w *
1008                                 (float)tiledef->animation.aspect_h;
1009                 frame_count = size.Y / frame_height;
1010                 int frame_length_ms = 1000.0 * tiledef->animation.length / frame_count;
1011                 tile->animation_frame_count = frame_count;
1012                 tile->animation_frame_length_ms = frame_length_ms;
1013         }
1014
1015         if (frame_count == 1) {
1016                 tile->material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
1017         } else {
1018                 std::ostringstream os(std::ios::binary);
1019                 tile->frames.resize(frame_count);
1020
1021                 for (int i = 0; i < frame_count; i++) {
1022
1023                         FrameSpec frame;
1024
1025                         os.str("");
1026                         os << tiledef->name << "^[verticalframe:"
1027                                 << frame_count << ":" << i;
1028
1029                         frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
1030                         if (tile->normal_texture)
1031                                 frame.normal_texture = tsrc->getNormalTexture(os.str());
1032                         tile->frames[i] = frame;
1033                 }
1034         }
1035 }
1036 #endif
1037
1038
1039 void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
1040 {
1041         writeU8(os, 1); // version
1042         u16 count = 0;
1043         std::ostringstream os2(std::ios::binary);
1044         for (u32 i = 0; i < m_content_features.size(); i++) {
1045                 if (i == CONTENT_IGNORE || i == CONTENT_AIR
1046                                 || i == CONTENT_UNKNOWN)
1047                         continue;
1048                 const ContentFeatures *f = &m_content_features[i];
1049                 if (f->name == "")
1050                         continue;
1051                 writeU16(os2, i);
1052                 // Wrap it in a string to allow different lengths without
1053                 // strict version incompatibilities
1054                 std::ostringstream wrapper_os(std::ios::binary);
1055                 f->serialize(wrapper_os, protocol_version);
1056                 os2<<serializeString(wrapper_os.str());
1057
1058                 // must not overflow
1059                 u16 next = count + 1;
1060                 FATAL_ERROR_IF(next < count, "Overflow");
1061                 count++;
1062         }
1063         writeU16(os, count);
1064         os << serializeLongString(os2.str());
1065 }
1066
1067
1068 void CNodeDefManager::deSerialize(std::istream &is)
1069 {
1070         clear();
1071         int version = readU8(is);
1072         if (version != 1)
1073                 throw SerializationError("unsupported NodeDefinitionManager version");
1074         u16 count = readU16(is);
1075         std::istringstream is2(deSerializeLongString(is), std::ios::binary);
1076         ContentFeatures f;
1077         for (u16 n = 0; n < count; n++) {
1078                 u16 i = readU16(is2);
1079
1080                 // Read it from the string wrapper
1081                 std::string wrapper = deSerializeString(is2);
1082                 std::istringstream wrapper_is(wrapper, std::ios::binary);
1083                 f.deSerialize(wrapper_is);
1084
1085                 // Check error conditions
1086                 if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
1087                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1088                                 "not changing builtin node " << i << std::endl;
1089                         continue;
1090                 }
1091                 if (f.name == "") {
1092                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1093                                 "received empty name" << std::endl;
1094                         continue;
1095                 }
1096
1097                 // Ignore aliases
1098                 u16 existing_id;
1099                 if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
1100                         infostream << "NodeDefManager::deSerialize(): WARNING: "
1101                                 "already defined with different ID: " << f.name << std::endl;
1102                         continue;
1103                 }
1104
1105                 // All is ok, add node definition with the requested ID
1106                 if (i >= m_content_features.size())
1107                         m_content_features.resize((u32)(i) + 1);
1108                 m_content_features[i] = f;
1109                 addNameIdMapping(i, f.name);
1110                 verbosestream << "deserialized " << f.name << std::endl;
1111         }
1112 }
1113
1114
1115 void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
1116 {
1117         m_name_id_mapping.set(i, name);
1118         m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1119 }
1120
1121
1122 IWritableNodeDefManager *createNodeDefManager()
1123 {
1124         return new CNodeDefManager();
1125 }
1126
1127
1128 //// Serialization of old ContentFeatures formats
1129 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
1130 {
1131         if (protocol_version == 13)
1132         {
1133                 writeU8(os, 5); // version
1134                 os<<serializeString(name);
1135                 writeU16(os, groups.size());
1136                 for (ItemGroupList::const_iterator
1137                                 i = groups.begin(); i != groups.end(); i++) {
1138                         os<<serializeString(i->first);
1139                         writeS16(os, i->second);
1140                 }
1141                 writeU8(os, drawtype);
1142                 writeF1000(os, visual_scale);
1143                 writeU8(os, 6);
1144                 for (u32 i = 0; i < 6; i++)
1145                         tiledef[i].serialize(os, protocol_version);
1146                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1147                 writeU8(os, 2);
1148                 for (u32 i = 0; i < 2; i++)
1149                         tiledef_special[i].serialize(os, protocol_version);
1150                 writeU8(os, alpha);
1151                 writeU8(os, post_effect_color.getAlpha());
1152                 writeU8(os, post_effect_color.getRed());
1153                 writeU8(os, post_effect_color.getGreen());
1154                 writeU8(os, post_effect_color.getBlue());
1155                 writeU8(os, param_type);
1156                 writeU8(os, param_type_2);
1157                 writeU8(os, is_ground_content);
1158                 writeU8(os, light_propagates);
1159                 writeU8(os, sunlight_propagates);
1160                 writeU8(os, walkable);
1161                 writeU8(os, pointable);
1162                 writeU8(os, diggable);
1163                 writeU8(os, climbable);
1164                 writeU8(os, buildable_to);
1165                 os<<serializeString(""); // legacy: used to be metadata_name
1166                 writeU8(os, liquid_type);
1167                 os<<serializeString(liquid_alternative_flowing);
1168                 os<<serializeString(liquid_alternative_source);
1169                 writeU8(os, liquid_viscosity);
1170                 writeU8(os, light_source);
1171                 writeU32(os, damage_per_second);
1172                 node_box.serialize(os, protocol_version);
1173                 selection_box.serialize(os, protocol_version);
1174                 writeU8(os, legacy_facedir_simple);
1175                 writeU8(os, legacy_wallmounted);
1176                 serializeSimpleSoundSpec(sound_footstep, os);
1177                 serializeSimpleSoundSpec(sound_dig, os);
1178                 serializeSimpleSoundSpec(sound_dug, os);
1179         }
1180         else if (protocol_version > 13 && protocol_version < 24) {
1181                 writeU8(os, 6); // version
1182                 os<<serializeString(name);
1183                 writeU16(os, groups.size());
1184                 for (ItemGroupList::const_iterator
1185                         i = groups.begin(); i != groups.end(); i++) {
1186                                 os<<serializeString(i->first);
1187                                 writeS16(os, i->second);
1188                 }
1189                 writeU8(os, drawtype);
1190                 writeF1000(os, visual_scale);
1191                 writeU8(os, 6);
1192                 for (u32 i = 0; i < 6; i++)
1193                         tiledef[i].serialize(os, protocol_version);
1194                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
1195                 writeU8(os, 2);
1196                 for (u32 i = 0; i < 2; i++)
1197                         tiledef_special[i].serialize(os, protocol_version);
1198                 writeU8(os, alpha);
1199                 writeU8(os, post_effect_color.getAlpha());
1200                 writeU8(os, post_effect_color.getRed());
1201                 writeU8(os, post_effect_color.getGreen());
1202                 writeU8(os, post_effect_color.getBlue());
1203                 writeU8(os, param_type);
1204                 writeU8(os, param_type_2);
1205                 writeU8(os, is_ground_content);
1206                 writeU8(os, light_propagates);
1207                 writeU8(os, sunlight_propagates);
1208                 writeU8(os, walkable);
1209                 writeU8(os, pointable);
1210                 writeU8(os, diggable);
1211                 writeU8(os, climbable);
1212                 writeU8(os, buildable_to);
1213                 os<<serializeString(""); // legacy: used to be metadata_name
1214                 writeU8(os, liquid_type);
1215                 os<<serializeString(liquid_alternative_flowing);
1216                 os<<serializeString(liquid_alternative_source);
1217                 writeU8(os, liquid_viscosity);
1218                 writeU8(os, liquid_renewable);
1219                 writeU8(os, light_source);
1220                 writeU32(os, damage_per_second);
1221                 node_box.serialize(os, protocol_version);
1222                 selection_box.serialize(os, protocol_version);
1223                 writeU8(os, legacy_facedir_simple);
1224                 writeU8(os, legacy_wallmounted);
1225                 serializeSimpleSoundSpec(sound_footstep, os);
1226                 serializeSimpleSoundSpec(sound_dig, os);
1227                 serializeSimpleSoundSpec(sound_dug, os);
1228                 writeU8(os, rightclickable);
1229                 writeU8(os, drowning);
1230                 writeU8(os, leveled);
1231                 writeU8(os, liquid_range);
1232         } else
1233                 throw SerializationError("ContentFeatures::serialize(): "
1234                         "Unsupported version requested");
1235 }
1236
1237
1238 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1239 {
1240         if (version == 5) // In PROTOCOL_VERSION 13
1241         {
1242                 name = deSerializeString(is);
1243                 groups.clear();
1244                 u32 groups_size = readU16(is);
1245                 for(u32 i=0; i<groups_size; i++){
1246                         std::string name = deSerializeString(is);
1247                         int value = readS16(is);
1248                         groups[name] = value;
1249                 }
1250                 drawtype = (enum NodeDrawType)readU8(is);
1251                 visual_scale = readF1000(is);
1252                 if (readU8(is) != 6)
1253                         throw SerializationError("unsupported tile count");
1254                 for (u32 i = 0; i < 6; i++)
1255                         tiledef[i].deSerialize(is);
1256                 if (readU8(is) != CF_SPECIAL_COUNT)
1257                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1258                 for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
1259                         tiledef_special[i].deSerialize(is);
1260                 alpha = readU8(is);
1261                 post_effect_color.setAlpha(readU8(is));
1262                 post_effect_color.setRed(readU8(is));
1263                 post_effect_color.setGreen(readU8(is));
1264                 post_effect_color.setBlue(readU8(is));
1265                 param_type = (enum ContentParamType)readU8(is);
1266                 param_type_2 = (enum ContentParamType2)readU8(is);
1267                 is_ground_content = readU8(is);
1268                 light_propagates = readU8(is);
1269                 sunlight_propagates = readU8(is);
1270                 walkable = readU8(is);
1271                 pointable = readU8(is);
1272                 diggable = readU8(is);
1273                 climbable = readU8(is);
1274                 buildable_to = readU8(is);
1275                 deSerializeString(is); // legacy: used to be metadata_name
1276                 liquid_type = (enum LiquidType)readU8(is);
1277                 liquid_alternative_flowing = deSerializeString(is);
1278                 liquid_alternative_source = deSerializeString(is);
1279                 liquid_viscosity = readU8(is);
1280                 light_source = readU8(is);
1281                 damage_per_second = readU32(is);
1282                 node_box.deSerialize(is);
1283                 selection_box.deSerialize(is);
1284                 legacy_facedir_simple = readU8(is);
1285                 legacy_wallmounted = readU8(is);
1286                 deSerializeSimpleSoundSpec(sound_footstep, is);
1287                 deSerializeSimpleSoundSpec(sound_dig, is);
1288                 deSerializeSimpleSoundSpec(sound_dug, is);
1289         } else if (version == 6) {
1290                 name = deSerializeString(is);
1291                 groups.clear();
1292                 u32 groups_size = readU16(is);
1293                 for (u32 i = 0; i < groups_size; i++) {
1294                         std::string name = deSerializeString(is);
1295                         int     value = readS16(is);
1296                         groups[name] = value;
1297                 }
1298                 drawtype = (enum NodeDrawType)readU8(is);
1299                 visual_scale = readF1000(is);
1300                 if (readU8(is) != 6)
1301                         throw SerializationError("unsupported tile count");
1302                 for (u32 i = 0; i < 6; i++)
1303                         tiledef[i].deSerialize(is);
1304                 // CF_SPECIAL_COUNT in version 6 = 2
1305                 if (readU8(is) != 2)
1306                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1307                 for (u32 i = 0; i < 2; i++)
1308                         tiledef_special[i].deSerialize(is);
1309                 alpha = readU8(is);
1310                 post_effect_color.setAlpha(readU8(is));
1311                 post_effect_color.setRed(readU8(is));
1312                 post_effect_color.setGreen(readU8(is));
1313                 post_effect_color.setBlue(readU8(is));
1314                 param_type = (enum ContentParamType)readU8(is);
1315                 param_type_2 = (enum ContentParamType2)readU8(is);
1316                 is_ground_content = readU8(is);
1317                 light_propagates = readU8(is);
1318                 sunlight_propagates = readU8(is);
1319                 walkable = readU8(is);
1320                 pointable = readU8(is);
1321                 diggable = readU8(is);
1322                 climbable = readU8(is);
1323                 buildable_to = readU8(is);
1324                 deSerializeString(is); // legacy: used to be metadata_name
1325                 liquid_type = (enum LiquidType)readU8(is);
1326                 liquid_alternative_flowing = deSerializeString(is);
1327                 liquid_alternative_source = deSerializeString(is);
1328                 liquid_viscosity = readU8(is);
1329                 liquid_renewable = readU8(is);
1330                 light_source = readU8(is);
1331                 damage_per_second = readU32(is);
1332                 node_box.deSerialize(is);
1333                 selection_box.deSerialize(is);
1334                 legacy_facedir_simple = readU8(is);
1335                 legacy_wallmounted = readU8(is);
1336                 deSerializeSimpleSoundSpec(sound_footstep, is);
1337                 deSerializeSimpleSoundSpec(sound_dig, is);
1338                 deSerializeSimpleSoundSpec(sound_dug, is);
1339                 rightclickable = readU8(is);
1340                 drowning = readU8(is);
1341                 leveled = readU8(is);
1342                 liquid_range = readU8(is);
1343         } else {
1344                 throw SerializationError("unsupported ContentFeatures version");
1345         }
1346 }
1347
1348
1349 inline bool CNodeDefManager::getNodeRegistrationStatus() const
1350 {
1351         return m_node_registration_complete;
1352 }
1353
1354
1355 inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
1356 {
1357         m_node_registration_complete = completed;
1358 }
1359
1360
1361 void CNodeDefManager::pendNodeResolve(NodeResolver *nr)
1362 {
1363         nr->m_ndef = this;
1364         if (m_node_registration_complete)
1365                 nr->nodeResolveInternal();
1366         else
1367                 m_pending_resolve_callbacks.push_back(nr);
1368 }
1369
1370
1371 bool CNodeDefManager::cancelNodeResolveCallback(NodeResolver *nr)
1372 {
1373         size_t len = m_pending_resolve_callbacks.size();
1374         for (size_t i = 0; i != len; i++) {
1375                 if (nr != m_pending_resolve_callbacks[i])
1376                         continue;
1377
1378                 len--;
1379                 m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
1380                 m_pending_resolve_callbacks.resize(len);
1381                 return true;
1382         }
1383
1384         return false;
1385 }
1386
1387
1388 void CNodeDefManager::runNodeResolveCallbacks()
1389 {
1390         for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
1391                 NodeResolver *nr = m_pending_resolve_callbacks[i];
1392                 nr->nodeResolveInternal();
1393         }
1394
1395         m_pending_resolve_callbacks.clear();
1396 }
1397
1398
1399 void CNodeDefManager::resetNodeResolveState()
1400 {
1401         m_node_registration_complete = false;
1402         m_pending_resolve_callbacks.clear();
1403 }
1404
1405
1406 ////
1407 //// NodeResolver
1408 ////
1409
1410 NodeResolver::NodeResolver()
1411 {
1412         m_ndef            = NULL;
1413         m_nodenames_idx   = 0;
1414         m_nnlistsizes_idx = 0;
1415         m_resolve_done    = false;
1416
1417         m_nodenames.reserve(16);
1418         m_nnlistsizes.reserve(4);
1419 }
1420
1421
1422 NodeResolver::~NodeResolver()
1423 {
1424         if (!m_resolve_done && m_ndef)
1425                 m_ndef->cancelNodeResolveCallback(this);
1426 }
1427
1428
1429 void NodeResolver::nodeResolveInternal()
1430 {
1431         m_nodenames_idx   = 0;
1432         m_nnlistsizes_idx = 0;
1433
1434         resolveNodeNames();
1435         m_resolve_done = true;
1436
1437         m_nodenames.clear();
1438         m_nnlistsizes.clear();
1439 }
1440
1441
1442 bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
1443         const std::string &node_alt, content_t c_fallback)
1444 {
1445         if (m_nodenames_idx == m_nodenames.size()) {
1446                 *result_out = c_fallback;
1447                 errorstream << "NodeResolver: no more nodes in list" << std::endl;
1448                 return false;
1449         }
1450
1451         content_t c;
1452         std::string name = m_nodenames[m_nodenames_idx++];
1453
1454         bool success = m_ndef->getId(name, c);
1455         if (!success && node_alt != "") {
1456                 name = node_alt;
1457                 success = m_ndef->getId(name, c);
1458         }
1459
1460         if (!success) {
1461                 errorstream << "NodeResolver: failed to resolve node name '" << name
1462                         << "'." << std::endl;
1463                 c = c_fallback;
1464         }
1465
1466         *result_out = c;
1467         return success;
1468 }
1469
1470
1471 bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
1472         bool all_required, content_t c_fallback)
1473 {
1474         bool success = true;
1475
1476         if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
1477                 errorstream << "NodeResolver: no more node lists" << std::endl;
1478                 return false;
1479         }
1480
1481         size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
1482
1483         while (length--) {
1484                 if (m_nodenames_idx == m_nodenames.size()) {
1485                         errorstream << "NodeResolver: no more nodes in list" << std::endl;
1486                         return false;
1487                 }
1488
1489                 content_t c;
1490                 std::string &name = m_nodenames[m_nodenames_idx++];
1491
1492                 if (name.substr(0,6) != "group:") {
1493                         if (m_ndef->getId(name, c)) {
1494                                 result_out->push_back(c);
1495                         } else if (all_required) {
1496                                 errorstream << "NodeResolver: failed to resolve node name '"
1497                                         << name << "'." << std::endl;
1498                                 result_out->push_back(c_fallback);
1499                                 success = false;
1500                         }
1501                 } else {
1502                         std::set<content_t> cids;
1503                         std::set<content_t>::iterator it;
1504                         m_ndef->getIds(name, cids);
1505                         for (it = cids.begin(); it != cids.end(); ++it)
1506                                 result_out->push_back(*it);
1507                 }
1508         }
1509
1510         return success;
1511 }