Increment protocol version
[oweals/minetest.git] / src / nodedef.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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) const
111 {
112         writeU8(os, 0); // version
113         os<<serializeString(name);
114         writeU8(os, animation.type);
115         writeU16(os, animation.aspect_w);
116         writeU16(os, animation.aspect_h);
117         writeF1000(os, animation.length);
118 }
119
120 void TileDef::deSerialize(std::istream &is)
121 {
122         int version = readU8(is);
123         if(version != 0)
124                 throw SerializationError("unsupported TileDef version");
125         name = deSerializeString(is);
126         animation.type = (TileAnimationType)readU8(is);
127         animation.aspect_w = readU16(is);
128         animation.aspect_h = readU16(is);
129         animation.length = readF1000(is);
130 }
131
132 /*
133         SimpleSoundSpec serialization
134 */
135
136 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
137                 std::ostream &os)
138 {
139         os<<serializeString(ss.name);
140         writeF1000(os, ss.gain);
141 }
142 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
143 {
144         ss.name = deSerializeString(is);
145         ss.gain = readF1000(is);
146 }
147
148 /*
149         ContentFeatures
150 */
151
152 ContentFeatures::ContentFeatures()
153 {
154         reset();
155 }
156
157 ContentFeatures::~ContentFeatures()
158 {
159 }
160
161 void ContentFeatures::reset()
162 {
163         /*
164                 Cached stuff
165         */
166 #ifndef SERVER
167         solidness = 2;
168         visual_solidness = 0;
169         backface_culling = true;
170 #endif
171         has_on_construct = false;
172         has_on_destruct = false;
173         has_after_destruct = false;
174         /*
175                 Actual data
176                 
177                 NOTE: Most of this is always overridden by the default values given
178                       in builtin.lua
179         */
180         name = "";
181         groups.clear();
182         // Unknown nodes can be dug
183         groups["dig_immediate"] = 2;
184         drawtype = NDT_NORMAL;
185         visual_scale = 1.0;
186         for(u32 i=0; i<6; i++)
187                 tiledef[i] = TileDef();
188         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
189                 tiledef_special[j] = TileDef();
190         alpha = 255;
191         post_effect_color = video::SColor(0, 0, 0, 0);
192         param_type = CPT_NONE;
193         param_type_2 = CPT2_NONE;
194         is_ground_content = false;
195         light_propagates = false;
196         sunlight_propagates = false;
197         walkable = true;
198         pointable = true;
199         diggable = true;
200         climbable = false;
201         buildable_to = false;
202         liquid_type = LIQUID_NONE;
203         liquid_alternative_flowing = "";
204         liquid_alternative_source = "";
205         liquid_viscosity = 0;
206         light_source = 0;
207         damage_per_second = 0;
208         node_box = NodeBox();
209         selection_box = NodeBox();
210         legacy_facedir_simple = false;
211         legacy_wallmounted = false;
212         sound_footstep = SimpleSoundSpec();
213         sound_dig = SimpleSoundSpec("__group");
214         sound_dug = SimpleSoundSpec();
215 }
216
217 void ContentFeatures::serialize(std::ostream &os)
218 {
219         writeU8(os, 5); // version
220         os<<serializeString(name);
221         writeU16(os, groups.size());
222         for(ItemGroupList::const_iterator
223                         i = groups.begin(); i != groups.end(); i++){
224                 os<<serializeString(i->first);
225                 writeS16(os, i->second);
226         }
227         writeU8(os, drawtype);
228         writeF1000(os, visual_scale);
229         writeU8(os, 6);
230         for(u32 i=0; i<6; i++)
231                 tiledef[i].serialize(os);
232         writeU8(os, CF_SPECIAL_COUNT);
233         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
234                 tiledef_special[i].serialize(os);
235         }
236         writeU8(os, alpha);
237         writeU8(os, post_effect_color.getAlpha());
238         writeU8(os, post_effect_color.getRed());
239         writeU8(os, post_effect_color.getGreen());
240         writeU8(os, post_effect_color.getBlue());
241         writeU8(os, param_type);
242         writeU8(os, param_type_2);
243         writeU8(os, is_ground_content);
244         writeU8(os, light_propagates);
245         writeU8(os, sunlight_propagates);
246         writeU8(os, walkable);
247         writeU8(os, pointable);
248         writeU8(os, diggable);
249         writeU8(os, climbable);
250         writeU8(os, buildable_to);
251         os<<serializeString(""); // legacy: used to be metadata_name
252         writeU8(os, liquid_type);
253         os<<serializeString(liquid_alternative_flowing);
254         os<<serializeString(liquid_alternative_source);
255         writeU8(os, liquid_viscosity);
256         writeU8(os, light_source);
257         writeU32(os, damage_per_second);
258         node_box.serialize(os);
259         selection_box.serialize(os);
260         writeU8(os, legacy_facedir_simple);
261         writeU8(os, legacy_wallmounted);
262         serializeSimpleSoundSpec(sound_footstep, os);
263         serializeSimpleSoundSpec(sound_dig, os);
264         serializeSimpleSoundSpec(sound_dug, os);
265 }
266
267 void ContentFeatures::deSerialize(std::istream &is)
268 {
269         int version = readU8(is);
270         if(version != 5)
271                 throw SerializationError("unsupported ContentFeatures version");
272         name = deSerializeString(is);
273         groups.clear();
274         u32 groups_size = readU16(is);
275         for(u32 i=0; i<groups_size; i++){
276                 std::string name = deSerializeString(is);
277                 int value = readS16(is);
278                 groups[name] = value;
279         }
280         drawtype = (enum NodeDrawType)readU8(is);
281         visual_scale = readF1000(is);
282         if(readU8(is) != 6)
283                 throw SerializationError("unsupported tile count");
284         for(u32 i=0; i<6; i++)
285                 tiledef[i].deSerialize(is);
286         if(readU8(is) != CF_SPECIAL_COUNT)
287                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
288         for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
289                 tiledef_special[i].deSerialize(is);
290         alpha = readU8(is);
291         post_effect_color.setAlpha(readU8(is));
292         post_effect_color.setRed(readU8(is));
293         post_effect_color.setGreen(readU8(is));
294         post_effect_color.setBlue(readU8(is));
295         param_type = (enum ContentParamType)readU8(is);
296         param_type_2 = (enum ContentParamType2)readU8(is);
297         is_ground_content = readU8(is);
298         light_propagates = readU8(is);
299         sunlight_propagates = readU8(is);
300         walkable = readU8(is);
301         pointable = readU8(is);
302         diggable = readU8(is);
303         climbable = readU8(is);
304         buildable_to = readU8(is);
305         deSerializeString(is); // legacy: used to be metadata_name
306         liquid_type = (enum LiquidType)readU8(is);
307         liquid_alternative_flowing = deSerializeString(is);
308         liquid_alternative_source = deSerializeString(is);
309         liquid_viscosity = readU8(is);
310         light_source = readU8(is);
311         damage_per_second = readU32(is);
312         node_box.deSerialize(is);
313         selection_box.deSerialize(is);
314         legacy_facedir_simple = readU8(is);
315         legacy_wallmounted = readU8(is);
316         deSerializeSimpleSoundSpec(sound_footstep, is);
317         deSerializeSimpleSoundSpec(sound_dig, is);
318         deSerializeSimpleSoundSpec(sound_dug, is);
319         // If you add anything here, insert it primarily inside the try-catch
320         // block to not need to increase the version.
321         try{
322         }catch(SerializationError &e) {};
323 }
324
325 /*
326         CNodeDefManager
327 */
328
329 class CNodeDefManager: public IWritableNodeDefManager
330 {
331 public:
332         void clear()
333         {
334                 m_name_id_mapping.clear();
335                 m_name_id_mapping_with_aliases.clear();
336
337                 for(u16 i=0; i<=MAX_CONTENT; i++)
338                 {
339                         ContentFeatures &f = m_content_features[i];
340                         f.reset(); // Reset to defaults
341                 }
342                 
343                 // Set CONTENT_AIR
344                 {
345                         ContentFeatures f;
346                         f.name = "air";
347                         f.drawtype = NDT_AIRLIKE;
348                         f.param_type = CPT_LIGHT;
349                         f.light_propagates = true;
350                         f.sunlight_propagates = true;
351                         f.walkable = false;
352                         f.pointable = false;
353                         f.diggable = false;
354                         f.buildable_to = true;
355                         // Insert directly into containers
356                         content_t c = CONTENT_AIR;
357                         m_content_features[c] = f;
358                         addNameIdMapping(c, f.name);
359                 }
360                 // Set CONTENT_IGNORE
361                 {
362                         ContentFeatures f;
363                         f.name = "ignore";
364                         f.drawtype = NDT_AIRLIKE;
365                         f.param_type = CPT_NONE;
366                         f.light_propagates = false;
367                         f.sunlight_propagates = false;
368                         f.walkable = false;
369                         f.pointable = false;
370                         f.diggable = false;
371                         // A way to remove accidental CONTENT_IGNOREs
372                         f.buildable_to = true;
373                         // Insert directly into containers
374                         content_t c = CONTENT_IGNORE;
375                         m_content_features[c] = f;
376                         addNameIdMapping(c, f.name);
377                 }
378         }
379         // CONTENT_IGNORE = not found
380         content_t getFreeId(bool require_full_param2)
381         {
382                 // If allowed, first search in the large 4-bit-param2 pool
383                 if(!require_full_param2){
384                         for(u16 i=0x800; i<=0xfff; i++){
385                                 const ContentFeatures &f = m_content_features[i];
386                                 if(f.name == "")
387                                         return i;
388                         }
389                 }
390                 // Then search from the small 8-bit-param2 pool
391                 for(u16 i=0; i<=125; i++){
392                         const ContentFeatures &f = m_content_features[i];
393                         if(f.name == "")
394                                 return i;
395                 }
396                 return CONTENT_IGNORE;
397         }
398         CNodeDefManager()
399         {
400                 clear();
401         }
402         virtual ~CNodeDefManager()
403         {
404         }
405         virtual IWritableNodeDefManager* clone()
406         {
407                 CNodeDefManager *mgr = new CNodeDefManager();
408                 for(u16 i=0; i<=MAX_CONTENT; i++)
409                 {
410                         mgr->set(i, get(i));
411                 }
412                 return mgr;
413         }
414         virtual const ContentFeatures& get(content_t c) const
415         {
416                 assert(c <= MAX_CONTENT);
417                 return m_content_features[c];
418         }
419         virtual const ContentFeatures& get(const MapNode &n) const
420         {
421                 return get(n.getContent());
422         }
423         virtual bool getId(const std::string &name, content_t &result) const
424         {
425                 std::map<std::string, content_t>::const_iterator
426                         i = m_name_id_mapping_with_aliases.find(name);
427                 if(i == m_name_id_mapping_with_aliases.end())
428                         return false;
429                 result = i->second;
430                 return true;
431         }
432         virtual content_t getId(const std::string &name) const
433         {
434                 content_t id = CONTENT_IGNORE;
435                 getId(name, id);
436                 return id;
437         }
438         virtual void getIds(const std::string &name, std::set<content_t> &result)
439                         const
440         {
441                 if(name.substr(0,6) != "group:"){
442                         content_t id = CONTENT_IGNORE;
443                         if(getId(name, id))
444                                 result.insert(id);
445                         return;
446                 }
447                 std::string group = name.substr(6);
448                 for(u16 id=0; id<=MAX_CONTENT; id++)
449                 {
450                         const ContentFeatures &f = m_content_features[id];
451                         if(f.name == "") // Quickly discard undefined nodes
452                                 continue;
453                         if(itemgroup_get(f.groups, group) != 0)
454                                 result.insert(id);
455                 }
456         }
457         virtual const ContentFeatures& get(const std::string &name) const
458         {
459                 content_t id = CONTENT_IGNORE;
460                 getId(name, id);
461                 return get(id);
462         }
463         // IWritableNodeDefManager
464         virtual void set(content_t c, const ContentFeatures &def)
465         {
466                 verbosestream<<"registerNode: registering content id \""<<c
467                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
468                 assert(c <= MAX_CONTENT);
469                 // Don't allow redefining CONTENT_IGNORE (but allow air)
470                 if(def.name == "ignore" || c == CONTENT_IGNORE){
471                         infostream<<"registerNode: WARNING: Ignoring "
472                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
473                         return;
474                 }
475                 // Check that the special contents are not redefined as different id
476                 // because it would mess up everything
477                 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
478                         (def.name == "air" && c != CONTENT_AIR)){
479                         errorstream<<"registerNode: IGNORING ERROR: "
480                                         <<"trying to register built-in type \""
481                                         <<def.name<<"\" as different id"<<std::endl;
482                         return;
483                 }
484                 m_content_features[c] = def;
485                 if(def.name != "")
486                         addNameIdMapping(c, def.name);
487         }
488         virtual content_t set(const std::string &name,
489                         const ContentFeatures &def)
490         {
491                 assert(name == def.name);
492                 u16 id = CONTENT_IGNORE;
493                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
494                 if(!found){
495                         // Determine if full param2 is required
496                         bool require_full_param2 = (
497                                 def.param_type_2 == CPT2_FULL
498                                 ||
499                                 def.param_type_2 == CPT2_FLOWINGLIQUID
500                                 ||
501                                 def.legacy_wallmounted
502                         );
503                         // Get some id
504                         id = getFreeId(require_full_param2);
505                         if(id == CONTENT_IGNORE)
506                                 return CONTENT_IGNORE;
507                         if(name != "")
508                                 addNameIdMapping(id, name);
509                 }
510                 set(id, def);
511                 return id;
512         }
513         virtual content_t allocateDummy(const std::string &name)
514         {
515                 assert(name != "");
516                 ContentFeatures f;
517                 f.name = name;
518                 return set(name, f);
519         }
520         virtual void updateAliases(IItemDefManager *idef)
521         {
522                 std::set<std::string> all = idef->getAll();
523                 m_name_id_mapping_with_aliases.clear();
524                 for(std::set<std::string>::iterator
525                                 i = all.begin(); i != all.end(); i++)
526                 {
527                         std::string name = *i;
528                         std::string convert_to = idef->getAlias(name);
529                         content_t id;
530                         if(m_name_id_mapping.getId(convert_to, id))
531                         {
532                                 m_name_id_mapping_with_aliases.insert(
533                                                 std::make_pair(name, id));
534                         }
535                 }
536         }
537         virtual void updateTextures(ITextureSource *tsrc)
538         {
539 #ifndef SERVER
540                 infostream<<"CNodeDefManager::updateTextures(): Updating "
541                                 <<"textures in node definitions"<<std::endl;
542
543                 bool new_style_water = g_settings->getBool("new_style_water");
544                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
545                 bool opaque_water = g_settings->getBool("opaque_water");
546                 
547                 for(u16 i=0; i<=MAX_CONTENT; i++)
548                 {
549                         ContentFeatures *f = &m_content_features[i];
550                         
551                         // Figure out the actual tiles to use
552                         TileDef tiledef[6];
553                         for(u32 j=0; j<6; j++)
554                         {
555                                 tiledef[j] = f->tiledef[j];
556                                 if(tiledef[j].name == "")
557                                         tiledef[j].name = "unknown_block.png";
558                         }
559
560                         switch(f->drawtype){
561                         default:
562                         case NDT_NORMAL:
563                                 f->solidness = 2;
564                                 break;
565                         case NDT_AIRLIKE:
566                                 f->solidness = 0;
567                                 break;
568                         case NDT_LIQUID:
569                                 assert(f->liquid_type == LIQUID_SOURCE);
570                                 if(opaque_water)
571                                         f->alpha = 255;
572                                 if(new_style_water){
573                                         f->solidness = 0;
574                                 } else {
575                                         f->solidness = 1;
576                                         if(f->alpha == 255)
577                                                 f->solidness = 2;
578                                         f->backface_culling = false;
579                                 }
580                                 break;
581                         case NDT_FLOWINGLIQUID:
582                                 assert(f->liquid_type == LIQUID_FLOWING);
583                                 f->solidness = 0;
584                                 if(opaque_water)
585                                         f->alpha = 255;
586                                 break;
587                         case NDT_GLASSLIKE:
588                                 f->solidness = 0;
589                                 f->visual_solidness = 1;
590                                 break;
591                         case NDT_ALLFACES:
592                                 f->solidness = 0;
593                                 f->visual_solidness = 1;
594                                 break;
595                         case NDT_ALLFACES_OPTIONAL:
596                                 if(new_style_leaves){
597                                         f->drawtype = NDT_ALLFACES;
598                                         f->solidness = 0;
599                                         f->visual_solidness = 1;
600                                 } else {
601                                         f->drawtype = NDT_NORMAL;
602                                         f->solidness = 2;
603                                         for(u32 i=0; i<6; i++){
604                                                 tiledef[i].name += std::string("^[noalpha");
605                                         }
606                                 }
607                                 break;
608                         case NDT_TORCHLIKE:
609                         case NDT_SIGNLIKE:
610                         case NDT_PLANTLIKE:
611                         case NDT_FENCELIKE:
612                         case NDT_RAILLIKE:
613                         case NDT_NODEBOX:
614                                 f->solidness = 0;
615                                 break;
616                         }
617
618                         // Tiles (fill in f->tiles[])
619                         for(u16 j=0; j<6; j++){
620                                 // Texture
621                                 f->tiles[j].texture = tsrc->getTexture(tiledef[j].name);
622                                 // Alpha
623                                 f->tiles[j].alpha = f->alpha;
624                                 if(f->alpha == 255)
625                                         f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
626                                 else
627                                         f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
628                                 // Material flags
629                                 f->tiles[j].material_flags = 0;
630                                 if(f->backface_culling)
631                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
632                                 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
633                                         f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
634                                 // Animation parameters
635                                 if(f->tiles[j].material_flags &
636                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
637                                 {
638                                         // Get raw texture size to determine frame count by
639                                         // aspect ratio
640                                         video::ITexture *t = tsrc->getTextureRaw(tiledef[j].name);
641                                         v2u32 size = t->getOriginalSize();
642                                         int frame_height = (float)size.X /
643                                                         (float)tiledef[j].animation.aspect_w *
644                                                         (float)tiledef[j].animation.aspect_h;
645                                         int frame_count = size.Y / frame_height;
646                                         int frame_length_ms = 1000.0 *
647                                                         tiledef[j].animation.length / frame_count;
648                                         f->tiles[j].animation_frame_count = frame_count;
649                                         f->tiles[j].animation_frame_length_ms = frame_length_ms;
650
651                                         // If there are no frames for an animation, switch
652                                         // animation off (so that having specified an animation
653                                         // for something but not using it in the texture pack
654                                         // gives no overhead)
655                                         if(frame_count == 1){
656                                                 f->tiles[j].material_flags &=
657                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
658                                         }
659                                 }
660                         }
661                         // Special tiles (fill in f->special_tiles[])
662                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
663                                 // Texture
664                                 f->special_tiles[j].texture =
665                                                 tsrc->getTexture(f->tiledef_special[j].name);
666                                 // Alpha
667                                 f->special_tiles[j].alpha = f->alpha;
668                                 if(f->alpha == 255)
669                                         f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
670                                 else
671                                         f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
672                                 // Material flags
673                                 f->special_tiles[j].material_flags = 0;
674                                 if(f->tiledef_special[j].backface_culling)
675                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
676                                 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
677                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
678                                 // Animation parameters
679                                 if(f->special_tiles[j].material_flags &
680                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
681                                 {
682                                         // Get raw texture size to determine frame count by
683                                         // aspect ratio
684                                         video::ITexture *t = tsrc->getTextureRaw(f->tiledef_special[j].name);
685                                         v2u32 size = t->getOriginalSize();
686                                         int frame_height = (float)size.X /
687                                                         (float)f->tiledef_special[j].animation.aspect_w *
688                                                         (float)f->tiledef_special[j].animation.aspect_h;
689                                         int frame_count = size.Y / frame_height;
690                                         int frame_length_ms = 1000.0 *
691                                                         f->tiledef_special[j].animation.length / frame_count;
692                                         f->special_tiles[j].animation_frame_count = frame_count;
693                                         f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
694
695                                         // If there are no frames for an animation, switch
696                                         // animation off (so that having specified an animation
697                                         // for something but not using it in the texture pack
698                                         // gives no overhead)
699                                         if(frame_count == 1){
700                                                 f->special_tiles[j].material_flags &=
701                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
702                                         }
703                                 }
704                         }
705                 }
706 #endif
707         }
708         void serialize(std::ostream &os)
709         {
710                 writeU8(os, 1); // version
711                 u16 count = 0;
712                 std::ostringstream os2(std::ios::binary);
713                 for(u16 i=0; i<=MAX_CONTENT; i++)
714                 {
715                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
716                                 continue;
717                         ContentFeatures *f = &m_content_features[i];
718                         if(f->name == "")
719                                 continue;
720                         writeU16(os2, i);
721                         // Wrap it in a string to allow different lengths without
722                         // strict version incompatibilities
723                         std::ostringstream wrapper_os(std::ios::binary);
724                         f->serialize(wrapper_os);
725                         os2<<serializeString(wrapper_os.str());
726                         count++;
727                 }
728                 writeU16(os, count);
729                 os<<serializeLongString(os2.str());
730         }
731         void deSerialize(std::istream &is)
732         {
733                 clear();
734                 int version = readU8(is);
735                 if(version != 1)
736                         throw SerializationError("unsupported NodeDefinitionManager version");
737                 u16 count = readU16(is);
738                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
739                 for(u16 n=0; n<count; n++){
740                         u16 i = readU16(is2);
741                         if(i > MAX_CONTENT){
742                                 errorstream<<"ContentFeatures::deSerialize(): "
743                                                 <<"Too large content id: "<<i<<std::endl;
744                                 continue;
745                         }
746                         /*// Do not deserialize special types
747                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
748                                 continue;*/
749                         ContentFeatures *f = &m_content_features[i];
750                         // Read it from the string wrapper
751                         std::string wrapper = deSerializeString(is2);
752                         std::istringstream wrapper_is(wrapper, std::ios::binary);
753                         f->deSerialize(wrapper_is);
754                         verbosestream<<"deserialized "<<f->name<<std::endl;
755                         if(f->name != "")
756                                 addNameIdMapping(i, f->name);
757                 }
758         }
759 private:
760         void addNameIdMapping(content_t i, std::string name)
761         {
762                 m_name_id_mapping.set(i, name);
763                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
764         }
765 private:
766         // Features indexed by id
767         ContentFeatures m_content_features[MAX_CONTENT+1];
768         // A mapping for fast converting back and forth between names and ids
769         NameIdMapping m_name_id_mapping;
770         // Like m_name_id_mapping, but only from names to ids, and includes
771         // item aliases too. Updated by updateAliases()
772         // Note: Not serialized.
773         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
774 };
775
776 IWritableNodeDefManager* createNodeDefManager()
777 {
778         return new CNodeDefManager();
779 }
780