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