]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodedef.cpp
d41df5c3b8a394f47c3ec10cf8d3a1018eb43aaa
[dragonfireclient.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/serialize.h"
31
32 /*
33         NodeBox
34 */
35
36 void NodeBox::reset()
37 {
38         type = NODEBOX_REGULAR;
39         // default is empty
40         fixed.clear();
41         // default is sign/ladder-like
42         wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
43         wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
44         wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
45 }
46
47 void NodeBox::serialize(std::ostream &os) const
48 {
49         writeU8(os, 1); // version
50         writeU8(os, type);
51
52         if(type == NODEBOX_FIXED)
53         {
54                 writeU16(os, fixed.size());
55                 for(std::vector<aabb3f>::const_iterator
56                                 i = fixed.begin();
57                                 i != fixed.end(); i++)
58                 {
59                         writeV3F1000(os, i->MinEdge);
60                         writeV3F1000(os, i->MaxEdge);
61                 }
62         }
63         else if(type == NODEBOX_WALLMOUNTED)
64         {
65                 writeV3F1000(os, wall_top.MinEdge);
66                 writeV3F1000(os, wall_top.MaxEdge);
67                 writeV3F1000(os, wall_bottom.MinEdge);
68                 writeV3F1000(os, wall_bottom.MaxEdge);
69                 writeV3F1000(os, wall_side.MinEdge);
70                 writeV3F1000(os, wall_side.MaxEdge);
71         }
72 }
73
74 void NodeBox::deSerialize(std::istream &is)
75 {
76         int version = readU8(is);
77         if(version != 1)
78                 throw SerializationError("unsupported NodeBox version");
79
80         reset();
81
82         type = (enum NodeBoxType)readU8(is);
83
84         if(type == NODEBOX_FIXED)
85         {
86                 u16 fixed_count = readU16(is);
87                 while(fixed_count--)
88                 {
89                         aabb3f box;
90                         box.MinEdge = readV3F1000(is);
91                         box.MaxEdge = readV3F1000(is);
92                         fixed.push_back(box);
93                 }
94         }
95         else if(type == NODEBOX_WALLMOUNTED)
96         {
97                 wall_top.MinEdge = readV3F1000(is);
98                 wall_top.MaxEdge = readV3F1000(is);
99                 wall_bottom.MinEdge = readV3F1000(is);
100                 wall_bottom.MaxEdge = readV3F1000(is);
101                 wall_side.MinEdge = readV3F1000(is);
102                 wall_side.MaxEdge = readV3F1000(is);
103         }
104 }
105
106 /*
107         TileDef
108 */
109
110 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
111 {
112         if(protocol_version >= 17)
113                 writeU8(os, 1); 
114         else
115                 writeU8(os, 0);
116         os<<serializeString(name);
117         writeU8(os, animation.type);
118         writeU16(os, animation.aspect_w);
119         writeU16(os, animation.aspect_h);
120         writeF1000(os, animation.length);
121         if(protocol_version >= 17)
122                 writeU8(os, backface_culling);
123 }
124
125 void TileDef::deSerialize(std::istream &is)
126 {
127         int version = readU8(is);
128         name = deSerializeString(is);
129         animation.type = (TileAnimationType)readU8(is);
130         animation.aspect_w = readU16(is);
131         animation.aspect_h = readU16(is);
132         animation.length = readF1000(is);
133         if(version >= 1)
134                 backface_culling = readU8(is);
135 }
136
137 /*
138         SimpleSoundSpec serialization
139 */
140
141 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
142                 std::ostream &os)
143 {
144         os<<serializeString(ss.name);
145         writeF1000(os, ss.gain);
146 }
147 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
148 {
149         ss.name = deSerializeString(is);
150         ss.gain = readF1000(is);
151 }
152
153 /*
154         ContentFeatures
155 */
156
157 ContentFeatures::ContentFeatures()
158 {
159         reset();
160 }
161
162 ContentFeatures::~ContentFeatures()
163 {
164 }
165
166 void ContentFeatures::reset()
167 {
168         /*
169                 Cached stuff
170         */
171 #ifndef SERVER
172         solidness = 2;
173         visual_solidness = 0;
174         backface_culling = true;
175 #endif
176         has_on_construct = false;
177         has_on_destruct = false;
178         has_after_destruct = false;
179         /*
180                 Actual data
181
182                 NOTE: Most of this is always overridden by the default values given
183                       in builtin.lua
184         */
185         name = "";
186         groups.clear();
187         // Unknown nodes can be dug
188         groups["dig_immediate"] = 2;
189         drawtype = NDT_NORMAL;
190         visual_scale = 1.0;
191         for(u32 i=0; i<6; i++)
192                 tiledef[i] = TileDef();
193         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
194                 tiledef_special[j] = TileDef();
195         alpha = 255;
196         post_effect_color = video::SColor(0, 0, 0, 0);
197         param_type = CPT_NONE;
198         param_type_2 = CPT2_NONE;
199         is_ground_content = false;
200         light_propagates = false;
201         sunlight_propagates = false;
202         walkable = true;
203         pointable = true;
204         diggable = true;
205         climbable = false;
206         buildable_to = false;
207         rightclickable = true;
208         liquid_type = LIQUID_NONE;
209         liquid_alternative_flowing = "";
210         liquid_alternative_source = "";
211         liquid_viscosity = 0;
212         liquid_renewable = true;
213         light_source = 0;
214         damage_per_second = 0;
215         node_box = NodeBox();
216         selection_box = NodeBox();
217         legacy_facedir_simple = false;
218         legacy_wallmounted = false;
219         sound_footstep = SimpleSoundSpec();
220         sound_dig = SimpleSoundSpec("__group");
221         sound_dug = SimpleSoundSpec();
222 }
223
224 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
225 {
226         if(protocol_version < 14){
227                 serializeOld(os, protocol_version);
228                 return;
229         }
230
231         writeU8(os, 6); // version
232         os<<serializeString(name);
233         writeU16(os, groups.size());
234         for(ItemGroupList::const_iterator
235                         i = groups.begin(); i != groups.end(); i++){
236                 os<<serializeString(i->first);
237                 writeS16(os, i->second);
238         }
239         writeU8(os, drawtype);
240         writeF1000(os, visual_scale);
241         writeU8(os, 6);
242         for(u32 i=0; i<6; i++)
243                 tiledef[i].serialize(os, protocol_version);
244         writeU8(os, CF_SPECIAL_COUNT);
245         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
246                 tiledef_special[i].serialize(os, protocol_version);
247         }
248         writeU8(os, alpha);
249         writeU8(os, post_effect_color.getAlpha());
250         writeU8(os, post_effect_color.getRed());
251         writeU8(os, post_effect_color.getGreen());
252         writeU8(os, post_effect_color.getBlue());
253         writeU8(os, param_type);
254         writeU8(os, param_type_2);
255         writeU8(os, is_ground_content);
256         writeU8(os, light_propagates);
257         writeU8(os, sunlight_propagates);
258         writeU8(os, walkable);
259         writeU8(os, pointable);
260         writeU8(os, diggable);
261         writeU8(os, climbable);
262         writeU8(os, buildable_to);
263         os<<serializeString(""); // legacy: used to be metadata_name
264         writeU8(os, liquid_type);
265         os<<serializeString(liquid_alternative_flowing);
266         os<<serializeString(liquid_alternative_source);
267         writeU8(os, liquid_viscosity);
268         writeU8(os, liquid_renewable);
269         writeU8(os, light_source);
270         writeU32(os, damage_per_second);
271         node_box.serialize(os);
272         selection_box.serialize(os);
273         writeU8(os, legacy_facedir_simple);
274         writeU8(os, legacy_wallmounted);
275         serializeSimpleSoundSpec(sound_footstep, os);
276         serializeSimpleSoundSpec(sound_dig, os);
277         serializeSimpleSoundSpec(sound_dug, os);
278         writeU8(os, rightclickable);
279         // Stuff below should be moved to correct place in a version that otherwise changes
280         // the protocol version
281 }
282
283 void ContentFeatures::deSerialize(std::istream &is)
284 {
285         int version = readU8(is);
286         if(version != 6){
287                 deSerializeOld(is, version);
288                 return;
289         }
290
291         name = deSerializeString(is);
292         groups.clear();
293         u32 groups_size = readU16(is);
294         for(u32 i=0; i<groups_size; i++){
295                 std::string name = deSerializeString(is);
296                 int value = readS16(is);
297                 groups[name] = value;
298         }
299         drawtype = (enum NodeDrawType)readU8(is);
300         visual_scale = readF1000(is);
301         if(readU8(is) != 6)
302                 throw SerializationError("unsupported tile count");
303         for(u32 i=0; i<6; i++)
304                 tiledef[i].deSerialize(is);
305         if(readU8(is) != CF_SPECIAL_COUNT)
306                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
307         for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
308                 tiledef_special[i].deSerialize(is);
309         alpha = readU8(is);
310         post_effect_color.setAlpha(readU8(is));
311         post_effect_color.setRed(readU8(is));
312         post_effect_color.setGreen(readU8(is));
313         post_effect_color.setBlue(readU8(is));
314         param_type = (enum ContentParamType)readU8(is);
315         param_type_2 = (enum ContentParamType2)readU8(is);
316         is_ground_content = readU8(is);
317         light_propagates = readU8(is);
318         sunlight_propagates = readU8(is);
319         walkable = readU8(is);
320         pointable = readU8(is);
321         diggable = readU8(is);
322         climbable = readU8(is);
323         buildable_to = readU8(is);
324         deSerializeString(is); // legacy: used to be metadata_name
325         liquid_type = (enum LiquidType)readU8(is);
326         liquid_alternative_flowing = deSerializeString(is);
327         liquid_alternative_source = deSerializeString(is);
328         liquid_viscosity = readU8(is);
329         liquid_renewable = readU8(is);
330         light_source = readU8(is);
331         damage_per_second = readU32(is);
332         node_box.deSerialize(is);
333         selection_box.deSerialize(is);
334         legacy_facedir_simple = readU8(is);
335         legacy_wallmounted = readU8(is);
336         deSerializeSimpleSoundSpec(sound_footstep, is);
337         deSerializeSimpleSoundSpec(sound_dig, is);
338         deSerializeSimpleSoundSpec(sound_dug, is);
339         rightclickable = readU8(is);
340         // If you add anything here, insert it primarily inside the try-catch
341         // block to not need to increase the version.
342         try{
343                 // Stuff below should be moved to correct place in a version that
344                 // otherwise changes the protocol version
345         }catch(SerializationError &e) {};
346 }
347
348 /*
349         CNodeDefManager
350 */
351
352 class CNodeDefManager: public IWritableNodeDefManager
353 {
354 public:
355         void clear()
356         {
357                 m_name_id_mapping.clear();
358                 m_name_id_mapping_with_aliases.clear();
359
360                 for(u16 i=0; i<=MAX_CONTENT; i++)
361                 {
362                         ContentFeatures &f = m_content_features[i];
363                         f.reset(); // Reset to defaults
364                 }
365
366                 // Set CONTENT_AIR
367                 {
368                         ContentFeatures f;
369                         f.name = "air";
370                         f.drawtype = NDT_AIRLIKE;
371                         f.param_type = CPT_LIGHT;
372                         f.light_propagates = true;
373                         f.sunlight_propagates = true;
374                         f.walkable = false;
375                         f.pointable = false;
376                         f.diggable = false;
377                         f.buildable_to = true;
378                         // Insert directly into containers
379                         content_t c = CONTENT_AIR;
380                         m_content_features[c] = f;
381                         addNameIdMapping(c, f.name);
382                 }
383                 // Set CONTENT_IGNORE
384                 {
385                         ContentFeatures f;
386                         f.name = "ignore";
387                         f.drawtype = NDT_AIRLIKE;
388                         f.param_type = CPT_NONE;
389                         f.light_propagates = false;
390                         f.sunlight_propagates = false;
391                         f.walkable = false;
392                         f.pointable = false;
393                         f.diggable = false;
394                         // A way to remove accidental CONTENT_IGNOREs
395                         f.buildable_to = true;
396                         // Insert directly into containers
397                         content_t c = CONTENT_IGNORE;
398                         m_content_features[c] = f;
399                         addNameIdMapping(c, f.name);
400                 }
401         }
402         // CONTENT_IGNORE = not found
403         content_t getFreeId()
404         {
405                 for(u32 i=0; i<=0xffff; i++){
406                         const ContentFeatures &f = m_content_features[i];
407                         if(f.name == "")
408                                 return i;
409                 }
410                 return CONTENT_IGNORE;
411         }
412         CNodeDefManager()
413         {
414                 clear();
415         }
416         virtual ~CNodeDefManager()
417         {
418         }
419         virtual IWritableNodeDefManager* clone()
420         {
421                 CNodeDefManager *mgr = new CNodeDefManager();
422                 for(u16 i=0; i<=MAX_CONTENT; i++)
423                 {
424                         mgr->set(i, get(i));
425                 }
426                 return mgr;
427         }
428         virtual const ContentFeatures& get(content_t c) const
429         {
430                 assert(c <= MAX_CONTENT);
431                 return m_content_features[c];
432         }
433         virtual const ContentFeatures& get(const MapNode &n) const
434         {
435                 return get(n.getContent());
436         }
437         virtual bool getId(const std::string &name, content_t &result) const
438         {
439                 std::map<std::string, content_t>::const_iterator
440                         i = m_name_id_mapping_with_aliases.find(name);
441                 if(i == m_name_id_mapping_with_aliases.end())
442                         return false;
443                 result = i->second;
444                 return true;
445         }
446         virtual content_t getId(const std::string &name) const
447         {
448                 content_t id = CONTENT_IGNORE;
449                 getId(name, id);
450                 return id;
451         }
452         virtual void getIds(const std::string &name, std::set<content_t> &result)
453                         const
454         {
455                 if(name.substr(0,6) != "group:"){
456                         content_t id = CONTENT_IGNORE;
457                         if(getId(name, id))
458                                 result.insert(id);
459                         return;
460                 }
461                 std::string group = name.substr(6);
462                 for(u16 id=0; id<=MAX_CONTENT; id++)
463                 {
464                         const ContentFeatures &f = m_content_features[id];
465                         if(f.name == "") // Quickly discard undefined nodes
466                                 continue;
467                         if(itemgroup_get(f.groups, group) != 0)
468                                 result.insert(id);
469                 }
470         }
471         virtual const ContentFeatures& get(const std::string &name) const
472         {
473                 content_t id = CONTENT_IGNORE;
474                 getId(name, id);
475                 return get(id);
476         }
477         // IWritableNodeDefManager
478         virtual void set(content_t c, const ContentFeatures &def)
479         {
480                 verbosestream<<"registerNode: registering content id \""<<c
481                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
482                 assert(c <= MAX_CONTENT);
483                 // Don't allow redefining CONTENT_IGNORE (but allow air)
484                 if(def.name == "ignore" || c == CONTENT_IGNORE){
485                         infostream<<"registerNode: WARNING: Ignoring "
486                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
487                         return;
488                 }
489                 // Check that the special contents are not redefined as different id
490                 // because it would mess up everything
491                 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
492                         (def.name == "air" && c != CONTENT_AIR)){
493                         errorstream<<"registerNode: IGNORING ERROR: "
494                                         <<"trying to register built-in type \""
495                                         <<def.name<<"\" as different id"<<std::endl;
496                         return;
497                 }
498                 m_content_features[c] = def;
499                 if(def.name != "")
500                         addNameIdMapping(c, def.name);
501         }
502         virtual content_t set(const std::string &name,
503                         const ContentFeatures &def)
504         {
505                 assert(name == def.name);
506                 u16 id = CONTENT_IGNORE;
507                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
508                 if(!found){
509                         // Get some id
510                         id = getFreeId();
511                         if(id == CONTENT_IGNORE)
512                                 return CONTENT_IGNORE;
513                         if(name != "")
514                                 addNameIdMapping(id, name);
515                 }
516                 set(id, def);
517                 return id;
518         }
519         virtual content_t allocateDummy(const std::string &name)
520         {
521                 assert(name != "");
522                 ContentFeatures f;
523                 f.name = name;
524                 return set(name, f);
525         }
526         virtual void updateAliases(IItemDefManager *idef)
527         {
528                 std::set<std::string> all = idef->getAll();
529                 m_name_id_mapping_with_aliases.clear();
530                 for(std::set<std::string>::iterator
531                                 i = all.begin(); i != all.end(); i++)
532                 {
533                         std::string name = *i;
534                         std::string convert_to = idef->getAlias(name);
535                         content_t id;
536                         if(m_name_id_mapping.getId(convert_to, id))
537                         {
538                                 m_name_id_mapping_with_aliases.insert(
539                                                 std::make_pair(name, id));
540                         }
541                 }
542         }
543         virtual void updateTextures(ITextureSource *tsrc)
544         {
545 #ifndef SERVER
546                 infostream<<"CNodeDefManager::updateTextures(): Updating "
547                                 <<"textures in node definitions"<<std::endl;
548
549                 bool new_style_water = g_settings->getBool("new_style_water");
550                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
551                 bool opaque_water = g_settings->getBool("opaque_water");
552
553                 for(u16 i=0; i<=MAX_CONTENT; i++)
554                 {
555                         ContentFeatures *f = &m_content_features[i];
556
557                         // Figure out the actual tiles to use
558                         TileDef tiledef[6];
559                         for(u32 j=0; j<6; j++)
560                         {
561                                 tiledef[j] = f->tiledef[j];
562                                 if(tiledef[j].name == "")
563                                         tiledef[j].name = "unknown_block.png";
564                         }
565
566                         bool is_liquid = false;
567                         switch(f->drawtype){
568                         default:
569                         case NDT_NORMAL:
570                                 f->solidness = 2;
571                                 break;
572                         case NDT_AIRLIKE:
573                                 f->solidness = 0;
574                                 break;
575                         case NDT_LIQUID:
576                                 assert(f->liquid_type == LIQUID_SOURCE);
577                                 if(opaque_water)
578                                         f->alpha = 255;
579                                 if(new_style_water){
580                                         f->solidness = 0;
581                                 } else {
582                                         f->solidness = 1;
583                                         f->backface_culling = false;
584                                 }
585                                 is_liquid = true;
586                                 break;
587                         case NDT_FLOWINGLIQUID:
588                                 assert(f->liquid_type == LIQUID_FLOWING);
589                                 f->solidness = 0;
590                                 if(opaque_water)
591                                         f->alpha = 255;
592                                 is_liquid = true;
593                                 break;
594                         case NDT_GLASSLIKE:
595                                 f->solidness = 0;
596                                 f->visual_solidness = 1;
597                                 break;
598                         case NDT_ALLFACES:
599                                 f->solidness = 0;
600                                 f->visual_solidness = 1;
601                                 break;
602                         case NDT_ALLFACES_OPTIONAL:
603                                 if(new_style_leaves){
604                                         f->drawtype = NDT_ALLFACES;
605                                         f->solidness = 0;
606                                         f->visual_solidness = 1;
607                                 } else {
608                                         f->drawtype = NDT_NORMAL;
609                                         f->solidness = 2;
610                                         for(u32 i=0; i<6; i++){
611                                                 tiledef[i].name += std::string("^[noalpha");
612                                         }
613                                 }
614                                 break;
615                         case NDT_PLANTLIKE:
616                                 f->solidness = 0;
617                                 f->backface_culling = false;
618                                 break;
619                         case NDT_TORCHLIKE:
620                         case NDT_SIGNLIKE:
621                         case NDT_FENCELIKE:
622                         case NDT_RAILLIKE:
623                         case NDT_NODEBOX:
624                                 f->solidness = 0;
625                                 break;
626                         }
627
628                         u8 material_type = 0;
629                         if(is_liquid){
630                                 if(f->alpha == 255)
631                                         material_type = TILE_MATERIAL_LIQUID_OPAQUE;
632                                 else
633                                         material_type = TILE_MATERIAL_LIQUID_TRANSPARENT;
634                         } else{
635                                 material_type = TILE_MATERIAL_BASIC;
636                         }
637
638                         // Tiles (fill in f->tiles[])
639                         for(u16 j=0; j<6; j++){
640                                 // Texture
641                                 f->tiles[j].texture = tsrc->getTexture(tiledef[j].name);
642                                 // Alpha
643                                 f->tiles[j].alpha = f->alpha;
644                                 // Material type
645                                 f->tiles[j].material_type = material_type;
646                                 // Material flags
647                                 f->tiles[j].material_flags = 0;
648                                 if(f->backface_culling)
649                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
650                                 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
651                                         f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
652                                 // Animation parameters
653                                 if(f->tiles[j].material_flags &
654                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
655                                 {
656                                         // Get raw texture size to determine frame count by
657                                         // aspect ratio
658                                         video::ITexture *t = tsrc->getTextureRaw(tiledef[j].name);
659                                         v2u32 size = t->getOriginalSize();
660                                         int frame_height = (float)size.X /
661                                                         (float)tiledef[j].animation.aspect_w *
662                                                         (float)tiledef[j].animation.aspect_h;
663                                         int frame_count = size.Y / frame_height;
664                                         int frame_length_ms = 1000.0 *
665                                                         tiledef[j].animation.length / frame_count;
666                                         f->tiles[j].animation_frame_count = frame_count;
667                                         f->tiles[j].animation_frame_length_ms = frame_length_ms;
668
669                                         // If there are no frames for an animation, switch
670                                         // animation off (so that having specified an animation
671                                         // for something but not using it in the texture pack
672                                         // gives no overhead)
673                                         if(frame_count == 1){
674                                                 f->tiles[j].material_flags &=
675                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
676                                         }
677                                 }
678                         }
679                         // Special tiles (fill in f->special_tiles[])
680                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
681                                 // Texture
682                                 f->special_tiles[j].texture =
683                                                 tsrc->getTexture(f->tiledef_special[j].name);
684                                 // Alpha
685                                 f->special_tiles[j].alpha = f->alpha;
686                                 // Material type
687                                 f->special_tiles[j].material_type = material_type;
688                                 // Material flags
689                                 f->special_tiles[j].material_flags = 0;
690                                 if(f->tiledef_special[j].backface_culling)
691                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
692                                 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
693                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
694                                 // Animation parameters
695                                 if(f->special_tiles[j].material_flags &
696                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
697                                 {
698                                         // Get raw texture size to determine frame count by
699                                         // aspect ratio
700                                         video::ITexture *t = tsrc->getTextureRaw(f->tiledef_special[j].name);
701                                         v2u32 size = t->getOriginalSize();
702                                         int frame_height = (float)size.X /
703                                                         (float)f->tiledef_special[j].animation.aspect_w *
704                                                         (float)f->tiledef_special[j].animation.aspect_h;
705                                         int frame_count = size.Y / frame_height;
706                                         int frame_length_ms = 1000.0 *
707                                                         f->tiledef_special[j].animation.length / frame_count;
708                                         f->special_tiles[j].animation_frame_count = frame_count;
709                                         f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
710
711                                         // If there are no frames for an animation, switch
712                                         // animation off (so that having specified an animation
713                                         // for something but not using it in the texture pack
714                                         // gives no overhead)
715                                         if(frame_count == 1){
716                                                 f->special_tiles[j].material_flags &=
717                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
718                                         }
719                                 }
720                         }
721                 }
722 #endif
723         }
724         void serialize(std::ostream &os, u16 protocol_version)
725         {
726                 writeU8(os, 1); // version
727                 u16 count = 0;
728                 std::ostringstream os2(std::ios::binary);
729                 for(u16 i=0; i<=MAX_CONTENT; i++)
730                 {
731                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
732                                 continue;
733                         ContentFeatures *f = &m_content_features[i];
734                         if(f->name == "")
735                                 continue;
736                         writeU16(os2, i);
737                         // Wrap it in a string to allow different lengths without
738                         // strict version incompatibilities
739                         std::ostringstream wrapper_os(std::ios::binary);
740                         f->serialize(wrapper_os, protocol_version);
741                         os2<<serializeString(wrapper_os.str());
742                         count++;
743                 }
744                 writeU16(os, count);
745                 os<<serializeLongString(os2.str());
746         }
747         void deSerialize(std::istream &is)
748         {
749                 clear();
750                 int version = readU8(is);
751                 if(version != 1)
752                         throw SerializationError("unsupported NodeDefinitionManager version");
753                 u16 count = readU16(is);
754                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
755                 for(u16 n=0; n<count; n++){
756                         u16 i = readU16(is2);
757                         if(i > MAX_CONTENT){
758                                 errorstream<<"ContentFeatures::deSerialize(): "
759                                                 <<"Too large content id: "<<i<<std::endl;
760                                 continue;
761                         }
762                         /*// Do not deserialize special types
763                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
764                                 continue;*/
765                         ContentFeatures *f = &m_content_features[i];
766                         // Read it from the string wrapper
767                         std::string wrapper = deSerializeString(is2);
768                         std::istringstream wrapper_is(wrapper, std::ios::binary);
769                         f->deSerialize(wrapper_is);
770                         verbosestream<<"deserialized "<<f->name<<std::endl;
771                         if(f->name != "")
772                                 addNameIdMapping(i, f->name);
773                 }
774         }
775 private:
776         void addNameIdMapping(content_t i, std::string name)
777         {
778                 m_name_id_mapping.set(i, name);
779                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
780         }
781 private:
782         // Features indexed by id
783         ContentFeatures m_content_features[MAX_CONTENT+1];
784         // A mapping for fast converting back and forth between names and ids
785         NameIdMapping m_name_id_mapping;
786         // Like m_name_id_mapping, but only from names to ids, and includes
787         // item aliases too. Updated by updateAliases()
788         // Note: Not serialized.
789         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
790 };
791
792 IWritableNodeDefManager* createNodeDefManager()
793 {
794         return new CNodeDefManager();
795 }
796
797 /*
798         Serialization of old ContentFeatures formats
799 */
800
801 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
802 {
803         if(protocol_version == 13)
804         {
805                 writeU8(os, 5); // version
806                 os<<serializeString(name);
807                 writeU16(os, groups.size());
808                 for(ItemGroupList::const_iterator
809                                 i = groups.begin(); i != groups.end(); i++){
810                         os<<serializeString(i->first);
811                         writeS16(os, i->second);
812                 }
813                 writeU8(os, drawtype);
814                 writeF1000(os, visual_scale);
815                 writeU8(os, 6);
816                 for(u32 i=0; i<6; i++)
817                         tiledef[i].serialize(os, protocol_version);
818                 writeU8(os, CF_SPECIAL_COUNT);
819                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
820                         tiledef_special[i].serialize(os, protocol_version);
821                 }
822                 writeU8(os, alpha);
823                 writeU8(os, post_effect_color.getAlpha());
824                 writeU8(os, post_effect_color.getRed());
825                 writeU8(os, post_effect_color.getGreen());
826                 writeU8(os, post_effect_color.getBlue());
827                 writeU8(os, param_type);
828                 writeU8(os, param_type_2);
829                 writeU8(os, is_ground_content);
830                 writeU8(os, light_propagates);
831                 writeU8(os, sunlight_propagates);
832                 writeU8(os, walkable);
833                 writeU8(os, pointable);
834                 writeU8(os, diggable);
835                 writeU8(os, climbable);
836                 writeU8(os, buildable_to);
837                 os<<serializeString(""); // legacy: used to be metadata_name
838                 writeU8(os, liquid_type);
839                 os<<serializeString(liquid_alternative_flowing);
840                 os<<serializeString(liquid_alternative_source);
841                 writeU8(os, liquid_viscosity);
842                 writeU8(os, light_source);
843                 writeU32(os, damage_per_second);
844                 node_box.serialize(os);
845                 selection_box.serialize(os);
846                 writeU8(os, legacy_facedir_simple);
847                 writeU8(os, legacy_wallmounted);
848                 serializeSimpleSoundSpec(sound_footstep, os);
849                 serializeSimpleSoundSpec(sound_dig, os);
850                 serializeSimpleSoundSpec(sound_dug, os);
851         }
852         else
853         {
854                 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
855         }
856 }
857
858 void ContentFeatures::deSerializeOld(std::istream &is, int version)
859 {
860         if(version == 5) // In PROTOCOL_VERSION 13
861         {
862                 name = deSerializeString(is);
863                 groups.clear();
864                 u32 groups_size = readU16(is);
865                 for(u32 i=0; i<groups_size; i++){
866                         std::string name = deSerializeString(is);
867                         int value = readS16(is);
868                         groups[name] = value;
869                 }
870                 drawtype = (enum NodeDrawType)readU8(is);
871                 visual_scale = readF1000(is);
872                 if(readU8(is) != 6)
873                         throw SerializationError("unsupported tile count");
874                 for(u32 i=0; i<6; i++)
875                         tiledef[i].deSerialize(is);
876                 if(readU8(is) != CF_SPECIAL_COUNT)
877                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
878                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
879                         tiledef_special[i].deSerialize(is);
880                 alpha = readU8(is);
881                 post_effect_color.setAlpha(readU8(is));
882                 post_effect_color.setRed(readU8(is));
883                 post_effect_color.setGreen(readU8(is));
884                 post_effect_color.setBlue(readU8(is));
885                 param_type = (enum ContentParamType)readU8(is);
886                 param_type_2 = (enum ContentParamType2)readU8(is);
887                 is_ground_content = readU8(is);
888                 light_propagates = readU8(is);
889                 sunlight_propagates = readU8(is);
890                 walkable = readU8(is);
891                 pointable = readU8(is);
892                 diggable = readU8(is);
893                 climbable = readU8(is);
894                 buildable_to = readU8(is);
895                 deSerializeString(is); // legacy: used to be metadata_name
896                 liquid_type = (enum LiquidType)readU8(is);
897                 liquid_alternative_flowing = deSerializeString(is);
898                 liquid_alternative_source = deSerializeString(is);
899                 liquid_viscosity = readU8(is);
900                 light_source = readU8(is);
901                 damage_per_second = readU32(is);
902                 node_box.deSerialize(is);
903                 selection_box.deSerialize(is);
904                 legacy_facedir_simple = readU8(is);
905                 legacy_wallmounted = readU8(is);
906                 deSerializeSimpleSoundSpec(sound_footstep, is);
907                 deSerializeSimpleSoundSpec(sound_dig, is);
908                 deSerializeSimpleSoundSpec(sound_dug, is);
909         }
910         else
911         {
912                 throw SerializationError("unsupported ContentFeatures version");
913         }
914 }
915