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