Fix totally messed up 3d modes interlaced/topbottom/sidebyside
[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 {
375 public:
376         void clear()
377         {
378                 m_content_features.clear();
379                 m_name_id_mapping.clear();
380                 m_name_id_mapping_with_aliases.clear();
381                 m_group_to_items.clear();
382                 m_next_id = 0;
383
384                 u32 initial_length = 0;
385                 initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
386                 initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
387                 initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
388                 m_content_features.resize(initial_length);
389
390                 // Set CONTENT_UNKNOWN
391                 {
392                         ContentFeatures f;
393                         f.name = "unknown";
394                         // Insert directly into containers
395                         content_t c = CONTENT_UNKNOWN;
396                         m_content_features[c] = f;
397                         addNameIdMapping(c, f.name);
398                 }
399
400                 // Set CONTENT_AIR
401                 {
402                         ContentFeatures f;
403                         f.name                = "air";
404                         f.drawtype            = NDT_AIRLIKE;
405                         f.param_type          = CPT_LIGHT;
406                         f.light_propagates    = true;
407                         f.sunlight_propagates = true;
408                         f.walkable            = false;
409                         f.pointable           = false;
410                         f.diggable            = false;
411                         f.buildable_to        = true;
412                         f.is_ground_content   = true;
413                         // Insert directly into containers
414                         content_t c = CONTENT_AIR;
415                         m_content_features[c] = f;
416                         addNameIdMapping(c, f.name);
417                 }
418
419                 // Set CONTENT_IGNORE
420                 {
421                         ContentFeatures f;
422                         f.name                = "ignore";
423                         f.drawtype            = NDT_AIRLIKE;
424                         f.param_type          = CPT_NONE;
425                         f.light_propagates    = false;
426                         f.sunlight_propagates = false;
427                         f.walkable            = false;
428                         f.pointable           = false;
429                         f.diggable            = false;
430                         f.buildable_to        = true; // A way to remove accidental CONTENT_IGNOREs
431                         f.is_ground_content   = true;
432                         // Insert directly into containers
433                         content_t c = CONTENT_IGNORE;
434                         m_content_features[c] = f;
435                         addNameIdMapping(c, f.name);
436                 }
437         }
438         CNodeDefManager()
439         {
440                 clear();
441         }
442         virtual ~CNodeDefManager()
443         {
444         }
445         virtual IWritableNodeDefManager* clone()
446         {
447                 CNodeDefManager *mgr = new CNodeDefManager();
448                 *mgr = *this;
449                 return mgr;
450         }
451         virtual const ContentFeatures& get(content_t c) const
452         {
453                 if(c < m_content_features.size())
454                         return m_content_features[c];
455                 else
456                         return m_content_features[CONTENT_UNKNOWN];
457         }
458         virtual const ContentFeatures& get(const MapNode &n) const
459         {
460                 return get(n.getContent());
461         }
462         virtual bool getId(const std::string &name, content_t &result) const
463         {
464                 std::map<std::string, content_t>::const_iterator
465                         i = m_name_id_mapping_with_aliases.find(name);
466                 if(i == m_name_id_mapping_with_aliases.end())
467                         return false;
468                 result = i->second;
469                 return true;
470         }
471         virtual content_t getId(const std::string &name) const
472         {
473                 content_t id = CONTENT_IGNORE;
474                 getId(name, id);
475                 return id;
476         }
477         virtual void getIds(const std::string &name, std::set<content_t> &result)
478                         const
479         {
480                 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
481                 if(name.substr(0,6) != "group:"){
482                         content_t id = CONTENT_IGNORE;
483                         if(getId(name, id))
484                                 result.insert(id);
485                         return;
486                 }
487                 std::string group = name.substr(6);
488
489                 std::map<std::string, GroupItems>::const_iterator
490                         i = m_group_to_items.find(group);
491                 if (i == m_group_to_items.end())
492                         return;
493
494                 const GroupItems &items = i->second;
495                 for (GroupItems::const_iterator j = items.begin();
496                         j != items.end(); ++j) {
497                         if ((*j).second != 0)
498                                 result.insert((*j).first);
499                 }
500                 //printf("getIds: %dus\n", t.stop());
501         }
502         virtual const ContentFeatures& get(const std::string &name) const
503         {
504                 content_t id = CONTENT_UNKNOWN;
505                 getId(name, id);
506                 return get(id);
507         }
508         // returns CONTENT_IGNORE if no free ID found
509         content_t allocateId()
510         {
511                 for(content_t id = m_next_id;
512                                 id >= m_next_id; // overflow?
513                                 ++id){
514                         while(id >= m_content_features.size()){
515                                 m_content_features.push_back(ContentFeatures());
516                         }
517                         const ContentFeatures &f = m_content_features[id];
518                         if(f.name == ""){
519                                 m_next_id = id + 1;
520                                 return id;
521                         }
522                 }
523                 // If we arrive here, an overflow occurred in id.
524                 // That means no ID was found
525                 return CONTENT_IGNORE;
526         }
527         // IWritableNodeDefManager
528         virtual content_t set(const std::string &name,
529                         const ContentFeatures &def)
530         {
531                 assert(name != "");
532                 assert(name == def.name);
533
534                 // Don't allow redefining ignore (but allow air and unknown)
535                 if(name == "ignore"){
536                         infostream<<"NodeDefManager: WARNING: Ignoring "
537                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
538                         return CONTENT_IGNORE;
539                 }
540
541                 content_t id = CONTENT_IGNORE;
542                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
543                 if(!found){
544                         // Get new id
545                         id = allocateId();
546                         if(id == CONTENT_IGNORE){
547                                 infostream<<"NodeDefManager: WARNING: Absolute "
548                                                 <<"limit reached"<<std::endl;
549                                 return CONTENT_IGNORE;
550                         }
551                         assert(id != CONTENT_IGNORE);
552                         addNameIdMapping(id, name);
553                 }
554                 m_content_features[id] = def;
555                 verbosestream<<"NodeDefManager: registering content id \""<<id
556                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
557
558                 // Add this content to the list of all groups it belongs to
559                 // FIXME: This should remove a node from groups it no longer
560                 // belongs to when a node is re-registered
561                 for (ItemGroupList::const_iterator i = def.groups.begin();
562                         i != def.groups.end(); ++i) {
563                         std::string group_name = i->first;
564
565                         std::map<std::string, GroupItems>::iterator
566                                 j = m_group_to_items.find(group_name);
567                         if (j == m_group_to_items.end()) {
568                                 m_group_to_items[group_name].push_back(
569                                                 std::make_pair(id, i->second));
570                         } else {
571                                 GroupItems &items = j->second;
572                                 items.push_back(std::make_pair(id, i->second));
573                         }
574                 }
575                 return id;
576         }
577         virtual content_t allocateDummy(const std::string &name)
578         {
579                 assert(name != "");
580                 ContentFeatures f;
581                 f.name = name;
582                 return set(name, f);
583         }
584         virtual void updateAliases(IItemDefManager *idef)
585         {
586                 std::set<std::string> all = idef->getAll();
587                 m_name_id_mapping_with_aliases.clear();
588                 for(std::set<std::string>::iterator
589                                 i = all.begin(); i != all.end(); i++)
590                 {
591                         std::string name = *i;
592                         std::string convert_to = idef->getAlias(name);
593                         content_t id;
594                         if(m_name_id_mapping.getId(convert_to, id))
595                         {
596                                 m_name_id_mapping_with_aliases.insert(
597                                                 std::make_pair(name, id));
598                         }
599                 }
600         }
601         virtual void updateTextures(ITextureSource *tsrc,
602                 IShaderSource *shdsrc)
603         {
604 #ifndef SERVER
605                 infostream<<"CNodeDefManager::updateTextures(): Updating "
606                                 <<"textures in node definitions"<<std::endl;
607
608                 bool new_style_water = g_settings->getBool("new_style_water");
609                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
610                 bool opaque_water = g_settings->getBool("opaque_water");
611                 bool enable_shaders = g_settings->getBool("enable_shaders");
612                 bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
613                 bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
614
615                 for(u32 i=0; i<m_content_features.size(); i++)
616                 {
617                         ContentFeatures *f = &m_content_features[i];
618
619                         // Figure out the actual tiles to use
620                         TileDef tiledef[6];
621                         for(u32 j = 0; j < 6; j++)
622                         {
623                                 tiledef[j] = f->tiledef[j];
624                                 if(tiledef[j].name == "")
625                                         tiledef[j].name = "unknown_node.png";
626                         }
627
628                         bool is_liquid = false;
629                         bool is_water_surface = false;
630
631                         u8 material_type;
632                         material_type = (f->alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
633
634                         switch(f->drawtype){
635                         default:
636                         case NDT_NORMAL:
637                                 f->solidness = 2;
638                                 break;
639                         case NDT_AIRLIKE:
640                                 f->solidness = 0;
641                                 break;
642                         case NDT_LIQUID:
643                                 assert(f->liquid_type == LIQUID_SOURCE);
644                                 if(opaque_water)
645                                         f->alpha = 255;
646                                 if(new_style_water){
647                                         f->solidness = 0;
648                                 } else {
649                                         f->solidness = 1;
650                                         f->backface_culling = false;
651                                 }
652                                 is_liquid = true;
653                                 break;
654                         case NDT_FLOWINGLIQUID:
655                                 assert(f->liquid_type == LIQUID_FLOWING);
656                                 f->solidness = 0;
657                                 if(opaque_water)
658                                         f->alpha = 255;
659                                 is_liquid = true;
660                                 break;
661                         case NDT_GLASSLIKE:
662                                 f->solidness = 0;
663                                 f->visual_solidness = 1;
664                                 break;
665                         case NDT_GLASSLIKE_FRAMED:
666                                 f->solidness = 0;
667                                 f->visual_solidness = 1;
668                                 break;
669                         case NDT_ALLFACES:
670                                 f->solidness = 0;
671                                 f->visual_solidness = 1;
672                                 break;
673                         case NDT_ALLFACES_OPTIONAL:
674                                 if(new_style_leaves){
675                                         f->drawtype = NDT_ALLFACES;
676                                         f->solidness = 0;
677                                         f->visual_solidness = 1;
678                                 } else {
679                                         f->drawtype = NDT_NORMAL;
680                                         f->solidness = 2;
681                                         for(u32 i=0; i<6; i++){
682                                                 tiledef[i].name += std::string("^[noalpha");
683                                         }
684                                 }
685                                 if (f->waving == 1)
686                                         material_type = TILE_MATERIAL_WAVING_LEAVES;
687                                 break;
688                         case NDT_PLANTLIKE:
689                                 f->solidness = 0;
690                                 f->backface_culling = false;
691                                 if (f->waving == 1)
692                                         material_type = TILE_MATERIAL_WAVING_PLANTS;
693                                 break;
694                         case NDT_FIRELIKE:
695                                 f->backface_culling = false;
696                         case NDT_TORCHLIKE:
697                         case NDT_SIGNLIKE:
698                         case NDT_FENCELIKE:
699                         case NDT_RAILLIKE:
700                         case NDT_NODEBOX:
701                                 f->solidness = 0;
702                                 break;
703                         }
704
705                         if (is_liquid){
706                                 material_type = (f->alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
707                                 if (f->name == "default:water_source")
708                                         is_water_surface = true;
709                         }
710                         u32 tile_shader[6];
711                         for(u16 j=0; j<6; j++)
712                                 tile_shader[j] = shdsrc->getShader("nodes_shader",material_type, f->drawtype);
713
714                         if (is_water_surface)
715                                 tile_shader[0] = shdsrc->getShader("water_surface_shader",material_type, f->drawtype);
716
717                         // Tiles (fill in f->tiles[])
718                         for(u16 j = 0; j < 6; j++){
719                                 // Shader
720                                 f->tiles[j].shader_id = tile_shader[j];
721                                 // Texture
722                                 f->tiles[j].texture = tsrc->getTexture(
723                                                 tiledef[j].name,
724                                                 &f->tiles[j].texture_id);
725                                 // Normal texture
726                                 if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion))
727                                         f->tiles[j].normal_texture = tsrc->getNormalTexture(tiledef[j].name);
728                                 // Alpha
729                                 f->tiles[j].alpha = f->alpha;
730                                 // Material type
731                                 f->tiles[j].material_type = material_type;
732                                 // Material flags
733                                 f->tiles[j].material_flags = 0;
734                                 if(f->backface_culling)
735                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
736                                 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
737                                         f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
738                                 // Animation parameters
739                                 int frame_count = 1;
740                                 if(f->tiles[j].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
741                                         // Get texture size to determine frame count by
742                                         // aspect ratio
743                                         v2u32 size = f->tiles[j].texture->getOriginalSize();
744                                         int frame_height = (float)size.X /
745                                                         (float)tiledef[j].animation.aspect_w *
746                                                         (float)tiledef[j].animation.aspect_h;
747                                         frame_count = size.Y / frame_height;
748                                         int frame_length_ms = 1000.0 *
749                                                         tiledef[j].animation.length / frame_count;
750                                         f->tiles[j].animation_frame_count = frame_count;
751                                         f->tiles[j].animation_frame_length_ms = frame_length_ms;
752                                 }
753                                 if(frame_count == 1) {
754                                         f->tiles[j].material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
755                                 } else {
756                                         std::ostringstream os(std::ios::binary);
757                                         for (int i = 0; i < frame_count; i++) {
758                                                 FrameSpec frame;
759                                                 os.str("");
760                                                 os<<tiledef[j].name<<"^[verticalframe:"<<frame_count<<":"<<i;
761                                                 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
762                                                 if (f->tiles[j].normal_texture)
763                                                         frame.normal_texture = tsrc->getNormalTexture(os.str());
764                                                 f->tiles[j].frames[i]=frame;
765                                         }
766                                 }
767                         }
768                         // Special tiles (fill in f->special_tiles[])
769                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
770                                 // Shader
771                                 f->special_tiles[j].shader_id = tile_shader[j];
772                                 // Texture
773                                 f->special_tiles[j].texture = tsrc->getTexture(
774                                                 f->tiledef_special[j].name,
775                                                 &f->special_tiles[j].texture_id);
776                                 // Normal texture
777                                 if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion))
778                                         f->special_tiles[j].normal_texture = tsrc->getNormalTexture(f->tiledef_special[j].name);
779                                 // Alpha
780                                 f->special_tiles[j].alpha = f->alpha;
781                                 // Material type
782                                 f->special_tiles[j].material_type = material_type;
783                                 // Material flags
784                                 f->special_tiles[j].material_flags = 0;
785                                 if(f->tiledef_special[j].backface_culling)
786                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
787                                 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
788                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
789                                 // Animation parameters
790                                 int frame_count = 1;
791                                 if(f->special_tiles[j].material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES) {
792                                         // Get texture size to determine frame count by
793                                         // aspect ratio
794                                         v2u32 size = f->special_tiles[j].texture->getOriginalSize();
795                                         int frame_height = (float)size.X /
796                                                         (float)f->tiledef_special[j].animation.aspect_w *
797                                                         (float)f->tiledef_special[j].animation.aspect_h;
798                                         frame_count = size.Y / frame_height;
799                                         int frame_length_ms = 1000.0 *
800                                                         f->tiledef_special[j].animation.length / frame_count;
801                                         f->special_tiles[j].animation_frame_count = frame_count;
802                                         f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
803                                 }
804                                 if(frame_count == 1) {
805                                         f->special_tiles[j].material_flags &= ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
806                                 } else {
807                                         std::ostringstream os(std::ios::binary);
808                                         for (int i = 0; i < frame_count; i++) {
809                                                 FrameSpec frame;
810                                                 os.str("");
811                                                 os<<f->tiledef_special[j].name<<"^[verticalframe:"<<frame_count<<":"<<i;
812                                                 frame.texture = tsrc->getTexture(os.str(), &frame.texture_id);
813                                                 if (f->special_tiles[j].normal_texture)
814                                                         frame.normal_texture = tsrc->getNormalTexture(os.str());
815                                                 f->special_tiles[j].frames[i]=frame;
816                                         }
817                                 }
818                         }
819                 }
820 #endif
821         }
822         void serialize(std::ostream &os, u16 protocol_version)
823         {
824                 writeU8(os, 1); // version
825                 u16 count = 0;
826                 std::ostringstream os2(std::ios::binary);
827                 for(u32 i = 0; i < m_content_features.size(); i++)
828                 {
829                         if(i == CONTENT_IGNORE || i == CONTENT_AIR
830                                         || i == CONTENT_UNKNOWN)
831                                 continue;
832                         ContentFeatures *f = &m_content_features[i];
833                         if(f->name == "")
834                                 continue;
835                         writeU16(os2, i);
836                         // Wrap it in a string to allow different lengths without
837                         // strict version incompatibilities
838                         std::ostringstream wrapper_os(std::ios::binary);
839                         f->serialize(wrapper_os, protocol_version);
840                         os2<<serializeString(wrapper_os.str());
841
842                         assert(count + 1 > count); // must not overflow
843                         count++;
844                 }
845                 writeU16(os, count);
846                 os<<serializeLongString(os2.str());
847         }
848         void deSerialize(std::istream &is)
849         {
850                 clear();
851                 int version = readU8(is);
852                 if(version != 1)
853                         throw SerializationError("unsupported NodeDefinitionManager version");
854                 u16 count = readU16(is);
855                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
856                 ContentFeatures f;
857                 for(u16 n = 0; n < count; n++){
858                         u16 i = readU16(is2);
859
860                         // Read it from the string wrapper
861                         std::string wrapper = deSerializeString(is2);
862                         std::istringstream wrapper_is(wrapper, std::ios::binary);
863                         f.deSerialize(wrapper_is);
864
865                         // Check error conditions
866                         if(i == CONTENT_IGNORE || i == CONTENT_AIR
867                                         || i == CONTENT_UNKNOWN){
868                                 infostream<<"NodeDefManager::deSerialize(): WARNING: "
869                                         <<"not changing builtin node "<<i
870                                         <<std::endl;
871                                 continue;
872                         }
873                         if(f.name == ""){
874                                 infostream<<"NodeDefManager::deSerialize(): WARNING: "
875                                         <<"received empty name"<<std::endl;
876                                 continue;
877                         }
878                         u16 existing_id;
879                         bool found = m_name_id_mapping.getId(f.name, existing_id);  // ignore aliases
880                         if(found && i != existing_id){
881                                 infostream<<"NodeDefManager::deSerialize(): WARNING: "
882                                         <<"already defined with different ID: "
883                                         <<f.name<<std::endl;
884                                 continue;
885                         }
886
887                         // All is ok, add node definition with the requested ID
888                         if(i >= m_content_features.size())
889                                 m_content_features.resize((u32)(i) + 1);
890                         m_content_features[i] = f;
891                         addNameIdMapping(i, f.name);
892                         verbosestream<<"deserialized "<<f.name<<std::endl;
893                 }
894         }
895 private:
896         void addNameIdMapping(content_t i, std::string name)
897         {
898                 m_name_id_mapping.set(i, name);
899                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
900         }
901 private:
902         // Features indexed by id
903         std::vector<ContentFeatures> m_content_features;
904         // A mapping for fast converting back and forth between names and ids
905         NameIdMapping m_name_id_mapping;
906         // Like m_name_id_mapping, but only from names to ids, and includes
907         // item aliases too. Updated by updateAliases()
908         // Note: Not serialized.
909         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
910         // A mapping from groups to a list of content_ts (and their levels)
911         // that belong to it.  Necessary for a direct lookup in getIds().
912         // Note: Not serialized.
913         std::map<std::string, GroupItems> m_group_to_items;
914         // Next possibly free id
915         content_t m_next_id;
916 };
917
918 IWritableNodeDefManager* createNodeDefManager()
919 {
920         return new CNodeDefManager();
921 }
922
923 /*
924         Serialization of old ContentFeatures formats
925 */
926
927 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
928 {
929         if(protocol_version == 13)
930         {
931                 writeU8(os, 5); // version
932                 os<<serializeString(name);
933                 writeU16(os, groups.size());
934                 for(ItemGroupList::const_iterator
935                                 i = groups.begin(); i != groups.end(); i++){
936                         os<<serializeString(i->first);
937                         writeS16(os, i->second);
938                 }
939                 writeU8(os, drawtype);
940                 writeF1000(os, visual_scale);
941                 writeU8(os, 6);
942                 for(u32 i = 0; i < 6; i++)
943                         tiledef[i].serialize(os, protocol_version);
944                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
945                 writeU8(os, 2);
946                 for(u32 i = 0; i < 2; i++){
947                         tiledef_special[i].serialize(os, protocol_version);
948                 }
949                 writeU8(os, alpha);
950                 writeU8(os, post_effect_color.getAlpha());
951                 writeU8(os, post_effect_color.getRed());
952                 writeU8(os, post_effect_color.getGreen());
953                 writeU8(os, post_effect_color.getBlue());
954                 writeU8(os, param_type);
955                 writeU8(os, param_type_2);
956                 writeU8(os, is_ground_content);
957                 writeU8(os, light_propagates);
958                 writeU8(os, sunlight_propagates);
959                 writeU8(os, walkable);
960                 writeU8(os, pointable);
961                 writeU8(os, diggable);
962                 writeU8(os, climbable);
963                 writeU8(os, buildable_to);
964                 os<<serializeString(""); // legacy: used to be metadata_name
965                 writeU8(os, liquid_type);
966                 os<<serializeString(liquid_alternative_flowing);
967                 os<<serializeString(liquid_alternative_source);
968                 writeU8(os, liquid_viscosity);
969                 writeU8(os, light_source);
970                 writeU32(os, damage_per_second);
971                 node_box.serialize(os, protocol_version);
972                 selection_box.serialize(os, protocol_version);
973                 writeU8(os, legacy_facedir_simple);
974                 writeU8(os, legacy_wallmounted);
975                 serializeSimpleSoundSpec(sound_footstep, os);
976                 serializeSimpleSoundSpec(sound_dig, os);
977                 serializeSimpleSoundSpec(sound_dug, os);
978         }
979         else if (protocol_version > 13 && protocol_version < 24) {
980                 writeU8(os, 6); // version
981                 os<<serializeString(name);
982                 writeU16(os, groups.size());
983                 for(ItemGroupList::const_iterator
984                         i = groups.begin(); i != groups.end(); i++){
985                                 os<<serializeString(i->first);
986                                 writeS16(os, i->second);
987                 }
988                 writeU8(os, drawtype);
989                 writeF1000(os, visual_scale);
990                 writeU8(os, 6);
991                 for(u32 i = 0; i < 6; i++)
992                         tiledef[i].serialize(os, protocol_version);
993                 //CF_SPECIAL_COUNT = 2 before cf ver. 7 and protocol ver. 24
994                 writeU8(os, 2);
995                 for(u32 i = 0; i < 2; i++){
996                         tiledef_special[i].serialize(os, protocol_version);
997                 }
998                 writeU8(os, alpha);
999                 writeU8(os, post_effect_color.getAlpha());
1000                 writeU8(os, post_effect_color.getRed());
1001                 writeU8(os, post_effect_color.getGreen());
1002                 writeU8(os, post_effect_color.getBlue());
1003                 writeU8(os, param_type);
1004                 writeU8(os, param_type_2);
1005                 writeU8(os, is_ground_content);
1006                 writeU8(os, light_propagates);
1007                 writeU8(os, sunlight_propagates);
1008                 writeU8(os, walkable);
1009                 writeU8(os, pointable);
1010                 writeU8(os, diggable);
1011                 writeU8(os, climbable);
1012                 writeU8(os, buildable_to);
1013                 os<<serializeString(""); // legacy: used to be metadata_name
1014                 writeU8(os, liquid_type);
1015                 os<<serializeString(liquid_alternative_flowing);
1016                 os<<serializeString(liquid_alternative_source);
1017                 writeU8(os, liquid_viscosity);
1018                 writeU8(os, liquid_renewable);
1019                 writeU8(os, light_source);
1020                 writeU32(os, damage_per_second);
1021                 node_box.serialize(os, protocol_version);
1022                 selection_box.serialize(os, protocol_version);
1023                 writeU8(os, legacy_facedir_simple);
1024                 writeU8(os, legacy_wallmounted);
1025                 serializeSimpleSoundSpec(sound_footstep, os);
1026                 serializeSimpleSoundSpec(sound_dig, os);
1027                 serializeSimpleSoundSpec(sound_dug, os);
1028                 writeU8(os, rightclickable);
1029                 writeU8(os, drowning);
1030                 writeU8(os, leveled);
1031                 writeU8(os, liquid_range);
1032         } else 
1033                 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
1034 }
1035
1036 void ContentFeatures::deSerializeOld(std::istream &is, int version)
1037 {
1038         if(version == 5) // In PROTOCOL_VERSION 13
1039         {
1040                 name = deSerializeString(is);
1041                 groups.clear();
1042                 u32 groups_size = readU16(is);
1043                 for(u32 i=0; i<groups_size; i++){
1044                         std::string name = deSerializeString(is);
1045                         int value = readS16(is);
1046                         groups[name] = value;
1047                 }
1048                 drawtype = (enum NodeDrawType)readU8(is);
1049                 visual_scale = readF1000(is);
1050                 if(readU8(is) != 6)
1051                         throw SerializationError("unsupported tile count");
1052                 for(u32 i=0; i<6; i++)
1053                         tiledef[i].deSerialize(is);
1054                 if(readU8(is) != CF_SPECIAL_COUNT)
1055                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1056                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
1057                         tiledef_special[i].deSerialize(is);
1058                 alpha = readU8(is);
1059                 post_effect_color.setAlpha(readU8(is));
1060                 post_effect_color.setRed(readU8(is));
1061                 post_effect_color.setGreen(readU8(is));
1062                 post_effect_color.setBlue(readU8(is));
1063                 param_type = (enum ContentParamType)readU8(is);
1064                 param_type_2 = (enum ContentParamType2)readU8(is);
1065                 is_ground_content = readU8(is);
1066                 light_propagates = readU8(is);
1067                 sunlight_propagates = readU8(is);
1068                 walkable = readU8(is);
1069                 pointable = readU8(is);
1070                 diggable = readU8(is);
1071                 climbable = readU8(is);
1072                 buildable_to = readU8(is);
1073                 deSerializeString(is); // legacy: used to be metadata_name
1074                 liquid_type = (enum LiquidType)readU8(is);
1075                 liquid_alternative_flowing = deSerializeString(is);
1076                 liquid_alternative_source = deSerializeString(is);
1077                 liquid_viscosity = readU8(is);
1078                 light_source = readU8(is);
1079                 damage_per_second = readU32(is);
1080                 node_box.deSerialize(is);
1081                 selection_box.deSerialize(is);
1082                 legacy_facedir_simple = readU8(is);
1083                 legacy_wallmounted = readU8(is);
1084                 deSerializeSimpleSoundSpec(sound_footstep, is);
1085                 deSerializeSimpleSoundSpec(sound_dig, is);
1086                 deSerializeSimpleSoundSpec(sound_dug, is);
1087         } else if (version == 6) {
1088                 name = deSerializeString(is);
1089                 groups.clear();
1090                 u32 groups_size = readU16(is);
1091                 for(u32 i=0; i<groups_size; i++){
1092                         std::string name = deSerializeString(is);
1093                         int     value = readS16(is);
1094                         groups[name] = value;
1095                 }
1096                 drawtype = (enum NodeDrawType)readU8(is);
1097                 visual_scale = readF1000(is);
1098                 if(readU8(is) != 6)
1099                         throw SerializationError("unsupported tile count");
1100                 for(u32 i=0; i<6; i++)
1101                         tiledef[i].deSerialize(is);
1102                 // CF_SPECIAL_COUNT in version 6 = 2
1103                 if(readU8(is) != 2)
1104                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
1105                 for(u32 i=0; i<2; i++)
1106                         tiledef_special[i].deSerialize(is);
1107                 alpha = readU8(is);
1108                 post_effect_color.setAlpha(readU8(is));
1109                 post_effect_color.setRed(readU8(is));
1110                 post_effect_color.setGreen(readU8(is));
1111                 post_effect_color.setBlue(readU8(is));
1112                 param_type = (enum ContentParamType)readU8(is);
1113                 param_type_2 = (enum ContentParamType2)readU8(is);
1114                 is_ground_content = readU8(is);
1115                 light_propagates = readU8(is);
1116                 sunlight_propagates = readU8(is);
1117                 walkable = readU8(is);
1118                 pointable = readU8(is);
1119                 diggable = readU8(is);
1120                 climbable = readU8(is);
1121                 buildable_to = readU8(is);
1122                 deSerializeString(is); // legacy: used to be metadata_name
1123                 liquid_type = (enum LiquidType)readU8(is);
1124                 liquid_alternative_flowing = deSerializeString(is);
1125                 liquid_alternative_source = deSerializeString(is);
1126                 liquid_viscosity = readU8(is);
1127                 liquid_renewable = readU8(is);
1128                 light_source = readU8(is);
1129                 damage_per_second = readU32(is);
1130                 node_box.deSerialize(is);
1131                 selection_box.deSerialize(is);
1132                 legacy_facedir_simple = readU8(is);
1133                 legacy_wallmounted = readU8(is);
1134                 deSerializeSimpleSoundSpec(sound_footstep, is);
1135                 deSerializeSimpleSoundSpec(sound_dig, is);
1136                 deSerializeSimpleSoundSpec(sound_dug, is);
1137                 rightclickable = readU8(is);
1138                 drowning = readU8(is);
1139                 leveled = readU8(is);
1140                 liquid_range = readU8(is);
1141         } else
1142                 throw SerializationError("unsupported ContentFeatures version");
1143 }