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