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